File: StateM.hs

package info (click to toggle)
haskell-utils 1.6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 104 kB
  • ctags: 13
  • sloc: haskell: 411; makefile: 87
file content (29 lines) | stat: -rw-r--r-- 746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- Based on the mtl Control.Monad.State, which is
-- 
-- Copyright   :  (c) Andy Gill 2001,
-- 		  (c) Oregon Graduate Institute of Science and Technology, 2001
-- License     :  BSD-style (see the file libraries/base/LICENSE)
-- 
-- We'd like to just use the library, but for nhc98 compatibility we can't

module StateM (
	StateM, evalStateM, get, put
  ) where

newtype StateM s a = StateM { runStateM :: s -> (a, s) }

evalStateM :: StateM s a -> s -> a
evalStateM m s = fst (runStateM m s)

instance Monad (StateM s) where
	return a = StateM $ \s -> (a, s)
	m >>= k  = StateM $ \s -> let
		(a, s') = runStateM m s
		in runStateM (k a) s'

get :: StateM s s
get = StateM $ \s -> (s, s)

put :: s -> StateM s ()
put s = StateM $ \_ -> ((), s)