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