Proposal: Make StateT in mtl lazy

Ian Lynagh igloo at earth.li
Thu Feb 1 09:24:05 EST 2007


On Wed, Jan 31, 2007 at 03:48:25PM +0000, Ian Lynagh wrote:
> 
> instance Monad (State s) where
>     return a = State $ \s -> (a, s)
>     m >>= k  = State $ \s -> case runState m s of
>                              (a, s') -> runState (k a) $! s'
> 
> instance (Monad m) => Monad (StateT s m) where
>     return a = StateT $ \s -> return (a, s)
>     m >>= k  = StateT $ \s -> do
>         (a, s') <- runStateT m s
>         runStateT (k a) $! s'
>     fail str = StateT $ \_ -> fail str

We agreed on IRC that the $!'s solve an orthogonal problem so the strict
monads will just have:

instance Monad (State s) where
    return a = State $ \s -> (a, s)
    m >>= k  = State $ \s -> case runState m s of
                             (a, s') -> runState (k a) s'

instance (Monad m) => Monad (StateT s m) where
    return a = StateT $ \s -> return (a, s)
    m >>= k  = StateT $ \s -> do
        (a, s') <- runStateT m s
        runStateT (k a) s'
    fail str = StateT $ \_ -> fail str


Thanks
Ian



More information about the Libraries mailing list