subtyping declaratively
play

Subtyping, Declaratively An Exercise in Mixed Induction and - PowerPoint PPT Presentation

Subtyping, Declaratively An Exercise in Mixed Induction and Coinduction Nils Anders Danielsson Thorsten Altenkirch (University of Nottingham) Lac-Beauport, Qu ebec, 2010-06-23 Introduction New way to define subtyping for recursive


  1. Subtyping, Declaratively An Exercise in Mixed Induction and Coinduction Nils Anders Danielsson Thorsten Altenkirch (University of Nottingham) Lac-Beauport, Qu´ ebec, 2010-06-23

  2. Introduction ◮ New way to define subtyping for recursive types. ◮ Example of the utility of mixed induction and coinduction ( ν X .µ Y . F X Y ).

  3. Induction in Agda

  4. Inductive types data N : Set where zero : N suc : N � N N ≈ µ X . 1 + X Structural recursion: + : N � N � N zero + n = n suc m + n = suc ( m + n )

  5. Inductive types Representation of (well-scoped) recursive types: data Ty ( n : N ) : Set where ⊥ : Ty n ⊤ : Ty n var : Fin n � Ty n � : Ty n � Ty n � Ty n µ � : Ty (1 + n ) � Ty (1 + n ) � Ty n σ, τ ::= ⊥ | ⊤ | X | σ � τ | µ X . σ � τ

  6. Inductive types Representation of (well-scoped) recursive types: ◮ µ X . X � X : σ : Ty 0 σ = µ var 0 � var 0 ◮ µ X . ( X � ⊥ ) � ⊤ : τ : Ty 0 τ = µ (var 0 � ⊥ ) � ⊤

  7. Inductive types Representation of (well-scoped) recursive types: ◮ Capture-avoiding substitution: [ ] : Ty (1 + n ) � Ty n � Ty n σ [ τ ]: Replaces variable 0 in σ with τ .

  8. Coinduction in Agda

  9. Coinductive types data Tree : Set where ⊥ : Tree ⊤ : Tree � : ∞ Tree � ∞ Tree � Tree ◮ ∞ marks coinductive arguments. ◮ Tree ≈ ν X . 1 + 1 + X × X . ◮ Delay and force: ♯ : A � ∞ A ♭ : ∞ A � A

  10. Coinductive types Guarded corecursion: � � : Ty 0 � Tree � ⊥ � = ⊥ � ⊤ � = ⊤ � var () � � σ � τ � = ♯ � σ � � ♯ � τ � � µ σ � τ � = � ( σ � τ ) [ µ σ � τ ] �

  11. Coinductive types Guarded corecursion: � � : Ty 0 � Tree � ⊥ � = ⊥ � ⊤ � = ⊤ � var () � � σ � τ � = ♯ � σ � � ♯ � τ � � µ σ � τ � = ♯ � σ [ µ σ � τ ] � � ♯ � τ [ µ σ � τ ] �

  12. Coinductive types � � � � µ var 0 � var 0 � = � � � � � � � � � � � � � � ⊤ � µ (var 0 � ⊥ ) � ⊤ � = � ⊥ � ⊤

  13. Subtyping

  14. Subtyping µ var 0 � var 0 � Type µ (var 0 � ⊥ ) � ⊤ � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤

  15. Subtyping � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤ ⊥ � Tree τ σ � Tree ⊤ ♭ τ 1 � Tree ♭ σ 1 ♭ σ 2 � Tree ♭ τ 2 (coinductive) σ 1 � σ 2 � Tree τ 1 � τ 2

  16. Indexed coinductive types Inference system ≈ indexed data type: : Tree � Tree � Set where data � Tree ⊥ : ⊥ � Tree τ ⊤ : σ � Tree ⊤ � : ∞ ( ♭ τ 1 � Tree ♭ σ 1 ) � ∞ ( ♭ σ 2 � Tree ♭ τ 2 ) � σ 1 � σ 2 � Tree τ 1 � τ 2

  17. Subtyping : Ty 0 � Ty 0 � Set � Type σ � Type τ = � σ � � Tree � τ � ex : µ var 0 � var 0 � Type µ (var 0 � ⊥ ) � ⊤ ex = ♯ ( ♯ ex � ♯ ⊥ ) � ♯ ⊤ � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤

  18. Subtyping : Ty 0 � Ty 0 � Set � Type σ � Type τ = � σ � � Tree � τ � Can we define this relation directly, without unfolding the types?

  19. Declarative vs. algorithmic Algorithmic Syntax-directed. Declarative Explicit rules for high-level concepts: reflexivity, transitivity. . .

  20. Declarative vs. algorithmic Algorithmic Syntax-directed. Declarative Explicit rules for high-level concepts: reflexivity, transitivity. . . Algorithmic Less modular. Declarative Problematic if coinductive.

  21. Coinductive transitivity Coinductive inference system with transitivity: trivial. : Ty 0 � Ty 0 � Set where data � . . . trans : ∞ ( τ 1 � τ 2 ) � ∞ ( τ 2 � τ 3 ) � τ 1 � τ 3 . . . . . . . . . . . . σ � τ τ � τ τ � τ τ � τ σ � τ τ � τ σ � τ

  22. Stuck? ◮ Stuck with syntax-directed definition? ◮ No, can use mixed induction and coinduction. Transitivity: inductive Remaining rules: coinductive

  23. Mixed induction and coinduction : Ty 0 � Ty 0 � Set where data � ⊥ : ⊥ � τ ⊤ : σ � ⊤ � : ∞ ( τ 1 � σ 1 ) � ∞ ( σ 2 � τ 2 ) � σ 1 � σ 2 � τ 1 � τ 2 unfold : µ τ 1 � τ 2 � ( τ 1 � τ 2 ) [ µ τ 1 � τ 2 ] fold : ( τ 1 � τ 2 ) [ µ τ 1 � τ 2 ] � µ τ 1 � τ 2 refl : τ � τ trans : τ 1 � τ 2 � τ 2 � τ 3 � τ 1 � τ 3

  24. Mixed induction and coinduction : Ty 0 � Ty 0 � Set where data � � : ∞ ( τ 1 � σ 1 ) � ∞ ( σ 2 � τ 2 ) � σ 1 � σ 2 � τ 1 � τ 2 trans : τ 1 � τ 2 � τ 2 � τ 3 � τ 1 � τ 3 � ≈ ν C . µ I . λ σ τ. ( ∃ σ 1 , σ 2 , τ 1 , τ 2 . σ ≡ σ 1 � σ 2 × τ ≡ τ 1 � τ 2 × C τ 1 σ 1 × C σ 2 τ 2 ) + ( ∃ χ. I σ χ × I χ τ )

  25. Mixed induction and coinduction : Ty 0 � Ty 0 � Set where data � ⊥ : ⊥ � τ ⊤ : σ � ⊤ � : ∞ ( τ 1 � σ 1 ) � ∞ ( σ 2 � τ 2 ) � σ 1 � σ 2 � τ 1 � τ 2 unfold : µ τ 1 � τ 2 � ( τ 1 � τ 2 ) [ µ τ 1 � τ 2 ] fold : ( τ 1 � τ 2 ) [ µ τ 1 � τ 2 ] � µ τ 1 � τ 2 refl : τ � τ trans : τ 1 � τ 2 � τ 2 � τ 3 � τ 1 � τ 3 Equivalent to � Type .

  26. Beware!

  27. Partiality monad A ⊥ Partial computations which may return something of type A . ⊥ ( A : Set ) : Set where data now : A � A ⊥ later : ∞ ( A ⊥ ) � A ⊥ never : A ⊥ never = later ( ♯ never )

  28. Equality When are two partial computations equivalent? Strong bisimilarity (coinductive): ∼ : A ⊥ � A ⊥ � Set where data now : now v ∼ now v later : ∞ ( ♭ x ∼ ♭ y ) � later x ∼ later y

  29. Equality When are two partial computations equivalent? Weak bisimilarity (mixed): ≈ : A ⊥ � A ⊥ � Set where data now : now v ≈ now v later : ∞ ( ♭ x ≈ ♭ y ) � later x ≈ later y later r : x ≈ ♭ y � x ≈ later y later l : ♭ x ≈ y � later x ≈ y

  30. The problem of “weak bisimulation up to” Weak bisimilarity is transitive. What happens if we make the definition more declarative? ≈ : A ⊥ � A ⊥ � Set where data now : now v ≈ now v later : ∞ ( ♭ x ≈ ♭ y ) � later x ≈ later y later r : x ≈ ♭ y � x ≈ later y later l : ♭ x ≈ y � later x ≈ y trans : x ≈ y � y ≈ z � x ≈ z

  31. The problem of “weak bisimulation up to” Weak bisimilarity is transitive. What happens if we make the definition more declarative? trivial : ( x y : A ⊥ ) � x ≈ y trivial x y = ≈� later r ( refl x ) � x later ( ♯ x ) ≈� later ( ♯ ( trivial x y )) � later ( ♯ y ) ≈� later l ( refl y ) � y �

  32. The problem of “weak bisimulation up to” Weak bisimilarity is transitive. What happens if we make the definition more declarative? ◮ Inductive case: Sound to postulate admissible rule. ◮ Coinductive case: Not always sound, proof may not be contractive. ◮ Known problem: “weak bisimulation up to”. ◮ Subtyping unproblematic: equivalent to � Type . �

  33. Conclusions ◮ Mixed induction and coinduction is a useful technique. ◮ Declarative, mostly coinductive inference systems possible. ◮ In particular: subtyping for recursive types. ◮ But don’t rely on intuitions which are only valid in the inductive case.

  34. ?

Recommend


More recommend