[Haskell-cafe] Why Maybe exists if there is Either?
John Lato
jwlato at gmail.com
Fri Jan 10 18:19:45 UTC 2014
Hi Vlatko,
Apologies for taking part in the de-railing of this topic.
I can't answer authoritatively, however I do have a challenge:
Try to implement a module that exposes the same interface as Data.Maybe but
defines 'newtype Maybe a = Maybe {unMaybe :: Either () a}'. We're
targeting base as of several years ago, so you can't use any packages
outside base or any GHC extensions. In particular, you can't use
GeneralizedNewtypeDeriving or any of the 'Derive*' extensions. See how
much repetition you're able to avoid.
John L.
On Fri, Jan 10, 2014 at 2:12 AM, Vlatko Basic <vlatko.basic at gmail.com>wrote:
> Hi Patrick,
>
> Due to my poor wording of the question (and the choice of String, instead
> of unit or Void, for Left), this discussion went the wrong way. Completely.
> I also noticed that some of my comments went in the "usage" direction.
>
> My intent was to ask the question about implementation of Maybe, *not it's
> usage*. Seems that most people understood that I'm arguing whether the
> Maybe is needed and/or should it be switched with Either. I'm not.
>
> However, what I was trying to find out were the reasons for the way it is
> *implemented*. When I look at the code for Maybe and Either, it seems to me
> like the violation of the "do not repeat yourself" principle. That
> principle is taken rather seriously in Haskell. Even HLint has "Reduce
> duplication" suggestion. *Some* of the code I looked at appears the same.
>
> I wanted to find out what were the reasons/restrictions/functionalities
> why Maybe wasn't (somehow) built on Either, so there will be less code
> duplication.
>
> The first reason would be that there are no data constructor synonyms, so
> it will not be elegant (although, that might be solved with
> PatternSynonyms, as Ben Gamari suggested).
>
> The other reason, that probably couldn't be solved, was pointed out by
> Nickolay Kudasov, and that is the generic functions like Show (for Left
> String) and probably more, like Read.
>
>
> vlatko
>
> -------- Original Message --------
> Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either?
> From: Patrick Hurst <lightquake at amateurtopologist.com><lightquake at amateurtopologist.com>
> To: vlatko.basic at gmail.com
> Cc: "haskell-cafe at haskell.org" <haskell-cafe at haskell.org>
> <haskell-cafe at haskell.org> <haskell-cafe at haskell.org>
> Date: 09.01.2014 21:57
>
>
> Why have Bool? Just let true = 1, false = 0, (||) = (+), (&&) = (*).
>
> Why have Ordering? Just use Integer and let lt = -1, eq = 0, gt = 1.
>
> Why have three-tuples (a, b, c)? Just use ((a, b), c).
>
> Why have Data.Map a b? Just use a -> Maybe b. You don't even need an Ord
> constraint any more!
>
> Why have Data.Set a? Just use a -> Bool (or, a -> Integer).
>
> For that matter, why use algebraic data types? data Person = Person
> String Int is isomorphic to type Person = (String, Int).
>
>
>
> On Thu, Jan 9, 2014 at 9:50 AM, Vlatko Basic <vlatko.basic at gmail.com>wrote:
>
>> Hello Cafe,
>>
>> With my current knowledge of Haskell, I do not see why is there Maybe if
>> we have Either.
>>
>> For example, Functor and Monad instances (and many others) of Maybe and
>> Either are the same (except for fail).
>>
>> In other words, this should hold:
>>
>> Maybe a = Either String a -- String or something else
>> Nothing = Left ""
>> Just a = Right a
>>
>>
>> I'm curious to find out what was the reasoning to make Maybe?
>> What is the added value with introducing it?
>> In which situations the above substitution does not hold?
>>
>>
>>
>> Best regards,
>>
>> vlatko
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140110/eb39908a/attachment.html>
More information about the Haskell-Cafe
mailing list