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

Henning Thielemann lemming at henning-thielemann.de
Wed Feb 17 11:02:23 UTC 2016

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.


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

More information about the Libraries mailing list