Skip to content

Instantly share code, notes, and snippets.

@tonymorris

tonymorris/State.hs

Created Jul 11, 2013
Embed
What would you like to do?
IO does not mean what you think it means.
import Control.Monad
data State s a = State (s -> (a, s))
instance Monad (State s) where
return a =
State (\s -> (a, s))
State j >>= f =
State (\s -> let (a, t) = j s
State k = f a
in k t)
get ::
State s s
get =
State (\s -> (s, s))
-- If it is the case that…
a = liftM (\ a -> [a, a]) getLine
b = sequence [getLine, getLine]
-- demonstrates that "IO is impure" because they are inequivalent programs,
-- then it is the case that…
a' = liftM (\ a -> [a, a]) get
b' = sequence [get, get]
-- demonstrates that "State is impure" because they are inequivalent programs,
-- which is absurd — QEA.
-- They are in fact inequivalent programs regardless.
-- Using IO does not mean you are not doing pure FP any more than using [] does.
-- Abandon this common myth, leaving it to propagation on degenerate mailing lists.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment