The function from a -> m b
to m (a -> b)
rarely appears in programming, but can be made in the Reader monad. The following code is a tentative implementation. Does such a library exist?
class Monad m => MonadShift m where
shift :: (a -> m b) -> m (a -> b)
instance MonadShift Identity where
shift f = Identity (\x -> runIdentity (f x))
instance MonadShift m => MonadShift (ReaderT r m) where
shift f = ReaderT (\r -> shift (\x -> runReaderT (f x) r))
It's a specialization of
distribute :: Functor f => f (g a) -> g (f a)
of theDistributive
class wheref
is the Functor(->) b
. Then you get the type signature:Note that (1) this doesn't require
g
to be aMonad
, but just aFunctor
, and (2) theIdentity
andReaderT
instances are basically the only instances that you can define: