Recursive Coalgebras from Comonads
Total Page:16
File Type:pdf, Size:1020Kb
' $ 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 de¯ne functions using (general) recursion, but when is this justi¯ed, 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, de¯nition, 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 de¯nes quicksort recursively. qsort: ListZ ! ListZ qsort [] = [] qsort (x : l) = qsort(l·x) ++ (x : qsort(l>x)) ² Why does this recursive (a priori dubious) \de¯nition" actually qualify as a de¯nition, 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+id£qsort£qsort 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; l·x; 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 de¯nition? 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 de¯nition. & 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 co¯xF;'®.] & 8 % ' $ ² Cf. \hylomorphisms" from functional programming: Let F : C ! C be a functor with an initial algebra 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 9!f ² 8' ² F C / C ¡1 ² But also: The F (Id £ K¹F )-algebra (¹F; F h id¹F ; id¹F i ± inF ) (primitive recursion) ¡1 F h id¹F ;id¹F i in F (¹F £ ¹F ) o F (¹F ) o F ¹F F (f£id¹F ) 9!f ² 8' ² F (C £ ¹F ) / C & 10 % ' $ ¡1 ¡1 ² But also: The F (Id £ F )-coalgebra (¹F; F h id¹F ; inF i ± inF ) (iteration back one or two steps). ¡1 ¡1 F h id¹F ;in i in F (¹F £ F (¹F )) o F F (¹F ) o F ¹F F (f£F f) 9!f ² 8' ² 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 su±x-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) = fx 2 A j x Á ag x Á a i® x 2 ®(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 2 A, f(a) = '(ff(x) j x Á ag) 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 (g£F g) F (A £ A) F g g F (g£g) F (f£F 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 (fst£F fst) F (fst£snd) 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 su±cient 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 idA£F 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.