[Haskell-cafe] Stacking StateTs

Antoine Latter aslatter at gmail.com
Sat Feb 21 21:01:38 EST 2009


On Sat, Feb 21, 2009 at 5:37 PM, David Menendez <dave at zednenem.com> wrote:
>
> PS. Here are two functions that I ended up not using in my examples,
> but which may come in handy when dealing with nested applications of
> StateT:
>
> curryStateT :: (Monad m) => StateT (s,t) m a -> StateT s (StateT t m) a
> curryStateT m = StateT $ \s -> StateT $ \t ->
>        runStateT m (s,t) >>= \ ~(a,(s,t)) -> return ((a,s),t)
>
> uncurryStateT :: (Monad m) => StateT s (StateT t m) a -> StateT (s,t) m a
> uncurryStateT m = StateT $ \ ~(s,t) ->
>        runStateT (runStateT m s) t >>= \ ~((a,s),t) -> return (a,(s,t))
>

For some reason that reminds me of one of my favorites:

embedReader :: Monad m => ReaderT r m a -> Reader r (m a)
embedReader m = Reader $ \r -> runReaderT m r

Antoine


More information about the Haskell-Cafe mailing list