Last active
March 18, 2016 21:31
-
-
Save imeckler/f1d5b201834808caf93a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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