summaryrefslogtreecommitdiff
path: root/compiler/utils/State.hs
diff options
context:
space:
mode:
authorTwan van Laarhoven <twanvl@gmail.com>2008-01-17 17:46:56 +0000
committerTwan van Laarhoven <twanvl@gmail.com>2008-01-17 17:46:56 +0000
commit452cba444c2f7cba3a9007f449fea3119c9074a9 (patch)
treec022ad28d8cdd1bd10922093edc38039563ae262 /compiler/utils/State.hs
parente2f9c3d7ea98b46d3d34d23915f682d82f520d2b (diff)
downloadhaskell-452cba444c2f7cba3a9007f449fea3119c9074a9.tar.gz
Added Applicative and Functor instances for State monad
Diffstat (limited to 'compiler/utils/State.hs')
-rw-r--r--compiler/utils/State.hs12
1 files changed, 12 insertions, 0 deletions
diff --git a/compiler/utils/State.hs b/compiler/utils/State.hs
index bf5b3a0d5e..f28a8efc51 100644
--- a/compiler/utils/State.hs
+++ b/compiler/utils/State.hs
@@ -1,8 +1,20 @@
module State where
+import MonadUtils
+
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