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 A value of type @UniqSupply@ is unique, and it can
47 supply {\em one} distinct @Unique@. Also, from the supply, one can
48 also manufacture an arbitrary number of further @UniqueSupplies@,
49 which will be distinct from the first and from all others.
53 = MkSplitUniqSupply Int -- make the Unique with this
55 -- when split => these two supplies
59 mkSplitUniqSupply :: Char -> IO UniqSupply
61 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
62 uniqFromSupply :: UniqSupply -> Unique
63 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
67 mkSplitUniqSupply (C# c#)
69 #if __GLASGOW_HASKELL__ >= 503
70 mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
72 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
74 -- here comes THE MAGIC:
76 -- This is one of the most hammered bits in the whole compiler
78 = unsafeInterleaveIO (
79 mk_unique >>= \ uniq ->
80 mk_supply# >>= \ s1 ->
81 mk_supply# >>= \ s2 ->
82 return (MkSplitUniqSupply uniq s1 s2)
85 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
86 return (I# (w2i (mask# `or#` u#)))
90 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
94 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
95 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
98 %************************************************************************
100 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
102 %************************************************************************
105 type UniqSM result = UniqSupply -> (result, UniqSupply)
107 -- the initUs function also returns the final UniqSupply; initUs_ drops it
108 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
109 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
111 initUs_ :: UniqSupply -> UniqSM a -> a
112 initUs_ init_us m = case m init_us of { (r,us) -> r }
114 {-# INLINE thenUs #-}
115 {-# INLINE lazyThenUs #-}
116 {-# INLINE returnUs #-}
117 {-# INLINE splitUniqSupply #-}
120 @thenUs@ is where we split the @UniqSupply@.
122 fixUs :: (a -> UniqSM a) -> UniqSM a
124 = (r,us') where (r,us') = m r us
126 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
128 = case (expr us) of { (result, us') -> cont result us' }
130 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
131 lazyThenUs expr cont us
132 = let (result, us') = expr us in cont result us'
134 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
136 = case (expr us) of { (_, us') -> cont us' }
139 returnUs :: a -> UniqSM a
140 returnUs result us = (result, us)
142 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
143 withUs f us = f us -- Ha ha!
145 getUs :: UniqSM UniqSupply
146 getUs us = splitUniqSupply us
148 getUniqueUs :: UniqSM Unique
149 getUniqueUs us = case splitUniqSupply us of
150 (us1,us2) -> (uniqFromSupply us1, us2)
152 getUniquesUs :: UniqSM [Unique]
153 getUniquesUs us = case splitUniqSupply us of
154 (us1,us2) -> (uniqsFromSupply us1, us2)
158 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
159 mapUs f [] = returnUs []
161 = f x `thenUs` \ r ->
162 mapUs f xs `thenUs` \ rs ->
165 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
166 lazyMapUs f [] = returnUs []
168 = f x `lazyThenUs` \ r ->
169 lazyMapUs f xs `lazyThenUs` \ rs ->
172 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
173 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
175 mapAndUnzipUs f [] = returnUs ([],[])
176 mapAndUnzipUs f (x:xs)
177 = f x `thenUs` \ (r1, r2) ->
178 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
179 returnUs (r1:rs1, r2:rs2)
181 mapAndUnzip3Us f [] = returnUs ([],[],[])
182 mapAndUnzip3Us f (x:xs)
183 = f x `thenUs` \ (r1, r2, r3) ->
184 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
185 returnUs (r1:rs1, r2:rs2, r3:rs3)
187 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
189 = m `thenUs` \ result ->
191 Nothing -> returnUs Nothing
194 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
199 mapAccumLUs f b [] = returnUs (b, [])
200 mapAccumLUs f b (x:xs)
201 = f b x `thenUs` \ (b__2, x__2) ->
202 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
203 returnUs (b__3, x__2:xs__2)