projects
/
ghc-hetmet.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
e2f9c3d
)
Added Applicative and Functor instances for State monad
author
Twan van Laarhoven
<twanvl@gmail.com>
Thu, 17 Jan 2008 17:46:56 +0000
(17:46 +0000)
committer
Twan van Laarhoven
<twanvl@gmail.com>
Thu, 17 Jan 2008 17:46:56 +0000
(17:46 +0000)
compiler/utils/State.hs
patch
|
blob
|
history
diff --git
a/compiler/utils/State.hs
b/compiler/utils/State.hs
index
bf5b3a0
..
f28a8ef
100644
(file)
--- a/
compiler/utils/State.hs
+++ b/
compiler/utils/State.hs
@@
-1,8
+1,20
@@
module State where
module State where
+import MonadUtils
+
newtype State s a = State { runState' :: s -> (# a, s #) }
newtype State s a = State { runState' :: s -> (# a, s #) }
+instance Functor (State s) where
+ fmap f m = State $ \s -> case runState' m s of
+ (# r, s' #) -> (# f r, s' #)
+
+instance Applicative (State s) where
+ pure x = State $ \s -> (# x, s #)
+ m <*> n = State $ \s -> case runState' m s of
+ (# f, s' #) -> case runState' n s' of
+ (# x, s'' #) -> (# f x, s'' #)
+
instance Monad (State s) where
return x = State $ \s -> (# x, s #)
m >>= n = State $ \s -> case runState' m s of
instance Monad (State s) where
return x = State $ \s -> (# x, s #)
m >>= n = State $ \s -> case runState' m s of