Commit 1c76e168 authored by Herbert Valerio Riedel's avatar Herbert Valerio Riedel 🕺
Browse files

template-haskell: define `MonadFail Q` instance

When `MonadFail`is available, this patch makes `MonadFail` a superclass
of `Quasi`, and `Q` an instance of `MonadFail`.

NB: Since f16ddcee, we need to be able
    to compile `template-haskell` with stage0 compilers that don't provide
    a `MonadFail` class yet. Once we reach GHC 8.3 development we can drop
    the CPP conditionals again.

Addresses #11661

Reviewed By: bgamari, goldfire

Differential Revision: https://phabricator.haskell.org/D1982
parent 941b8f5f
......@@ -12,6 +12,7 @@ import GHCi.RemoteTypes
import GHC.Serialized
import Control.Exception
import qualified Control.Monad.Fail as Fail
import Data.Binary
import Data.Binary.Put
import Data.ByteString (ByteString)
......@@ -60,6 +61,9 @@ instance Monad GHCiQ where
do (m', s') <- runGHCiQ m s
(a, s'') <- runGHCiQ (f m') s'
return (a, s'')
fail = Fail.fail
instance Fail.MonadFail GHCiQ where
fail err = GHCiQ $ \s -> throwIO (GHCiQException s err)
getState :: GHCiQ QState
......
......@@ -4,6 +4,10 @@
{-# OPTIONS_GHC -fno-warn-inline-rule-shadowing #-}
#if MIN_VERSION_base(4,9,0)
# define HAS_MONADFAIL 1
#endif
-----------------------------------------------------------------------------
-- |
-- Module : Language.Haskell.Syntax
......@@ -38,13 +42,21 @@ import GHC.Lexeme ( startsVarSym, startsVarId )
import Language.Haskell.TH.LanguageExtensions
import Numeric.Natural
#if HAS_MONADFAIL
import qualified Control.Monad.Fail as Fail
#endif
-----------------------------------------------------
--
-- The Quasi class
--
-----------------------------------------------------
#if HAS_MONADFAIL
class Fail.MonadFail m => Quasi m where
#else
class Monad m => Quasi m where
#endif
qNewName :: String -> m Name
-- ^ Fresh names
......@@ -162,7 +174,14 @@ runQ (Q m) = m
instance Monad Q where
Q m >>= k = Q (m >>= \x -> unQ (k x))
(>>) = (*>)
#if !HAS_MONADFAIL
fail s = report True s >> Q (fail "Q monad failure")
#else
fail = Fail.fail
instance Fail.MonadFail Q where
fail s = report True s >> Q (Fail.fail "Q monad failure")
#endif
instance Functor Q where
fmap f (Q x) = Q (fmap f x)
......
......@@ -43,6 +43,8 @@
fixity if there is an explicit fixity declaration for that `Name`, and
`Nothing` otherwise (#10704 and #11345)
* Add `MonadFail Q` instance for GHC 8.0 and later (#11661)
* TODO: document API changes and important bugfixes
......
......@@ -22,6 +22,9 @@ module Eval (
import Control.Applicative
import Control.Monad
#if __GLASGOW_HASKELL__ >= 800
import Control.Monad.Fail (MonadFail(fail))
#endif
import Data.Binary
import Data.Binary.Get
......@@ -73,6 +76,11 @@ instance Monad GHCJSQ where
return (a, s'')
return = pure
#if __GLASGOW_HASKELL__ >= 800
instance MonadFail GHCJSQ where
fail = undefined
#endif
instance TH.Quasi GHCJSQ where qRunIO m = GHCJSQ $ \s -> fmap (,s) m
-- | the Template Haskell server
......
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