summaryrefslogtreecommitdiff
path: root/compiler/utils/MonadUtils.hs
diff options
context:
space:
mode:
authorsimonpj@microsoft.com <unknown>2008-10-30 12:51:08 +0000
committersimonpj@microsoft.com <unknown>2008-10-30 12:51:08 +0000
commit9bcd95bad83ee937c178970e8b729732e680fe1e (patch)
treee0cbcf15a961d05da7b12b45b9aaf0efb4672338 /compiler/utils/MonadUtils.hs
parentb1f3ff48870a3a4670cb41b890b78bbfffa8a32e (diff)
downloadhaskell-9bcd95bad83ee937c178970e8b729732e680fe1e.tar.gz
Add (a) CoreM monad, (b) new Annotations feature
This patch, written by Max Bolingbroke, does two things 1. It adds a new CoreM monad (defined in simplCore/CoreMonad), which is used as the top-level monad for all the Core-to-Core transformations (starting at SimplCore). It supports * I/O (for debug printing) * Unique supply * Statistics gathering * Access to the HscEnv, RuleBase, Annotations, Module The patch therefore refactors the top "skin" of every Core-to-Core pass, but does not change their functionality. 2. It adds a completely new facility to GHC: Core "annotations". The idea is that you can say {#- ANN foo (Just "Hello") #-} which adds the annotation (Just "Hello") to the top level function foo. These annotations can be looked up in any Core-to-Core pass, and are persisted into interface files. (Hence a Core-to-Core pass can also query the annotations of imported things.) Furthermore, a Core-to-Core pass can add new annotations (eg strictness info) of its own, which can be queried by importing modules. The design of the annotation system is somewhat in flux. It's designed to work with the (upcoming) dynamic plug-ins mechanism, but is meanwhile independently useful. Do not merge to 6.10!
Diffstat (limited to 'compiler/utils/MonadUtils.hs')
-rw-r--r--compiler/utils/MonadUtils.hs34
1 files changed, 32 insertions, 2 deletions
diff --git a/compiler/utils/MonadUtils.hs b/compiler/utils/MonadUtils.hs
index 85d8642313..28613a4284 100644
--- a/compiler/utils/MonadUtils.hs
+++ b/compiler/utils/MonadUtils.hs
@@ -9,10 +9,13 @@ module MonadUtils
, MonadFix(..)
, MonadIO(..)
+ , liftIO1, liftIO2, liftIO3, liftIO4
+
, mapAndUnzipM, mapAndUnzip3M, mapAndUnzip4M
, mapAccumLM
, mapSndM
, concatMapM
+ , mapMaybeM
, anyM, allM
, foldlM, foldrM
) where
@@ -33,6 +36,8 @@ module MonadUtils
-- Imports
----------------------------------------------------------------------------------------
+import Maybes
+
#if HAVE_APPLICATIVE
import Control.Applicative
#endif
@@ -77,8 +82,29 @@ instance MonadIO IO where liftIO = id
#endif
----------------------------------------------------------------------------------------
+-- Lift combinators
+-- These are used throughout the compiler
+----------------------------------------------------------------------------------------
+
+-- | Lift an 'IO' operation with 1 argument into another monad
+liftIO1 :: MonadIO m => (a -> IO b) -> a -> m b
+liftIO1 = (.) liftIO
+
+-- | Lift an 'IO' operation with 2 arguments into another monad
+liftIO2 :: MonadIO m => (a -> b -> IO c) -> a -> b -> m c
+liftIO2 = ((.).(.)) liftIO
+
+-- | Lift an 'IO' operation with 3 arguments into another monad
+liftIO3 :: MonadIO m => (a -> b -> c -> IO d) -> a -> b -> c -> m d
+liftIO3 = ((.).((.).(.))) liftIO
+
+-- | Lift an 'IO' operation with 4 arguments into another monad
+liftIO4 :: MonadIO m => (a -> b -> c -> d -> IO e) -> a -> b -> c -> d -> m e
+liftIO4 = (((.).(.)).((.).(.))) liftIO
+
+----------------------------------------------------------------------------------------
-- Common functions
--- These are used throught the compiler
+-- These are used throughout the compiler
----------------------------------------------------------------------------------------
-- | mapAndUnzipM for triples
@@ -117,6 +143,10 @@ mapSndM f ((a,b):xs) = do { c <- f b; rs <- mapSndM f xs; return ((a,c):rs) }
concatMapM :: Monad m => (a -> m [b]) -> [a] -> m [b]
concatMapM f xs = liftM concat (mapM f xs)
+-- | Monadic version of mapMaybe
+mapMaybeM :: (Monad m) => (a -> m (Maybe b)) -> [a] -> m [b]
+mapMaybeM f = liftM catMaybes . mapM f
+
-- | Monadic version of 'any', aborts the computation at the first @True@ value
anyM :: Monad m => (a -> m Bool) -> [a] -> m Bool
anyM _ [] = return False
@@ -136,4 +166,4 @@ foldlM = foldM
-- | Monadic version of foldr
foldrM :: (Monad m) => (b -> a -> m a) -> a -> [b] -> m a
foldrM _ z [] = return z
-foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r }
+foldrM k z (x:xs) = do { r <- foldrM k z xs; k x r } \ No newline at end of file