200 likes | 340 Views
The Essence of ML Type Inference. Francois Pottier and Didier Remy Type Refinements Seminar. Outline. Extend system with constructs for practical programming Sums, Products Datatypes Row Types. Encoding Pairs. Extensions to signature Type constructor £ : * * ) *
E N D
The Essence of ML Type Inference Francois Pottier and Didier Remy Type Refinements Seminar
Outline • Extend system with constructs for practical programming • Sums, Products • Datatypes • Row Types
Encoding Pairs • Extensions to signature • Type constructor £ : * * ) * • Constructor (¢,¢) : 8 X Y.X ! Y ! X £ Y • Destructors 1 : 8 X Y.X £ Y ! X 2 : 8 X Y.X £ Y ! X • rule i (v1,v2) ! vi
Encoding Sums • Extensions to signature • Type constructor + : * * ) * • Constructors inj1 : 8 XY.X ! X + Y inj2 : 8 XY.Y ! X + Y • Destructor case:8XYZ.(X+Y)!(X!Z)!(Y!Z)!Z • rule case (inji v) v1 v2! (vi v)
Problems • Components must be referred to by numeric index (i, inji ) • Unbounded Data Structures not definable • e.g. letrec length = l.case l (_.0) ( z.1 + length (2 z)) • Gives rise to constraint X=Y+(Z£ X)
Algebraic Data Types • Abstract Type isomorphic to a (k-ary) product or sum type by definition • DX 'ki=1 li : Ti • DX 'ki=1 li : Ti • Here, D ranges over datatypes • li2 L, a set of labels
Datatypes (sums) • For each DX 'ki=1 li: Ti declared • k constructors of arity 1 named li • A destructor of arity k+1 caseD • li : 8X. Ti! DX • caseD : 8XZ.DX! (T1! Z) Z • caseD (li v) v1 vk! vi v
Datatypes (products) • For each DX 'ki=1 li: Ti declared • A constructor of arity k makeD • k destructors of arity 1 named li • li : 8X. DX ! Ti • makeD : 8X.T1! Tk! D • (makeD v1 vk).li! vi
Need for Rows • We cannot write code operating uniformly on different variants or records • e.g. polymorphic record access: access a particular field • e.g. polymorphic record extension: update or create a particular field
Rows • Mappings from labels to types • Can be defined incrementally • Can be abstracted by type variable • Define polymorphic records and variants
Specifying Types • Have unary type constructors • “ Takes a row and returns a record type • o Takes a row and returns a variant type • Row Types specified as mapping from all possible labels to types • Use presence types
Rows • Two type constructors : • ∂¢ • ∂ T : a row where all elements are T • (l : ¢;¢) • (l : T;T’) : a row with l : T and everything else is T’
Kinding • Row kinds s ::= Type | Row (L) • Signature has composite kinds .s (product of ordinary kinds and row kinds) • Parametrized by • S0 : ordinary type constructors, e.g. ! • S1 : use row types, e.g. “
Kinding (contd) • G:K)2 S0 implies Gs:(K)).s 2 S • H:K) 2 S1 implies H:K.Row() ).Type 2 S • ∂,L: .(Type) Row(L)) 2 S • l,L: .(Type Row(l.L) ) Row(l)) 2 S
Equations Satisfied • (l1:T1;l2:T2;T3) = (l2:T2;l1:T1;T3) • ∂ T = (l:T;∂ T) • G ∂T1 ∂Tn = ∂(G T1 Tn) • G (l:T1;T1’) (l:Tn;Tn’) = (l:G T1 Tn;G T1’ Tn’)
Constraint Equivalence Laws • (l1:T1 ; T1’) = (l2:T2 ; T2’) ´ 9 X.(T1’=(l2:T2 ; X) Æ T2’ = (l1:T1 ; X)) • G T1 Tn= ∂T ´ 9 X1 Xn.(G X1 Xn = T Æ Æni=1(Ti= ∂Xi))
Row Unification • (l1:X1 ; X1’) = (l2:T2 ; T2’) = ! 9 X. (X1’=(l2:T2 ; X) Æ T2’ = (l1:X1 ; X)) Æ (l1:X1 ; X1’) = • G T1 Tn= ∂X = ! 9 X1 Xn.(G X1 Xn = X Æ Æni=1(Ti= ∂Xi)) Æ ∂X =
Example : Record Application • apply {l=not;succ} {l=true;0} ! {l=false;1} • apply : 8 XY. “(X! Y) ! “X ! “Y • Notice X and Y are row types • {l=not;succ} :“(l:bool!bool,∂(int!int)) • “(l:bool ! bool , ∂(int ! int)) = “(l:bool ! bool, ∂int ! ∂int) = “((l:bool, ∂int) ! (l:bool, ∂int))
Message Dispatch • Assume a record of functions v • With same return type • Also a variant (l w) of domain type • Operation # : # v (l w) ! v.hli w • # : 8 XY. “(X!∂Y)!o X ! Y
Conclusion • Rows : a powerful typing discipline • Allows typechecking records and variants • Allows typechecking refinements • Soft typing has been studied • Type Constraints : useful explanation of type inference