relational parametricity
play

Relational Parametricity for Higher Kinds Robert Atkey University - PowerPoint PPT Presentation

Relational Parametricity for Higher Kinds Robert Atkey University of Strathclyde Glasgow, UK 5th September 2012 robert.atkey@strath.ac.uk Higher Kinds Higher Kinded Polymorphism System F : antification over types : Present in Haskell,


  1. Relational Parametricity for Higher Kinds Robert Atkey University of Strathclyde Glasgow, UK 5th September 2012 robert.atkey@strath.ac.uk

  2. Higher Kinds

  3. Higher Kinded Polymorphism System F : antification over types : Present in Haskell, Scala, and ML (via the module system) ∀ α. List α → List α System F ω : antification over type operators : ∀ ∗→∗ f . ∀ ∗ α. f α → f α and type-level λ -abstraction: List = λα : ∗ → ∗ . ∀ ∗ β. β → ( α → β → β ) → β Monad = λ m : ∗ → ∗ . ( ∀ ∗ α. α → m α ) × ( ∀ ∗ αβ. m α → ( α → m β ) → m β )

  4. Chur Encodings Booleans Naturals Lists and initial algebras, (co)products, final coalgebras, existentials. no reasoning principle Bool = ∀ α. α → α → α Nat = ∀ α. α → ( α → α ) → α List α = ∀ β. β → ( α → β → β ) → β { no uniqueness but only weakly initial or final :

  5. Chur Encodings with Higher Kinds Lists Vectors Equality no reasoning principle List = λα : ∗ . ∀ ∗ β. β → ( α → β → β ) → β Vec = λα n : ∗ . ∀ ∗→∗ β. β Z → ( ∀ ∗ n . α → β n → β ( S n )) → β n Eq κ = λαβ : κ. ∀ κ → κ →∗ f . ( ∀ κ γ. f γγ ) → f αβ { no uniqueness but only weakly initial (or final) :

  6. Relational Parametricity (Reynolds, 1983)

  7. Relational Parametricity For example, then Preservation of Relations e : ∀ α. α → ( α → α ) → α let X and Y be sets, and let R ⊆ X × Y if we have z 1 ∈ X , z 2 ∈ Y such that: ( z 1 , z 2 ) ∈ R and s 1 : X → X , s 2 : Y → Y such that: ∀ ( a , b ) ∈ R . ( s 1 a , s 2 b ) ∈ R ( e [ X ] z 1 s 1 , e [ Y ] z 2 s 2 ) ∈ R implies initiality, and ( ∀ α. α → ( α → α ) → α ) ∼ = N

  8. Relational Parametricity Relational interpretations of types Relational Parametricity Identity Extension : and Abstraction : R � Θ ⊢ A � θ θ ′ ρ ⊆ T � Θ ⊢ A � θ × T � Θ ⊢ A � θ ′ R � α � ρ = ρ ( α ) R � A → B � ρ = { ( f 1 , f 2 ) | ∀ ( a 1 , a 2 ) ∈ R � A � ρ. ( f 1 a 1 , f 2 a 2 ) ∈ R � B � ρ } R � ∀ α. A � ρ = { ( x 1 , x 2 ) | ∀ X , Y , R ⊆ X × Y . ( x 1 [ X ] , x 2 [ Y ]) ∈ R � A � ( ρ [ α �→ R ]) } (( x , y ) ∈ R � Θ ⊢ A � ( � Θ � ∆ θ ) ⇔ x = y ) ∀ x , y ∈ T � Θ ⊢ A � θ ⇒ Θ | − ⊢ e : A ⇒ � e � ∈ T � Θ ⊢ A � θ

  9. Manufacturing Relationally Parametric Models Option I: find them Operational Models (Pis, 2000)

  10. Manufacturing Relationally Parametric Models Option II: force them Mutually define base and relational interpretations of types (Reynolds, 1983) (Bainbridge et al. , 1990) then : prove Abstraction T � α � θ = θ ( α ) T � A → B � θ = T � A � θ → T � B � θ T � ∀ α. A � θ = { x : ∀ X . T � A � ( θ [ α �→ X ]) | ∀ X , Y , R ⊆ X × Y . R � τ � (∆ θ [ α �→ R ]) ( x A 1 ) ( x A 2 ) } R � α � ρ = ρ ( α ) R � A → B � ρ = { ( f 1 , f 2 ) | ∀ ( a 1 , a 2 ) ∈ R � A � ρ. ( f 1 a 1 , f 2 a 2 ) ∈ R � B � ρ } R � ∀ α.τ � ρ x y = { ( x 1 , x 2 ) | ∀ X , Y , R ⊆ X × Y . ( x [ X ] , y [ Y ]) ∈ R � τ � ( ρ [ α �→ R ]) } { prove Identity Extension

  11. Relational Parametricity for Higher Kinds

  12. Relational Parametricity for Higher Kinds How to interpret kinds? Implicitly: and So let us try: and � ∗ � R = ( X , Y ) �→ P ( X × Y ) � ∗ � = Set � ∗ � = Set � κ 1 → κ 2 � = � κ 1 � → � κ 2 � � κ � R : � κ � × � κ � → Set � ∗ � R = ( X , Y ) �→ P ( X , Y ) � κ 1 → κ 2 � R = ( F , G ) �→ ∀ X , Y . � κ 1 � R ( X , Y ) → � κ 2 � R ( FX , GY )

  13. Relational Parametricity for Higher Kinds Identity extension? Recall identity extension: No good answer in general. Solution build-in an “identity” for every semantic type operator require every semantic type operator to preserve identities (( x , y ) ∈ R � Θ ⊢ A : ∗ � ( � Θ � ∆ θ ) ⇔ x = y ) ∀ x , y ∈ T � Θ ⊢ A : ∗ � θ ⇒ What is � ∗ → ∗ � ∆ ( F ) ?

  14. Kinds as Reflexive Graphs � A kind is interpreted as a pair of (large) sets O and R , with maps: Kinds as “Categories without Composition” Interpret kinds as elements of Set RG � Reflexive Graph Categories Higher kinds are interpreted using the cartesian-closed structure. i (Dunphy and Reddy, 2004) (Robinson and Rosolini, 1994) (Hasegawa, 1994) δ 0 � • Let RG = • such that δ 0 ◦ i = id and δ 1 ◦ i = id . δ 1 1 . id : O → R src : R → O tgt : R → O

  15. Interpretation of Kinds Kinds interpreted as “categories without composition” Interpretation of Types — interpreted as a functor “without composition” — actually, natural transformations in Set RG — recreates the mutual induction used for System F Interpretation of Terms — interpreted as a natural transformations “without composition” — yields the standard abstraction theorem Interpretation of System F ω � ∗ � = ( Set , { ( A , B , R ⊆ A × B ) | A , B ∈ Set } ) Θ ⊢ A : κ 1 Θ | Γ ⊢ e : A

  16. Applications of Relational Parametricity for Higher Kinds

  17. Equality Types Specification Implementation Eq κ : κ → κ → ∗ , with refl κ : ∀ κ α. Eq κ αα elimEq κ : ∀ κ αβ. Eq κ αβ → ∀ κ → κ →∗ ρ. ( ∀ κ γ. ργγ ) → ραβ with β - and η -laws. Eq κ = λαβ : κ. ∀ κ → κ →∗ f . ( ∀ κ γ. f γ γ ) → f α β refl κ = Λ α. Λ ρ. λ f . f [ α ] elimEq κ = Λ αβ. λ e . Λ ρ. λ f . e [ ρ ] f use relational parametricity to prove the η -law

  18. Existential Types Specification Implementation For F : κ → ∗ , ∃ κ α. F α , with pack κ : ∀ κ →∗ ρ. ∀ κ α. ρα → ( ∃ κ α. ρα ) elimEx κ : ∀ κ →∗ ρ. ∀ ∗ β. ( ∀ κ α. ρα → β ) → ( ∃ κ α. ρα ) → β with β - and η -laws ∃ κ α. F α = ∀ ∗ β. ( ∀ κ α. F α → β ) → β pack κ = Λ ρα.λ x . Λ β.λ f . f [ α ] x elimEx κ = Λ ρβ.λ fe . e [ β ] f use relational parametricity to prove the η -law

  19. Higher-Kinded Initial Algebras Specification Implementation For functors ( F : ( κ → ∗ ) → ( κ → ∗ ) , fmap F ) , µ F : κ → ∗ , with in F : ∀ κ α. F ( µ F ) α → ( µ F ) α fold F : ∀ κ →∗ ρ. ( ∀ κ α. F ρα → ρα ) → ( ∀ κ α. ( µ F ) α → ρα ) with β - and η -laws µ F = λα. ∀ κ →∗ ρ. ( ∀ κ β. F ρβ → ρβ ) → ρα fold F = Λ ρ.λ f . Λ α.λ x . x [ ρ ] f in F = Λ γ.λ x . Λ ρ.λ f . f [ γ ] ( fmap F [ µ F ] [ ρ ] ( fold F [ ρ ] f ) [ γ ] x ) use relational parametricity to prove the η -law

  20. GADTs Generalised Algebraic Datatypes Example from Haskell: Encoding using Initial Algebras and Equality Types (Johann and Ghani, 2008) where data Z data S a data Vec :: * -> * -> * where VNil :: Vec a Z VCons :: a -> Vec a n -> Vec a (S n) Vec = λα. µ ( F α ) F αρ n = Eq ∗ n Z + ( ∃ ∗ n ′ .α × ρ n ′ × Eq ∗ n ( S n ′ ))

  21. Summary

  22. Summary Types as functors without composition Future work Natural number indexed types (in the paper) Generalised Algebraic Datatypes Initial Algebras Existentials Equality types Applications of Higher-kinded Parametricity (in the paper) Equality in parametric model implies observational equiv Constructed within impredicative CIC Final coalgebras Kinds as reflexive graphs Relationally parametric model of System F ω                       { Extension to dependent types

Recommend


More recommend