76e5ab3e80f5f4b772b95e28bccac0bfdc099404
[ghc-hetmet.git] / ghc / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module UniqSupply (
10
11         UniqSupply,             -- Abstractly
12
13         getUnique, getUniques,  -- basic ops
14
15         SYN_IE(UniqSM),         -- type: unique supply monad
16         initUs, thenUs, returnUs, fixUs,
17         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
18         thenMaybeUs, mapAccumLUs,
19
20         mkSplitUniqSupply,
21         splitUniqSupply
22   ) where
23
24 IMP_Ubiq(){-uitous-}
25
26 import Unique
27 import Util
28
29 import PreludeGlaST
30
31 #if __GLASGOW_HASKELL__ >= 200
32 # define WHASH      GHCbase.W#
33 #else
34 # define WHASH      W#
35 #endif
36
37 w2i x = word2Int# x
38 i2w x = int2Word# x
39 i2w_s x = (x :: Int#)
40 \end{code}
41
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection{Splittable Unique supply: @UniqSupply@}
46 %*                                                                      *
47 %************************************************************************
48
49 %************************************************************************
50 %*                                                                      *
51 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
52 %*                                                                      *
53 %************************************************************************
54
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.
59
60 \begin{code}
61 data UniqSupply
62   = MkSplitUniqSupply Int       -- make the Unique with this
63                    UniqSupply UniqSupply
64                                 -- when split => these two supplies
65 \end{code}
66
67 \begin{code}
68 mkSplitUniqSupply :: Char -> IO UniqSupply
69
70 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
71 getUnique :: UniqSupply -> Unique
72 getUniques :: Int -> UniqSupply -> [Unique]
73 \end{code}
74
75 \begin{code}
76 mkSplitUniqSupply (C# c#)
77   = let
78         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
79
80         -- here comes THE MAGIC:
81
82         mk_supply#
83           = unsafeInterleavePrimIO {-unsafe_interleave-} (
84                 mk_unique   `thenPrimIO` \ uniq ->
85                 mk_supply#  `thenPrimIO` \ s1 ->
86                 mk_supply#  `thenPrimIO` \ s2 ->
87                 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
88             )
89           where
90 {-
91             -- inlined copy of unsafeInterleavePrimIO;
92             -- this is the single-most-hammered bit of code
93             -- in the compiler....
94             -- Too bad it's not 1.3-portable...
95             unsafe_interleave m s
96               = let
97                     (r, new_s) = m s
98                 in
99                 (r, s)
100 -}
101
102         mk_unique = _ccall_ genSymZh            `thenPrimIO` \ (WHASH u#) ->
103                     returnPrimIO (I# (w2i (mask# `or#` u#)))
104     in
105 #if __GLASGOW_HASKELL__ >= 200
106     primIOToIO mk_supply#       >>= \ s ->
107     return s
108 #else
109     mk_supply#  `thenPrimIO` \ s ->
110     return s
111 #endif
112
113 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
114 \end{code}
115
116 \begin{code}
117 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
118
119 getUniques (I# i) supply = i `get_from` supply
120   where
121     get_from 0# _ = []
122     get_from n (MkSplitUniqSupply (I# u) _ s2)
123       = mkUniqueGrimily u : get_from (n `minusInt#` 1#) s2
124 \end{code}
125
126 %************************************************************************
127 %*                                                                      *
128 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
129 %*                                                                      *
130 %************************************************************************
131
132 \begin{code}
133 type UniqSM result = UniqSupply -> result
134
135 -- the initUs function also returns the final UniqSupply
136
137 initUs :: UniqSupply -> UniqSM a -> a
138
139 initUs init_us m = m init_us
140
141 {-# INLINE thenUs #-}
142 {-# INLINE returnUs #-}
143 {-# INLINE splitUniqSupply #-}
144 \end{code}
145
146 @thenUs@ is where we split the @UniqSupply@.
147 \begin{code}
148 fixUs :: (a -> UniqSM a) -> UniqSM a
149 fixUs m us
150   = r  where  r = m r us
151
152 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
153
154 thenUs expr cont us
155   = case (splitUniqSupply us) of { (s1, s2) ->
156     case (expr s1)            of { result ->
157     cont result s2 }}
158 \end{code}
159
160 \begin{code}
161 returnUs :: a -> UniqSM a
162 returnUs result us = result
163
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165
166 mapUs f []     = returnUs []
167 mapUs f (x:xs)
168   = f x         `thenUs` \ r  ->
169     mapUs f xs  `thenUs` \ rs ->
170     returnUs (r:rs)
171
172 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
173 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
174
175 mapAndUnzipUs f [] = returnUs ([],[])
176 mapAndUnzipUs f (x:xs)
177   = f x                 `thenUs` \ (r1,  r2)  ->
178     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
179     returnUs (r1:rs1, r2:rs2)
180
181 mapAndUnzip3Us f [] = returnUs ([],[],[])
182 mapAndUnzip3Us f (x:xs)
183   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
184     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
185     returnUs (r1:rs1, r2:rs2, r3:rs3)
186
187 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
188 thenMaybeUs m k
189   = m   `thenUs` \ result ->
190     case result of
191       Nothing -> returnUs Nothing
192       Just x  -> k x
193
194 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
195             -> acc
196             -> [x]
197             -> UniqSM (acc, [y])
198
199 mapAccumLUs f b []     = returnUs (b, [])
200 mapAccumLUs f b (x:xs)
201   = f b x                           `thenUs` \ (b__2, x__2) ->
202     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
203     returnUs (b__3, x__2:xs__2)
204 \end{code}