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 # if __GLASGOW_HASKELL__ == 202
37 import PrelBase ( Char(..) )
39 # define WHASH GlaExts.W#
45 #if __GLASGOW_HASKELL__ >= 209
46 import Unsafe ( unsafeInterleaveIO )
55 %************************************************************************
57 \subsection{Splittable Unique supply: @UniqSupply@}
59 %************************************************************************
61 %************************************************************************
63 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
65 %************************************************************************
67 A value of type @UniqSupply@ is unique, and it can
68 supply {\em one} distinct @Unique@. Also, from the supply, one can
69 also manufacture an arbitrary number of further @UniqueSupplies@,
70 which will be distinct from the first and from all others.
74 = MkSplitUniqSupply Int -- make the Unique with this
76 -- when split => these two supplies
80 mkSplitUniqSupply :: Char -> IO UniqSupply
82 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
83 getUnique :: UniqSupply -> Unique
84 getUniques :: Int -> UniqSupply -> [Unique]
88 mkSplitUniqSupply (C# c#)
90 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
92 -- here comes THE MAGIC:
96 mk_unique `thenPrimIO` \ uniq ->
97 mk_supply# `thenPrimIO` \ s1 ->
98 mk_supply# `thenPrimIO` \ s2 ->
99 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
103 -- inlined copy of unsafeInterleavePrimIO;
104 -- this is the single-most-hammered bit of code
105 -- in the compiler....
106 -- Too bad it's not 1.3-portable...
107 unsafe_interleave m =
108 #if __GLASGOW_HASKELL__ >= 209
114 ST_RET(r, new_s) = m' s
119 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (WHASH u#) ->
120 returnPrimIO (I# (w2i (mask# `or#` u#)))
122 #if __GLASGOW_HASKELL__ >= 200
123 primIOToIO mk_supply# >>= \ s ->
126 mk_supply# `thenPrimIO` \ s ->
130 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
134 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
136 getUniques (I# i) supply = i `get_from` supply
139 get_from n (MkSplitUniqSupply (I# u) _ s2)
140 = mkUniqueGrimily u : get_from (n -# 1#) s2
143 %************************************************************************
145 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
147 %************************************************************************
150 type UniqSM result = UniqSupply -> result
152 -- the initUs function also returns the final UniqSupply
154 initUs :: UniqSupply -> UniqSM a -> a
156 initUs init_us m = m init_us
158 {-# INLINE thenUs #-}
159 {-# INLINE returnUs #-}
160 {-# INLINE splitUniqSupply #-}
163 @thenUs@ is where we split the @UniqSupply@.
165 fixUs :: (a -> UniqSM a) -> UniqSM a
169 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
172 = case (splitUniqSupply us) of { (s1, s2) ->
173 case (expr s1) of { result ->
178 returnUs :: a -> UniqSM a
179 returnUs result us = result
181 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
183 mapUs f [] = returnUs []
185 = f x `thenUs` \ r ->
186 mapUs f xs `thenUs` \ rs ->
189 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
190 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
192 mapAndUnzipUs f [] = returnUs ([],[])
193 mapAndUnzipUs f (x:xs)
194 = f x `thenUs` \ (r1, r2) ->
195 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
196 returnUs (r1:rs1, r2:rs2)
198 mapAndUnzip3Us f [] = returnUs ([],[],[])
199 mapAndUnzip3Us f (x:xs)
200 = f x `thenUs` \ (r1, r2, r3) ->
201 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
202 returnUs (r1:rs1, r2:rs2, r3:rs3)
204 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
206 = m `thenUs` \ result ->
208 Nothing -> returnUs Nothing
211 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
216 mapAccumLUs f b [] = returnUs (b, [])
217 mapAccumLUs f b (x:xs)
218 = f b x `thenUs` \ (b__2, x__2) ->
219 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
220 returnUs (b__3, x__2:xs__2)