Created
December 3, 2013 23:35
-
-
Save joneshf/7779693 to your computer and use it in GitHub Desktop.
This file contains 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
{-# 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