[ 1 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 Banana Algebra: Jacob Andersen [ ] Aarhus University Claus Brabrand.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Path-Sensitive Analysis for Linear Arithmetic and Uninterpreted Functions SAS 2004 Sumit Gulwani George Necula EECS Department University of California,
Simplifications of Context-Free Grammars
1
Logical Model and Specification of Usage Control Xinwen Zhang, Jaehong Park Francesco Parisi-Presicce, Ravi Sandhu George Mason University.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
David Burdett May 11, 2004 Package Binding for WS CDL.
Introduction to Algorithms 6.046J/18.401J
We need a common denominator to add these fractions.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Programming Language Concepts
Chapter 2-2 A Simple One-Pass Compiler
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
EE, NCKU Tien-Hao Chang (Darby Chang)
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Modern Programming Languages, 2nd ed.
3 Logic The Study of What’s True or False or Somewhere in Between.
Semantic Analysis and Symbol Tables
EIS Bridge Tool and Staging Tables September 1, 2009 Instructor: Way Poteat Slide: 1.
LIAL HORNSBY SCHNEIDER
Outline Minimum Spanning Tree Maximal Flow Algorithm LP formulation 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
CS 6143 COMPUTER ARCHITECTURE II SPRING 2014 ACM Principles and Practice of Parallel Programming, PPoPP, 2006 Panel Presentations Parallel Processing is.
Operating Systems Operating Systems - Winter 2010 Chapter 3 – Input/Output Vrije Universiteit Amsterdam.
Lexical Analysis Arial Font Family.
[ 1 ] Claus Brabrand, ITU BANANA ALGEBRA March 28, 2009 ETAPS/LDTA, York Banana Algebra: Jacob Andersen [ ] Aarhus University Claus Brabrand.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Microsoft Research March 20, 2000 A Programming Language for Developing Interactive Web Services Claus Brabrand BRICS, University of Aarhus, Denmark.
Joint work with Andre Lieutier Dassault Systemes Domain Theory and Differential Calculus Abbas Edalat Imperial College Oxford.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Adding Up In Chunks.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
1 Termination and shape-shifting heaps Byron Cook Microsoft Research, Cambridge Joint work with Josh Berdine, Dino Distefano, and.
25 seconds left…...
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
18-Dec-14 Pruning. 2 Exponential growth How many leaves are there in a complete binary tree of depth N? This is easy to demonstrate: Count “going left”
Essential Cell Biology
Clock will move after 1 minute
Chapter Eleven: Non-Regular Languages
Introduction to Recursion and Recursive Algorithms
Select a time to count down from the clock above
Claus Brabrand, UFPE, Brazil Aug 11, 2010DATA-FLOW ANALYSIS Claus Brabrand ((( ))) Associate Professor, Ph.D. ((( Programming, Logic, and.
From Model-based to Model-driven Design of User Interfaces.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
The Pumping Lemma for CFL’s
4/11/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
SAT Solver CS 680 Formal Methods Jeremy Johnson. 2 Disjunctive Normal Form  A Boolean expression is a Boolean function  Any Boolean function can be.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
8. Introduction to Denotational Semantics. © O. Nierstrasz PS — Denotational Semantics 8.2 Roadmap Overview:  Syntax and Semantics  Semantics of Expressions.
Xsugar: Dual Syntax for XML LanguagesTōkyō Daigaku [ July 15, 2005 ] / 401 xsugar Dual Syntax for XML Languages Claus Brabrand Anders Møller Michael Schwartzbach.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
PEPM 2002 Growing Languages with Metamorphic Syntax Macros January 14, 2002 Growing Languages with Metamorphic Syntax Macros Claus Brabrand Michael Schwartzbach.
Chapter 3 – Describing Syntax
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Presentation transcript:

[ 1 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 Banana Algebra: Jacob Andersen [ ] Aarhus University Claus Brabrand [ ] IT University of Copenhagen Syntactic Language Extension via an Algebra of Languages and Transformations

[ 2 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Abstract We propose an algebra of languages and transformations as a means for extending languages syntactically. The algebra provides a layer of high-level abstractions built on top of languages (captured by CFGs) and transformations (captured by constructive catamorphisms). The algebra is self-contained in that any term of the algebra specifying a transformation can be reduced to a constant catamorphism, before the transformation is run. Thus, the algebra comes "for free" without sacrificing the strong safety and efficiency properties of constructive catamorphisms. The entire algebra as presented in the paper is implemented as the Banana Algebra Tool which may be used to syntactically extend languages in an incremental and modular fashion via algebraic composition of previously defined languages and transformations. We demonstrate and evaluate the tool via several kinds of extensions.

[ 3 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark The Banana Algebra Café The "Banana Algebra Café": Located in Costa Rica City of Cahuita (pop. 3,000): Cahuita ! Banana Breakfast! Banana Breakfast!

[ 4 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Introduction: "What is a Banana?" Bananas for Language Transformation Language Extension Pattern Banana Algebra Examples Implementation Related Work Conclusion Outline

[ 5 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark What is a 'Banana' ? Datatype; "list": Banana ("sum-of-list"): Implicit recursion on input structure Separation of recursion and evaluation bottom-up re-combination of intermediate results list = Num int | Cons int * list [Num n] = n [Cons n l] = n + [l] list  int (aka. "Catamorphism" ) (| n.n, (n, l ).n+ l |) Another Ex.: "length-of-list"

[ 6 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Language Transformation Bananas (statically typed): Source language: ' L S ' Target language: ' L T ' Nonterminal-typing: '  ' Reconstructors: ' c ' list = Num int | Cons int * list tree = Nil | Leaf int | Node tree * tree [Num n] = Leaf n [Cons n l] = Node (Leaf n) [l] [list -> tree] (| L S -> L T [  ] c |) Type-check'able! L S -> L T 

[ 7 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Banana properties: Simple (corresponds to: “simple recursion”) Safe (syntactically safe + always terminate) Efficient (linear time in size of input + output) (Expressive) (…enough for interesting extensions) Banana Algebra “for free” (16 banana ops): Modular Incremental Simple Safe Efficient (Expressive) Statically reduce: Banana Algebra (term)  Banana (const)  Banana Properties "The metafront System: Safe and Extensible Parsing and Transformation" [ Claus Brabrand | Michael Schwartzbach ] ( LDTA 2003, SCP J ) "Growing Languages with Metamorphic Syntax Macros" [ Claus Brabrand | Michael Schwartzbach ] ( PEPM 2002 ) Been around for many years We now propose

[ 8 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Introduction: "What is a Banana?" Bananas for Language Transformation Language Extension Pattern Banana Algebra Examples Implementation Related Work Conclusion Outline

[ 9 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Language Extension Pattern [var V] = var V [lam V E] = lam V [E ] [app E 1 E 2 ] = app [E 1 ] [E 2 ] [zero] = lam z (var z) [succ E] = lam s [E ] [pred E] = app [E ] (lam z (var z)) Numeral extension:Lambda-Calculus: Nonterminal typing: Reconstructors: [Exp -> Exp] Exp : var Id : lam Id * Exp : app Exp * Exp : zero : succ Exp : pred Exp Exp : var Id : lam Id * Exp : app Exp * Exp 'LT''LT' 'LS''LS' ' '' ' 'c ''c ' (| L S -> L T [  ] c |) Catamorphism: Using very simple numeral encoding

[ 10 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Algebraic Solution Exp : var Id : lam Id * Exp : app Exp * Exp Exp : zero : succ Exp : pred Exp (| ln -> l [Exp -> Exp] [zero] = lam z (var z) [succ E] = lam s [E ] [pred E] = app [E ]... |) idx + lnl ln  l ln+l  l l  ll  l

[ 11 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Extending Java Java grammar: ( 575 lines !!! )... Stm : "repeat" Stm "until" "(" Exp ")" ";" (| repeat -> java [Stm -> Stm, Exp -> Exp] [repeat S E] = do-while [S] (not [E]) |) idx + repeatjava repeat  java java+repeat  java java  java

[ 12 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Banana Algebra Languages (L): l v L \ LL \ L L + LL + L src ( X ) tgt ( X ) let v = L in L letx w = X in L Transformations (X): x w X \ LX \ L X + XX + X X XX X idx ( L ) let v = L in X letx w = X in X (| L -> L [  ] c |) {  CFG  }

[ 13 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Algebraic Laws Idempotency of '+': Commutativity of '+': Associativity of '+': Source-identity: … L  L + L L 1 + L 2  L 2 + L 1 L 1 + (L 2 + L 3 )  (L 1 + L 2 ) + L 3 L  src(idx(L)) L  tgt(idx(L)) Target-identity:

[ 14 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Introduction: "What is a Banana?" Bananas for Language Transformation Language Extension Pattern Banana Algebra Examples Implementation Related Work Conclusion Outline

[ 15 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Example Revisited { Id = [a-z] [a-z0-9]* ; Exp.var : Id ; Exp.lam : "\\" Id "." Exp ; Exp.app : "(" Exp Exp ")" ; } { Exp.zero : "zero" ; Exp.succ : "succ" "(" Exp ")" ; Exp.pred : "pred" "(" Exp ")" ; } --- "l.l" "ln.l" --- let l = "l.l" in let ln = "ln.l" in idx(l) + (| ln -> l [Exp -> Exp] Exp.zero = '\z.z' ; Exp.succ = '\s.$1' ; Exp.pred = '($1 \z.z)' ; |) --- "ln2l.x" ---

[ 16 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Numerals + Booleans l lb  l idx + lb+l  l l  ll  l lb l ln  l idx + ln+l  l ln + l+ln+lb  l …with Nums …with Bools …with Nums & Bools?

[ 17 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Java + Repeat let java = "java.l" in let repeat = "repeat.l" in idx(java) + (| repeat -> java [Exp -> Exp, Stm -> Stm] Stm.repeat = 'do $1 while (!($2));' ; |) { Stm.repeat : "repeat" Stm "until" "(" Exp ")" ";" ; } 7 lines ! { Java... "try" Stm "catch"... Name.id : Id ; } 575 lines --- "java.l" "repeat.l" "repeat2java.x" ---

[ 18 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Concrete vs. Abstract Syntax Exp.or : Exp1 "||" Exp ;.exp1 : Exp1 ; Exp1.and : Exp2 "&&" Exp1 ;.exp2 : Exp2 ; Exp2.add : Exp3 "+" Exp2 ;.exp3 : Exp3 ;  Exp7.neg : "!" Exp8 ;.exp8 : Exp8 ; Exp8.par : "(" Exp ")" ;.var : Id ;.num : IntConst ; Exp (with explicit assoc./prec.) : Stm.repeat = Stm.do(, Exp.exp1( Exp1.exp2( Exp2.exp3( Exp3.exp4( Exp4.exp5( Exp5.exp6( Exp6.exp7( Exp7.neg( Exp8.par( ) ))))))))) ; Abstract syntax: Stm.repeat = 'do $1 while (!($2));' ; Concrete syntax: NB: Tool supports BOTH ! (unambiguous: concrete  abstract)

[ 19 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark "FUN" Example Lambda Calculus Nums→  Unsigned arithmetic + booleans + definitions + pairs Bools→  Defs→  Pairs→  +++ Fun Literals Literals→Nums Fun grammar transform The "FUN" Language: used for Teaching Functional Programming Basically The Lambda Calculus with…: numerals, booleans, arithmetic, boolean logic, local definitions, pairs, literals, lists, signs, comparisons, dynamic types, fixed-point combinators, … (at Aarhus University)

[ 20 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Literals Literals→Nums "FUN" Example Lambda Calculus Nums→  Unsigned arithmetic + booleans + definitions + pairs Bools→  Defs→  Pairs→  +++ Fun Fun grammar transform Signed arith→Nums Literals→Nums Fun grammar transformFunSigned GT + FunFunSigned + Component re-use

[ 21 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark "FUN" Example Lambda Calculus Nums→  Unsigned arithmetic + booleans + definitions + pairs Bools→  Defs→  Pairs→  +++ Fun GTFunSigned GT + FunFunSigned + FunCompareFunTypesafe ++ FunCompare GT + FunTypesafe GT + 245x Banana Algebra ops  4 MB Banana !

[ 22 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark "FUN" Usage Statistics Usage statistics (245x operators) in "FUN": 58x { …cfg… } Constant languages 51x "file.l" Language inclusions 28x L + L Language additions 23x v Language variables 17x (|L  L [  ] c|) Constant transformations 17x X + X Transformation additions 14x "file.x" Transformation inclusions 10x let-in Local definitions 9x idx(L) Identity transformations 8x X X Compositions 4x L \ L Language restriction 4x w Transformation variables 2x src(X) Source extractions

[ 23 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Incremental Development let l = "l.l" in idx(l) + (| "ln.l" -> l [Exp -> Exp] Exp.zero : '\z.z' ; Exp.succ : '\x.$1' ; Exp.pred : '($1 \z.z)' ; |) let l = "l.l" in idx(l) + (| "li.l" -> l [Exp -> Exp] Exp.id : '\z.z' ; |) { Exp.zero : "zero" ; Exp.succ : "succ" Exp ; Exp.pred : "pred" Exp ; } { Id = [a-z] [a-z0-9]* ; Exp.var : Id ; Exp.lam : "\\" Id "." Exp ; Exp.app : "(" Exp Exp ")" ; } --- "ln.l" "ln2li.x" "l.l" "ln2l.x" --- let l = "l.l" in idx(l) + (| ln -> l+"li.l" [Exp -> Exp] Exp.zero : 'id' ; Exp.succ : '\x.$1' ; Exp.pred : '($1 id)' ; |) { Exp.id : "id" ; } --- "li.l" "li2l.x" --- "li2l.x" o "ln2li.x" --- "ln2l.x" --- EXERCISE

[ 24 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Example cont'd Both statically reduce to same catamorphism: (| Exp.app : Exp.app($1, $2) ; Exp.lam : Exp.lam($1, $2) ; Exp.pred : Exp.app($1, Exp.lam(Id("z"), Exp.var(Id("z")))) ; Exp.succ : Exp.lam(Id("x"), $1) ; Exp.var : Exp.var($1) ; Exp.zero : Exp.lam(Id("z"), Exp.var(Id("z"))) ; |) { Id = [a-z] [0-9a-z]* ; Exp.app : "(" Exp Exp ")" ; Exp.lam : "\" Id "." Exp ; Exp.var : Id ; } { Id = [a-z] [0-9a-z]* ; Exp.app : "(" Exp Exp ")" ; Exp.lam : "\" Id "." Exp ; Exp.pred : "pred" Exp ; Exp.succ : "succ" Exp ; Exp.var : Id ; Exp.zero : "zero" ; } -> [Exp -> Exp, Id->Id]

[ 25 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Other Examples Self-Application (The tool on itself!): SQL embedding (in ): My-Java (endless variations): [L 1 << L 2 ] = '(L 1 \ L 2 ) + L 2 ' [X 1 << X 2 ] = '(X 1 \ src(X 2 )) + X 2 ' Stm.select = 'factor ( ) { if ( ) return ( # \+ ( ) ); }' java ( + sql) ( \ loops) o syntaxe_francais

[ 26 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Implementation [ / ] The 'Banana Algebra' Tool: (3,600 lines of O'Caml) Uses (underlying technologies): 'dk.brics.grammar': for parsing, unparsing, and ambiguity analysis ! 'XSugar': for transformation: "concrete syntax  abstract XML syntax" 'XSLT': for transformation: "XML  XML"

[ 27 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Introduction: "What is a Banana?" Bananas for Language Transformation Language Extension Pattern Banana Algebra Examples Implementation Related Work Conclusion Outline

[ 28 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Related Work (I/III) Macro Systems: "The metafront System: Safe and Extensible Parsing and Transformation" [ Claus Brabrand | Michael Schwartzbach ] ( LDTA 2003, SCP J ) "Growing Languages with Metamorphic Syntax Macros" [ Claus Brabrand | Michael Schwartzbach ] ( PEPM 2002 )

[ 29 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Related Work (II/III) Attribute Grammars: Language transformation (and extension)… …via computation on AST's (using "inherited" or "synthesized" or … attributes) E.g., Eli, JastAdd, Silver, … Rewrite Systems: Language transformation (and extension)… …via syntactic rewriting, using encodings…: gradually rewrite " S -syntax" to " T -syntax" E.g., Elan, TXL, ASF+SDF, Stratego/XT, … S  TS  T S  TS  T Both; compared to bananas: More ambitious (expressivity) No termination guarantees (safety) Transformation "indirect" (simplicity)

[ 30 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Related Work (III/III) Functional Programming: Catas mimicked by "disciplined style" of fun. programming …aided by: Traversal functions (auto-synthesized from datatypes) Combinator libraries "Shortcut fusion" (to eliminate ' ' at compile-time) Category Theory: A lot of this work can be viewed as Category Theory: Basically ye olde issue: GPL vs. DSL

[ 31 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Conclusion IF bananas are sufficiently: (Expressive) THEN you get…: Banana Algebra “for free” (16 banana ops): Incremental Modular Simple Safe Efficient "Niche" Statically reduce: Banana Algebra (term)  Banana (term) 

[ 32 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 BONUS SLIDES - Reduction Semantics - " Syntactic Language Extension via an Algebra of Languages and Transformations " [ Jacob Andersen | Claus Brabrand ] ( ITU Technical Report, Dec ) If you want all the details:

[ 33 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Reduction Semantics Environments: Reduction relations: Abbreviations:...as a short-hand for: ENV L = VAR L  EXP L ENV L  ENV X  EXP L  EXP L  '  L ' ENV X = VAR X  EXP X ENV L  ENV X  EXP X  EXP X  '  X ' ,  | - L  L l ( , ,L,l)  '  L ' ( , ,X,x)  '  X ' ,  | - X  X x environment of languages environment of transformations

[ 34 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Semantics (L) ,  l  L l [ CON L ] ,  v  L  (v) [ VAR L ] ,  L \ L'  L l l' [ RES L ] ,  L'  L l' ,  L  L l ,  L + L'  L l l' [ ADD L ] ,  L'  L l' ,  L  L l l l l ~ l' l l wfl

[ 35 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Semantics (L) ,  src (X)  L l S [ SRC L ] ,  X  X (| l S -> l T [  ] c |) ,  tgt (X)  L l T [ TGT L ] ,  X  X (| l S -> l T [  ] c |) ,  let v=L in L'  L l' [ LET L ]  [v=l],  L'  L l' ,  L  L l

[ 36 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Semantics (X) ,  X \ L  X x l [ RES X ] ,  L  L l ,  X  X x ,  X + X'  X x x' [ ADD X ] ,  X'  X x' ,  X  X x x x x ~ x' x ,  (| L S -> L T [  ] c |)  X (| l S -> l T [  ] c |) [ CON X ] ,  L S  L l S ,  L T  L l T (| l S -> l T [  ] c |) wfx ,  w  X  (w) [ VAR X ]

[ 37 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Semantics (X) ,  X' X  X (| l S -> l T ' [  '  ] c' c |) [ COMP X ] ,  X'  X (| l S ' -> l T ' [  '] c' |) ,  idx (L)  X (| l -> l [id  (l)] id c (l) |) [ IDX X ] ,  letx w=X in X'  X x' [ LET L ] ,  [w=x] X'  X x' ,  X  X x ,  X  X (| l S -> l T [  ] c |) ,  L  L l l T l S ' l

[ 38 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 BONUS SLIDES - More Examples -

[ 39 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Numeral & Boolean Extension Numeral Extension (catamorphism): Boolean Extension (catamorphism): [var V] = var [V ] [lam V E] = lam [V ] [E ] [app E 1 E 2 ] = app [E 1 ] [E 2 ] [zero] = lam z (var z) [succ E] = lam s [E ] [pred E] = app [E ] (lam z (var z)) [var V] = var [V ] [lam V E] = lam [V ] [E ] [app E 1 E 2 ] = app [E 1 ] [E 2 ] [true] = lam a (lam b (var a)) [false] = lam a (lam b (var b)) [if E 1 E 2 E 3 ] = app (app [E 1 ] [E 2 ]) [E 3 ] Exp : var Id : lam Id * Exp : app Exp * Exp : zero : succ Exp : pred Exp Exp : var Id : lam Id * Exp : app Exp * Exp Exp : var Id : lam Id * Exp : app Exp * Exp : true : false : if Exp Exp Exp Exp : var Id : lam Id * Exp : app Exp * Exp

[ 40 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Lambda with Booleans Exp : true : false : if Exp Exp Exp (| lb -> l [Exp -> Exp] [true] = '\a.\b. a' [false] = '\a.\b. b' [if E 1 E 2 E 3 ] = '(([E 1 ] [E 2 ]) [E 3 ])' |) idx + lbl lb  l lb+l  l l  ll  l Exp : var Id : lam Id * Exp : app Exp * Exp

[ 41 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Incremental Development let l = "l.l" in idx(l) + (| "ln.l" -> l [Exp -> Exp] Exp.zero : '\z.z' ; Exp.succ : '\x.$1' ; Exp.pred : '($1 \z.z)' ; |) let l = "l.l" in idx(l) + (| "li.l" -> l [Exp -> Exp] Exp.id : '\z.z' ; |) { Exp.zero : "zero" ; Exp.succ : "succ" Exp ; Exp.pred : "pred" Exp ; } { Id = [a-z] [a-z0-9]* ; Exp.var : Id ; Exp.lam : "\\" Id "." Exp ; Exp.app : "(" Exp Exp ")" ; } --- "ln.l" "ln2li.x" "l.l" "ln2l.x" --- let l = "l.l" in idx(l) + (| ln -> l+"li.l" [Exp -> Exp] Exp.zero : 'id' ; Exp.succ : '\x.$1' ; Exp.pred : '($1 id)' ; |) { Exp.id : "id" ; } --- "li.l" "li2l.x" --- "li2l.x" o "ln2li.x" --- "ln2l.x" ---

[ 42 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Java + Repeat Java : Exp : Exp "+" Exp... Stm : Exp ";" : "if" "(" Exp ")" Stm : "while" "(" Exp ")" Stm... Stm : "repeat" Stm "until" "(" Exp ")" ";" (| repeat -> java [Exp -> Exp, Stm -> Stm] [repeat S until (E);] = … ; |) idx + repeat java repeat  java java+repeat  java java  java 575 lines Entire extension: 7 lines !

[ 43 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Usage Scenarios Programmers: May extend existing languages (~ syntax macros) Developers: May embed DSLs into host languages (SQL in Java) Developers (and teachers): May incrementally specify multi-layered languages Compiler writers: May rely on tool and implement only a small core (and then specify the rest externally as extensions)

[ 44 ] Claus Brabrand, ITU BANANA ALGEBRA DIKU, KU, Denmark May 11, 2010 BONUS SLIDES - Parsing & Error Reporting -

[ 45 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Parsing Parsing (XSugar): Variant of Earley's algorithm: O ( |  | 3 ) Can parse any context-free grammar Closed under union of languages Support for production priority Tool easily adapts to other parsing algorithms

[ 46 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Unparsing: Canonical whitespace Ambiguity: parsing  unparsing AST L / ~ L. L.  Parsing: Grammar ambiguity AST L / ~ L. L. .

[ 47 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Ambiguity Analysis Ambiguity Analysis: Using implementation ( ) on: Source language; Target language; and/or …all intermediate languages (somewhat expensive) (Note: Ambiguity analysis comes with XSugar tool) "Analyzing Ambiguity of Context-Free Grammars" [ Claus Brabrand | Robert Giegerich | Anders Møller ] ( CIAA 2007 ) " dk.brics.grammar " [ by Anders Møller ]

[ 48 ] Claus Brabrand, ITU BANANA ALGEBRA May 11, 2010 DIKU, KU, Denmark Error Reporting Error reporting: Static parse-error (O'Caml-lex): Static transformation error (XSugar): (is actually a parse-error in a cata reconstructor) Dynamic parse-error (XSugar): Dynamic transformation error: impossible :-) *** In ln2l.x (4,4)-(4,7): Parse error at "Exp" *** Parse error at character 6 (line 1, column 7) in /tmp/shape84e645.txt *** Parse error at character 23 (line 1, column 24) in /dev/stdin Could be improved Prototype