Commit f2e06f0f authored by Petr Pudlak's avatar Petr Pudlak Committed by Klaus Aehlig
Browse files

Add Alternative instances for GenericResult and ResultT

This allows to use Alternative specific combinators, namely `optional`.
Signed-off-by: default avatarPetr Pudlak <>
Reviewed-by: default avatarKlaus Aehlig <>

Cherry-pick of 78209a84

Signed-off-by: default avatarKlaus Aehlig <>
Reviewed-by: default avatarPetr Pudlak <>
parent 2e09344f
......@@ -112,6 +112,10 @@ instance Applicative (GenericResult a) where
_ <*> (Bad x) = Bad x
(Ok f) <*> (Ok x) = Ok $ f x
instance (FromString a, Monoid a) => Alternative (GenericResult a) where
empty = mzero
(<|>) = mplus
-- | This is a monad transformation for Result. It's implementation is
-- based on the implementations of MaybeT and ErrorT.
newtype ResultT a m b = ResultT {runResultT :: m (GenericResult a b)}
......@@ -157,6 +161,10 @@ instance (Monad m, FromString a, Monoid a) => MonadPlus (ResultT a m) where
mplus x y = elimResultT combine return x
where combine x' = ResultT $ liftM (mplus (Bad x')) (runResultT y)
instance (Monad m, FromString a, Monoid a) => Alternative (ResultT a m) where
empty = mzero
(<|>) = mplus
-- | Lift a `Result` value to a `ResultT`.
resultT :: Monad m => GenericResult a b -> ResultT a m b
resultT = ResultT . return
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment