[Haskell-cafe] unsafeInterleaveIO respecting order of actions

Henning Thielemann lemming at henning-thielemann.de
Sun Jan 4 14:50:53 EST 2009


On Sat, 3 Jan 2009, Henning Thielemann wrote:

> I think I now have general Applicative functionality ...

I hope the following is a proper Monad implementation. In contrast to 
Applicative a Writer for sequencing actions does no longer work, instead I 
need a State monad.


newtype LazyIO a = LazyIO {runLazyIO :: StateT RunAll IO a}

data RunAll = RunAll
    deriving Show

instance Monad LazyIO where
    return x = LazyIO $ return x
    x >>= f = LazyIO $
       mapStateT unsafeInterleaveIO . runLazyIO . f =<<
       mapStateT unsafeInterleaveIO (runLazyIO x)

instance MonadIO LazyIO where
    liftIO m = LazyIO $ StateT $ \RunAll -> fmap (\x->(x,RunAll)) m

evalLazyIO :: LazyIO a -> IO a
evalLazyIO =
    flip evalStateT RunAll . runLazyIO


I'll write some tests and upload it to Hackage.

Thank you for being a patient audience. ;-)


More information about the Haskell-Cafe mailing list