[MonadError example based on the MTL Samuel Bronson **20061024233706] { addfile ./tests/examples/MonadError.phc hunk ./tests/examples/MonadError.phc 1 +-- Port of part of Control.Monad.Error to use ATs (and then to phrac) + +data Either a b = Left a | Right b; + +class Error e where { + noMsg :: e; + strMsg :: List Char -> e; +} + +instance Error (List Char) where { + noMsg = Nil; + strMsg s = s; +} + +class Monad m where { + return :: a -> m a; + (>>=) :: m a -> (a -> m b) -> m b; +} + +(>>) p q = p >>= \_ -> q; + +class Monad m => MonadError m where { + type MError m; + throwError :: MError m -> m a; + catchError :: m a -> (MError m -> m a) -> m a; +} + +instance (Error e) => Monad (Either e) where { + return = Right; + (>>=) p q = case p of + Left e -> Left e; + Right x -> q x;; +} + +instance (Error e) => MonadError (Either e) where { + type MError (Either e) = e; + throwError = Left; + catchError p h = case p of + Left e -> h e; + x -> x;; +} + +main :: Either (List Char) Unit; +main = return (); }