{-# LANGUAGE CPP #-}
--
-- (c) The University of Glasgow 2002-2006
--
-- The IO Monad with an environment
--
-- The environment is passed around as a Reader monad but
-- as its in the IO monad, mutable references can be used
-- for updating state.
--

module IOEnv (
        IOEnv, -- Instance of Monad

        -- Monad utilities
        module MonadUtils,

        -- Errors
        failM, failWithM,
        IOEnvFailure(..),

        -- Getting at the environment
        getEnv, setEnv, updEnv,

        runIOEnv, unsafeInterleaveM, uninterruptibleMaskM_,
        tryM, tryAllM, tryMostM, fixM,

        -- I/O operations
        IORef, newMutVar, readMutVar, writeMutVar, updMutVar,
        atomicUpdMutVar, atomicUpdMutVar'
  ) where

import GhcPrelude

import DynFlags
import Exception
import Module
import Panic

import Data.IORef       ( IORef, newIORef, readIORef, writeIORef, modifyIORef,
                          atomicModifyIORef, atomicModifyIORef' )
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.IO        ( fixIO )
import Control.Monad
import qualified Control.Monad.Fail as MonadFail
import MonadUtils
import Control.Applicative (Alternative(..))

----------------------------------------------------------------------
-- Defining the monad type
----------------------------------------------------------------------


newtype IOEnv env a = IOEnv (env -> IO a)

unIOEnv :: IOEnv env a -> (env -> IO a)
unIOEnv :: IOEnv env a -> env -> IO a
unIOEnv (IOEnv m :: env -> IO a
m) = env -> IO a
m

instance Monad (IOEnv m) where
    >>= :: 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
    >> :: 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
(*>)
#if !MIN_VERSION_base(4,13,0)
    fail   = MonadFail.fail
#endif

instance MonadFail.MonadFail (IOEnv m) where
    fail :: String -> IOEnv m a
fail _ = IOEnv m a
forall env a. IOEnv env a
failM -- Ignore the string

instance Applicative (IOEnv m) where
    pure :: a -> IOEnv m a
pure = a -> IOEnv m a
forall a env. a -> IOEnv env a
returnM
    IOEnv f :: m -> IO (a -> b)
f <*> :: IOEnv m (a -> b) -> IOEnv m a -> IOEnv m b
<*> IOEnv x :: m -> IO a
x = (m -> IO b) -> IOEnv m b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: 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 )
    *> :: 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_

instance Functor (IOEnv m) where
    fmap :: (a -> b) -> IOEnv m a -> IOEnv m b
fmap f :: a -> b
f (IOEnv m :: m -> IO a
m) = (m -> IO b) -> IOEnv m b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: m
env -> (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (m -> IO a
m m
env))

returnM :: a -> IOEnv env a
returnM :: a -> IOEnv env a
returnM a :: a
a = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ _ -> 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 :: IOEnv env a -> (a -> IOEnv env b) -> IOEnv env b
thenM (IOEnv m :: env -> IO a
m) f :: a -> IOEnv env b
f = (env -> IO b) -> IOEnv env b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: 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_ :: IOEnv env a -> IOEnv env b -> IOEnv env b
thenM_ (IOEnv m :: env -> IO a
m) f :: IOEnv env b
f = (env -> IO b) -> IOEnv env b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: 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 :: IOEnv env a
failM = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ _ -> IOEnvFailure -> IO a
forall e a. Exception e => e -> IO a
throwIO IOEnvFailure
IOEnvFailure)

failWithM :: String -> IOEnv env a
failWithM :: String -> IOEnv env a
failWithM s :: String
s = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ _ -> 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 = "IOEnv failure"

instance Exception IOEnvFailure

instance ExceptionMonad (IOEnv a) where
  gcatch :: IOEnv a a -> (e -> IOEnv a a) -> IOEnv a a
gcatch act :: IOEnv a a
act handle :: e -> IOEnv a a
handle =
      (a -> IO a) -> IOEnv a a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv ((a -> IO a) -> IOEnv a a) -> (a -> IO a) -> IOEnv a a
forall a b. (a -> b) -> a -> b
$ \s :: a
s -> IOEnv a a -> a -> IO a
forall env a. IOEnv env a -> env -> IO a
unIOEnv IOEnv a a
act a
s IO a -> (e -> IO a) -> IO a
forall (m :: * -> *) e a.
(ExceptionMonad m, Exception e) =>
m a -> (e -> m a) -> m a
`gcatch` \e :: e
e -> IOEnv a a -> a -> IO a
forall env a. IOEnv env a -> env -> IO a
unIOEnv (e -> IOEnv a a
handle e
e) a
s
  gmask :: ((IOEnv a a -> IOEnv a a) -> IOEnv a b) -> IOEnv a b
gmask f :: (IOEnv a a -> IOEnv a a) -> IOEnv a b
f =
      (a -> IO b) -> IOEnv a b
forall env a. (env -> IO a) -> IOEnv env a
IOEnv ((a -> IO b) -> IOEnv a b) -> (a -> IO b) -> IOEnv a b
forall a b. (a -> b) -> a -> b
$ \s :: a
s -> ((IO a -> IO a) -> IO b) -> IO b
forall (m :: * -> *) a b.
ExceptionMonad m =>
((m a -> m a) -> m b) -> m b
gmask (((IO a -> IO a) -> IO b) -> IO b)
-> ((IO a -> IO a) -> IO b) -> IO b
forall a b. (a -> b) -> a -> b
$ \io_restore :: IO a -> IO a
io_restore ->
                             let
                                g_restore :: IOEnv env a -> IOEnv env a
g_restore (IOEnv m :: env -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv ((env -> IO a) -> IOEnv env a) -> (env -> IO a) -> IOEnv env a
forall a b. (a -> b) -> a -> b
$ \s :: env
s -> IO a -> IO a
io_restore (env -> IO a
m env
s)
                             in
                                IOEnv a b -> a -> IO b
forall env a. IOEnv env a -> env -> IO a
unIOEnv ((IOEnv a a -> IOEnv a a) -> IOEnv a b
f IOEnv a a -> IOEnv a a
forall env. IOEnv env a -> IOEnv env a
g_restore) a
s

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

----------------------------------------------------------------------
-- Fundamental combinators specific to the monad
----------------------------------------------------------------------


---------------------------
runIOEnv :: env -> IOEnv env a -> IO a
runIOEnv :: env -> IOEnv env a -> IO a
runIOEnv env :: env
env (IOEnv m :: env -> IO a
m) = env -> IO a
m env
env


---------------------------
{-# NOINLINE fixM #-}
  -- Aargh!  Not inlining fixM alleviates a space leak problem.
  -- Normally fixM is used with a lazy tuple match: if the optimiser is
  -- shown the definition of fixM, it occasionally transforms the code
  -- in such a way that the code generator doesn't spot the selector
  -- thunks.  Sigh.

fixM :: (a -> IOEnv env a) -> IOEnv env a
fixM :: (a -> IOEnv env a) -> IOEnv env a
fixM f :: a -> IOEnv env a
f = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: env
env -> (a -> IO a) -> IO a
forall a. (a -> IO a) -> IO a
fixIO (\ r :: 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)
-- Reflect UserError exceptions (only) into IOEnv monad
-- Other exceptions are not caught; they are simply propagated as exns
--
-- The idea is that errors in the program being compiled will give rise
-- to UserErrors.  But, say, pattern-match failures in GHC itself should
-- not be caught here, else they'll be reported as errors in the program
-- begin compiled!
tryM :: IOEnv env r -> IOEnv env (Either IOEnvFailure r)
tryM (IOEnv thing :: 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
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 :: 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

-- XXX We shouldn't be catching everything, e.g. timeouts
tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
-- Catch *all* exceptions
-- This is used when running a Template-Haskell splice, when
-- even a pattern-match failure is a programmer error
tryAllM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryAllM (IOEnv thing :: 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
env -> IO r -> IO (Either SomeException r)
forall e a. Exception e => IO a -> IO (Either e a)
try (env -> IO r
thing env
env))

tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM (IOEnv thing :: 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
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 :: IOEnv env a -> IOEnv env a
unsafeInterleaveM (IOEnv m :: 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 -> IO a
forall a. IO a -> IO a
unsafeInterleaveIO (env -> IO a
m env
env))

uninterruptibleMaskM_ :: IOEnv env a -> IOEnv env a
uninterruptibleMaskM_ :: IOEnv env a -> IOEnv env a
uninterruptibleMaskM_ (IOEnv m :: 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 -> IO a
forall a. IO a -> IO a
uninterruptibleMask_ (env -> IO a
m env
env))

----------------------------------------------------------------------
-- Alternative/MonadPlus
----------------------------------------------------------------------

instance Alternative (IOEnv env) where
    empty :: 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)
    m :: IOEnv env a
m <|> :: IOEnv env a -> IOEnv env a -> IOEnv env a
<|> n :: IOEnv env a
n = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\env :: 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)

----------------------------------------------------------------------
-- Accessing input/output
----------------------------------------------------------------------

instance MonadIO (IOEnv env) where
    liftIO :: IO a -> IOEnv env a
liftIO io :: IO a
io = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ _ -> IO a
io)

newMutVar :: a -> IOEnv env (IORef a)
newMutVar :: a -> IOEnv env (IORef a)
newMutVar val :: 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 :: IORef a -> a -> IOEnv env ()
writeMutVar var :: IORef a
var val :: 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 :: IORef a -> IOEnv env a
readMutVar var :: 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 :: IORef a -> (a -> a) -> IOEnv env ()
updMutVar var :: IORef a
var upd :: 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)

-- | Atomically update the reference.  Does not force the evaluation of the
-- new variable contents.  For strict update, use 'atomicUpdMutVar''.
atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar var :: IORef a
var upd :: 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)

-- | Strict variant of 'atomicUpdMutVar'.
atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
atomicUpdMutVar' var :: IORef a
var upd :: 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)

----------------------------------------------------------------------
-- Accessing the environment
----------------------------------------------------------------------

getEnv :: IOEnv env env
{-# INLINE getEnv #-}
getEnv :: IOEnv env env
getEnv = (env -> IO env) -> IOEnv env env
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: env
env -> env -> IO env
forall (m :: * -> *) a. Monad m => a -> m a
return env
env)

-- | Perform a computation with a different environment
setEnv :: env' -> IOEnv env' a -> IOEnv env a
{-# INLINE setEnv #-}
setEnv :: env' -> IOEnv env' a -> IOEnv env a
setEnv new_env :: env'
new_env (IOEnv m :: env' -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ _ -> env' -> IO a
m env'
new_env)

-- | Perform a computation with an altered environment
updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
{-# INLINE updEnv #-}
updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
updEnv upd :: env -> env'
upd (IOEnv m :: env' -> IO a
m) = (env -> IO a) -> IOEnv env a
forall env a. (env -> IO a) -> IOEnv env a
IOEnv (\ env :: env
env -> env' -> IO a
m (env -> env'
upd env
env))