2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
9 UniqSupply, -- Abstractly
11 -- ** Operations on supplies
12 uniqFromSupply, uniqsFromSupply, -- basic ops
16 splitUniqSupply, listSplitUniqSupply,
18 -- * Unique supply monad and its abstraction
19 UniqSM, MonadUnique(..),
21 -- ** Operations on the monad
23 lazyThenUs, lazyMapUs,
25 -- ** Deprecated operations on 'UniqSM'
26 getUniqueUs, getUs, returnUs, thenUs, mapUs
34 import GHC.IO (unsafeDupableInterleaveIO)
38 %************************************************************************
40 \subsection{Splittable Unique supply: @UniqSupply@}
42 %************************************************************************
45 -- | A value of type 'UniqSupply' is unique, and it can
46 -- supply /one/ distinct 'Unique'. Also, from the supply, one can
47 -- also manufacture an arbitrary number of further 'UniqueSupply' values,
48 -- which will be distinct from the first and from all others.
50 = MkSplitUniqSupply FastInt -- make the Unique with this
52 -- when split => these two supplies
56 mkSplitUniqSupply :: Char -> IO UniqSupply
57 -- ^ Create a unique supply out of thin air. The character given must
58 -- be distinct from those of all calls to this function in the compiler
59 -- for the values generated to be truly unique.
61 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
62 -- ^ Build two 'UniqSupply' from a single one, each of which
63 -- can supply its own 'Unique'.
64 listSplitUniqSupply :: UniqSupply -> [UniqSupply]
65 -- ^ Create an infinite list of 'UniqSupply' from a single one
66 uniqFromSupply :: UniqSupply -> Unique
67 -- ^ Obtain the 'Unique' from this particular 'UniqSupply'
68 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
69 -- ^ Obtain an infinite list of 'Unique' that can be generated by constant splitting of the supply
70 takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
71 -- ^ Obtain the 'Unique' from this particular 'UniqSupply', and a new supply
76 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
78 -- here comes THE MAGIC:
80 -- This is one of the most hammered bits in the whole compiler
82 = unsafeDupableInterleaveIO (
83 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
86 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
91 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
93 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
94 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
98 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
99 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
100 takeUniqFromSupply (MkSplitUniqSupply n s1 _) = (mkUniqueGrimily (iBox n), s1)
103 %************************************************************************
105 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
107 %************************************************************************
110 -- | A monad which just gives the ability to obtain 'Unique's
111 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
113 instance Monad UniqSM where
118 instance Functor UniqSM where
119 fmap f (USM x) = USM (\us -> case x us of
120 (r, us') -> (f r, us'))
122 instance Applicative UniqSM where
124 (USM f) <*> (USM x) = USM $ \us -> case f us of
125 (ff, us') -> case x us' of
126 (xx, us'') -> (ff xx, us'')
128 -- | Run the 'UniqSM' action, returning the final 'UniqSupply'
129 initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
130 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
132 -- | Run the 'UniqSM' action, discarding the final 'UniqSupply'
133 initUs_ :: UniqSupply -> UniqSM a -> a
134 initUs_ init_us m = case unUSM m init_us of { (r, _) -> r }
136 {-# INLINE thenUs #-}
137 {-# INLINE lazyThenUs #-}
138 {-# INLINE returnUs #-}
139 {-# INLINE splitUniqSupply #-}
142 @thenUs@ is where we split the @UniqSupply@.
144 instance MonadFix UniqSM where
145 mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
147 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
148 thenUs (USM expr) cont
149 = USM (\us -> case (expr us) of
150 (result, us') -> unUSM (cont result) us')
152 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
153 lazyThenUs (USM expr) cont
154 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
156 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
157 thenUs_ (USM expr) (USM cont)
158 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
160 returnUs :: a -> UniqSM a
161 returnUs result = USM (\us -> (result, us))
163 getUs :: UniqSM UniqSupply
164 getUs = USM (\us -> splitUniqSupply us)
166 -- | A monad for generating unique identifiers
167 class Monad m => MonadUnique m where
168 -- | Get a new UniqueSupply
169 getUniqueSupplyM :: m UniqSupply
170 -- | Get a new unique identifier
171 getUniqueM :: m Unique
172 -- | Get an infinite list of new unique identifiers
173 getUniquesM :: m [Unique]
175 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
176 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
178 instance MonadUnique UniqSM where
179 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
180 getUniqueM = getUniqueUs
181 getUniquesM = getUniquesUs
183 getUniqueUs :: UniqSM Unique
184 getUniqueUs = USM (\us -> case splitUniqSupply us of
185 (us1,us2) -> (uniqFromSupply us1, us2))
187 getUniquesUs :: UniqSM [Unique]
188 getUniquesUs = USM (\us -> case splitUniqSupply us of
189 (us1,us2) -> (uniqsFromSupply us1, us2))
191 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
192 mapUs _ [] = returnUs []
194 = f x `thenUs` \ r ->
195 mapUs f xs `thenUs` \ rs ->
200 -- {-# SPECIALIZE mapM :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
201 -- {-# SPECIALIZE mapAndUnzipM :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) #-}
202 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
204 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
205 lazyMapUs _ [] = returnUs []
207 = f x `lazyThenUs` \ r ->
208 lazyMapUs f xs `lazyThenUs` \ rs ->