DeriveFoldable treatment of tuples is surprising

Ryan Yates fryguybob at gmail.com
Wed Mar 22 12:50:16 UTC 2017


On Wed, Mar 22, 2017 at 4:12 AM, Sven Panne <svenpanne at gmail.com> wrote:

> 2017-03-21 22:29 GMT+01:00 Edward Kmett <ekmett at gmail.com>:
>
>> [... In general I think the current behavior is the least surprising as
>> it "walks all the a's it can" and is the only definition compatible with
>> further extension with Traversable. [...]
>>
>
> OTOH, the current behavior contradicts my intuition that wrapping a type
> into data/newtype plus using the deriving machinery is basically a no-op
> (modulo bottoms etc.). When I e.g. wrap a type t, I would be very surprised
> if the Eq/Ord instances of the wrapped type would behave differently than
> the one on t. I know that this is very handwavy argument, but I think the
> current behavior is *very* surprising.
>
> Somehow the current behavior seems to be incompatible with the FTP, where
> pairs are given a special treatment (if that't the right/intuitive choice
> is a completely different topic, though).
>

I'm not sure what you mean by "pairs are given a special treatment".
Tuples are given the only possible treatment:

data (,) a b = (a,b)

The b is the only place to fold over with a Foldable or change with a
Functor instance.  When things are monomorphic there are more options and
that leads to the least surprising, fold over all the options for:

data Pair a = Pair a a

or

data X a = X (a,a)

The (a,a) here is most certainly not the same thing as (a,b).  There is
something that is a bit surprising to me in that DerivingFoldable will not
a user declared data type for pair with two arguments:

> data Pair a = Pair a a deriving (Functor, Foldable, Show)
> data X a = X (Pair a) deriving (Functor, Foldable, Show)
> length (X (Pair 1 2))
2
> data Tup a b  = Tup a b deriving (Functor, Foldable, Show)
> data Y a = Y (Tup a a) deriving (Functor, Show)

<interactive>:10:34: error:
    • Can't make a derived instance of ‘Functor Y’:
        Constructor ‘Y’ must use the type variable only as the last
argument of a data type
    • In the data declaration for ‘Y’
> data Y a = Y (Tup a a) deriving (Foldable, Show)

<interactive>:11:34: error:
    • Can't make a derived instance of ‘Foldable Y’:
        Constructor ‘Y’ must use the type variable only as the last
argument of a data type
    • In the data declaration for ‘Y’
> data Y a = Y (Tup a a) deriving (Foldable, Show)

But it is happy to do just that with (a,a).


Ryan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20170322/6f109936/attachment.html>


More information about the ghc-devs mailing list