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

subtyping declaratively
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

Introduction

◮ New way to define subtyping for

recursive types.

◮ Example of the utility of

mixed induction and coinduction (νX.µY .F X Y ).

slide-3
SLIDE 3

Induction in Agda

slide-4
SLIDE 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)

slide-5
SLIDE 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. σ τ

slide-6
SLIDE 6

Inductive types

Representation of (well-scoped) recursive types:

◮ µX. X X:

σ : Ty 0 σ = µ var 0 var 0

◮ µX. (X ⊥) ⊤:

τ : Ty 0 τ = µ (var 0 ⊥) ⊤

slide-7
SLIDE 7

Inductive types

Representation of (well-scoped) recursive types:

◮ Capture-avoiding substitution:

[ ] : Ty (1 + n) Ty n Ty n σ [ τ ]: Replaces variable 0 in σ with τ.

slide-8
SLIDE 8

Coinduction in Agda

slide-9
SLIDE 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

slide-10
SLIDE 10

Coinductive types

Guarded corecursion: : Ty 0 Tree ⊥ = ⊥ ⊤ = ⊤ var ()

  • σ τ = ♯ σ ♯ τ

µ σ τ = (σ τ) [ µ σ τ ]

slide-11
SLIDE 11

Coinductive types

Guarded corecursion: : Ty 0 Tree ⊥ = ⊥ ⊤ = ⊤ var ()

  • σ τ = ♯ σ ♯ τ

µ σ τ = ♯ σ [ µ σ τ ] ♯ τ [ µ σ τ ]

slide-12
SLIDE 12

Coinductive types

µ var 0 var 0 =

  • µ (var 0 ⊥) ⊤ =

⊥ ⊤

slide-13
SLIDE 13

Subtyping

slide-14
SLIDE 14

Subtyping

µ var 0 var 0 Type µ (var 0 ⊥) ⊤

  • Tree

⊥ ⊤

slide-15
SLIDE 15

Subtyping

  • Tree

⊥ ⊤ ⊥ Tree τ σ Tree ⊤ ♭ τ1 Tree ♭ σ1 ♭ σ2 Tree ♭ τ2 σ1 σ2 Tree τ1 τ2 (coinductive)

slide-16
SLIDE 16

Indexed coinductive types

Inference system ≈ indexed data type: data Tree : Tree Tree Set where ⊥ : ⊥ Tree τ ⊤ : σ Tree ⊤

  • : ∞ (♭ τ1 Tree ♭ σ1)

∞ (♭ σ2 Tree ♭ τ2 ) σ1 σ2 Tree τ1 τ2

slide-17
SLIDE 17

Subtyping

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

  • Tree

⊥ ⊤

slide-18
SLIDE 18

Subtyping

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

slide-19
SLIDE 19

Declarative vs. algorithmic

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

slide-20
SLIDE 20

Declarative vs. algorithmic

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

slide-21
SLIDE 21

Coinductive transitivity

Coinductive inference system with transitivity: trivial. data

  • : Ty 0 Ty 0 Set where

. . . trans : ∞ (τ1 τ2) ∞ (τ2 τ3) τ1 τ3 . . . σ τ . . . τ τ σ τ . . . τ τ . . . τ τ τ τ σ τ

slide-22
SLIDE 22

Stuck?

◮ Stuck with syntax-directed definition? ◮ No, can use mixed induction and coinduction.

Transitivity: inductive Remaining rules: coinductive

slide-23
SLIDE 23

Mixed induction and coinduction

data

  • : Ty 0 Ty 0 Set where

⊥ : ⊥ τ ⊤ : σ ⊤

  • : ∞ (τ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

slide-24
SLIDE 24

Mixed induction and coinduction

data

  • : Ty 0 Ty 0 Set where
  • : ∞ (τ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 χ τ)

slide-25
SLIDE 25

Mixed induction and coinduction

data

  • : Ty 0 Ty 0 Set where

⊥ : ⊥ τ ⊤ : σ ⊤

  • : ∞ (τ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 .

slide-26
SLIDE 26

Beware!

slide-27
SLIDE 27

Partiality monad

A ⊥ Partial computations which may return something of type A. data

⊥ (A : Set) : Set where

now : A A ⊥ later : ∞ (A ⊥) A ⊥ never : A ⊥ never = later (♯ never)

slide-28
SLIDE 28

Equality

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

slide-29
SLIDE 29

Equality

When are two partial computations equivalent? Weak bisimilarity (mixed): data ≈ : A ⊥ A ⊥ Set where now : now v ≈ now v later : ∞ (♭ x ≈ ♭ y) later x ≈ later y laterr : x ≈ ♭ y

  • x ≈ later y

laterl : ♭ x ≈ y later x ≈ y

slide-30
SLIDE 30

The problem of “weak bisimulation up to”

Weak bisimilarity is transitive. What happens if we make the definition more declarative? data ≈ : A ⊥ A ⊥ Set where now : now v ≈ now v later : ∞ (♭ x ≈ ♭ y) later x ≈ later y laterr : x ≈ ♭ y

  • x ≈ later y

laterl : ♭ x ≈ y later x ≈ y trans : x ≈ y y ≈ z x ≈ z

slide-31
SLIDE 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 = x ≈ laterr (refl x) later (♯ x) ≈ later (♯ (trivial x y)) later (♯ y) ≈ laterl (refl y) y

slide-32
SLIDE 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 .

slide-33
SLIDE 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.

slide-34
SLIDE 34

?