[Haskell-cafe] What is this applicative functor?
Oleg Grenrus
oleg.grenrus at iki.fi
Wed Feb 1 07:34:10 UTC 2017
These instances are quite similar to
https://github.com/ambiata/disorder.hs/blob/ce9ffc1139e32eaa9b82a1a6e2cfeb914d3f705c/disorder-jack/src/Disorder/Jack/Tree.hs#L62-L82
(I wish `disorder-jack` were already on Hackage!)
- Oleg
On 01.02.2017 07:28, David Menendez wrote:
> On Tue, Jan 31, 2017 at 11:55 PM, Joachim Breitner
> <mail at joachim-breitner.de <mailto:mail at joachim-breitner.de>> wrote:
>
> Hi,
>
> Am Dienstag, den 31.01.2017, 15:22 -0500 schrieb Joachim Breitner:
> > 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
> <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?
>
> I guess it does not exist, so I am preparing a package for it here:
> https://github.com/nomeata/haskell-successors
> <https://github.com/nomeata/haskell-successors>
>
> The source code contains (in comments) a proof of the Applicative
> laws.
>
> My gut feeling says that this does not have a Monad instance that is
> compatible with the given Applicative instance, but it is too late
> today to substantiate this feeling. If anyone feels like puzzling: Can
> you come up with a Monad instance, or (more likely) give good reasons
> why there cannot be one?
>
>
> How about this?
>
> hd (OneStep x xs) = x
>
> instance Monad OneStep where
> OneStep x xs >>= f = OneStep y (map (hd . f) xs ++ ys)
> where
> OneStep y ys = f x
>
> Not sure if it’s good for anything, but it seems valid and consistent
> based on a preliminary investigation.
>
> --
> Dave Menendez <dave at zednenem.com <mailto:dave at zednenem.com>>
> <http://www.eyrie.org/~zednenem/ <http://www.eyrie.org/%7Ezednenem/>>
>
>
> _______________________________________________
> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170201/76ffbd04/attachment.sig>
More information about the Haskell-Cafe
mailing list