-{-# OPTIONS -w #-}
--- The above warning supression flag is a temporary kludge.
--- While working on this module you are encouraged to remove it and fix
--- any warnings in the module. See
--- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
--- for details
-
--
-- (c) The University of Glasgow 2002-2006
--
-- The IO Monad with an environment
--
+{-# LANGUAGE UndecidableInstances #-}
module IOEnv (
- IOEnv, -- Instance of Monad
+ IOEnv, -- Instance of Monad
- -- Monad utilities
- module MonadUtils,
+ -- Monad utilities
+ module MonadUtils,
- -- Errors
- failM, failWithM,
+ -- Errors
+ failM, failWithM,
+ IOEnvFailure(..),
- -- Getting at the environment
- getEnv, setEnv, updEnv,
+ -- Getting at the environment
+ getEnv, setEnv, updEnv,
- runIOEnv, unsafeInterleaveM,
- tryM, tryAllM, tryMostM, fixM,
+ runIOEnv, unsafeInterleaveM,
+ tryM, tryAllM, tryMostM, fixM,
- -- I/O operations
- IORef, newMutVar, readMutVar, writeMutVar, updMutVar
+ -- I/O operations
+ IORef, newMutVar, readMutVar, writeMutVar, updMutVar,
+ atomicUpdMutVar, atomicUpdMutVar'
) where
-#include "HsVersions.h"
-import Panic ( try, tryUser, tryMost, Exception(..) )
+import Exception
+import Panic
-import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef )
-import System.IO.Unsafe ( unsafeInterleaveIO )
-import System.IO ( fixIO )
+import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef,
+ atomicModifyIORef )
+import Data.Typeable
+import System.IO.Unsafe ( unsafeInterleaveIO )
+import System.IO ( fixIO )
+import Control.Monad
import MonadUtils
----------------------------------------------------------------------
--- Defining the monad type
+-- Defining the monad type
----------------------------------------------------------------------
newtype IOEnv env a = IOEnv (env -> IO a)
+
+unIOEnv :: IOEnv env a -> (env -> IO a)
unIOEnv (IOEnv m) = m
instance Monad (IOEnv m) where
(>>=) = thenM
(>>) = thenM_
return = returnM
- fail s = failM -- Ignore the string
+ fail _ = failM -- Ignore the string
instance Applicative (IOEnv m) where
pure = returnM
fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
returnM :: a -> IOEnv env a
-returnM a = IOEnv (\ env -> return 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 })
+ 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 })
+thenM_ (IOEnv m) f = IOEnv (\ env -> do { _ <- m env ; unIOEnv f env })
failM :: IOEnv env a
-failM = IOEnv (\ env -> ioError (userError "IOEnv failure"))
+failM = IOEnv (\ _ -> throwIO IOEnvFailure)
failWithM :: String -> IOEnv env a
-failWithM s = IOEnv (\ env -> ioError (userError s))
+failWithM s = IOEnv (\ _ -> ioError (userError s))
+
+data IOEnvFailure = IOEnvFailure
+ deriving Typeable
+instance Show IOEnvFailure where
+ show IOEnvFailure = "IOEnv failure"
+instance Exception IOEnvFailure
----------------------------------------------------------------------
--- Fundmantal combinators specific to the monad
+-- Fundmantal combinators specific to the monad
----------------------------------------------------------------------
---------------------------
-tryM :: IOEnv env r -> IOEnv env (Either Exception r)
+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
+-- not be caught here, else they'll be reported as errors in the program
-- begin compiled!
-tryM (IOEnv thing) = IOEnv (\ env -> tryUser (thing env))
+tryM (IOEnv thing) = IOEnv (\ env -> tryIOEnvFailure (thing env))
-tryAllM :: IOEnv env r -> IOEnv env (Either Exception r)
+tryIOEnvFailure :: IO a -> IO (Either IOEnvFailure a)
+tryIOEnvFailure = 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 thing) = IOEnv (\ env -> try (thing env))
-tryMostM :: IOEnv env r -> IOEnv env (Either Exception r)
+tryMostM :: IOEnv env r -> IOEnv env (Either SomeException r)
tryMostM (IOEnv thing) = IOEnv (\ env -> tryMost (thing env))
---------------------------
----------------------------------------------------------------------
--- Accessing input/output
+-- MonadPlus
+----------------------------------------------------------------------
+
+-- For use if the user has imported Control.Monad.Error from MTL
+-- Requires UndecidableInstances
+instance MonadPlus IO => MonadPlus (IOEnv env) where
+ mzero = IOEnv (const mzero)
+ m `mplus` n = IOEnv (\env -> unIOEnv m env `mplus` unIOEnv n env)
+
+----------------------------------------------------------------------
+-- Accessing input/output
----------------------------------------------------------------------
instance MonadIO (IOEnv env) where
- liftIO io = IOEnv (\ env -> io)
+ liftIO io = IOEnv (\ _ -> io)
newMutVar :: a -> IOEnv env (IORef a)
newMutVar val = liftIO (newIORef val)
updMutVar :: IORef a -> (a -> a) -> IOEnv env ()
updMutVar var upd = liftIO (modifyIORef var 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 var upd = liftIO (atomicModifyIORef var upd)
+
+-- | Strict variant of 'atomicUpdMutVar'.
+atomicUpdMutVar' :: IORef a -> (a -> (a, b)) -> IOEnv env b
+atomicUpdMutVar' var upd = do
+ r <- atomicUpdMutVar var upd
+ _ <- liftIO . evaluate =<< readMutVar var
+ return r
----------------------------------------------------------------------
--- Accessing the environment
+-- Accessing the environment
----------------------------------------------------------------------
getEnv :: IOEnv env env
-- | Perform a computation with a different environment
setEnv :: env' -> IOEnv env' a -> IOEnv env a
{-# INLINE setEnv #-}
-setEnv new_env (IOEnv m) = IOEnv (\ env -> m new_env)
+setEnv new_env (IOEnv m) = IOEnv (\ _ -> m new_env)
-- | Perform a computation with an altered environment
updEnv :: (env -> env') -> IOEnv env' a -> IOEnv env a
----------------------------------------------------------------------
--- Standard combinators, but specialised for this monad
--- (for efficiency)
+-- Standard combinators, but specialised for this monad
+-- (for efficiency)
----------------------------------------------------------------------
-{-# -- SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
-{-# -- SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
-{-# -- SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
-{-# -- SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
-{-# -- SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
-{-# -- SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
-{-# -- SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
-{-# -- SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
-{-# -- SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
-{-# -- SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
-{-# -- SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
-{-# -- SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
-{-# -- SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
-{-# -- SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}
+-- {-# SPECIALIZE mapM :: (a -> IOEnv env b) -> [a] -> IOEnv env [b] #-}
+-- {-# SPECIALIZE mapM_ :: (a -> IOEnv env b) -> [a] -> IOEnv env () #-}
+-- {-# SPECIALIZE mapSndM :: (b -> IOEnv env c) -> [(a,b)] -> IOEnv env [(a,c)] #-}
+-- {-# SPECIALIZE sequence :: [IOEnv env a] -> IOEnv env [a] #-}
+-- {-# SPECIALIZE sequence_ :: [IOEnv env a] -> IOEnv env () #-}
+-- {-# SPECIALIZE foldlM :: (a -> b -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
+-- {-# SPECIALIZE foldrM :: (b -> a -> IOEnv env a) -> a -> [b] -> IOEnv env a #-}
+-- {-# SPECIALIZE mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) #-}
+-- {-# SPECIALIZE mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d]) #-}
+-- {-# SPECIALIZE zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c] #-}
+-- {-# SPECIALIZE zipWithM_ :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env () #-}
+-- {-# SPECIALIZE anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool #-}
+-- {-# SPECIALIZE when :: Bool -> IOEnv env a -> IOEnv env () #-}
+-- {-# SPECIALIZE unless :: Bool -> IOEnv env a -> IOEnv env () #-}