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 import GHC.IOBase (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
74 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
76 -- here comes THE MAGIC:
78 -- This is one of the most hammered bits in the whole compiler
80 = unsafeDupableInterleaveIO (
81 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
84 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
89 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
91 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
92 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
96 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
97 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
100 %************************************************************************
102 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
104 %************************************************************************
107 -- | A monad which just gives the ability to obtain 'Unique's
108 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
110 instance Monad UniqSM where
115 instance Functor UniqSM where
116 fmap f (USM x) = USM (\us -> case x us of
117 (r, us') -> (f r, us'))
119 instance Applicative UniqSM where
121 (USM f) <*> (USM x) = USM $ \us -> case f us of
122 (ff, us') -> case x us' of
123 (xx, us'') -> (ff xx, us'')
125 -- | Run the 'UniqSM' action, returning the final 'UniqSupply'
126 initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
127 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
129 -- | Run the 'UniqSM' action, discarding the final 'UniqSupply'
130 initUs_ :: UniqSupply -> UniqSM a -> a
131 initUs_ init_us m = case unUSM m init_us of { (r, _) -> r }
133 {-# INLINE thenUs #-}
134 {-# INLINE lazyThenUs #-}
135 {-# INLINE returnUs #-}
136 {-# INLINE splitUniqSupply #-}
139 @thenUs@ is where we split the @UniqSupply@.
141 instance MonadFix UniqSM where
142 mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
144 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
145 thenUs (USM expr) cont
146 = USM (\us -> case (expr us) of
147 (result, us') -> unUSM (cont result) us')
149 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
150 lazyThenUs (USM expr) cont
151 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
153 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
154 thenUs_ (USM expr) (USM cont)
155 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
157 returnUs :: a -> UniqSM a
158 returnUs result = USM (\us -> (result, us))
160 getUs :: UniqSM UniqSupply
161 getUs = USM (\us -> splitUniqSupply us)
163 -- | A monad for generating unique identifiers
164 class Monad m => MonadUnique m where
165 -- | Get a new UniqueSupply
166 getUniqueSupplyM :: m UniqSupply
167 -- | Get a new unique identifier
168 getUniqueM :: m Unique
169 -- | Get an infinite list of new unique identifiers
170 getUniquesM :: m [Unique]
172 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
173 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
175 instance MonadUnique UniqSM where
176 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
177 getUniqueM = getUniqueUs
178 getUniquesM = getUniquesUs
180 getUniqueUs :: UniqSM Unique
181 getUniqueUs = USM (\us -> case splitUniqSupply us of
182 (us1,us2) -> (uniqFromSupply us1, us2))
184 getUniquesUs :: UniqSM [Unique]
185 getUniquesUs = USM (\us -> case splitUniqSupply us of
186 (us1,us2) -> (uniqsFromSupply us1, us2))
188 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
189 mapUs _ [] = returnUs []
191 = f x `thenUs` \ r ->
192 mapUs f xs `thenUs` \ rs ->
197 -- {-# SPECIALIZE mapM :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
198 -- {-# SPECIALIZE mapAndUnzipM :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c]) #-}
199 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
201 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
202 lazyMapUs _ [] = returnUs []
204 = f x `lazyThenUs` \ r ->
205 lazyMapUs f xs `lazyThenUs` \ rs ->