130 likes | 354 Views
Existential Types. Polymorphic Type Inference and Abstract Data Types by Martin Odersky. Table Of Content. Existential type Syntax of ExML Formal syntax of ExML Type inference rules Type inference algorithm Semantic function. Existential Type. [Mit88] and [Cardelli85]
E N D
Existential Types Polymorphic Type Inference and Abstract Data Typesby Martin Odersky
Table Of Content • Existential type • Syntax of ExML • Formal syntax of ExML • Type inference rules • Type inference algorithm • Semantic function
Existential Type • [Mit88] and [Cardelli85] • abstype complex with creat: real →real → complex, plus: complex → complex, re: complex → real, im: complex → realispack real ∧ realλx: real. λy: real. <x, y>λz: real ∧ real. λw: real ∧ real. <fst(z) + fst(w), snd(z) + snd(w)>λz: real ∧ real. fst(z)λz: real ∧ real. snd(z)to ∃t. [(real → real → t) ∧ (t → t) ∧(t → real) ∧ (t → real)]
Syntax of ExML • type [args] T = K1ofτ1 | … | Kkofτk • type KEY = Key of‘a * (‘a -> int) • Key(3, fun x -> 5) • Key([1, 2, 3], list_length) • Key(3, list_length) (* error *) • let (Key(v,f)) = x in f v • let (Key(v,f)) = x in v (* error *)
Syntax of ExML (cont.) • type Key = {x : ‘a; f : ‘a -> int} • let z = {x = 3, f = fun x -> x + 2} in z.f z.x • let z = {x = 3, f = fun x -> x + 2} in z.f (* error *) • let z = {x = 3, f = fun x -> x + 2} inlet y = z in z.f y.x (* error *)
Syntax of ExML (cont.) • type‘a STACK = {Self : ‘b; Push : ‘a -> ‘b -> ‘b; Pop : ‘b -> ‘b; Top : ‘b -> ‘a; Null : ‘b -> bool} • let push x s = s with {Self = s.Push x s.Self} • let top s = s.Top s.Self