module Success.Pure
(
Success,
nothing,
failure,
success,
asEither,
asMaybe,
)
where
import Prelude
import Control.Applicative
import Control.Monad
newtype Success a b =
Success (Either (Maybe a) b)
deriving (Functor, Applicative, Monad)
instance Alternative (Success a) where
empty =
Success (Left Nothing)
(<|>) =
\case
Success (Right x) -> const (Success (Right x))
Success (Left _) -> id
instance MonadPlus (Success a) where
mzero =
empty
mplus =
(<|>)
nothing :: Success a b
nothing =
Success (Left Nothing)
failure :: a -> Success a b
failure failure =
Success (Left (Just failure))
success :: b -> Success a b
success =
pure
asEither :: Success a b -> Either (Maybe a) b
asEither (Success x) =
x
asMaybe :: Success a b -> Maybe b
asMaybe (Success x) =
case x of
Left _ -> Nothing
Right x -> Just x