domain-specific type systems
Post on 15-Jul-2015
88 Views
Preview:
TRANSCRIPT
Domain-Specific Type Systems
Stratego
SDF2
ESV editor
SPT tests
3
syntax definition
concrete syntax
abstract syntax
static semantics
name binding
type system
dynamic semantics
translation
interpretation
Domain-Specific Type Systems 4
rules // declare
declare-all = alltd(declare); rename-all
declare : Def(x, Type(ty), e) -> Def(y, Type(ty), e) where <rename-var> (x, ty) => y
Declare, Rename, Type, Check
Domain-Specific Type Systems 5
rules // rename expressions
rename-all = alltd(rename) rename-var : (x, ty) -> y with y := x{<new>} with rules( RenameId : x -> y TypeOf : y -> ty ) rename : Var(x) -> Var(y) where y := <RenameId> x
rename : Def(x, Type(ty), e) -> Def(x, Type(ty), e') where {| RenameId : e' := <rename-all> e |} rename : Let([Bind(x, Type(ty), e1)], e2) -> Let([Bind(y, Type(ty), e1')], e2') where <rename-all> e1 => e1' ; {| RenameId : <rename-var> (x, ty) => y ; <rename-all> e2 => e2' |} rename : Fun(param, e) -> Fun(param', e') where {| RenameId : param' := <rename-all> param ; e' := <rename-all> e |} rename : Param(x, ty) -> Param(y, ty) with y := <rename-var>(x, ty)
Declare, Rename, Type, Check
Domain-Specific Type Systems 6
rules // type of expressions
type-of : Int(_) -> BaseType("int")
type-of : Var(name) -> ty where <TypeOf> name => ty
type-of : Param(name, ty) -> ty
type-of : Fun(param, e) -> FunType(ty1, ty2) where <type-of> param => ty1
; <type-of> e => ty2
type-of : Fix(param, e) -> ty_param where <type-of> param => ty_param
; <type-of> e => ty_e
type-of : Let(binds, e) -> <type-of> e type-of : App(e1, e2) -> ty_res where <type-of> e1 => FunType(ty_arg, ty_res) ; <type-of> e2 => ty_e2 type-of : Ifz(e1, e2, e2) -> ty_e2 where <type-of> e2 => ty_e2 type-of : BinOp(e1, op, e2) -> BaseType("int")
Declare, Rename, Type, Check
Domain-Specific Type Systems 7
rules // checking
check-all = collect(check)
check : Var(x) -> (x, $[variable not declared]) where require(<type-of>Var(x)) check : Def(name, Type(ty), e) -> (e, $[type mismatch]) where <type-of> e => ty_e ; require(<eq>(ty_e, ty))
check : App(e1, e2) -> (e2, $[function expected]) where <type-of> e1 => ty_e1 ; require(!ty_e1 => FunType(ty_arg, ty_res)) check : App(e1, e2) -> (e2, $[argument type mismatch])
where <type-of> e1 => FunType(ty_arg, ty_res) ; <type-of> e2 => ty_e2 ; require(<eq>(ty_arg, ty_e2))
Declare, Rename, Type, Check
Domain-Specific Type Systems
Stratego
NaBL TS
SDF3
ESV editor
SPT tests
9
syntax definition
concrete syntax
abstract syntax
static semantics
name binding
type system
dynamic semantics
translation
interpretation
Domain-Specific Type Systems
binding rules
Def(name, Type(ty), e) : defines Variable name of type ty Def(name, e) : defines Variable name of type ty where e has type ty Var(name) : refers to Variable name
10
type rules
Int(_): BaseType("int")
Var(v): ty where definition of v: ty Param(name, ty): ty
Fun(param, e): FunType(pty, ety) where param: pty and e: ety Fix(param, e): pty where param: pty and e: ety and pty == ety else error "type mismatch" on param
Let(binds, e): ty where e: ty App(e1, e2): ty where e1: FunType(pty, ty) else error "function expected" on e1 and e2: pty else error "argument type mismatch" on e2 Ifz(e1, e2, e3): ty2 where e1: BaseType("int") else error "int expected" on e1 and e2: ty2 and e3: ty3 and ty2 == ty3 else error "type mismatch" on e3
Software Languages 11
tasks A Language Independent Task Engine for Incremental Name and Type Analysis
Guido Wachsmuth, Gabriël Konat, Vlad Vergu, Danny Groenewegen, and Eelco Visser Software Language Engineering (SLE) 2013
Domain-Specific Type Systems 12
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}int
int
B
int
int
int
Domain-Specific Type Systems 13
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}int
int
B
int
int
int
Units of computation ...
Domain-Specific Type Systems 14
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}int
int
B
int
int
int
... with cacheable results ...
Domain-Specific Type Systems 15
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}int
int
B
int
int
int
... and dependencies on each other
Domain-Specific Type Systems 16
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}int
int
B
int
int
int
resolve class A resolve class B
resolve method m resolve field b
resolve field i
Domain-Specific Type Systems 17
class C : A {
int n() { return m(); }}
class A {
B b;
int m;
int m() { return 1 + b.i; }}
class B {
int i;
float f;
int m() { return 0; }}
resolve class A resolve class B
resolve method m resolve field b
resolve field i
calc type of m()
calc type of i
calc type of b
calc type of b.i
calc type of 1 + b.i
calc type of 0
Domain-Specific Type Systems 18
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field i
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.i
calc type of i
calc type of b.i
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.i; }}
resolve class B1
6
5
7
2
4
3
8
Facts
Tasks
define class A
Domain-Specific Type Systems 19
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field i
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.i
calc type of i
calc type of b.i
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.i; }}
resolve class B
define class A
i
Domain-Specific Type Systems
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field i
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.i
calc type of i
calc type of b.i
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.i; }}
resolve class B
define class A
f
20
i
Domain-Specific Type Systems
resolve field i
calc type of 1 + b.i
calc type of i
calc type of b.i
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.i; }}
resolve class B
define class A
f
21
i
Domain-Specific Type Systems
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.i; }}
resolve class B
define class A
f
22
i
resolve field f
calc type of 1 + b.f
calc type of f
calc type of b.f
2
1
3 4
Domain-Specific Type Systems
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field f
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.f
calc type of f
calc type of b.f
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
float f;
23
Domain-Specific Type Systems
type of i is int
define field i
define field f
type of f is float
class B { int i; float f;}
define class B
resolve field f
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.f
calc type of f
calc type of b.f
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
24
Domain-Specific Type Systems
type of i is int
define field i
class B { int i; float f;}
define class B
resolve field f
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.f
calc type of f
calc type of b.f
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
25
define field f
type of f is float
Domain-Specific Type Systems
type of i is int
define field i
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
calc type of 1 + b.fcalc type of b.f
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
26
resolve field f
calc type of f
Domain-Specific Type Systems
type of i is int
define field i
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
27
resolve field f
calc type of 1 + b.f
calc type of f
calc type of b.f
2
1
3
Domain-Specific Type Systems
type of i is int
define field i
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
define field b
define method m
type of b is B
type of m is float
class A { B b; float m() { return 1 + b.f; }}
resolve class B
define class A
28
resolve field f
calc type of 1 + b.f
calc type of f
calc type of b.f
Domain-Specific Type Systems
class A { B b; float m() { return 1 + b.f; }}
type of i is int
define field i
class B { int i; float f;}
define class B
resolve field b
calc type of b
calc type of 1
define field b
define method m
type of b is B
type of m is float
resolve class B
define class A
29
resolve field f
calc type of 1 + b.f
calc type of f
calc type of b.f
Domain-Specific Type Systems
type rules
True() : BoolTy() False() : BoolTy()
Not(e) : BoolTy()where e : BoolTy() else error "Expected boolean" on e
And(x, y) + Or(x, y) : BoolTy()where x : BoolTy() else error "Expected boolean" on x
and y : BoolTy() else error "Expected boolean" on y
31
type rules // Arithmetic expressions IntLit(_) : IntTy() FloatLit(_) : FloatTy() MoneyLit(_) : MoneyTy()
t@Plus(x, y) + t@Minus(x, y) : tywhere x : x-ty
and y : y-ty and ( (x-ty <is: Numeric() and y-ty <is: Numeric()) or (x-ty == MoneyTy() and y-ty == MoneyTy()) ) else error $[Cannot add or subtract [x-ty] and [y-ty]] on t and <largest-type> (x-ty, y-ty) => ty
Domain-Specific Type Systems 32
signatures
Numeric : TypeKind
relations
IntTy() <is: Numeric()FloatTy() <is: Numeric()
type functions
largest-type : (x-ty, y-ty) -> tywhere ((x-ty == MoneyTy() or y-ty == MoneyTy()) and MoneyTy() => ty) or ((x-ty == FloatTy() or y-ty == FloatTy()) and FloatTy() => ty) or y-ty => ty
Domain-Specific Type Systems 33
relations
define transitive <depends-on:
type rules
Ref(qid) has dependency qid Not(e) has dependency dep where e has dependency dep And(e1, e2) + Or(e1, e2) + Lt(e1, e2) + Gt(e1, e2) + Leq(e1, e2) + Geq(e1, e2)+ Plus(e1, e2) + Minus(e1, e2) + Mul(e1, e2) + Div(e1, e2) has dependency dep1 union dep2 where e1 has dependency dep1 and e2 has dependency dep2
Computed(qid, _, TExpr(_, e)):- where e has dependency dep and store qid <depends-on: dep Conditional(e, [Question(qid, _, _)]):- where e has dependency dep and store qid <depends-on: dep
Conditional(e, [Question(qid, _, _)]):- where e has dependency dep and not( dep <depends-on: qid ) else error "cyclic dependency" on qid
Domain-Specific Type Systems 35
TemplateProductionWithCons(SortCons(SortDef(s), Constructor(c)), templ, _): defines non-unique Sort s of type SortType(s) of constructorName c defines unique Constructor c of type FunType(ty*, SortType(s)) where templ has type ty*
Placeholder(s, opt) : t where s : t
create-type-task(|ctx) = ?Template(<mapconcat(?Line(<filter(is-placeh; create-type-task(|ctx))>))>) ; type-is(|ctx)
Domain-Specific Type Systems 37
int z = +INF; int x = 0; bool b = (x < z); bool c = (x < (z + 1)); int w = −1 ∗ +INF; int y = +INF foreach (n: G.nodes) { y min= n.A; }
int i = 3; float f = 0.1; long l = 10; double d = 0.2; long l2 = i; int i2 = l; int i3 = (int) l; float f2 = i; float f3 = (float) i;
proc foo(G1, G2: graph, n: node(G1), m: node(G2)) { bool b = (n == m); }
procedure foo (G: graph , A: nodeProp<int >(G) ) { nodeProp<int>(G) B; foreach (n: G.nodes) { nodeProp<int>(G) C; foreach (k: G.nodes) { k.C = k.A ∗ n.A + 1;
} n.B = max(k: G.nodes) (k.C > 0){k.A} } }
procedure foo (G1,G2: graph ; A: nodeProp<int >(G1) ) { node(G2) a; a.A = 0; edge(G1) b; b.A = 0; node(G1) c; bool z = c.A; }
Domain-Specific Type Systems 39
int z = +INF; int x = 0; bool b = (x < z); bool c = (x < (z + 1)); int w = −1 ∗ +INF; int y = +INF foreach (n: G.nodes) { y min= n.A; }
int i = 3; float f = 0.1; long l = 10; double d = 0.2; long l2 = i; int i2 = l; int i3 = (int) l; float f2 = i; float f3 = (float) i;
proc foo(G1, G2: graph, n: node(G1), m: node(G2)) { bool b = (n == m); }
procedure foo (G: graph , A: nodeProp<int >(G) ) { nodeProp<int>(G) B; foreach (n: G.nodes) { nodeProp<int>(G) C; foreach (k: G.nodes) { k.C = k.A ∗ n.A + 1;
} n.B = max(k: G.nodes) (k.C > 0){k.A} } }
procedure foo (G1,G2: graph ; A: nodeProp<int >(G1) ) { node(G2) a; a.A = 0; edge(G1) b; b.A = 0; node(G1) c; bool z = c.A; }
Domain-Specific Type Systems 40
Except where otherwise noted, this work is licensed under
top related