module Quipper.Utils.Template.ErrorMsgQ where
import Language.Haskell.TH
import Control.Applicative (Applicative(..))
import Control.Monad (liftM, ap)
type ErrMsg a = Either String a
data ErrMsgQ a = ErrMsgQ (Q (ErrMsg a))
instance Monad ErrMsgQ where
return x = ErrMsgQ $ return $ return x
(>>=) (ErrMsgQ x) f = ErrMsgQ $ do
x' <- x
case x' of
Left s -> return (Left s)
Right r -> let (ErrMsgQ y) = f r in y
instance Applicative ErrMsgQ where
pure = return
(<*>) = ap
instance Functor ErrMsgQ where
fmap = liftM
errorMsg :: String -> ErrMsgQ a
errorMsg s = ErrMsgQ (return (Left s))
embedQ :: Q a -> ErrMsgQ a
embedQ x = ErrMsgQ $ do x' <- x; return (return x')
extractQ :: String -> ErrMsgQ a -> Q a
extractQ prefix (ErrMsgQ x) =
do
x' <- x
case x' of
Left s -> error (prefix ++ s)
Right x -> return x