automatically introducing templates in domain-specific...
TRANSCRIPT
![Page 1: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/1.jpg)
Automatically introducing templates in Domain-Specific Languages
Andrey Breslav
University of Tartu / ITMO (St. Petersburg)
February 06, 2010
![Page 2: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/2.jpg)
Outline
● Basics● What is a template?● How is a language described?● How to plug templates in?● Intermediate summary
● Some notes on correctness● Issues with names● Constraints for name resolution● Open issues
● Conclusion
![Page 3: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/3.jpg)
MyToy Language
VAR x = 2; VAR y = 5; VAR xy = 0;VAR trigger = 0;COMEFROM trigger = 1; xy = xy + y; x = x - 1; trigger = 1;COMEFROM x = 0;
VAR x = 2; VAR y = 5;VAR xy = 0;$UNTIL <x = 0> < xy = xy + y; x = x - 1;>;
template $UNTIL <condition> <body> { VAR trigger = 0; comefrom trigger = 1; <body> trigger = 1; comefrom <condition>;}
TemplateMyToy-TPLLanguage
![Page 4: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/4.jpg)
Our Goal
Language Language-TPL
● C preprocessor (cpp)● C++ templates● Lisp● Nemerle macros● NOT Java Generics
Useless error messages
Only one language
![Page 5: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/5.jpg)
Our Goal
Language Language-TPL
Static checkingof templates
AutomaticHelpful
error messages
![Page 6: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/6.jpg)
Alternative Views of a Program
VAR x = 2; VAR y = 5; y = y + x;
x
y
2
5
=
+
Pro
gram
Program
x
x 2x
y 5
=
+ y
yx
:: Program:: Var
:: Var
:: Literal
:: Literal
:: Binary
:: Assignment
rhs
lhs
![Page 7: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/7.jpg)
Describing graph structure
Program
Statement ComeFrom
Vartype : {Int, ...}
Assignment
Expression
Literalvalue : Int
Binaryoperation : {+,-}
0..*
1
1
1 2
[Structural] Constraints:● Node types● Node attributes● Edge types● Edge multiplicities● Typing of edge ends
1
lhs
rhs
![Page 8: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/8.jpg)
Describing tree structure
Program
Statement ComeFrom
Vartype : {Int, …}name : String
Assignment
Expression
Literalvalue : Int
Binaryoperation : {+,-}
0..*
1
1
1 2
1
lhs
rhs
VarUsagename : String
ref/resolve
![Page 9: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/9.jpg)
Templates for Graph
N
N
Graph ASTSerialize
Deserialize
Templates ASTTemplatesSerialize
Deserialize
AST
![Page 10: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/10.jpg)
Templates for AST
N
N
Graph ASTSerialize
Deserialize
Templates AST
AST
![Page 11: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/11.jpg)
Describing templates
template $MULT <ab> <a> <b> { <ab> = 0; $UNTIL <<b> = 0> < <ab> = <ab> + <a>; <b> = <b> - 1; >;}
VAR x = 2;VAR y = 5;VAR xy = 0;$MULT <xy> <x> <y>;
Templatename : String
Parametername : String
Applicationtemplate : String
arguments
ParameterUsageparameter : String
VarTemplatetype : {Int, …}name : String
![Page 12: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/12.jpg)
Template Description
Templatename : String
Parametername : String
Applicationtemplate : String
ParameterUsageparameter : String
TemplateBody
VarTemplatetype : {Int, …}name : String
0..*
0..*1
1
applied
args
body
params
ExpressionTemplate
1
init
![Page 13: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/13.jpg)
Language-Specific Templates
Vartype : {Int, ...}
Expression
Literalvalue : Int
Binaryoperation : {+,-}
1 2
initoperands
TemplateBody
VarTemplatetype : {Int, …}name : String
ExpressionTemplate
LiteralTemplatevalue : Int
BinaryTemplateoperation : {+,-}
1
init
2operands
Initial AST
Template AST
![Page 14: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/14.jpg)
Instantiation
Applicationtemplate : String
ParameterUsageparameter : String
TemplateBody
VarTemplatetype : {Int, …}name : String
ExpressionTemplate
Construct of theMyToy language
Varname : String
Expression
Instantiate the bodyof the corresponding template
Instantiate the argument
![Page 15: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/15.jpg)
Structural CorrectnessConstraints:
● Node types● Node attributes● Edge types● Edge multiplicities● Typing of edge ends
TemplateBody
VarTemplatetype : {Int, …}name : String
1
init
ProgramTemplate
ParameterUsageparameter : String
ExpressionTemplate
![Page 16: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/16.jpg)
Types for Templates
Templatename : String
Parametername : String
TemplateBody
Type
Expression
Statement
Literal
...
Assignment
......
template $UNTIL :: Statement[1..*] <condition :: Assignment[1]> <body :: Statement[0..*]> { VAR trigger = 0 :: Literal[1]; comefrom trigger = 1; <body> Trigger = 1; :: Assignment[1] comefrom <condition>;}
Typesreflect
multiplicities
![Page 17: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/17.jpg)
Intermediate Summary
● A language is described as (Graph <-> AST)● We extend a language to support templates● We generate a type checker for templates
● (no type annotations required)● This facilitates error reporting
● We generate an instantiation procedure● Everything else is reused form the original
language
● That's all great, but we capture only some errors
![Page 18: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/18.jpg)
One more type of errors
VAR x = 2; VAR y = 5;VAR xy = 0;$UNTIL <x = 0> < xy = xy + y; x = x - 1;>;$UNTIL <y = 0> < y = y - 1;>;
Error: variable 'trigger' is already defined!
template $UNTIL <condition> <body> { VAR trigger = 0; comefrom trigger = 1; <body> trigger = 1; comefrom <condition>;}
![Page 19: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/19.jpg)
Who said that was an error?!
N
N
Graph ASTSerialize
DeserializeAST
Deserialize
Structural Constraints
Identification Constraints Templates AST
![Page 20: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/20.jpg)
Our plan
Graph ASTSerialize
DeserializeAST
Deserialize
Identification Constraints
Templates AST
Template Type System must be extended
![Page 21: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/21.jpg)
Identification Constraints
constraint UniqueVarNames for p :: Program { no duplicates in [var.name | var <- p.statements, var is instance of Var]}
Scope
Key
![Page 22: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/22.jpg)
Name Checking for MyToy
VAR x = 2; VAR y = 5;VAR xy = 0;...VAR trigger = 0;...VAR trigger = 0;...
{x, y, xy, trigger, trigger}
● for every scope● compute the collection of keys● check it for duplicates
![Page 23: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/23.jpg)
Is it sufficient?
● Yes!● MOF/Ecore● EBNF● DDL● DTD● Other language which describe static structures
● No● Programming languages● ...
![Page 24: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/24.jpg)
Types for Name checking
template $UNTIL :: defines('trigger', bodyDef) <condition> <body :: defines(bodyDef)> { VAR trigger = 0; :: defines('trigger') comefrom trigger = 1; <body> :: defines(bodyDef) trigger = 1; comefrom <condition>;}
Forget about names of templates and their parameters!
![Page 25: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/25.jpg)
Name Checking Example
VAR x = 2; :: defines('x')VAR y = 5; :: defines('y')VAR xy = 0; :: defines('xy')$UNTIL <x = 0> < xy = xy + y; x = x - 1;>; :: defines('trigger')$UNTIL <y = 0> < VAR one = 1; y = y - one;>; :: defines('trigger', 'one')
template $UNTIL :: defines('trigger', bodyDef)
Error: templates 'UNTIL' and 'UNTIL' define the same variable: trigger!
![Page 26: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/26.jpg)
Naming Errors
● Duplicate definition● done
● “Hygienic” templates● achievable
● Usage of undefined name (only AST)● symmetric
● Illegal context● In the graph an object is used while it's name is not visible
● Collision● Two distinct objects with the same names are used
![Page 27: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/27.jpg)
Symmary
● If a language is described as (Graph <-> AST)● Automatically extend it with template support● Structural correctness guarantees● Some naming correctness guarantees
● Ecore, EBNF, DTD, etc.● Complete support for templates
● More complicated languages● Subject to future work...
![Page 28: Automatically introducing templates in Domain-Specific Languagesvarmo/tday-andu/breslav-slides.pdf · 2010-02-08 · Automatically introducing templates in Domain-Specific Languages](https://reader036.vdocument.in/reader036/viewer/2022062506/5f01dacd7e708231d4015c93/html5/thumbnails/28.jpg)
Thanks for your attention!