From 452cba444c2f7cba3a9007f449fea3119c9074a9 Mon Sep 17 00:00:00 2001 From: Twan van Laarhoven Date: Thu, 17 Jan 2008 17:46:56 +0000 Subject: [PATCH] Added Applicative and Functor instances for State monad --- compiler/utils/State.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compiler/utils/State.hs b/compiler/utils/State.hs index bf5b3a0..f28a8ef 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 -- 1.7.10.4