Last time on Types ... picture from http://learnyouahaskell.com
Last time on Types ... � Simply-typed λ -calculus (recap) Γ � e : τ � Parametric polymorphism let f = λ x ( x ) in ( f true ) :: ( f nil ) � The beginnings of the Mini-ML type system... let -polymorphism
Mini-ML types and type schemes Types ::= type variable τ α | type of booleans bool | τ � τ function type | list type τ list where α ranges over a fixed, countably infinite set TyVar . Type Schemes ::= � A ( τ ) σ where A ranges over finite subsets of the set TyVar . When A = { α 1 , . . . , α n } , we write � A ( τ ) as � α 1 , . . . , α n ( τ ) .
The ‘generalises’ relation between type schemes and types We say a type scheme σ = � α 1 , . . . , α n ( τ � ) generalises a type τ , and write σ � τ if τ can be obtained from the type τ � by simultaneously substituting some types τ i for the type variables α i ( i = 1 , . . . , n ): τ = τ � [ τ 1 / α 1 , . . . , τ n / α n ] . (N.B. The relation is una ff ected by the particular choice of names of bound type variables in σ .) The converse relation is called specialisation: a type τ is a specialisation of a type scheme σ if σ � τ .
Generalisations: some examples and non-examples � � α . ( α � α ) � bool � bool with [ bool / α ] � � α . ( α � α ) � � ( int � bool ) � � α . ( α � α ) � [ β ] � [ β ] with [[ β ] / α ] � � α , β . ( α � β ) � ( int � bool ) with [ int / α , bool / β ] � � α . ( α � β ) � � ( int � bool ) � � α . ( α � β ) � ( int � β ) with [ int / α ]
Mini-ML typing judgement takes the form Γ � M : τ where � the typing environment Γ is a finite function from variables to type schemes . (We write Γ = { x 1 : σ 1 , . . . , x n : σ n } to indicate that Γ has domain of definition dom ( Γ ) = { x 1 , . . . , x n } and maps each x i to the type scheme σ i for i = 1 .. n .) � M is a Mini-ML expression � τ is a Mini-ML type.
Mini-ML expressions, M ::= variable x | true boolean values | false | if M then M else M conditional | λ x ( M ) function abstraction | function application M M | let x = M in M local declaration | nil list nil | M :: M list cons | case M of nil = > M | x :: x = > M case expression
Mini-ML type system, I Γ � x : τ if ( x : σ ) � Γ and σ � τ ( var � ) Γ � B : bool if B � { true , false } ( bool ) Γ � M 1 : bool Γ � M 2 : τ Γ � M 3 : τ ( if ) Γ � if M 1 then M 2 else M 3 : τ
Mini-ML type system, II Γ � nil : τ list ( nil ) Γ � M 1 : τ Γ � M 2 : τ list ( cons ) Γ � M 1 :: M 2 : τ list Γ � M 1 : τ 1 list Γ � M 2 : τ 2 Γ , x 1 : τ 1 , x 2 : τ 1 list � M 3 : τ 2 if x 1 , x 2 / � dom ( Γ ) � x 1 � = x 2 Γ � case M 1 of nil = > M 2 | x 1 :: x 2 = > M 3 : τ 2 ( case )
Mini-ML type system, III Γ , x : τ 1 � M : τ 2 if x / � dom ( Γ ) ( fn ) Γ � λ x ( M ) : τ 1 � τ 2 Γ � M 1 : τ 1 � τ 2 Γ � M 2 : τ 1 ( app ) Γ � M 1 M 2 : τ 2 Γ , x : � A ( τ ) � M 2 : τ � Γ � M 1 : τ if x / � dom ( Γ ) Γ � let x = M 1 in M 2 : τ � � A = ftv ( τ ) � ftv ( Γ ) ( let )
Assigning type schemes to Mini-ML expressions Given a type scheme σ = � A ( τ ), write Γ � M : σ if A = ftv ( τ ) � ftv ( Γ ) and Γ � M : τ is derivable from the axiom and rules on Slides 65–67. When Γ = { } we just write � M : σ for { } � M : σ and say that the (necessarily closed—see Exercise 2) expression M is typeable in Mini-ML with type scheme σ .
Mini-ML - Type checking, typeability, and type inference � Type-checking problem: given closed M , and σ , is {} � M : σ derivable in the type system? � Typeability problem: given closed M , is there any σ for which {} � M : σ is derivable in the type system?
Two examples involving self-application def M = let f = λ x 1 ( λ x 2 ( x 1 )) in f f M � def = ( λ f ( f f )) λ x 1 ( λ x 2 ( x 1 )) Are M and M � typeable in the Mini-ML type system?
Example using let polymorphism (z is used polymorphically)
Recommend
More recommend