Created
November 11, 2011 08:05
-
-
Save raimohanska/1357480 to your computer and use it in GitHub Desktop.
A simple Monad for Transactional actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Transactional(Transactional, Connection, transactionally, getConnection) where | |
data Connection = Connection | |
data Transactional a = Transactional (Connection -> IO a) | |
instance Monad Transactional where | |
(>>=) op toNext = Transactional $ \conn -> perform conn op >>= perform conn . toNext | |
return a = Transactional $ return . const a | |
getConnection :: Transactional Connection | |
getConnection = Transactional $ return | |
perform :: Connection -> Transactional a -> IO a | |
perform conn (Transactional op) = op conn | |
newConnection :: IO Connection | |
newConnection = undefined | |
commit :: Connection -> IO () | |
commit = undefined | |
transactionally :: Transactional a -> IO a | |
transactionally (Transactional op) = do | |
conn <- newConnection | |
result <- op conn | |
commit conn | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment