Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Functor instance (Hilco Wijbenga)
2. Re: Functor instance (Francesco Ariis)
3. Re: Functor instance (Hilco Wijbenga)
4. Re: Functor instance (Francesco Ariis)
5. Re: Functor instance (Hilco Wijbenga)
6. Re: Functor instance (Theodore Lief Gannon)
----------------------------------------------------------------------
Message: 1
Date: Sat, 3 Mar 2018 12:32:34 -0800
From: Hilco Wijbenga <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] Functor instance
Message-ID:
<cae1poi2dwbjd0strquszpqzi1cbg-ue6sjvxzqmojacycr-...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"
Hi all,
I'm trying to implement my own Result type (and yes, I'm aware you can
abuse Either for this :-) ) but doing something as (seemingly?) simple
as implementing a Functor instance was surprisingly difficult.
data Result failure success
= Success success
| Failure failure
instance Functor (Result failure) where
fmap f (Success value) = Success (f value)
fmap _ (Failure error) = Failure error
-- fmap _ result@(Failure error) = result
-- fmap _ result = result
1) Is it possible to define "Result" as "Result success failure"
(instead of "Result failure success") and _still_ create an instance
of Functor?
2) The two alternatives for fmap for the Failure scenario do not
compile (the end result is "Result failure a" instead of "Result
failure b") and that makes sense. But I would like to be able to
express that "result" is not touched. Is there any way to do that?
3) And while wondering about that, is GHC smart enough to realize that
"= Failure error" in the failure scenario is actually a NOP? (I'm just
curious.)
Cheers,
Hilco
------------------------------
Message: 2
Date: Sat, 3 Mar 2018 22:40:43 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Functor instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hello Hilco,
On Sat, Mar 03, 2018 at 12:32:34PM -0800, Hilco Wijbenga wrote:
> data Result failure success
> = Success success
> | Failure failure
>
> instance Functor (Result failure) where
> fmap f (Success value) = Success (f value)
> fmap _ (Failure error) = Failure error
> -- fmap _ result@(Failure error) = result
> -- fmap _ result = result
>
> 1) Is it possible to define "Result" as "Result success failure"
> (instead of "Result failure success") and _still_ create an instance
> of Functor?
Yes, as far the compiler is concerned `data Result failure success`
is equivalent to `data Result a b`. Same in your instance, you could
have written:
instance Functor (Result a) where
-- etc.
no problem.
> 2) The two alternatives for fmap for the Failure scenario do not
> compile (the end result is "Result failure a" instead of "Result
> failure b") and that makes sense. But I would like to be able to
> express that "result" is not touched. Is there any way to do that?
You can but you have to modify your datatype! Probably you want
something like this:
data Result r f = Result r (ResState e)
data ResState e = Ok | Error e
> 3) And while wondering about that, is GHC smart enough to realize that
> "= Failure error" in the failure scenario is actually a NOP? (I'm just
> curious.)
Not sure about this one, but I strongly suspect so!
Was the explanation clear?
-F
------------------------------
Message: 3
Date: Sat, 3 Mar 2018 18:31:47 -0800
From: Hilco Wijbenga <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: Re: [Haskell-beginners] Functor instance
Message-ID:
<cae1poi1+dbt3nk7hqd1pfhdr+jm4c6pdw4jc8z7kzwxepi0...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"
On Sat, Mar 3, 2018 at 1:40 PM, Francesco Ariis <[email protected]> wrote:
> On Sat, Mar 03, 2018 at 12:32:34PM -0800, Hilco Wijbenga wrote:
>> data Result failure success
>> = Success success
>> | Failure failure
>>
>> instance Functor (Result failure) where
>> fmap f (Success value) = Success (f value)
>> fmap _ (Failure error) = Failure error
>> -- fmap _ result@(Failure error) = result
>> -- fmap _ result = result
>>
>> 1) Is it possible to define "Result" as "Result success failure"
>> (instead of "Result failure success") and _still_ create an instance
>> of Functor?
>
> Yes, as far the compiler is concerned `data Result failure success`
> is equivalent to `data Result a b`. Same in your instance, you could
> have written:
>
> instance Functor (Result a) where
> -- etc.
>
> no problem.
But now "a" has a different meaning, doesn't it? I had the impression
that the "Result a" was similar to currying or leaving a hole
(something like " Result a * " but if I change the meaning of "a" from
"failure" to "success" then things don't work anymore, do they?
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"?
>> 2) The two alternatives for fmap for the Failure scenario do not
>> compile (the end result is "Result failure a" instead of "Result
>> failure b") and that makes sense. But I would like to be able to
>> express that "result" is not touched. Is there any way to do that?
>
> You can but you have to modify your datatype! Probably you want
> something like this:
>
> data Result r f = Result r (ResState e)
> data ResState e = Ok | Error e
Ah, I see. Mmm, I'll have to think about that. I prefer the current setup. :-)
------------------------------
Message: 4
Date: Sun, 4 Mar 2018 03:41:39 +0100
From: Francesco Ariis <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Functor instance
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
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). 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
------------------------------
Message: 5
Date: Sat, 3 Mar 2018 19:54:48 -0800
From: Hilco Wijbenga <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: Re: [Haskell-beginners] Functor instance
Message-ID:
<cae1poi0swq8dedemm4if_vkgl1_aghctpwenogmlnxupdiw...@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"
On Sat, Mar 3, 2018 at 6:41 PM, Francesco Ariis <[email protected]> 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")?
------------------------------
Message: 6
Date: Sat, 3 Mar 2018 20:10:36 -0800
From: Theodore Lief Gannon <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Functor instance
Message-ID:
<CAJoPsuD=9r-h6-hqe6p-xbvjspvncwx9ys-d4wh_5sjjhvu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
That is correct. You can think of type parameters as being curried and
applied the same way as function parameters. Since application is strictly
left to right, and we have no type-level flip function (barring newtype),
getting your type down to the single parameter allowed in a Functor
instance can only ever mean using the rightmost parameter in the type's
definition. This is just a semantic limitation of how Haskell types are
expressed.
On Mar 3, 2018 7:56 PM, "Hilco Wijbenga" <[email protected]> wrote:
> On Sat, Mar 3, 2018 at 6:41 PM, Francesco Ariis <[email protected]> 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")?
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20180303/0a84ee9c/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 117, Issue 2
*****************************************