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