[Haskell-cafe] What is this applicative functor?
dave at zednenem.com
Wed Feb 1 05:28:18 UTC 2017
On Tue, Jan 31, 2017 at 11:55 PM, Joachim Breitner <mail at joachim-breitner.de
> 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.
> > 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:
> 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)
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>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe