X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Futils%2FIOEnv.hs;h=224147f6671a6d40f2f100fc8536e520149ef3b5;hb=36fa8c4890e439fe8c2a4682df2a877fa2cc606b;hp=d01588f47b6b36f5728dd4451b1d3dba76af5acb;hpb=ad94d40948668032189ad22a0ad741ac1f645f50;p=ghc-hetmet.git diff --git a/compiler/utils/IOEnv.hs b/compiler/utils/IOEnv.hs index d01588f..224147f 100644 --- a/compiler/utils/IOEnv.hs +++ b/compiler/utils/IOEnv.hs @@ -1,23 +1,23 @@ --- --- (c) The University of Glasgow 2002-2006 --- --- The IO Monad with an environment --- - {-# 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/CodingStyle#Warnings +-- 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 +-- + module IOEnv ( IOEnv, -- Instance of Monad -- Standard combinators, specialised returnM, thenM, thenM_, failM, failWithM, mappM, mappM_, mapSndM, sequenceM, sequenceM_, - foldlM, foldrM, + foldlM, foldrM, anyM, mapAndUnzipM, mapAndUnzip3M, checkM, ifM, zipWithM, zipWithM_, @@ -28,17 +28,16 @@ module IOEnv ( tryM, tryAllM, tryMostM, fixM, -- I/O operations - ioToIOEnv, IORef, newMutVar, readMutVar, writeMutVar, updMutVar ) where #include "HsVersions.h" import Panic ( try, tryUser, tryMost, Exception(..) ) -import Data.IORef ( IORef, newIORef, readIORef, writeIORef ) +import Data.IORef ( IORef, newIORef, readIORef, writeIORef, modifyIORef ) import System.IO.Unsafe ( unsafeInterleaveIO ) import System.IO ( fixIO ) - +import MonadUtils ---------------------------------------------------------------------- -- Defining the monad type @@ -49,13 +48,17 @@ newtype IOEnv env a = IOEnv (env -> IO a) unIOEnv (IOEnv m) = m instance Monad (IOEnv m) where - (>>=) = thenM - (>>) = thenM_ - return = returnM - fail s = failM -- Ignore the string + (>>=) = thenM + (>>) = thenM_ + return = returnM + fail s = failM -- Ignore the string + +instance Applicative (IOEnv m) where + pure = returnM + IOEnv f <*> IOEnv x = IOEnv (\ env -> f env <*> x env ) instance Functor (IOEnv m) where - fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env)) + fmap f (IOEnv m) = IOEnv (\ env -> fmap f (m env)) returnM :: a -> IOEnv env a returnM a = IOEnv (\ env -> return a) @@ -99,7 +102,9 @@ fixM f = IOEnv (\ env -> fixIO (\ r -> unIOEnv (f r) env)) --------------------------- 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 @@ -124,20 +129,20 @@ unsafeInterleaveM (IOEnv m) = IOEnv (\ env -> unsafeInterleaveIO (m env)) -- Accessing input/output ---------------------------------------------------------------------- -ioToIOEnv :: IO a -> IOEnv env a -ioToIOEnv io = IOEnv (\ env -> io) +instance MonadIO (IOEnv env) where + liftIO io = IOEnv (\ env -> io) newMutVar :: a -> IOEnv env (IORef a) -newMutVar val = IOEnv (\ env -> newIORef val) +newMutVar val = liftIO (newIORef val) writeMutVar :: IORef a -> a -> IOEnv env () -writeMutVar var val = IOEnv (\ env -> writeIORef var val) +writeMutVar var val = liftIO (writeIORef var val) readMutVar :: IORef a -> IOEnv env a -readMutVar var = IOEnv (\ env -> readIORef var) +readMutVar var = liftIO (readIORef var) -updMutVar :: IORef a -> (a->a) -> IOEnv env () -updMutVar var upd_fn = IOEnv (\ env -> do { v <- readIORef var; writeIORef var (upd_fn v) }) +updMutVar :: IORef a -> (a -> a) -> IOEnv env () +updMutVar var upd = liftIO (modifyIORef var upd) ---------------------------------------------------------------------- @@ -174,6 +179,7 @@ mapAndUnzipM :: (a -> IOEnv env (b,c)) -> [a] -> IOEnv env ([b],[c]) 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) } @@ -184,6 +190,10 @@ mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):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 []