1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
module Text.RE.ZeInternals.Types.Poss where
import Control.Monad.Fail
data Poss a
= Eek String
| Yup a
deriving (Eq,Ord,Show)
instance Functor Poss where
fmap f p = case p of
Eek m -> Eek m
Yup x -> Yup $ f x
instance Applicative Poss where
pure = Yup
(<*>) p1 p2 = case p1 of
Eek m -> Eek m
Yup f -> case p2 of
Eek n -> Eek n
Yup x -> Yup $ f x
instance Monad Poss where
return = pure
(>>=) p f = case p of
Eek m -> Eek m
Yup x -> f x
instance MonadFail Poss where
fail = Eek
poss :: (String->b) -> (a->b) -> Poss a -> b
poss f _ (Eek s) = f s
poss _ g (Yup x) = g x
poss2either :: Poss a -> Either String a
poss2either (Eek m) = Left m
poss2either (Yup x) = Right x
|