Purpose of MonadSplit? #20

Zemyla opened this Issue Apr 1, 2016 · 1 comment


None yet

2 participants

Zemyla commented Apr 1, 2016

What exactly is MonadSplit for? It seems to get a split version of the state, but you can't do anything with it without any further constraints, and you can't feed it back into the monad.

A more useful purpose of MonadSplit would be for something along the lines of unsafeInterleaveIO or unsafeInterleaveST, but safe; it would split the state, run the action with one half, and return the result with the other half, so a lengthy calculation doesn't have to hold up the evaluation.

class (MonadRandom m) => MonadInterleave m where
    interleave :: m a -> m a

instance (Monad m, RandomGen g) => MonadRandom (RandT g m a) where
    interleave (RandT m) = liftRandT $ \g -> case split g of
        (gl, gr) -> fmap (\p -> (fst p, gr)) $ runStateT m gl

This way, you can produce (for instance) a lazy list in the monad with something like let infList = liftM2 (:) someValue $ interleave infList.

byorgey commented Apr 1, 2016

To be honest I'm not really sure. I inherited maintainership of the code from someone else and I haven't really looked much at MonadSplit. I'm certainly open to a pull request for something like MonadInterleave.

@byorgey byorgey added a commit that closed this issue Nov 8, 2016
@byorgey MonadInterleave
Closes #20
@byorgey byorgey closed this in 3db5ace Nov 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment