last time on types
play

Last time on Types ... picture from http://learnyouahaskell.com Last - PowerPoint PPT Presentation

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


  1. Last time on Types ... picture from http://learnyouahaskell.com

  2. 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

  3. 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 ( τ ) .

  4. 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 σ � τ .

  5. Generalisations: some examples and non-examples � � α . ( α � α ) � bool � bool with [ bool / α ] � � α . ( α � α ) � � ( int � bool ) � � α . ( α � α ) � [ β ] � [ β ] with [[ β ] / α ] � � α , β . ( α � β ) � ( int � bool ) with [ int / α , bool / β ] � � α . ( α � β ) � � ( int � bool ) � � α . ( α � β ) � ( int � β ) with [ int / α ]

  6. 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.

  7. 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

  8. 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 : τ

  9. 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 )

  10. 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 )

  11. 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 σ .

  12. 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?

  13. 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?

  14. Example using let polymorphism (z is used polymorphically)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend