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,
30 #if __GLASGOW_HASKELL__ == 201
32 # define WHASH GHCbase.W#
33 #elif __GLASGOW_HASKELL__ >= 202
36 # define WHASH GlaExts.W#
48 %************************************************************************
50 \subsection{Splittable Unique supply: @UniqSupply@}
52 %************************************************************************
54 %************************************************************************
56 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
58 %************************************************************************
60 A value of type @UniqSupply@ is unique, and it can
61 supply {\em one} distinct @Unique@. Also, from the supply, one can
62 also manufacture an arbitrary number of further @UniqueSupplies@,
63 which will be distinct from the first and from all others.
67 = MkSplitUniqSupply Int -- make the Unique with this
69 -- when split => these two supplies
73 mkSplitUniqSupply :: Char -> IO UniqSupply
75 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
76 getUnique :: UniqSupply -> Unique
77 getUniques :: Int -> UniqSupply -> [Unique]
81 mkSplitUniqSupply (C# c#)
83 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
85 -- here comes THE MAGIC:
89 mk_unique `thenPrimIO` \ uniq ->
90 mk_supply# `thenPrimIO` \ s1 ->
91 mk_supply# `thenPrimIO` \ s2 ->
92 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
96 -- inlined copy of unsafeInterleavePrimIO;
97 -- this is the single-most-hammered bit of code
98 -- in the compiler....
99 -- Too bad it's not 1.3-portable...
100 unsafe_interleave m =
109 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (WHASH u#) ->
110 returnPrimIO (I# (w2i (mask# `or#` u#)))
112 #if __GLASGOW_HASKELL__ >= 200
113 primIOToIO mk_supply# >>= \ s ->
116 mk_supply# `thenPrimIO` \ s ->
120 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
124 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
126 getUniques (I# i) supply = i `get_from` supply
129 get_from n (MkSplitUniqSupply (I# u) _ s2)
130 = mkUniqueGrimily u : get_from (n -# 1#) s2
133 %************************************************************************
135 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
137 %************************************************************************
140 type UniqSM result = UniqSupply -> result
142 -- the initUs function also returns the final UniqSupply
144 initUs :: UniqSupply -> UniqSM a -> a
146 initUs init_us m = m init_us
148 {-# INLINE thenUs #-}
149 {-# INLINE returnUs #-}
150 {-# INLINE splitUniqSupply #-}
153 @thenUs@ is where we split the @UniqSupply@.
155 fixUs :: (a -> UniqSM a) -> UniqSM a
159 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
162 = case (splitUniqSupply us) of { (s1, s2) ->
163 case (expr s1) of { result ->
168 returnUs :: a -> UniqSM a
169 returnUs result us = result
171 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
173 mapUs f [] = returnUs []
175 = f x `thenUs` \ r ->
176 mapUs f xs `thenUs` \ rs ->
179 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
180 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
182 mapAndUnzipUs f [] = returnUs ([],[])
183 mapAndUnzipUs f (x:xs)
184 = f x `thenUs` \ (r1, r2) ->
185 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
186 returnUs (r1:rs1, r2:rs2)
188 mapAndUnzip3Us f [] = returnUs ([],[],[])
189 mapAndUnzip3Us f (x:xs)
190 = f x `thenUs` \ (r1, r2, r3) ->
191 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
192 returnUs (r1:rs1, r2:rs2, r3:rs3)
194 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
196 = m `thenUs` \ result ->
198 Nothing -> returnUs Nothing
201 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
206 mapAccumLUs f b [] = returnUs (b, [])
207 mapAccumLUs f b (x:xs)
208 = f b x `thenUs` \ (b__2, x__2) ->
209 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
210 returnUs (b__3, x__2:xs__2)