Why Maybe exists if there is Either?

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

Hi Vlatko, to keep programs clear, short and simple. What makes you choose String as type to replace Nothing? Best, Johannes On 09/01/2014 14:50, Vlatko Basic 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

Hi Johannes,
I thought there was some "more important" reason than convenience, so I asked. :-O)
I put String because I'm currently thinking about error handling, and Left
String is the usual way of reporting failure, and I see Maybe as a type for
reporting errors, failures and similar.
Somehow it looks to me that famous "8 ways to report errors in Haskell" could be
shortened by one if Maybe is replaced with Either (with appropriate synonyms, of
course).
vlatko
-------- Original Message --------
Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either?
From: Johannes Erber
Hi Vlatko,
to keep programs clear, short and simple. What makes you choose String as type to replace Nothing?
Best, Johannes
On 09/01/2014 14:50, Vlatko Basic 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

On Thu, Jan 9, 2014 at 10:26 AM, Vlatko Basic
I put String because I'm currently thinking about error handling, and Left String is the usual way of reporting failure, and I see Maybe as a type for reporting errors, failures and similar.
Actually, the fact that all you can convey is "something failed" makes Maybe not a good error reporting type. And this is fine; there is still the "no value" niche (Perl's undefined, SQL's NULL, etc.) --- and the evidence from C's NULL that an *out of band* representation is often a very good idea (and from IEEE754's NaN that multiple out of band values is often a very bad idea). Also, a point that seems to often be missed in considering what is "more important": ultimately, what is *most* important is letting the programmer do what they need to do with a minimum of fuss or extra work. Predefining very useful stuff like Maybe which could be reinvented on the fly based on Either is about minimizing even the trivial extra work. Pedagogy is only rarely a useful design goal. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Thu, Jan 9, 2014 at 11:25 AM, Vlatko Basic
... is letting the programmer do what they need to do with a minimum of fuss or extra work ... But doesn't the need of mixing Maybe and Either cause more work for the programmer? Programmer of library, or programmer using the library?
Arguably if you need to switch from Maybe to Either then you did not think out your program sufficiently beforehand. And if you did think it out, what exactly is the problem with using appropriate data types in appropriate places? -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Thu, Jan 9, 2014 at 11:52 AM, Vlatko Basic
... what exactly is the problem with using appropriate data types in appropriate places ... It is a good thing. Very good. But I do not think all library writers are using it in correct places. But that's another problem.
In Haskell it's even a solved one: use an appropriate typeclass. We have several error-like typeclasses. Admittedly, if some library does not take advantage of this your recourse may be to submit a patch. And you still seem to be fixated on the notion that Maybe is only for errors. What exactly is the error in "user did not provide an optional parameter"? -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Thu, Jan 9, 2014 at 12:12 PM, Brandon Allbery
On Thu, Jan 9, 2014 at 11:52 AM, Vlatko Basic
wrote: ... what exactly is the problem with using appropriate data types in appropriate places ... It is a good thing. Very good. But I do not think all library writers are using it in correct places. But that's another problem.
In Haskell it's even a solved one: use an appropriate typeclass. We have several error-like typeclasses. Admittedly, if some library does not take advantage of this your recourse may be to submit a patch.
And you still seem to be fixated on the notion that Maybe is only for errors. What exactly is the error in "user did not provide an optional parameter"?
Indeed. I use Maybe for non-errors, namely optional values, all the time. To compare with some other languages: Maybe is like a nil vs. non-nil pointer. Either is like returning two values/a tuple of Foo and Error. (For that matter, I sometimes use Either when a value can be either something or the other, but neither of them are errors.)

On 09 Jan 2014, at 20:52, Vlatko Basic
wrote: Hi Brandon,
... you did not think out your program sufficiently beforehand ... Yes, that is quite possible. I consider myself an FP beginner still. I encountered that mixing Maybe and Either problem few months ago and I think it was about using parseUri and simpleHTTP and I had to `case` them separately to provide a meaningful error message. Haven't been able to simply chain them. In other words, it was not a nice looking function. Maybe today I'd write it differently.
I suggest taking a loon at the 'errors' package mentioned earlier in this thread, it somewhat simplified my issues with unifying Maybe/Either/EitherT
... what exactly is the problem with using appropriate data types in appropriate places ... It is a good thing. Very good. But I do not think all library writers are using it in correct places. But that's another problem.
vlatko
-------- Original Message -------- Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either? From: Brandon Allbery
To: Vlatko Bašić Cc: Johannes Erber , "haskell-cafe@haskell.org" Date: 09.01.2014 17:32 On Thu, Jan 9, 2014 at 11:25 AM, Vlatko Basic
wrote: ... is letting the programmer do what they need to do with a minimum of fuss or extra work ... But doesn't the need of mixing Maybe and Either cause more work for the programmer? Programmer of library, or programmer using the library?
Arguably if you need to switch from Maybe to Either then you did not think out your program sufficiently beforehand. And if you did think it out, what exactly is the problem with using appropriate data types in appropriate places?
-- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi Vlatko, it is not only about convenience, but also about readability and conciseness which are quite important in order to maintain software and to prevent it from getting cluttered up unnecessarily. Also Maybe evolves naturally as minimal complete type for use cases in which you can have either a value or no value. Either extends Maybe by more information why no value is returned by a function. Of course you could replace Maybe with Eithers and your code would still work, but it would be much less concise and hence maintainable. Best, Johannes On 09/01/2014 15:26, Vlatko Basic wrote:
Hi Johannes,
I thought there was some "more important" reason than convenience, so I asked. :-O)
I put String because I'm currently thinking about error handling, and Left String is the usual way of reporting failure, and I see Maybe as a type for reporting errors, failures and similar.
Somehow it looks to me that famous "8 ways to report errors in Haskell" could be shortened by one if Maybe is replaced with Either (with appropriate synonyms, of course).
vlatko
-------- Original Message -------- Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either? From: Johannes Erber
To: vlatko.basic@gmail.com, haskell-cafe@haskell.org Date: 09.01.2014 16:07 Hi Vlatko,
to keep programs clear, short and simple. What makes you choose String as type to replace Nothing?
Best, Johannes
On 09/01/2014 14:50, Vlatko Basic 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

On Thu, Jan 9, 2014 at 9:50 AM, Vlatko Basic
With my current knowledge of Haskell, I do not see why is there Maybe if we have Either.
Because, while Either () a is isomorphic to Maybe a, it can be rather less convenient to have to track that Left (). We can easily define a type that doesn't need the extra work; why not use it? Especially when that particular case turns out to be very common. The fact that you *can* build up a standard library from first principles every time you write a program does not mean that you *should*. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Hi Vlatko, On Thu, Jan 09, 2014 at 03:50:16PM +0100, Vlatko Basic wrote:
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?
If you just want to signalize a fail case without any additional information, then a Maybe fits better than an Either, because why should you need this bogus empty string? How should you know that the string doesn't contain something relevant? Greetings, Daniel

Hi Daniel,
If you just want to signalize a fail case without any additional information, then a Maybe fits better than an Either, because why should you need this bogus empty string?
Now it looks to me that it might be better and more consistent to write an empty
bogus string (as with: nothing = Left "") than to have two distinct ways of
error reporting, and both are used widely and often should be intermixed.
And at the end, call site could decide does it want to use the string or not.
vlatko
-------- Original Message --------
Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either?
From: Daniel Trstenjak
Hi Vlatko,
On Thu, Jan 09, 2014 at 03:50:16PM +0100, Vlatko Basic wrote:
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?
If you just want to signalize a fail case without any additional information, then a Maybe fits better than an Either, because why should you need this bogus empty string?
How should you know that the string doesn't contain something relevant?
Greetings, Daniel _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, Jan 9, 2014 at 10:36 AM, Vlatko Basic
If you just want to signalize a fail case without any additional information, then a Maybe fits better than an Either, because why should you need this bogus empty string?
Now it looks to me that it might be better and more consistent to write an empty bogus string (as with: nothing = Left "") than to have two distinct ways of error reporting, and both are used widely and often should be intermixed. And at the end, call site could decide does it want to use the string or not.
I should note here that this is pretty much the "fail" method for Monad, which is widely considered to have been a mistake. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

Hi Vlatko,
Actually, Maybe can be seen as Either () a. Not String or Int because then
you would have multiple Nothings (if Left "" is Nothing then what would Left
"hello" be?). So Either e a is (generally) somewhat *bigger* than Maybe a.
Since Either can be used to represent strictly bigger types, you may not
want it sometimes. lookup function (to find element in a list by its index)
is an example. It is unnecessary to keep extra information in () or Stringor
Int about what caused failure. So if we either *don’t care what caused
failure* or *can unambiguously determine the cause* Maybe suits better (is
more readable) than Either ().
Nick
2014/1/9 Vlatko Basic
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

Besides, even defining type Maybe a = Either () a in standard library wouldn’t be the same as data Maybe a = Nothing | Just a since in Haskell 98 type synonyms aren’t allowed in instance declarations, which means that programmers would still have to remember that `Maybe` is actually `Either ()` under-the-hood every time when writing an instance.

Hi Artyom,
Yes, indeed.
But instance declarations wouldn't be needed because we already have all the
instances for Either. The point would be to have them unified.
But it is a problem that there are no data constructor synonyms in Haskell. So
at least pattern matching wouldn't be possible the simple way. Maybe
ViewPatterns could (partially) solve that.
vlatko
-------- Original Message --------
Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either?
From: Artyom Kazak
Besides, even defining
type Maybe a = Either () a
in standard library wouldn’t be the same as
data Maybe a = Nothing | Just a
since in Haskell 98 type synonyms aren’t allowed in instance declarations, which means that programmers would still have to remember that `Maybe` is actually `Either ()` under-the-hood every time when writing an instance. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, Jan 09, 2014 at 05:36:06PM +0100, Vlatko Basic wrote:
But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified.
http://hackage.haskell.org/package/errors-1.4.5/docs/Control-Error-Util.html

But you actually might want different instances for `Maybe` and for `Either`.
For example, let's say that you have a typeclasse for serializing
data-structures into a database.
In case of Maybe you want to serialize `Nothing` into 'NULL' or
something similar; in case of Either you want to have an
entirely different structure.
Additionally, as it have been mentioned, `Either String a` is not the
same thing as `Maybe a`.
Say, you have a value of type `Either String a`. How do you know that
all `Left`-values are suppose to be empty string?
What do you actually want is `Either () a`.
On Thu, Jan 9, 2014 at 8:36 PM, Vlatko Basic
Hi Artyom,
Yes, indeed.
But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified. But it is a problem that there are no data constructor synonyms in Haskell. So at least pattern matching wouldn't be possible the simple way. Maybe ViewPatterns could (partially) solve that.
vlatko
-------- Original Message -------- Subject: Re: [Haskell-cafe] Why Maybe exists if there is Either? From: Artyom Kazak
To: haskell-cafe@haskell.org Date: 09.01.2014 16:23 Besides, even defining
type Maybe a = Either () a
in standard library wouldn’t be the same as
data Maybe a = Nothing | Just a
since in Haskell 98 type synonyms aren’t allowed in instance declarations, which means that programmers would still have to remember that `Maybe` is actually `Either ()` under-the-hood every time when writing an instance. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Sincerely yours, -- Daniil

But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified. Consider a generic Show instance for Either – there’s no way¹ to make it behave differently for Either () (or Maybe if it was a type synonym). I
On 01/09/2014 08:36 PM, Vlatko Basic wrote: think there are other cases in which we want Maybe to behave differently from Either, but I can’t think of any on the spot. ¹ this point also applies to String, which is a type synonym for [Char] – and it was solved somewhat inelegantly by adding an additional method to Show typeclass *specifically* for showing lists of things.

On Thu, Jan 9, 2014 at 8:51 AM, Artyom Kazak
On 01/09/2014 08:36 PM, Vlatko Basic wrote:
But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified.
Consider a generic Show instance for Either – there’s no way¹ to make it behave differently for Either () (or Maybe if it was a type synonym). I think there are other cases in which we want Maybe to behave differently from Either, but I can’t think of any on the spot.
¹ this point also applies to String, which is a type synonym for [Char] – and it was solved somewhat inelegantly by adding an additional method to Show typeclass *specifically* for showing lists of things.
I think this is a really important point. If I may go further, it's arguable that 'type String = [Char]' was a poor decision, and one of the arguments is that it's not possible to make different instances for String and [a] (hence the showsList issue). Also, due to laziness, Either () a is bigger than Maybe a. It should be 'Either Void a'. But Void has only recently been added to the standard-ish library, which means if we'd used Either () from the start now we'd be stuck with the wrong type. I'd rather have a separate Maybe that does exactly what it's meant to. John L.

I think this is a really important point. If I may go further, it's arguable that 'type String = [Char]' was a poor decision, and one of the arguments is that it's not possible to make different instances for String and [a] (hence the showsList issue). Actually, I think it’s an instance of a bigger problem: *newtypes aren’t as transparent as they should’ve been*. The very first thing a beginner is told about newtypes is that they bear no additional runtime cost at all – which, in fact, hasn’t been strictly true until the recent introduction of Roles – but the first thing they learn *by theirself* is
On 01/10/2014 04:17 AM, John Lato wrote: that newtypes are only “free” for the computer, not for the programmer! Imagine an alternative Prelude in which `String` is a newtype for `[Char]` and not a type synonym: you wouldn’t be able to `map` over it without deconstructing it first, or `reverse` it, or even compute its `length`... And having to type `f (Str s)` instead of `f s` would already discourage people enough that most of them would be trying to avoid Strings, even if only subconsciously. General tendency to follow the path of least resistance is probably the reason why newtypes aren’t used as often as they should be. Have there been any proposals aiming to solve this issue? (A quick search didn’t bring up anything, but maybe I was just searching for a wrong thing.)

On Thu, Jan 9, 2014 at 5:29 PM, Artyom Kazak
On 01/10/2014 04:17 AM, John Lato wrote:
I think this is a really important point. If I may go further, it's arguable that 'type String = [Char]' was a poor decision, and one of the arguments is that it's not possible to make different instances for String and [a] (hence the showsList issue).
Actually, I think it’s an instance of a bigger problem: *newtypes aren’t as transparent as they should’ve been*. The very first thing a beginner is told about newtypes is that they bear no additional runtime cost at all – which, in fact, hasn’t been strictly true until the recent introduction of Roles – but the first thing they learn *by theirself* is that newtypes are only “free” for the computer, not for the programmer!
Imagine an alternative Prelude in which `String` is a newtype for `[Char]` and not a type synonym: you wouldn’t be able to `map` over it without deconstructing it first, or `reverse` it, or even compute its `length`... And having to type `f (Str s)` instead of `f s` would already discourage people enough that most of them would be trying to avoid Strings, even if only subconsciously.
General tendency to follow the path of least resistance is probably the reason why newtypes aren’t used as often as they should be. Have there been any proposals aiming to solve this issue? (A quick search didn’t bring up anything, but maybe I was just searching for a wrong thing.)
The programmer overhead from newtypes is greater than I would like, even as minimal as it is. I find that using isomorphisms from the lens package goes a long way to reducing the programmer overhead from using newtypes. I recently did a very lens-heavy project, and I found that the ease of working with newtype'd values via isomorphisms made me much more likely to define newtypes in the first place. So that's one solution, YMMV. Of course, that's assuming that String would just be a newtype instead of a type alias. If it were an actual abstract type, then we couldn't use map/fmap at all. However, we've had type class based solutions for that problem for some time now, e.g. the ListLike package, and newer alternatives like mono-traversable. (Incidentally, I've been toying with the idea of making ListLike depend on mono-traversable. It would basically just be an API re-skinning, plus we could have more efficient definitions of some functions). An abstract String would also be better than our current situation because Data.List functions on Strings are just plain wrong anyway. Simple example: what should be the reverse of "This is two lines\r\nbecause Windows!\r\n"? It gets even more fun with unicode. Oddly, I was just looking at http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity... 10 minutes ago. (Of course if String were properly abstract, you could still define an isomorphism between String and [Char] somehow. And somewhere in Turkey a tier-1 techie cries out...)

On 10/01/2014, at 8:36 PM, John Lato wrote:
An abstract String would also be better than our current situation because Data.List functions on Strings are just plain wrong anyway. Simple example: what should be the reverse of "This is two lines\r\nbecause Windows!\r\n"? It gets even more fun with unicode.
Linear sequences of characters are a fundamentally broken data type no matter how you represent them in the computer. John Lato gave this link: http://msmvps.com/blogs/jon_skeet/archive/2009/11/02/omg-ponies-aka-humanity... where in the course of making a valid point, the author made the mistake of writing "Les Mis\u0301erables" which should of course be "Les Mise\u0301rables". And when he wrote "Unicode has its own special line terminator character as well" he should have said "Unicode adds THREE more special characters": the Latin-1 Next Line character (U+0085), Line Separator (U+2028), and Paragraph Separator (U+2029). Just to add to the fun, \r, \n, \r\n, and \205 are line *terminators* while the other two are *separators*. Unicode is *insanely* complicated. It includes a set of *prefix* operators (the "Ideographic Description Characters") for describing *trees* of Chinese characters to be displayed in a single box, but a set of *postfix* operators for other things like accents and for saying "the FUEL PUMP U+26FD characte preceding should be in colour" [VS15 = "text style", VS16 = "emoji style", see http://www.unicode.org/L2/L2011/11438-emoji-var.pdf]. And just to complete the trifecta, there's a distfix operator <interlinear annotation anchor> base text <interlinear annotation separator> annotation text <interlinear annotation terminator>. Oh, I didn't mention the *infix* operator "combining grapheme cluster". If there is _any_ sane way to reverse a Unicode string, which I rather doubt, it would be _horrible_ to implement it. And frankly, the use of lists here would *not* contribute materially to the difficulty. (For the record, I tried to implement a string reversal operation that made sense for Unicode, and very quickly became extremely bewildered.)

Hi, Am Mittwoch, den 22.01.2014, 14:56 +1300 schrieb Richard A. O'Keefe:
If there is _any_ sane way to reverse a Unicode string, which I rather doubt, it would be _horrible_ to implement it.
I suggest this:
reverse unicode_str = '\u202E' : unicode_str
It works great! Greetings, Joachim -- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0x4743206C Debian Developer: nomeata@debian.org

Ha, this is a clever trick. Does it really work in all the cases?
On Wed, Jan 22, 2014 at 1:23 PM, Joachim Breitner
Hi,
Am Mittwoch, den 22.01.2014, 14:56 +1300 schrieb Richard A. O'Keefe:
If there is _any_ sane way to reverse a Unicode string, which I rather doubt, it would be _horrible_ to implement it.
I suggest this:
reverse unicode_str = '\u202E' : unicode_str
It works great!
Greetings, Joachim
-- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0x4743206C Debian Developer: nomeata@debian.org
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Sincerely yours, -- Daniil

Hi, Am Mittwoch, den 22.01.2014, 14:04 +0400 schrieb Daniil Frumin:
Ha, this is a clever trick. Does it really work in all the cases?
that depends on the unicode implementation of the client, of course. But in enough cases to annoy or confuse people. Greetings, Joachim -- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0x4743206C Debian Developer: nomeata@debian.org

For still more fun:
http://www.explainxkcd.com/wiki/index.php/1137:_RTL
Cheers,
Bob
On Jan 22, 2014, at 5:07 AM, Joachim Breitner
Hi,
Am Mittwoch, den 22.01.2014, 14:04 +0400 schrieb Daniil Frumin:
Ha, this is a clever trick. Does it really work in all the cases?
that depends on the unicode implementation of the client, of course. But in enough cases to annoy or confuse people.
Greetings, Joachim
-- Joachim “nomeata” Breitner mail@joachim-breitner.de • http://www.joachim-breitner.de/ Jabber: nomeata@joachim-breitner.de • GPG-Key: 0x4743206C Debian Developer: nomeata@debian.org _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 22/01/2014, at 10:23 PM, Joachim Breitner wrote:
Am Mittwoch, den 22.01.2014, 14:56 +1300 schrieb Richard A. O'Keefe:
If there is _any_ sane way to reverse a Unicode string, which I rather doubt, it would be _horrible_ to implement it.
I suggest this:
reverse unicode_str = '\u202E' : unicode_str
It works great!
According to "Unicode Demystified", The Unicode bidirectional text layout algorithm (or "bi-di algorithm," as it is most commonly called) is possibly the most complicated and difficult-to- understand aspect of the Unicode standard. Considering the rest of Unicode, that says a LOT. This particular example using RIGHT-TO-LEFT OVERRIDE is a neat hack, but as a *general* way to reverse strings it's a FAIL: let s="\u202DIt's harder than you think." '\u202E' : s You will see no reversal in the output. Reverting to the original topic, Maybe and Either signify different things to human beings, and in the original libraries, the cost of having both was negligible compared with the benefits. Just recently I was revising some code in another language, where they had the equivalent of infinity = 99999.0 to serve as the initial value in a "search for the cheapest element and its cost" loop, and I suggested the equivalent of using Maybe (Cost, Item) instead, on the grounds that when you make up an "arbitrary" value it's a bad sign. In fact making this change led me to a deeper understanding of what the algorithm could do and a clarifying shift in its structure. (Amongst other things, the original code actually _relied_ on infinity + finite > infinity, which is not the way infinities are supposed to behave.) Representing Maybe as Either with an arbitrary value made up to be the "missing" value seems to me like just such a bad sign.

On 10 Jan 2014, at 04:17, John Lato
wrote: On Thu, Jan 9, 2014 at 8:51 AM, Artyom Kazak
wrote: On 01/09/2014 08:36 PM, Vlatko Basic wrote: But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified. Consider a generic Show instance for Either – there’s no way¹ to make it behave differently for Either () (or Maybe if it was a type synonym). I think there are other cases in which we want Maybe to behave differently from Either, but I can’t think of any on the spot.
¹ this point also applies to String, which is a type synonym for [Char] – and it was solved somewhat inelegantly by adding an additional method to Show typeclass *specifically* for showing lists of things.
I think this is a really important point. If I may go further, it's arguable that 'type String = [Char]' was a poor decision, and one of the arguments is that it's not possible to make different instances for String and [a] (hence the showsList issue).
Also, due to laziness, Either () a is bigger than Maybe a. It should be 'Either Void a'. But Void has only recently been added to the standard-ish library, which means if we'd used Either () from the start now we'd be stuck with the wrong type. I'd rather have a separate Maybe that does exactly what it's meant to.
Why is 'Either Void a' isomorphic to 'Maybe a'. What would 'Nothing' be in this case? Either () a is the correct version. Roughly speaking 'Maybe a' contains |a|+1 values while Either Void a contains only |a| values, since you can not construct any Left's
John L. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Actually, Left absurd is perfect Nothing (mind laziness!).
On the other hand, with Either () a you have 2 different Nothings:
Left ()and Left
_|_.
2014/1/10 Dan Frumin
On 10 Jan 2014, at 04:17, John Lato
wrote: On Thu, Jan 9, 2014 at 8:51 AM, Artyom Kazak
wrote: On 01/09/2014 08:36 PM, Vlatko Basic wrote:
But instance declarations wouldn't be needed because we already have all the instances for Either. The point would be to have them unified.
Consider a generic Show instance for Either – there’s no way¹ to make it behave differently for Either () (or Maybe if it was a type synonym). I think there are other cases in which we want Maybe to behave differently from Either, but I can’t think of any on the spot.
¹ this point also applies to String, which is a type synonym for [Char] – and it was solved somewhat inelegantly by adding an additional method to Show typeclass *specifically* for showing lists of things.
I think this is a really important point. If I may go further, it's arguable that 'type String = [Char]' was a poor decision, and one of the arguments is that it's not possible to make different instances for String and [a] (hence the showsList issue).
Also, due to laziness, Either () a is bigger than Maybe a. It should be 'Either Void a'. But Void has only recently been added to the standard-ish library, which means if we'd used Either () from the start now we'd be stuck with the wrong type. I'd rather have a separate Maybe that does exactly what it's meant to.
Why is 'Either Void a' isomorphic to 'Maybe a'. What would 'Nothing' be in this case?
Either () a is the correct version. Roughly speaking 'Maybe a' contains |a|+1 values while Either Void a contains only |a| values, since you can not construct any Left's
John L.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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

On 09/01/14 20:57, Patrick Hurst wrote:
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).
Hey, you're right, I should have been using C all along! Thanks for showing me the light. -- Mateusz K.

On Fri, Jan 10, 2014 at 5:12 AM, Vlatko Basic
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.
Less so than you'd think, or there wouldn't be so much resistance to (for example) replacing list-specific stuff in Prelude with the more general Foldable/Traversable ones. The pressure for simple and easy to understand stuff (like Maybe vs. Either () or whatever) turns out to be stronger. -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

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
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
To: vlatko.basic@gmail.com Cc: "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
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (16)
-
Artyom Kazak
-
Bob Hutchison
-
Brandon Allbery
-
Clint Adams
-
Dan Frumin
-
Daniel Trstenjak
-
Daniil Frumin
-
Joachim Breitner
-
Johannes Erber
-
John Lato
-
Mateusz Kowalczyk
-
Nickolay Kudasov
-
Patrick Hurst
-
Patrick Mylund Nielsen
-
Richard A. O'Keefe
-
Vlatko Basic