2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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 System.IO.Unsafe ( 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 genSymZh >>= \ (W# u#) ->
77 mk_supply# >>= \ s1 ->
78 mk_supply# >>= \ s2 ->
79 return (MkSplitUniqSupply (w2i (mask# `or#` u#)) s1 s2)
84 foreign import ccall unsafe "genSymZh" genSymZh :: IO Word
86 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
90 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (I# n)
91 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
94 %************************************************************************
96 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
98 %************************************************************************
101 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
103 instance Monad UniqSM where
108 -- the initUs function also returns the final UniqSupply; initUs_ drops it
109 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
110 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
112 initUs_ :: UniqSupply -> UniqSM a -> a
113 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
115 {-# INLINE thenUs #-}
116 {-# INLINE lazyThenUs #-}
117 {-# INLINE returnUs #-}
118 {-# INLINE splitUniqSupply #-}
121 @thenUs@ is where we split the @UniqSupply@.
123 fixUs :: (a -> UniqSM a) -> UniqSM a
124 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
126 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
127 thenUs (USM expr) cont
128 = USM (\us -> case (expr us) of
129 (result, us') -> unUSM (cont result) us')
131 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
132 lazyThenUs (USM expr) cont
133 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
135 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
136 thenUs_ (USM expr) (USM cont)
137 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
140 returnUs :: a -> UniqSM a
141 returnUs result = USM (\us -> (result, us))
143 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
144 withUs f = USM (\us -> f us) -- Ha ha!
146 getUs :: UniqSM UniqSupply
147 getUs = USM (\us -> splitUniqSupply us)
149 getUniqueUs :: UniqSM Unique
150 getUniqueUs = USM (\us -> case splitUniqSupply us of
151 (us1,us2) -> (uniqFromSupply us1, us2))
153 getUniquesUs :: UniqSM [Unique]
154 getUniquesUs = USM (\us -> case splitUniqSupply us of
155 (us1,us2) -> (uniqsFromSupply us1, us2))
159 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
160 mapUs f [] = returnUs []
162 = f x `thenUs` \ r ->
163 mapUs f xs `thenUs` \ rs ->
166 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
167 lazyMapUs f [] = returnUs []
169 = f x `lazyThenUs` \ r ->
170 lazyMapUs f xs `lazyThenUs` \ rs ->
173 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
174 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
176 mapAndUnzipUs f [] = returnUs ([],[])
177 mapAndUnzipUs f (x:xs)
178 = f x `thenUs` \ (r1, r2) ->
179 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
180 returnUs (r1:rs1, r2:rs2)
182 mapAndUnzip3Us f [] = returnUs ([],[],[])
183 mapAndUnzip3Us f (x:xs)
184 = f x `thenUs` \ (r1, r2, r3) ->
185 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
186 returnUs (r1:rs1, r2:rs2, r3:rs3)
188 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
190 = m `thenUs` \ result ->
192 Nothing -> returnUs Nothing
195 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
200 mapAccumLUs f b [] = returnUs (b, [])
201 mapAccumLUs f b (x:xs)
202 = f b x `thenUs` \ (b__2, x__2) ->
203 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
204 returnUs (b__3, x__2:xs__2)