STM in Haskell(joeyh.name)
joeyh.name
STM in Haskell
http://joeyh.name/blog/entry/the_newinwheezy_game:_STM/
11 コメント
You can think about your second version as without side-effects too. It's just in IO monad. Actually, all IO is kinda pure, just (IO smth) values fly behind the scenes. All dirty unpure things happen in runtime.
And in the first snippet it is in STM monad, and you can do, for example
And in the first snippet it is in STM monad, and you can do, for example
modifyTVar :: TVar a -> (a -> a) -> STM ()
You should understand monads more to grok it, it's quite beautiful.Haskell isn't non-side-effecting. It's just a bit more sane and structured about when and how.
Anything that happens in the STM monad can be undone if something is wrong. If you for example write to a TVal but another thread was doing it at the same time, one of the threads won't have its changes go through until the transaction is retried. Of course this is all invisible to the person using STM.
I do think this example http://computationalthoughts.blogspot.se/2008/03/some-exampl... is a lot better than the one in the article.
How can that push be non-side-effecting and still do something sensible? If the snippet is replayed, won't multiple pushes happen? This may be a question only joeyh can answer, if this is something specific to his code base...
Naively, I would write something like (again, my Haskell not so good):