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@amateurtopologist.com>
To: vlatko.basic@gmail.com
Cc: "haskell-cafe@haskell.org" <haskell-cafe@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@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@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe