[project @ 1996-12-19 09:10:02 by simonpj]
[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 -> (UniqSupply, a)
138
139 initUs init_us m
140   = case (splitUniqSupply init_us) of { (s1, s2) ->
141     (s2, m s1) }
142
143 {-# INLINE thenUs #-}
144 {-# INLINE returnUs #-}
145 {-# INLINE splitUniqSupply #-}
146 \end{code}
147
148 @thenUs@ is where we split the @UniqSupply@.
149 \begin{code}
150 fixUs :: (a -> UniqSM a) -> UniqSM a
151 fixUs m us
152   = r  where  r = m r us
153
154 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
155
156 thenUs expr cont us
157   = case (splitUniqSupply us) of { (s1, s2) ->
158     case (expr s1)            of { result ->
159     cont result s2 }}
160 \end{code}
161
162 \begin{code}
163 returnUs :: a -> UniqSM a
164 returnUs result us = result
165
166 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
167
168 mapUs f []     = returnUs []
169 mapUs f (x:xs)
170   = f x         `thenUs` \ r  ->
171     mapUs f xs  `thenUs` \ rs ->
172     returnUs (r:rs)
173
174 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
175 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
176
177 mapAndUnzipUs f [] = returnUs ([],[])
178 mapAndUnzipUs f (x:xs)
179   = f x                 `thenUs` \ (r1,  r2)  ->
180     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
181     returnUs (r1:rs1, r2:rs2)
182
183 mapAndUnzip3Us f [] = returnUs ([],[],[])
184 mapAndUnzip3Us f (x:xs)
185   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
186     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
187     returnUs (r1:rs1, r2:rs2, r3:rs3)
188
189 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
190 thenMaybeUs m k
191   = m   `thenUs` \ result ->
192     case result of
193       Nothing -> returnUs Nothing
194       Just x  -> k x
195
196 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
197             -> acc
198             -> [x]
199             -> UniqSM (acc, [y])
200
201 mapAccumLUs f b []     = returnUs (b, [])
202 mapAccumLUs f b (x:xs)
203   = f b x                           `thenUs` \ (b__2, x__2) ->
204     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
205     returnUs (b__3, x__2:xs__2)
206 \end{code}