module GHC.Data.IOEnv (
IOEnv,
module GHC.Utils.Monad,
failM, failWithM,
IOEnvFailure(..),
getEnv, setEnv, updEnv,
runIOEnv, unsafeInterleaveM, uninterruptibleMaskM_,
tryM, tryAllM, tryMostM, fixM,
IORef, newMutVar, readMutVar, writeMutVar, updMutVar,
atomicUpdMutVar, atomicUpdMutVar'
) where
import GHC.Prelude
import GHC.Driver.Session
import GHC.Utils.Exception
import GHC.Unit.Module
import GHC.Utils.Panic
import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef,
atomicModifyIORef, atomicModifyIORef' )
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.IO ( fixIO )
import Control.Monad
import Control.Monad.Trans.Reader
import Control.Monad.Catch (MonadCatch, MonadMask, MonadThrow)
import GHC.Utils.Monad
import Control.Applicative (Alternative(..))
newtype IOEnv env a = IOEnv (env -> IO a)
deriving (Functor)
deriving (MonadThrow, MonadCatch, MonadMask, MonadIO) via (ReaderT env IO)
unIOEnv :: IOEnv env a -> (env -> IO a)
unIOEnv (IOEnv m) = m
instance Monad (IOEnv m) where
(>>=) = thenM
(>>) = (*>)
instance MonadFail (IOEnv m) where
fail _ = failM
instance Applicative (IOEnv m) where
pure = returnM
IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env )
(*>) = thenM_
returnM :: a -> IOEnv env a
returnM a = IOEnv (\ _ -> return a)
thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
thenM (IOEnv m) f = IOEnv (\ env -> do { r <- m env ;
unIOEnv (f r) env })
thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
thenM_ (IOEnv m) f = IOEnv (\ env -> do { _ <- m env ; unIOEnv f env })
failM :: IOEnv env a
failM = IOEnv (\ _ -> throwIO IOEnvFailure)
failWithM :: String -> IOEnv env a
failWithM s = IOEnv (\ _ -> ioError (userError s))
data IOEnvFailure = IOEnvFailure
instance Show IOEnvFailure where
show IOEnvFailure = "IOEnv failure"
instance Exception IOEnvFailure
instance ContainsDynFlags env => HasDynFlags (IOEnv env) where
getDynFlags = do env <- getEnv
return $! extractDynFlags env
instance ContainsModule env => HasModule (IOEnv env) where
getModule = do env <- getEnv
return $ extractModule env
runIOEnv :: env -> IOEnv env a -> IO a
runIOEnv env (IOEnv m) = m env
fixM :: (a -> IOEnv env a) -> IOEnv env a
fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env))
tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
tryIOEnvFailure = try
tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (IOEnv thing) = IOEnv (\ env -> try (thing env))
tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
unsafeInterleaveM :: IOEnv env a -> IOEnv env a
unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
uninterruptibleMaskM_ :: IOEnv env a -> IOEnv env a
uninterruptibleMaskM_ (IOEnv m) = IOEnv (\ env -> uninterruptibleMask_ (m env))
instance Alternative (IOEnv env) where
empty = IOEnv (const empty)
m <|> n = IOEnv (\env -> unIOEnv m env <|> unIOEnv n env)
instance MonadPlus (IOEnv env)
newMutVar :: a -> IOEnv env (IORef a)
newMutVar val = liftIO (newIORef val)
writeMutVar :: IORef a -> a -> IOEnv env ()
writeMutVar var val = liftIO (writeIORef var val)
readMutVar :: IORef a -> IOEnv env a
readMutVar var = liftIO (readIORef var)
updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
updMutVar var upd = liftIO (modifyIORef var upd)
atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar var upd = liftIO (atomicModifyIORef var upd)
atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar' var upd = liftIO (atomicModifyIORef' var upd)
getEnv :: IOEnv env env
getEnv = IOEnv (\ env -> return env)
setEnv :: env' -> IOEnv env' a -> IOEnv env a
setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
updEnv upd (IOEnv m) = IOEnv (\ env -> m (upd env))