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

Viktor Dukhovni ietf-dane at dukhovni.org
Thu Nov 26 04:24:41 UTC 2020


On Wed, Nov 25, 2020 at 07:23:20PM -0500, amindfv--- via Haskell-Cafe wrote:

> > 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:

We're not disagreeing. :-)

> 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.

Yes, basically, which makes it easy to define Functor and related
instances for the partially applied two parameter type constructor:

    Foo a :: * -> *

and these are somewhat useful, since one then gets to embelish
a general thing with some additional data:
    
        a -> (decorated, a)

and adding the decoration on the left comes with a built-in functor
instance, which, though the left/right choice is a somewhat (but not
entirely) arbitrary choice, does no harm.  No surprising behaviour
arises from the Functor/Applicative/Monad instances for 2-tuples.

Indeed, you probably find it less surprising that with "Either a b" and
"Left a" as the conventional "exception" slot, the Functor instance
transforms "Right b".  The overall approach is consistent in its right
bias, made more convenient through currying of multi-parameter type
constructors.

AFAICT, it is really just the Foldable instance that can lead to
unexpected and behaviour and unnoticed bugs.

> 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.

Type inference would get more complicated if it would have to consider
all possible slots in each term when searching for the instance head.

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

So you'd like to see a 2-tuple with fewer built-in instances, I'm
sympathetic to the case for not having Traversable/Foldable for
2-tuples, but I don't see a compelling for not having Functor/Monad.

-- 
    Viktor.


More information about the Haskell-Cafe mailing list