88ac980af2730a47818f2247e5b4449654f92579
[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,
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#
107 #else
108     mk_supply#  `thenPrimIO` \ s ->
109     return s
110 #endif
111
112 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
113 \end{code}
114
115 \begin{code}
116 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
117
118 getUniques (I# i) supply = i `get_from` supply
119   where
120     get_from 0# _ = []
121     get_from n (MkSplitUniqSupply (I# u) _ s2)
122       = mkUniqueGrimily u : get_from (n `minusInt#` 1#) s2
123 \end{code}
124
125 %************************************************************************
126 %*                                                                      *
127 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
128 %*                                                                      *
129 %************************************************************************
130
131 \begin{code}
132 type UniqSM result = UniqSupply -> result
133
134 -- the initUs function also returns the final UniqSupply
135
136 initUs :: UniqSupply -> UniqSM a -> (UniqSupply, a)
137
138 initUs init_us m
139   = case (splitUniqSupply init_us) of { (s1, s2) ->
140     (s2, m s1) }
141
142 {-# INLINE thenUs #-}
143 {-# INLINE returnUs #-}
144 {-# INLINE splitUniqSupply #-}
145 \end{code}
146
147 @thenUs@ is where we split the @UniqSupply@.
148 \begin{code}
149 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
150
151 thenUs expr cont us
152   = case (splitUniqSupply us) of { (s1, s2) ->
153     case (expr s1)            of { result ->
154     cont result s2 }}
155 \end{code}
156
157 \begin{code}
158 returnUs :: a -> UniqSM a
159 returnUs result us = result
160
161 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
162
163 mapUs f []     = returnUs []
164 mapUs f (x:xs)
165   = f x         `thenUs` \ r  ->
166     mapUs f xs  `thenUs` \ rs ->
167     returnUs (r:rs)
168
169 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
170 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
171
172 mapAndUnzipUs f [] = returnUs ([],[])
173 mapAndUnzipUs f (x:xs)
174   = f x                 `thenUs` \ (r1,  r2)  ->
175     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
176     returnUs (r1:rs1, r2:rs2)
177
178 mapAndUnzip3Us f [] = returnUs ([],[],[])
179 mapAndUnzip3Us f (x:xs)
180   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
181     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
182     returnUs (r1:rs1, r2:rs2, r3:rs3)
183
184 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
185 thenMaybeUs m k
186   = m   `thenUs` \ result ->
187     case result of
188       Nothing -> returnUs Nothing
189       Just x  -> k x
190
191 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
192             -> acc
193             -> [x]
194             -> UniqSM (acc, [y])
195
196 mapAccumLUs f b []     = returnUs (b, [])
197 mapAccumLUs f b (x:xs)
198   = f b x                           `thenUs` \ (b__2, x__2) ->
199     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
200     returnUs (b__3, x__2:xs__2)
201 \end{code}