(Co/Contra)Functor and Comonad

Mario Blažević mblazevic at stilo.com
Thu Dec 23 22:43:24 CET 2010

Why are Cofunctor and Comonad classes not a part of the base library?

I recently defined a data type (Control.Cofunctor.Ticker in 
monad-coroutine on Hackage) that happens to be a co-functor, or 
contra-functor if you prefer. In other words, it can implement the 
following function:

 > cofmap :: (a -> b) -> cf b -> cf a

I wanted to define a proper instance for it, but to my surprise I 
discovered that I couldn't. Not only is the class not defined in base, 
the only package I could find on Hackage that defines it is 
category-extras. This is a huge package I'd rather not have as a 
dependency, so I opted not to declare any instance.

Later on I found that this question has been raised before by *Conal 
Elliott*, nearly four years ago.


The result was the TypeCompose package, which presents a decent 
solution. I still can't think of any harm in having a proper class 
declaration in the base library: if you don't need it, you don't need to 
know it. But without the class declaration in the base library (or in 
some other obvious package) every other released library must either 
lack the instance declarations or declare the class itself and risk clashes.

This is not some obscure class you've never encountered, by the way: any 
"consumer" of data is a cofunctor. All regexp data types, for example, 
are cofunctor instances - or would be if there was a class to declare 
them instances of.

The same question extends to Comonad. I am not too familiar with this 
area so there may be fewer potential instances of the class, but I 
remember there was quite a bit of buzz around comonads a few years ago. 
Judging by a cursory Hayoo search, if anything the situation is worse 
than with Cofunctor: there are multiple incompatible declarations of the 
class scattered in various libraries that need it: ad, category-extras, 
data-category, rope. There is also evidence of undeclared Comonad 
instances: see the ListZipper and value-supply libraries.

So, is it time to add these two classes to the base library?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20101223/89244c70/attachment.htm>

More information about the Libraries mailing list