Skip to content

Instantly share code, notes, and snippets.

@imeckler
Last active March 18, 2016 21:31
Show Gist options
  • Save imeckler/f1d5b201834808caf93a to your computer and use it in GitHub Desktop.
Save imeckler/f1d5b201834808caf93a to your computer and use it in GitHub Desktop.
module State ( State
, return
, join
, map
, bind
, (>!=)
, run
, eval
, exec
, get
, put
, modify
) where
type alias State s a = s -> (a, s)
get : State s s
get = \s -> (s, s)
put : s -> State s ()
put s' = \_ -> ((), s')
modify : (s -> s) -> State s ()
modify f = \s -> ((), f s)
eval : State s a -> s -> a
eval mx s = fst (run mx s)
exec : State s a -> s -> s
exec mx s = snd (run mx s)
run : State s a -> s -> (a, s)
run mx = mx
return : a -> State s a
return x = \s -> (x, s)
join : State s (State s a) -> State s a
join f = \s -> let (mx, s') = f s in mx s'
map : (a -> b) -> State s a -> State s b
map f mx = \s -> let (x, s') = mx s in (f x, s')
bind : (a -> State s b) -> State s a -> State s b
bind f mx = \s -> let (x, s') = mx s in f x s'
-- A non-principal type was inferred for this, causing wonky errors
-- (>>=) : State s a -> (a -> State s b) -> State s b
(>>=) mx f = bind f mx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment