wander types - realizing data structures through ... · leftist heaps are heap-ordered binary trees...

72
IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion Wander Types Realizing Data Structures Through Coinduction-Recursion Venanzio Capretta University of Nottingham Shonan Meeting on Dependently Typed Programming, 2011 Venanzio CaprettaUniversity of Nottingham Wander Types

Upload: others

Post on 09-Oct-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Wander Types

Realizing Data Structures Through Coinduction-Recursion

Venanzio CaprettaUniversity of Nottingham

Shonan Meeting on Dependently Typed Programming, 2011

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 2: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

IR definitions

Inductive-Recursive (IR) definitions

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 3: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 4: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 5: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 6: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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 [Dybjer 2001, Dybjer/Setzer 1999]

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 7: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 8: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 9: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Advanced Data Structures

Heap (priority queue) on ordered set A,4

Heap : Set

empty : HeapisEmpty : Heap → B

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 10: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 11: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 12: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 13: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 14: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 15: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Leftist Heaps

Definition of Leftist Heaps [Crane 1972, Knuth 1973]

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 16: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Leftist Heaps

Definition of Leftist Heaps [Crane 1972, Knuth 1973]

Heaps are often implemented as heap-ordered trees, inwhich the element at each node is no larger than theelements at its children. Under this ordering, theminimum element in a tree is always at the root.Leftist heaps are heap-ordered binary trees that satisfythe leftist property: the rank of any left child is at leastas large as the rank of its right sibling. The rank of anode is defined to be the length of its right spine (i.e.,the rightmost path from the node in question to anempty node).

[Okasaki 1998]

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 17: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Inductive-Recursive Leftist Heaps

lHeap : Set

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 18: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Inductive-Recursive Leftist Heaps

lHeap : Set

rootor : lHeap → A → A

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 19: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Inductive-Recursive Leftist Heaps

lHeap : Set

rootor : lHeap → A → A

rank : lHeap → N

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 20: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Inductive-Recursive Leftist Heaps

lHeap : Set

leaf : lHeap

rootor : lHeap → A → A

rootor leaf = id

rank : lHeap → N

rank leaf = 0

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 21: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 22: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 23: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 24: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Codes for IR Definitions

Dybjer/Setzer: codes for Inductive Recursive definitions

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 25: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Codes for IR Definitions

Dybjer/Setzer: codes for Inductive Recursive definitions

Given D result type: IRD type of codes for ind/rec defs

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 26: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Codes for IR Definitions

Dybjer/Setzer: codes for Inductive Recursive definitions

Given D result type: IRD type of codes for ind/rec defs

c : IRD decodes to:

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 27: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Codes for IR Definitions

Dybjer/Setzer: codes for Inductive Recursive definitions

Given D result type: IRD type of codes for ind/rec defs

c : IRD decodes to:

Elemc : Setfunc : Elemc → D

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 28: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Codes for IR Definitions

Dybjer/Setzer: codes for Inductive Recursive definitions

Given D result type: IRD type of codes for ind/rec defs

c : IRD decodes to:

Elemc : Setfunc : Elemc → D

IRD is itself a simple inductive type

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 29: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 30: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element dd : D

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 31: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element d Elemc ≃ {•}d : D func(•) = d

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 32: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element d Elemc ≃ {•}d : D func(•) = d

A constructor with a non-recursive argument

c = chooseAu

A : Set

u : A → IRD

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 33: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element d Elemc ≃ {•}d : D func(•) = d

A constructor with a non-recursive argument

c = chooseAu Elemc ≃ (a : A)× Elem(u a)

A : Set func〈a, x〉 = fun(u a)(x)

u : A → IRD

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 34: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element d Elemc ≃ {•}d : D func(•) = d

A constructor with a non-recursive argument

c = chooseAu Elemc ≃ (a : A)× Elem(u a)

A : Set func〈a, x〉 = fun(u a)(x)

u : A → IRD

A constructor with recursive arguments

c = recurse I v

I : Set

v : (I → D) → IRD

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 35: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Constructors of IR codes

A single element with a given value

c = element d Elemc ≃ {•}d : D func(•) = d

A constructor with a non-recursive argument

c = chooseAu Elemc ≃ (a : A)× Elem(u a)

A : Set func〈a, x〉 = fun(u a)(x)

u : A → IRD

A constructor with recursive arguments

c = recurse I v Elemc ≃ (t : I → Elemc)× Elemv(func◦t)

I : Set func〈t, y〉 = funv(func◦t)(y)

v : (I → D) → IRD

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 36: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 37: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 38: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

chooseAλa.

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 39: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

chooseAλa.recurseBλ〈root1, rank1, root2, rank2〉.

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 40: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

chooseAλa.recurseBλ〈root1, rank1, root2, rank2〉.choose (a 4 (root1 a) ∧ a 4 (root2 a) ∧ (rank2 ≤ rank1))

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 41: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

chooseAλa.recurseBλ〈root1, rank1, root2, rank2〉.choose (a 4 (root1 a) ∧ a 4 (root2 a) ∧ (rank2 ≤ rank1))λ . element 〈λx .a, 1 + rank2〉

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 42: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Code for Leftist Heaps

The Leftist Heap type has the following IR code:

lhcode : IR ((A → A)× N)= element 〈id, 0〉+

chooseAλa.recurseBλ〈root1, rank1, root2, rank2〉.choose (a 4 (root1 a) ∧ a 4 (root2 a) ∧ (rank2 ≤ rank1))λ . element 〈λx .a, 1 + rank2〉

+: separate different constructors, non-dependent chooseElements of B → (A → A)× N:quadruples 〈root1, rank1, root2, rank2〉

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 43: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Slice Category

Inductive Types: initial algebras in the base category.

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 44: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 45: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 46: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 47: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 48: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Functor Θ : Set ↓D → Set ↓DIR definition: Initial Θ-algebraWhat are the final coalgebras?Call them Wander Types

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 49: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Wander Types: Final Slice Coalgebra

Coinductive version of leftist heaps?

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 50: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Wander Types: Final Slice Coalgebra

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

Wander Types

Page 51: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Wander Types: Final Slice Coalgebra

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 recursive functionsimultaneously.Final coalgebras in slice categories

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 52: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Wander Types: Final Slice Coalgebra

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 recursive functionsimultaneously.Final coalgebras in slice categories

Mixed Inductive-Coinductive Definitions [Danielsson/Altenkirch 2009]

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 53: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Mixed Induction-Coinduction

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

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 54: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Mixed Induction-Coinduction

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

CoInductive ZeroOne : Set

zero : ZeroOne → ZeroOneone : ZeroOne → ZeroOne

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 55: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 56: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 57: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 58: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 59: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 60: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 61: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 62: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Stream Processors

Other example [Ghani/Hancock/Pattinson 2009]

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

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 63: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 64: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 65: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 66: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 67: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

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

Wander Types

Page 68: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Perspective

Induction-recursion:Direct implementation of advanced data types

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 69: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Perspective

Induction-recursion:Direct implementation of advanced data types

Leftist heaps

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 70: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Perspective

Induction-recursion:Direct implementation of advanced data types

Leftist heaps

Dybjer/Setzer codes

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 71: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Perspective

Induction-recursion:Direct implementation of advanced data types

Leftist heaps

Dybjer/Setzer codes

Coinductive version (Wander types) leads to a realization ofmixed induction-coinduction

Venanzio CaprettaUniversity of Nottingham

Wander Types

Page 72: Wander Types - Realizing Data Structures Through ... · Leftist heaps are heap-ordered binary trees that satisfy the leftist property: the rank of any left child is at least as large

IR definitions Leftist Heaps Dybjer/Setzer Codes Slice Categories Wander Types Conclusion

Perspective

Induction-recursion:Direct implementation of advanced data types

Leftist heaps

Dybjer/Setzer codes

Coinductive version (Wander types) leads to a realization ofmixed induction-coinduction

Data with fine control on structural properties

Venanzio CaprettaUniversity of Nottingham

Wander Types