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 SYN_IE(UniqSM), -- type: unique supply monad
16 initUs, thenUs, returnUs, fixUs,
17 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
18 thenMaybeUs, mapAccumLUs,
31 #if __GLASGOW_HASKELL__ >= 200
32 # define WHASH GHCbase.W#
43 %************************************************************************
45 \subsection{Splittable Unique supply: @UniqSupply@}
47 %************************************************************************
49 %************************************************************************
51 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
53 %************************************************************************
55 A value of type @UniqSupply@ is unique, and it can
56 supply {\em one} distinct @Unique@. Also, from the supply, one can
57 also manufacture an arbitrary number of further @UniqueSupplies@,
58 which will be distinct from the first and from all others.
62 = MkSplitUniqSupply Int -- make the Unique with this
64 -- when split => these two supplies
68 mkSplitUniqSupply :: Char -> IO UniqSupply
70 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
71 getUnique :: UniqSupply -> Unique
72 getUniques :: Int -> UniqSupply -> [Unique]
76 mkSplitUniqSupply (C# c#)
78 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
80 -- here comes THE MAGIC:
83 = unsafeInterleavePrimIO {-unsafe_interleave-} (
84 mk_unique `thenPrimIO` \ uniq ->
85 mk_supply# `thenPrimIO` \ s1 ->
86 mk_supply# `thenPrimIO` \ s2 ->
87 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
91 -- inlined copy of unsafeInterleavePrimIO;
92 -- this is the single-most-hammered bit of code
93 -- in the compiler....
94 -- Too bad it's not 1.3-portable...
102 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (WHASH u#) ->
103 returnPrimIO (I# (w2i (mask# `or#` u#)))
105 #if __GLASGOW_HASKELL__ >= 200
106 primIOToIO mk_supply# >>= \ s ->
109 mk_supply# `thenPrimIO` \ s ->
113 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
117 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
119 getUniques (I# i) supply = i `get_from` supply
122 get_from n (MkSplitUniqSupply (I# u) _ s2)
123 = mkUniqueGrimily u : get_from (n `minusInt#` 1#) s2
126 %************************************************************************
128 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
130 %************************************************************************
133 type UniqSM result = UniqSupply -> result
135 -- the initUs function also returns the final UniqSupply
137 initUs :: UniqSupply -> UniqSM a -> a
139 initUs init_us m = m init_us
141 {-# INLINE thenUs #-}
142 {-# INLINE returnUs #-}
143 {-# INLINE splitUniqSupply #-}
146 @thenUs@ is where we split the @UniqSupply@.
148 fixUs :: (a -> UniqSM a) -> UniqSM a
152 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
155 = case (splitUniqSupply us) of { (s1, s2) ->
156 case (expr s1) of { result ->
161 returnUs :: a -> UniqSM a
162 returnUs result us = result
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
166 mapUs f [] = returnUs []
168 = f x `thenUs` \ r ->
169 mapUs f xs `thenUs` \ rs ->
172 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
173 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
175 mapAndUnzipUs f [] = returnUs ([],[])
176 mapAndUnzipUs f (x:xs)
177 = f x `thenUs` \ (r1, r2) ->
178 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
179 returnUs (r1:rs1, r2:rs2)
181 mapAndUnzip3Us f [] = returnUs ([],[],[])
182 mapAndUnzip3Us f (x:xs)
183 = f x `thenUs` \ (r1, r2, r3) ->
184 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
185 returnUs (r1:rs1, r2:rs2, r3:rs3)
187 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
189 = m `thenUs` \ result ->
191 Nothing -> returnUs Nothing
194 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
199 mapAccumLUs f b [] = returnUs (b, [])
200 mapAccumLUs f b (x:xs)
201 = f b x `thenUs` \ (b__2, x__2) ->
202 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
203 returnUs (b__3, x__2:xs__2)