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 -> (UniqSupply, a)
140 = case (splitUniqSupply init_us) of { (s1, s2) ->
143 {-# INLINE thenUs #-}
144 {-# INLINE returnUs #-}
145 {-# INLINE splitUniqSupply #-}
148 @thenUs@ is where we split the @UniqSupply@.
150 fixUs :: (a -> UniqSM a) -> UniqSM a
154 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
157 = case (splitUniqSupply us) of { (s1, s2) ->
158 case (expr s1) of { result ->
163 returnUs :: a -> UniqSM a
164 returnUs result us = result
166 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
168 mapUs f [] = returnUs []
170 = f x `thenUs` \ r ->
171 mapUs f xs `thenUs` \ rs ->
174 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
175 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
177 mapAndUnzipUs f [] = returnUs ([],[])
178 mapAndUnzipUs f (x:xs)
179 = f x `thenUs` \ (r1, r2) ->
180 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
181 returnUs (r1:rs1, r2:rs2)
183 mapAndUnzip3Us f [] = returnUs ([],[],[])
184 mapAndUnzip3Us f (x:xs)
185 = f x `thenUs` \ (r1, r2, r3) ->
186 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
187 returnUs (r1:rs1, r2:rs2, r3:rs3)
189 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
191 = m `thenUs` \ result ->
193 Nothing -> returnUs Nothing
196 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
201 mapAccumLUs f b [] = returnUs (b, [])
202 mapAccumLUs f b (x:xs)
203 = f b x `thenUs` \ (b__2, x__2) ->
204 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
205 returnUs (b__3, x__2:xs__2)