Created
September 2, 2013 15:38
-
-
Save funrep/6414202 to your computer and use it in GitHub Desktop.
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
{-# 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!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment