multistate 0.1.2 → 0.1.3.1
raw patch · 2 files changed
+110/−23 lines, 2 filesdep ~basedep ~mtldep ~tfp
Dependency ranges changed: base, mtl, tfp, transformers
Files
- example/Example.hs +76/−9
- multistate.cabal +34/−14
example/Example.hs view
@@ -10,6 +10,7 @@ import Control.Applicative ( (<$>), (<*>) ) import Control.Monad.Trans ( lift )+import Control.Monad.Writer {- Small example showing@@ -25,25 +26,91 @@ -- , MonadMultiState Char m -- , m~MultiStateT x IO) -- => m ()-examplePrint = do+simpleExamplePrint = do c <- mGet cs <- mGet lift $ putStrLn (c:cs) -exampleAction = do- examplePrint+simpleExampleAction = do+ simpleExamplePrint mSet 'J'- examplePrint+ simpleExamplePrint -main = evalMultiStateT- $ withMultiState 'H'- $ withMultiState "ello, World!"- $ exampleAction+simpleExample = do+ evalMultiStateT+ $ withMultiState 'H'+ $ withMultiState "ello, World!"+ $ simpleExampleAction --- output of main:+-- output: -- "Hello, World! -- Jello, World! -- "++-- and a more complex example:++newtype Account = Account Float+newtype Interest = Interest Float++setAccount :: MonadMultiState Account m => Float -> m ()+setAccount x = mSet (Account x)+getAccount :: MonadMultiState Account m => m Float+getAccount = do+ (Account x) <- mGet+ return x+modAccount :: MonadMultiState Account m => (Float -> Float) -> m ()+modAccount f = do+ (Account x) <- mGet+ mSet (Account (f x))++-- wait for a specific time, changing the account according to interest+wait :: ( MonadMultiState Account m+ , MonadMultiState Interest m )+ => Float+ -> m ()+wait t = do+ (Interest i) <- mGet+ (Account x) <- mGet+ mSet (Account (x*(1+i)**t))++logAccount :: ( MonadWriter [String] m+ , MonadMultiState Account m)+ => m ()+logAccount = do+ (Account x) <- mGet+ tell $ ["account balance = " ++ show x]++accountCalculation :: Writer [String] ()+accountCalculation = evalMultiStateT $ do+ tell ["account calculation start"]+ -- we cannot use any of the account methods here, because state is empty+ -- logAccount+ -- -->+ -- No instance for (Control.Monad.MultiState.ContainsType Account Null)+ withMultiState (Account 0.0) $ do -- state contains an Account.+ logAccount+ modAccount (+10.0)+ logAccount+ -- trying to use "wait" here would give type error, like above.+ withMultiState (Interest 0.03) $ do -- state now also contains Interest.+ wait 10.0 -- we can use wait, because state contains all+ -- necessary stuff.+ logAccount+ modAccount (\x -> x - 10.0)+ wait 10.0+ logAccount+ mSet (Interest 0.00)+ wait 10.0+ -- we can return back to the environment without interest+ -- but the changes to the account are still present+ logAccount+ -- and we can return to an empty state+ tell ["account calculation end"]++main = do+ simpleExample+ mapM_ putStrLn $ execWriter accountCalculation+ --whatIsNotPossible :: MultiStateT (Cons [Char] Null) IO () --whatIsNotPossible = mGet >>= (lift . print) -- type ambiguous
multistate.cabal view
@@ -1,5 +1,5 @@ Name: multistate-Version: 0.1.2+Version: 0.1.3.1 Cabal-Version: >= 1.8 Build-Type: Simple license: BSD3@@ -11,7 +11,7 @@ Bug-reports: https://github.com/lspitzner/multistate/issues Stability: Experimental category: Control-tested-with: GHC == 7.8.2+tested-with: GHC == 7.6.3, GHC == 7.8.3 Synopsis: like mtl's ReaderT/StateT, but more than one contained value/type. Description:@@ -44,8 +44,24 @@ > $ withMultiState "ello, World!" > $ examplePrint .- ( you can find this example as an executable in the package. )+ the output is: .+ > Hello, World!+ > Jello, World!+ .+ ( you can find both this and a more complex example+ in an executable in the package. )+ .+ == Error Messages+ .+ If you try to execute an action that requires a specific type in the state,+ but the current state does not contain that type, the error message is+ something like+ .+ > Control.Monad.MultiState.ContainsType Foo Null+ .+ where @Foo@ is the missing type.+ . == Known Deficits . This package currently lacks a complete set of "lifting instances", i.e.@@ -77,10 +93,10 @@ Control.Monad.MultiReader other-modules: build-depends:- base >=4 && <6,- mtl >=2 && <3,- tfp >=0.8,- transformers == 0.3.*+ base >= 4.6 && <4.8,+ mtl >= 2.1 && <2.3,+ tfp >= 0.8 && <0.9,+ transformers >= 0.3 && <0.5 extensions: GADTs TypeFamilies@@ -97,10 +113,12 @@ if flag(build-test) { buildable: True build-depends:+ -- no version constraints necessary, because they are already+ -- given by library multistate,- base >= 4 && < 6,- transformers == 0.3.*,- tfp >=0.8+ base,+ tfp,+ transformers } else { buildable: False }@@ -113,11 +131,13 @@ if flag(build-example) { buildable: True build-depends:+ -- no version constraints necessary, because they are already+ -- given by library multistate,- base >= 4 && < 6,- transformers == 0.3.*,- tfp >=0.8,- mtl >=2 && <3+ base,+ mtl,+ tfp,+ transformers } else { buildable: False }