Is there a reason cofree comonads do not provide a fold?

This is the standard cofree comonad package. It provides all sorts of unfolds, but no folds. Why?

  • Its opposite free monad provides both.
  • Theoretically, Cofree a f is a fixed point of Compose ((, ) a) f, and a fixed point of any functor supports both folds and unfolds out of the box.

Am I missing something?

Cofree comonads are coinductive, and in a general setting won’t necessarily have folds (because they’re nonterminating). In Haskell and similar languages, inductive and coinductive types coincide, so we don’t always notice this. The old Jacobs/Rutten tutorial might be of interest to you: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.37.1418

1 Like

So what you are saying is that a fold is not provided because it would not necessarily be terminating, right? But an unfold may also easily be non-terminating, may it not? And can we not have productive folds, given laziness? Either I am missing something or this is not an applicable reason.

A very nice tutorial. I added it to my reading list. But it is also quite long, and at this time I cannot give it the time and attention it requires. Is there some specific section I should read before proceeding with the present inquiry?

Cofree has a Foldable f => Foldable (Cofree f) instance, and has instances for the appropriate recursion-schemes classes if you need more power.

1 Like

Thanks Chris. I overlooked the support for recursion schemes. I had some worries about rolling out my own folds in production, but now I see that my fears are unfounded and from the practical standpoint these free comonads are production ready.