2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
7 #include "HsVersions.h"
11 UniqSupply, -- Abstractly
13 getUnique, getUniques, -- basic ops
15 UniqSM(..), -- type: unique supply monad
16 initUs, thenUs, returnUs,
17 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
18 thenMaybeUs, mapAccumLUs,
23 -- and the access functions for the `builtin' UniqueSupply
24 getBuiltinUniques, mkBuiltinUnique,
25 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
41 %************************************************************************
43 \subsection{Splittable Unique supply: @UniqSupply@}
45 %************************************************************************
47 %************************************************************************
49 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
51 %************************************************************************
53 A value of type @UniqSupply@ is unique, and it can
54 supply {\em one} distinct @Unique@. Also, from the supply, one can
55 also manufacture an arbitrary number of further @UniqueSupplies@,
56 which will be distinct from the first and from all others.
60 = MkSplitUniqSupply Int -- make the Unique with this
62 -- when split => these two supplies
66 mkSplitUniqSupply :: Char -> PrimIO UniqSupply
68 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
69 getUnique :: UniqSupply -> Unique
70 getUniques :: Int -> UniqSupply -> [Unique]
74 mkSplitUniqSupply (MkChar c#)
76 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
78 -- here comes THE MAGIC:
82 mk_unique `thenPrimIO` \ uniq ->
83 mk_supply# `thenPrimIO` \ s1 ->
84 mk_supply# `thenPrimIO` \ s2 ->
85 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
88 -- inlined copy of unsafeInterleavePrimIO;
89 -- this is the single-most-hammered bit of code
90 -- in the compiler....
97 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (W# u#) ->
98 returnPrimIO (MkInt (w2i (mask# `or#` u#)))
102 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
106 getUnique (MkSplitUniqSupply (MkInt n) _ _) = mkUniqueGrimily n
108 getUniques i@(MkInt i#) supply = i# `get_from` supply
111 get_from n# (MkSplitUniqSupply (MkInt u#) _ s2)
112 = mkUniqueGrimily u# : get_from (n# `minusInt#` 1#) s2
115 %************************************************************************
117 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
119 %************************************************************************
122 type UniqSM result = UniqSupply -> result
124 -- the initUs function also returns the final UniqSupply
126 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
129 = case (splitUniqSupply init_us) of { (s1, s2) ->
132 {-# INLINE thenUs #-}
133 {-# INLINE returnUs #-}
134 {-# INLINE splitUniqSupply #-}
137 @thenUs@ is where we split the @UniqSupply@.
139 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
142 = case (splitUniqSupply us) of { (s1, s2) ->
143 case (expr s1) of { result ->
148 returnUs :: a -> UniqSM a
149 returnUs result us = result
151 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
153 mapUs f [] = returnUs []
155 = f x `thenUs` \ r ->
156 mapUs f xs `thenUs` \ rs ->
159 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
160 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
162 mapAndUnzipUs f [] = returnUs ([],[])
163 mapAndUnzipUs f (x:xs)
164 = f x `thenUs` \ (r1, r2) ->
165 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
166 returnUs (r1:rs1, r2:rs2)
168 mapAndUnzip3Us f [] = returnUs ([],[],[])
169 mapAndUnzip3Us f (x:xs)
170 = f x `thenUs` \ (r1, r2, r3) ->
171 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
172 returnUs (r1:rs1, r2:rs2, r3:rs3)
174 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
176 = m `thenUs` \ result ->
178 Nothing -> returnUs Nothing
181 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
186 mapAccumLUs f b [] = returnUs (b, [])
187 mapAccumLUs f b (x:xs)
188 = f b x `thenUs` \ (b__2, x__2) ->
189 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
190 returnUs (b__3, x__2:xs__2)
193 %************************************************************************
195 \subsubsection[UniqueSupplies-compiler]{@UniqueSupplies@ specific to the compiler}
197 %************************************************************************
200 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
201 mkBuiltinUnique :: Int -> Unique
203 mkBuiltinUnique i = mkUnique 'B' i
204 mkPseudoUnique1 i = mkUnique 'C' i -- used for uniqueOf on Regs
205 mkPseudoUnique2 i = mkUnique 'D' i -- ditto
206 mkPseudoUnique3 i = mkUnique 'E' i -- ditto
208 getBuiltinUniques :: Int -> [Unique]
209 getBuiltinUniques n = map (mkUnique 'B') [1 .. n]
212 The following runs a uniq monad expression, using builtin uniq values:
214 --runBuiltinUs :: UniqSM a -> a
215 --runBuiltinUs m = snd (initUs uniqSupply_B m)