I would like to make some of my code more “monadic” and to use a Reader monad rather than passing around a common environment. But somewhere I have to use mutable vectors, and hence the ST monad as well; to make things interesting, the ST action needs to access the environment (but the rest of the function does not). In other words, this works:
aux :: Int -> Reader Env Double
aux i = -- something
bla :: [a] -> Reader Env Double
bla l = do e <- ask
return $ runST $ do -- something producing an Int
let o = runReader (aux i) e
-- something else depending on o
return something
but it feels very ugly and wrong, kind of a convoluted way to still pass the environment explicitly; I would like bla to look more like this:
bla :: [a] -> Reader Env Double
bla l = return $ runST $ do -- something producing an Int
o <- ??? aux i
-- something depending on o
return something
Does it make sense? Is it possible? If yes, what do I have to put instead of ??? ? I guess the true question would be, what is a good way to achieve this kind of thing? What design would you recommend?
You can mix
ReaderwithSTusingReaderT. Something like that: