13
Existential Types Existential Types Polymorphic Type Inferenc Polymorphic Type Inferenc e and Abstract Data Types e and Abstract Data Types by Martin Odersky by Martin Odersky

Existential Types

  • Upload
    dezso

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

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] - PowerPoint PPT Presentation

Citation preview

Page 1: Existential Types

Existential TypesExistential Types

Polymorphic Type Inference and Polymorphic Type Inference and Abstract Data TypesAbstract Data Typesby Martin Oderskyby Martin Odersky

Page 2: Existential Types

Table Of ContentTable Of Content

Existential typeExistential type Syntax of ExMLSyntax of ExML Formal syntax of ExMLFormal syntax of ExML Type inference rulesType inference rules Type inference algorithmType inference algorithm Semantic functionSemantic function

Page 3: Existential Types

Existential TypeExistential Type [Mit88] and [Cardelli85][Mit88] and [Cardelli85] abstypeabstype complex complex withwith

creat: real creat: real →real → complex,→real → complex, plus: complex → complex, plus: complex → complex, re: complex → real, re: complex → real, im: complex → real im: complex → realisispackpack real real ∧ ∧ realreal λλx: real. x: real. λλy: real. <x, y>y: real. <x, y> λλz: real z: real ∧∧ real. real. λλw: real w: real ∧∧ real. real. <fst(z) + fst(w), snd(z) + snd(w)> <fst(z) + fst(w), snd(z) + snd(w)> λλz: real z: real ∧∧ real. fst(z) real. fst(z) λλz: real z: real ∧∧ real. snd(z) real. snd(z)to to ∃t. [(real ∃t. [(real →→ real real →→ t) t) ∧ ∧ (t (t → t) → t) ∧∧ (t → real) (t → real) ∧ ∧ (t → real)(t → real)] ]

Page 4: Existential Types

Syntax of ExMLSyntax of ExML

typetype [args] T = K [args] T = K11 ofof ττ 11 | … | K | … | Kkk ofof ττ kk typetype KEY = Key KEY = Key ofof ‘a * (‘a -> int) ‘a * (‘a -> int) Key(3, fun x -> 5)Key(3, fun x -> 5) Key([1, 2, 3], list_length)Key([1, 2, 3], list_length) Key(3, list_length) Key(3, list_length) (* error *)(* error *) letlet (Key(v,f)) = x (Key(v,f)) = x inin f v f v letlet (Key(v,f)) = x (Key(v,f)) = x inin v v (* error *)(* error *)

Page 5: Existential Types

Syntax of ExML (cont.)Syntax of ExML (cont.)

typetype Key = {x : ‘a; f : ‘a -> int} Key = {x : ‘a; f : ‘a -> int} letlet z = {x = 3, f = fun x -> x + 2} z = {x = 3, f = fun x -> x + 2} inin

z.f z.x z.f z.x letlet z = {x = 3, f = fun x -> x + 2} z = {x = 3, f = fun x -> x + 2} inin

z.f z.f (* error *)(* error *) letlet z = {x = 3, f = fun x -> x + 2} z = {x = 3, f = fun x -> x + 2} inin

letlet y = z y = z inin z.f y.x z.f y.x (* error *)(* error *)

Page 6: Existential Types

Syntax of ExML (cont.)Syntax of ExML (cont.)

typetype ‘a STACK = ‘a STACK = {Self : ‘b; {Self : ‘b; Push : ‘a -> ‘b -> ‘b; Push : ‘a -> ‘b -> ‘b; Pop : ‘b -> ‘b; Pop : ‘b -> ‘b; Top : ‘b -> ‘a; Top : ‘b -> ‘a; Null : ‘b -> bool} Null : ‘b -> bool}

letlet push x s = s with {Self = s.Push x s.Self} push x s = s with {Self = s.Push x s.Self} letlet top s = s.Top s.Self top s = s.Top s.Self

Page 7: Existential Types

Formal Syntax of ExMLFormal Syntax of ExML

Page 8: Existential Types

Original Type Inference RuleOriginal Type Inference Rule

Page 9: Existential Types

Type Inference Rule ExtensionType Inference Rule Extension

Page 10: Existential Types

Original Type Inference AlgorithmOriginal Type Inference Algorithm

Page 11: Existential Types

Type Inference Algorithm ExtensionType Inference Algorithm Extension

Page 12: Existential Types

Semantic Function for ExpressionSemantic Function for Expression

Page 13: Existential Types

Semantic Function for TypesSemantic Function for Types