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 UniqSM(..), -- type: unique supply monad
16 initUs, thenUs, returnUs,
17 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
18 thenMaybeUs, mapAccumLUs,
37 %************************************************************************
39 \subsection{Splittable Unique supply: @UniqSupply@}
41 %************************************************************************
43 %************************************************************************
45 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
47 %************************************************************************
49 A value of type @UniqSupply@ is unique, and it can
50 supply {\em one} distinct @Unique@. Also, from the supply, one can
51 also manufacture an arbitrary number of further @UniqueSupplies@,
52 which will be distinct from the first and from all others.
56 = MkSplitUniqSupply Int -- make the Unique with this
58 -- when split => these two supplies
62 mkSplitUniqSupply :: Char -> IO UniqSupply
64 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
65 getUnique :: UniqSupply -> Unique
66 getUniques :: Int -> UniqSupply -> [Unique]
70 mkSplitUniqSupply (C# c#)
72 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
74 -- here comes THE MAGIC:
78 mk_unique `thenPrimIO` \ uniq ->
79 mk_supply# `thenPrimIO` \ s1 ->
80 mk_supply# `thenPrimIO` \ s2 ->
81 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
84 -- inlined copy of unsafeInterleavePrimIO;
85 -- this is the single-most-hammered bit of code
86 -- in the compiler....
93 mk_unique = _ccall_ genSymZh `thenPrimIO` \ (W# u#) ->
94 returnPrimIO (I# (w2i (mask# `or#` u#)))
96 mk_supply# `thenPrimIO` \ s ->
99 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
103 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
105 getUniques (I# i) supply = i `get_from` supply
108 get_from n (MkSplitUniqSupply (I# u) _ s2)
109 = mkUniqueGrimily u : get_from (n `minusInt#` 1#) s2
112 %************************************************************************
114 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
116 %************************************************************************
119 type UniqSM result = UniqSupply -> result
121 -- the initUs function also returns the final UniqSupply
123 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
126 = case (splitUniqSupply init_us) of { (s1, s2) ->
129 {-# INLINE thenUs #-}
130 {-# INLINE returnUs #-}
131 {-# INLINE splitUniqSupply #-}
134 @thenUs@ is where we split the @UniqSupply@.
136 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
139 = case (splitUniqSupply us) of { (s1, s2) ->
140 case (expr s1) of { result ->
145 returnUs :: a -> UniqSM a
146 returnUs result us = result
148 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
150 mapUs f [] = returnUs []
152 = f x `thenUs` \ r ->
153 mapUs f xs `thenUs` \ rs ->
156 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
157 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
159 mapAndUnzipUs f [] = returnUs ([],[])
160 mapAndUnzipUs f (x:xs)
161 = f x `thenUs` \ (r1, r2) ->
162 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
163 returnUs (r1:rs1, r2:rs2)
165 mapAndUnzip3Us f [] = returnUs ([],[],[])
166 mapAndUnzip3Us f (x:xs)
167 = f x `thenUs` \ (r1, r2, r3) ->
168 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
169 returnUs (r1:rs1, r2:rs2, r3:rs3)
171 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
173 = m `thenUs` \ result ->
175 Nothing -> returnUs Nothing
178 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
183 mapAccumLUs f b [] = returnUs (b, [])
184 mapAccumLUs f b (x:xs)
185 = f b x `thenUs` \ (b__2, x__2) ->
186 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
187 returnUs (b__3, x__2:xs__2)