This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 http://www.csg.lcs.mit.edu/6.827 L7 1 Arvind Laboratory for Computer Science M.I.T. The HindleyMilner Type System ( Continued) September 30, 2002 September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L72 Arvind Outline Hindley Milner Type inference rules Type inference algorithm Overloading Type classes 2 September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L73 Arvind A mini Language to study Hindley Milner Types There are no types in the syntax of the language! The type of each subexpression is derived by the Hindley M ilner type inference algorithm . Expressions E ::= c constant  x variable  x. E abstraction  (E 1 E 2 ) application  let x = E 1 in E 2 let block September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L74 Arvind A Formal Type System Note, all the 2200 s occur in the beginning of a type schem e, i.e., a type cannot contain a type scheme Types : : = base types  t type variables  ?  > 2 Function types Type Schemes : : =  2200 t. ? Type Environments T E ::= Identifiers  > Type Schemes 3 September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L75 Arvind Unification An essential subroutine for type inference def Unify( 1 , 2 ) = case ( 1 , 2 ) of ( 1 , t 2 ) = [ 1 / t 2 ] ( t 1 , 2 ) = [ 2 / t 1 ] ( 1 , 2 ) = if ( eq ? 1 2 ) then [ ] else fail ( 1 1  > 1 2 , 2 1   > 2 2 ) = let S 1 = U nify( 1 1 , 2 1 ) S 2 =U nify(S 1 ( 1 2 ), S 1 ( 2 2 ) ) in S 2 S 1 otherwise = fail U nify( 1 , 2 ) tries to unify 1 and 2 and returns a substitution if successful Order in which sub expressions are unified does not m atter. September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L76 Arvind Instantiations Type scheme can be instantiated into a type by substituting types for the bound variables of , i.e ., = S for som e S s.t. D om ( S ) BV( ) is said to be an instance of ( > ) is said to be a generic instance of when S maps variables to new variables. = 2200 t 1 ...t n . Example: = 2200 t 1 . t 1   > t 2 t 3   > t 2 is a generic instance of In t   > t 2 is a non generic instance of 4 September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L77 Arvind Generalization aka Closing Generalization introduces polym orphism Q uantify type variables that are free in but not free in the type environm ent (TE) Captures the notion of new type variables of Gen(TE, ) = 2200 t 1 ...t n . w h e re { t 1 ...t n } = F V ( )  FV(TE) September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L78 Arvind Type Inference Type inference is typically presented in two different form s: Type inference rules: Rules define the type of each...
View
Full
Document
This note was uploaded on 02/13/2012 for the course CS 91.531 taught by Professor Giam during the Fall '09 term at UMass Lowell.
 Fall '09
 Giam
 Computer Science

Click to edit the document details