Skip to content

Instantly share code, notes, and snippets.

@funrep
Created September 2, 2013 15:38
Show Gist options
  • Save funrep/6414202 to your computer and use it in GitHub Desktop.
Save funrep/6414202 to your computer and use it in GitHub Desktop.
{-# 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