Skip to content

Instantly share code, notes, and snippets.

@joneshf
Created December 3, 2013 23:35
Show Gist options
  • Save joneshf/7779693 to your computer and use it in GitHub Desktop.
Save joneshf/7779693 to your computer and use it in GitHub Desktop.
{-# LANGUAGE FlexibleInstances, FunctionalDependencies, UndecidableInstances #-}
newtype State s a = State (s -> (a, s))
instance Monad (State s) where
return a = State $ \s -> (a, s)
State st >>= f = State $ \s ->
let
(a, s') = st s
State st' = f a
in
st' s'
class Monad m => StateMonad m s | m -> s where
update :: (s -> s) -> m s
set :: s -> m s
set = update . const
fetch :: m s
fetch = update id
instance StateMonad (State s) s where
update f = State $ \s -> (s, f s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment