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, thenUs, thenUs_, returnUs, fixUs, getUs, setUs,
15 getUniqueUs, getUniquesUs,
16 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
17 thenMaybeUs, mapAccumLUs,
23 #include "HsVersions.h"
26 import Panic ( panic )
30 #if __GLASGOW_HASKELL__ < 301
31 import IOBase ( IO(..), IOResult(..) )
41 %************************************************************************
43 \subsection{Splittable Unique supply: @UniqSupply@}
45 %************************************************************************
47 %************************************************************************
49 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
51 %************************************************************************
53 A value of type @UniqSupply@ is unique, and it can
54 supply {\em one} distinct @Unique@. Also, from the supply, one can
55 also manufacture an arbitrary number of further @UniqueSupplies@,
56 which will be distinct from the first and from all others.
60 = MkSplitUniqSupply Int -- make the Unique with this
62 -- when split => these two supplies
66 mkSplitUniqSupply :: Char -> IO UniqSupply
68 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
69 uniqFromSupply :: UniqSupply -> Unique
70 uniqsFromSupply :: Int -> UniqSupply -> [Unique]
74 mkSplitUniqSupply (C# c#)
76 mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
78 -- here comes THE MAGIC:
80 -- This is one of the most hammered bits in the whole compiler
82 = unsafeInterleaveIO (
83 mk_unique >>= \ uniq ->
84 mk_supply# >>= \ s1 ->
85 mk_supply# >>= \ s2 ->
86 return (MkSplitUniqSupply uniq s1 s2)
89 mk_unique = _ccall_ genSymZh >>= \ (W# u#) ->
90 return (I# (w2i (mask# `or#` u#)))
94 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
98 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
100 uniqsFromSupply (I# i) supply = i `get_from` supply
103 get_from n (MkSplitUniqSupply (I# u) _ s2)
104 = mkUniqueGrimily u : get_from (n -# 1#) s2
107 %************************************************************************
109 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
111 %************************************************************************
114 type UniqSM result = UniqSupply -> (result, UniqSupply)
116 -- the initUs function also returns the final UniqSupply
118 initUs :: UniqSupply -> UniqSM a -> a
120 initUs init_us m = case m init_us of { (r,_) -> r }
122 {-# INLINE thenUs #-}
123 {-# INLINE returnUs #-}
124 {-# INLINE splitUniqSupply #-}
127 @thenUs@ is where we split the @UniqSupply@.
129 fixUs :: (a -> UniqSM a) -> UniqSM a
131 = (r,us') where (r,us') = m r us
133 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
135 = case (expr us) of { (result, us') -> cont result us' }
137 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
139 = case (expr us) of { (_, us') -> cont us' }
141 returnUs :: a -> UniqSM a
142 returnUs result us = (result, us)
144 getUs :: UniqSM UniqSupply
145 getUs us = (us, panic "getUs: bad supply")
147 setUs :: UniqSupply -> UniqSM ()
148 setUs us old_us = ((), us)
150 getUniqueUs :: UniqSM Unique
151 getUniqueUs us = case splitUniqSupply us of
152 (us1,us2) -> (uniqFromSupply us1, us2)
154 getUniquesUs :: Int -> UniqSM [Unique]
155 getUniquesUs n us = case splitUniqSupply us of
156 (us1,us2) -> (uniqsFromSupply n us1, us2)
160 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
162 mapUs f [] = returnUs []
164 = f x `thenUs` \ r ->
165 mapUs f xs `thenUs` \ rs ->
168 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
169 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
171 mapAndUnzipUs f [] = returnUs ([],[])
172 mapAndUnzipUs f (x:xs)
173 = f x `thenUs` \ (r1, r2) ->
174 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
175 returnUs (r1:rs1, r2:rs2)
177 mapAndUnzip3Us f [] = returnUs ([],[],[])
178 mapAndUnzip3Us f (x:xs)
179 = f x `thenUs` \ (r1, r2, r3) ->
180 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
181 returnUs (r1:rs1, r2:rs2, r3:rs3)
183 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
185 = m `thenUs` \ result ->
187 Nothing -> returnUs Nothing
190 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
195 mapAccumLUs f b [] = returnUs (b, [])
196 mapAccumLUs f b (x:xs)
197 = f b x `thenUs` \ (b__2, x__2) ->
198 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
199 returnUs (b__3, x__2:xs__2)