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#
51 %************************************************************************
53 \subsection{Splittable Unique supply: @UniqSupply@}
55 %************************************************************************
57 %************************************************************************
59 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
61 %************************************************************************
63 A value of type @UniqSupply@ is unique, and it can
64 supply {\em one} distinct @Unique@. Also, from the supply, one can
65 also manufacture an arbitrary number of further @UniqueSupplies@,
66 which will be distinct from the first and from all others.
70 = MkSplitUniqSupply Int -- make the Unique with this
72 -- when split => these two supplies
76 mkSplitUniqSupply :: Char -> IO UniqSupply
78 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
79 getUnique :: UniqSupply -> Unique
80 getUniques :: Int -> UniqSupply -> [Unique]
84 mkSplitUniqSupply (C# c#)
86 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
88 -- here comes THE MAGIC:
92 mk_unique `thenPrimIO` \ uniq ->
93 mk_supply# `thenPrimIO` \ s1 ->
94 mk_supply# `thenPrimIO` \ s2 ->
95 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
99 -- inlined copy of unsafeInterleavePrimIO;
100 -- this is the single-most-hammered bit of code
101 -- in the compiler....
102 -- Too bad it's not 1.3-portable...
103 unsafe_interleave m =
112 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (WHASH u#) ->
113 returnPrimIO (I# (w2i (mask# `or#` u#)))
115 #if __GLASGOW_HASKELL__ >= 200
116 primIOToIO mk_supply# >>= \ s ->
119 mk_supply# `thenPrimIO` \ s ->
123 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
127 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
129 getUniques (I# i) supply = i `get_from` supply
132 get_from n (MkSplitUniqSupply (I# u) _ s2)
133 = mkUniqueGrimily u : get_from (n -# 1#) s2
136 %************************************************************************
138 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
140 %************************************************************************
143 type UniqSM result = UniqSupply -> result
145 -- the initUs function also returns the final UniqSupply
147 initUs :: UniqSupply -> UniqSM a -> a
149 initUs init_us m = m init_us
151 {-# INLINE thenUs #-}
152 {-# INLINE returnUs #-}
153 {-# INLINE splitUniqSupply #-}
156 @thenUs@ is where we split the @UniqSupply@.
158 fixUs :: (a -> UniqSM a) -> UniqSM a
162 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
165 = case (splitUniqSupply us) of { (s1, s2) ->
166 case (expr s1) of { result ->
171 returnUs :: a -> UniqSM a
172 returnUs result us = result
174 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
176 mapUs f [] = returnUs []
178 = f x `thenUs` \ r ->
179 mapUs f xs `thenUs` \ rs ->
182 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
183 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
185 mapAndUnzipUs f [] = returnUs ([],[])
186 mapAndUnzipUs f (x:xs)
187 = f x `thenUs` \ (r1, r2) ->
188 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
189 returnUs (r1:rs1, r2:rs2)
191 mapAndUnzip3Us f [] = returnUs ([],[],[])
192 mapAndUnzip3Us f (x:xs)
193 = f x `thenUs` \ (r1, r2, r3) ->
194 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
195 returnUs (r1:rs1, r2:rs2, r3:rs3)
197 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
199 = m `thenUs` \ result ->
201 Nothing -> returnUs Nothing
204 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
209 mapAccumLUs f b [] = returnUs (b, [])
210 mapAccumLUs f b (x:xs)
211 = f b x `thenUs` \ (b__2, x__2) ->
212 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
213 returnUs (b__3, x__2:xs__2)