1 / 20

The Essence of ML Type Inference

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 £ : * ­ * ) *

bernie
Télécharger la présentation

The Essence of ML Type Inference

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Essence of ML Type Inference Francois Pottier and Didier Remy Type Refinements Seminar

  2. Outline • Extend system with constructs for practical programming • Sums, Products • Datatypes • Row Types

  3. 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

  4. 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)

  5. 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)

  6. Algebraic Data Types • Abstract Type isomorphic to a (k-ary) product or sum type by definition • DX 'ki=1 li : Ti • DX 'ki=1 li : Ti • Here, D ranges over datatypes • li2 L, a set of labels

  7. Datatypes (sums) • For each DX 'ki=1 li: Ti declared • k constructors of arity 1 named li • A destructor of arity k+1 caseD • li : 8X. Ti! DX • caseD : 8XZ.DX! (T1! Z)  Z • caseD (li v) v1 vk! vi v

  8. Datatypes (products) • For each DX 'ki=1 li: Ti declared • A constructor of arity k makeD • k destructors of arity 1 named li • li : 8X. DX ! Ti • makeD : 8X.T1! Tk! D • (makeD v1 vk).li! vi

  9. 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

  10. Rows • Mappings from labels to types • Can be defined incrementally • Can be abstracted by type variable • Define polymorphic records and variants

  11. 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

  12. 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’

  13. 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. “

  14. 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

  15. 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’)

  16. 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))

  17. 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 = 

  18. 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))

  19. 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

  20. 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

More Related