I have a bunch of state functions inside the state monad. At some point, the program should have some I / O, so I wrapped IO inside StateT, getting a couple of these types:
mostfunctions :: State Sometype a toplevel :: StateT Sometype IO a
To keep things simple, I donβt want to pass the IO context into the main set of functions, and I would like to avoid wrapping them in the form of a monad stack. But in order to call them from the top-level function, I need something like an elevator, but I'm not trying to raise the value from the inner monad. Rather, I want to convert the state in the StateT monad to something equivalent in the state monad. For this, I have the following:
wrapST :: (State Sometype a) -> StateT Sometype IO a wrapST f = do s <- get let (r,s2) = runState fs put s2 return r
You will then use the following things to alternate:
toplevel = do liftIO $ Some IO functions wrapST $ Some state mutations liftIO $ More IO functions ....
This seems to be a pretty obvious block of code, so I wonder if this function has a standard name, and is it already implemented somewhere in standard libraries? I tried to keep the description simple, but obviously it expands the ability to pull one of the transformers from the stack, converting the wrapped value into a cousin like a transformer, skipping the monads below on the stack and then returning the results back to the end.
Amoss
source share