import Control.Monad data Result a e = Ok a | Error e newtype StateError s e a = StateError { runStateError :: s -> (Result a e, s) } instance Functor (StateError s e) where fmap f x = x >>= (pure . f) instance Applicative (StateError s e) where pure x = StateError $ \s -> (Ok x, s) x <*> y = ap x y instance Monad (StateError s e) where return x = pure x m >>= f = StateError $ \s -> case runStateError m s of (Ok x, s1) -> runStateError (f x) s1 (Error e, s1) -> (Error e, s1) get = StateError $ \s -> ((Ok s), s) put s = StateError $ \_ -> ((Ok ()), s) main = return ()