[Haskell] STM Monad Transformer
drtomc at gmail.com
Tue Jun 6 03:16:43 EDT 2006
I'm not quite sure I've even got the question right, but here goes anyway....
I am using STM for a concurrent BTree implementation. My plan is to
have a cache of file pages that have been read from disk. A "pointer"
within the BTree will either dereference to a page address for a page
that has not been read in, or the in-memory representation for a page
that has been read. Clear as mud? Consider the following prototypical
data Node k v = Leaf [(k,v)] | Node [(k,NodePtr k v)]
type NodePtr k v = TVar (Either Int (Node k v))
where the Int is a file offset, or page number, or somesuch.
If an attempt is made during a STM transaction to access a page that
is not in memory,
I'd like the transaction to "abort" and return the address of the page
so that it can be read from disk and the transaction retried.
Now, the code is going to contain quite a frew fragments like the following:
insert p k v
n <- getNode p
case n of
Leaf ... ->
I think I want the type of insert to be
insert :: NodePtr k v -> k -> v -> Either (STM ()) Int
that is, either I get back the transaction, or the page address.
The question is, how do I combine the two monads STM and Either to
achieve this cleanly?
I've tried reading a few pages about monad transformers, but it is not
evident to me yet how to combine the two. I think it calls for STMT
(following the FooT naming convention).
Any astute observations welcome,
More information about the Haskell