Exploring the flexibility of Scala Implicits towards an ... Exploring the flexibility of Scala

  • View
    214

  • Download
    0

Embed Size (px)

Text of Exploring the flexibility of Scala Implicits towards an ... Exploring the...

FACULDADE DE ENGENHARIA DA UNIVERSIDADE DO PORTO

Exploring the flexibility of ScalaImplicits towards an Extensible Live

Environment

Vasco Andr Costa Grilo

Master in Informatics and Computing Engineering

Supervisor: Hugo Jos Sereno Lopes Ferreira (PhD)

Second Supervisor: Tiago Boldt Sousa

February 13th, 2013

Exploring the flexibility of Scala Implicits towards anExtensible Live Environment

Vasco Andr Costa Grilo

Master in Informatics and Computing Engineering

Approved in oral examination by the committee:

Chair: Prof. Doutor Lus Filipe Pinto de Almeida Teixeira

External Examiner: Prof. Doutor Maximino Esteves Correia BessaSupervisor: Prof. Doutor Hugo Jos Sereno Lopes FerreiraFebruary 13th, 2013

Abstract

This thesis presents the research and implementation details of an extensible live developmentenvironment for the Scala programming language called Visual Scala. It also describes the ex-ploration of the flexibility of Scalas implicits towards such an environment. Scala is an Object-Functional programming language created by Martin Odersky which has been growing in popu-larity over the past few years.

When talking about development environments which support dynamic execution of code dur-ing runtime probably the most famous example are Read-Eval-Print Loops. REPLs are interactivetop-level environments usually associated with dynamic or functional languages. Although theyprovide quick ways of inspecting and exploring a certain programming language, they stick to ver-bose textual representations of the intermediate values that are being generated by the programmer.We feel such an approach is rather limiting to our understanding of data structures and the effectsof computation and so we propose to develop a development environment for Scala built on topof the Scalas standard REPLs with an associated library for converting type values to graphicalrepresentations.

Using the power of Scalas Implicits conversions we want to explore how flexible they canbe when implementing an innovative mechanism of converting data structures to rich, interactivevisualizations. As a web application, benefiting from HTML, CSS and JavaScript, along with thepower of D3, a library for easily create graphical representations of data, Visual Scala offers aneasily extensible and innovative conversions library for Scalas native type values. In this way, endusers can interact with Visual Scala in a command-line fashion, with expressions resulting in richgraphical widgets instead of verbose text.

From reviewing the state of the art we find that there are similar tools which provide graphicalrepresentations of data structures, however they are usually intended for more technical and math-ematical computing. The Julia Language is a very interesting example of such tools, although itprovides graphical conversions in an explicit way and does not allow for new conversions to becreated by the end-user. We feel that such a component, implicitly generating these graphics, fora more general purpose programming language, like Scala, is an interesting subject to explore andcan have a significant impact on the community.

From implementing and exploring Visual Scala we conclude that Scalas Implicits providehigh flexibility for developing a mechanism of invoking the right conversion for the right typevalue at hand. The resulting Conversions Library apart from being extremely flexible, is also easilyextensible as it is not part of the running backend system and instead an external resource compo-nent. By evaluating user code by using the standard Scalas interpreter, Visual Scala also providesa live environment which maximizes graphical conversion of intermediate data throughout de-velopment.

i

ii

Resumo

Esta tese descreve o estudo e os detalhes de implementao de um ambiente de desenvolvimentovivo e extensvel para a linguagem de programao Scala, chamado de Visual Scala. Descrevetambm o processo de explorao da flexibilidade dos Implcitos de Scala quando se pretendecaminhar em direco a um ambiente desta natureza. Scala uma linguagem de programaoque mistura dois paradigmas, Orientado a Objectos e Funcional, crescendo em popularidade nosltimos anos.

Quando se fala em ambientes de desenvolvimento que suportam a execuo dinmica decdigo durante o seu runtime, o exemplo mais famoso so os Read-Eval-Print Loops. REPLs,ou Interpretadores, so ambientes interactivos de alto-nvel, frequentemente associados a lingua-gens dinmicas ou funcionais. Embora ofeream um ambiente de rpida inspeo e explorao deuma linguagem de programao, restringem-se a apresentar os resultados e as estruturas de dadosem formatos textuais bastante simples. Ns sentimos que esta aproximao bastante limitativana nossa percepo e anlise das estruturas de dados e dos efeitos da computao sobre elas ecomo tal, propomos criar um ambiente para Scala que, construdo com base no interpretador deScala, oferece uma biblioteca de converses grficas de resultados.

Utilizando o poder das converses implcitas de Scala, pretendemos explorar o quo flexvelelas so quando se implementa um mecanismo inovador de converter tipos de dados para repre-sentaes grficas, ricas e interactivas. Apresentando-se como uma aplicao Web, beneficiandodo HTML, CSS e JavaScript, juntamente com o poder de D3 (uma biblioteca de fcil gerao derepresentaes grficas de dados), Visual Scala oferece uma inovadora e extensvel biblioteca deconverses grficas. Desta forma, os utilizadores podem interagir com o Visual Scala submetendoexpresso a expresso, sendo que estas produziro resultados grficos em vez de apenas texto.

Da reviso do estado da arte descobrimos que j existem projectos similares, que oferecemrepresentaes grficas de dados, no entanto, estas ferramentas esto geralmente associadas acontextos de computao tecnica ou matemtica. The Julia Language um exemplo bastante in-teressante de tal ambiente, no entanto, este tipo de ferramentas oferece representaes grficas deforma explcita e no permite a criao de novas converses, por parte do utilizador. Ns acred-itamos que esta componente de converso implcita, dirigida a uma linguagem de programaode um mbito mais geral, um tema bastante interessante de ser explorado e pode criar algumimpacto na comunidade.

Da implementao e explorao do Visual Scala, conclumos que os Implcitos de Scala per-mitem construir um mecanismo de invocar correctamente a converso apropriada para o tipo dedados a converter de uma forma altamente flexvel. A Biblioteca de Converses resultante, paraalm de ser flexvel, tambm facilmente extensvel pois ela no parte integrante do sistemabackend do Visual Scala. Ela faz parte do conjunto externo de recursos, em que modificaesproduzem efeitos imediatos na componente de converso. Como a interpretao de cdigo feitarecorrendo ao interpretador oficial de Scala, Visual Scala tambm oferece um ambiente vivo quemaximiza a converso grfica de resultados intermdios enquanto se escreve cdigo.

iii

iv

Contents

1 Introduction 11.1 Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 Outline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2 State of the Art 52.1 Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.1.1 Type Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.1.2 Static vs Dynamic Languages . . . . . . . . . . . . . . . . . . . . . . . 62.1.3 Software Development Environments . . . . . . . . . . . . . . . . . . . 72.1.4 Read-Eval-Print Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.1.5 Reflection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.1.6 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . 102.1.7 Scala . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.2 Related Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.2.1 Scala Worksheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122.2.2 Simply Scala . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.2.3 Scripster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.2.4 The Julia Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142.2.5 Online Python Tutor . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

3 Designing Visual Scala 193.1 Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

3.1.1 Functional . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.1.2 Usability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

3.2 The approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.2.1 Methodology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213.2.2 Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

4 Implementation 254.1 High-Level Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.2 Frontend (HTML/CSS/JavaScript) . . . . . . . . . . . . . . . . . . . . . . . . . 25

4.2.1 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.3 Backend (Scala) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

4.3.1 The Scala Web Server . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.3.2 Evaluation Module . .