monad-tx-0.0.1: A transactional state monad.

Portabilityportable
Stabilityexperimental
Maintainer<morrow@moonpatio.com>

Control.Monad.Tx

Description

A transactional state monad.

Synopsis

Documentation

data TxM o s a Source

The transaction monad. A State monad, with transactional state.

Instances

Monad (TxM o s) 
Functor (TxM o s) 

data Tx o s e a Source

A transaction handle.

data TxStat e a Source

Transaction Status.

Constructors

Begin 
Abort (Maybe e)

Reverted state, returned an error.

Dirty (Maybe e)

Committed state, returned an error.

Rollback a

Reverted state, returned a result.

Commit a

Committed state, returned a result.

Instances

(Eq e, Eq a) => Eq (TxStat e a) 
(Ord e, Ord a) => Ord (TxStat e a) 
(Read e, Read a) => Read (TxStat e a) 
(Show e, Show a) => Show (TxStat e a) 

runTxM :: TxM o s a -> s -> (s -> a -> o) -> oSource

runTxM_ :: TxM (a, s) s a -> s -> (a, s)Source

begin :: (Tx o s e a -> TxM o s ()) -> TxM o s (TxStat e a)Source

Begin a transaction. begin takes a function which represents this transaction.

abort :: Tx o s e a -> Maybe e -> TxM o s ()Source

Revert state, return an error.

dirty :: Tx o s e a -> Maybe e -> TxM o s ()Source

Commit state, return an error.

rollback :: Tx o s e a -> a -> TxM o s ()Source

Revert state, return a result.

commit :: Tx o s e a -> a -> TxM o s ()Source

Commit state, return a result.

get :: TxM o s sSource

gets :: (s -> a) -> TxM o s aSource

set :: s -> TxM o s ()Source

modify :: (s -> s) -> TxM o s ()Source

test0 :: Tx o Int String String -> TxM o Int ()Source

 test0 :: Tx o Int String String -> TxM o Int ()
 test0 tx = do
   s <- get
   set 99
   case s of
     0 -> return   ()
     1 -> abort    tx (Just (show s))
     2 -> dirty    tx (Just (show s))
     3 -> rollback tx  "rollback!"
     _ -> commit   tx  "wooo"

runTest0 :: [(TxStat String String, Int)]Source

 runTest0 :: [(TxStat String String, Int)]
 runTest0 = fmap (runTxM_ (begin test0)) [0..4]
 ghci> mapM_ print runTest0
 (Abort Nothing,0)
 (Abort (Just "1"),1)
 (Dirty (Just "2"),99)
 (Rollback "rollback!",3)
 (Commit "wooo",99)