induction-recursion a polymorphic representation · leftist heaps are heap-ordered binary trees...

69
Induction- Recursion A polymorphic representation Venanzio Capretta University of Nottingham IR definitions Leftist Heaps Slice Categories Wander Types Conclusion Coda Induction-Recursion A polymorphic representation Venanzio Capretta University of Nottingham DTP 2011, Nijmegen, The Netherlands Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Upload: others

Post on 09-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Induction-Recursion

A polymorphic representation

Venanzio CaprettaUniversity of Nottingham

DTP 2011, Nijmegen, The Netherlands

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 2: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

IR definitions

Inductive-Recursive (IR) definitions

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 3: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

IR definitions

Inductive-Recursive (IR) definitionsSimultaneously define

◮ an inductive type T : Set

◮ a recursive function on it f : T → D

mutually dependent

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 4: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

IR definitions

Inductive-Recursive (IR) definitionsSimultaneously define

◮ an inductive type T : Set

◮ a recursive function on it f : T → D

mutually dependent

◮ The constructors of T can use f

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 5: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

IR definitions

Inductive-Recursive (IR) definitionsSimultaneously define

◮ an inductive type T : Set

◮ a recursive function on it f : T → D

mutually dependent

◮ The constructors of T can use f

◮ f recursive over T

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 6: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

IR definitions

Inductive-Recursive (IR) definitionsSimultaneously define

◮ an inductive type T : Set

◮ a recursive function on it f : T → D

mutually dependent

◮ The constructors of T can use f

◮ f recursive over T

Definition of Type Universes [Martin-Lof 1984, Palmgren 1998]

General Definition [Dybjier 2001, Dybjier/Setzer 1999]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 7: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type(large) type of codes for (small) types

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 8: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type(large) type of codes for (small) types

◮ El : U → Typedecoding function, giving type of elements of codes

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 9: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

nat : U

◮ El : U → Type

El nat = N

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 10: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

nat : Uprod : U → U → U

◮ El : U → Type

El nat = N

El (prod a b) = (El a)× (El b)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 11: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

nat : Uprod : U → U → Usum : U → U → U

◮ El : U → Type

El nat = N

El (prod a b) = (El a)× (El b)El (sum a b) = (El a) + (El b)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 12: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

nat : Uprod : U → U → Usum : U → U → Uarrow : U → U → U

◮ El : U → Type

El nat = N

El (prod a b) = (El a)× (El b)El (sum a b) = (El a) + (El b)El (arrow a b) = (El a) → (El b)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 13: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

· · ·pi : (a : U; b : El a → U) → U

◮ El : U → Type

· · ·El (pi a b) = Πx : El a.El (b x)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 14: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Type Universes

◮ U : Type

· · ·pi : (a : U; b : El a → U) → Usig : (a : U; b : El a → U) → U

◮ El : U → Type

· · ·El (pi a b) = Πx : El a.El (b x)El (sig a b) = Σx : El a.El (b x)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 15: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 16: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 17: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 18: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

insert : A → Heap → Heap

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 19: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

insert : A → Heap → HeapfindMin : Heap → A

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 20: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

insert : A → Heap → HeapfindMin : Heap → AdeleteMin : Heap → Heap

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 21: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

insert : A → Heap → HeapfindMin : Heap → AdeleteMin : Heap → Heap

merge : Heap → Heap → Heap

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 22: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

insert : A → Heap → HeapfindMin : Heap → AdeleteMin : Heap → Heap

merge : Heap → Heap → Heap

With lists: linear complexityWith leftist heaps: logarithmic complexity

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 23: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Leftist Heaps

Definition of Leftist Heaps [Crane 1972, Knuth 1973]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 24: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Leftist Heaps

Definition of Leftist Heaps [Crane 1972, Knuth 1973]

Heaps are often implemented as heap-ordered trees,in which the element at each node is no larger thanthe elements at its children. Under this ordering, theminimum element in a tree is always at the root.Leftist heaps are heap-ordered binary trees thatsatisfy the leftist property: the rank of any left childis at least as large as the rank of its right sibling. Therank of a node is defined to be the length of its rightspine (i.e., the rightmost path from the node inquestion to an empty node).

[Okasaki 1998]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 25: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 26: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

◮ rootor : lHeap → A → A

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 27: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

◮ rootor : lHeap → A → A

◮ rank : lHeap → N

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 28: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Setleaf : lHeap

◮ rootor : lHeap → A → A

rootor leaf = id

◮ rank : lHeap → N

rank leaf = 0

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 29: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

leaf : lHeapnode : (a : A; t1, t2 : lHeap)

→ a 4 (rootor t1 a) → a 4 (rootor t2 a)→ (rank t2) ≤ (rank t1) → lHeap

◮ rootor : lHeap → A → A

rootor leaf = id

◮ rank : lHeap → N

rank leaf = 0

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 30: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

leaf : lHeapnode : (a : A; t1, t2 : lHeap)

→ a 4 (rootor t1 a) → a 4 (rootor t2 a)→ (rank t2) ≤ (rank t1) → lHeap

◮ rootor : lHeap → A → A

rootor leaf = idrootor (node a t1 t2 . . .) = λx .a

◮ rank : lHeap → N

rank leaf = 0

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 31: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Inductive-Recursive Leftist Heaps

◮ lHeap : Set

leaf : lHeapnode : (a : A; t1, t2 : lHeap)

→ a 4 (rootor t1 a) → a 4 (rootor t2 a)→ (rank t2) ≤ (rank t1) → lHeap

◮ rootor : lHeap → A → A

rootor leaf = idrootor (node a t1 t2 . . .) = λx .a

◮ rank : lHeap → N

rank leaf = 0rank (node a t1 t2 . . .) = 1 + rank t2

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 32: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 33: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.IR Types: initial algebras in a slice category.

[Dybjer/Setzer 1999, Hancock/Ghani 2011]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 34: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.IR Types: initial algebras in a slice category.

[Dybjer/Setzer 1999, Hancock/Ghani 2011]

For every type D the slice category Set ↓D has:

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 35: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.IR Types: initial algebras in a slice category.

[Dybjer/Setzer 1999, Hancock/Ghani 2011]

For every type D the slice category Set ↓D has:

Objects: pairs 〈X , f 〉where X : Set f : X → D

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 36: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.IR Types: initial algebras in a slice category.

[Dybjer/Setzer 1999, Hancock/Ghani 2011]

For every type D the slice category Set ↓D has:

Objects: pairs 〈X , f 〉where X : Set f : X → D

Morphisms: functions g : 〈X1, f1〉 → 〈X2, f2〉where g : X1 → X2 f2 ◦ g = f1

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 37: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Category

Inductive Types: initial algebras in the base category.IR Types: initial algebras in a slice category.

[Dybjer/Setzer 1999, Hancock/Ghani 2011]

For every type D the slice category Set ↓D has:

Objects: pairs 〈X , f 〉where X : Set f : X → D

Morphisms: functions g : 〈X1, f1〉 → 〈X2, f2〉where g : X1 → X2 f2 ◦ g = f1

In the case of leftist heaps:D = (A → A)× N

〈lHeap, 〈rootor, rank〉〉 object of Set ↓D

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 38: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Functors

A functor Θ : Set ↓D → Set ↓D has three components:

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 39: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Functors

A functor Θ : Set ↓D → Set ↓D has three components:

◮ Set part, for every 〈X , f 〉: F X f : Set

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 40: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Functors

A functor Θ : Set ↓D → Set ↓D has three components:

◮ Set part, for every 〈X , f 〉: F X f : Set

◮ Arrow part: e X f : F X f → D

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 41: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Functors

A functor Θ : Set ↓D → Set ↓D has three components:

◮ Set part, for every 〈X , f 〉: F X f : Set

◮ Arrow part: e X f : F X f → D

◮ Mapping: for every g : 〈X1, f1〉 → 〈X2, f2〉:Θ g : F X1 f1 → F X2 f2

satisfying some equalities

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 42: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Slice Functors

A functor Θ : Set ↓D → Set ↓D has three components:

◮ Set part, for every 〈X , f 〉: F X f : Set

◮ Arrow part: e X f : F X f → D

◮ Mapping: for every g : 〈X1, f1〉 → 〈X2, f2〉:Θ g : F X1 f1 → F X2 f2

satisfying some equalitiesNote:No underlying functor on Set: F essentially depends on f

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 43: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Initial Slice Algebras

A Θ-algebra is a triple 〈X , f , g〉

X : Setf : X → Dg : 〈F X f , e X f 〉 → 〈X , f 〉

The inductive-recursive pair is the initial Θ-algebra

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 44: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Initial Slice Algebras

A Θ-algebra is a triple 〈X , f , g〉

X : Setf : X → Dg : 〈F X f , e X f 〉 → 〈X , f 〉

The inductive-recursive pair is the initial Θ-algebra

How to represent ind-rec in a system (COQ) not supporting it?

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 45: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Second Order Encoding

Future Work:

Second-order representation of induction recursion [VC 2004]

Similar to polymorphic definition of (weak) inductive types:[Bohm/Berarducci 1985, Girard 1989]

µΘ = Π〈X , f 〉 : Set ↓D.(Θ〈X , f 〉 → 〈X , f 〉)

The inductive-recursive type is the product of all Θ-algebras[Also coinductive version, Wraith 1989, Geuvers 1992]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 46: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Second Order Encoding

Future Work:

Second-order representation of induction recursion [VC 2004]

Similar to polymorphic definition of (weak) inductive types:[Bohm/Berarducci 1985, Girard 1989]

µΘ = Π〈X , f 〉 : Set ↓D.(Θ〈X , f 〉 → 〈X , f 〉)

The inductive-recursive type is the product of all Θ-algebras[Also coinductive version, Wraith 1989, Geuvers 1992]

Better Way [Conor McBride]

Inductive family indexed on the result of the function.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 47: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Wander Types: Final Θ-algebra

Coinductive version of leftist heaps?

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 48: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Wander Types: Final Θ-algebra

Coinductive version of leftist heaps?Potentially infinite binary trees.But the rank function must still be defined.Right spine must be finite.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 49: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Wander Types: Final Θ-algebra

Coinductive version of leftist heaps?Potentially infinite binary trees.But the rank function must still be defined.Right spine must be finite.

Wander Types: Define a coinductive type and a recursivefunction simultaneously.Final coalgebras in slice categories

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 50: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Wander Types: Final Θ-algebra

Coinductive version of leftist heaps?Potentially infinite binary trees.But the rank function must still be defined.Right spine must be finite.

Wander Types: Define a coinductive type and a recursivefunction simultaneously.Final coalgebras in slice categories

Mixed Inductive-Coinductive Definitions [Danielsson/Altenkirch 2009]

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 51: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Mixed Induction-Coinduction

Streams 0s and 1s, with no infinite consecutive 1s.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 52: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Mixed Induction-Coinduction

Streams 0s and 1s, with no infinite consecutive 1s.

◮ CoInductive ZeroOne : Set

zero : ZeroOne → ZeroOneone : ZeroOne → ZeroOne

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 53: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Mixed Induction-Coinduction

Streams 0s and 1s, with no infinite consecutive 1s.

◮ CoInductive ZeroOne : Set

zero : ZeroOne → ZeroOneone : ZeroOne → ZeroOne

◮ Simultaneously count1 : ZeroOne → N

count1 (zero s) = 0count1 (one s) = 1 + count1 s

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 54: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Mixed Induction-Coinduction

Streams 0s and 1s, with no infinite consecutive 1s.

◮ CoInductive ZeroOne : Set

zero : ZeroOne → ZeroOneone : ZeroOne → ZeroOne

◮ Simultaneously count1 : ZeroOne → N

count1 (zero s) = 0count1 (one s) = 1 + count1 s

◮ We can even make the zeros finite (still infinite sequences)

count0 : ZeroOne → N

count0 (zero s) = 1 + count0 scount0 (one s) = 0

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 55: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

No-zigzag type

Define a type of potentially infinite binary trees, but with therestriction that there can’t be infinite zigzags.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 56: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

No-zigzag type

Define a type of potentially infinite binary trees, but with therestriction that there can’t be infinite zigzags.

CoInductive ZigZag : Set

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 57: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

No-zigzag type

Define a type of potentially infinite binary trees, but with therestriction that there can’t be infinite zigzags.

CoInductive ZigZag : SetSimultaneously zigs : ZigZag → N

zags : ZigZag → N

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 58: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

No-zigzag type

Define a type of potentially infinite binary trees, but with therestriction that there can’t be infinite zigzags.

CoInductive ZigZag : SetSimultaneously zigs : ZigZag → N

zags : ZigZag → N

Constructorszzlf : ZigZagzznd : ZigZag → ZigZag → ZigZag

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 59: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

No-zigzag type

Define a type of potentially infinite binary trees, but with therestriction that there can’t be infinite zigzags.

CoInductive ZigZag : SetSimultaneously zigs : ZigZag → N

zags : ZigZag → N

Constructorszzlf : ZigZagzznd : ZigZag → ZigZag → ZigZag

Equationszigs zzlf = 0 zigs (zznd t1 t2) = 1 + zags t1zags zzlf = 0 zags (zznd t1 t2) = 1 + zigs t2

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 60: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Perspective

◮ Induction-recursion: direct implementation of advanceddata types

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 61: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Perspective

◮ Induction-recursion: direct implementation of advanceddata types

◮ Realization by polymorphic quantification in slice category

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 62: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Perspective

◮ Induction-recursion: direct implementation of advanceddata types

◮ Realization by polymorphic quantification in slice category

◮ Coinductive version (Wander types) leads to a realizationof mixed induction-coinduction

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 63: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Perspective

◮ Induction-recursion: direct implementation of advanceddata types

◮ Realization by polymorphic quantification in slice category

◮ Coinductive version (Wander types) leads to a realizationof mixed induction-coinduction

◮ Data with fine control on structural properties

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 64: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Stream Processors

Other example [Ghani/Hancock/Pattinson 2009]

Continuous Stream Processors (StreamA) → (StreamB)represented by nested fixed points.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 65: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Stream Processors

Other example [Ghani/Hancock/Pattinson 2009]

Continuous Stream Processors (StreamA) → (StreamB)represented by nested fixed points.

StrProc (A,B) : Setwrite : B → StrProc (A,B) → StrProc (A,B)read : (A → StrProc (A,B)) → StrProc (A,B)

write coinductive, read inductive.

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 66: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Stream Processors

Other example [Ghani/Hancock/Pattinson 2009]

Continuous Stream Processors (StreamA) → (StreamB)represented by nested fixed points.

StrProc (A,B) : Setwrite : B → StrProc (A,B) → StrProc (A,B)read : (A → StrProc (A,B)) → StrProc (A,B)

write coinductive, read inductive.

eval : StrProc (A,B) → StreamA → StreamBeval (write b p) s = b :: eval p seval (read f ) (a :: s) = eval (f a) s

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 67: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Simultaneous Coinductive-Recursive Definition

◮ Coinductive StrProc (A,B) : Type

write : B → StrProc (A,B) → StrProc (A,B)read : (A → StrProc (A,B)) → StrProc (A,B)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 68: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Simultaneous Coinductive-Recursive Definition

◮ Coinductive StrProc (A,B) : Type

write : B → StrProc (A,B) → StrProc (A,B)read : (A → StrProc (A,B)) → StrProc (A,B)

◮ Simultaneous readWf : StrProc (A,B) → Prop

readWf (write b s) = ⊤readWf (read f ) = ∀x : A.readWf (f x)

That’s a large type. But alternatively ...

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation

Page 69: Induction-Recursion A polymorphic representation · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

Induction-Recursion

A polymorphicrepresentation

VenanzioCapretta

University ofNottingham

IR definitions

Leftist Heaps

SliceCategories

Wander Types

Conclusion

Coda

Simultaneous Coinductive-Recursive Definition

◮ Coinductive StrProc (A,B) : Set

write : B → StrProc (A,B) → StrProc (A,B)read : (A → StrProc (A,B)) → StrProc (A,B)

◮ Simultaneous readTree : StrProc (A,B) → TreeA

readTree (write b s) = leafreadTree (read f ) = node (λx .readTree (f x))

(TreeA: well-founded A-branching trees.)

Venanzio CaprettaUniversity of Nottingham Induction-Recursion A polymorphic representation