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,
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 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
153 = case (splitUniqSupply us) of { (s1, s2) ->
154 case (expr s1) of { result ->
159 returnUs :: a -> UniqSM a
160 returnUs result us = result
162 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
164 mapUs f [] = returnUs []
166 = f x `thenUs` \ r ->
167 mapUs f xs `thenUs` \ rs ->
170 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
171 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
173 mapAndUnzipUs f [] = returnUs ([],[])
174 mapAndUnzipUs f (x:xs)
175 = f x `thenUs` \ (r1, r2) ->
176 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
177 returnUs (r1:rs1, r2:rs2)
179 mapAndUnzip3Us f [] = returnUs ([],[],[])
180 mapAndUnzip3Us f (x:xs)
181 = f x `thenUs` \ (r1, r2, r3) ->
182 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
183 returnUs (r1:rs1, r2:rs2, r3:rs3)
185 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
187 = m `thenUs` \ result ->
189 Nothing -> returnUs Nothing
192 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
197 mapAccumLUs f b [] = returnUs (b, [])
198 mapAccumLUs f b (x:xs)
199 = f b x `thenUs` \ (b__2, x__2) ->
200 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
201 returnUs (b__3, x__2:xs__2)