' $

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 dene functions using (general) , but when is this justied, 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/nal coalgebras: Lenisa (et al.), Uustalu-Vene-Pardo, Bartels.

& 2 % ' $

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 opposition to nal coalgebras) to deal with guarded-by-constructors corecursion.

& 3 % ' $

Outline

Recursive coalgebras: motivation, denition, examples Basic properties Two variations: strongly recursive coalgebras, parametrically recursive coalgebras. Recursive coalgebras from comonads Recursive coalgebras from (cofree comonads of) functors

& 4 % ' $ Recursive coalgebras: motivation

Think of quicksort: Let Z be a set linearly ordered by . One usually denes quicksort recursively.

qsort: ListZ → ListZ qsort [] = []

qsort (x : l) = qsort(lx) ++ (x : qsort(l>x))

Why does this recursive (a priori dubious) “denition” actually qualify as a denition, 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 % ' $

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.

qsplit 1 + Z ListZ ListZ o ListZ

id+idqsortqsort qsort  qmerge  1 + Z ListZ ListZ / ListZ

& 6 % ' $ Preparation of argument values for recursive calls:

qsplit: ListZ → 1 + Z ListZ ListZ qsplit [] = inl()

qsplit (x : l) = inr(hx, lx, l>xi)

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

qmerge: 1 + Z ListZ ListZ → ListZ qmerge inl() = []

qmerge inr(hx, l1, l2i) = l1 ++ (x : l2)

So why the does equation make sense as a denition? 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 denition.

& 7 % ' $

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

F A o A

F f f  ϕ  F C / C

An F -coalgebra (A, ) is recursive (or algebra-initial) i for every F -algebra (C, ϕ) there exists a unique coalgebra-to-algebra morphism from (A, ) to

(C, ϕ), denoted xF,(ϕ). [An F -algebra (C, ϕ) is completely iterative (or coalgebra-nal) i for every F -coalgebra (A, ) there exists a unique coalgebra-to-algebra morphism from

(A, ) to (C, ϕ), denoted coxF,ϕ.]

& 8 % ' $

Cf. “hylomorphisms” from : Let F : C → C be a functor with an whose inverse is its nal 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:

F A o A

F CoitF ( ) CoitF ( )  inF  *  F (F ) m F 1 inF F ItF ( ϕ ) ItF ( ϕ )  ϕ  F C / C

& 9 % ' $ Examples

Consider any functor F : C → C with an initial algebra, (F, inF ).

1 The F -coalgebra (F, inF ) is recursive (iteration).

1 in F (F ) o F F

F f ∃!f  ∀ϕ  F C / C

1 But also: The F (Id KF )-algebra (F, F h idF , idF i inF ) (primitive recursion)

1 F h idF ,idF i in F (F F ) o F (F ) o F F

F (fidF ) ∃!f  ∀ϕ  F (C F ) / C

& 10 % ' $

1 1 But also: The F (Id F )-coalgebra (F, F h idF , inF i inF ) (iteration back one or two steps).

1 1 F h idF ,in i in F (F F (F )) o F F (F ) o F F

F (fF f) ∃!f  ∀ϕ  F (C F C) / C

Etc.

& 11 % ' $

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 sux-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 % ' $ 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 i 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, ϕ) i is wellfounded. So: (A, ) is recursive i (A, ) is wellfounded. But P has no initial algebra!

& 13 % ' $

Recursive coalgebras: basic properties

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

(F, inF ).

1 Prop. If F has an initial algebra, then (F, inF ) is a nal recursive F -coalgebra.

inF * o F (F ) m F F A A 1 in F g F g F f f    inF  ϕ / * F C C F (F ) m F 1 inF

& 14 % ' $

Prop. If (A, ) is a recursive F -coalgebra, then (F A, 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 nal recursive F -coalgebra, then is iso (both as a morphism and as a coalgebra morphism) (and hence (A, 1) is an initial F -algebra).

1 ) o (a) F A k A (b) F A o A F f f F  ϕ   F  F C / C F 2A o F A

F h h   F A o A

& 15 % ' $

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

F -coalgebra, then (A, F h idA, i ) is a recursive F (Id F )-coalgebra. Construction of the recursive morphism:

F h id, i F (A F A) o F A o A Si SS mmm SSSF (id) F h id,id i mmm SSS mm SSS mmm SS mv mm F (gF g) F (A A) F g g

F (gg) F (fF f)   f F id F id,id   F ((C F C) ( )/ F ((C F C) ho i / F (C F C)) (C F C)) F (C F C) C F C

F (fstF fst) F (fstsnd) fst

"   ϕ  { F (C F C) F (C F C) F (C F C) / C

& 16 % ' $

“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 (F A, F ) is a recursive F G-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 % ' $ 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) i, for any object of C and F -algebra (C, ϕ), there is a unique morphism f : A → C satisfying

,A id F ( A) o F A o A

F f f  ϕ  F C / C

i.e., i, 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 sucient that C is cartesian closed.

& 18 % ' $ Variation 2: parametrically recursive coalgebras

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

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

h idA, i A F A o A

idAF f f  ϕ  A F C / C

i.e., i the (KA F )-coalgebra (A, h idA, i) 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 % ' $

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,

X X DX / D2X DX / D2X HHH HHHH HHH ε X HHHH DX X DX  HHHH    DεX   DX  D2X / DX D2X / D3X

& 20 % ' $

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

ı / ı / A CC DA A DA CCCC CCC ε CCC A ı A CCCC   Dı  A DA / D2A

& 21 % ' $

Def. A distributive law of a functor F : C → C over a comonad (D, ε, ) on C is . a natural transformation : F D → DF satisfying, for any object X,

X X F DX / DF X F DX / DF X

F εX εF X F X F X        DX DX  F X F X F D2X / DF DX / D2F X

& 22 % ' $ 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

F A o A

F ı ı

 A D  F DA / DF A o DA then (A, F ı ) is a recursive F D-coalgebra (and, consequently, (A, D ı) is a recursive DF -coalgebra).

F ı F DA o F A o A

F Df ∃!f  ∀ϕ  F DC / C

[Cf. bialgebras]

& 23 % ' $

Construction of the recursive morphism:

F ı F DA o F A o A

F Dg F g g   *  F Df F D2C o F DC / F D2C / DF DC / DC f F C F C DC Dϕ

F DεC F εDC εF DC εC     ϕ  Ò F DC F DC F DC F DC / C

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

& 24 % ' 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 1 (F, F ItF ( DinF F ) inF ) is a recursive F D-coalgebra.

1 in p F F (F ) p 1 A inF F ı ∃!ı 1 Din  F  F p F D(F ) / DF (F ) 1 D(F ) DinF

1 F ItF ( DinF F ) in F D(F ) o F (F ) o F F

F Df ∃!f  ∀ϕ  F DC / C This was the original generalized iteration of Uustalu-Vene-Pardo and of Bartels (modulo duality).

1 Now instead of (F, inF ) we know we can use any recursive F -coalgebra.

& 25 % ' $ 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 : F DH → HF is a nat. transf. satisfying F A o A

F     A H  F DH A / HF A o HA then (A, F  ) is a recursive F DH -coalgebra (and, consequently, (A, H ) is a recursive HF -coalgebra). Proof: From the previous theorem taking D = DH , ı = , = , where . . : F DH → DH F is easily produced from : F DH → HF .

& 26 % ' 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 0 : F H → HF is a natural transformation satisfying

F A o A

F   0  A H  F HA / HF A o HA then (A, F  ) is a recursive F H-coalgebra. . Proof: From the previous theorem dening : F DH → HF by

0 X = X F X

The recursiveness of the F (Id F )-coalgebra (A, F h idA, i ) is now the conclusion for

H = Id F

 = h idA, i 0 2 X = h F fstX,F X , F sndX,F X i : F (X F X) → F X F X

& 27 % ' $

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 nal coalgebras, modulo duality). Relation between recursiveness and wellfoundedness (Paul Taylor’s work). Do the results admit any type-theoretic versions?

& 28 %