fork download
  1. import Control.Monad
  2.  
  3. data Result a e = Ok a | Error e
  4.  
  5. newtype StateError s e a = StateError { runStateError :: s -> (Result a e, s) }
  6.  
  7. instance Functor (StateError s e) where
  8. fmap f x = x >>= (pure . f)
  9.  
  10. instance Applicative (StateError s e) where
  11. pure x = StateError $ \s -> (Ok x, s)
  12. x <*> y = ap x y
  13.  
  14. instance Monad (StateError s e) where
  15. return x = pure x
  16.  
  17. m >>= f = StateError $
  18. \s -> case runStateError m s of
  19. (Ok x, s1) -> runStateError (f x) s1
  20. (Error e, s1) -> (Error e, s1)
  21.  
  22. get = StateError $ \s -> ((Ok s), s)
  23.  
  24. put s = StateError $ \_ -> ((Ok ()), s)
  25.  
  26. main = return ()
Success #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty