[project @ 1997-11-24 20:13:08 by sof]
[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
30 #if __GLASGOW_HASKELL__ == 201
31 import PreludeGlaST
32 # define WHASH      GHCbase.W#
33 #elif __GLASGOW_HASKELL__ >= 202
34 import GlaExts
35 import STBase
36 # if __GLASGOW_HASKELL__ == 202
37 import PrelBase ( Char(..) )
38 # endif
39 # define WHASH      GlaExts.W#
40 #else
41 import PreludeGlaST
42 # define WHASH      W#
43 #endif
44
45 #if __GLASGOW_HASKELL__ >= 209
46 import Unsafe ( unsafeInterleaveIO )
47 #endif
48
49 w2i x = word2Int# x
50 i2w x = int2Word# x
51 i2w_s x = (x :: Int#)
52 \end{code}
53
54
55 %************************************************************************
56 %*                                                                      *
57 \subsection{Splittable Unique supply: @UniqSupply@}
58 %*                                                                      *
59 %************************************************************************
60
61 %************************************************************************
62 %*                                                                      *
63 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
64 %*                                                                      *
65 %************************************************************************
66
67 A value of type @UniqSupply@ is unique, and it can
68 supply {\em one} distinct @Unique@.  Also, from the supply, one can
69 also manufacture an arbitrary number of further @UniqueSupplies@,
70 which will be distinct from the first and from all others.
71
72 \begin{code}
73 data UniqSupply
74   = MkSplitUniqSupply Int       -- make the Unique with this
75                    UniqSupply UniqSupply
76                                 -- when split => these two supplies
77 \end{code}
78
79 \begin{code}
80 mkSplitUniqSupply :: Char -> IO UniqSupply
81
82 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
83 getUnique :: UniqSupply -> Unique
84 getUniques :: Int -> UniqSupply -> [Unique]
85 \end{code}
86
87 \begin{code}
88 mkSplitUniqSupply (C# c#)
89   = let
90         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
91
92         -- here comes THE MAGIC:
93
94         mk_supply#
95           = unsafe_interleave (
96                 mk_unique   `thenPrimIO` \ uniq ->
97                 mk_supply#  `thenPrimIO` \ s1 ->
98                 mk_supply#  `thenPrimIO` \ s2 ->
99                 returnPrimIO (MkSplitUniqSupply uniq s1 s2)
100             )
101           where
102 --
103             -- inlined copy of unsafeInterleavePrimIO;
104             -- this is the single-most-hammered bit of code
105             -- in the compiler....
106             -- Too bad it's not 1.3-portable...
107             unsafe_interleave m =
108 #if __GLASGOW_HASKELL__ >= 209
109                unsafeInterleaveIO m
110 #else
111                MkST ( \ s ->
112                 let
113                     (MkST m') = m
114                     ST_RET(r, new_s) = m' s
115                 in
116                 ST_RET(r, s))
117 #endif
118
119         mk_unique = _ccall_ genSymZh            `thenPrimIO` \ (WHASH u#) ->
120                     returnPrimIO (I# (w2i (mask# `or#` u#)))
121     in
122 #if __GLASGOW_HASKELL__ >= 200
123     primIOToIO mk_supply#       >>= \ s ->
124     return s
125 #else
126     mk_supply#  `thenPrimIO` \ s ->
127     return s
128 #endif
129
130 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
131 \end{code}
132
133 \begin{code}
134 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
135
136 getUniques (I# i) supply = i `get_from` supply
137   where
138     get_from 0# _ = []
139     get_from n (MkSplitUniqSupply (I# u) _ s2)
140       = mkUniqueGrimily u : get_from (n -# 1#) s2
141 \end{code}
142
143 %************************************************************************
144 %*                                                                      *
145 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
146 %*                                                                      *
147 %************************************************************************
148
149 \begin{code}
150 type UniqSM result = UniqSupply -> result
151
152 -- the initUs function also returns the final UniqSupply
153
154 initUs :: UniqSupply -> UniqSM a -> a
155
156 initUs init_us m = m init_us
157
158 {-# INLINE thenUs #-}
159 {-# INLINE returnUs #-}
160 {-# INLINE splitUniqSupply #-}
161 \end{code}
162
163 @thenUs@ is where we split the @UniqSupply@.
164 \begin{code}
165 fixUs :: (a -> UniqSM a) -> UniqSM a
166 fixUs m us
167   = r  where  r = m r us
168
169 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
170
171 thenUs expr cont us
172   = case (splitUniqSupply us) of { (s1, s2) ->
173     case (expr s1)            of { result ->
174     cont result s2 }}
175 \end{code}
176
177 \begin{code}
178 returnUs :: a -> UniqSM a
179 returnUs result us = result
180
181 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
182
183 mapUs f []     = returnUs []
184 mapUs f (x:xs)
185   = f x         `thenUs` \ r  ->
186     mapUs f xs  `thenUs` \ rs ->
187     returnUs (r:rs)
188
189 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
190 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
191
192 mapAndUnzipUs f [] = returnUs ([],[])
193 mapAndUnzipUs f (x:xs)
194   = f x                 `thenUs` \ (r1,  r2)  ->
195     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
196     returnUs (r1:rs1, r2:rs2)
197
198 mapAndUnzip3Us f [] = returnUs ([],[],[])
199 mapAndUnzip3Us f (x:xs)
200   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
201     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
202     returnUs (r1:rs1, r2:rs2, r3:rs3)
203
204 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
205 thenMaybeUs m k
206   = m   `thenUs` \ result ->
207     case result of
208       Nothing -> returnUs Nothing
209       Just x  -> k x
210
211 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
212             -> acc
213             -> [x]
214             -> UniqSM (acc, [y])
215
216 mapAccumLUs f b []     = returnUs (b, [])
217 mapAccumLUs f b (x:xs)
218   = f b x                           `thenUs` \ (b__2, x__2) ->
219     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
220     returnUs (b__3, x__2:xs__2)
221 \end{code}