Petr Pudlák | 9 Apr 10:00 2013
Picon

correction of the documentation for Traversable

Dear haskellers,

I propose a correction to the documentation for Traversable. Currently it says:

  Minimal complete definition: traverse or sequenceA. 

This is not (completely) correct, it should be:

  Minimal complete definition:
    - traverse or
    - sequenceA with fmap.

What happened to me: I defined a Traversable instance with just sequenceA and used fmapDefault and foldMapDefault to define instances of Functor and Foldable. This resulted in an infinite loop. This is because traverse is defined using sequenceA and fmap, but fmapDefault is defined using traverse. So it's not enough to define sequenceA and use the default implementations for the rest, one also has to define fmap explicitly.

    With best regards,
    Petr Pudlak

_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Henning Thielemann | 9 Apr 10:54 2013
Picon

Re: correction of the documentation for Traversable


On Tue, 9 Apr 2013, Petr Pudlák wrote:

> Dear haskellers,
> 
> I propose a correction to the documentation for Traversable. Currently it says:
> 
>   Minimal complete definition: traverse or sequenceA. 
> 
> This is not (completely) correct, it should be:
> 
>   Minimal complete definition:
>     - traverse or
>     - sequenceA with fmap.
> 
> What happened to me: I defined a Traversable instance with just sequenceA and used fmapDefault and
> foldMapDefault to define instances of Functor and Foldable. This resulted in an infinite loop. This is
> because traverse is defined using sequenceA and fmap, but fmapDefault is defined using traverse. So it's not
> enough to define sequenceA and use the default implementations for the rest, one also has to define fmap
> explicitly.

That's right. I think I also stumbled about it, but became curious how 
fmap can be implemented in terms of only sequenceA. Then I saw that 
fmapDefault is implemented in terms of 'traverse', where the default 
implementation of 'traverse' calls fmap and sequenceA.

Thus, if I want to define Functor, Foldable and Traversable instances with 
minimal effort, then I implement only traversable.
_______________________________________________
Libraries mailing list
Libraries <at> haskell.org
http://www.haskell.org/mailman/listinfo/libraries
Ross Paterson | 9 Apr 14:43 2013
Picon

Re: correction of the documentation for Traversable

On Tue, Apr 09, 2013 at 10:00:56AM +0200, Petr Pudlák wrote:
> I propose a correction to the documentation for Traversable. Currently it says:
> 
>   Minimal complete definition: traverse or sequenceA. 
> 
> This is not (completely) correct, it should be:
> 
>   Minimal complete definition:
>     - traverse or
>     - sequenceA with fmap.
> 
> What happened to me: I defined a Traversable instance with just
> sequenceA and used fmapDefault and foldMapDefault to define instances
> of Functor and Foldable. This resulted in an infinite loop. This is
> because traverse is defined using sequenceA and fmap, but fmapDefault
> is defined using traverse. So it's not enough to define sequenceA and
> use the default implementations for the rest, one also has to define
> fmap explicitly.

I was about to say that the right place to say this would be the
documentation for fmapDefault, because "Minimal complete definition"
lists methods of the class being defined, and the real problem here
is in the Functor instance.  But I see it's already there:

-- | This function may be used as a value for `fmap` in a `Functor`
--   instance, provided that 'traverse' is defined. (Using
--   `fmapDefault` with a `Traversable` instance defined only by
--   'sequenceA' will result in infinite recursion.)
fmapDefault :: Traversable t => (a -> b) -> t a -> t b

Gmane