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,
21 splitUniqSupply, listSplitUniqSupply
24 #include "HsVersions.h"
29 import System.IO.Unsafe ( unsafeInterleaveIO )
31 #if __GLASGOW_HASKELL__ >= 607
32 import GHC.IOBase (unsafeDupableInterleaveIO)
34 unsafeDupableInterleaveIO :: IO a -> IO a
35 unsafeDupableInterleaveIO = unsafeInterleaveIO
44 %************************************************************************
46 \subsection{Splittable Unique supply: @UniqSupply@}
48 %************************************************************************
50 A value of type @UniqSupply@ is unique, and it can
51 supply {\em one} distinct @Unique@. Also, from the supply, one can
52 also manufacture an arbitrary number of further @UniqueSupplies@,
53 which will be distinct from the first and from all others.
57 = MkSplitUniqSupply Int# -- make the Unique with this
59 -- when split => these two supplies
63 mkSplitUniqSupply :: Char -> IO UniqSupply
65 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
66 listSplitUniqSupply :: UniqSupply -> [UniqSupply] -- Infinite
67 uniqFromSupply :: UniqSupply -> Unique
68 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
72 mkSplitUniqSupply (C# c#)
74 mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
75 -- here comes THE MAGIC:
77 -- This is one of the most hammered bits in the whole compiler
79 = unsafeDupableInterleaveIO (
80 genSymZh >>= \ (I# u#) ->
81 mk_supply# >>= \ s1 ->
82 mk_supply# >>= \ s2 ->
83 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
88 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
90 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
91 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
95 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (I# n)
96 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
99 %************************************************************************
101 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
103 %************************************************************************
106 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
108 instance Monad UniqSM where
113 -- the initUs function also returns the final UniqSupply; initUs_ drops it
114 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
115 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
117 initUs_ :: UniqSupply -> UniqSM a -> a
118 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
120 {-# INLINE thenUs #-}
121 {-# INLINE lazyThenUs #-}
122 {-# INLINE returnUs #-}
123 {-# INLINE splitUniqSupply #-}
126 @thenUs@ is where we split the @UniqSupply@.
128 fixUs :: (a -> UniqSM a) -> UniqSM a
129 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
131 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
132 thenUs (USM expr) cont
133 = USM (\us -> case (expr us) of
134 (result, us') -> unUSM (cont result) us')
136 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
137 lazyThenUs (USM expr) cont
138 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
140 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
141 thenUs_ (USM expr) (USM cont)
142 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
145 returnUs :: a -> UniqSM a
146 returnUs result = USM (\us -> (result, us))
148 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
149 withUs f = USM (\us -> f us) -- Ha ha!
151 getUs :: UniqSM UniqSupply
152 getUs = USM (\us -> splitUniqSupply us)
154 getUniqueUs :: UniqSM Unique
155 getUniqueUs = USM (\us -> case splitUniqSupply us of
156 (us1,us2) -> (uniqFromSupply us1, us2))
158 getUniquesUs :: UniqSM [Unique]
159 getUniquesUs = USM (\us -> case splitUniqSupply us of
160 (us1,us2) -> (uniqsFromSupply us1, us2))
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165 mapUs f [] = returnUs []
167 = f x `thenUs` \ r ->
168 mapUs f xs `thenUs` \ rs ->
171 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 lazyMapUs f [] = returnUs []
174 = f x `lazyThenUs` \ r ->
175 lazyMapUs f xs `lazyThenUs` \ rs ->
178 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
179 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
181 mapAndUnzipUs f [] = returnUs ([],[])
182 mapAndUnzipUs f (x:xs)
183 = f x `thenUs` \ (r1, r2) ->
184 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
185 returnUs (r1:rs1, r2:rs2)
187 mapAndUnzip3Us f [] = returnUs ([],[],[])
188 mapAndUnzip3Us f (x:xs)
189 = f x `thenUs` \ (r1, r2, r3) ->
190 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
191 returnUs (r1:rs1, r2:rs2, r3:rs3)
193 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
195 = m `thenUs` \ result ->
197 Nothing -> returnUs Nothing
200 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
205 mapAccumLUs f b [] = returnUs (b, [])
206 mapAccumLUs f b (x:xs)
207 = f b x `thenUs` \ (b__2, x__2) ->
208 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
209 returnUs (b__3, x__2:xs__2)