last time
play

Last time Introduced type inference can write type-safe programs - PDF document

Last time Introduced type inference can write type-safe programs without writing CS 611 down types explicitly Advanced Programming Languages useful for higher-order functions because types are large, obscure code Andrew Myers


  1. Last time • Introduced type inference –can write type-safe programs without writing CS 611 down types explicitly Advanced Programming Languages –useful for higher-order functions because types are large, obscure code Andrew Myers • Showed type inference algorithm for Cornell University polymorphic code –limited form of parametric polymorphism Lecture 33 –polymorphism is orthogonal to type inference Parametric Polymorphism (& more important!) 13 Nov 00 Cornell University CS 611 Fall'00 -- Andrew Myers 2 Parametric polymorphism Ad-hoc polymorphism • Polymorphism: expression has multiple types • Same name can be used to denote values • Parametric polymorphism ( ∀ X 1 ,..., X n . τ ) with different types – types appear as parameters • e.g. “+” refers to one operator on int, – expression is written the same way for all types another on float, yet another on String – polymorphic value is instantiated on some types • Java, C: no parametric polymorphism • Examples: C++, Java overloading – Can’t write generic code that doesn’t care what are the • Can be modeled by allowing overloading types of values it manipulates void sort(T[ ] arr) —must say what T is! in the type context Γ Map m; —can’t define key, value type of m • Not true polymorphism: no polymorphic v = m.get(k); — no useful type checking • C++, Modula-3: generic code through templates values – (nearly) textual substitution Cornell University CS 611 Fall'00 -- Andrew Myers 3 Cornell University CS 611 Fall'00 -- Andrew Myers 4 Subtype polymorphism First-class polymorphic values • One type S is a subtype of another type T • ML: polymorphic values only bound by “let”… if all the values of S are also values of T instantiated immediately on use • S ≤ T = “S is a subtype of T” • Polymorphic values as first-class values: τ ::= B | X | τ 1 →τ 2 Turbak & Gifford S ≤ T � �� S � � �� T � “plambda”“proj” σ ::= ∀ X . σ | τ | σ 1 →σ 2 • A value is polymorphic because it is a e ::= λ x: σ . e | e 1 e 2 | Λ X . e | e [ τ ] member of all types that are supertypes of its type • Idea: polymorphic value Λ X . e can be explicitly instantiated on type τ using e [ τ ] class C extends D { } C ≤ D � • Can have ∀ X . σ inside function type: ( ∀ X . X ) → bool • Object-oriented languages support • Predicative polymorphism: σ , τ separated, can subtype polymorphism – we will discuss only instantiate on τ later Cornell University CS 611 Fall'00 -- Andrew Myers 5 Cornell University CS 611 Fall'00 -- Andrew Myers 6 1

  2. Operational Semantics Examples τ ::= B | X | τ 1 →τ 2 τ ::= B | X | τ 1 →τ 2 σ ::= ∀ X . σ | τ | σ 1 →σ 2 σ ::= ∀ X . σ | τ | σ 1 →σ 2 e ::= λ x : σ . e | e 1 e 2 | Λ X . e | e [ τ ] e ::= λ x: σ . e | e 1 e 2 | Λ X . e | e [ τ ] value type abstraction abstraction IF � Λ V . λ f : ∀ X . X → X → X. λ x : V. λ y : V. ( f [ V ] x y ) ( λ x: σ . e 1 ) e 2 → e 1 { e 2 / x } TRUE � Λ V. λ x,y : V.x : ∀ X . X → X → X ( Λ X . e ) [ τ ] → e { τ/ X } FALSE � Λ V. λ x,y : V.y : ∀ X . X → X → X • Type abstraction and application are purely if e 0 e 1 e 2 : τ IF [ τ ] e 0 e 1 e 2 : τ compile-time phenomena � Cornell University CS 611 Fall'00 -- Andrew Myers 7 Cornell University CS 611 Fall'00 -- Andrew Myers 8 Static Semantics Modeling predicative polymorphism • Need universe � � of all ordinary type τ ::= B | X | τ 1 →τ 2 � � interpretations: σ ::= ∀ X . σ | τ | σ 1 →σ 2 e ::= λ x : σ . e | e 1 e 2 | Λ X . e | e [ τ ] � 0 = { Z , U } � � � Γ ::= Ø | Γ , x : σ � n = { X → Y | X , Y ∈ � � n -1 } ∪ � ∆ ; Γ, x : σ � x : σ � � n -1 � � � � � � ∆ ::= Ø | ∆ , X � = � n ∈ω � Judgements : � n ∆ ; Γ, x : σ � e : σ � � � –Each element of � � is meaning of some type ∆ ; Γ � e : σ � � ∆ ; Γ � (λ x : σ . e ) : σ→σ � �� int � χ = Ζ, �� unit � χ = U dependent ∆ ; Γ � e 1 : σ→σ � ∆ ; Γ � e 2 : σ ∆ � σ �� τ 1 →τ 2 � χ = �� τ 1 � χ→ �� τ 2 � χ product: set of ∆ ; Γ � e 1 e 2 : σ � ∀ X . σ≅∀ Y . σ { Y / X } �� σ 1 →σ 2 � χ = �� σ 1 � χ→ �� σ 2 � χ all functions �� X � χ = χ ( X ) mapping D ∈ � � � � ∆ ; Γ � e : ∀ X . σ ∆, X ; Γ � e : σ to �� σ � χ[ X � D ] �� ∀ X . σ � χ = Π D ∈ �� �� �� σ � χ[ X � D ] ∆ ; Γ � Λ X . e : ∀ X . σ ∆ ; Γ � e [ τ ] : σ { τ / X } �� �� Cornell University CS 611 Fall'00 -- Andrew Myers 9 Cornell University CS 611 Fall'00 -- Andrew Myers 10 Interpreting terms System F • Given type variable environment χ , ordinary • a.k.a “The polymorphic lambda calculus” variable environment ρ , such that χ � ∆ and ρ � Γ, • Merges types schemes and types: �� ∆;Γ � e : σ � χρ ∈ �� σ � χ τ ::= B | X | τ 1 →τ 2 �� ∆;Γ � x : σ � χρ = ρ ( x ) σ,τ ::= B | X | τ 1 →τ 2 | ∀ X . τ σ ::= ∀ X . σ | τ | σ 1 →σ 2 �� ∆;Γ � e 1 e 2 : σ �� χρ = ( �� ∆;Γ � e 1 : σ→σ �� χρ) ( �� ∆;Γ � e 2 : σ � χρ) • Type schemes are first-class: can instantiate �� ∆;Γ � λ x : σ . e : σ→σ �� χρ = λ v ∈ �� σ � χ. �� ∆;Γ, x : σ � e : σ �� χρ[ x � v ]) • Impredicative polymorphism �� ∆;Γ � Λ X . e : ∀ T . σ � χρ = λ D ∈ � . �� ∆, X ;Γ � e : σ � χ[ T � D ] ρ • Type inference: undecidable �� ∆;Γ � e [ τ ] : σ { τ/ T } � χρ = ( �� ∆;Γ � e : ∀ T . σ � χρ)( �� τ � χ) • No model for types based on sets Substitution lemma: �� σ{τ / T } � χ = �� σ � χ [ T � τ ] Cornell University CS 611 Fall'00 -- Andrew Myers 11 Cornell University CS 611 Fall'00 -- Andrew Myers 12 2

  3. Self-application No set model • In System F, can write a type for term ( λ x • Predicative model: (x x)) without recursion: �� ∀ X . σ � χ = Π D ∈ �� �� �� σ � χ[ X � D ] �� �� • SELF-APP ≡ ( λ x : ∀ T.T → T. ( x [ ∀ T.T → T] x )) • ∀ X . σ is the set of all functions from type : ( ∀ T.T → T) → ( ∀ T.T → T) interpretations D (in � ) to corresponding • All standard λ -calculus encodings can be sets �� σ � χ[ X � D ] given types too (Church numerals, etc.) • Need to extend � to include σ ’s • ∀ X.X is function mapping all D ∈ � � to D � � • Extension of �� ∀ X.X � is { � D,D � | D ∈ � � }. � � But �� ∀ X.X � ∈ � � … can’t be a set! � � Cornell University CS 611 Fall'00 -- Andrew Myers 13 Cornell University CS 611 Fall'00 -- Andrew Myers 14 More polymorphism Dependent types • Ordinary function application ( e 1 e 2 ): • Types polymorphic with respect to a value term × term → term • Example: CLU, some varieties of Pascal • Type abstraction application ( e [ τ ]): procedure quicksort(a: array[1..n] of integer, n: int) term × type → term –Allows more compile-time reasoning • More options? –A generalization of parametric polymorphism: type × term → type : dependent typing type × type → type : polymorphic types ∀ n : int , α: type . array [ n , α ] → unit … and more… • Have to be careful about defining type equivalence if “ n ” can change... Cornell University CS 611 Fall'00 -- Andrew Myers 15 Cornell University CS 611 Fall'00 -- Andrew Myers 16 Polymorphic Types • Most languages include at least one built- in polymorphic type: array : type → type Useful to be able to declare own types: ML: datatype 'a tree = leaf of 'a | branch of 'a*'a tree*'a tree PolyJ: class Tree[T] { T e; T element(); } class Branch[T] extends Leaf[T] { Tree[T] left, right; } Cornell University CS 611 Fall'00 -- Andrew Myers 17 3

Recommend


More recommend