ti1220 lecture 14: domain-specific languages
DESCRIPTION
TRANSCRIPT
![Page 1: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/1.jpg)
TI1220 2012-2013Concepts of Programming Languages
Eelco Visser / TU Delft
Lecture 14: Domain-Specific Languages
![Page 2: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/2.jpg)
Syntax and SemanticsNames, Bindings, and Scopes Storage Data TypesFunctional ProgrammingFirst-class FunctionsPolymorphism
Type ParameterizationParsing and InterpretationData Abstraction / Modular ProgrammingFunctional Programming ReduxConcurrencyConcurrent ProgrammingDomain-Specific Languages
Quarter 3
Quarter 4
Basics ofScalaJavaScriptC
![Page 3: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/3.jpg)
Linguistic Abstraction
Formalizing Design Patterns
![Page 4: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/4.jpg)
ProblemDomain
SolutionDomain
implement
validate
Software Engineering
![Page 5: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/5.jpg)
Software Reuse: Don’t Repeat Yourself
software reuse patterns
• copy and paste
• libraries
• frameworks
• service APIs
• design patterns
![Page 6: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/6.jpg)
Linguistic Abstraction
identify pattern
use new abstraction
language A language Bdesign abstraction
![Page 7: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/7.jpg)
From Instructions to Expressions
mov &a, &cadd &b, &cmov &a, &t1sub &b, &t1and &t1,&c
Source: http://sites.google.com/site/arch1utep/home/course_outline/translating-complex-expressions-into-assembly-language-using-expression-trees
c = ac += bt1 = at1 -= bc &= t1
c = (a + b) & (a - b)
![Page 8: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/8.jpg)
From Calling Conventions to Procedures
calc: push eBP ; save old frame pointer mov eBP,eSP ; get new frame pointer sub eSP,localsize ; reserve place for locals . . ; perform calculations, leave result in AX . mov eSP,eBP ; free space for locals pop eBP ; restore old frame pointer ret paramsize ; free parameter space and return
f(e1,e2,...,en)
push eAX ; pass some register resultpush byte[eBP+20] ; pass some memory variable (FASM/TASM syntax)push 3 ; pass some constantcall calc ; the returned result is now in eAX
f(x) { ... }
http://en.wikipedia.org/wiki/Calling_convention
![Page 9: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/9.jpg)
A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling. (Structures are called ``records'' in some languages, notably Pascal.)
struct point { int x; int y;};
member
structure tag
Structures in C: Abstract from Memory Layout
![Page 10: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/10.jpg)
Malloc/Free to Automatic Memory Management
/* Allocate space for an array with ten elements of type int. */
int *ptr = (int*)malloc(10 * sizeof (int));if (ptr == NULL) { /* Memory could not be allocated, the program should handle the error here as appropriate. */} else { /* Allocation succeeded. Do something. */ free(ptr); /* We are done with the int objects, and free the associated pointer. */ ptr = NULL; /* The pointer must not be used again, unless re-assigned to using malloc again. */}
http://en.wikipedia.org/wiki/Malloc
int [] = new int[10];/* use it; gc will clean up (hopefully) */
![Page 11: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/11.jpg)
typedef struct Base { void* (**vtable)(); int x;} Base;
void (*Base_Vtable[])() = { &Base_print };
Base* newBase(int v) { Base* obj = (Base*)malloc(sizeof(Base)); obj->vtable = Base_Vtable; obj->x = v; return obj;}
void print(Base* obj) { obj->vtable[0](obj);}
class Base { Integer x; public Base(Integer v) { x = v; } public void print() { System.out.println("Base: " + x); }}class Child extends Base { Integer y; public Child(Integer v1, Integer v2) { super(v1); y = v2; } public void print() { System.out.println("Child: (" + x + "," + y + ")"); }}
Dynamic Dispatch
![Page 12: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/12.jpg)
Polymorphic Higher-Order Functions
def map[A,B](f: A => B, xs: List[A]): List[B] = { xs match{ case Nil() => Nil() case Cons(y, ys) => Cons(f(y), map(f, ys)) } }
def incList(xs: IntList): IntList = xs match { case Nil() => Nil() case Cons(y, ys) => Cons(y + 1, incList(ys)) }
![Page 13: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/13.jpg)
Abstractions in Programming Languages
❖ Structured control-flow
★ if-then-else, while
❖ Procedural abstraction
★ procedures, first-class functions (closures)
❖ Memory management
★ garbage collection
❖ Data abstraction
★ abstract data types, objects
❖ Modules
★ inheritance, traits, mixins
![Page 14: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/14.jpg)
“A programming language is low level when its programs require attention to the irrelevant”
Alan J. Perlis. Epigrams on Programming. SIGPLAN Notices, 17(9):7-13, 1982.
![Page 15: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/15.jpg)
Do HLLs eliminate all irrelevant details?
What about
❖ data persistence
❖ data services
❖ concurrency
❖ distribution
❖ access control
❖ data invariants
❖ workflow
❖ ...
![Page 16: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/16.jpg)
Do HLLs eliminate all irrelevant details?
What about
❖ data persistence
❖ data services
❖ concurrency
❖ distribution
❖ access control
❖ data invariants
❖ workflow
❖ ...
many of these concerns require
programmatic encodings
![Page 17: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/17.jpg)
What is the Next Level of Abstraction?
ProblemDomain HLL Machine
![Page 18: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/18.jpg)
Domain-Specific Languages
ProblemDomain HLL MachineDSL
![Page 19: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/19.jpg)
Example: Encoding Units
compiler
computerinput
input distance : Float;input duration : Float;output speed : Float := duration / distance;
output
![Page 20: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/20.jpg)
Example: Encoding Units
compiler
computerinput
input distance : Float;input duration : Float;output speed : Float := duration / distance;
error
wrong output
![Page 21: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/21.jpg)
Impact of Software Errors
compiler
computer
error
Mars Climate OrbiterUnit mismatch: Orbiter variables in Newtons, Ground control software in Pound-force.
Damage: ~350 M$
input distance : Float;input duration : Float;output speed : Float := duration / distance;
wrong output
![Page 22: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/22.jpg)
Example: Explicit Representation of Units
computer
input distance : Meter;input duration : Second;output speed : Meter/Second := duration / distance;
compiler
formalize knowledge of application area (domain) in language
error
![Page 23: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/23.jpg)
DSLs Provide Domain-Specific ...
Abstractions
★ directly represent domain concepts
Concrete syntax
★ natural notation
Optimization
★ based on domain assumptions
Error checking
★ report errors in terms of domain concepts
Tool support
★ interpreter, compiler, code generator, IDE
![Page 24: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/24.jpg)
Internal DSL
Library in HLL
★ Haskell, Scala, Ruby, ...
★ API is language
★ language features for ‘linguistic abstraction’
Advantages
★ host language = implementation language
Disadvantages
★ host language = implementation language (encoding)
★ no portability
★ no domain-specific errors, analysis, optimization
![Page 25: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/25.jpg)
External DSL
Dedicated language
★ independent of host/target language (portable)
★ implementation with interpreter or compiler
Advantages
★ language tuned to domain
★ domain-specific errors, analysis, optimizations
Disadvantages
★ cost of learning new language
★ cost of maintaining language
![Page 26: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/26.jpg)
Example DSLs (1)
Spreadsheet
★ formulas, macros
Querying
★ SQL, XQuery, XPath
Graph layout
★ GraphViz
Web
★ HTML, CSS, RSS, XML, XSLT
★ Ruby/Rails, JSP, ASP, JSF, WebDSL
![Page 27: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/27.jpg)
Example DSLs (2)
Games
★ Lua, UnrealScript
Modeling
★ UML, OCL, QVT
Language engineering
★ YACC, LEX, RegExp, ANTLR, SDF
★ TXL, ASF+SDF, Stratego
![Page 28: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/28.jpg)
Example: Linguistic Integration in
WebDSL
![Page 29: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/29.jpg)
browser server database
web app
Web Programming
![Page 30: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/30.jpg)
browser server database
Java SQLHTML, JS, CSS
Web Programming = Distributed Programming
![Page 31: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/31.jpg)
Concerns in Web Programming
Data Persistence
Access Control
Injection Attacks
Search
XSS
Data Validation
Data Binding
Routing
... ...
![Page 32: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/32.jpg)
Zef Hemel, Danny M. Groenewegen, Lennart C. L. Kats, Eelco Visser. Static consistency checking of web applications with WebDSL. Journal of Symbolic Computation, 46(2):150-182, 2011.
Late Failure Detection in Web Applications
![Page 33: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/33.jpg)
Complexity in Web Programming:
Multiple Languages x Multiple Concerns
Consistency not statically checked
![Page 34: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/34.jpg)
Eelco Visser. WebDSL: A Case Study in Domain-Specific Language Engineering. In Ralf Lämmel, Joost Visser, João Saraiva, editors, Generative and Transformational Techniques in Software Engineering II, International Summer School, GTTSE 2007. Volume 5235 of Lecture Notes in Computer Science, pages 291-373, Springer, Braga, Portugal, 2007.
Separation of Concerns & Linguistic Integration
![Page 35: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/35.jpg)
![Page 36: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/36.jpg)
Formalizing Navigation Logic
![Page 37: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/37.jpg)
http://eelcovisser.org/blog/post/42/dsl-engineering
“http://” + “eelcovisser.org” // host domain + “blog” // application name + “post” // page+ “42” // identity+ “dsl-engineering” // title
![Page 38: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/38.jpg)
page blog(b: Blog, index: Int) { main(b){ for(p: Post in b.recentPosts(index,5)) { section{ header{ navigate post(p) { output(p.title) } } par{ output(p.content) } par{ output(p.created.format("MMMM d, yyyy")) } } }}page post(p: Post) { ... }
Statically checked navigation
![Page 39: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/39.jpg)
entity Blog { key :: String (id) title :: String (name) posts -> Set<Post> (inverse=Post.blog) function recentPosts(index: Int, n: Int): List<Post> { var i := max(1,index) - 1; return [p | p: Post in posts order by p.created desc limit n offset i*n].list(); }}entity Post { key :: String (id) title :: String (name, searchable) content :: WikiText (searchable) blog -> Blog }
Persistent Data Models
Generation of queries: no injection attacks
![Page 40: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/40.jpg)
entity Assignment { key :: String (id) title :: String (name, searchable) shortTitle :: String description :: WikiText (searchable) course -> CourseEdition (searchable) weight :: Float (default=1.0) deadline :: DateTime (default=null) // ...} page assignment(assign: Assignment, tab: String) { main{ progress(assign, tab) pageHeader{ output(assign.title) breadcrumbs(assign) } // ... } }
Persistent variables in WebDSL
http://department.st.ewi.tudelft.nl/weblab/assignment/752
objects are automatically persisted in database
1
2
3
![Page 41: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/41.jpg)
page post(p: Post) { ... }page editpost(p: Post) { action save() { return blog(p); } main(p.blog){ form{ formEntry("Title"){ input(p.title) } formEntry("Content") { input(p.content) } formEntry("Posted") { input(p.created) } submit save() { "Save" } } }}
Forms & Data Binding
No separate controller!
![Page 42: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/42.jpg)
access control rules
principal is User with credentials username, password rule page blog(b: Blog, index: Int) { true } rule page post(p: Post) { p.public || p.author == principal } rule page editpost(p: Post) { principal == p.author }
extend entity User { password :: Secret}
extend entity Blog { owner -> User}
extend entity Post { public :: Bool}
Declarative Access Control Rules
![Page 43: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/43.jpg)
Linguistically Integrated
Persistent data model
Logic
Templates (UI, Email, Service)
Data binding
Access control
Data validation
Faceted search
Collaborative filtering
![Page 44: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/44.jpg)
DSL Summary
software reuse through linguistic abstraction
• capture understanding of design patterns in language concepts
• abstract from accidental complexity
• program in terms of domain concepts
• automatically generate implementation
![Page 45: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/45.jpg)
When to Use/Create DSLs?
Hierarchy of abstractions
• first understand how to program it
• make variations by copy, paste, adapt
• (avoid over-engineering)
• make library of frequently used patterns
• find existing (internal) DSLs for the domain
Time for a DSL?
• large class of applications using same design patterns
• design patterns cannot be captured in PL
• lack of checking / optimization for DSL abstractions
![Page 46: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/46.jpg)
Language Engineering
![Page 47: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/47.jpg)
object ExpParser extends JavaTokenParsers with PackratParsers { lazy val exp: PackratParser[Exp] = (exp <~ "+") ~ exp1 ^^ { case lhs~rhs => Add(lhs, rhs) } | exp1
lazy val exp1: PackratParser[Exp] = (exp1 ~ exp0) ^^ { case lhs~rhs => App(lhs, rhs) } | exp0 lazy val exp0: PackratParser[Exp] = number | identifier | function | letBinding | "(" ~> exp <~ ")" // ... def parse(text: String) = parseAll(exp, text)}
syntax through parsers
![Page 48: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/48.jpg)
sealed abstract class Valuecase class numV(n: Int) extends Valuecase class closureV(param: Symbol, body: Exp, env: Env) extends Value
def eval(exp: Exp, env: Env): Value = exp match { case Num(v) => numV(v) case Add(l, r) => plus(eval(l, env), eval(r, env)) case Id(name) => lookup(name, env) case Let(name, e1, e2) => eval(e2, bind(name, eval(e1, env), env))
case Fun(name, body) => closureV(name, body, env)
case App(fun, arg) => eval(fun, env) match { case closureV(name, body, env2) => eval(body, bind(name, eval(arg, env), env2)) case _ => sys.error("Closure expected") }
} semantics through interpreter
![Page 49: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/49.jpg)
Traditional Compilers
![Page 50: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/50.jpg)
Traditional Compilers
ls
Course.java
![Page 51: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/51.jpg)
Traditional Compilers
ls
Course.java
javac -verbose Course.java
[parsing started Course.java][parsing completed 8ms][loading java/lang/Object.class(java/lang:Object.class)][checking university.Course][wrote Course.class][total 411ms]
![Page 52: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/52.jpg)
Traditional Compilers
ls
Course.java
javac -verbose Course.java
[parsing started Course.java][parsing completed 8ms][loading java/lang/Object.class(java/lang:Object.class)][checking university.Course][wrote Course.class][total 411ms]
ls
Course.class Course.java
![Page 53: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/53.jpg)
Language Processors
syntax analysis
• parsing
• AST construction
static analysis
• name analysis
• type analysis
semantics
• generation
• interpretation
![Page 54: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/54.jpg)
Integrated Development Environments (IDE)
![Page 55: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/55.jpg)
Modern Compilers in IDEs
syntactic editor services
• syntax checking
• syntax highlighting
• outline view
• code folding
• bracket matching
semantic editor services
• error checking
• reference resolving
• hover help
• content completion
• refactoring
![Page 56: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/56.jpg)
Eclipse Platform
runtime platform
• composition
• integration
development platform
• complex APIs
• abstractions for Eclipse IDEs
• concepts: editors, views, label provider, label provider factory, …
• tedious, boring, frustrating
![Page 57: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/57.jpg)
Spoofax Language Workbench
declarative meta-languages
• syntax definition
• editor services
• term rewriting
implementation
• generic integration into Eclipse and IMP
• compilation & interpretation of language definitions
agile
• Spoofax & IDE under development in same Eclipse instance
• support for test-driven development
![Page 58: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/58.jpg)
A Taste of Language Engineeringwith Spoofax
• abstract syntax trees
• declarative syntax definition
• name binding and scope
• transformation by term rewriting
![Page 59: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/59.jpg)
EnFun: Entities with Functions
module blog entity String { function plus(that:String): String } entity Bool { } entity Set<T> { function add(x: T) function remove(x: T) function member(x: T): Bool } entity Blog { posts : Set<Post> function newPost(): Post { var p : Post := Post.new(); posts.add(p); } } entity Post { title : String }
![Page 60: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/60.jpg)
Structure: Abstract Syntax
![Page 61: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/61.jpg)
Signature & Terms
constructors Module : ID * List(Definition) -> Module Imports : ID -> Definition
Module( "application", [Imports("library"), Imports("users"), Imports("frontend")])
![Page 62: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/62.jpg)
Entities & Properties
constructors Entity : ID * List(Property) -> Definition Type : ID -> Type New : Type -> Exp
constructors Property : ID * Type -> Property This : Exp PropAccess : Exp * ID -> Exp
Module("users", [ Imports("library") , Entity("User" , [ Property("email", Type("String")) , Property("password", Type("String")) , Property("isAdmin", Type("Bool"))])])
![Page 63: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/63.jpg)
Parsing: From Text to Structure
![Page 64: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/64.jpg)
Declarative Syntax Definition
Entity("User", [ Property("first", Type("String")), Property("last", Type("String"))])
signature constructors Entity : ID * List(Property) -> Definition Type : ID -> Type Property : ID * Type -> Property
![Page 65: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/65.jpg)
Declarative Syntax Definition
entity User { first : String last : String}
Entity("User", [ Property("first", Type("String")), Property("last", Type("String"))])
signature constructors Entity : ID * List(Property) -> Definition Type : ID -> Type Property : ID * Type -> Property
![Page 66: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/66.jpg)
Declarative Syntax Definition
entity User { first : String last : String}
Entity("User", [ Property("first", Type("String")), Property("last", Type("String"))])
signature constructors Entity : ID * List(Property) -> Definition Type : ID -> Type Property : ID * Type -> Property
context-free syntax "entity" ID "{" Property* "}" -> Definition {"Entity"} ID -> Type {"Type"} ID ":" Type -> Property {"Property"}
![Page 67: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/67.jpg)
Prototyping Syntax Definition
![Page 68: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/68.jpg)
Context-free Syntax
constructors True : Exp False : Exp Not : Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And"} Exp "||" Exp -> Exp {"Or"}
![Page 69: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/69.jpg)
Lexical Syntax
constructors True : Exp False : Exp Not : Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And"} Exp "||" Exp -> Exp {"Or"}
lexical syntax [a-zA-Z][a-zA-Z0-9]* -> ID "-"? [0-9]+ -> INT [\ \t\n\r] -> LAYOUT
constructors : String -> ID : String -> INT
scannerless generalized (LR) parsing
form of tokens (words, lexemes)
![Page 70: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/70.jpg)
Ambiguity
constructors True : Exp False : Exp Not : Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And"} Exp "||" Exp -> Exp {"Or"}
isPublic || isDraft && (author == principal())
![Page 71: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/71.jpg)
Ambiguity
constructors True : Exp False : Exp Not : Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And"} Exp "||" Exp -> Exp {"Or"}
isPublic || isDraft && (author == principal())
amb([ And(Or(Var("isPublic"), Var("isDraft")), Eq(Var("author"), ThisCall("principal", []))), Or(Var("isPublic"), And(Var("isDraft"), Eq(Var("author"), ThisCall("principal", []))))])
![Page 72: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/72.jpg)
Disambiguation by Encoding Precedence
constructors True : Exp False : Exp Not : Exp -> Exp And : Exp * Exp -> Exp Or : Exp * Exp -> Exp
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And"} Exp "||" Exp -> Exp {"Or"}
context-free syntax "(" Exp ")" -> Exp0 {bracket} "true" -> Exp0 {"True"} "false" -> Exp0 {"False"} Exp0 -> Exp1 "!" Exp0 -> Exp1 {"Not"} Exp1 -> Exp2 Exp1 "&&" Exp2 -> Exp2 {"And"} Exp2 -> Exp3 Exp2 "||" Exp3 -> Exp3 {"Or"}
![Page 73: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/73.jpg)
Declarative Disambiguation
context-free syntax "true" -> Exp {"True"} "false" -> Exp {"False"} "!" Exp -> Exp {"Not"} Exp "&&" Exp -> Exp {"And", left} Exp "||" Exp -> Exp {"Or", left} "(" Exp ")" -> Exp {bracket}context-free priorities {left: Exp.Not} > {left: Exp.Mul} > {left: Exp.Plus Exp.Minus} > {left: Exp.And} > {non-assoc: Exp.Eq Exp.Lt Exp.Gt Exp.Leq Exp.Geq}
isPublic || isDraft && (author == principal())
Or(Var("isPublic"), And(Var("isDraft"), Eq(Var("author"), ThisCall("principal", []))))
![Page 74: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/74.jpg)
Analysis: Name Resolution
+
![Page 75: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/75.jpg)
Definitions and References
module test
entity String { }
entity User { first : String last : String }
definition
reference
![Page 76: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/76.jpg)
Name Binding in IDE
![Page 77: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/77.jpg)
From Tree to Graph
Module( "test", [ Entity("String", []) , Entity( "User" , [ Property("first", ) , Property("last", ) ] ) ])
![Page 78: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/78.jpg)
NaBL: Name Binding Language
module names
imports include/Cam namespaces Type Property Function Variable rules
Entity(name, None(), None(), _): defines Type name of type Type(name, []) scopes Type, Function, Property, Variable
Type(name, _): refers to Type name
![Page 79: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/79.jpg)
Transformation
![Page 80: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/80.jpg)
Transformation by Strategic Rewriting
rules desugar: Plus(e1, e2) -> MethCall(e1, "plus", [e2]) desugar: Or(e1, e2) -> MethCall(e1, "or", [e2])
desugar : VarDeclInit(x, t, e) -> Seq([VarDecl(x, t), Assign(Var(x), e)])
strategies desugar-all = topdown(repeat(desugar))
![Page 81: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/81.jpg)
Return-Lifting Applied
function fact(n: Int): Int { var res: Int; if(n == 0) { res := 1; } else { res := this * fact(n - 1); } return res;}
function fact(n: Int): Int {
if(n == 0) { return 1; } else { return this * fact(n - 1); }
}
![Page 82: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/82.jpg)
Return-Lifting Rules
rules lift-return-all = alltd(lift-return; normalize-all) lift-return : FunDef(x, arg*, Some(t), stat1*) -> FunDef(x, arg*, Some(t), Seq([ VarDecl(y, t), Seq(stat2*), Return(Var(y)) ])) where y := <new>; stat2* := <alltd(replace-return(|y))> stat1* replace-return(|y) : Return(e) -> Assign(y, e)
![Page 83: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/83.jpg)
Language Engineering Summary
apply linguistic abstraction to language engineering
• declarative languages for language definition
• automatic derivation of efficient compilers
• automatic derivation of IDEs
![Page 84: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/84.jpg)
Research Agenda
![Page 85: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/85.jpg)
Example: Explicit Representation of Units
computer
input distance : Meter;input duration : Second;output speed : Meter/Second := duration / distance;
compiler
formalize knowledge of application area (domain) in language
error
![Page 86: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/86.jpg)
error
Problem: Correctness of Language Definitions
computer
compilerCan we trust the compiler?
wrong outputinput
program
type soundness: well-typed programs don’t go wrong
![Page 87: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/87.jpg)
compiler
error
Challenge: Automatic Verification of Correctness
computer
compiler
wrong output
program
type soundness: well-typed programs don’t go wrong
typechecker
codegenerator
input
![Page 88: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/88.jpg)
CorrectnessProof
Language Workbench
State-of-the-Art: Language Engineering
SyntaxChecker
NameResolver
TypeChecker
CodeGenerator
focus on implementation; not suitable for verification
CompilerEditor(IDE) Tests
![Page 89: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/89.jpg)
Formal Language Specification
State-of-the-Art: Semantics Engineering
AbstractSyntax
TypeSystem
DynamicSemantics Transforms
focus on (only semi-automatic) verification; not suitable for implementation
CorrectnessProof TestsCompiler
Editor(IDE)
![Page 90: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/90.jpg)
Declarative Language Definition
My Approach: Multi-Purpose Language Definitions
SyntaxDefinition
NameBinding
TypeSystem
DynamicSemantics Transforms
CompilerEditor(IDE)
CorrectnessProof Tests
bridging the gap between language engineering and semantics engineering
![Page 91: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/91.jpg)
Software Development on the Web
revisiting the architecture of the IDE
![Page 92: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/92.jpg)
Exam
![Page 93: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/93.jpg)
Syntax and SemanticsNames, Bindings, and Scopes Storage Data TypesFunctional ProgrammingFirst-class FunctionsPolymorphism
Type ParameterizationParsing and InterpretationData Abstraction / Modular ProgrammingFunctional Programming ReduxConcurrencyConcurrent ProgrammingDomain-Specific Languages
Quarter 3
Quarter 4
Basics ofScalaJavaScriptC
![Page 94: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/94.jpg)
Material for exam
Slides from lectures
Tutorial exercises
Graded assignments
Sebesta: Chapters 1-13, 15
Programming in Scala: Chapters 1, 4-16, 19, 32-33
K&R C: Chapters 1-6
JavaScript Good Parts: Chapters 1-4
![Page 95: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/95.jpg)
Content of exam
10% multiple choice questions about concepts
50% Scala programming (functional programming)
20% C programming (structures and pointers)
20% JavaScript programming (objects and prototypes)
![Page 96: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/96.jpg)
Registration for Exam is Required
http://department.st.ewi.tudelft.nl/weblab/assignment/761 -> your submission
![Page 97: TI1220 Lecture 14: Domain-Specific Languages](https://reader033.vdocuments.us/reader033/viewer/2022052504/5482063eb4af9f960d8b46a8/html5/thumbnails/97.jpg)
Good Luck!