[Haskell-beginners] How can I lift my function taking a function to a Monad?
Simon Peter Nicholls
simon at mintsource.org
Fri Dec 11 09:20:24 UTC 2015
On Thu, 10 Dec, 2015 at 6:43 PM, Kim-Ee Yeoh <ky3 at atamo.com> wrote:
> The wrong way is to try to write:
>
> neededLift :: (Monad m) => ((b -> b) -> a -> a) -> (m b -> m b) -> m
> a -> m a
>
> which, as Daniel pointed out, is impossible.
>
> So we start with the isomorphisms:
>
> isoL :: (f -> a) -> (a -> f -> f) -> ((a -> a) -> (f -> f))
> isoL g s m f = s (m (g f)) f
> isoR :: (f -> a) -> ((a -> a) -> (f -> f)) -> (a -> f -> f)
> isoR _ m a = m (const a)
>
> It turns out we really only need isoL, but isoR is there for
> completeness.
>
> Then we write:
>
> liftALabel :: Applicative f => a :-> b -> f a :-> f b
> liftALabel l0 = lens g1 m1 where
> g1 = fmap (get l0)
> s1 = liftA2 (set l0)
> m1 = isoL g1 s1
>
> Naturally, monad has been effect-reduced to applicative to admit more
> legal programs.
>
> -- Kim-Ee
Thanks guys, I appreciate your help. You've given me a lot of food for
thought here, Kim-Ee, and I'll spin off a new project to explore this
in isolation.
Si
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151211/b2e6db94/attachment-0001.html>
More information about the Beginners
mailing list