Download presentation
Presentation is loading. Please wait.
Published byCassandra Fisher Modified over 8 years ago
1
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http://www.csg.csail.mit.edu/6.827 Types and Simple Type Inference September 21, 2006
2
L05-2 September 21, 2006http://www.csg.csail.mit.edu/6.827 Outline General issues Type instances Type Unification Type Inference rules for a simple non- polymorphic type system Type Inference rules for a polymorphic type system Overloading next time...
3
L05-3 September 21, 2006http://www.csg.csail.mit.edu/6.827 What are Types? A method of classifying objects (values) in a language x :: says object x has type or object x belongs to a type denotes a set of values. This notion of types is different from types in languages like C, where a type is a storage class specifier.
4
L05-4 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Correctness If x :: then only those operations that are appropriate to set may be performed on x. A program is type correct if it never performs a wrong operation on an object. - Add an Int and a Bool - Head of an Int - Square root of a list
5
L05-5 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Safety A language is type safe if only type correct programs can be written in that language. Most languages are not type safe, i.e., have “holes” in their type systems. Fortran: Equivalence, Parameter passing Pascal: Variant records, files C, C++: Pointers, type casting However, Java, CLU, Ada, ML, Id, Haskell, pH etc. are type safe.
6
L05-6 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Declaration vs Reconstruction Languages where the user must declare the types –CLU, Pascal, Ada, C, C++, Fortran, Java Languages where type declarations are not needed and the types are reconstructed at run time –Scheme, Lisp Languages where type declarations are generally not needed but allowed, and types are reconstructed at compile time –ML, Id, Haskell, pH A language is said to be statically typed if type-checking is done at compile time
7
L05-7 September 21, 2006http://www.csg.csail.mit.edu/6.827 Polymorphism In a monomorphic language like Pascal, one defines a different length function for each type of list In a polymorphic language like ML, one defines a polymorphic type (list t), where t is a type variable, and a single function for computing the length pH and most modern functional languages have polymorphic objects and follow the Hindley-Milner type system.
8
L05-8 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Instances The type of a variable can be instantiated differently within its lexical scope. let id = \x.x in ((id 1 5), (id 2 True)) id 1 :: ? id 2 :: ? Both id 1 and id 2 can be regarded as instances of type ? Int --> Int Bool --> Bool t --> t
9
L05-9 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Instances: another example twice 1 ::? twice 2 :: ? ((I -> I) -> I -> I) -> (I -> I) -> (I -> I) let twice :: (t -> t) -> t -> t twice f x = f (f x) in twice 1 twice 2 (plus 3) 4 (I -> I) -> I -> I
10
L05-10 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Instantiation: -bound vs Let-bound Variables Only let-bound identifiers can be instantiated differently. let twice f x = f (f x) in twice twice succ 4 vs. let twice f x = f (f x) foo g = (g g succ) 4 in foo twice foo is not type correct ! Generic vs. Non-generic type variables
11
L05-11 September 21, 2006http://www.csg.csail.mit.edu/6.827 A mini Language (-calculus + let) 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-Milner type inference algorithm. but first a Simple Type System... Expressions E ::= c constant | x variable | x. E abstraction | (E 1 E 2 ) application | let x = E 1 in E 2 let-block
12
L05-12 September 21, 2006http://www.csg.csail.mit.edu/6.827 A Simple Type System Types ::= base types | t type variables | --> 2 Function types Type Environments TE ::= Identifiers --> Types
13
L05-13 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Inference Issues What does it mean for two types a and b to be equal? –Structural Equality Suppose a = 1 --> 2 b = 3 --> 4 Is a = b ? iff 1 3 and 2 4 Can two types be made equal by choosing appropriate substitutions for their type variables? –Robinson’s unification algorithm Suppose a t 1 --> Bool b Int --> t 2 Are a and b unifiable ? if t 1 = Int and t 2 = Bool Suppose a t 1 --> Bool b Int --> Int Are a and b unifiable ? No
14
L05-14 September 21, 2006http://www.csg.csail.mit.edu/6.827 Simple Type Substitutions needed to define type unification A substitution is a map S : Type Variables --> Types S = [ / t 1,..., n / t n ] ’ = S ’ is a Substitution Instance of Example: S = [(t --> Bool) / t 1 ] S( t 1 --> t 1 ) = ? Types ::= base types (Int, Bool..) | t type variables | --> Function types Substitutions can be composed, i.e., S 2 S 1 Example: S 1 = [(t --> Bool) / t 1 ] ; S 2 = [Int / t] S 2 S 1 ( t 1 --> t 1 ) = ? ( t --> Bool) --> ( t --> Bool) ( Int --> Bool) --> ( Int --> Bool)
15
L05-15 September 21, 2006http://www.csg.csail.mit.edu/6.827 Unification An essential subroutine for type inference def Unify( 1, 2 ) = case ( 1, 2 ) of ( 1, t 2 )= [ 1 / t 2 ] provided t 2 FV( 1 ) (t 1, 2 )= [ 2 / t 1 ] provided t 1 FV( 2 ) ( 1, 2 )= if (eq? 1 2 ) then [ ] else fail ( 11 --> 12, 21 --> 22 ) = Unify( 1, 2 ) tries to unify 1 and 2 and returns a substitution if successful Does the order matter? letS 1 =Unify( 11, 21 ) S 2 =Unify(S 1 ( 12 ), S 1 ( 22 )) in S 2 S 1 otherwise= fail
16
L05-16 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Inference Rules Typing: TE |--e : Suppose we want to assert (prove) that given some type environment TE, the expression (e 1 e 2 ) has the type ’. Then it must be the case that the same TE implies that e 1 has type --> ’ and e 2 has the type . Such an inference rule can be written as: (App) TE ├ e 1 : TE ├ e 2 : TE ├ (e 1 e 2 ) : ’ --> ’
17
L05-17 September 21, 2006http://www.csg.csail.mit.edu/6.827 Simple Type Inference Rules Typing: TE ├ e : (App) TE ├ e 1 : --> ’ TE ├ e 2 : TE ├ (e 1 e 2 ) : ’ (Abs) TE ├ x.e :--> ’ (Var) TE ├ x : (Const) TE ├ c : (Let) TE ├ (let x = e 1 in e 2 ) : ’ TE + {x : } ├ e : ’ (x : )TE typeof(c) = TE+{x:} ├ e 1 : TE+{x:} ├ e 2 : ’
18
L05-18 September 21, 2006http://www.csg.csail.mit.edu/6.827 Inference Algorithm W(TE, e) returns (S,) such that S (TE) |-- e : The type environment TE records the most general type of each identifier while the substitution S records the changes in the type variables Def W(TE, e) = Case e of x =... x.e =... (e 1 e 2 ) =... let x = e 1 in e 2 =...
19
L05-19 September 21, 2006http://www.csg.csail.mit.edu/6.827 Inference Algorithm (cont.) Def W(TE, e)= Case e of x = if (x Dom(TE)) then Fail else let = TE(x); in ({}, ) x.e = let (S 1, 1 ) = W(TE + { x : u }, e); in (S 1, S 1 (u) --> 1 ) (e 1 e 2 ) = let x = e 1 in e 2 = u’s represent new type variables let(S 1, 1 ) = W(TE, e 1 ) (S 2, 2 ) = W(S 1 (TE), e 2 ) S 3 = Unify(S 2 ( 1 ), 2 --> u); in (S 3 S 2 S 1, S 3 (u)) let (S 1, 1 ) = W(TE + {x : u}, e 1 ); S 2 = Unify(S 1 (u), 1 ); (S 3, 2 ) = W(S 2 S 1 (TE) + {x : }, e 2 ); in (S 3 S 2 S 1, 2 )
20
L05-20 September 21, 2006http://www.csg.csail.mit.edu/6.827 Type Inference Let fact = n.if (n == 0) then 1 else n * fact (n-1) In fact
21
L05-21 September 21, 2006http://www.csg.csail.mit.edu/6.827 Inferring Polymorphic Types let id = x. x in... (id True)... (id 1)...
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.