Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

How can I turn a monadic function into a monadic state transformer?

Basically, what I want is this:

stateIO :: (s -> IO (a, s)) -> StateT s IO a
stateIO f = do
    r <- get
    (a, r') <- liftIO $ f r
    put r'
    return a

I tried to use state, but the inner liftIO is problematic. Is there another way to be more clever, without that manual tuple unpacking?

Upvotes: 0

Views: 78

Answers (1)

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39390

Oh well. Thanks @bisserlis.

stateIO = StateT

(perhaps applied to avoid monomorphism restriction).

Upvotes: 2

Related Questions