On the Power of Coercion Abstraction Julien Cretin Didier Rémy INRIA January 26, 2012 1 / 36
Why study coercions? People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features. 2 / 36
Why study coercions? People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features. These techniques have a lot in common, but also differ in some details. Can we understand them as several instances of the same framework and use it to more easily design new type system features? 2 / 36
Why study coercions? People have often used similar mechanisms, called coercions or type conversions, to explain non-trivial type system features. These techniques have a lot in common, but also differ in some details. Can we understand them as several instances of the same framework and use it to more easily design new type system features? In this work, we restrict to erasable coercions ( i.e. coercions without computational content). 2 / 36
Intuition: Goal Let’s design a type system to type the following untyped lambda term: ( λ x . x x ) ( λ x . x ) We can graphically represent it bottom-up like that: x x x @ x x λ λ @ 3 / 36
Intuition: Typing rules The type system necessarily gives typing rules for the untyped constructs: ◮ variable: x ◮ abstraction: λ x . M ◮ application: M N We choose simple types for illustration. 4 / 36
Intuition: Graphical typing rules We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ ⊢ M : τ → σ Γ ⊢ N : τ Γ ⊢ M N : σ M N τ → σ τ Γ Γ @ σ Γ 5 / 36
Intuition: Graphical typing rules We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ , ( x : τ ) ⊢ M : σ Γ ⊢ λ ( x : τ ) M : τ → σ M Γ , ( x : τ ) σ x : τ λ τ → σ Γ 5 / 36
Intuition: Graphical typing rules We can annotate the graphical untyped constructs to obtain their graphical typing rule: Γ 1 , ( x : τ ) , Γ 2 ⊢ x : τ x τ Γ 1 , ( x : τ ) , Γ 2 5 / 36
Intuition: Simply-typed lambda calculus x x @ x λ 6 / 36
Intuition: Simply-typed lambda calculus x x @ σ Γ , ( x : τ ) x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x @ Γ , ( x : τ ) σ σ Γ , ( x : τ ) x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x ρ → σ ρ Γ , ( x : τ ) Γ , ( x : τ ) @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x Γ , ( x : τ ) τ ρ → σ ρ Γ , ( x : τ ) Γ , ( x : τ ) @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x Γ , ( x : τ ) τ τ → σ Γ , ( x : τ ) Γ , ( x : τ ) τ @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x τ Γ , ( x : τ ) τ → σ Γ , ( x : τ ) @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x Γ , ( x : τ ) τ τ Γ , ( x : τ ) τ → σ Γ , ( x : τ ) @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Simply-typed lambda calculus x x Γ , ( x : τ ) τ ERROR τ Γ , ( x : τ ) τ → σ Γ , ( x : τ ) @ Γ , ( x : τ ) σ x : τ λ τ → σ Γ 6 / 36
Intuition: Type system features Terms should be allowed to have several types. 7 / 36
Intuition: Type system features Terms should be allowed to have several types. Several type system features can represent multiple types: ◮ intersection types, ◮ polymorphism , ◮ subtyping, or ◮ dependent types. We choose polymorphism for illustration. 7 / 36
Intuition: ∀ -elim Polymorphism elimination can be x seen as a coercion (which is an Γ ′ ∀ α. α → α erasable type conversion): · τ x Γ ′ ⊢ x : ∀ α. α → α τ τ → τ Γ ′ Γ ′ Γ ′ ⊢ x τ : τ → τ @ τ Γ ′ x : τ λ τ → τ Γ With τ � ∀ α. α → α and Γ ′ � Γ , ( x : τ ) . 8 / 36
Intuition: ∀ -intro Polymorphism introduction may extend the environment: so coercions may in fact change the whole typing, not just types! Type system features are typing conversions. Untyped term: x λ x . x x λ 9 / 36
Intuition: ∀ -intro Polymorphism introduction may extend the environment: so coercions may in fact change the whole typing, not just types! Type system features are typing conversions. Typing derivation: x Γ , α, ( x : α ) ⊢ x : α α Γ , α, ( x : α ) x : α Γ , α ⊢ λ ( x : α ) x : α → α λ Γ , α α → α Γ ⊢ Λ α λ ( x : α ) x : ∀ α. α → α α Λ Γ ∀ α. α → α We can now pass this term to ( λ x . x x ) as wanted. 9 / 36
Coercions A one-node coercion P , drawn in red , is a one-node erasable retyping context. Γ , ∆ ⊢ M : τ M ◮ retyping : where M Γ , ∆ τ Γ ⊢ P [ M ] : σ and P [ M ] are explicitly-typed P version of the same implicit term. σ Γ 10 / 36
Coercions A one-node coercion P , drawn in red , is a one-node erasable retyping context. Γ , ∆ ⊢ M : τ M ◮ retyping : where M Γ , ∆ τ Γ ⊢ P [ M ] : σ and P [ M ] are explicitly-typed P version of the same implicit term. σ Γ ◮ erasable : P doesn’t modify or block the reduction. It is purely static. 10 / 36
Coercions A coercion G is a sequence of one-node coercions. · α → β · α → β β β Λ Λ α α Λ Λ G We fill the hole with a diamond: G = Λ α Λ β ♦ ( α → β ) 11 / 36
Erasability The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. x Γ , ( x : τ ) τ τ x x α τ → τ Γ , α, ( x : α ) Γ , ( x : τ ) Γ , ( x : τ ) τ x : α @ λ τ Γ , α α → α Γ , ( x : τ ) x : τ α λ Λ τ Γ τ → τ Γ @ τ Γ 12 / 36
Erasability The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. x τ x x x : α @ λ x : τ α λ Λ @ 12 / 36
Erasability The erasing function ⌊·⌋ keeps the blue parts and removes both the annotations and the red nodes. x x x x @ λ x λ @ 12 / 36
Bisimulation The reduction is labelled: ◮ β -reduction involves only blue nodes ◮ ι -reduction involves at least one red node 13 / 36
Bisimulation The reduction is labelled: ◮ β -reduction involves only blue nodes ◮ ι -reduction involves at least one red node We want a bisimulation up to ι -steps: M N M N ι β ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊ M ⌋ ⌊ N ⌋ ⌊ M ⌋ Forward simulation The forward simulation tells that coercions do not contribute to computation. 13 / 36
Bisimulation The reduction is labelled: ◮ β -reduction involves only blue nodes ◮ ι -reduction involves at least one red node We want a bisimulation up to ι -steps: ⋆ M N M N M N ι β ι β ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊·⌋ ⌊ M ⌋ ⌊ N ⌋ ⌊ M ⌋ ⌊ M ⌋ ⌊ N ⌋ Forward simulation Backward simulation The forward simulation tells that coercions do not contribute to computation. The backward simulation tells that coercions cannot block the computation. (Thus, values remain values after erasure.) 13 / 36
Coercion judgments We give the following judgment for coercions: Γ ⊢ G : τ ⊲ σ Γ , ∆ τ G σ Γ 14 / 36
System F τ, σ ::= τ → σ | α | ∀ α.τ M , N ::= x | λ ( x : τ ) M | M N | Λ α M | M τ G ::= Λ α G | G τ Polymorphism: (Λ α M ) τ � ι M [ α ← τ ] M M Γ , α τ Γ ∀ α. σ · τ Λ α · σ [ α ← τ ] Γ Γ ∀ α. τ 15 / 36
System F η τ, σ ::= τ → σ | α | ∀ α.τ M , N ::= x | λ ( x : τ ) M | M N | Λ α M | M τ | G � M � G ::= Λ α G | G τ | G 1 � G 2 � Coercion application: (we want G � M � � ⋆ ι G [ ♦ ← M ] ) M τ τ Γ if G G �·� σ Γ σ Γ 15 / 36
System F η τ, σ ::= τ → σ | α | ∀ α.τ M , N ::= x | λ ( x : τ ) M | M N | Λ α M | M τ | G � M � G ::= Λ α G | G τ | G 1 � G 2 � | ♦ τ Reflexivity: ♦ τ � M � � ι M τ ♦ τ Γ 15 / 36
System F η τ, σ ::= τ → σ | α | ∀ α.τ M , N ::= x | λ ( x : τ ) M | M N | Λ α M | M τ | G � M � G ::= Λ α G | G τ | G 1 � G 2 � | ♦ τ | G 1 τ → G 2 Arrow congruence (subtyping): τ ′ → G 2 ) � λ ( x : τ 1 ) M � � ι λ ( x : τ ′ 1 1 ) G 2 � M [ x ← G 1 � x � ] � ( G 1 τ 1 → τ 2 τ 2 τ ′ 1 if and G 1 → G 2 G 2 G 1 τ ′ 1 → τ ′ τ ′ τ 1 Γ Γ Γ 2 2 15 / 36
System F η τ, σ ::= τ → σ | α | ∀ α.τ M , N ::= x | λ ( x : τ ) M | M N | Λ α M | M τ | G � M � G ::= Λ α G | G τ | G 1 � G 2 � | ♦ τ | G 1 τ → G 2 | Dist ∀ α. τ → σ It permutes Λ α and λ ( x : τ ) Dist ∀ α. τ ′ → σ ′ � Λ α λ ( x : τ ) M � � ι λ ( x : τ ) Λ α M ∀ α. τ → σ with α / ∈ ftv ( τ ) Dist ∀ α. τ → σ Γ τ → ∀ α. σ 15 / 36
Recommend
More recommend