second order types
play

Second Order Types Giuseppe Castagna: Foundation of OOP Tutorial - PDF document

Second Order Types Giuseppe Castagna: Foundation of OOP Tutorial Slides) 68 0 0 Loss of information Consider the function I = x T .x : T T By the rule for application I : T T M : U < T I ( M ) : T Therefore ( x a :


  1. Second Order Types Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 68 0 0

  2. Loss of information Consider the function I = λx T .x : T → T By the rule for application I : T → T M : U < T I ( M ) : T Therefore ( λx � � a : int � � .x ) � a = 1 � b = 2 � : � � a : int � � Second order I : ∀ X ≤ T.X → X Two ways: 1. Implicit polymorphism 2. Explicit polymorphism Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 69 62 0

  3. Implicit polymorphism No types in terms λx.x : ∀ α.α → α ( λx.x )3 : int x : α � x : α [ α = β ] � λx.x : α → β � 3 : int [ α = int ] � ( λx.x )3 : β Subtyping λx. (( λy.x )( x.� + 3)) : ∀ α ≤� � � : int � � .α → α Therefore λx. (( λy.x )( x.� +3))( � � = 1 � m = true � ) : � � � : int� m : bool � � Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 70 0 0

  4. Giuseppe Castagna: Foundation of OOP �Tutorial Slides) Inference with subtyping x : α � x : α α ≤ � � � : � � � x : α� y : γ � x : α x : α � 3 : int x : α � x.� : � α = β δ = int� � ≤ int x : α � λy.x : γ → β x : α � x.� + 3 : δ δ ≤ γ x : α � ( λy.x )( x.� + 3) : β � λx. (( λy.x )( x.� + 3)) : α → β Resulting type ∀ � ≤ int . ∀ α ≤� � � : � � � . α → α Simplified ∀ α ≤� � � : int � � . α → α 71 0 0

  5. Explicit polymorphism Λ X.λx X .x : ∀ X.X → X The programmer specifies the type (Λ X.λx X .x )( int )(3) ( λx int .x )(3) � Subtyping � .λx X .x Λ X ≤� � a : int � The application � .λx X .x )( � (Λ X ≤� � a : int � � a : int� b : int � � ) has type � � a : int� b : int � � → � � a : int� b : int � � thus � .λx X (Λ X ≤� � a : int � .x )( � � a : int� b : int � � )( � a = 1 � b = 3 � ) has type � a : int� b : int � � � Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 72 0 0

  6. F ≤ Types ::= X | Top | T → T | ∀ ( X ≤ T ) T T Terms x | ( λx T .a ) | a ( a ) ::= a | top | Λ X ≤ T.a | a ( T ) Reduction ( λx T .a )( b ) � a [ x T := b ] ( β ) (Λ X ≤ T.a )( T � ) � a [ X := T � ] ( β ∀ ) Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 73 0 0

  7. Subtyping (refl) C � T ≤ T C � T 1 ≤ T 2 C � T 2 ≤ T 3 (trans) C � T 1 ≤ T 3 (taut) C � X ≤ C ( X ) (Top) C � T ≤ Top C � T 1 ≤ S 1 C � S 2 ≤ T 2 ( → ) C � S 1 → S 2 ≤ T 1 → T 2 C � T 1 ≤ S 1 C� ( X ≤ T 1 ) � S 2 ≤ T 2 ( ∀ ) C � ∀ ( X ≤ S 1 ) S 2 ≤ ∀ ( X ≤ T 1 ) T 2 Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 74 0 0

  8. Type system [Vars] C ; Γ � x : Γ( x ) C ; Γ � ( x : T ) � a : T � [ → Intro] C ; Γ � ( λx T .a ): T → T � C ; Γ � a : S → T C ; Γ � b : S [ → Elim] C ; Γ � a ( b ): T [Top] C ; Γ � top : Top C� ( X ≤ T ) ; Γ � a : T � [ ∀ Intro] C ; Γ � Λ X ≤ T.a : ∀ ( X ≤ T ) T � C ; Γ � a : ∀ ( X ≤ S ) T [ ∀ Elim] C ; Γ � a ( S ): T [ X := S ] C � T � ≤ T C ; Γ � a : T � [Subsump] C ; Γ � a : T Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 75 0 0

  9. Transitivity elimination Id A | X T | Top T | c → c � | ∀ ( X ≤ c ) c � | c c � c : : = C � Id A : A ≤ A (refl) C � c � : T 2 ≤ T 3 C � c : T 1 ≤ T 2 (trans) C � c � c : T 1 ≤ T 3 C ∪ � X ≤ T } � X T : X ≤ T (taut) C � Top T : T ≤ Top (Top) C � c 1 : T � C � c 2 : T 2 ≤ T � 1 ≤ T 1 2 ( → ) C � c 1 → c 2 : T 1 → T 2 ≤ T � 1 → T � 2 C � c 1 : T � C ∪ � X ≤ T � 1 } � c 2 : T 2 ≤ T � 1 ≤ T 1 2 ( ∀ ) C � ∀ ( X ≤ c 1 ) c 2 : ∀ ( X ≤ T 1 ) T 2 ≤ ∀ ( X ≤ T � 1 ) T � 2 Theorem 5 There is a 1-1 correspondence be- tween well-typed coerce expressions and sub- typing derivations. Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 76 0 0

  10. Giuseppe Castagna: Foundation of OOP �Tutorial Slides) The rewriting system ( Asc ) ( c d ) e c ( d e ) � ( c → d ) ( c � → d � ) ( c � c ) → ( d d � ) ( → � ) � ( → �� ) ( c → d ) (( c � → d � ) e ) (( c � c ) → ( d d � )) e � ∀ ( X ≤ c � c )( d d � [ X T : = c X S ]) ( ∀ � ) ( ∀ ( X ≤ c ) d ) ( ∀ ( X ≤ c � ) d � ) � ( ∀ ( X ≤ c � c )( d d � [ X T := c X S ])) e ( ∀ �� ) ( ∀ ( X ≤ c ) d ) (( ∀ ( X ≤ c � ) d � ) e ) � Normal forms are subterms of ( c → d ) e 1 . . . e n or of ( ∀ ( X ≤ c ) d ) e 1 . . . e n where c� c i � d� d i are in normal form and e 1 � . . . � e n are either X t or Top T . They normal forms correspond to derivations in which every left premise of a (trans) rule is a leaf. Thus, the rewriting system pushes the transitivity up to the leaves. 76 0 0

  11. Example ( c → d ) (( c � → d � ) e ) � (( c � c ) → ( d d � )) e ∗ Theorem 6 (Soundness) If c � d and C � c : Δ then C � d : Δ Theorem 7 (Weak normalization) Every in- nermost strategy for � terminates. Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 76 0 0

  12. Coherence Let c : S ≤ T (id l ) Id T c c � (id r ) c Id S c � (top) Top T c � Top S (varTop) X Top � Top X Consider the composition of the rewriting sys- tems: Theorem 8 (normal forms) Every well-typed coerce expression in normal form has the form c 0 c 1 ... c n with n ≥ 0 , where c 0 can be any co- erce expression different from a composition �of other coerce expressions) whose subformu- lae are in normal form, and c 1 . . . c n are vari- ables. Theorem 9 For every provable subtyping judg- ment, there exists only one coerce expression in normal form proving it. Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 76 0 0

  13. Coherence Theorem 10 (coherence) Let Π 1 and Π 2 be two proofs of the same judgment C � Δ . If c 1 and c 2 are the corresponding coerce ex- pressions then c 1 and c 2 are equal modulo the rewriting system. Shape of NFs and the subtyping algorithm The normal forms of Theorem 8 correspond to derivations in which every application of a (trans) rule has as left premise an application of the rule (taut). Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 76 0 0

  14. Subtyping algorithm (AlgRefl) C � X ≤ X C � C ( X ) ≤ T (AlgTrans) C � X ≤ T (Top) C � T ≤ Top C � T 1 ≤ S 1 C � S 2 ≤ T 2 ( → ) C � S 1 → S 2 ≤ T 1 → T 2 C� ( X ≤ T 1 ) � S 2 ≤ T 2 C � T 1 ≤ S 1 ( ∀ ) C � ∀ ( X ≤ S 1 ) S 2 ≤ ∀ ( X ≤ T 1 ) T 2 Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 76 0 0

  15. Typing algorithm [Vars] C ; Γ � x : Γ( x ) C ; Γ � ( x : T ) � a : T � [ → I] C ; Γ � ( λx T .a ): T → T � [ → E] C ; Γ � a : U C ; Γ � b : S � C � S � ≤ S B C ( U ) = S → T C ; Γ � a ( b ): T [Top] C ; Γ � top : Top C� ( X ≤ T ) ; Γ � a : T � [ ∀ I] C ; Γ � Λ X ≤ T.a : ∀ ( X ≤ T ) T � C � S � ≤ S C ; Γ � a : U [ ∀ E] B C ( U ) = ∀ ( X ≤ S ) T C ; Γ � a ( S � ): T [ X := S � ] Definition 2 � B C ( C ( X )) if T ≡ X B C ( T ) = otherwise T Giuseppe Castagna: Foundation of OOP �Tutorial Slides) 77 0 0

  16. Typing and subtyping algorithms are sound and complete Giuseppe Castagna: Foundation of OOP �Tutorial Slides) Sound and complete does not mean decidable�� let ¬ T and ∀ ( X ) T denote T → Top and ∀ ( X ≤ Top ) T : X 0 ≤ ∀ ( Y ) ¬ ( ∀ ( Z ≤ Y ) ¬ Y ) ∀ ( X 1 ≤ X 0 ) ¬ X 0 � X 0 ≤ by applying AlgTrans: X 0 ≤ ∀ ( Y ) ¬ ( ∀ ( Z ≤ Y ) ¬ Y ) � ∀ ( X 1 ) ¬ ( ∀ ( X 2 ≤ X 1 ) ¬ X 1 ) ∀ ( X 1 ≤ X 0 ) ¬ X 0 ≤ by applying ( ∀ ): X 0 ≤ ∀ ( Y ) ¬ ( ∀ ( Z ≤ Y ) ¬ Y ) � X 1 ≤ X 0 � ¬ ( ∀ ( X 2 ≤ X 1 ) ¬ X 1 ) ≤ ¬ X 0 by the contravariance of ( → ): X 0 ≤ ∀ ( Y ) ¬ ( ∀ ( Z ≤ Y ) ¬ Y ) � X 1 ≤ X 0 � X 0 ≤ ∀ ( X 2 ≤ X 1 ) ¬ X 1 the same judgement as the one we started from. Just semi-decidability holds Kernel-Fun: compare quantifications with equal bounds. 78 0 0

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