module Database.PostgreSQL.Simple.Ok where
import Control.Applicative
import Control.Exception
import Control.Monad(MonadPlus(..))
import Data.Typeable
newtype ManyErrors = ManyErrors [SomeException]
deriving (Show, Typeable)
instance Exception ManyErrors
data Ok a = Errors [SomeException] | Ok !a
deriving(Show, Typeable, Functor)
instance Eq a => Eq (Ok a) where
Errors _ == Errors _ = True
Ok a == Ok b = a == b
_ == _ = False
instance Applicative Ok where
pure = Ok
Errors es <*> _ = Errors es
_ <*> Errors es = Errors es
Ok f <*> Ok a = Ok (f a)
instance Alternative Ok where
empty = Errors []
a@(Ok _) <|> _ = a
Errors _ <|> b@(Ok _) = b
Errors as <|> Errors bs = Errors (as ++ bs)
instance MonadPlus Ok where
mzero = empty
mplus = (<|>)
instance Monad Ok where
return = Ok
Errors es >>= _ = Errors es
Ok a >>= f = f a