2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 UniqSupply, -- Abstractly
18 uniqFromSupply, uniqsFromSupply, -- basic ops
20 UniqSM, -- type: unique supply monad
22 lazyThenUs, lazyMapUs,
23 module MonadUtils, mapAndUnzipM,
27 splitUniqSupply, listSplitUniqSupply
30 #include "HsVersions.h"
35 #if __GLASGOW_HASKELL__ >= 607
36 import GHC.IOBase (unsafeDupableInterleaveIO)
38 import System.IO.Unsafe ( unsafeInterleaveIO )
39 unsafeDupableInterleaveIO :: IO a -> IO a
40 unsafeDupableInterleaveIO = unsafeInterleaveIO
46 %************************************************************************
48 \subsection{Splittable Unique supply: @UniqSupply@}
50 %************************************************************************
52 A value of type @UniqSupply@ is unique, and it can
53 supply {\em one} distinct @Unique@. Also, from the supply, one can
54 also manufacture an arbitrary number of further @UniqueSupplies@,
55 which will be distinct from the first and from all others.
59 = MkSplitUniqSupply FastInt -- make the Unique with this
61 -- when split => these two supplies
65 mkSplitUniqSupply :: Char -> IO UniqSupply
67 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
68 listSplitUniqSupply :: UniqSupply -> [UniqSupply] -- Infinite
69 uniqFromSupply :: UniqSupply -> Unique
70 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
75 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
77 -- here comes THE MAGIC:
79 -- This is one of the most hammered bits in the whole compiler
81 = unsafeDupableInterleaveIO (
82 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
85 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
90 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
92 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
93 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
97 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
98 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
101 %************************************************************************
103 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
105 %************************************************************************
108 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
110 instance Monad UniqSM where
115 -- the initUs function also returns the final UniqSupply; initUs_ drops it
116 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
117 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
119 initUs_ :: UniqSupply -> UniqSM a -> a
120 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
122 {-# INLINE thenUs #-}
123 {-# INLINE lazyThenUs #-}
124 {-# INLINE returnUs #-}
125 {-# INLINE splitUniqSupply #-}
128 @thenUs@ is where we split the @UniqSupply@.
130 instance MonadFix UniqSM where
131 mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
133 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
134 thenUs (USM expr) cont
135 = USM (\us -> case (expr us) of
136 (result, us') -> unUSM (cont result) us')
138 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
139 lazyThenUs (USM expr) cont
140 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
142 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
143 thenUs_ (USM expr) (USM cont)
144 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
146 returnUs :: a -> UniqSM a
147 returnUs result = USM (\us -> (result, us))
149 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
150 withUs f = USM (\us -> f us) -- Ha ha!
152 getUs :: UniqSM UniqSupply
153 getUs = USM (\us -> splitUniqSupply us)
155 -- | A monad for generating unique identifiers
156 class Monad m => MonadUnique m where
157 -- | Get a new UniqueSupply
158 getUniqueSupplyM :: m UniqSupply
159 -- | Get a new unique identifier
160 getUniqueM :: m Unique
161 -- | Get an infinite list of new unique identifiers
162 getUniquesM :: m [Unique]
164 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
165 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
167 instance MonadUnique UniqSM where
168 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
169 getUniqueM = getUniqueUs
170 getUniquesM = getUniquesUs
172 getUniqueUs :: UniqSM Unique
173 getUniqueUs = USM (\us -> case splitUniqSupply us of
174 (us1,us2) -> (uniqFromSupply us1, us2))
176 getUniquesUs :: UniqSM [Unique]
177 getUniquesUs = USM (\us -> case splitUniqSupply us of
178 (us1,us2) -> (uniqsFromSupply us1, us2))
182 {-# -- SPECIALIZE mapM :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
183 {-# -- SPECIALIZE mapAndUnzipM :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) #-}
184 {-# -- SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
186 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
187 lazyMapUs f [] = returnUs []
189 = f x `lazyThenUs` \ r ->
190 lazyMapUs f xs `lazyThenUs` \ rs ->