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 #if __GLASGOW_HASKELL__ >= 611
35 import GHC.IO (unsafeDupableInterleaveIO)
37 import GHC.IOBase (unsafeDupableInterleaveIO)
42 %************************************************************************
44 \subsection{Splittable Unique supply: @UniqSupply@}
46 %************************************************************************
49 -- | A value of type 'UniqSupply' is unique, and it can
50 -- supply /one/ distinct 'Unique'. Also, from the supply, one can
51 -- also manufacture an arbitrary number of further 'UniqueSupply' values,
52 -- which will be distinct from the first and from all others.
54 = MkSplitUniqSupply FastInt -- make the Unique with this
56 -- when split => these two supplies
60 mkSplitUniqSupply :: Char -> IO UniqSupply
61 -- ^ Create a unique supply out of thin air. The character given must
62 -- be distinct from those of all calls to this function in the compiler
63 -- for the values generated to be truly unique.
65 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
66 -- ^ Build two 'UniqSupply' from a single one, each of which
67 -- can supply its own 'Unique'.
68 listSplitUniqSupply :: UniqSupply -> [UniqSupply]
69 -- ^ Create an infinite list of 'UniqSupply' from a single one
70 uniqFromSupply :: UniqSupply -> Unique
71 -- ^ Obtain the 'Unique' from this particular 'UniqSupply'
72 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
73 -- ^ Obtain an infinite list of 'Unique' that can be generated by constant splitting of the supply
74 takeUniqFromSupply :: UniqSupply -> (Unique, UniqSupply)
75 -- ^ Obtain the 'Unique' from this particular 'UniqSupply', and a new supply
80 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
82 -- here comes THE MAGIC:
84 -- This is one of the most hammered bits in the whole compiler
86 = unsafeDupableInterleaveIO (
87 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
90 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
95 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
97 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
98 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
102 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
103 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
104 takeUniqFromSupply (MkSplitUniqSupply n s1 _) = (mkUniqueGrimily (iBox n), s1)
107 %************************************************************************
109 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
111 %************************************************************************
114 -- | A monad which just gives the ability to obtain 'Unique's
115 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
117 instance Monad UniqSM where
122 instance Functor UniqSM where
123 fmap f (USM x) = USM (\us -> case x us of
124 (r, us') -> (f r, us'))
126 instance Applicative UniqSM where
128 (USM f) <*> (USM x) = USM $ \us -> case f us of
129 (ff, us') -> case x us' of
130 (xx, us'') -> (ff xx, us'')
132 -- | Run the 'UniqSM' action, returning the final 'UniqSupply'
133 initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
134 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
136 -- | Run the 'UniqSM' action, discarding the final 'UniqSupply'
137 initUs_ :: UniqSupply -> UniqSM a -> a
138 initUs_ init_us m = case unUSM m init_us of { (r, _) -> r }
140 {-# INLINE thenUs #-}
141 {-# INLINE lazyThenUs #-}
142 {-# INLINE returnUs #-}
143 {-# INLINE splitUniqSupply #-}
146 @thenUs@ is where we split the @UniqSupply@.
148 instance MonadFix UniqSM where
149 mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
151 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
152 thenUs (USM expr) cont
153 = USM (\us -> case (expr us) of
154 (result, us') -> unUSM (cont result) us')
156 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
157 lazyThenUs (USM expr) cont
158 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
160 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
161 thenUs_ (USM expr) (USM cont)
162 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
164 returnUs :: a -> UniqSM a
165 returnUs result = USM (\us -> (result, us))
167 getUs :: UniqSM UniqSupply
168 getUs = USM (\us -> splitUniqSupply us)
170 -- | A monad for generating unique identifiers
171 class Monad m => MonadUnique m where
172 -- | Get a new UniqueSupply
173 getUniqueSupplyM :: m UniqSupply
174 -- | Get a new unique identifier
175 getUniqueM :: m Unique
176 -- | Get an infinite list of new unique identifiers
177 getUniquesM :: m [Unique]
179 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
180 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
182 instance MonadUnique UniqSM where
183 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
184 getUniqueM = getUniqueUs
185 getUniquesM = getUniquesUs
187 getUniqueUs :: UniqSM Unique
188 getUniqueUs = USM (\us -> case splitUniqSupply us of
189 (us1,us2) -> (uniqFromSupply us1, us2))
191 getUniquesUs :: UniqSM [Unique]
192 getUniquesUs = USM (\us -> case splitUniqSupply us of
193 (us1,us2) -> (uniqsFromSupply us1, us2))
195 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
196 mapUs _ [] = returnUs []
198 = f x `thenUs` \ r ->
199 mapUs f xs `thenUs` \ rs ->
204 -- {-# SPECIALIZE mapM :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
205 -- {-# SPECIALIZE mapAndUnzipM :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) #-}
206 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
208 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
209 lazyMapUs _ [] = returnUs []
211 = f x `lazyThenUs` \ r ->
212 lazyMapUs f xs `lazyThenUs` \ rs ->