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 import UNSAFE_IO ( unsafeInterleaveIO )
37 %************************************************************************
39 \subsection{Splittable Unique supply: @UniqSupply@}
41 %************************************************************************
43 A value of type @UniqSupply@ is unique, and it can
44 supply {\em one} distinct @Unique@. Also, from the supply, one can
45 also manufacture an arbitrary number of further @UniqueSupplies@,
46 which will be distinct from the first and from all others.
50 = MkSplitUniqSupply Int -- make the Unique with this
52 -- when split => these two supplies
56 mkSplitUniqSupply :: Char -> IO UniqSupply
58 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
59 uniqFromSupply :: UniqSupply -> Unique
60 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
64 mkSplitUniqSupply (C# c#)
66 #if __GLASGOW_HASKELL__ >= 503
67 mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
69 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
71 -- here comes THE MAGIC:
73 -- This is one of the most hammered bits in the whole compiler
75 = unsafeInterleaveIO (
76 mk_unique >>= \ uniq ->
77 mk_supply# >>= \ s1 ->
78 mk_supply# >>= \ s2 ->
79 return (MkSplitUniqSupply uniq s1 s2)
83 #if __GLASGOW_HASKELL__ < 603
86 genSymZh >>= \ (W# u#) ->
87 return (I# (w2i (mask# `or#` u#)))
91 #if __GLASGOW_HASKELL__ >= 603
92 foreign import ccall unsafe "genSymZh" genSymZh :: IO Word
95 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
99 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
100 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
103 %************************************************************************
105 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
107 %************************************************************************
110 type UniqSM result = UniqSupply -> (result, UniqSupply)
112 -- the initUs function also returns the final UniqSupply; initUs_ drops it
113 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
114 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
116 initUs_ :: UniqSupply -> UniqSM a -> a
117 initUs_ init_us m = case m init_us of { (r,us) -> r }
119 {-# INLINE thenUs #-}
120 {-# INLINE lazyThenUs #-}
121 {-# INLINE returnUs #-}
122 {-# INLINE splitUniqSupply #-}
125 @thenUs@ is where we split the @UniqSupply@.
127 fixUs :: (a -> UniqSM a) -> UniqSM a
129 = (r,us') where (r,us') = m r us
131 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
133 = case (expr us) of { (result, us') -> cont result us' }
135 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
136 lazyThenUs expr cont us
137 = let (result, us') = expr us in cont result us'
139 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
141 = case (expr us) of { (_, us') -> cont us' }
144 returnUs :: a -> UniqSM a
145 returnUs result us = (result, us)
147 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
148 withUs f us = f us -- Ha ha!
150 getUs :: UniqSM UniqSupply
151 getUs us = splitUniqSupply us
153 getUniqueUs :: UniqSM Unique
154 getUniqueUs us = case splitUniqSupply us of
155 (us1,us2) -> (uniqFromSupply us1, us2)
157 getUniquesUs :: UniqSM [Unique]
158 getUniquesUs us = case splitUniqSupply us of
159 (us1,us2) -> (uniqsFromSupply us1, us2)
163 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
164 mapUs f [] = returnUs []
166 = f x `thenUs` \ r ->
167 mapUs f xs `thenUs` \ rs ->
170 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
171 lazyMapUs f [] = returnUs []
173 = f x `lazyThenUs` \ r ->
174 lazyMapUs f xs `lazyThenUs` \ rs ->
177 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
178 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
180 mapAndUnzipUs f [] = returnUs ([],[])
181 mapAndUnzipUs f (x:xs)
182 = f x `thenUs` \ (r1, r2) ->
183 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
184 returnUs (r1:rs1, r2:rs2)
186 mapAndUnzip3Us f [] = returnUs ([],[],[])
187 mapAndUnzip3Us f (x:xs)
188 = f x `thenUs` \ (r1, r2, r3) ->
189 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
190 returnUs (r1:rs1, r2:rs2, r3:rs3)
192 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
194 = m `thenUs` \ result ->
196 Nothing -> returnUs Nothing
199 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
204 mapAccumLUs f b [] = returnUs (b, [])
205 mapAccumLUs f b (x:xs)
206 = f b x `thenUs` \ (b__2, x__2) ->
207 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
208 returnUs (b__3, x__2:xs__2)