[Haskell-cafe] What is this applicative functor?

Christopher Allen cma at bitemyapp.com
Tue Jan 31 21:32:16 UTC 2017


I must confess, this one's behavior makes more sense to me. It's the
product of the guaranteed values and the cartesian product.

On Tue, Jan 31, 2017 at 3:25 PM, Matt <parsonsmatt at gmail.com> wrote:
> Interestingly, neither NEL nor OneStep behave the same as `Product Identity
> []`:
>
> λ> let a = Pair (Identity 0) [1,2]
> λ> let b = Pair (Identity 3) [4,5]
> λ> (,) <$> a <*> b
> Pair (Identity (0,3)) [(1,4),(1,5),(2,4),(2,5)]
>
> So many applicatives!
>
> Matt Parsons
>
> On Tue, Jan 31, 2017 at 3:41 PM, Christopher Allen <cma at bitemyapp.com>
> wrote:
>>
>> Ah right, I didn't stare hard enough at the Applicative.
>>
>> If I squint a bit at the results of your Applicative instance it seems
>> similar to the difference between []'s Applicative and the ZipList
>> Applicative, except a little crossed up. Sure you already identified
>> that, sorry I don't have more to offer here.
>>
>> On Tue, Jan 31, 2017 at 2:36 PM, Joachim Breitner
>> <mail at joachim-breitner.de> wrote:
>> > Hi,
>> >
>> > good idea, but no. The datatypes are superficially equivalent, but the
>> > Applicative instance differs:
>> >
>> > With NonEmpty’s instance:
>> >
>> >> (,) <$> 0 :| [1,2] <*> 3 :| [4,5]
>> > (0,3) :| [(0,4),(0,5),(1,3),(1,4),(1,5),(2,3),(2,4),(2,5)]
>> >
>> > with my instance:
>> >
>> >> (,) <$> 0 :| [1,2] <*> 3 :| [4,5]
>> > (0,3) :| [(1,3),(2,3),(0,4),(0,5)]
>> >
>> > Greetings,
>> > Joachim
>> >
>> > Am Dienstag, den 31.01.2017, 14:24 -0600 schrieb Christopher Allen:
>> >> NonEmpty?
>> >>
>> >> On Tue, Jan 31, 2017 at 2:22 PM, Joachim Breitner
>> >> <mail at joachim-breitner.de> wrote:
>> >> > Hi,
>> >> >
>> >> > I recently wrote this applicative functor:
>> >> >
>> >> >     data OneStep a = OneStep a [a]
>> >> >
>> >> >     instance Functor OneStep where
>> >> >         fmap f (OneStep o s) = OneStep (f o) (map f s)
>> >> >
>> >> >     instance Applicative OneStep where
>> >> >         pure x = OneStep x []
>> >> >         OneStep f fs <*> OneStep x xs = OneStep (f x) (map ($x) fs
>> >> > ++ map f xs)
>> >> >
>> >> >     takeOneStep :: OneStep t -> [t]
>> >> >     takeOneStep (OneStep _ xs) = xs
>> >> >
>> >> > This was useful in the context of writing a shrink for QuickCheck,
>> >> > as
>> >> > discussed at http://stackoverflow.com/a/41944525/946226.
>> >> >
>> >> > Now I wonder: Does this functor have a proper name? Does it already
>> >> > exist in the libraries somewhere? Should it?
>> >> >
>> >> > Greetings,
>> >> > Joachim
>> >> >
>> >> > --
>> >> > Joachim “nomeata” Breitner
>> >> >   mail at joachim-breitner.dehttps://www.joachim-breitner.de/
>> >> >   XMPP: nomeata at joachim-breitner.de • OpenPGP-Key: 0xF0FBF51F
>> >> >   Debian Developer: nomeata at debian.org
>> >> > _______________________________________________
>> >> > Haskell-Cafe mailing list
>> >> > To (un)subscribe, modify options or view archives go to:
>> >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> >> > Only members subscribed via the mailman list are allowed to post.
>> >>
>> >>
>> >>
>> > --
>> > Joachim “nomeata” Breitner
>> >   mail at joachim-breitner.dehttps://www.joachim-breitner.de/
>> >   XMPP: nomeata at joachim-breitner.de • OpenPGP-Key: 0xF0FBF51F
>> >   Debian Developer: nomeata at debian.org
>> >
>> > _______________________________________________
>> > Haskell-Cafe mailing list
>> > To (un)subscribe, modify options or view archives go to:
>> > http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> > Only members subscribed via the mailman list are allowed to post.
>>
>>
>>
>> --
>> Chris Allen
>> Currently working on http://haskellbook.com
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
>
>



-- 
Chris Allen
Currently working on http://haskellbook.com


More information about the Haskell-Cafe mailing list