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 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
77 -- here comes THE MAGIC:
79 -- This is one of the most hammered bits in the whole compiler
81 = unsafeInterleaveIO (
82 mk_unique >>= \ uniq ->
83 mk_supply# >>= \ s1 ->
84 mk_supply# >>= \ s2 ->
85 return (MkSplitUniqSupply uniq s1 s2)
88 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
89 return (I# (w2i (mask# `or#` u#)))
93 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
97 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
98 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
101 %************************************************************************
103 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
105 %************************************************************************
108 type UniqSM result = UniqSupply -> (result, UniqSupply)
110 -- the initUs function also returns the final UniqSupply; initUs_ drops it
111 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
112 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
114 initUs_ :: UniqSupply -> UniqSM a -> a
115 initUs_ init_us m = case m init_us of { (r,us) -> r }
117 {-# INLINE thenUs #-}
118 {-# INLINE lazyThenUs #-}
119 {-# INLINE returnUs #-}
120 {-# INLINE splitUniqSupply #-}
123 @thenUs@ is where we split the @UniqSupply@.
125 fixUs :: (a -> UniqSM a) -> UniqSM a
127 = (r,us') where (r,us') = m r us
129 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
131 = case (expr us) of { (result, us') -> cont result us' }
133 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
134 lazyThenUs expr cont us
135 = let (result, us') = expr us in cont result us'
137 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
139 = case (expr us) of { (_, us') -> cont us' }
142 returnUs :: a -> UniqSM a
143 returnUs result us = (result, us)
145 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
146 withUs f us = f us -- Ha ha!
148 getUs :: UniqSM UniqSupply
149 getUs us = splitUniqSupply us
151 getUniqueUs :: UniqSM Unique
152 getUniqueUs us = case splitUniqSupply us of
153 (us1,us2) -> (uniqFromSupply us1, us2)
155 getUniquesUs :: UniqSM [Unique]
156 getUniquesUs us = case splitUniqSupply us of
157 (us1,us2) -> (uniqsFromSupply us1, us2)
161 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
162 mapUs f [] = returnUs []
164 = f x `thenUs` \ r ->
165 mapUs f xs `thenUs` \ rs ->
168 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
169 lazyMapUs f [] = returnUs []
171 = f x `lazyThenUs` \ r ->
172 lazyMapUs f xs `lazyThenUs` \ rs ->
175 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
176 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
178 mapAndUnzipUs f [] = returnUs ([],[])
179 mapAndUnzipUs f (x:xs)
180 = f x `thenUs` \ (r1, r2) ->
181 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
182 returnUs (r1:rs1, r2:rs2)
184 mapAndUnzip3Us f [] = returnUs ([],[],[])
185 mapAndUnzip3Us f (x:xs)
186 = f x `thenUs` \ (r1, r2, r3) ->
187 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
188 returnUs (r1:rs1, r2:rs2, r3:rs3)
190 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
192 = m `thenUs` \ result ->
194 Nothing -> returnUs Nothing
197 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
202 mapAccumLUs f b [] = returnUs (b, [])
203 mapAccumLUs f b (x:xs)
204 = f b x `thenUs` \ (b__2, x__2) ->
205 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
206 returnUs (b__3, x__2:xs__2)