2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
9 UniqSupply, -- Abstractly
11 uniqFromSupply, uniqsFromSupply, -- basic ops
13 UniqSM, -- type: unique supply monad
14 initUs, initUs_, thenUs, thenUs_, returnUs, fixUs, getUs, withUs,
15 getUniqueUs, getUniquesUs,
16 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
17 thenMaybeUs, mapAccumLUs,
18 lazyThenUs, lazyMapUs,
24 #include "HsVersions.h"
29 #if __GLASGOW_HASKELL__ < 301
30 import IOBase ( IO(..), IOResult(..) )
40 %************************************************************************
42 \subsection{Splittable Unique supply: @UniqSupply@}
44 %************************************************************************
46 %************************************************************************
48 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
50 %************************************************************************
52 A value of type @UniqSupply@ is unique, and it can
53 supply {\em one} distinct @Unique@. Also, from the supply, one can
54 also manufacture an arbitrary number of further @UniqueSupplies@,
55 which will be distinct from the first and from all others.
59 = MkSplitUniqSupply Int -- make the Unique with this
61 -- when split => these two supplies
65 mkSplitUniqSupply :: Char -> IO UniqSupply
67 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
68 uniqFromSupply :: UniqSupply -> Unique
69 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
73 mkSplitUniqSupply (C# c#)
75 #if __GLASGOW_HASKELL__ >= 503
76 mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
78 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
80 -- here comes THE MAGIC:
82 -- This is one of the most hammered bits in the whole compiler
84 = unsafeInterleaveIO (
85 mk_unique >>= \ uniq ->
86 mk_supply# >>= \ s1 ->
87 mk_supply# >>= \ s2 ->
88 return (MkSplitUniqSupply uniq s1 s2)
91 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
92 return (I# (w2i (mask# `or#` u#)))
96 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
100 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
101 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
104 %************************************************************************
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
108 %************************************************************************
111 type UniqSM result = UniqSupply -> (result, UniqSupply)
113 -- the initUs function also returns the final UniqSupply; initUs_ drops it
114 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
115 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
117 initUs_ :: UniqSupply -> UniqSM a -> a
118 initUs_ init_us m = case m init_us of { (r,us) -> r }
120 {-# INLINE thenUs #-}
121 {-# INLINE lazyThenUs #-}
122 {-# INLINE returnUs #-}
123 {-# INLINE splitUniqSupply #-}
126 @thenUs@ is where we split the @UniqSupply@.
128 fixUs :: (a -> UniqSM a) -> UniqSM a
130 = (r,us') where (r,us') = m r us
132 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
134 = case (expr us) of { (result, us') -> cont result us' }
136 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
137 lazyThenUs expr cont us
138 = let (result, us') = expr us in cont result us'
140 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
142 = case (expr us) of { (_, us') -> cont us' }
145 returnUs :: a -> UniqSM a
146 returnUs result us = (result, us)
148 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
149 withUs f us = f us -- Ha ha!
151 getUs :: UniqSM UniqSupply
152 getUs us = splitUniqSupply us
154 getUniqueUs :: UniqSM Unique
155 getUniqueUs us = case splitUniqSupply us of
156 (us1,us2) -> (uniqFromSupply us1, us2)
158 getUniquesUs :: UniqSM [Unique]
159 getUniquesUs us = case splitUniqSupply us of
160 (us1,us2) -> (uniqsFromSupply us1, us2)
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165 mapUs f [] = returnUs []
167 = f x `thenUs` \ r ->
168 mapUs f xs `thenUs` \ rs ->
171 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 lazyMapUs f [] = returnUs []
174 = f x `lazyThenUs` \ r ->
175 lazyMapUs f xs `lazyThenUs` \ rs ->
178 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
179 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
181 mapAndUnzipUs f [] = returnUs ([],[])
182 mapAndUnzipUs f (x:xs)
183 = f x `thenUs` \ (r1, r2) ->
184 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
185 returnUs (r1:rs1, r2:rs2)
187 mapAndUnzip3Us f [] = returnUs ([],[],[])
188 mapAndUnzip3Us f (x:xs)
189 = f x `thenUs` \ (r1, r2, r3) ->
190 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
191 returnUs (r1:rs1, r2:rs2, r3:rs3)
193 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
195 = m `thenUs` \ result ->
197 Nothing -> returnUs Nothing
200 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
205 mapAccumLUs f b [] = returnUs (b, [])
206 mapAccumLUs f b (x:xs)
207 = f b x `thenUs` \ (b__2, x__2) ->
208 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
209 returnUs (b__3, x__2:xs__2)