software language design & engineering: mobl & spoofax

76
Eelco Visser http://eelcovisser.org Software Language Design & Engineering Microsoft Research, Redmond October 20, 2011

Upload: eelco-visser

Post on 05-Dec-2014

1.588 views

Category:

Technology


6 download

DESCRIPTION

Slides for talk at Microsoft Research about Mobl and Spoofax projects.

TRANSCRIPT

Page 1: Software Language Design & Engineering: Mobl & Spoofax

Eelco Visserhttp://eelcovisser.org

Software Language Design & Engineering

Microsoft Research, RedmondOctober 20, 2011

Page 2: Software Language Design & Engineering: Mobl & Spoofax

bridging the gap between problem domain and solution domain

Software Engineering

ProblemDomain Machine

Page 3: Software Language Design & Engineering: Mobl & Spoofax

HLLs reduce gap

High-Level Languages

ProblemDomain HLL Machine

"A programming language is low level when its programs require attention to the irrelevant." Alan J. Perlis (1982)

Page 4: Software Language Design & Engineering: Mobl & Spoofax

domain-specific languages support more specialization

Domain-Specific Languages

ProblemDomain HLL MachineDSL

Page 5: Software Language Design & Engineering: Mobl & Spoofax

Software Language Design & Engineering

enable software engineers to effectively design, implement, and apply domain-specific

software languages

Page 6: Software Language Design & Engineering: Mobl & Spoofax

Research: Software Language Engineering

automatically derive efficient, scalable, incremental compiler +

usable IDE from high-level,

declarative language definition

Page 7: Software Language Design & Engineering: Mobl & Spoofax

creating full featured IDEs for domain-specific languages

The Spoofax Language Workbench

Page 8: Software Language Design & Engineering: Mobl & Spoofax

Research: Software Language Design

systematically design domain-specific

software languages with optimal tradeoff between

expressivity, completeness, portability, coverage, and maintainability

Page 9: Software Language Design & Engineering: Mobl & Spoofax

Software Language Design Case Studies

Mobl: client-side stateful web applications

WebDSL: server-side restful web applications

Page 10: Software Language Design & Engineering: Mobl & Spoofax

Two Talks

Declaratively Programming the Mobile Web with Mobl

The Spoofax Language Workbench

Page 11: Software Language Design & Engineering: Mobl & Spoofax

Eelco Visserhttp://eelcovisser.org

Declaratively Programming the Mobile Web with Mobl*

* To appear: SPLASH/OOPSLA, October 2011, Portland

Zef Hemelhttp://zef.me

Page 12: Software Language Design & Engineering: Mobl & Spoofax

Native Applications not Portable

Divergence in Mobile Platforms

Objective-C Java J2ME/C++

HTML/Javascript Java .NET

Page 13: Software Language Design & Engineering: Mobl & Spoofax

Convergence in Mobile Platform

Webkit browser

Webkit browser

Webkit browser

Webkit browser

Page 14: Software Language Design & Engineering: Mobl & Spoofax

The Universal Userinterface Engine

Page 15: Software Language Design & Engineering: Mobl & Spoofax

Mobile Web Architecture

Page 16: Software Language Design & Engineering: Mobl & Spoofax

Rich Applications

WebDatabases

Location information (GPS)

Canvas

Multi-touch

Offline support

Full-screen support

Accelerator support

Audio

Page 17: Software Language Design & Engineering: Mobl & Spoofax

Native Applications

Address book

Camera

Compass

File IO

Notifications

Page 18: Software Language Design & Engineering: Mobl & Spoofax

MVC, No Integration, No Abstraction, Accidental Complexity

Software Engineering with JavaScript

annotated HTML imperative Javascript

Page 20: Software Language Design & Engineering: Mobl & Spoofax
Page 21: Software Language Design & Engineering: Mobl & Spoofax

declarativetyped

integrated concise

http://www.mobl-lang.org

Page 22: Software Language Design & Engineering: Mobl & Spoofax

Web Application with Touch

Page 23: Software Language Design & Engineering: Mobl & Spoofax

Portable Applications

Page 24: Software Language Design & Engineering: Mobl & Spoofax

Mobl Architecture

Page 25: Software Language Design & Engineering: Mobl & Spoofax

tipcalculator.mobl

application tipcalculator

import mobl::ui::generic

screen root() { var amount = 20 var percentage = 10 header("Tip calculator") group { item { numField(amount, label="amount") } item { numField(percentage, label="percentage") } item { "$" label(Math.round(amount * (1 + percentage/100))) } }}

Page 26: Software Language Design & Engineering: Mobl & Spoofax

Model-View Pattern

Page 27: Software Language Design & Engineering: Mobl & Spoofax

Task Manager

Page 28: Software Language Design & Engineering: Mobl & Spoofax

HTML5 Data Persistence

Data Model

entity Task { name : String (searchable) done : Bool due : DateTime category : Category (inverse: tasks) tags : Collection<Tag> (inverse: tasks)}entity Category { name : String tasks : Collection<Task> (inverse: category)}entity Tag { name : String tasks : Collection<Task> (inverse: tags)}

Page 29: Software Language Design & Engineering: Mobl & Spoofax

statically typed: catch errors early

Logic

entity Task { ... function postpone(days : Num) { this.due = DateTime.create( this.due.getFullYear(), this.due.getMonth(), this.due.getDate() + days); } function import(user : String, pw : String) { var tasksJSON = httpRequest("/export?user="+ user + "&pw=" + pw); foreach(t in tasksJSON) { add(Task.fromSelectJSON(t)); } }}

Page 30: Software Language Design & Engineering: Mobl & Spoofax

screen root() { var phrase = "" header("Tasks") { button("Add", onclick={ addTask(); }) } searchBox(phrase) group { list(t in Task.search(phrase) limit 20){ item { checkBox(t.done, label=t.name) } } }}

Reactive User Interfaces

Page 31: Software Language Design & Engineering: Mobl & Spoofax

screen root() { var phrase = "" header("Tasks") { button("Add", onclick={ addTask(); }) } searchBox(phrase) group { list(t in Task.search(phrase) limit 20){ item { checkBox(t.done, label=t.name) } } }}

screen addTask() { var t = Task() header("Add") { button("Done", onclick={ add(t); screen return; }) } textField(t.name) datePicker(t.due)}

Navigation

Page 32: Software Language Design & Engineering: Mobl & Spoofax

Continuations

screen root() { button("Ask", onclick={ alert("Hello " + prompt("First name") + " " + prompt("Last name")); })}screen prompt(question : String) : String { var answer = "" header(question) { button("Done", onclick={ screen return answer; }) } textField(answer)}

Page 33: Software Language Design & Engineering: Mobl & Spoofax

User Interface Idiom: Tab

Reusable Controls

screen root() { tabSet([("One", tab1), ("Two", tab2)], defaultTab="One")} control tab1() { header("Tab 1") label("This is tab 1")}control tab2() { header("Tab 2") label("This is tab 2")}

Page 34: Software Language Design & Engineering: Mobl & Spoofax

increase coverage: developers can create abstractions

Tab Set: Higher-Order Control

control tabSet(tabs : [(String,Control)], activeTab : String) { list((tabName, tabControl) in tabs) { block(onclick={ activeTab = tabName; }, style=activeTab==tabName ? activeTabButton : inactiveTabButton) { label(tabName) } } list((tabName, tabControl) in tabs) { block(activeTab==tabName ? visibleTab : invisibleTab) { tabControl() } }}

Page 35: Software Language Design & Engineering: Mobl & Spoofax

User Interface Idiom: Master Detail

screen root() { header("Tasks") masterDetail( Task.all() order by due desc, taskItem, taskDetail)} control taskItem(t : Task) { checkBox(t.done, label=t.name)}control taskDetail(t : Task) { textField(t.name) datePicker(t.due)}

Page 36: Software Language Design & Engineering: Mobl & Spoofax

Master Detail: Higher-Order Control

control masterDetail(items : Collection<?>, masterItem : Control1<?>, detail : Control1<?>) { group { list(it in items) { item(onclick={ detailScreen(it,detail); }) { masterItem(it) } } }}screen detailScreen(it : ?, detail : Control1<?>) { header("Detail") { backButton() } detail(it)}

Page 37: Software Language Design & Engineering: Mobl & Spoofax

Adaptive Layout

Page 38: Software Language Design & Engineering: Mobl & Spoofax

applications

Page 39: Software Language Design & Engineering: Mobl & Spoofax

Mobl Applications

Page 40: Software Language Design & Engineering: Mobl & Spoofax

GR8 Conference Program

Page 41: Software Language Design & Engineering: Mobl & Spoofax

mPodder

Page 42: Software Language Design & Engineering: Mobl & Spoofax

Ken

http://itsneh.com/ken/

Page 43: Software Language Design & Engineering: Mobl & Spoofax

Mobl Summary

❖ Linguistic integration

★ data model, user interface, styling, application logic

❖ Domain abstractions

★ reduce accidental complexity, platform details

❖ User-defined abstractions

★ data binding

★ reactive programming

★ reusable controls

❖ Model-View

★ automate the controller

Page 44: Software Language Design & Engineering: Mobl & Spoofax

Constructed on Spoofax Language Workbench

Mobl IDE: Static Cross-Concern Consistency Checking

Page 45: Software Language Design & Engineering: Mobl & Spoofax

Eelco Visserhttp://eelcovisser.org

The Spoofax Language Workbench*Rules for Declarative Specification of Languages and IDEs

* OOPSLA 2010

Lennart Katshttp://www.lclnet.nl

Page 46: Software Language Design & Engineering: Mobl & Spoofax

Research: Software Language Engineering

Automatically derive efficient, scalable, incremental compiler +

usable IDE from high-level,

declarative language definition

Page 47: Software Language Design & Engineering: Mobl & Spoofax

Language Definitions for Compilers

Syntax definition★ concrete syntax★ abstract syntax

Static semantics★ error checking★ name resolution★ type analysis

Model-to-model transformation★ express constructs in core language

Code generation★ translate core language models to implementation

Page 48: Software Language Design & Engineering: Mobl & Spoofax

Editor Services for Modern IDEs

Syntactic Editor Services★ syntax checking ★ bracket matching★ syntax highlighting★ code folding★ outline view

Semantic Editor Services★ error checking★ reference resolving★ hover help★ code completion★ refactoring

Page 49: Software Language Design & Engineering: Mobl & Spoofax

Syntax Definition with SDF

Page 50: Software Language Design & Engineering: Mobl & Spoofax

character-level grammars: integration of lexical and context-free syntax

Declarative Syntax Definition

lexical syntax [a-zA-Z\_\$][a-zA-Z0-9\_]* -> ID

context-free syntax STRING -> LimitedSetExp {cons("String")} NUMBER -> LimitedSetExp {cons("Num")} LimitedSetExp -> Exp QId -> LimitedExp {cons("Var")} "(" Exp ")" -> LimitedExp {cons("Brackets")} Exp BoolMethodId Exp -> Exp {cons("BinMethodCall"), left} Exp CompareMethodId Exp -> Exp {cons("BinMethodCall"), left} Exp TermOperatorMethodId Exp -> Exp {cons("BinMethodCall"), left} Exp OperatorMethodId Exp -> Exp {cons("BinMethodCall"), left}

Page 51: Software Language Design & Engineering: Mobl & Spoofax

develop and use language in same environment

An Interactive Language Workbench

Page 52: Software Language Design & Engineering: Mobl & Spoofax

Debugging Ambiguities

Page 53: Software Language Design & Engineering: Mobl & Spoofax

Declarative Disambiguation

context-free priorities Exp "." ID -> LimitedExp > Exp "." ID "(" {NamedExp ","}* ")" -> LimitedExp > Exp TermOperatorMethodId Exp -> Exp > Exp OperatorMethodId Exp -> Exp > Exp CompareMethodId Exp -> Exp > Exp BoolMethodId Exp -> Exp > "!" Exp -> Exp > Exp "?" Exp ":" Exp -> Exp > LimitedExp Filter+ -> Exp

Page 54: Software Language Design & Engineering: Mobl & Spoofax

Parsing after Disambiguation

Page 55: Software Language Design & Engineering: Mobl & Spoofax

permissive grammar + backtracking + region discovery [*OOPSLA & SLE 2009]

Natural and Flexible Error Recovery*

Page 56: Software Language Design & Engineering: Mobl & Spoofax

Transformation with Stratego

Page 57: Software Language Design & Engineering: Mobl & Spoofax

Syntactic Normalization and Desugaring

rules

normalize : FunctionNoReturnType(manno*, name, farg*, stat*) -> Function(manno*, name, farg*, SimpleType(QId("mobl", "void")), stat*)

normalize : IfNoElse(e, block) -> If(e, block, Block([]))

desugar : ForInferred(lvalue, e, elem*) -> For(lvalue, t, e, elem*) where GenericType(_, [t]) := <type-of> e

Page 58: Software Language Design & Engineering: Mobl & Spoofax

CPS Transform

cps-lift-exprs : [Return(e)|stats] -> <concat>[stats2, [Return(e2)|<cps-lift-expressions> stats]] where not(<is-sync> e) with {| Exp : stats2 := <cps-lift-expression> e ; e2 := <Exp> |}

Page 59: Software Language Design & Engineering: Mobl & Spoofax

Type Analysis

eq-type-of : String(_) -> SimpleType(QId("mobl", "String"))

eq-type-of : FieldAccess(e, x) -> t where Property(_, _, t, _) := <lookup-property> (<type-of> e, x)

Page 60: Software Language Design & Engineering: Mobl & Spoofax

origin tracking

Type Checking

constraint-error : t@SimpleType(_) -> (t, $[Type is not defined: [<pp-mobl-type> t]]) where not(<lookup-type> t)

constraint-error : t@FieldAccess(e, x) -> (t, $[Property [x] not defined]) where <type-of> e where not(type-of)

Page 61: Software Language Design & Engineering: Mobl & Spoofax

Semantic Editor Services

editor-analyze: (ast, path, project-path) -> (ast2, errors, warnings, notes) with // ...

editor-complete-proposal : SimpleType(COMPLETION(_)) -> proposals where all-types := <get-all-types>; proposals := <map(type-name-to-proposal); flatten-list> all-types

editor-hover: (t@SimpleType(_), position, ast, path, project-path) -> <get-doc> <lookup-type> t2 where t2 := <lookup-node> (position, ast)

editor-resolve: (t@SimpleType(qid), position, ast, path, project-path) -> target where target := <ensure-origin(lookup-type|qid)> t

Page 62: Software Language Design & Engineering: Mobl & Spoofax

Connect transformations to IDE

Editor Services Bindings

module MoBL-Builders

imports MoBL-Builders.generated

builders provider : include/mobl.jar observer : editor-analyze on save : generate-artifacts builder : "Show ATerm" = generate-aterm (openeditor) (realtime) (meta) builder : "Format code" = format-code (openeditor) (realtime) builder : "Desugar" = editor-desugar (openeditor) (realtime) (meta)

Page 63: Software Language Design & Engineering: Mobl & Spoofax

Includes testing of the editor services [* OOPSLA 2011]

Integrated Language Definition Testing*

Page 64: Software Language Design & Engineering: Mobl & Spoofax

http://researchr.org/search/publication/mobl+spoofax+webdsl

Software Language Design & Engineering

http://spoofax.org

http://mobl-lang.org

http://webdsl.org

http://researchr.org

http://eelcovisser.org

separation of concerns + linguistic integration

cross concern consistency checking

early detection of failures

Linguistic abstraction:capture software knowledge in

domain-specific languages

Language workbench: DSL design and implementation with less effort than

traditional language engineering

Page 65: Software Language Design & Engineering: Mobl & Spoofax

behind the scenes

Page 66: Software Language Design & Engineering: Mobl & Spoofax

var results = Task.all().list();for(var i = 0; i < results.length; i++) { alert(results[i].name);}

synchronous programming

Page 67: Software Language Design & Engineering: Mobl & Spoofax

render page

query database and process

results

...

time

Page 68: Software Language Design & Engineering: Mobl & Spoofax

render page

query database and process

results

...

timebrowser freeze

Page 69: Software Language Design & Engineering: Mobl & Spoofax

render page

send query

...

process query result

...

time

Page 70: Software Language Design & Engineering: Mobl & Spoofax

Task.all.list(function(results) { for(var i = 0; i < results.length; i++) { alert(results[i].name); } });...

asynchronous programming

Page 71: Software Language Design & Engineering: Mobl & Spoofax

Task.all().list(function(results) { alert("Hello, "); });alert("world!");

breaks sequential execution assumption

Page 72: Software Language Design & Engineering: Mobl & Spoofax

Task.all().list(function(results) { // make changes ... persistence.flush(function() { alert("Changes saved!"); });});

Page 73: Software Language Design & Engineering: Mobl & Spoofax

continuation-passing styletransformation

Page 74: Software Language Design & Engineering: Mobl & Spoofax

function displayLocationAndReturn() : Coordinates { var position = mobl::location::getPosition(); log("Lat: " + position.latitude); log("Long: " + position.longitude); return position;}

function displayLocationAndReturn(callback) { mobl.location.getPosition(function(position) { console.log("Lat: " + position.latitude); console.log("Long: " + position.longitude); callback(position); });};

Page 75: Software Language Design & Engineering: Mobl & Spoofax

function displayLocationAndReturn() : Coordinates { var position = mobl::location::getPosition(); log("Lat: " + position.latitude); log("Long: " + position.longitude); return position;}

function displayLocationAndReturn(callback) { mobl.location.getPosition(function(position) { console.log("Lat: " + position.latitude); console.log("Long: " + position.longitude); callback(position); });};

Page 76: Software Language Design & Engineering: Mobl & Spoofax

function displayLocationAndReturn() : Coordinates { var position = mobl::location::getPosition(); log("Lat: " + position.latitude); log("Long: " + position.longitude); return position;}

function displayLocationAndReturn(callback) { mobl.location.getPosition(function(position) { console.log("Lat: " + position.latitude); console.log("Long: " + position.longitude); callback(position); });};