[Haskell-cafe] Combining ST with STM
tkoster at gmail.com
Tue Feb 9 13:13:14 UTC 2016
On 9 February 2016 at 14:43, Thomas Koster <tkoster at gmail.com> wrote:
> I have an STM transaction that needs some private, temporary state.
> The most obvious way is to simply pass pure state as arguments, but
> for efficiency, I would like this state to be some kind of mutable
> array, like STArray.
On 9 February 2016 at 22:29, Alberto G. Corona <agocorona at gmail.com> wrote:
> Why not use the state monad transformer?
> atomically $ evalStateT todo initialState
> Seems Ok to me
> evalStateT :: Monad m => StateT s m a -> s -> m a
> in this case:
> evalStateT :: StateT MyState STM a -> MyState -> STM a
Thank you for your suggestion.
Certainly, if the state was an immutable value, small or otherwise
cheaply modified, like a finger tree spine, StateT would be a much
cleaner alternative. But unfortunately it is the mutability of the
STArray that makes it valuable to my application.
"StateT (Vector Value) STM" would not help me much as the state would
need to be copied every time it was modified. In fact, I used a
variation of this in a very early version of my program, except that
the Vector was passed even more simply: as an argument, the way
ReaderT does, with no actual transformer in sight. Switching over to
ST and passing an STArray instead has improved the throughput of my
program greatly (far more than I expect to see from any extra
parallelism I might gain from using STM).
More information about the Haskell-Cafe