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

amindfv at mailbox.org amindfv at mailbox.org
Thu Nov 26 00:23:20 UTC 2020


On Wed, Nov 25, 2020 at 06:13:10PM -0500, Viktor Dukhovni wrote:
> On Wed, Nov 25, 2020 at 04:15:18PM -0500, amindfv--- via Haskell-Cafe wrote:
> 
> > > 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,
> 
> Yes, the last type variable gets the "for free" functor instance.
> 
> > but it's not hard to imagine a Haskell where [...]
> > 
> > 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).
> 
> The intent is clear, but ultimately not as useful as one might wish,
> since when it comes down to evaluating terms:
> 
>     fmap f (x, y)
> 
> at most one such instance can match, since the terms don't carry any
> information about whether you want to fmap on the left or right.  So
> all you'd get to do is flip the bias from right to left.
> 

Not sure if we're agreeing or not :-), but this is exactly my point:
The fact that (fmap f (x, y)) == (Control.Arrow.second f (x, y)) seems to only be based, essentially, on the historical accident that in Haskell the last type variable is special w.r.t. typeclass instances.

It's not hard to imagine, in the absence of these instances, Haskell loosening the restriction on creating these types of instances. The restriction is certainly not innate to mathematics or type theory.

My position is against having most of these instances in base, not for switching the bias from right to left.

Tom



More information about the Haskell-Cafe mailing list