--- (c) The University of Glasgow 2002
+{-# 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
--
-- Standard combinators, specialised
returnM, thenM, thenM_, failM, failWithM,
mappM, mappM_, mapSndM, sequenceM, sequenceM_,
- foldlM, foldrM,
+ foldlM, foldrM, anyM,
mapAndUnzipM, mapAndUnzip3M,
checkM, ifM, zipWithM, zipWithM_,
getEnv, setEnv, updEnv,
runIOEnv, unsafeInterleaveM,
- tryM, tryAllM, fixM,
+ tryM, tryAllM, tryMostM, fixM,
-- I/O operations
ioToIOEnv,
) where
#include "HsVersions.h"
-import Panic ( try, tryUser, Exception(..) )
-import DATA_IOREF ( IORef, newIORef, readIORef, writeIORef )
-import UNSAFE_IO ( unsafeInterleaveIO )
-import FIX_IO ( fixIO )
+import Panic ( try, tryUser, tryMost, Exception(..) )
+
+import Data.IORef ( IORef, newIORef, readIORef, writeIORef )
+import System.IO.Unsafe ( unsafeInterleaveIO )
+import System.IO ( fixIO )
----------------------------------------------------------------------
return = returnM
fail s = failM -- Ignore the string
+instance Functor (IOEnv m) where
+ fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env))
+
returnM :: a -> IOEnv env a
returnM a = IOEnv (\ env -> return a)
---------------------------
tryM :: IOEnv env r -> IOEnv env (Either Exception r)
--- Reflect UserError exceptions into IOEnv monad
+-- 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
-- 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 thing) = IOEnv (\ env -> tryMost (thing env))
+
---------------------------
unsafeInterleaveM :: IOEnv env a -> IOEnv env a
unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env))
mapAndUnzip3M :: (a -> IOEnv env (b,c,d)) -> [a] -> IOEnv env ([b],[c],[d])
checkM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is False
ifM :: Bool -> IOEnv env a -> IOEnv env () -- Perform arg if bool is True
+anyM :: (a -> IOEnv env Bool) -> [a] -> IOEnv env Bool
mappM f [] = return []
mappM f (x:xs) = do { r <- f x; rs <- mappM f xs; return (r:rs) }
mappM_ f [] = return ()
mappM_ f (x:xs) = f x >> mappM_ f xs
+anyM f [] = return False
+anyM f (x:xs) = do { b <- f x; if b then return True
+ else anyM f xs }
+
zipWithM :: (a -> b -> IOEnv env c) -> [a] -> [b] -> IOEnv env [c]
zipWithM f [] bs = return []
zipWithM f as [] = return []