[Haskell-cafe] Textbook example of instance Foldable ((,)a)

amindfv at mailbox.org amindfv at mailbox.org
Wed Nov 25 21:15:18 UTC 2020


On Wed, Nov 25, 2020 at 03:16:22PM -0500, Viktor Dukhovni wrote:
> On Wed, Nov 25, 2020 at 07:56:49PM +0000, Barak A. Pearlmutter wrote:
> 
> > True. And their use in Arrow is a great example of pairs that do *not*
> > work well as instances of Foldable as currently defined. Arrow is
> > using pairs as abstract Cartesian products, and the natural
> > isomorphism A×B≅B×A is used implicitly all the time. The Foldable
> > definition for pairs breaks that symmetry inappropriately.
> 
> The "symmetry breaking" is not a result of the perhaps regrettable
> Foldable instance, rather it is a basic consequence of the fact that
> given:
> 
>     data Foo a b = Foo a b
> 
> we immediately get a Functor "Foo a :: * -> *" for each a, of which
> "(,) a" is but one example.  I find nothing objectionable in the Functor
> instance "(,) a" or the Bifunctor instance "(,)".  It is only the
> Traversable and Foldable instances of "(,) a" that have the noted
> unexpected behaviour.

This is almost incidental, though, right? I.e. it's just a result of typeclass "currying" syntax, but it's not hard to imagine a Haskell where similar to how

    foo :: a -> b -> c

gives you both:

    foo a :: b -> c

i.e.

    (\b -> foo a b) :: b -> c

and

    (\a -> foo a b) :: a -> c

we're also allowed to write something like:

    instance Functor (\a -> Foo a b)
    instance Functor (\a -> (a, b))

Mirroring the current (implicit)

    instance Functor (\b -> Foo a b)
    instance Functor (\b -> (a, b))

If that were the case, the instances favoring the last paramater wouldn't seem natural at all, and we'd be sensibly asking on what basis "(+1) <$> (2, 4)" equals (2, 5) but not (3, 4).

Tom



More information about the Haskell-Cafe mailing list