Added Applicative and Functor instances for State monad
authorTwan van Laarhoven <twanvl@gmail.com>
Thu, 17 Jan 2008 17:46:56 +0000 (17:46 +0000)
committerTwan van Laarhoven <twanvl@gmail.com>
Thu, 17 Jan 2008 17:46:56 +0000 (17:46 +0000)
compiler/utils/State.hs

index bf5b3a0..f28a8ef 100644 (file)
@@ -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