If I have a stack of monads, say IO, State and Error, and a function that only uses IO and Error. How would one go about “removing” the middle State monad from the stack so I can use my function? If the order was IO, Error, State, I could use lift to match the types, but I want to be able to use my function if the monad stack contains IO and Error and possibly other monads in whatever order. For example:
fun :: ErrorT String IO ()
fun = throwError "error"
someCode :: ErrorT String (StateT Int IO) ()
someCode = do
-- I want to use fun here
Just change the type signature of
funtofun :: (MonadError String m, MonadIO m) => m (). It will then be usable for any monad stack that have aStringError, and can perform IO (such asErrorT String (StateT Int IO)).Eg: