L07HindleyMilner2Print

L07HindleyMilner2Print - 1 http://www.csg.lcs.mit.edu/6.827...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 http://www.csg.lcs.mit.edu/6.827 L7- 1 Arvind Laboratory for Computer Science M.I.T. The Hindley-Milner Type System ( Continued) September 30, 2002 September 30, 2002 http://www.csg.lcs.mit.edu/6.827 L7-2 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 L7-3 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 L7-4 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 L7-5 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 L7-6 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 L7-7 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 L7-8 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.

Page1 / 14

L07HindleyMilner2Print - 1 http://www.csg.lcs.mit.edu/6.827...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online