X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=ghc%2Fcompiler%2FbasicTypes%2FUniqSupply.lhs;h=4f56474a52fe486066a5dc07e94ace0642a6b439;hb=1b7a99e3e7f64c6f402e8aece32ba0b9a3703bfa;hp=23bd2c051e9e76c74daad8a8b06b5625031e5fb0;hpb=9dd6e1c216993624a2cd74b62ca0f0569c02c26b;p=ghc-hetmet.git diff --git a/ghc/compiler/basicTypes/UniqSupply.lhs b/ghc/compiler/basicTypes/UniqSupply.lhs index 23bd2c0..4f56474 100644 --- a/ghc/compiler/basicTypes/UniqSupply.lhs +++ b/ghc/compiler/basicTypes/UniqSupply.lhs @@ -1,5 +1,5 @@ % -% (c) The GRASP/AQUA Project, Glasgow University, 1992-1996 +% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 % \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof} @@ -8,12 +8,14 @@ module UniqSupply ( UniqSupply, -- Abstractly - getUnique, getUniques, -- basic ops + uniqFromSupply, uniqsFromSupply, -- basic ops UniqSM, -- type: unique supply monad - initUs, thenUs, returnUs, fixUs, + initUs, initUs_, thenUs, thenUs_, returnUs, fixUs, getUs, setUs, + getUniqueUs, getUniquesUs, mapUs, mapAndUnzipUs, mapAndUnzip3Us, thenMaybeUs, mapAccumLUs, + lazyThenUs, lazyMapUs, mkSplitUniqSupply, splitUniqSupply @@ -22,12 +24,14 @@ module UniqSupply ( #include "HsVersions.h" import Unique -import Util - +import Panic ( panic ) import GlaExts -import IOBase ( IO(..), IOResult(..) ) -import PrelBase ( Char(..) ) + +#if __GLASGOW_HASKELL__ < 301 +import IOBase ( IO(..), IOResult(..) ) +#else +#endif w2i x = word2Int# x i2w x = int2Word# x @@ -63,8 +67,8 @@ data UniqSupply mkSplitUniqSupply :: Char -> IO UniqSupply splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply) -getUnique :: UniqSupply -> Unique -getUniques :: Int -> UniqSupply -> [Unique] +uniqFromSupply :: UniqSupply -> Unique +uniqsFromSupply :: Int -> UniqSupply -> [Unique] \end{code} \begin{code} @@ -92,9 +96,9 @@ splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2) \end{code} \begin{code} -getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n +uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n -getUniques (I# i) supply = i `get_from` supply +uniqsFromSupply (I# i) supply = i `get_from` supply where get_from 0# _ = [] get_from n (MkSplitUniqSupply (I# u) _ s2) @@ -108,15 +112,17 @@ getUniques (I# i) supply = i `get_from` supply %************************************************************************ \begin{code} -type UniqSM result = UniqSupply -> result - --- the initUs function also returns the final UniqSupply +type UniqSM result = UniqSupply -> (result, UniqSupply) -initUs :: UniqSupply -> UniqSM a -> a +-- the initUs function also returns the final UniqSupply; initUs_ drops it +initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply) +initUs init_us m = case m init_us of { (r,us) -> (r,us) } -initUs init_us m = m init_us +initUs_ :: UniqSupply -> UniqSM a -> a +initUs_ init_us m = case m init_us of { (r,us) -> r } {-# INLINE thenUs #-} +{-# INLINE lazyThenUs #-} {-# INLINE returnUs #-} {-# INLINE splitUniqSupply #-} \end{code} @@ -125,28 +131,54 @@ initUs init_us m = m init_us \begin{code} fixUs :: (a -> UniqSM a) -> UniqSM a fixUs m us - = r where r = m r us + = (r,us') where (r,us') = m r us thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b - thenUs expr cont us - = case (splitUniqSupply us) of { (s1, s2) -> - case (expr s1) of { result -> - cont result s2 }} -\end{code} + = case (expr us) of { (result, us') -> cont result us' } + +lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b +lazyThenUs expr cont us + = let (result, us') = expr us in cont result us' + +thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b +thenUs_ expr cont us + = case (expr us) of { (_, us') -> cont us' } + -\begin{code} returnUs :: a -> UniqSM a -returnUs result us = result +returnUs result us = (result, us) -mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b] +getUs :: UniqSM UniqSupply +getUs us = (us, panic "getUs: bad supply") + +setUs :: UniqSupply -> UniqSM () +setUs us old_us = ((), us) + +getUniqueUs :: UniqSM Unique +getUniqueUs us = case splitUniqSupply us of + (us1,us2) -> (uniqFromSupply us1, us2) + +getUniquesUs :: Int -> UniqSM [Unique] +getUniquesUs n us = case splitUniqSupply us of + (us1,us2) -> (uniqsFromSupply n us1, us2) +\end{code} +\begin{code} +mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b] mapUs f [] = returnUs [] mapUs f (x:xs) = f x `thenUs` \ r -> mapUs f xs `thenUs` \ rs -> returnUs (r:rs) +lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b] +lazyMapUs f [] = returnUs [] +lazyMapUs f (x:xs) + = f x `lazyThenUs` \ r -> + lazyMapUs f xs `lazyThenUs` \ rs -> + returnUs (r:rs) + mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])