Make a SplitUniqSupply contain an Int# rather than an Int
[ghc-hetmet.git] / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
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
22   ) where
23
24 #include "HsVersions.h"
25
26 import Unique
27
28 import GLAEXTS
29 import UNSAFE_IO        ( unsafeInterleaveIO )
30
31 w2i x = word2Int# x
32 i2w x = int2Word# x
33 i2w_s x = (x :: Int#)
34 \end{code}
35
36
37 %************************************************************************
38 %*                                                                      *
39 \subsection{Splittable Unique supply: @UniqSupply@}
40 %*                                                                      *
41 %************************************************************************
42
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.
47
48 \begin{code}
49 data UniqSupply
50   = MkSplitUniqSupply Int#      -- make the Unique with this
51                    UniqSupply UniqSupply
52                                 -- when split => these two supplies
53 \end{code}
54
55 \begin{code}
56 mkSplitUniqSupply :: Char -> IO UniqSupply
57
58 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
59 uniqFromSupply  :: UniqSupply -> Unique
60 uniqsFromSupply :: UniqSupply -> [Unique]       -- Infinite
61 \end{code}
62
63 \begin{code}
64 mkSplitUniqSupply (C# c#)
65   = let
66 #if __GLASGOW_HASKELL__ >= 503
67         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
68 #else
69         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
70 #endif
71         -- here comes THE MAGIC:
72
73         -- This is one of the most hammered bits in the whole compiler
74         mk_supply#
75           = unsafeInterleaveIO (
76                 genSymZh    >>= \ (W# u#) ->
77                 mk_supply#  >>= \ s1 ->
78                 mk_supply#  >>= \ s2 ->
79                 return (MkSplitUniqSupply (w2i (mask# `or#` u#)) s1 s2)
80             )
81     in
82     mk_supply#
83
84 foreign import ccall unsafe "genSymZh" genSymZh :: IO Word
85
86 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
87 \end{code}
88
89 \begin{code}
90 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (I# n)
91 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
92 \end{code}
93
94 %************************************************************************
95 %*                                                                      *
96 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
97 %*                                                                      *
98 %************************************************************************
99
100 \begin{code}
101 type UniqSM result = UniqSupply -> (result, UniqSupply)
102
103 -- the initUs function also returns the final UniqSupply; initUs_ drops it
104 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
105 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
106
107 initUs_ :: UniqSupply -> UniqSM a -> a
108 initUs_ init_us m = case m init_us of { (r,us) -> r }
109
110 {-# INLINE thenUs #-}
111 {-# INLINE lazyThenUs #-}
112 {-# INLINE returnUs #-}
113 {-# INLINE splitUniqSupply #-}
114 \end{code}
115
116 @thenUs@ is where we split the @UniqSupply@.
117 \begin{code}
118 fixUs :: (a -> UniqSM a) -> UniqSM a
119 fixUs m us
120   = (r,us')  where  (r,us') = m r us
121
122 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
123 thenUs expr cont us
124   = case (expr us) of { (result, us') -> cont result us' }
125
126 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
127 lazyThenUs expr cont us
128   = let (result, us') = expr us in cont result us'
129
130 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
131 thenUs_ expr cont us
132   = case (expr us) of { (_, us') -> cont us' }
133
134
135 returnUs :: a -> UniqSM a
136 returnUs result us = (result, us)
137
138 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
139 withUs f us = f us      -- Ha ha!
140                 
141 getUs :: UniqSM UniqSupply
142 getUs us = splitUniqSupply us
143
144 getUniqueUs :: UniqSM Unique
145 getUniqueUs us = case splitUniqSupply us of
146                    (us1,us2) -> (uniqFromSupply us1, us2)
147
148 getUniquesUs :: UniqSM [Unique]
149 getUniquesUs us = case splitUniqSupply us of
150                       (us1,us2) -> (uniqsFromSupply us1, us2)
151 \end{code}
152
153 \begin{code}
154 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
155 mapUs f []     = returnUs []
156 mapUs f (x:xs)
157   = f x         `thenUs` \ r  ->
158     mapUs f xs  `thenUs` \ rs ->
159     returnUs (r:rs)
160
161 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
162 lazyMapUs f []     = returnUs []
163 lazyMapUs f (x:xs)
164   = f x             `lazyThenUs` \ r  ->
165     lazyMapUs f xs  `lazyThenUs` \ rs ->
166     returnUs (r:rs)
167
168 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
169 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
170
171 mapAndUnzipUs f [] = returnUs ([],[])
172 mapAndUnzipUs f (x:xs)
173   = f x                 `thenUs` \ (r1,  r2)  ->
174     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
175     returnUs (r1:rs1, r2:rs2)
176
177 mapAndUnzip3Us f [] = returnUs ([],[],[])
178 mapAndUnzip3Us f (x:xs)
179   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
180     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
181     returnUs (r1:rs1, r2:rs2, r3:rs3)
182
183 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
184 thenMaybeUs m k
185   = m   `thenUs` \ result ->
186     case result of
187       Nothing -> returnUs Nothing
188       Just x  -> k x
189
190 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
191             -> acc
192             -> [x]
193             -> UniqSM (acc, [y])
194
195 mapAccumLUs f b []     = returnUs (b, [])
196 mapAccumLUs f b (x:xs)
197   = f b x                           `thenUs` \ (b__2, x__2) ->
198     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
199     returnUs (b__3, x__2:xs__2)
200 \end{code}