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 -> IO 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#)))
100 mk_supply# `thenPrimIO` \ s ->
103 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
107 getUnique (MkSplitUniqSupply (MkInt n) _ _) = mkUniqueGrimily n
109 getUniques i@(MkInt i#) supply = i# `get_from` supply
112 get_from n# (MkSplitUniqSupply (MkInt u#) _ s2)
113 = mkUniqueGrimily u# : get_from (n# `minusInt#` 1#) s2
116 %************************************************************************
118 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
120 %************************************************************************
123 type UniqSM result = UniqSupply -> result
125 -- the initUs function also returns the final UniqSupply
127 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
130 = case (splitUniqSupply init_us) of { (s1, s2) ->
133 {-# INLINE thenUs #-}
134 {-# INLINE returnUs #-}
135 {-# INLINE splitUniqSupply #-}
138 @thenUs@ is where we split the @UniqSupply@.
140 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
143 = case (splitUniqSupply us) of { (s1, s2) ->
144 case (expr s1) of { result ->
149 returnUs :: a -> UniqSM a
150 returnUs result us = result
152 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
154 mapUs f [] = returnUs []
156 = f x `thenUs` \ r ->
157 mapUs f xs `thenUs` \ rs ->
160 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
161 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
163 mapAndUnzipUs f [] = returnUs ([],[])
164 mapAndUnzipUs f (x:xs)
165 = f x `thenUs` \ (r1, r2) ->
166 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
167 returnUs (r1:rs1, r2:rs2)
169 mapAndUnzip3Us f [] = returnUs ([],[],[])
170 mapAndUnzip3Us f (x:xs)
171 = f x `thenUs` \ (r1, r2, r3) ->
172 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
173 returnUs (r1:rs1, r2:rs2, r3:rs3)
175 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
177 = m `thenUs` \ result ->
179 Nothing -> returnUs Nothing
182 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
187 mapAccumLUs f b [] = returnUs (b, [])
188 mapAccumLUs f b (x:xs)
189 = f b x `thenUs` \ (b__2, x__2) ->
190 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
191 returnUs (b__3, x__2:xs__2)
194 %************************************************************************
196 \subsubsection[UniqueSupplies-compiler]{@UniqueSupplies@ specific to the compiler}
198 %************************************************************************
201 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
202 mkBuiltinUnique :: Int -> Unique
204 mkBuiltinUnique i = mkUnique 'B' i
205 mkPseudoUnique1 i = mkUnique 'C' i -- used for uniqueOf on Regs
206 mkPseudoUnique2 i = mkUnique 'D' i -- ditto
207 mkPseudoUnique3 i = mkUnique 'E' i -- ditto
209 getBuiltinUniques :: Int -> [Unique]
210 getBuiltinUniques n = map (mkUnique 'B') [1 .. n]
213 The following runs a uniq monad expression, using builtin uniq values:
215 --runBuiltinUs :: UniqSM a -> a
216 --runBuiltinUs m = snd (initUs uniqSupply_B m)