Proposal: Add Foldable and Traversable instances for ((,) a)

Yitzchak Gale gale at sefer.org
Tue Jun 23 10:25:58 EDT 2009


I wrote:
>> ...fmapDefault and foldMapDefault...
>> cannot be used as methods in superclasses,
>> since the superclasses must already exist before
>> these functions can be defined.

Ross Paterson wrote:
> The intention is that if you only want to define Traversable, you can
> fill in the required superclass instances by defining
>
>  instance Functor F where
>    fmap = fmapDefault
>
>  instance Foldable F where
>    foldMap = foldMapDefault
>
>  instance Traversable F where
>    traverse = ...
>
> That should work fine.

Cool, I didn't know that works. On the other hand,
in general, that mechanism sounds a bit
dangerous. It could lead to infinite recursion
that would be very difficult to debug in the case
of a complex class dependency graph, couldn't it?

> However the fmap definition won't work if you defined
> sequenceA instead of traverse, so the documentation
> of fmapDefault needs to be corrected to say that.

Case in point.

OK, how about this:

In the documentation for class Traversable, change
(`fmapDefault`) to (see `fmapDefault`), similarly
for foldMapDefault.

Documentation for fmapDefault:

-- | This function should be equivalent to `fmap` in
-- the `Functor` superclass instance. If you do not
-- already have a `Functor` instance, you can use
-- this function to define one:
--
-- > instance Functor T where
-- >   fmap = fmapDefault
--
-- Note, however, that this will lead to infinite
-- recursion if you did not provide an explicit
-- implementation of the `traverse` method.

Documentation for foldMapDefault:

-- | This function should be equivalent to `foldMap` in
-- the `Foldable` superclass instance. If you do not
-- already have a `Foldable` instance, you
-- can use this function to define one:
--
-- > instance Functor T where
-- >   fmap = fmapDefault

Thanks,
Yitz


More information about the Libraries mailing list