Proposal: Add IsString instance for (Maybe a) to base

Sturdy, Ian sturdyi12 at mail.wlu.edu
Mon Jul 15 18:21:23 CEST 2013


> Like (IsString (Maybe String)) does not fulfill intuitive laws, (Num
(Maybe a)) does not, too. E.g. if in your code you see 'a+b-b' you would
like to simplify that to 'a'. With a Maybe instance this is not true in
case 'b' is Nothing.

It also violates that rule if `a :: Float` and b is NaN. Should we abolish the Num instance of Float? In any event, I think the example contrived--the only conceivable reason I see for adding zero in such a creative way is to introduce an evaluation dependency, in which case returning Nothing if `b == Nothing` is exactly what I want. Consider also among unquestionably useful instances that violate simple prejudice the Num instance for `Num a => Set a`, where `a + {1,-1} - {1,-1} == {a-2,a,a+2}`.

As for the original proposal, I am a tentative +1; the proposed standards for isString instances are already violated (I think of Attoparsec's instance for Parser). OverloadedStrings is already somewhat far down the road of convenience, and this would be most useful in certain places.

Ian Sturdy
________________________________________
From: Libraries [libraries-bounces at haskell.org] on behalf of Henning Thielemann [lemming at henning-thielemann.de]
Sent: Monday, July 15, 2013 11:24 AM
To: MightyByte
Cc: Haskell Libraries
Subject: Re: Proposal: Add IsString instance for (Maybe a) to base

On Mon, 15 Jul 2013, MightyByte wrote:

> I'll just throw out a data point related to this discussion.  In one of my previous commercial Haskell
> projects we ended up writing a similar Num instance for Maybe a.  I think I asked about it in #haskell back
> then and got pretty vocal distaste about the idea from several people who spoke up.  "Maybe is not a
> number!"  But we did end up using the instance in our project because it greatly simplified a section of our
> code.

I also had this analogy in mind. Looks like syntactic heroin.

Like (IsString (Maybe String)) does not fulfill intuitive laws, (Num
(Maybe a)) does not, too. E.g. if in your code you see 'a+b-b' you would
like to simplify that to 'a'. With a Maybe instance this is not true in
case 'b' is Nothing.

>  I'm certainly not going to recommend that we also add the Num instance
> for Maybe.  But I think it's a useful data point that this happened in
> real-world production code at a company where writing Haskell code was
> my full-time job.

This confirms that real world code contains a lot of mess.





More information about the Libraries mailing list