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,
26 module MonadUtils, mapAndUnzipM,
30 splitUniqSupply, listSplitUniqSupply
33 #include "HsVersions.h"
38 #if __GLASGOW_HASKELL__ >= 607
39 import GHC.IOBase (unsafeDupableInterleaveIO)
41 import System.IO.Unsafe ( unsafeInterleaveIO )
42 unsafeDupableInterleaveIO :: IO a -> IO a
43 unsafeDupableInterleaveIO = unsafeInterleaveIO
49 %************************************************************************
51 \subsection{Splittable Unique supply: @UniqSupply@}
53 %************************************************************************
55 A value of type @UniqSupply@ is unique, and it can
56 supply {\em one} distinct @Unique@. Also, from the supply, one can
57 also manufacture an arbitrary number of further @UniqueSupplies@,
58 which will be distinct from the first and from all others.
62 = MkSplitUniqSupply FastInt -- make the Unique with this
64 -- when split => these two supplies
68 mkSplitUniqSupply :: Char -> IO UniqSupply
70 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
71 listSplitUniqSupply :: UniqSupply -> [UniqSupply] -- Infinite
72 uniqFromSupply :: UniqSupply -> Unique
73 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
78 = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
80 -- here comes THE MAGIC:
82 -- This is one of the most hammered bits in the whole compiler
84 = unsafeDupableInterleaveIO (
85 genSymZh >>= \ u_ -> case iUnbox u_ of { u -> (
88 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
93 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
95 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
96 listSplitUniqSupply (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
100 uniqFromSupply (MkSplitUniqSupply n _ _) = mkUniqueGrimily (iBox n)
101 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
104 %************************************************************************
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
108 %************************************************************************
111 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
113 instance Monad UniqSM where
118 -- the initUs function also returns the final UniqSupply; initUs_ drops it
119 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
120 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
122 initUs_ :: UniqSupply -> UniqSM a -> a
123 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
125 {-# INLINE thenUs #-}
126 {-# INLINE lazyThenUs #-}
127 {-# INLINE returnUs #-}
128 {-# INLINE splitUniqSupply #-}
131 @thenUs@ is where we split the @UniqSupply@.
133 fixUs :: (a -> UniqSM a) -> UniqSM a
134 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
136 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
137 thenUs (USM expr) cont
138 = USM (\us -> case (expr us) of
139 (result, us') -> unUSM (cont result) us')
141 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
142 lazyThenUs (USM expr) cont
143 = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
145 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
146 thenUs_ (USM expr) (USM cont)
147 = USM (\us -> case (expr us) of { (_, us') -> cont us' })
150 returnUs :: a -> UniqSM a
151 returnUs result = USM (\us -> (result, us))
153 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
154 withUs f = USM (\us -> f us) -- Ha ha!
156 getUs :: UniqSM UniqSupply
157 getUs = USM (\us -> splitUniqSupply us)
159 -- | A monad for generating unique identifiers
160 class Monad m => MonadUnique m where
161 -- | Get a new UniqueSupply
162 getUniqueSupplyM :: m UniqSupply
163 -- | Get a new unique identifier
164 getUniqueM :: m Unique
165 -- | Get an infinite list of new unique identifiers
166 getUniquesM :: m [Unique]
168 getUniqueM = liftM uniqFromSupply getUniqueSupplyM
169 getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
171 instance MonadUnique UniqSM where
172 getUniqueSupplyM = USM (\us -> splitUniqSupply us)
173 getUniqueM = getUniqueUs
174 getUniquesM = getUniquesUs
176 getUniqueUs :: UniqSM Unique
177 getUniqueUs = USM (\us -> case splitUniqSupply us of
178 (us1,us2) -> (uniqFromSupply us1, us2))
180 getUniquesUs :: UniqSM [Unique]
181 getUniquesUs = USM (\us -> case splitUniqSupply us of
182 (us1,us2) -> (uniqsFromSupply us1, us2))
186 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
187 mapUs f [] = returnUs []
189 = f x `thenUs` \ r ->
190 mapUs f xs `thenUs` \ rs ->
193 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
194 lazyMapUs f [] = returnUs []
196 = f x `lazyThenUs` \ r ->
197 lazyMapUs f xs `lazyThenUs` \ rs ->
200 mapAndUnzipUs :: (a -> UniqSM (b,c)) -> [a] -> UniqSM ([b],[c])
201 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
203 mapAndUnzipUs f [] = returnUs ([],[])
204 mapAndUnzipUs f (x:xs)
205 = f x `thenUs` \ (r1, r2) ->
206 mapAndUnzipUs f xs `thenUs` \ (rs1, rs2) ->
207 returnUs (r1:rs1, r2:rs2)
209 mapAndUnzip3Us f [] = returnUs ([],[],[])
210 mapAndUnzip3Us f (x:xs)
211 = f x `thenUs` \ (r1, r2, r3) ->
212 mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
213 returnUs (r1:rs1, r2:rs2, r3:rs3)
215 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
217 = m `thenUs` \ result ->
219 Nothing -> returnUs Nothing
222 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
227 mapAccumLUs f b [] = returnUs (b, [])
228 mapAccumLUs f b (x:xs)
229 = f b x `thenUs` \ (b__2, x__2) ->
230 mapAccumLUs f b__2 xs `thenUs` \ (b__3, xs__2) ->
231 returnUs (b__3, x__2:xs__2)