8e3ac96ae540a40317b21584d2618ceb51e45f93
[ghc-hetmet.git] / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 \begin{code}
7 module UniqSupply (
8
9         UniqSupply,             -- Abstractly
10
11         uniqFromSupply, uniqsFromSupply,        -- basic ops
12
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,
19
20         mkSplitUniqSupply,
21         splitUniqSupply, listSplitUniqSupply
22   ) where
23
24 #include "HsVersions.h"
25
26 import Unique
27
28 import GHC.Exts
29 import System.IO.Unsafe ( unsafeInterleaveIO )
30
31 #if __GLASGOW_HASKELL__ >= 607
32 import GHC.IOBase (unsafeDupableInterleaveIO)
33 #else
34 unsafeDupableInterleaveIO :: IO a -> IO a
35 unsafeDupableInterleaveIO = unsafeInterleaveIO
36 #endif
37
38 w2i x = word2Int# x
39 i2w x = int2Word# x
40 i2w_s x = (x :: Int#)
41 \end{code}
42
43
44 %************************************************************************
45 %*                                                                      *
46 \subsection{Splittable Unique supply: @UniqSupply@}
47 %*                                                                      *
48 %************************************************************************
49
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.
54
55 \begin{code}
56 data UniqSupply
57   = MkSplitUniqSupply Int#      -- make the Unique with this
58                    UniqSupply UniqSupply
59                                 -- when split => these two supplies
60 \end{code}
61
62 \begin{code}
63 mkSplitUniqSupply :: Char -> IO UniqSupply
64
65 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
66 listSplitUniqSupply :: UniqSupply -> [UniqSupply]   -- Infinite
67 uniqFromSupply  :: UniqSupply -> Unique
68 uniqsFromSupply :: UniqSupply -> [Unique]       -- Infinite
69 \end{code}
70
71 \begin{code}
72 mkSplitUniqSupply (C# c#)
73   = let
74         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
75         -- here comes THE MAGIC:
76
77         -- This is one of the most hammered bits in the whole compiler
78         mk_supply#
79           = unsafeDupableInterleaveIO (
80                 genSymZh    >>= \ (I# u#) ->
81                 mk_supply#  >>= \ s1 ->
82                 mk_supply#  >>= \ s2 ->
83                 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
84             )
85     in
86     mk_supply#
87
88 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
89
90 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
91 listSplitUniqSupply  (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
92 \end{code}
93
94 \begin{code}
95 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (I# n)
96 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
97 \end{code}
98
99 %************************************************************************
100 %*                                                                      *
101 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
102 %*                                                                      *
103 %************************************************************************
104
105 \begin{code}
106 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
107
108 instance Monad UniqSM where
109   return = returnUs
110   (>>=) = thenUs
111   (>>)  = thenUs_
112
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) }
116
117 initUs_ :: UniqSupply -> UniqSM a -> a
118 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
119
120 {-# INLINE thenUs #-}
121 {-# INLINE lazyThenUs #-}
122 {-# INLINE returnUs #-}
123 {-# INLINE splitUniqSupply #-}
124 \end{code}
125
126 @thenUs@ is where we split the @UniqSupply@.
127 \begin{code}
128 fixUs :: (a -> UniqSM a) -> UniqSM a
129 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
130
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')
135
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')
139
140 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
141 thenUs_ (USM expr) (USM cont)
142   = USM (\us -> case (expr us) of { (_, us') -> cont us' })
143
144
145 returnUs :: a -> UniqSM a
146 returnUs result = USM (\us -> (result, us))
147
148 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
149 withUs f = USM (\us -> f us)    -- Ha ha!
150                 
151 getUs :: UniqSM UniqSupply
152 getUs = USM (\us -> splitUniqSupply us)
153
154 getUniqueUs :: UniqSM Unique
155 getUniqueUs = USM (\us -> case splitUniqSupply us of
156                            (us1,us2) -> (uniqFromSupply us1, us2))
157
158 getUniquesUs :: UniqSM [Unique]
159 getUniquesUs = USM (\us -> case splitUniqSupply us of
160                               (us1,us2) -> (uniqsFromSupply us1, us2))
161 \end{code}
162
163 \begin{code}
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165 mapUs f []     = returnUs []
166 mapUs f (x:xs)
167   = f x         `thenUs` \ r  ->
168     mapUs f xs  `thenUs` \ rs ->
169     returnUs (r:rs)
170
171 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 lazyMapUs f []     = returnUs []
173 lazyMapUs f (x:xs)
174   = f x             `lazyThenUs` \ r  ->
175     lazyMapUs f xs  `lazyThenUs` \ rs ->
176     returnUs (r:rs)
177
178 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
179 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
180
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)
186
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)
192
193 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
194 thenMaybeUs m k
195   = m   `thenUs` \ result ->
196     case result of
197       Nothing -> returnUs Nothing
198       Just x  -> k x
199
200 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
201             -> acc
202             -> [x]
203             -> UniqSM (acc, [y])
204
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)
210 \end{code}