Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Functor instance (sasa bogicevic) 2. Re: Functor instance (Francesco Ariis) 3. Re: Functor instance (Mihai Maruseac) 4. Re: Functor instance (sasa bogicevic) 5. Re: Functor instance (David McBride) 6. Re: Functor instance (sasa bogicevic) ---------------------------------------------------------------------- Message: 1 Date: Mon, 9 Jan 2017 16:41:39 +0100 From: sasa bogicevic <brutalles...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Functor instance Message-ID: <c75a867c-271a-49c7-b1e4-28b2ce901...@gmail.com> Content-Type: text/plain; charset=us-ascii Hi all, Can someone explain to me why exactly when defining the Functor instance for type that has polimorphic parameter constraint I am not allowed to put that parameter in definition. So basically: data Four a b c d = Four a b c d instance Functor (Four a b c) where <-- why can't I specify also param d here ?? fmap f (Four a b c d) = Four a b c (f d) I asked the same question on IRC and got the answer that it is because of Functor has kind * -> *. I thought I understand it but it is not clear to me completely. I understand type kinds on examples I looked at before but when looking at this Functor instance why exactly I don't specify all params for the type and while we are at it why don't I also specify f param for the Functor like this "instance Functor f (Four a b c d) where ...". Thanks, Sasa { name: Bogicevic Sasa phone: +381606006200 } ------------------------------ Message: 2 Date: Mon, 9 Jan 2017 17:00:55 +0100 From: Francesco Ariis <fa...@ariis.it> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Functor instance Message-ID: <20170109160055.ga9...@casa.casa> Content-Type: text/plain; charset=us-ascii On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote: > Hi all, > Can someone explain to me why exactly when defining the Functor > instance for type that has polimorphic parameter constraint I > am not allowed to put that parameter in definition. So basically: > > data Four a b c d = Four a b c d > > instance Functor (Four a b c) where <-- why can't I specify also > param d here ?? > fmap f (Four a b c d) = Four a b c (f d) Hello Sasa, think for a moment about functors you already know: Maybe a, [a], (Either e) a, etc. In every case there is an `a` and something preceding a (we can call it `f`). Now let's look at the class-definition of functor. class Functor f where -- etc. etc. Here you have it, `f`; so for the instance you should only place the `f`-part in there, like instance Functor Maybe where -- not Maybe a! instance Functor (Four a b c) where -- without the a too! It makes sense as `f` will stay the same and `a` will be mapped over (and change). Indeed you can very this with ghci (using :k Functor, :k Maybe etc.), but after a while it sinks in. Does this help? ------------------------------ Message: 3 Date: Mon, 9 Jan 2017 08:05:23 -0800 From: Mihai Maruseac <mihai.marus...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Functor instance Message-ID: <CAOMsUMLcrRBHiuUcQB50jVJXTmSRYSrRdb2=8ydleoi_w4e...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 If you imagine a type as a box with some slots (each slot for each type variable), Functor only allows you to map on the last slot. That and the currying of functions lead to the intuition behind "Functor has kind * -> *" (aka: you need the type for the last slot to fully build the type that would be an instance of Functor) On Mon, Jan 9, 2017 at 7:41 AM, sasa bogicevic <brutalles...@gmail.com> wrote: > Hi all, > Can someone explain to me why exactly when defining the Functor instance for > type that has polimorphic parameter constraint I am not allowed to put that > parameter in definition. So basically: > > data Four a b c d = Four a b c d > > instance Functor (Four a b c) where <-- why can't I specify also param d > here ?? > fmap f (Four a b c d) = Four a b c (f d) > > I asked the same question on IRC and got the answer that it is because of > Functor has kind * -> *. I thought I understand it but it is not clear to me > completely. I understand type kinds on examples I looked at before but > when looking at this Functor instance why exactly I don't specify all params > for the type and while we are at it why don't I also specify f param for the > Functor like this "instance Functor f (Four a b c d) where ...". > > Thanks, Sasa > { > name: Bogicevic Sasa > phone: +381606006200 > } > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -- Mihai Maruseac (MM) "If you can't solve a problem, then there's an easier problem you can solve: find it." -- George Polya ------------------------------ Message: 4 Date: Mon, 9 Jan 2017 17:26:12 +0100 From: sasa bogicevic <brutalles...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Functor instance Message-ID: <34f367d7-867f-4cf3-838e-04989d757...@gmail.com> Content-Type: text/plain; charset=us-ascii Good explanation! Thanks I understand it now Sasa Bogicevic { phone: +381606006200 } > On Jan 9, 2017, at 17:00, Francesco Ariis <fa...@ariis.it> wrote: > >> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote: >> Hi all, >> Can someone explain to me why exactly when defining the Functor >> instance for type that has polimorphic parameter constraint I >> am not allowed to put that parameter in definition. So basically: >> >> data Four a b c d = Four a b c d >> >> instance Functor (Four a b c) where <-- why can't I specify also >> param d here ?? >> fmap f (Four a b c d) = Four a b c (f d) > > Hello Sasa, > think for a moment about functors you already know: Maybe a, [a], > (Either e) a, etc. In every case there is an `a` and something preceding > a (we can call it `f`). > Now let's look at the class-definition of functor. > > class Functor f where -- etc. etc. > > Here you have it, `f`; so for the instance you should only place > the `f`-part in there, like > > instance Functor Maybe where -- not Maybe a! > instance Functor (Four a b c) where -- without the a too! > > It makes sense as `f` will stay the same and `a` will be mapped over > (and change). > Indeed you can very this with ghci (using :k Functor, :k Maybe etc.), > but after a while it sinks in. > > Does this help? > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 5 Date: Mon, 9 Jan 2017 11:40:23 -0500 From: David McBride <toa...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Functor instance Message-ID: <CAN+Tr40RnEmGiL8OiAGqmDKbt9=sdadyprm8k5gff8tsyoj...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 There is also a BiFunctor in base which allows you to fmap over the last two variables in your type. bimap :: (a -> b) -> (c -> d) -> p a c -> p b d And it conveniently has methods to map over only one at a time, if you want. first :: (a -> b) -> p a c -> p b c second :: (b -> c) -> p a b -> p a c This makes sense for types like Either a b, where sometimes you might want to fmap over one or both values. However, going beyond two would cause the number of options for mapping over such a type to blow up. At that point it is better to just use your own simple function to map over your own type. On Mon, Jan 9, 2017 at 11:26 AM, sasa bogicevic <brutalles...@gmail.com> wrote: > Good explanation! Thanks I understand it now > > Sasa Bogicevic > { > phone: +381606006200 > } > >> On Jan 9, 2017, at 17:00, Francesco Ariis <fa...@ariis.it> wrote: >> >>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote: >>> Hi all, >>> Can someone explain to me why exactly when defining the Functor >>> instance for type that has polimorphic parameter constraint I >>> am not allowed to put that parameter in definition. So basically: >>> >>> data Four a b c d = Four a b c d >>> >>> instance Functor (Four a b c) where <-- why can't I specify also >>> param d here ?? >>> fmap f (Four a b c d) = Four a b c (f d) >> >> Hello Sasa, >> think for a moment about functors you already know: Maybe a, [a], >> (Either e) a, etc. In every case there is an `a` and something preceding >> a (we can call it `f`). >> Now let's look at the class-definition of functor. >> >> class Functor f where -- etc. etc. >> >> Here you have it, `f`; so for the instance you should only place >> the `f`-part in there, like >> >> instance Functor Maybe where -- not Maybe a! >> instance Functor (Four a b c) where -- without the a too! >> >> It makes sense as `f` will stay the same and `a` will be mapped over >> (and change). >> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.), >> but after a while it sinks in. >> >> Does this help? >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Message: 6 Date: Mon, 9 Jan 2017 19:07:17 +0100 From: sasa bogicevic <brutalles...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Functor instance Message-ID: <452e2d46-e198-4876-b948-47a75b03e...@gmail.com> Content-Type: text/plain; charset=us-ascii I will look at bimap too, sounds usefull, thanks! Sasa Bogicevic { phone: +381606006200 } > On Jan 9, 2017, at 17:40, David McBride <toa...@gmail.com> wrote: > > There is also a BiFunctor in base which allows you to fmap over the > last two variables in your type. > > bimap :: (a -> b) -> (c -> d) -> p a c -> p b d > > And it conveniently has methods to map over only one at a time, if you want. > > first :: (a -> b) -> p a c -> p b c > second :: (b -> c) -> p a b -> p a c > > This makes sense for types like Either a b, where sometimes you might > want to fmap over one or both values. > > However, going beyond two would cause the number of options for > mapping over such a type to blow up. At that point it is better to > just use your own simple function to map over your own type. > >> On Mon, Jan 9, 2017 at 11:26 AM, sasa bogicevic <brutalles...@gmail.com> >> wrote: >> Good explanation! Thanks I understand it now >> >> Sasa Bogicevic >> { >> phone: +381606006200 >> } >> >>>> On Jan 9, 2017, at 17:00, Francesco Ariis <fa...@ariis.it> wrote: >>>> >>>> On Mon, Jan 09, 2017 at 04:41:39PM +0100, sasa bogicevic wrote: >>>> Hi all, >>>> Can someone explain to me why exactly when defining the Functor >>>> instance for type that has polimorphic parameter constraint I >>>> am not allowed to put that parameter in definition. So basically: >>>> >>>> data Four a b c d = Four a b c d >>>> >>>> instance Functor (Four a b c) where <-- why can't I specify also >>>> param d here ?? >>>> fmap f (Four a b c d) = Four a b c (f d) >>> >>> Hello Sasa, >>> think for a moment about functors you already know: Maybe a, [a], >>> (Either e) a, etc. In every case there is an `a` and something preceding >>> a (we can call it `f`). >>> Now let's look at the class-definition of functor. >>> >>> class Functor f where -- etc. etc. >>> >>> Here you have it, `f`; so for the instance you should only place >>> the `f`-part in there, like >>> >>> instance Functor Maybe where -- not Maybe a! >>> instance Functor (Four a b c) where -- without the a too! >>> >>> It makes sense as `f` will stay the same and `a` will be mapped over >>> (and change). >>> Indeed you can very this with ghci (using :k Functor, :k Maybe etc.), >>> but after a while it sinks in. >>> >>> Does this help? >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 103, Issue 3 *****************************************