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, setUs,
15 getUniqueUs, getUniquesUs,
16 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
17 thenMaybeUs, mapAccumLUs,
18 lazyThenUs, lazyMapUs,
24 #include "HsVersions.h"
27 import Panic ( panic )
31 #if __GLASGOW_HASKELL__ < 301
32 import IOBase ( IO(..), IOResult(..) )
42 %************************************************************************
44 \subsection{Splittable Unique supply: @UniqSupply@}
46 %************************************************************************
48 %************************************************************************
50 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
52 %************************************************************************
54 A value of type @UniqSupply@ is unique, and it can
55 supply {\em one} distinct @Unique@. Also, from the supply, one can
56 also manufacture an arbitrary number of further @UniqueSupplies@,
57 which will be distinct from the first and from all others.
61 = MkSplitUniqSupply Int -- make the Unique with this
63 -- when split => these two supplies
67 mkSplitUniqSupply :: Char -> IO UniqSupply
69 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
70 uniqFromSupply :: UniqSupply -> Unique
71 uniqsFromSupply :: Int -> UniqSupply -> [Unique]
75 mkSplitUniqSupply (C# c#)
77 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
79 -- here comes THE MAGIC:
81 -- This is one of the most hammered bits in the whole compiler
83 = unsafeInterleaveIO (
84 mk_unique >>= \ uniq ->
85 mk_supply# >>= \ s1 ->
86 mk_supply# >>= \ s2 ->
87 return (MkSplitUniqSupply uniq s1 s2)
90 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
91 return (I# (w2i (mask# `or#` u#)))
95 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
99 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
101 uniqsFromSupply (I# i) supply = i `get_from` supply
104 get_from n (MkSplitUniqSupply (I# u) _ s2)
105 = mkUniqueGrimily u : get_from (n -# 1#) s2
108 %************************************************************************
110 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
112 %************************************************************************
115 type UniqSM result = UniqSupply -> (result, UniqSupply)
117 -- the initUs function also returns the final UniqSupply; initUs_ drops it
118 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
119 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
121 initUs_ :: UniqSupply -> UniqSM a -> a
122 initUs_ init_us m = case m init_us of { (r,us) -> r }
124 {-# INLINE thenUs #-}
125 {-# INLINE lazyThenUs #-}
126 {-# INLINE returnUs #-}
127 {-# INLINE splitUniqSupply #-}
130 @thenUs@ is where we split the @UniqSupply@.
132 fixUs :: (a -> UniqSM a) -> UniqSM a
134 = (r,us') where (r,us') = m r us
136 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
138 = case (expr us) of { (result, us') -> cont result us' }
140 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
141 lazyThenUs expr cont us
142 = let (result, us') = expr us in cont result us'
144 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
146 = case (expr us) of { (_, us') -> cont us' }
149 returnUs :: a -> UniqSM a
150 returnUs result us = (result, us)
152 getUs :: UniqSM UniqSupply
153 getUs us = (us, panic "getUs: bad supply")
155 setUs :: UniqSupply -> UniqSM ()
156 setUs us old_us = ((), us)
158 getUniqueUs :: UniqSM Unique
159 getUniqueUs us = case splitUniqSupply us of
160 (us1,us2) -> (uniqFromSupply us1, us2)
162 getUniquesUs :: Int -> UniqSM [Unique]
163 getUniquesUs n us = case splitUniqSupply us of
164 (us1,us2) -> (uniqsFromSupply n us1, us2)
168 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
169 mapUs f [] = returnUs []
171 = f x `thenUs` \ r ->
172 mapUs f xs `thenUs` \ rs ->
175 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
176 lazyMapUs f [] = returnUs []
178 = f x `lazyThenUs` \ r ->
179 lazyMapUs f xs `lazyThenUs` \ rs ->
182 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
183 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
185 mapAndUnzipUs f [] = returnUs ([],[])
186 mapAndUnzipUs f (x:xs)
187 = f x `thenUs` \ (r1, r2) ->
188 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
189 returnUs (r1:rs1, r2:rs2)
191 mapAndUnzip3Us f [] = returnUs ([],[],[])
192 mapAndUnzip3Us f (x:xs)
193 = f x `thenUs` \ (r1, r2, r3) ->
194 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
195 returnUs (r1:rs1, r2:rs2, r3:rs3)
197 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
199 = m `thenUs` \ result ->
201 Nothing -> returnUs Nothing
204 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
209 mapAccumLUs f b [] = returnUs (b, [])
210 mapAccumLUs f b (x:xs)
211 = f b x `thenUs` \ (b__2, x__2) ->
212 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
213 returnUs (b__3, x__2:xs__2)