Recursive coalgebras from comonads Venanzio Capretta (U. of Ottawa) - - PowerPoint PPT Presentation

recursive coalgebras from comonads
SMART_READER_LITE
LIVE PREVIEW

Recursive coalgebras from comonads Venanzio Capretta (U. of Ottawa) - - PowerPoint PPT Presentation

Recursive coalgebras from comonads Venanzio Capretta (U. of Ottawa) Tarmo Uustalu (Inst. of Cybernetics, Tallinn) Varmo Vene (U. of Tartu) Braga, 23 Jan. 2006 1 Motivation It is handy to define functions using


slide-1
SLIDE 1

✬ ✫ ✩ ✪

Recursive coalgebras from comonads

Venanzio Capretta (U. of Ottawa) Tarmo Uustalu (Inst. of Cybernetics, Tallinn) Varmo Vene (U. of Tartu) Braga, 23 Jan. 2006

1

slide-2
SLIDE 2

✬ ✫ ✩ ✪ Motivation

  • It is handy to define functions using (general) recursion, but when is this

justified, i.e, in which situations can we be sure that the equation we want to rely on has a unique solution?

  • Approches: inductive, coinductive types, structured recursion, corecursion

schemes, guarded-by-destructors recursion, guarded-by-constructors corecursion; general totality/termination/productivity analysis methodologies.

  • Structured recursion/corecursion for initial algebras/final coalgebras: Lenisa

(et al.), Uustalu-Vene-Pardo, Bartels.

2

slide-3
SLIDE 3

✬ ✫ ✩ ✪

  • This talk: Osius’ recursive coalgebras (in opposition to initial algebras, i.e.,

inductive types) – as a framework to deal with guarded-by-destructors recursion generically, – generalizing Uustalu-Vene-Pardo and Bartels.

  • Everything dualizes: Could just as well do completely iterative algebras (in
  • pposition to final coalgebras) to deal with guarded-by-constructors

corecursion.

3

slide-4
SLIDE 4

✬ ✫ ✩ ✪ Outline

  • Recursive coalgebras: motivation, definition, examples
  • Basic properties
  • Two variations: strongly recursive coalgebras, parametrically recursive

coalgebras.

  • Recursive coalgebras from comonads
  • Recursive coalgebras from (cofree comonads of) functors

4

slide-5
SLIDE 5

✬ ✫ ✩ ✪ Recursive coalgebras: motivation

  • Think of quicksort: Let Z be a set linearly ordered by ≤.

One usually defines quicksort recursively. qsort: ListZ → ListZ qsort [] = [] qsort (x : l) = qsort(l≤x) + + (x : qsort(l>x))

  • Why does this recursive (a priori dubious) “definition” actually qualify as a

definition, i.e., how do we know the underlying equation has a unique solution?

  • Notice the equation has the form

qsort = Φ(qsort) where Φ : Set(ListZ, ListZ) → Set(ListZ, ListZ). This is quite unstructured.

5

slide-6
SLIDE 6

✬ ✫ ✩ ✪

  • With a minimal rearrangement of the equation, we see it has a clear overt
  • structure. We can rewrite it as

qsort = qmerge ◦ BTqsort ◦ qsplit where BTZ X = 1 + Z × X × X, i.e. 1 + Z × ListZ × ListZ

id+id×qsort×qsort

  • ListZ

qsort

  • qsplit
  • 1 + Z × ListZ × ListZ

qmerge ListZ 6

slide-7
SLIDE 7

✬ ✫ ✩ ✪

  • Preparation of argument values for recursive calls:

qsplit: ListZ → 1 + Z × ListZ × ListZ qsplit [] = inl(∗) qsplit (x : l) = inr(x, l≤x, l>x)

  • Calculating the main call return value from the recursive calls return values.

qmerge: 1 + Z × ListZ × ListZ → ListZ qmerge inl(∗) = [] qmerge inr(x, l1, l2) = l1 + + (x : l2)

  • So why the does equation make sense as a definition?

Because qsplit sends a list to a container of strictly shorter lists. That the result type was ListZ and that the assembling function was qmerge did not play any role, we replace them with something else and the equation is still a definition.

7

slide-8
SLIDE 8

✬ ✫ ✩ ✪ Recursive coalgebras: definition

  • Def. Let F : C → C be a functor. A coalgebra-to-algebra morphism from an

F-coalgebra (A, α) to an F-algebra (C, ϕ) is a morphism f : A → C such that FA

F f

  • A

α

  • f
  • FC

ϕ

C

  • An F-coalgebra (A, α) is recursive (or algebra-initial) iff for every F-algebra

(C, ϕ) there exists a unique coalgebra-to-algebra morphism from (A, α) to (C, ϕ), denoted fixF,α(ϕ).

  • [An F-algebra (C, ϕ) is completely iterative (or coalgebra-final) iff for every

F-coalgebra (A, α) there exists a unique coalgebra-to-algebra morphism from (A, α) to (C, ϕ), denoted cofixF,ϕα.]

8

slide-9
SLIDE 9

✬ ✫ ✩ ✪

  • Cf. “hylomorphisms” from functional programming:

Let F : C → C be a functor with an initial algebra whose inverse is its final coalgebra (always the case if C is algebraically compact). Then for any F-coalgebra (A, α) and F-algebra (C, ϕ), there is a canonical (but not at all unique in general) coalgebra-to-algebra morphism: FA

F CoitF ( α )

  • A

α

  • CoitF ( α )
  • F(µF)

F ItF ( ϕ )

  • inF

µF

ItF ( ϕ )

  • in−1

F

  • FC

ϕ

C

9

slide-10
SLIDE 10

✬ ✫ ✩ ✪ Examples

  • Consider any functor F : C → C with an initial algebra, (µF, inF ).
  • The F-coalgebra (µF, in−1

F ) is recursive (iteration).

F(µF)

F f

  • µF

in−1

F

  • ∃!f
  • FC

∀ϕ

C

  • But also: The F(Id × KµF )-algebra (µF, F idµF , idµF ◦ in−1

F ) (primitive

recursion) F(µF × µF)

F (f×idµF )

  • F(µF)

F idµF ,idµF

  • µF

in−1

F

  • ∃!f
  • F(C × µF)

∀ϕ

C

10

slide-11
SLIDE 11

✬ ✫ ✩ ✪

  • But also: The F(Id × F)-coalgebra (µF, F idµF , in−1

F ◦ in−1 F ) (iteration back

  • ne or two steps).

F(µF × F(µF))

F (f×F f)

  • F(µF)

F idµF ,in−1

F

  • µF

in−1

F

  • ∃!f
  • F(C × FC)

∀ϕ

C

  • Etc.

11

slide-12
SLIDE 12

✬ ✫ ✩ ✪ Examples ctd.

  • The set ListZ is the initial algebra of the functor

LZ = K1 + KZ × Id : Set → Set.

  • The following are recursive coalgebras:
  • ListZ together with the analysis of every non-empty list into its head and tail is

a LZ-recursive coalgebra.

  • So is every suffix-closed subset of ListZ.
  • ListZ together with the analysis of every non-empty list into its smallest

element and the rest is also a recursive LZ-coalgebra.

  • ListZ together with the analysis of every non-empty, non-singleton list into two

halves is a recursive coalgebra of the functor BTZ = K1 + KZ + Id × Id. Etc.

12

slide-13
SLIDE 13

✬ ✫ ✩ ✪ Examples ctd.

  • A functor may well have recursive coalgebras without having an initial algebra:
  • Let P : Set → Set be the covariant powerset function.
  • A P-coalgebra (A, α) is a binary relation (A, ≺):

α(a) = {x ∈ A | x ≺ a} x ≺ a iff x ∈ α(a)

  • A P-coalgebra-to-algebra morphism from (A, α) to (C, ϕ) is a function

f : A → C such that f = ϕ ◦ Pf ◦ α i.e., such that, for any a ∈ A, f(a) = ϕ({f(x) | x ≺ a}) Such a morphism exists uniquely for any (C, ϕ) iff ≺ is wellfounded. So: (A, α) is recursive iff (A, ≺) is wellfounded.

  • But P has no initial algebra!

13

slide-14
SLIDE 14

✬ ✫ ✩ ✪ Recursive coalgebras: basic properties

  • Let F : C → C be a functor. If F has an initial algebra, we agree to denote it

(µF, inF ).

  • Prop. If F has an initial algebra, then (µF, in−1

F ) is a final recursive

F-coalgebra. F(µF)

F g

  • inF

µF

g

  • in−1

F

  • FC

ϕ

C FA

F f

  • A

α

  • f
  • F(µF)

inF

µF

in−1

F

  • 14
slide-15
SLIDE 15

✬ ✫ ✩ ✪

  • Prop. If (A, α) is a recursive F-coalgebra, then (FA, Fα) is also a recursive

F-coalgebra.

  • Prop. (a) If (A, α) is a recursive F-coalgebra and α is iso, then (A, α−1) is an

initial F-algebra. (b) If (A, α) is a final recursive F-coalgebra, then α is iso (both as a morphism and as a coalgebra morphism) (and hence (A, α−1) is an initial F-algebra). (a) FA

F f

  • α−1

A

f

  • α
  • FC

ϕ

C (b) FA

F α

  • A

α

  • α
  • F 2A

F h

  • FA

F α

  • h
  • FA

A

α

  • 15
slide-16
SLIDE 16

✬ ✫ ✩ ✪

  • Prop. Let C be cartesian and F : C → C a functor. If (A, α) is a recursive

F-coalgebra, then (A, F idA, α ◦ α) is a recursive F(Id × F)-coalgebra.

  • Construction of the recursive morphism:

F(A × FA)

F (f×F f)

  • F (g×F g)
  • FA

F id,α

  • F id,id
  • F g
  • A

α

  • g
  • f
  • F(A × A)

F (id×α)

  • F (g×g)
  • F((C × FC)

×F(C × FC))

F (fst×F fst)

  • F (id×ψ)

F((C × FC) ×(C × FC))

F (fst×snd)

  • F(C × FC)

F id,id

  • ψ

C × FC

fst

  • F(C × FC)

F(C × FC) F(C × FC)

ϕ

C

16

slide-17
SLIDE 17

✬ ✫ ✩ ✪ “Transposition properties”

  • Prop. (folklore) Let F, G : C → C be functors and τ : F

.

→ G a natural transformation. If an F-coalgebra (A, α) is recursive, then the G-coalgebra (A, τA ◦ α) is recursive.

  • Prop. (Eppendahl) Let F : C → D and G : D → C be functors.

If (A, α) is a recursive GF-coalgebra, then (FA, Fα) is a recursive FG-coalgebra.

  • Cor. Let F : C → C, G : D → D be functors, L : C → D a functor with a right

adjoint, and τ : LF

.

→ GL a natural transformation. If (A, α) is a recursive F-coalgebra, then (LA, τA ◦ Lα) is a recursive G-coalgebra.

17

slide-18
SLIDE 18

✬ ✫ ✩ ✪ Variation 1: Strongly recursive coalgebras

  • Def. Let C be cartesian and F : C → C a functor with a strength σ. An

F-coalgebra (A, ϕ) is strongly recursive (or recursive with parameters) iff, for any object Γ of C and F-algebra (C, ϕ), there is a unique morphism f : Γ × A → C satisfying F(Γ × A)

F f

  • Γ × FA

σΓ,A

  • Γ × A

idΓ×α

  • f
  • FC

ϕ

C i.e., iff, for any object Γ, the F-coalgebra (Γ × A, σΓ,A ◦ (idΓ × α)) is recursive.

  • [Cf. iteration with parameters.]
  • A strongly recursive F-coalgebra (A, α) is also a recursive F-coalgebra.
  • For the converse to hold, it is sufficient that C is cartesian closed.

18

slide-19
SLIDE 19

✬ ✫ ✩ ✪ Variation 2: parametrically recursive coalgebras

  • Def. Let C be cartesian and F : C → C a functor. An F-coalgebra (A, α) is

parametrically recursive iff, for any (KA × F)-algebra (C, ϕ), there is a unique morphism f : A → C satisfying A × FA

idA×F f

  • A

f

  • idA,α
  • A × FC

ϕ

C i.e., iff the (KA × F)-coalgebra (A, idA, α ) is recursive.

  • [Cf. primitive recursion.]
  • A parametrically recursive F-coalgebra (A, α) is necessarily recursive, but the

converse does not hold in general.

  • Adamek-Milius-Velebil completely iterative algebras are dual to this version of

the concept of recursive coalgebras.

19

slide-20
SLIDE 20

✬ ✫ ✩ ✪ Comonads, comonad algebras, distributive laws

  • Def. A comonad on a category C is a functor D : C → C together with natural

transformations ε : D

.

→ Id (counit) and δ : D

.

→ D2 (comultiplication) satisfying, for any object X, DX

δX

  • δX
  • D2X

εDX

  • D2X

DεX DX

DX

δX

  • δX D2X

δDX

  • D2X

DδX D3X 20

slide-21
SLIDE 21

✬ ✫ ✩ ✪

  • Def. A coalgebra of a comonad (D, ε, δ) on C is a coalgebra (A, ı) of the

functor D satisfying A

  • ı

DA

εA

  • A

A

ı

  • ı

DA

δA

  • DA

Dı D2A 21

slide-22
SLIDE 22

✬ ✫ ✩ ✪

  • Def. A distributive law of a functor F : C → C over a comonad (D, ε, δ) on C is

a natural transformation κ : FD

.

→ DF satisfying, for any object X, FDX

F εX

  • κX DFX

εF X

  • FX

FX FDX

F δX

  • κX

DFX

δF X

  • FD2X

κDX DFDX DκX D2FX 22

slide-23
SLIDE 23

✬ ✫ ✩ ✪ Recursive coalgebras from comonads

  • Thm. Let F : C → C be a functor, (A, α) a recursive F-coalgebra,

D = (D, ε, δ) a comonad on C and (A, ı) a D-coalgebra. If κ is a distributive law of F over D satisfying FA

F ı

  • A

ı

  • α
  • FDA

κA DFA

DA

  • then (A, Fı ◦ α) is a recursive FD-coalgebra (and, consequently, (A, Dα ◦ ı) is a

recursive DF-coalgebra). FDA

F Df

  • FA

F ı

  • A

α

  • ∃!f
  • FDC

∀ϕ

C

  • [Cf. bialgebras]

23

slide-24
SLIDE 24

✬ ✫ ✩ ✪

  • Construction of the recursive morphism:

FDA

F Df

  • F Dg
  • FA

F ı

  • F g
  • A

α

  • g
  • f
  • FD2C

F DεC

  • FDC

ψ

  • F δC
  • F δC

FD2C

F εDC

  • κDC DFDC

εF DC

DC

εC

  • FDC

FDC FDC FDC

ϕ

C (The commutation of the upper left square is a sublemma.)

24

slide-25
SLIDE 25

✬ ✫ ✩ ✪

  • Cor. Let F : C → C be a functor with an initial algebra and D = (D, ε, δ) a

comonad on C. If κ is a distributive law of F over D, then (µF, FItF ( DinF ◦ κµF ) ◦ in−1

F ) is a recursive FD-coalgebra.

F(µF)

F ı

  • inF

A

∃!ı

  • in−1

F

  • FD(µF)

κµF DF(µF) DinF

D(µF)

Din−1

F

  • FD(µF)

F Df

  • F(µF)

F ItF ( DinF ◦κµF )

  • µF

in−1

F

  • ∃!f
  • FDC

∀ϕ

C

  • This was the original generalized iteration of Uustalu-Vene-Pardo and of

Bartels (modulo duality).

  • Now instead of (µF, in−1

F ) we know we can use any recursive F-coalgebra. 25

slide-26
SLIDE 26

✬ ✫ ✩ ✪ Recursive coalgebras from (cofree comonads of) functors

  • For a functor H with a cofree comonad, write DH = (DH, εH, δH) for this

comonad and σH for the extraction of H from DH.

  • Thm. Let F : C → C be a functor, (A, α) a recursive F-coalgebra, H : C → C a

functor with a cofree comonad and (A, ) a H-coalgebra. Let (A, ¯ ) be the induced DH-coalgebra. If λ : FDH

.

→ HF is a nat. transf. satisfying FA

F ¯ 

  • A

  • α
  • FDHA

λA

HFA HA

  • then (A, F ¯

 ◦ α) is a recursive FDH-coalgebra (and, consequently, (A, Hα ◦ ) is a recursive HF-coalgebra).

  • Proof: From the previous theorem taking D = DH, ı = ¯

, κ = ¯ λ, where ¯ λ : FDH

.

→ DHF is easily produced from λ : FDH

.

→ HF.

26

slide-27
SLIDE 27

✬ ✫ ✩ ✪

  • Thm. Let F : C → C be a functor, (A, α) a recursive F-coalgebra, H : C → C a

functor with a cofree comonad and (A, ) a H-coalgebra. If λ′ : FH

.

→ HF is a natural transformation satisfying FA

F 

  • A

  • α
  • FHA

λ′

A HFA

HA

  • then (A, F ◦ α) is a recursive FH-coalgebra.
  • Proof: From the previous theorem defining λ : FDH

.

→ HF by λX = λ′

X ◦ FσX

  • The recursiveness of the F(Id × F)-coalgebra (A, F idA, α ◦ α) is now the

conclusion for H = Id × F  = idA, α λ′

X

= FfstX,F X, FsndX,F X : F(X × FX) → FX × F 2X

27

slide-28
SLIDE 28

✬ ✫ ✩ ✪ Conclusions and future work

  • Done: An elegant framework, a generalization of results known for initial

algebras and modularization of proofs. A serious use of distributive laws. All results dualize.

  • To do: Develop further methods for checking a coalgebra for recursiveness (e.g.,

generalize our main result along the lines of the most general constructions in Bartels’ work on structured corecursion for final coalgebras, modulo duality).

  • Relation between recursiveness and wellfoundedness (Paul Taylor’s work).
  • Do the results admit any type-theoretic versions?

28