2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
16 UniqSupply, -- Abstractly
18 uniqFromSupply, uniqsFromSupply, -- basic ops
20 UniqSM, -- type: unique supply monad
21 initUs, initUs_, thenUs, thenUs_, returnUs, fixUs, getUs, withUs,
22 getUniqueUs, getUniquesUs,
23 mapUs, mapAndUnzipUs, mapAndUnzip3Us,
24 thenMaybeUs, mapAccumLUs,
25 lazyThenUs, lazyMapUs,
28 splitUniqSupply, listSplitUniqSupply
31 #include "HsVersions.h"
36 import System.IO.Unsafe ( unsafeInterleaveIO )
38 #if __GLASGOW_HASKELL__ >= 607
39 import GHC.IOBase (unsafeDupableInterleaveIO)
41 unsafeDupableInterleaveIO :: IO a -> IO a
42 unsafeDupableInterleaveIO = unsafeInterleaveIO
51 %************************************************************************
53 \subsection{Splittable Unique supply: @UniqSupply@}
55 %************************************************************************
57 A value of type @UniqSupply@ is unique, and it can
58 supply {\em one} distinct @Unique@. Also, from the supply, one can
59 also manufacture an arbitrary number of further @UniqueSupplies@,
60 which will be distinct from the first and from all others.
64 = MkSplitUniqSupply Int# -- make the Unique with this
66 -- when split => these two supplies
70 mkSplitUniqSupply :: Char -> IO UniqSupply
72 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
73 listSplitUniqSupply :: UniqSupply -> [UniqSupply] -- Infinite
74 uniqFromSupply :: UniqSupply -> Unique
75 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
79 mkSplitUniqSupply (C# c#)
81 mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
82 -- here comes THE MAGIC:
84 -- This is one of the most hammered bits in the whole compiler
86 = unsafeDupableInterleaveIO (
87 genSymZh >>= \ (I# u#) ->
88 mk_supply# >>= \ s1 ->
89 mk_supply# >>= \ s2 ->
90 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
95 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
97 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
98 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
102 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (I# n)
103 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
106 %************************************************************************
108 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
110 %************************************************************************
113 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
115 instance Monad UniqSM where
120 -- the initUs function also returns the final UniqSupply; initUs_ drops it
121 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
122 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
124 initUs_ :: UniqSupply -> UniqSM a -> a
125 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
127 {-# INLINE thenUs #-}
128 {-# INLINE lazyThenUs #-}
129 {-# INLINE returnUs #-}
130 {-# INLINE splitUniqSupply #-}
133 @thenUs@ is where we split the @UniqSupply@.
135 fixUs :: (a -> UniqSM a) -> UniqSM a
136 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
138 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
139 thenUs (USM expr) cont
140 = USM (\us -> case (expr us) of
141 (result, us') -> unUSM (cont result) us')
143 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
144 lazyThenUs (USM expr) cont
145 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
147 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
148 thenUs_ (USM expr) (USM cont)
149 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
152 returnUs :: a -> UniqSM a
153 returnUs result = USM (\us -> (result, us))
155 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
156 withUs f = USM (\us -> f us) -- Ha ha!
158 getUs :: UniqSM UniqSupply
159 getUs = USM (\us -> splitUniqSupply us)
161 getUniqueUs :: UniqSM Unique
162 getUniqueUs = USM (\us -> case splitUniqSupply us of
163 (us1,us2) -> (uniqFromSupply us1, us2))
165 getUniquesUs :: UniqSM [Unique]
166 getUniquesUs = USM (\us -> case splitUniqSupply us of
167 (us1,us2) -> (uniqsFromSupply us1, us2))
171 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 mapUs f [] = returnUs []
174 = f x `thenUs` \ r ->
175 mapUs f xs `thenUs` \ rs ->
178 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
179 lazyMapUs f [] = returnUs []
181 = f x `lazyThenUs` \ r ->
182 lazyMapUs f xs `lazyThenUs` \ rs ->
185 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
186 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
188 mapAndUnzipUs f [] = returnUs ([],[])
189 mapAndUnzipUs f (x:xs)
190 = f x `thenUs` \ (r1, r2) ->
191 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
192 returnUs (r1:rs1, r2:rs2)
194 mapAndUnzip3Us f [] = returnUs ([],[],[])
195 mapAndUnzip3Us f (x:xs)
196 = f x `thenUs` \ (r1, r2, r3) ->
197 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
198 returnUs (r1:rs1, r2:rs2, r3:rs3)
200 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
202 = m `thenUs` \ result ->
204 Nothing -> returnUs Nothing
207 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
212 mapAccumLUs f b [] = returnUs (b, [])
213 mapAccumLUs f b (x:xs)
214 = f b x `thenUs` \ (b__2, x__2) ->
215 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
216 returnUs (b__3, x__2:xs__2)