{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L07HindleyMilner2Print

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

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 Document

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

View Full Document
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

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online