1
0
forked from GitHub/gf-core

MonadFail: Make backwards-compatible

This commit is contained in:
Andreas Källberg
2020-09-05 20:23:07 +02:00
parent 1234c715fc
commit 7268253f5a
15 changed files with 49 additions and 16 deletions

View File

@@ -82,6 +82,7 @@ Library
pretty, pretty,
mtl, mtl,
exceptions, exceptions,
fail,
ghc-prim ghc-prim
hs-source-dirs: src/runtime/haskell hs-source-dirs: src/runtime/haskell

View File

@@ -25,7 +25,7 @@ data PGFEnv = Env {pgf::Maybe PGF,concs::Map.Map ConcName Concr}
pgfEnv pgf = Env (Just pgf) (languages pgf) pgfEnv pgf = Env (Just pgf) (languages pgf)
emptyPGFEnv = Env Nothing Map.empty emptyPGFEnv = Env Nothing Map.empty
class (MonadFail m,MonadSIO m) => HasPGFEnv m where getPGFEnv :: m PGFEnv class (Fail.MonadFail m,MonadSIO m) => HasPGFEnv m where getPGFEnv :: m PGFEnv
instance (Monad m,HasPGFEnv m) => TypeCheckArg m where instance (Monad m,HasPGFEnv m) => TypeCheckArg m where
typeCheckArg e = do env <- getPGFEnv typeCheckArg e = do env <- getPGFEnv

View File

@@ -41,6 +41,7 @@ import Control.Monad
import Control.Monad.Identity import Control.Monad.Identity
--import Control.Exception --import Control.Exception
--import Debug.Trace(trace) --import Debug.Trace(trace)
import qualified Control.Monad.Fail as Fail
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- main conversion function -- main conversion function
@@ -196,7 +197,7 @@ newtype CnvMonad a = CM {unCM :: SourceGrammar
-> ([ProtoFCat],[Symbol]) -> ([ProtoFCat],[Symbol])
-> Branch b} -> Branch b}
instance MonadFail CnvMonad where instance Fail.MonadFail CnvMonad where
fail = bug fail = bug
instance Applicative CnvMonad where instance Applicative CnvMonad where

View File

@@ -1,3 +1,4 @@
{-# LANGUAGE CPP #-}
module GF.Compile.TypeCheck.ConcreteNew( checkLType, inferLType ) where module GF.Compile.TypeCheck.ConcreteNew( checkLType, inferLType ) where
-- The code here is based on the paper: -- The code here is based on the paper:
@@ -19,6 +20,7 @@ import GF.Text.Pretty
import Data.List (nub, (\\), tails) import Data.List (nub, (\\), tails)
import qualified Data.IntMap as IntMap import qualified Data.IntMap as IntMap
import Data.Maybe(fromMaybe,isNothing) import Data.Maybe(fromMaybe,isNothing)
import qualified Control.Monad.Fail as Fail
checkLType :: GlobalEnv -> Term -> Type -> Check (Term, Type) checkLType :: GlobalEnv -> Term -> Type -> Check (Term, Type)
checkLType ge t ty = runTcM $ do checkLType ge t ty = runTcM $ do
@@ -647,7 +649,12 @@ instance Monad TcM where
TcOk x ms msgs -> unTcM (g x) ms msgs TcOk x ms msgs -> unTcM (g x) ms msgs
TcFail msgs -> TcFail msgs) TcFail msgs -> TcFail msgs)
instance MonadFail TcM where #if !(MIN_VERSION_base(4,13,0))
-- Monad(fail) will be removed in GHC 8.8+
fail = Fail.fail
#endif
instance Fail.MonadFail TcM where
fail = tcError . pp fail = tcError . pp

View File

@@ -27,9 +27,10 @@ import Data.List
import qualified Data.Map as Map import qualified Data.Map as Map
import Control.Monad import Control.Monad
import GF.Text.Pretty import GF.Text.Pretty
import qualified Control.Monad.Fail as Fail
-- | combine a list of definitions into a balanced binary search tree -- | combine a list of definitions into a balanced binary search tree
buildAnyTree :: MonadFail m => ModuleName -> [(Ident,Info)] -> m (Map.Map Ident Info) buildAnyTree :: Fail.MonadFail m => ModuleName -> [(Ident,Info)] -> m (Map.Map Ident Info)
buildAnyTree m = go Map.empty buildAnyTree m = go Map.empty
where where
go map [] = return map go map [] = return map

View File

@@ -20,6 +20,8 @@ import GF.Infra.Ident(moduleNameS)
import GF.Text.Pretty import GF.Text.Pretty
import GF.System.Console(TermColors(..),getTermColors) import GF.System.Console(TermColors(..),getTermColors)
import qualified Data.ByteString.Lazy as BS import qualified Data.ByteString.Lazy as BS
-- Control.Monad.Fail import will become redundant in GHC 8.8+
import qualified Control.Monad.Fail as Fail
-- | Compile the given grammar files and everything they depend on, -- | Compile the given grammar files and everything they depend on,
-- like 'batchCompile'. This function compiles modules in parallel. -- like 'batchCompile'. This function compiles modules in parallel.
@@ -256,7 +258,7 @@ instance Output m => Output (CollectOutput m) where
putStrLnE s = CO (return (putStrLnE s,())) putStrLnE s = CO (return (putStrLnE s,()))
putStrE s = CO (return (putStrE s,())) putStrE s = CO (return (putStrE s,()))
instance MonadFail m => MonadFail (CollectOutput m) where instance Fail.MonadFail m => Fail.MonadFail (CollectOutput m) where
fail = CO . fail fail = CO . fail
instance ErrorMonad m => ErrorMonad (CollectOutput m) where instance ErrorMonad m => ErrorMonad (CollectOutput m) where

View File

@@ -30,12 +30,13 @@ import qualified Data.Map as Map
import GF.Text.Pretty(render,(<+>),($$)) --Doc, import GF.Text.Pretty(render,(<+>),($$)) --Doc,
import GF.System.Console(TermColors(..),getTermColors) import GF.System.Console(TermColors(..),getTermColors)
import Control.Monad((<=<)) import Control.Monad((<=<))
import qualified Control.Monad.Fail as Fail
type OneOutput = (Maybe FullPath,CompiledModule) type OneOutput = (Maybe FullPath,CompiledModule)
type CompiledModule = Module type CompiledModule = Module
compileOne, reuseGFO, useTheSource :: compileOne, reuseGFO, useTheSource ::
(Output m,ErrorMonad m,MonadIO m, MonadFail m) => (Output m,ErrorMonad m,MonadIO m, Fail.MonadFail m) =>
Options -> Grammar -> FullPath -> m OneOutput Options -> Grammar -> FullPath -> m OneOutput
-- | Compile a given source file (or just load a .gfo file), -- | Compile a given source file (or just load a .gfo file),

View File

@@ -13,6 +13,7 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
{-# LANGUAGE Rank2Types, MultiParamTypeClasses, FlexibleInstances #-} {-# LANGUAGE Rank2Types, MultiParamTypeClasses, FlexibleInstances #-}
{-# LANGUAGE CPP #-}
module GF.Data.BacktrackM ( module GF.Data.BacktrackM (
-- * the backtracking state monad -- * the backtracking state monad
BacktrackM, BacktrackM,
@@ -32,6 +33,7 @@ import Data.List
import Control.Applicative import Control.Applicative
import Control.Monad import Control.Monad
import Control.Monad.State.Class import Control.Monad.State.Class
import qualified Control.Monad.Fail as Fail
---------------------------------------------------------------------- ----------------------------------------------------------------------
-- Combining endomorphisms and continuations -- Combining endomorphisms and continuations
@@ -70,7 +72,12 @@ instance Monad (BacktrackM s) where
BM m >>= k = BM (\c s b -> m (\a s b -> unBM (k a) c s b) s b) BM m >>= k = BM (\c s b -> m (\a s b -> unBM (k a) c s b) s b)
where unBM (BM m) = m where unBM (BM m) = m
instance MonadFail (BacktrackM s) where #if !(MIN_VERSION_base(4,13,0))
-- Monad(fail) will be removed in GHC 8.8+
fail = Fail.fail
#endif
instance Fail.MonadFail (BacktrackM s) where
fail _ = mzero fail _ = mzero
instance Functor (BacktrackM s) where instance Functor (BacktrackM s) where

View File

@@ -12,10 +12,12 @@
-- hack for BNFC generated files. AR 21/9/2003 -- hack for BNFC generated files. AR 21/9/2003
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
{-# LANGUAGE CPP #-}
module GF.Data.ErrM where module GF.Data.ErrM where
import Control.Monad (MonadPlus(..),ap) import Control.Monad (MonadPlus(..),ap)
import Control.Applicative import Control.Applicative
import qualified Control.Monad.Fail as Fail
-- | Like 'Maybe' type with error msgs -- | Like 'Maybe' type with error msgs
data Err a = Ok a | Bad String data Err a = Ok a | Bad String
@@ -36,7 +38,12 @@ instance Monad Err where
Ok a >>= f = f a Ok a >>= f = f a
Bad s >>= f = Bad s Bad s >>= f = Bad s
instance MonadFail Err where #if !(MIN_VERSION_base(4,13,0))
-- Monad(fail) will be removed in GHC 8.8+
fail = Fail.fail
#endif
instance Fail.MonadFail Err where
fail = Bad fail = Bad

View File

@@ -53,6 +53,7 @@ import Control.Monad (liftM,liftM2) --,ap
import GF.Data.ErrM import GF.Data.ErrM
import GF.Data.Relation import GF.Data.Relation
import qualified Control.Monad.Fail as Fail
infixr 5 +++ infixr 5 +++
infixr 5 ++- infixr 5 ++-
@@ -88,10 +89,10 @@ checkUnique ss = ["overloaded" +++ show s | s <- nub overloads] where
overloaded s = length (filter (==s) ss) > 1 overloaded s = length (filter (==s) ss) > 1
-- | this is what happens when matching two values in the same module -- | this is what happens when matching two values in the same module
unifyMaybe :: (Eq a, MonadFail m) => Maybe a -> Maybe a -> m (Maybe a) unifyMaybe :: (Eq a, Fail.MonadFail m) => Maybe a -> Maybe a -> m (Maybe a)
unifyMaybe = unifyMaybeBy id unifyMaybe = unifyMaybeBy id
unifyMaybeBy :: (Eq b, MonadFail m) => (a->b) -> Maybe a -> Maybe a -> m (Maybe a) unifyMaybeBy :: (Eq b, Fail.MonadFail m) => (a->b) -> Maybe a -> Maybe a -> m (Maybe a)
unifyMaybeBy f (Just p1) (Just p2) unifyMaybeBy f (Just p1) (Just p2)
| f p1==f p2 = return (Just p1) | f p1==f p2 = return (Just p1)
| otherwise = fail "" | otherwise = fail ""

View File

@@ -19,6 +19,7 @@ import qualified Data.Map as Map
import Data.Word(Word8) import Data.Word(Word8)
import Data.Char(readLitChar) import Data.Char(readLitChar)
--import Debug.Trace(trace) --import Debug.Trace(trace)
import qualified Control.Monad.Fail as Fail
} }
@@ -285,7 +286,7 @@ instance Monad P where
PFailed posn err -> PFailed posn err PFailed posn err -> PFailed posn err
instance MonadFail P where instance Fail.MonadFail P where
fail msg = P $ \(_,AI posn _ _) -> PFailed posn msg fail msg = P $ \(_,AI posn _ _) -> PFailed posn msg

View File

@@ -32,6 +32,7 @@ import Control.Monad (liftM, liftM2, liftM3)
import Data.List (sortBy,nub) import Data.List (sortBy,nub)
import Data.Monoid import Data.Monoid
import GF.Text.Pretty(render,(<+>),hsep,fsep) import GF.Text.Pretty(render,(<+>),hsep,fsep)
import qualified Control.Monad.Fail as Fail
-- ** Functions for constructing and analysing source code terms. -- ** Functions for constructing and analysing source code terms.
@@ -237,7 +238,7 @@ isPredefConstant t = case t of
Q (mod,_) | mod == cPredef || mod == cPredefAbs -> True Q (mod,_) | mod == cPredef || mod == cPredefAbs -> True
_ -> False _ -> False
checkPredefError :: MonadFail m => Term -> m Term checkPredefError :: Fail.MonadFail m => Term -> m Term
checkPredefError t = checkPredefError t =
case t of case t of
Error s -> fail ("Error: "++s) Error s -> fail ("Error: "++s)

View File

@@ -32,6 +32,7 @@ import System.FilePath(makeRelative)
import Control.Parallel.Strategies(parList,rseq,using) import Control.Parallel.Strategies(parList,rseq,using)
import Control.Monad(liftM,ap) import Control.Monad(liftM,ap)
import Control.Applicative(Applicative(..)) import Control.Applicative(Applicative(..))
import qualified Control.Monad.Fail as Fail
type Message = Doc type Message = Doc
type Error = Message type Error = Message
@@ -53,7 +54,7 @@ instance Monad Check where
(ws,Success x) -> unCheck (g x) {-ctxt-} ws (ws,Success x) -> unCheck (g x) {-ctxt-} ws
(ws,Fail msg) -> (ws,Fail msg) (ws,Fail msg) -> (ws,Fail msg)
instance MonadFail Check where instance Fail.MonadFail Check where
fail = raise fail = raise
instance Applicative Check where instance Applicative Check where

View File

@@ -44,6 +44,7 @@ import Data.Set (Set)
import qualified Data.Set as Set import qualified Data.Set as Set
import PGF.Internal(Literal(..)) import PGF.Internal(Literal(..))
import qualified Control.Monad.Fail as Fail
usageHeader :: String usageHeader :: String
usageHeader = unlines usageHeader = unlines
@@ -547,7 +548,7 @@ lookupShow xs z = fromMaybe "lookupShow" $ lookup z [(y,x) | (x,y) <- xs]
lookupReadsPrec :: [(String,a)] -> Int -> ReadS a lookupReadsPrec :: [(String,a)] -> Int -> ReadS a
lookupReadsPrec xs _ s = [(z,rest) | (x,rest) <- lex s, (y,z) <- xs, y == x] lookupReadsPrec xs _ s = [(z,rest) | (x,rest) <- lex s, (y,z) <- xs, y == x]
onOff :: MonadFail m => (Bool -> m a) -> Bool -> ArgDescr (m a) onOff :: Fail.MonadFail m => (Bool -> m a) -> Bool -> ArgDescr (m a)
onOff f def = OptArg g "[on,off]" onOff f def = OptArg g "[on,off]"
where g ma = maybe (return def) readOnOff ma >>= f where g ma = maybe (return def) readOnOff ma >>= f
readOnOff x = case map toLower x of readOnOff x = case map toLower x of
@@ -555,7 +556,7 @@ onOff f def = OptArg g "[on,off]"
"off" -> return False "off" -> return False
_ -> fail $ "Expected [on,off], got: " ++ show x _ -> fail $ "Expected [on,off], got: " ++ show x
readOutputFormat :: MonadFail m => String -> m OutputFormat readOutputFormat :: Fail.MonadFail m => String -> m OutputFormat
readOutputFormat s = readOutputFormat s =
maybe (fail $ "Unknown output format: " ++ show s) return $ lookup s outputFormats maybe (fail $ "Unknown output format: " ++ show s) return $ lookup s outputFormats

View File

@@ -42,6 +42,7 @@ import qualified GF.Command.Importing as GF(importGrammar, importSource)
#ifdef C_RUNTIME #ifdef C_RUNTIME
import qualified PGF2 import qualified PGF2
#endif #endif
import qualified Control.Monad.Fail as Fail
-- * The SIO monad -- * The SIO monad
@@ -58,7 +59,7 @@ instance Monad SIO where
return x = SIO (const (return x)) return x = SIO (const (return x))
SIO m1 >>= xm2 = SIO $ \ h -> m1 h >>= \ x -> unS (xm2 x) h SIO m1 >>= xm2 = SIO $ \ h -> m1 h >>= \ x -> unS (xm2 x) h
instance MonadFail SIO where instance Fail.MonadFail SIO where
fail = liftSIO . fail fail = liftSIO . fail
instance Output SIO where instance Output SIO where