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
15 splitUniqSupply, listSplitUniqSupply,
17 -- * Unique supply monad and its abstraction
18 UniqSM, MonadUnique(..),
20 -- ** Operations on the monad
22 lazyThenUs, lazyMapUs,
24 -- ** Deprecated operations on 'UniqSM'
25 getUniqueUs, getUs, returnUs, thenUs, mapUs
33 import Control.Monad.Fix
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
78 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
80 -- here comes THE MAGIC:
82 -- This is one of the most hammered bits in the whole compiler
84 = unsafeDupableInterleaveIO (
85 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
88 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
93 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
95 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
96 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
100 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
101 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
104 %************************************************************************
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
108 %************************************************************************
111 -- | A monad which just gives the ability to obtain 'Unique's
112 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
114 instance Monad UniqSM where
119 instance Functor UniqSM where
120 fmap f (USM x) = USM (\us -> case x us of
121 (r, us') -> (f r, us'))
123 instance Applicative UniqSM where
125 (USM f) <*> (USM x) = USM $ \us -> case f us of
126 (ff, us') -> case x us' of
127 (xx, us'') -> (ff xx, us'')
129 -- | Run the 'UniqSM' action, returning the final 'UniqSupply'
130 initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
131 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
133 -- | Run the 'UniqSM' action, discarding the final 'UniqSupply'
134 initUs_ :: UniqSupply -> UniqSM a -> a
135 initUs_ init_us m = case unUSM m init_us of { (r, _) -> r }
137 {-# INLINE thenUs #-}
138 {-# INLINE lazyThenUs #-}
139 {-# INLINE returnUs #-}
140 {-# INLINE splitUniqSupply #-}
143 @thenUs@ is where we split the @UniqSupply@.
145 instance MonadFix UniqSM where
146 mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
148 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
149 thenUs (USM expr) cont
150 = USM (\us -> case (expr us) of
151 (result, us') -> unUSM (cont result) us')
153 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
154 lazyThenUs (USM expr) cont
155 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
157 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
158 thenUs_ (USM expr) (USM cont)
159 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
161 returnUs :: a -> UniqSM a
162 returnUs result = USM (\us -> (result, us))
164 getUs :: UniqSM UniqSupply
165 getUs = USM (\us -> splitUniqSupply us)
167 -- | A monad for generating unique identifiers
168 class Monad m => MonadUnique m where
169 -- | Get a new UniqueSupply
170 getUniqueSupplyM :: m UniqSupply
171 -- | Get a new unique identifier
172 getUniqueM :: m Unique
173 -- | Get an infinite list of new unique identifiers
174 getUniquesM :: m [Unique]
176 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
177 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
179 instance MonadUnique UniqSM where
180 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
181 getUniqueM = getUniqueUs
182 getUniquesM = getUniquesUs
184 getUniqueUs :: UniqSM Unique
185 getUniqueUs = USM (\us -> case splitUniqSupply us of
186 (us1,us2) -> (uniqFromSupply us1, us2))
188 getUniquesUs :: UniqSM [Unique]
189 getUniquesUs = USM (\us -> case splitUniqSupply us of
190 (us1,us2) -> (uniqsFromSupply us1, us2))
192 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
193 mapUs _ [] = returnUs []
195 = f x `thenUs` \ r ->
196 mapUs f xs `thenUs` \ rs ->
201 -- {-# SPECIALIZE mapM :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
202 -- {-# SPECIALIZE mapAndUnzipM :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) #-}
203 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
205 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
206 lazyMapUs _ [] = returnUs []
208 = f x `lazyThenUs` \ r ->
209 lazyMapUs f xs `lazyThenUs` \ rs ->