module GF.Data.ErrM where
import Control.Monad (MonadPlus(..),ap)
import Control.Applicative
data Err a = Ok a | Bad String
deriving (Read, Show, Eq)
err :: (String -> b) -> (a -> b) -> Err a -> b
err d f e = case e of
Ok a -> f a
Bad s -> d s
fromErr :: a -> Err a -> a
fromErr a = err (const a) id
instance Monad Err where
return = Ok
fail = Bad
Ok a >>= f = f a
Bad s >>= f = Bad s
instance Functor Err where
fmap f (Ok a) = Ok (f a)
fmap f (Bad s) = Bad s
instance Applicative Err where
pure = return
(<*>) = ap
instance MonadPlus Err where
mzero = Bad "error (no reason given)"
mplus (Ok a) _ = Ok a
mplus (Bad s) b = b
instance Alternative Err where
empty = mzero
(<|>) = mplus