X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=ghc%2Fcompiler%2FbasicTypes%2FUniqSupply.lhs;h=218df9ee9034801e89d6b562b98978f6fa75ae07;hb=e8964a486b2d0915617116eedf8b34670d443fbf;hp=4b83b52a610b005ae6a2330af0b9a327e07326a1;hpb=be33dbc967b4915cfdb0307ae1b7ae3cee651b8c;p=ghc-hetmet.git diff --git a/ghc/compiler/basicTypes/UniqSupply.lhs b/ghc/compiler/basicTypes/UniqSupply.lhs index 4b83b52..218df9e 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, withUs, + getUniqueUs, getUniquesUs, mapUs, mapAndUnzipUs, mapAndUnzip3Us, thenMaybeUs, mapAccumLUs, + lazyThenUs, lazyMapUs, mkSplitUniqSupply, splitUniqSupply @@ -22,8 +24,6 @@ module UniqSupply ( #include "HsVersions.h" import Unique -import Util - import GlaExts #if __GLASGOW_HASKELL__ < 301 @@ -65,8 +65,8 @@ data UniqSupply mkSplitUniqSupply :: Char -> IO UniqSupply splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply) -getUnique :: UniqSupply -> Unique -getUniques :: Int -> UniqSupply -> [Unique] +uniqFromSupply :: UniqSupply -> Unique +uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite \end{code} \begin{code} @@ -94,13 +94,8 @@ splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2) \end{code} \begin{code} -getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n - -getUniques (I# i) supply = i `get_from` supply - where - get_from 0# _ = [] - get_from n (MkSplitUniqSupply (I# u) _ s2) - = mkUniqueGrimily u : get_from (n -# 1#) s2 +uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n +uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2 \end{code} %************************************************************************ @@ -110,15 +105,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} @@ -127,28 +124,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] +withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a +withUs f us = f us -- Ha ha! + +getUs :: UniqSM UniqSupply +getUs us = splitUniqSupply us + +getUniqueUs :: UniqSM Unique +getUniqueUs us = case splitUniqSupply us of + (us1,us2) -> (uniqFromSupply us1, us2) + +getUniquesUs :: UniqSM [Unique] +getUniquesUs us = case splitUniqSupply us of + (us1,us2) -> (uniqsFromSupply 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])