Skip to content
Browse files

Added an assignment operator to the state effect which takes monadic …

…values.
  • Loading branch information...
1 parent b810ef6 commit 858d0ec28880c3cd46628d5ed03374cfdb5145c8 @sjoerdvisscher committed Nov 13, 2011
Showing with 12 additions and 11 deletions.
  1. +7 −10 examples.hs
  2. +5 −1 src/Control/Effects/State.hs
View
17 examples.hs
@@ -10,6 +10,7 @@ import Control.Effects.NonDet
import qualified Data.Set as Set
import Data.Monoid
+import Control.Applicative
testIO :: IO ()
@@ -21,16 +22,12 @@ testIO = runBase $ do
testRef :: (Int, Int)
testRef = run $ do
- with (ref 10) $ \u -> do
- with (ref 20) $ \v -> do
- w <- get u
- put u (w + 5)
- x <- get v
- put v (x + 1)
- y <- get u
- z <- get v
- return (y, z)
-
+ with (ref 5) $ \x -> do
+ with (ref 10) $ \y -> do
+ x =: (+) <$> get x <*> get y
+ y =: (+) <$> get x <*> get y
+ (,) <$> get x <*> get y
+
testWriter :: (String, (String, Int))
testWriter = run $ do
View
6 src/Control/Effects/State.hs
@@ -9,7 +9,11 @@ get :: AutoLift (State s m a) m n => Effect (State s m a) m -> n s
get p = operation p $ \k -> return $ \s -> do r <- k s; r s
put :: AutoLift (State s m a) m n => Effect (State s m a) m -> s -> n ()
-put p s' = operation p $ \k -> return $ \_ -> do r <- k (); r s'
+put p s = operation p $ \k -> return $ \_ -> do r <- k (); r s
+
+infixr 3 =:
+(=:) :: AutoLift (State s m a) m n => Effect (State s m a) m -> n s -> n ()
+p =: m = m >>= put p
modify :: AutoLift (State s m a) m n => Effect (State s m a) m -> (s -> s) -> n ()
modify p f = do

0 comments on commit 858d0ec

Please sign in to comment.
Something went wrong with that request. Please try again.