Haskell Foldable Wats (Was: Add conspicuously missing Functor instances for tuples)
Bart Massey
bart.massey at gmail.com
Wed Feb 17 17:34:46 UTC 2016
As a less-capable Haskeller, I agree 100%.
The whole point of importing Data.List (either directly or from the
Prelude) is to get functions that work on lists. A crucial feature of this
is that applying these functions to non-lists should generate type errors,
so that I can notice that I passed e.g. a tuple where I meant to pass a
list. I would be fine with adding Foldable versions of list functions to
Data.Foldable: if I want my functions to work on arbitrary Foldables,
that's what I would import.
As someone who spends some time teaching beginners Haskell, this is a
disaster. Trying to explain to a beginner why "Data.List.length ('x', 'y')"
returns a number instead of failing is bad enough: trying to explain why it
returns 1 is hopeless. (I'm better off with Python: at least there it has
the grace to return 2.) And then I have to explain why "length ('x', 'y',
'z')" returns an utterly incomprehensible type error? No thanks.
I'm recommending SML over Haskell to beginners a lot more these days.
On Wed, Feb 17, 2016 at 3:02 AM Henning Thielemann <
lemming at henning-thielemann.de> wrote:
>
> 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/20160217/5011e4d2/attachment.html>
More information about the Libraries
mailing list