SlideShare ist ein Scribd-Unternehmen logo
1 von 77
Downloaden Sie, um offline zu lesen
Separation of Concerns !
in Language Definition
Eelco Visser!
Delft University of Technology
“99% of errors found by Semmle are due to bad language design”
— Oege de Moor, CEO Semmle
Problem
Domain
Solution
Domain
General-
Purpose
Language
Problem
Domain
Solution
Domain
General-
Purpose
Language
• Lack of safety
Problem
Domain
Solution
Domain
General-
Purpose
Language
• Lack of safety
• Lack of abstraction
Problem
Domain
Solution
Domain
General-
Purpose
Language
• Lack of safety
• Lack of abstraction
• Distance from domain
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
• Lack of safety
• Lack of abstraction
• Distance from domain
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
• Language-based safety and security
• Lack of abstraction
• Distance from domain
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
• Language-based safety and security
• High-level domain-specific abstraction
• Distance from domain
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
• Language-based safety and security
• High-level domain-specific abstraction
• Reduced distance from problem domain
General-
Purpose
Language
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
Language
Design
Compiler +
Editor (IDE)
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative-
Meta
Languages
Solution
Domain
Problem
Domain
General-
Purpose
Language
Domain-
Specific
Language
Language
Design
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative-
Meta
Languages
Language
Design
Language workbench
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative-
Meta
Languages
Language
Design
Language workbench
Declarative multi-purpose meta-languages
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative-
Meta
Languages
Language
Design
Language workbench
Declarative multi-purpose meta-languages
Useable language implementations
Compiler +
Editor (IDE)
Language
Design
General-
Purpose
Language
Declarative-
Meta
Languages
Language
Design
Language workbench
Declarative multi-purpose meta-languages
Useable language implementations
High quality language designs
parser
parser
type checker
parser
type checker
code generator
parser
type checker
code generator
interpreter
parser
type checker
code generator
interpreter
parser
parser
type checker
code generator
interpreter
parser
error recovery
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
type system
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
type system
operational
semantics
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
type system
operational
semantics
type soundness
proof
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
type system
operational
semantics
type soundness
proof
Language Design
Syntax
Definition
Name
Binding
Type
Constraints
Dynamic
Semantics
Transform
Syntax Definition
int fib(int n) {	
if(n <= 1) 	
return 1;	
else 	
return fib(n - 2) + fib(n - 1);	
}
Syntax: Phrase Structure of Programs
Syntax: Phrase Structure of Programs
}
int fib ( int n ) {
if ( n <= 1 )
return 1 ;
else
return fib ( n - 2 ) + fib ( n - 1 ) ;
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Syntax: Phrase Structure of Programs
Exp.IntExp.Int
Exp.Leq Statement.Return Exp.Sub Exp.Sub
Exp.Call Exp.Call
Param.Param
IntType
ID
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType
Syntax: Phrase Structure of Programs
Exp.IntExp.Int
Exp.Leq Statement.Return Exp.Sub Exp.Sub
Exp.Call Exp.Call
Exp.Add
Param.Param
IntType
ID
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType
Syntax: Phrase Structure of Programs
Exp.IntExp.Int
Exp.Leq Statement.Return Exp.Sub Exp.Sub
Exp.Call Exp.Call
Exp.Add
Statement.Return
Param.Param
IntType
ID
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType
Syntax: Phrase Structure of Programs
Exp.IntExp.Int
Exp.Leq Statement.Return Exp.Sub Exp.Sub
Exp.Call Exp.Call
Exp.Add
Statement.Return
Param.Param
IntType
Statement.If
ID
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType
Syntax: Phrase Structure of Programs
Exp.IntExp.Int
Exp.Leq Statement.Return Exp.Sub Exp.Sub
Exp.Call Exp.Call
Exp.Add
Statement.Return
Param.Param
IntType
Definition.Function
Statement.If
ID
}int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType
Syntax: Phrase Structure of Programs
Function
IntType IntType
Param
Var Int Int
Leq Return
Return
Call
Add
Var Int
Sub
Var Int
Sub
Call
If
fib n n 1 1 fib n 2 fib n 1
Abstract!
Syntax!
Tree
int fib(int n) {	
if(n <= 1) 	
return 1;	
else 	
return fib(n - 2) + fib(n - 1);	
}
Text
parse
Function(	
IntType()	
, "fib"	
, [Param(IntType(), "n")]	
, [ If(	
Leq(Var("n"), Int("1"))	
, Int("1")	
, Add(	
Call("fib", [Sub(Var("n"), Int("2"))])	
, Call("fib", [Sub(Var("n"), Int("1"))])	
)	
) 	
]	
)
int fib(int n) {	
if(n <= 1) 	
return 1;	
else 	
return fib(n - 2) + fib(n - 1);	
}
Abstract !
Syntax !
Term
Text
parse
Type
Definition.Function
ID }( ) {Param* Statement*
Exp
Statement.Return
return ;
Statement.If
if ( ) elseExp Statement Statement
Exp.Add
+Exp Exp
Exp.Var
ID
Understanding Syntax =!
Understanding Tree Structure
parse(prettyprint(t)) = t
No need to understand !
how parse works!
Demo: Syntax Definition in SDF3
Language Design
Syntax!
Definition
Name !
Binding
Type!
Constraints
Dynamic!
Semantics
Transform
Type
Definition.Function
ID }( ) {Param* Statement*
Exp
Statement.Return
return ;
Statement.If
if ( ) elseExp Statement Statement
Exp.Add
+Exp Exp
templates 	
	
Definition.Function = <	
<Type> <ID>(<Param*; separator=",">) {	
<Statement*; separator="n">	
}	
>	
!
Statement.If = <	
if(<Exp>) 	
<Statement>	
else	
<Statement>	
>	
!
Statement.Return = <return <Exp>;>	
!
Exp.Add = <<Exp> + <Exp>>	
!
Exp.Var = <<ID>>Exp.Var
ID
The Syntax Definition Formalism SDF3
Statement.If = <	
if(<Exp>) 	
<Statement>	
else	
<Statement>	
>
Parser
Abstract syntax tree schema
Pretty-Printer
Syntactic completion templates
Folding rules
Error recovery rules
Syntactic coloring
Outline rules
Syntax Definition
Multi-Purpose Declarative Syntax Definition
Name and Type Analysis
Name Binding & Scope Rules
int fib(int n) {	
if(n <= 1) 	
return 1;	
else 	
return fib(n - 2) + fib(n - 1);	
}
what does this variable refer to?
Our contribution
!
- declarative language for name binding & scope rules
- generation of incremental name resolution algorithm
!
- Konat, Kats, Wachsmuth, Visser (SLE 2012)
- Wachsmuth, Konat, Vergu, Groenewegen, Visser (SLE 2013)
which function is being called here?
Needed for
!
- checking correct use of names and types
- lookup in interpretation and compilation
- navigation in IDE
- code completion
State-of-the-art
!
- programmatic encoding of name resolution algorithms
Demo: Name and Type Analysis in NaBL+TS
Language Design
Syntax!
Definition
Name !
Binding
Type!
Constraints
Dynamic!
Semantics
Transform
Declarative Name Binding and Scope Rules
binding rules	
!
Param(t, name) :	
defines Variable name	
	
Var(name) :	
refers to Variable name	
!
Function(t, name, param*, s) :	
defines Function name	
scopes Variable, Function	
!
Call(name, exp*) :	
refers to Function name
Incremental name resolution algorithm
Name checks
Semantic code completion
Reference resolution
Semantics of Name Binding?
binding rules	
!
Param(t, name) :	
defines Variable name	
	
Var(name) :	
refers to Variable name	
!
Function(t, name, param*, s) :	
defines Function name	
scopes Variable, Function	
!
Call(name, exp*) :	
refers to Function name
Research: how to characterize correctness of the result of
name resolution without appealing to the algorithm itself?
Analogy: declarative semantics of syntax definition
Interpretation!
& Verification
Language Design
Syntax!
Definition
Name !
Binding
Type!
Constraints
Dynamic!
Semantics
Transform
rules	
	
Num(i) --> I(i)	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i = 0, e2 --> v	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i != 0, e3 --> v	
	
Add(e1, e2) --> I(addInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Sub(e1, e2) --> I(subInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Mul(e1, e2) --> I(mulInt(i, j))	
where e1 --> I(i), e2 --> I(j)
DynSem: Dynamic Semantics Specification
module semantics	
!
rules	
!
E env |- Var(x) --> v 	
where env[x] => T(e, env'),	
E env' |- e --> v	
	
E env |- Fun(Param(x, t), e) --> C(x, e, env)	
!
E env |- App(e1, e2) --> v	
where E env |- e1 --> C(x, e, env'),	
E {x |--> T(e2, env), env'} |- e --> v	
	
E env |- Fix(Param(x, t), e) --> v	
where 	
E {x |--> T(Fix(Param(x,t),e),env), env} |- e --> v	
	
E env |- Let(x, t, e1, e2) --> v	
where E {x |--> T(e1, env), env} |- e2 --> v
Implicitly-Modular Structural Operational Semantics (I-MSOS)*
rules	
!
E env |- Var(x) --> v 	
where env[x] => T(e, env'),	
E env' |- e --> v	
!
Add(e1, e2) --> I(addInt(i, j))	
where e1 --> I(i), 	
e2 --> I(j)
rules	
!
E env |- Var(x) --> v 	
where env[x] => T(e, env'),	
E env' |- e --> v	
!
E env |- Add(e1, e2) --> I(addInt(i, j))	
where E env |- e1 --> I(i), 	
E env |- e2 --> I(j)explicate
* P. D. Mosses. Modular structural operational semantics. JLP, 60-61:195–228, 2004.
M. Churchill, P. D. Mosses, and P. Torrini. Reusable components of semantic specifications. In MODULARITY, April 2014.
rules	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i = 0, e2 --> v	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i != 0, e3 --> v
Interpreter Generation
rules	
	
E env |- Ifz(e1, e2, e3) --> v	
where E env |- e1 --> I(i), 	
[i = 0, E env |- e2 --> v] +	
i != 0, E env |- e3 --> v]
explicate
& merge
module PCF	
sorts Exp Param Type 	
templates	
Exp.Var = [[ID]]	
Exp.App = [[Exp] [Exp]] {left} 	
Exp.Fun = [	
	 fun [Param] (	
	 	 [Exp]	
	 )	
] 	 	
Exp.Fix = [	
	 fix [Param] (	
	 	 [Exp]	
	 )	
]	
Exp.Let = [	
	 let [ID] : [Type] = 	
	 	 [Exp] 	
	 in [Exp]	
] 	
Exp.Num = [[INT]]	
Exp.Add = [[Exp] + [Exp]] {left}	
Exp.Sub = [[Exp] - [Exp]] {left}	
Exp.Mul = [[Exp] * [Exp]] {left}	
Exp = [([Exp])] {bracket} 	
Exp.Ifz = [	
	 ifz [Exp] then 	
	 [Exp] 	
	 else 	
	 [Exp]	
]	
Type.IntType = [int]	
Type.FunType = [[Type] -> [Type]] 	
Param.Param = [[ID] : [Type]]	
	
context-free priorities	
!
Exp.App > Exp.Mul > {left: Exp.Add Exp.Sub} 	
> Exp.Ifz
module types	
type rules	
!
Var(x) : t	
where definition of x : t	
	
Param(x, t) : t	
	
Fun(p, e) : FunType(tp, te)	
where p : tp and e : te 	
	
App(e1, e2) : tr	
where e1 : FunType(tf, tr) and e2 : ta	
and tf == ta	
else error "type mismatch" on e2	
!
Fix(p, e) : tp	
where p : tp and e : te 	
and tp == te	
else error "type mismatch" on p	
	
Let(x, tx, e1, e2) : t2	
where e2 : t2 and e1 : t1 	
and t1 == tx	
else error "type mismatch" on e1	
	
Num(i) : IntType()	
!
Ifz(e1, e2, e3) : t2	
where e1 : IntType() and e2 : t2 and e3 : t3	
and t2 == t3 	
else error "types not compatible" on e3	
	
e@Add(e1, e2) + e@Sub(e1, e2) + e@Mul(e1, e2) : IntType()	
where e1 : IntType() 	
else error "Int type expected" on e	
and e2 : IntType()	
else error "Int type expected" on e	
module semantics	
!
rules	
!
E env |- Var(x) --> v 	
where env[x] => T(e, env'),	
E env' |- e --> v	
	
E env |- Fun(Param(x, t), e) --> C(x, e, env)	
!
E env |- App(e1, e2) --> v	
where E env |- e1 --> C(x, e, env'),	
E {x |--> T(e2, env), env'} |- e --> v	
	
E env |- Fix(Param(x, t), e) --> v	
where 	
E {x |--> T(Fix(Param(x,t),e),env), env} |- e --> v	
	
E env |- Let(x, t, e1, e2) --> v	
where E {x |--> T(e1, env), env} |- e2 --> v	
	
rules	
	
Num(i) --> I(i)	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i = 0, e2 --> v	
	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i != 0, e3 --> v	
	
Add(e1, e2) --> I(addInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Sub(e1, e2) --> I(subInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Mul(e1, e2) --> I(mulInt(i, j))	
where e1 --> I(i), e2 --> I(j)
module names	
	 	
namespaces Variable	
	 	
binding rules	
!
Var(x) :	
refers to Variable x	
	
Param(x, t) :	
	 defines Variable x of type t	
	
Fun(p, e) :	
scopes Variable	
	
Fix(p, e) :	
scopes Variable	
	
Let(x, t, e1, e2) :	
defines Variable x of type t in e2
First Little (Big) Step: From PCF in Spoofax …
else 	
	 [Exp]	
]	
Type.IntType = [int]	
Type.FunType = [[Type] -> [Type]] 	
Param.Param = [[ID] : [Type]]	
	
context-free priorities	
!
Exp.App > Exp.Mul > {left: Exp.Add Exp.Sub} 	
> Exp.Ifz
Ifz(e1, e2, e3) : t2	
where e1 : IntType() and e2 : t2 and e3 : t3	
and t2 == t3 	
else error "types not compatible" on e3	
	
e@Add(e1, e2) + e@Sub(e1, e2) + e@Mul(e1, e2) : IntType()	
where e1 : IntType() 	
else error "Int type expected" on e	
and e2 : IntType()	
else error "Int type expected" on e	
Ifz(e1, e2, e3) --> v	
where e1 --> I(i), i != 0, e3 --> v	
	
Add(e1, e2) --> I(addInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Sub(e1, e2) --> I(subInt(i, j))	
where e1 --> I(i), e2 --> I(j)	
	
Mul(e1, e2) --> I(mulInt(i, j))	
where e1 --> I(i), e2 --> I(j)
Param(x, t) :	
	 defines Variable x of type t	
	
Fun(p, e) :	
scopes Variable	
	
Fix(p, e) :	
scopes Variable	
	
Let(x, t, e1, e2) :	
defines Variable x of type t in e2
Inductive has_type (C: Context) : term -> term -> Prop :=	
| VarC_ht ns k0 t x k1 : lookup C x ns k0 t -> has_type C (Co VarC [Id x k0] k1) t	
| ParamC_ht x t k0 : has_type C (Co ParamC [x;t] k0) t	
| FunC_ht k0 t_p t_e p e k1 : has_type C p t_p -> has_type C e t_e -> has_type C (Co FunC [p;e] k1) (Co FunTypeC [t_p;t_e] k0)	
| FixC_ht t_p t_e p e k0 : has_type C p t_p -> has_type C e t_e -> (t_p = t_e) -> has_type C (Co FixC [p;e] k0) t_p	
| AppC_ht t_r k0 t_f t_a e1 e2 k1 : has_type C e1 (Co FunTypeC [t_f;t_r] k0) -> has_type C e2 t_a -> (t_f = t_a) -> has_type C (Co AppC [e1;e2] k1) t_r	
| LetC_ht t2 t1 x t_x e1 e2 k0 : has_type C e2 t2 -> has_type C e1 t1 -> (t1 = t_x) -> has_type C (Co LetC [x;t_x;e1;e2] k0) t2	
| NumC_ht k0 i k1 : has_type C (Co NumC [i] k1) (Co IntTypeC [] k0)	
| IfzC_ht k0 t2 t3 e1 e2 e3 k1 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 t2 -> has_type C e3 t3 -> (t2 = t3) -> has_type C (Co IfzC [e1;e2;e3] k1) t2	
| AddC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co AddC [e1;e2] k3) (Co IntTypeC [] k2)	
| SubC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co SubC [e1;e2] k3) (Co IntTypeC [] k2)	
| MulC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co MulC [e1;e2] k3) (Co IntTypeC [] k2)	
| HT_eq e ty1 ty2 (hty1: has_type C e ty1) (tyeq: term_eq ty1 ty2) : has_type C e ty2	
.
Inductive semantics_cbn : Env -> term -> value -> Prop :=	
| Var0C_sem env' e env x k0 v : get_env x env e env' -> semantics_cbn env' e v -> semantics_cbn env (Co VarC [x] k0) v	
| Fun0C_sem t k1 k0 x e env : semantics_cbn env (Co FunC [Co ParamC [x;t] k1;e] k0) (Clos x e env)	
| Fix0C_sem k1 k0 env x t k3 e k2 v : semantics_cbn { x |--> (Co FixC [Co ParamC [x;t] k1;e] k0,env), env } e v -> semantics_cbn env (Co FixC [Co ParamC [x;t] k3;e] k2) v	
| App0C_sem env' x e env e1 e2 k0 v : semantics_cbn env e1 (Clos x e env') -> semantics_cbn { x |--> (e2,env), env' } e v -> semantics_cbn env (Co AppC [e1;e2] k0) v	
| Let0C_sem env x t e1 e2 k0 v : semantics_cbn { x |--> (e1,env), env } e2 v -> semantics_cbn env (Co LetC [x;t;e1;e2] k0) v	
| Num0C_sem env k0 i : semantics_cbn env (Co NumC [i] k0) (Natval i)	
| Ifz0C_sem i env e1 e2 e3 k0 v : semantics_cbn env e1 (Natval i) -> (i = 0) -> semantics_cbn env e2 v -> semantics_cbn env (Co IfzC [e1;e2;e3] k0) v	
| Ifz1C_sem i env e1 e2 e3 k0 v : semantics_cbn env e1 (Natval i) -> (i <> 0) -> semantics_cbn env e3 v -> semantics_cbn env (Co IfzC [e1;e2;e3] k0) v	
| Add0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co AddC [e1;e2] k0) (plus i j)	
| Sub0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co SubC [e1;e2] k0) (minus i j)	
| Mul0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co MulC [e1;e2] k0) (mult i j)	
.
Inductive ID_NS : Set :=	
| VariableNS 	
.	
!Definition NS :=	
ID_NS	
.	
!Inductive scopesR : term -> NS -> Prop :=	
| Fun_scopes_Variable p e k0 : scopesR (Co FunC [p;e] k0) VariableNS	
| Fix_scopes_Variable p e k0 : scopesR (Co FixC [p;e] k0) VariableNS	
.	
!Definition scopes_R :=	
scopesR	
.	
!Inductive definesR : term -> Ident -> NS -> key -> Prop :=	
| Param_defines_Variable x k1 t k0 : definesR (Co ParamC [Id x k1;t] k0) x VariableNS k1	
.	
!Definition defines_R :=	
definesR	
.	
!Inductive refers_toR : term -> Ident -> NS -> key -> Prop :=	
| Var_refers_to_Variable x k1 k0 : refers_toR (Co VarC [Id x k1] k0) x VariableNS k1	
.	
!Definition refers_to_R :=	
refers_toR	
.	
!Inductive typed_definesR : term -> Ident -> NS -> term -> key -> Prop :=	
| Param_typed_defines_Variable x t k1 t k0 : typed_definesR (Co ParamC [Id x k1;t] k0) x VariableNS t k1	
.	
!Definition typed_defines_R :=	
typed_definesR	
.
Inductive sorts : Set :=	
| Param_S 	
| ID_S 	
| INT_S 	
| Exp_S 	
| Type_S 	
.	
!Parameter Ident : Set.	
!Definition sort :=	
sorts	
.	
!Definition Ident_Sort :=	
ID_S	
.	
!Inductive Constructors :=	
| INTC (n: nat) 	
| VarC 	
| FunC 	
| FixC 	
| AppC 	
| LetC 	
| ParamC 	
| NumC 	
| AddC 	
| SubC 	
| MulC 	
| DivC 	
| IfzC 	
| IntTypeC 	
| FunTypeC 	
.	
!Definition constructors :=	
Constructors	
.	
!Fixpoint	
get_sig (x: constructors) : list sort * sort :=	
match x with	
| INTC n => ([],INT_S)	
| VarC => ([ID_S],Exp_S)	
| FunC => ([Param_S;Exp_S],Exp_S)	
| FixC => ([Param_S;Exp_S],Exp_S)	
| AppC => ([Exp_S;Exp_S],Exp_S)	
| LetC => ([ID_S;Type_S;Exp_S;Exp_S],Exp_S)	
| ParamC => ([ID_S;Type_S],Param_S)	
| NumC => ([INT_S],Exp_S)	
| AddC => ([Exp_S;Exp_S],Exp_S)	
| SubC => ([Exp_S;Exp_S],Exp_S)	
| MulC => ([Exp_S;Exp_S],Exp_S)	
| DivC => ([Exp_S;Exp_S],Exp_S)	
| IfzC => ([Exp_S;Exp_S;Exp_S],Exp_S)	
| IntTypeC => ([],Type_S)	
| FunTypeC => ([Type_S;Type_S],Type_S)	
end.
… to PCF in Coq (+ manual proof of type preservation)
Summary
parser
type checker
code generator
interpreter
parser
error recovery
syntax highlighting
outline
code completion
navigation
type checker
debugger
syntax definition
static semantics
dynamic semantics
abstract syntax
type system
operational
semantics
type soundness
proof
Declarative Multi-Purpose Language Definition
Syntax
Definition
Name
Binding
Type
Constraints
Dynamic
Semantics
Transform
Declarative Multi-Purpose Language Definition
SDF3: Syntax
Definition
NaBL: Name
Binding
TS: Type
Constraints
DynSem:
Dynamic
Semantics
Stratego:
Transform
Declarative Multi-Purpose Language Definition
SDF3: Syntax
Definition
NaBL: Name
Binding
TS: Type
Constraints
DynSem:
Dynamic
Semantics
Stratego:
Transform
Spoofax Language Workbench

Weitere ähnliche Inhalte

Was ist angesagt?

Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
 

Was ist angesagt? (20)

Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
Declare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) ServicesDeclare Your Language: Syntactic (Editor) Services
Declare Your Language: Syntactic (Editor) Services
 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
 
07. Java Array, Set and Maps
07.  Java Array, Set and Maps07.  Java Array, Set and Maps
07. Java Array, Set and Maps
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
02. Data Types and variables
02. Data Types and variables02. Data Types and variables
02. Data Types and variables
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax DefinitionCompiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax Definition
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
Name binding with scope graphs
Name binding with scope graphsName binding with scope graphs
Name binding with scope graphs
 

Ähnlich wie Separation of Concerns in Language Definition

Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
Avelin Huo
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
Manoj Patil
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory building
ClarkTony
 
Moore_slides.ppt
Moore_slides.pptMoore_slides.ppt
Moore_slides.ppt
butest
 
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
Chunyang Chen
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
DhruvKushwaha12
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
AtreyiB
 

Ähnlich wie Separation of Concerns in Language Definition (20)

A Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query LanguagesA Brief Overview of (Static) Program Query Languages
A Brief Overview of (Static) Program Query Languages
 
Tricks in natural language processing
Tricks in natural language processingTricks in natural language processing
Tricks in natural language processing
 
Python basic
Python basicPython basic
Python basic
 
Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03Designing A Syntax Based Retrieval System03
Designing A Syntax Based Retrieval System03
 
System Programming Unit III
System Programming Unit IIISystem Programming Unit III
System Programming Unit III
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
6. describing syntax and semantics
6. describing syntax and semantics6. describing syntax and semantics
6. describing syntax and semantics
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory building
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Moore_slides.ppt
Moore_slides.pptMoore_slides.ppt
Moore_slides.ppt
 
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
Unsupervised Software-Specific Morphological Forms Inference from Informal Di...
 
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmmUnit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
Java for android developers
Java for android developersJava for android developers
Java for android developers
 
Ch2
Ch2Ch2
Ch2
 
KiwiPyCon 2014 - NLP with Python tutorial
KiwiPyCon 2014 - NLP with Python tutorialKiwiPyCon 2014 - NLP with Python tutorial
KiwiPyCon 2014 - NLP with Python tutorial
 
Programming in Computational Biology
Programming in Computational BiologyProgramming in Computational Biology
Programming in Computational Biology
 

Mehr von Eelco Visser

Mehr von Eelco Visser (17)

CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?
 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code Generation
 
Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2
 
Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1
 

Kürzlich hochgeladen

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Separation of Concerns in Language Definition

  • 1. Separation of Concerns ! in Language Definition Eelco Visser! Delft University of Technology
  • 2. “99% of errors found by Semmle are due to bad language design” — Oege de Moor, CEO Semmle
  • 6. Problem Domain Solution Domain General- Purpose Language • Lack of safety • Lack of abstraction • Distance from domain
  • 9. Solution Domain Problem Domain General- Purpose Language Domain- Specific Language • Language-based safety and security • High-level domain-specific abstraction • Distance from domain
  • 10. Solution Domain Problem Domain General- Purpose Language Domain- Specific Language • Language-based safety and security • High-level domain-specific abstraction • Reduced distance from problem domain
  • 15. Compiler + Editor (IDE) Language Design General- Purpose Language Declarative- Meta Languages Language Design Language workbench Declarative multi-purpose meta-languages Useable language implementations
  • 16. Compiler + Editor (IDE) Language Design General- Purpose Language Declarative- Meta Languages Language Design Language workbench Declarative multi-purpose meta-languages Useable language implementations High quality language designs
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 31. parser type checker code generator interpreter parser error recovery syntax highlighting outline
  • 32. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion
  • 33. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation
  • 34. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker
  • 35. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger
  • 36. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition
  • 37. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics
  • 38. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics
  • 39. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax
  • 40. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax type system
  • 41. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax type system operational semantics
  • 42. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax type system operational semantics type soundness proof
  • 43. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax type system operational semantics type soundness proof
  • 46. int fib(int n) { if(n <= 1) return 1; else return fib(n - 2) + fib(n - 1); } Syntax: Phrase Structure of Programs
  • 47. Syntax: Phrase Structure of Programs } int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ;
  • 48. }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Syntax: Phrase Structure of Programs
  • 49. Exp.IntExp.Int Exp.Leq Statement.Return Exp.Sub Exp.Sub Exp.Call Exp.Call Param.Param IntType ID }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType Syntax: Phrase Structure of Programs
  • 50. Exp.IntExp.Int Exp.Leq Statement.Return Exp.Sub Exp.Sub Exp.Call Exp.Call Exp.Add Param.Param IntType ID }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType Syntax: Phrase Structure of Programs
  • 51. Exp.IntExp.Int Exp.Leq Statement.Return Exp.Sub Exp.Sub Exp.Call Exp.Call Exp.Add Statement.Return Param.Param IntType ID }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType Syntax: Phrase Structure of Programs
  • 52. Exp.IntExp.Int Exp.Leq Statement.Return Exp.Sub Exp.Sub Exp.Call Exp.Call Exp.Add Statement.Return Param.Param IntType Statement.If ID }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType Syntax: Phrase Structure of Programs
  • 53. Exp.IntExp.Int Exp.Leq Statement.Return Exp.Sub Exp.Sub Exp.Call Exp.Call Exp.Add Statement.Return Param.Param IntType Definition.Function Statement.If ID }int fib ( int n ) { if ( n <= 1 ) return 1 ; else return fib ( n - 2 ) + fib ( n - 1 ) ; Exp.IntExp.Var Exp.IntExp.VarExp.VarIntType Syntax: Phrase Structure of Programs
  • 54. Function IntType IntType Param Var Int Int Leq Return Return Call Add Var Int Sub Var Int Sub Call If fib n n 1 1 fib n 2 fib n 1 Abstract! Syntax! Tree
  • 55. int fib(int n) { if(n <= 1) return 1; else return fib(n - 2) + fib(n - 1); } Text parse
  • 56. Function( IntType() , "fib" , [Param(IntType(), "n")] , [ If( Leq(Var("n"), Int("1")) , Int("1") , Add( Call("fib", [Sub(Var("n"), Int("2"))]) , Call("fib", [Sub(Var("n"), Int("1"))]) ) ) ] ) int fib(int n) { if(n <= 1) return 1; else return fib(n - 2) + fib(n - 1); } Abstract ! Syntax ! Term Text parse
  • 57. Type Definition.Function ID }( ) {Param* Statement* Exp Statement.Return return ; Statement.If if ( ) elseExp Statement Statement Exp.Add +Exp Exp Exp.Var ID Understanding Syntax =! Understanding Tree Structure parse(prettyprint(t)) = t No need to understand ! how parse works!
  • 58. Demo: Syntax Definition in SDF3 Language Design Syntax! Definition Name ! Binding Type! Constraints Dynamic! Semantics Transform
  • 59. Type Definition.Function ID }( ) {Param* Statement* Exp Statement.Return return ; Statement.If if ( ) elseExp Statement Statement Exp.Add +Exp Exp templates Definition.Function = < <Type> <ID>(<Param*; separator=",">) { <Statement*; separator="n"> } > ! Statement.If = < if(<Exp>) <Statement> else <Statement> > ! Statement.Return = <return <Exp>;> ! Exp.Add = <<Exp> + <Exp>> ! Exp.Var = <<ID>>Exp.Var ID The Syntax Definition Formalism SDF3
  • 60. Statement.If = < if(<Exp>) <Statement> else <Statement> > Parser Abstract syntax tree schema Pretty-Printer Syntactic completion templates Folding rules Error recovery rules Syntactic coloring Outline rules Syntax Definition Multi-Purpose Declarative Syntax Definition
  • 61. Name and Type Analysis
  • 62. Name Binding & Scope Rules int fib(int n) { if(n <= 1) return 1; else return fib(n - 2) + fib(n - 1); } what does this variable refer to? Our contribution ! - declarative language for name binding & scope rules - generation of incremental name resolution algorithm ! - Konat, Kats, Wachsmuth, Visser (SLE 2012) - Wachsmuth, Konat, Vergu, Groenewegen, Visser (SLE 2013) which function is being called here? Needed for ! - checking correct use of names and types - lookup in interpretation and compilation - navigation in IDE - code completion State-of-the-art ! - programmatic encoding of name resolution algorithms
  • 63. Demo: Name and Type Analysis in NaBL+TS Language Design Syntax! Definition Name ! Binding Type! Constraints Dynamic! Semantics Transform
  • 64. Declarative Name Binding and Scope Rules binding rules ! Param(t, name) : defines Variable name Var(name) : refers to Variable name ! Function(t, name, param*, s) : defines Function name scopes Variable, Function ! Call(name, exp*) : refers to Function name Incremental name resolution algorithm Name checks Semantic code completion Reference resolution
  • 65. Semantics of Name Binding? binding rules ! Param(t, name) : defines Variable name Var(name) : refers to Variable name ! Function(t, name, param*, s) : defines Function name scopes Variable, Function ! Call(name, exp*) : refers to Function name Research: how to characterize correctness of the result of name resolution without appealing to the algorithm itself? Analogy: declarative semantics of syntax definition
  • 68. rules Num(i) --> I(i) Ifz(e1, e2, e3) --> v where e1 --> I(i), i = 0, e2 --> v Ifz(e1, e2, e3) --> v where e1 --> I(i), i != 0, e3 --> v Add(e1, e2) --> I(addInt(i, j)) where e1 --> I(i), e2 --> I(j) Sub(e1, e2) --> I(subInt(i, j)) where e1 --> I(i), e2 --> I(j) Mul(e1, e2) --> I(mulInt(i, j)) where e1 --> I(i), e2 --> I(j) DynSem: Dynamic Semantics Specification module semantics ! rules ! E env |- Var(x) --> v where env[x] => T(e, env'), E env' |- e --> v E env |- Fun(Param(x, t), e) --> C(x, e, env) ! E env |- App(e1, e2) --> v where E env |- e1 --> C(x, e, env'), E {x |--> T(e2, env), env'} |- e --> v E env |- Fix(Param(x, t), e) --> v where E {x |--> T(Fix(Param(x,t),e),env), env} |- e --> v E env |- Let(x, t, e1, e2) --> v where E {x |--> T(e1, env), env} |- e2 --> v
  • 69. Implicitly-Modular Structural Operational Semantics (I-MSOS)* rules ! E env |- Var(x) --> v where env[x] => T(e, env'), E env' |- e --> v ! Add(e1, e2) --> I(addInt(i, j)) where e1 --> I(i), e2 --> I(j) rules ! E env |- Var(x) --> v where env[x] => T(e, env'), E env' |- e --> v ! E env |- Add(e1, e2) --> I(addInt(i, j)) where E env |- e1 --> I(i), E env |- e2 --> I(j)explicate * P. D. Mosses. Modular structural operational semantics. JLP, 60-61:195–228, 2004. M. Churchill, P. D. Mosses, and P. Torrini. Reusable components of semantic specifications. In MODULARITY, April 2014.
  • 70. rules Ifz(e1, e2, e3) --> v where e1 --> I(i), i = 0, e2 --> v Ifz(e1, e2, e3) --> v where e1 --> I(i), i != 0, e3 --> v Interpreter Generation rules E env |- Ifz(e1, e2, e3) --> v where E env |- e1 --> I(i), [i = 0, E env |- e2 --> v] + i != 0, E env |- e3 --> v] explicate & merge
  • 71. module PCF sorts Exp Param Type templates Exp.Var = [[ID]] Exp.App = [[Exp] [Exp]] {left} Exp.Fun = [ fun [Param] ( [Exp] ) ] Exp.Fix = [ fix [Param] ( [Exp] ) ] Exp.Let = [ let [ID] : [Type] = [Exp] in [Exp] ] Exp.Num = [[INT]] Exp.Add = [[Exp] + [Exp]] {left} Exp.Sub = [[Exp] - [Exp]] {left} Exp.Mul = [[Exp] * [Exp]] {left} Exp = [([Exp])] {bracket} Exp.Ifz = [ ifz [Exp] then [Exp] else [Exp] ] Type.IntType = [int] Type.FunType = [[Type] -> [Type]] Param.Param = [[ID] : [Type]] context-free priorities ! Exp.App > Exp.Mul > {left: Exp.Add Exp.Sub} > Exp.Ifz module types type rules ! Var(x) : t where definition of x : t Param(x, t) : t Fun(p, e) : FunType(tp, te) where p : tp and e : te App(e1, e2) : tr where e1 : FunType(tf, tr) and e2 : ta and tf == ta else error "type mismatch" on e2 ! Fix(p, e) : tp where p : tp and e : te and tp == te else error "type mismatch" on p Let(x, tx, e1, e2) : t2 where e2 : t2 and e1 : t1 and t1 == tx else error "type mismatch" on e1 Num(i) : IntType() ! Ifz(e1, e2, e3) : t2 where e1 : IntType() and e2 : t2 and e3 : t3 and t2 == t3 else error "types not compatible" on e3 e@Add(e1, e2) + e@Sub(e1, e2) + e@Mul(e1, e2) : IntType() where e1 : IntType() else error "Int type expected" on e and e2 : IntType() else error "Int type expected" on e module semantics ! rules ! E env |- Var(x) --> v where env[x] => T(e, env'), E env' |- e --> v E env |- Fun(Param(x, t), e) --> C(x, e, env) ! E env |- App(e1, e2) --> v where E env |- e1 --> C(x, e, env'), E {x |--> T(e2, env), env'} |- e --> v E env |- Fix(Param(x, t), e) --> v where E {x |--> T(Fix(Param(x,t),e),env), env} |- e --> v E env |- Let(x, t, e1, e2) --> v where E {x |--> T(e1, env), env} |- e2 --> v rules Num(i) --> I(i) Ifz(e1, e2, e3) --> v where e1 --> I(i), i = 0, e2 --> v Ifz(e1, e2, e3) --> v where e1 --> I(i), i != 0, e3 --> v Add(e1, e2) --> I(addInt(i, j)) where e1 --> I(i), e2 --> I(j) Sub(e1, e2) --> I(subInt(i, j)) where e1 --> I(i), e2 --> I(j) Mul(e1, e2) --> I(mulInt(i, j)) where e1 --> I(i), e2 --> I(j) module names namespaces Variable binding rules ! Var(x) : refers to Variable x Param(x, t) : defines Variable x of type t Fun(p, e) : scopes Variable Fix(p, e) : scopes Variable Let(x, t, e1, e2) : defines Variable x of type t in e2 First Little (Big) Step: From PCF in Spoofax …
  • 72. else [Exp] ] Type.IntType = [int] Type.FunType = [[Type] -> [Type]] Param.Param = [[ID] : [Type]] context-free priorities ! Exp.App > Exp.Mul > {left: Exp.Add Exp.Sub} > Exp.Ifz Ifz(e1, e2, e3) : t2 where e1 : IntType() and e2 : t2 and e3 : t3 and t2 == t3 else error "types not compatible" on e3 e@Add(e1, e2) + e@Sub(e1, e2) + e@Mul(e1, e2) : IntType() where e1 : IntType() else error "Int type expected" on e and e2 : IntType() else error "Int type expected" on e Ifz(e1, e2, e3) --> v where e1 --> I(i), i != 0, e3 --> v Add(e1, e2) --> I(addInt(i, j)) where e1 --> I(i), e2 --> I(j) Sub(e1, e2) --> I(subInt(i, j)) where e1 --> I(i), e2 --> I(j) Mul(e1, e2) --> I(mulInt(i, j)) where e1 --> I(i), e2 --> I(j) Param(x, t) : defines Variable x of type t Fun(p, e) : scopes Variable Fix(p, e) : scopes Variable Let(x, t, e1, e2) : defines Variable x of type t in e2 Inductive has_type (C: Context) : term -> term -> Prop := | VarC_ht ns k0 t x k1 : lookup C x ns k0 t -> has_type C (Co VarC [Id x k0] k1) t | ParamC_ht x t k0 : has_type C (Co ParamC [x;t] k0) t | FunC_ht k0 t_p t_e p e k1 : has_type C p t_p -> has_type C e t_e -> has_type C (Co FunC [p;e] k1) (Co FunTypeC [t_p;t_e] k0) | FixC_ht t_p t_e p e k0 : has_type C p t_p -> has_type C e t_e -> (t_p = t_e) -> has_type C (Co FixC [p;e] k0) t_p | AppC_ht t_r k0 t_f t_a e1 e2 k1 : has_type C e1 (Co FunTypeC [t_f;t_r] k0) -> has_type C e2 t_a -> (t_f = t_a) -> has_type C (Co AppC [e1;e2] k1) t_r | LetC_ht t2 t1 x t_x e1 e2 k0 : has_type C e2 t2 -> has_type C e1 t1 -> (t1 = t_x) -> has_type C (Co LetC [x;t_x;e1;e2] k0) t2 | NumC_ht k0 i k1 : has_type C (Co NumC [i] k1) (Co IntTypeC [] k0) | IfzC_ht k0 t2 t3 e1 e2 e3 k1 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 t2 -> has_type C e3 t3 -> (t2 = t3) -> has_type C (Co IfzC [e1;e2;e3] k1) t2 | AddC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co AddC [e1;e2] k3) (Co IntTypeC [] k2) | SubC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co SubC [e1;e2] k3) (Co IntTypeC [] k2) | MulC_ht k2 k0 k1 e1 e2 k3 : has_type C e1 (Co IntTypeC [] k0) -> has_type C e2 (Co IntTypeC [] k1) -> has_type C (Co MulC [e1;e2] k3) (Co IntTypeC [] k2) | HT_eq e ty1 ty2 (hty1: has_type C e ty1) (tyeq: term_eq ty1 ty2) : has_type C e ty2 . Inductive semantics_cbn : Env -> term -> value -> Prop := | Var0C_sem env' e env x k0 v : get_env x env e env' -> semantics_cbn env' e v -> semantics_cbn env (Co VarC [x] k0) v | Fun0C_sem t k1 k0 x e env : semantics_cbn env (Co FunC [Co ParamC [x;t] k1;e] k0) (Clos x e env) | Fix0C_sem k1 k0 env x t k3 e k2 v : semantics_cbn { x |--> (Co FixC [Co ParamC [x;t] k1;e] k0,env), env } e v -> semantics_cbn env (Co FixC [Co ParamC [x;t] k3;e] k2) v | App0C_sem env' x e env e1 e2 k0 v : semantics_cbn env e1 (Clos x e env') -> semantics_cbn { x |--> (e2,env), env' } e v -> semantics_cbn env (Co AppC [e1;e2] k0) v | Let0C_sem env x t e1 e2 k0 v : semantics_cbn { x |--> (e1,env), env } e2 v -> semantics_cbn env (Co LetC [x;t;e1;e2] k0) v | Num0C_sem env k0 i : semantics_cbn env (Co NumC [i] k0) (Natval i) | Ifz0C_sem i env e1 e2 e3 k0 v : semantics_cbn env e1 (Natval i) -> (i = 0) -> semantics_cbn env e2 v -> semantics_cbn env (Co IfzC [e1;e2;e3] k0) v | Ifz1C_sem i env e1 e2 e3 k0 v : semantics_cbn env e1 (Natval i) -> (i <> 0) -> semantics_cbn env e3 v -> semantics_cbn env (Co IfzC [e1;e2;e3] k0) v | Add0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co AddC [e1;e2] k0) (plus i j) | Sub0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co SubC [e1;e2] k0) (minus i j) | Mul0C_sem env e1 e2 k0 i j : semantics_cbn env e1 (Natval i) -> semantics_cbn env e2 (Natval j) -> semantics_cbn env (Co MulC [e1;e2] k0) (mult i j) . Inductive ID_NS : Set := | VariableNS . !Definition NS := ID_NS . !Inductive scopesR : term -> NS -> Prop := | Fun_scopes_Variable p e k0 : scopesR (Co FunC [p;e] k0) VariableNS | Fix_scopes_Variable p e k0 : scopesR (Co FixC [p;e] k0) VariableNS . !Definition scopes_R := scopesR . !Inductive definesR : term -> Ident -> NS -> key -> Prop := | Param_defines_Variable x k1 t k0 : definesR (Co ParamC [Id x k1;t] k0) x VariableNS k1 . !Definition defines_R := definesR . !Inductive refers_toR : term -> Ident -> NS -> key -> Prop := | Var_refers_to_Variable x k1 k0 : refers_toR (Co VarC [Id x k1] k0) x VariableNS k1 . !Definition refers_to_R := refers_toR . !Inductive typed_definesR : term -> Ident -> NS -> term -> key -> Prop := | Param_typed_defines_Variable x t k1 t k0 : typed_definesR (Co ParamC [Id x k1;t] k0) x VariableNS t k1 . !Definition typed_defines_R := typed_definesR . Inductive sorts : Set := | Param_S | ID_S | INT_S | Exp_S | Type_S . !Parameter Ident : Set. !Definition sort := sorts . !Definition Ident_Sort := ID_S . !Inductive Constructors := | INTC (n: nat) | VarC | FunC | FixC | AppC | LetC | ParamC | NumC | AddC | SubC | MulC | DivC | IfzC | IntTypeC | FunTypeC . !Definition constructors := Constructors . !Fixpoint get_sig (x: constructors) : list sort * sort := match x with | INTC n => ([],INT_S) | VarC => ([ID_S],Exp_S) | FunC => ([Param_S;Exp_S],Exp_S) | FixC => ([Param_S;Exp_S],Exp_S) | AppC => ([Exp_S;Exp_S],Exp_S) | LetC => ([ID_S;Type_S;Exp_S;Exp_S],Exp_S) | ParamC => ([ID_S;Type_S],Param_S) | NumC => ([INT_S],Exp_S) | AddC => ([Exp_S;Exp_S],Exp_S) | SubC => ([Exp_S;Exp_S],Exp_S) | MulC => ([Exp_S;Exp_S],Exp_S) | DivC => ([Exp_S;Exp_S],Exp_S) | IfzC => ([Exp_S;Exp_S;Exp_S],Exp_S) | IntTypeC => ([],Type_S) | FunTypeC => ([Type_S;Type_S],Type_S) end. … to PCF in Coq (+ manual proof of type preservation)
  • 74. parser type checker code generator interpreter parser error recovery syntax highlighting outline code completion navigation type checker debugger syntax definition static semantics dynamic semantics abstract syntax type system operational semantics type soundness proof
  • 75. Declarative Multi-Purpose Language Definition Syntax Definition Name Binding Type Constraints Dynamic Semantics Transform
  • 76. Declarative Multi-Purpose Language Definition SDF3: Syntax Definition NaBL: Name Binding TS: Type Constraints DynSem: Dynamic Semantics Stratego: Transform
  • 77. Declarative Multi-Purpose Language Definition SDF3: Syntax Definition NaBL: Name Binding TS: Type Constraints DynSem: Dynamic Semantics Stratego: Transform Spoofax Language Workbench