public
Created

  • Download Gist
acid-state.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{-# LANGUAGE DeriveDataTypeable, TypeFamilies, TemplateHaskell #-}
module Main (main) where
 
import Data.Acid
 
import Control.Monad.State
import Control.Monad.Reader
import System.Environment
import Data.SafeCopy
 
import Data.Typeable
 
------------------------------------------------------
-- The Haskell structure that we want to encapsulate
 
data HelloWorldState = HelloWorldState String
deriving (Show, Typeable)
 
$(deriveSafeCopy 0 'base ''HelloWorldState)
 
------------------------------------------------------
-- The transaction we will execute over the state.
 
writeState :: String -> Update HelloWorldState ()
writeState newValue
= put (HelloWorldState newValue)
 
queryState :: Query HelloWorldState String
queryState = do HelloWorldState string <- ask
return string
 
$(makeAcidic ''HelloWorldState ['writeState, 'queryState])
 
------------------------------------------------------
-- This is how AcidState is used:
 
main :: IO ()
main = do acid <- openLocalState (HelloWorldState "Hello world")
args <- getArgs
if null args
then do string <- query acid QueryState
putStrLn $ "The state is: " ++ string
else do update acid (WriteState (unwords args))
putStrLn $ "The state has been modified!"

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.