Haskell Foldable Wats (Was: Add conspicuously missing Functor instances for tuples)

Fumiaki Kinoshita fumiexcel at gmail.com
Wed Feb 24 11:08:39 UTC 2016


Thinking tuples of as multi-element containers is not recommended. A tuple
(a, b) is, a pair of one 'a' and one 'b'; as Foldable works on values
pointed by the rightmost type argument, 1 should be the only reasonable
result of 'length'.

    data TwoThree a b = TwoThree a a b b b

What should 'length (TwoThree "Foo" "Bar" 0 1 2)' be? Looking at only the
expression, 5 might seem to make sense, but is not meaningful considering
the type.

2016-02-17 20:02 GMT+09:00 Henning Thielemann <lemming at henning-thielemann.de
>:

>
> On Mon, 18 Jan 2016, Ryan Scott wrote:
>
> * The Not-A-Wat in Haskell:
>> https://www.youtube.com/watch?v=87re_yIQMDw
>>
>
> I see his examples and draw the opposite conclusions. What he presents are
> perfect Wats and they have eventually moved Haskell to the MatLab league
> where everything is allowed and the programming system accepts almost
> everything the programmer enters.
>
> Sure,
>
> length (2,3) = 1
>> product (2,3) = 3
>> sum (2,3) = 3
>> or (True,False) = False
>>
>
> are all consistent but consistently useless, unintuitive (not only to
> novices) and dangerous. There are alternatives: There was no need to
> generalize 'Prelude.length' using Foldable. I always opposed to the
> argument "put the most general variant to Prelude", because there is no
> clear most general variant or there is one like "length :: Length f => f"
> and you won't like it.
>
> We could reasonably have the Haskell 98 class
>
>   class Length a where
>      length :: a -> Int
>
>   instance Length [a] where
>      length = List.length
>
>   instance Length (a,b) where
>      length _ = 2
>
> This would yield the intuitive
>   length (2,3) = 2
>
> I do not propose to implement this class, because I never encountered a
> situation where I could equally choose between lists and pairs. If at all,
> I can see value in a special TupleSize class. However, the Length class
> proves that the suggestion that the only reasonable result of 'length
> (2,3)' is 1, is plain wrong.
>
> How did we get there? There were three steps that made this Wat possible:
>   1. Foldable.length added
>   2. instance Foldable ((,) a)
>   3. export Foldable.length from Prelude.
>
> For me
>   1. was correct
>   2. was wrong because a programmer should better define a custom type
>      like "data AdornedSingleton a b = AS a b"
>   3. Was wrong because there are multiple ways to generalize 'length'.
>      Without 3. you would have to use explicitly 'length' from Foldable
>      and this way you would have been warned, that strange things may
> happen.
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/libraries/attachments/20160224/006a2f2f/attachment-0001.html>


More information about the Libraries mailing list