[Haskell-beginners] Functor instance

Hilco Wijbenga hilco.wijbenga at gmail.com
Sun Mar 4 03:54:48 UTC 2018


On Sat, Mar 3, 2018 at 6:41 PM, Francesco Ariis <fa-ml at ariis.it> wrote:
> On Sat, Mar 03, 2018 at 06:31:47PM -0800, Hilco Wijbenga wrote:
>> In any case, _when_ I flip "success" and "failure" the Functor
>> instance no longer compiles. Which probably makes sense because I did
>> not tell the compiler to interpret "Result failure" as "Result *
>> failure"?
>
> I wonder if you are talking about failure (type parameter) or
> Failure (data constructor).

I believe I am talking about "failure" the type parameter.

> This instance obviously work
>
>     instance Functor (Result success) where
>         fmap f (Success value) = Success (f value)
>         fmap _ (Failure error) = Failure error
>
> Flipping in `data` of course means you are to flip one of:
> a) instance or b) data constructor, e.g.:
>
>     instance Functor (Result success) where
>         fmap f (Failure error) = Failure (f error)
>         fmap _ (Success value) = Success value

Yes, indeed. But that's what I meant with 'now "a" has a different
meaning'. I understand that to the compiler there is no practical
difference between Result a b and Result b a ... but there is to me.
:-)

So am I to understand then that to be able to do the kind of "fmap" I
want (i.e. one that affects the "success" value), I _have to_ make
sure that I use "Result failure success" (and not "Result success
failure")?


More information about the Beginners mailing list