{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DerivingVia #-}
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(..))
import Control.Concurrent.MVar (newEmptyMVar, readMVar, putMVar)
import Control.Concurrent (forkIO, killThread)
newtype IOEnv env a = IOEnv (env -> IO a)
deriving ((forall a b. (a -> b) -> IOEnv env a -> IOEnv env b)
-> (forall a b. a -> IOEnv env b -> IOEnv env a)
-> Functor (IOEnv env)
forall a b. a -> IOEnv env b -> IOEnv env a
forall a b. (a -> b) -> IOEnv env a -> IOEnv env b
forall env a b. a -> IOEnv env b -> IOEnv env a
forall env a b. (a -> b) -> IOEnv env a -> IOEnv env b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> IOEnv env b -> IOEnv env a
$c<$ :: forall env a b. a -> IOEnv env b -> IOEnv env a
fmap :: forall a b. (a -> b) -> IOEnv env a -> IOEnv env b
$cfmap :: forall env a b. (a -> b) -> IOEnv env a -> IOEnv env b
Functor)
deriving (Monad (IOEnv env)
Monad (IOEnv env)
-> (forall e a. Exception e => e -> IOEnv env a)
-> MonadThrow (IOEnv env)
forall {env}. Monad (IOEnv env)
forall e a. Exception e => e -> IOEnv env a
forall env e a. Exception e => e -> IOEnv env a
forall (m :: * -> *).
Monad m -> (forall e a. Exception e => e -> m a) -> MonadThrow m
throwM :: forall e a. Exception e => e -> IOEnv env a
$cthrowM :: forall env e a. Exception e => e -> IOEnv env a
MonadThrow, MonadThrow (IOEnv env)
MonadThrow (IOEnv env)
-> (forall e a.
Exception e =>
IOEnv env a -> (e -> IOEnv env a) -> IOEnv env a)
-> MonadCatch (IOEnv env)
forall env. MonadThrow (IOEnv env)
forall e a.
Exception e =>
IOEnv env a -> (e -> IOEnv env a) -> IOEnv env a
forall env e a.
Exception e =>
IOEnv env a -> (e -> IOEnv env a) -> IOEnv env a
forall (m :: * -> *).
MonadThrow m
-> (forall e a. Exception e => m a -> (e -> m a) -> m a)
-> MonadCatch m
catch :: forall e a.
Exception e =>
IOEnv env a -> (e -> IOEnv env a) -> IOEnv env a
$ccatch :: forall env e a.
Exception e =>
IOEnv env a -> (e -> IOEnv env a) -> IOEnv env a
MonadCatch, MonadCatch (IOEnv env)
MonadCatch (IOEnv env)
-> (forall b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b)
-> (forall b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b)
-> (forall a b c.
IOEnv env a
-> (a -> ExitCase b -> IOEnv env c)
-> (a -> IOEnv env b)
-> IOEnv env (b, c))
-> MonadMask (IOEnv env)
forall env. MonadCatch (IOEnv env)
forall b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
forall env b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
forall a b c.
IOEnv env a
-> (a -> ExitCase b -> IOEnv env c)
-> (a -> IOEnv env b)
-> IOEnv env (b, c)
forall env a b c.
IOEnv env a
-> (a -> ExitCase b -> IOEnv env c)
-> (a -> IOEnv env b)
-> IOEnv env (b, c)
forall (m :: * -> *).
MonadCatch m
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall b. ((forall a. m a -> m a) -> m b) -> m b)
-> (forall a b c.
m a -> (a -> ExitCase b -> m c) -> (a -> m b) -> m (b, c))
-> MonadMask m
generalBracket :: forall a b c.
IOEnv env a
-> (a -> ExitCase b -> IOEnv env c)
-> (a -> IOEnv env b)
-> IOEnv env (b, c)
$cgeneralBracket :: forall env a b c.
IOEnv env a
-> (a -> ExitCase b -> IOEnv env c)
-> (a -> IOEnv env b)
-> IOEnv env (b, c)
uninterruptibleMask :: forall b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
$cuninterruptibleMask :: forall env b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
mask :: forall b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
$cmask :: forall env b.
((forall a. IOEnv env a -> IOEnv env a) -> IOEnv env b)
-> IOEnv env b
MonadMask, Monad (IOEnv env)
Monad (IOEnv env)
-> (forall a. IO a -> IOEnv env a) -> MonadIO (IOEnv env)
forall {env}. Monad (IOEnv env)
forall a. IO a -> IOEnv env a
forall env a. IO a -> IOEnv env a
forall (m :: * -> *).
Monad m -> (forall a. IO a -> m a) -> MonadIO m
liftIO :: forall a. IO a -> IOEnv env a
$cliftIO :: forall env a. IO a -> IOEnv env a
MonadIO) via (ReaderT env IO)
unIOEnv :: IOEnv env a -> (env -> IO a)
unIOEnv :: forall env a. IOEnv env a -> env -> IO a
unIOEnv (IOEnv env -> IO a
m) = env -> IO a
m
instance Monad (IOEnv m) where
>>= :: forall a b. IOEnv m a -> (a -> IOEnv m b) -> IOEnv m b
(>>=) = IOEnv m a -> (a -> IOEnv m b) -> IOEnv m b
forall m a b. IOEnv m a -> (a -> IOEnv m b) -> IOEnv m b
thenM
>> :: forall a b. IOEnv m a -> IOEnv m b -> IOEnv m b
(>>) = IOEnv m a -> IOEnv m b -> IOEnv m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
instance MonadFail (IOEnv m) where
fail :: forall a. String -> IOEnv m a
fail String
_ = IOEnv m a
forall env a. IOEnv env a
failM
instance Applicative (IOEnv m) where
pure :: forall a. a -> IOEnv m a
pure = a -> IOEnv m a
forall a env. a -> IOEnv env a
returnM
IOEnv m -> IO (a -> b)
f <*> :: forall a b. IOEnv m (a -> b) -> IOEnv m a -> IOEnv m b
<*> IOEnv m -> IO a
x = (m -> IO b) -> IOEnv m b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ m
env -> m -> IO (a -> b)
f m
env IO (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> m -> IO a
x m
env )
*> :: forall a b. IOEnv m a -> IOEnv m b -> IOEnv m b
(*>) = IOEnv m a -> IOEnv m b -> IOEnv m b
forall m a b. IOEnv m a -> IOEnv m b -> IOEnv m b
thenM_
returnM :: a -> IOEnv env a
returnM :: forall a env. a -> IOEnv env a
returnM a
a = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
_ -> a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a)
thenM :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
thenM :: forall m a b. IOEnv m a -> (a -> IOEnv m b) -> IOEnv m b
thenM (IOEnv env -> IO a
m) a -> IOEnv env b
f = (env -> IO b) -> IOEnv env b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> do { a
r <- env -> IO a
m env
env ;
IOEnv env b -> env -> IO b
forall env a. IOEnv env a -> env -> IO a
unIOEnv (a -> IOEnv env b
f a
r) env
env })
thenM_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
thenM_ :: forall m a b. IOEnv m a -> IOEnv m b -> IOEnv m b
thenM_ (IOEnv env -> IO a
m) IOEnv env b
f = (env -> IO b) -> IOEnv env b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> do { a
_ <- env -> IO a
m env
env ; IOEnv env b -> env -> IO b
forall env a. IOEnv env a -> env -> IO a
unIOEnv IOEnv env b
f env
env })
failM :: IOEnv env a
failM :: forall env a. IOEnv env a
failM = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
_ -> IOEnvFailure -> IO a
forall e a. Exception e => e -> IO a
throwIO IOEnvFailure
IOEnvFailure)
failWithM :: String -> IOEnv env a
failWithM :: forall m a. String -> IOEnv m a
failWithM String
s = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
_ -> IOError -> IO a
forall a. IOError -> IO a
ioError (String -> IOError
userError String
s))
data IOEnvFailure = IOEnvFailure
instance Show IOEnvFailure where
show :: IOEnvFailure -> String
show IOEnvFailure
IOEnvFailure = String
"IOEnv failure"
instance Exception IOEnvFailure
instance ContainsDynFlags env => HasDynFlags (IOEnv env) where
getDynFlags :: IOEnv env DynFlags
getDynFlags = do env
env <- IOEnv env env
forall env. IOEnv env env
getEnv
DynFlags -> IOEnv env DynFlags
forall (m :: * -> *) a. Monad m => a -> m a
return (DynFlags -> IOEnv env DynFlags) -> DynFlags -> IOEnv env DynFlags
forall a b. (a -> b) -> a -> b
$! env -> DynFlags
forall t. ContainsDynFlags t => t -> DynFlags
extractDynFlags env
env
instance ContainsModule env => HasModule (IOEnv env) where
getModule :: IOEnv env Module
getModule = do env
env <- IOEnv env env
forall env. IOEnv env env
getEnv
Module -> IOEnv env Module
forall (m :: * -> *) a. Monad m => a -> m a
return (Module -> IOEnv env Module) -> Module -> IOEnv env Module
forall a b. (a -> b) -> a -> b
$ env -> Module
forall t. ContainsModule t => t -> Module
extractModule env
env
runIOEnv :: env -> IOEnv env a -> IO a
runIOEnv :: forall env a. env -> IOEnv env a -> IO a
runIOEnv env
env (IOEnv env -> IO a
m) = env -> IO a
m env
env
{-# NOINLINE fixM #-}
fixM :: (a -> IOEnv env a) -> IOEnv env a
fixM :: forall a env. (a -> IOEnv env a) -> IOEnv env a
fixM a -> IOEnv env a
f = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> (a -> IO a) -> IO a
forall a. (a -> IO a) -> IO a
fixIO (\ a
r -> IOEnv env a -> env -> IO a
forall env a. IOEnv env a -> env -> IO a
unIOEnv (a -> IOEnv env a
f a
r) env
env))
tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM :: forall env r. IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM (IOEnv env -> IO r
thing) = (env -> IO (Either IOEnvFailure r))
-> IOEnv env (Either IOEnvFailure r)
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> IO r -> IO (Either IOEnvFailure r)
forall a. IO a -> IO (Either IOEnvFailure a)
tryIOEnvFailure (env -> IO r
thing env
env))
tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
tryIOEnvFailure :: forall a. IO a -> IO (Either IOEnvFailure a)
tryIOEnvFailure = IO a -> IO (Either IOEnvFailure a)
forall e a. Exception e => IO a -> IO (Either e a)
try
tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM :: forall env r. IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (IOEnv env -> IO r
thing) = (env -> IO (Either SomeException r))
-> IOEnv env (Either SomeException r)
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> IO r -> IO (Either SomeException r)
forall a. IO a -> IO (Either SomeException a)
safeTry (env -> IO r
thing env
env))
safeTry :: IO a -> IO (Either SomeException a)
safeTry :: forall a. IO a -> IO (Either SomeException a)
safeTry IO a
act = do
MVar (Either SomeException a)
var <- IO (MVar (Either SomeException a))
forall a. IO (MVar a)
newEmptyMVar
((forall a. IO a -> IO a) -> IO (Either SomeException a))
-> IO (Either SomeException a)
forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
uninterruptibleMask (((forall a. IO a -> IO a) -> IO (Either SomeException a))
-> IO (Either SomeException a))
-> ((forall a. IO a -> IO a) -> IO (Either SomeException a))
-> IO (Either SomeException a)
forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
restore -> do
ThreadId
t <- IO () -> IO ThreadId
forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ IO a -> IO (Either SomeException a)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO a -> IO a
forall a. IO a -> IO a
restore IO a
act) IO (Either SomeException a)
-> (Either SomeException a -> IO ()) -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MVar (Either SomeException a) -> Either SomeException a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Either SomeException a)
var
IO (Either SomeException a) -> IO (Either SomeException a)
forall a. IO a -> IO a
restore (MVar (Either SomeException a) -> IO (Either SomeException a)
forall a. MVar a -> IO a
readMVar MVar (Either SomeException a)
var)
IO (Either SomeException a)
-> (SomeException -> IO (Either SomeException a))
-> IO (Either SomeException a)
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \(SomeException
e :: SomeException) -> do
ThreadId -> IO ()
killThread ThreadId
t
SomeException -> IO (Either SomeException a)
forall e a. Exception e => e -> IO a
throwIO SomeException
e
tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM :: forall env r. IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM (IOEnv env -> IO r
thing) = (env -> IO (Either SomeException r))
-> IOEnv env (Either SomeException r)
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> IO r -> IO (Either SomeException r)
forall a. IO a -> IO (Either SomeException a)
tryMost (env -> IO r
thing env
env))
unsafeInterleaveM :: IOEnv env a -> IOEnv env a
unsafeInterleaveM :: forall env a. IOEnv env a -> IOEnv env a
unsafeInterleaveM (IOEnv env -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> IO a -> IO a
forall a. IO a -> IO a
unsafeInterleaveIO (env -> IO a
m env
env))
uninterruptibleMaskM_ :: IOEnv env a -> IOEnv env a
uninterruptibleMaskM_ :: forall env a. IOEnv env a -> IOEnv env a
uninterruptibleMaskM_ (IOEnv env -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> IO a -> IO a
forall a. IO a -> IO a
uninterruptibleMask_ (env -> IO a
m env
env))
instance Alternative (IOEnv env) where
empty :: forall a. IOEnv env a
empty = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (IO a -> env -> IO a
forall a b. a -> b -> a
const IO a
forall (f :: * -> *) a. Alternative f => f a
empty)
IOEnv env a
m <|> :: forall a. IOEnv env a -> IOEnv env a -> IOEnv env a
<|> IOEnv env a
n = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\env
env -> IOEnv env a -> env -> IO a
forall env a. IOEnv env a -> env -> IO a
unIOEnv IOEnv env a
m env
env IO a -> IO a -> IO a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> IOEnv env a -> env -> IO a
forall env a. IOEnv env a -> env -> IO a
unIOEnv IOEnv env a
n env
env)
instance MonadPlus (IOEnv env)
newMutVar :: a -> IOEnv env (IORef a)
newMutVar :: forall a env. a -> IOEnv env (IORef a)
newMutVar a
val = IO (IORef a) -> IOEnv env (IORef a)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (a -> IO (IORef a)
forall a. a -> IO (IORef a)
newIORef a
val)
writeMutVar :: IORef a -> a -> IOEnv env ()
writeMutVar :: forall a env. IORef a -> a -> IOEnv env ()
writeMutVar IORef a
var a
val = IO () -> IOEnv env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> a -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef a
var a
val)
readMutVar :: IORef a -> IOEnv env a
readMutVar :: forall a env. IORef a -> IOEnv env a
readMutVar IORef a
var = IO a -> IOEnv env a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> IO a
forall a. IORef a -> IO a
readIORef IORef a
var)
updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
updMutVar :: forall a env. IORef a -> (a -> a) -> IOEnv env ()
updMutVar IORef a
var a -> a
upd = IO () -> IOEnv env ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> (a -> a) -> IO ()
forall a. IORef a -> (a -> a) -> IO ()
modifyIORef IORef a
var a -> a
upd)
atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar :: forall a b env. IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar IORef a
var a -> (a, b)
upd = IO b -> IOEnv env b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> (a -> (a, b)) -> IO b
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef IORef a
var a -> (a, b)
upd)
atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar' :: forall a b env. IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar' IORef a
var a -> (a, b)
upd = IO b -> IOEnv env b
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IORef a -> (a -> (a, b)) -> IO b
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef a
var a -> (a, b)
upd)
getEnv :: IOEnv env env
{-# INLINE getEnv #-}
getEnv :: forall env. IOEnv env env
getEnv = (env -> IO env) -> IOEnv env env
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> env -> IO env
forall (m :: * -> *) a. Monad m => a -> m a
return env
env)
setEnv :: env' -> IOEnv env' a -> IOEnv env a
{-# INLINE setEnv #-}
setEnv :: forall env' a env. env' -> IOEnv env' a -> IOEnv env a
setEnv env'
new_env (IOEnv env' -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
_ -> env' -> IO a
m env'
new_env)
updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
{-# INLINE updEnv #-}
updEnv :: forall env env' a. (env -> env') -> IOEnv env' a -> IOEnv env a
updEnv env -> env'
upd (IOEnv env' -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env
env -> env' -> IO a
m (env -> env'
upd env
env))