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 types. ◮ Example of the utility of mixed induction and coinduction ( ν X .µ Y . F X Y ).
Induction in Agda
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 )
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 . σ � τ
Inductive types Representation of (well-scoped) recursive types: ◮ µ X . X � X : σ : Ty 0 σ = µ var 0 � var 0 ◮ µ X . ( X � ⊥ ) � ⊤ : τ : Ty 0 τ = µ (var 0 � ⊥ ) � ⊤
Inductive types Representation of (well-scoped) recursive types: ◮ Capture-avoiding substitution: [ ] : Ty (1 + n ) � Ty n � Ty n σ [ τ ]: Replaces variable 0 in σ with τ .
Coinduction in Agda
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
Coinductive types Guarded corecursion: � � : Ty 0 � Tree � ⊥ � = ⊥ � ⊤ � = ⊤ � var () � � σ � τ � = ♯ � σ � � ♯ � τ � � µ σ � τ � = � ( σ � τ ) [ µ σ � τ ] �
Coinductive types Guarded corecursion: � � : Ty 0 � Tree � ⊥ � = ⊥ � ⊤ � = ⊤ � var () � � σ � τ � = ♯ � σ � � ♯ � τ � � µ σ � τ � = ♯ � σ [ µ σ � τ ] � � ♯ � τ [ µ σ � τ ] �
Coinductive types � � � � µ var 0 � var 0 � = � � � � � � � � � � � � � � ⊤ � µ (var 0 � ⊥ ) � ⊤ � = � ⊥ � ⊤
Subtyping
Subtyping µ var 0 � var 0 � Type µ (var 0 � ⊥ ) � ⊤ � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤
Subtyping � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤ ⊥ � Tree τ σ � Tree ⊤ ♭ τ 1 � Tree ♭ σ 1 ♭ σ 2 � Tree ♭ τ 2 (coinductive) σ 1 � σ 2 � Tree τ 1 � τ 2
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
Subtyping : Ty 0 � Ty 0 � Set � Type σ � Type τ = � σ � � Tree � τ � ex : µ var 0 � var 0 � Type µ (var 0 � ⊥ ) � ⊤ ex = ♯ ( ♯ ex � ♯ ⊥ ) � ♯ ⊤ � � � � � ⊤ � Tree � � � � � ⊥ � � � � � � � � � ⊤
Subtyping : Ty 0 � Ty 0 � Set � Type σ � Type τ = � σ � � Tree � τ � Can we define this relation directly, without unfolding the types?
Declarative vs. algorithmic Algorithmic Syntax-directed. Declarative Explicit rules for high-level concepts: reflexivity, transitivity. . .
Declarative vs. algorithmic Algorithmic Syntax-directed. Declarative Explicit rules for high-level concepts: reflexivity, transitivity. . . Algorithmic Less modular. Declarative Problematic if coinductive.
Coinductive transitivity Coinductive inference system with transitivity: trivial. : Ty 0 � Ty 0 � Set where data � . . . trans : ∞ ( τ 1 � τ 2 ) � ∞ ( τ 2 � τ 3 ) � τ 1 � τ 3 . . . . . . . . . . . . σ � τ τ � τ τ � τ τ � τ σ � τ τ � τ σ � τ
Stuck? ◮ Stuck with syntax-directed definition? ◮ No, can use mixed induction and coinduction. Transitivity: inductive Remaining rules: coinductive
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
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 χ τ )
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 .
Beware!
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 )
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
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
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
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 �
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 . �
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.
?
Recommend
More recommend