File: State.hs

package info (click to toggle)
bali-phy 4.0~beta16%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 15,192 kB
  • sloc: cpp: 119,288; xml: 13,482; haskell: 9,722; python: 2,930; yacc: 1,329; perl: 1,169; lex: 904; sh: 343; makefile: 26
file content (22 lines) | stat: -rw-r--r-- 704 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{-# LANGUAGE NoImplicitPrelude #-}
module Control.Monad.State where

import Data.Tuple    -- for fst, snd
import Data.Function -- for id
import Control.Monad

data State s a = State { runState :: s->(a,s) }

instance Monad State where
    f >>= g = State (\s1 -> let (x,s2) = runState f s1 in runState g s2)
    return x = State (\s -> (x,s))
    mfix f = State (\state1 -> let result@(state1, x) = runState (f x) state1 in result)
    unsafeInterleaveIO f = State (\state -> (state, snd $ runState f state))

get      = State ( \s -> (s , s)   )
put s    = State ( \_ -> ((), s)   )
modify f = State ( \s -> ((), f s) )

evalState x s = fst $ runState x s
execState x s = snd $ runState x s
-- mapState