✬ ✩ 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 (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
✬ ✩ • 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 final coalgebras) to deal with guarded-by-constructors corecursion. ✫ ✪ 3
✬ ✩ 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
✬ ✩ Recursive coalgebras: motivation • Think of quicksort: Let Z be a set linearly ordered by ≤ . One usually defines quicksort recursively. qsort : List Z → List Z 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 ( List Z, List Z ) → Set ( List Z, List Z ). 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 ◦ BT qsort ◦ qsplit where BT Z X = 1 + Z × X × X , i.e. qsplit 1 + Z × List Z × List Z List Z id + id × qsort × qsort qsort qmerge � List Z 1 + Z × List Z × List Z ✫ ✪ 6
✬ ✩ • Preparation of argument values for recursive calls: qsplit : List Z → 1 + Z × List Z × List Z 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 × List Z × List Z → List Z qmerge inl ( ∗ ) = [] qmerge inr ( � x, l 1 , l 2 � ) = l 1 + + ( x : l 2 ) • 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 List Z 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
� � � ✬ ✩ 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 A F f f ϕ � C FC • 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 fix F,α ( ϕ ). • [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 cofix F,ϕ α .] ✫ ✪ 8
� � � � � � ✬ ✩ • 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 A F Coit F ( α ) Coit F ( α ) in F � µF F ( µF ) in − 1 F F It F ( ϕ ) It F ( ϕ ) ϕ � C FC ✫ ✪ 9
� � � � � � � ✬ ✩ Examples • Consider any functor F : C → C with an initial algebra, ( µF, in F ). • The F -coalgebra ( µF, in − 1 F ) is recursive (iteration). in − 1 F F ( µF ) µF ∃ ! f F f ∀ ϕ � C FC • But also: The F ( Id × K µF )-algebra ( µF, F � id µF , id µF � ◦ in − 1 F ) (primitive recursion) in − 1 F � id µF , id µF � F F ( µF × µF ) F ( µF ) µF F ( f × id µF ) ∃ ! f ∀ ϕ � C F ( C × µF ) ✫ ✪ 10
� � � � ✬ ✩ • But also: The F ( Id × F )-coalgebra ( µF, F � id µF , in − 1 F � ◦ in − 1 F ) (iteration back one or two steps). F � id µF , in − 1 in − 1 � F F F ( µF × F ( µF )) F ( µF ) µF F ( f × F f ) ∃ ! f ∀ ϕ � C F ( C × FC ) • Etc. ✫ ✪ 11
✬ ✩ Examples ctd. • The set List Z is the initial algebra of the functor L Z = K 1 + K Z × Id : Set → Set . • The following are recursive coalgebras: • List Z together with the analysis of every non-empty list into its head and tail is a L Z -recursive coalgebra. • So is every suffix-closed subset of List Z . • List Z together with the analysis of every non-empty list into its smallest element and the rest is also a recursive L Z -coalgebra. • List Z together with the analysis of every non-empty, non-singleton list into two halves is a recursive coalgebra of the functor BT Z = K 1 + K Z + 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 iff x ∈ α ( a ) • A P -coalgebra-to-algebra morphism from ( A, α ) to ( C, ϕ ) is a function f : A → C such that f = ϕ ◦ P f ◦ α 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
� � � � � � � ✬ ✩ Recursive coalgebras: basic properties • Let F : C → C be a functor. If F has an initial algebra, we agree to denote it ( µF, in F ). • Prop. If F has an initial algebra, then ( µF, in − 1 F ) is a final recursive F -coalgebra. in F � µF α F ( µF ) FA A in − 1 F g F g F f f in F � µF ϕ � C F ( µF ) FC in − 1 F ✫ ✪ 14
� � � � � � � � � � ✬ ✩ • 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). α − 1 � A α ( a ) ( b ) FA FA A α F f f α F α ϕ � C F α F 2 A FC FA F h h α FA A ✫ ✪ 15
� � � � � � � � � � � � ✬ ✩ • Prop. Let C be cartesian and F : C → C a functor. If ( A, α ) is a recursive F -coalgebra, then ( A, F � id A , α � ◦ α ) is a recursive F ( Id × F )-coalgebra. • Construction of the recursive morphism: F � id ,α � α F ( A × FA ) FA A � �������������� � �������������� F ( id × α ) F � id , id � F ( A × A ) g F ( g × F g ) F g F ( g × g ) F ( f × F f ) f F ( id × ψ ) � F � id , id � ψ F (( C × FC ) F (( C × FC ) � C × FC F ( C × FC ) × F ( C × FC )) × ( C × FC )) F ( fst × F fst ) F ( fst × snd ) fst ϕ � C F ( C × FC ) F ( C × FC ) F ( C × FC ) ✫ ✪ 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 ( 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
� � � � ✬ ✩ 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 σ Γ ,A id Γ × α F (Γ × A ) Γ × FA Γ × A F f f ϕ � C FC 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
Recommend
More recommend