[project @ 2000-12-07 09:28:42 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
5
6 \begin{code}
7 module UniqSupply (
8
9         UniqSupply,             -- Abstractly
10
11         uniqFromSupply, uniqsFromSupply,        -- basic ops
12
13         UniqSM,         -- type: unique supply monad
14         initUs, initUs_, thenUs, thenUs_, returnUs, fixUs, getUs, withUs,
15         getUniqueUs, getUniquesUs,
16         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
17         thenMaybeUs, mapAccumLUs,
18         lazyThenUs, lazyMapUs,
19
20         mkSplitUniqSupply,
21         splitUniqSupply
22   ) where
23
24 #include "HsVersions.h"
25
26 import Unique
27 import GlaExts
28
29 #if __GLASGOW_HASKELL__ < 301
30 import IOBase           ( IO(..), IOResult(..) )
31 #else
32 #endif
33
34 w2i x = word2Int# x
35 i2w x = int2Word# x
36 i2w_s x = (x :: Int#)
37 \end{code}
38
39
40 %************************************************************************
41 %*                                                                      *
42 \subsection{Splittable Unique supply: @UniqSupply@}
43 %*                                                                      *
44 %************************************************************************
45
46 %************************************************************************
47 %*                                                                      *
48 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
49 %*                                                                      *
50 %************************************************************************
51
52 A value of type @UniqSupply@ is unique, and it can
53 supply {\em one} distinct @Unique@.  Also, from the supply, one can
54 also manufacture an arbitrary number of further @UniqueSupplies@,
55 which will be distinct from the first and from all others.
56
57 \begin{code}
58 data UniqSupply
59   = MkSplitUniqSupply Int       -- make the Unique with this
60                    UniqSupply UniqSupply
61                                 -- when split => these two supplies
62 \end{code}
63
64 \begin{code}
65 mkSplitUniqSupply :: Char -> IO UniqSupply
66
67 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
68 uniqFromSupply  :: UniqSupply -> Unique
69 uniqsFromSupply :: Int -> UniqSupply -> [Unique]
70 \end{code}
71
72 \begin{code}
73 mkSplitUniqSupply (C# c#)
74   = let
75         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
76
77         -- here comes THE MAGIC:
78
79         -- This is one of the most hammered bits in the whole compiler
80         mk_supply#
81           = unsafeInterleaveIO (
82                 mk_unique   >>= \ uniq ->
83                 mk_supply#  >>= \ s1 ->
84                 mk_supply#  >>= \ s2 ->
85                 return (MkSplitUniqSupply uniq s1 s2)
86             )
87
88         mk_unique = _ccall_ genSymZh            >>= \ (W# u#) ->
89                     return (I# (w2i (mask# `or#` u#)))
90     in
91     mk_supply#
92
93 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
94 \end{code}
95
96 \begin{code}
97 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
98
99 uniqsFromSupply (I# i) supply = i `get_from` supply
100   where
101     get_from 0# _ = []
102     get_from n (MkSplitUniqSupply (I# u) _ s2)
103       = mkUniqueGrimily u : get_from (n -# 1#) s2
104 \end{code}
105
106 %************************************************************************
107 %*                                                                      *
108 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
109 %*                                                                      *
110 %************************************************************************
111
112 \begin{code}
113 type UniqSM result = UniqSupply -> (result, UniqSupply)
114
115 -- the initUs function also returns the final UniqSupply; initUs_ drops it
116 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
117 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
118
119 initUs_ :: UniqSupply -> UniqSM a -> a
120 initUs_ init_us m = case m init_us of { (r,us) -> r }
121
122 {-# INLINE thenUs #-}
123 {-# INLINE lazyThenUs #-}
124 {-# INLINE returnUs #-}
125 {-# INLINE splitUniqSupply #-}
126 \end{code}
127
128 @thenUs@ is where we split the @UniqSupply@.
129 \begin{code}
130 fixUs :: (a -> UniqSM a) -> UniqSM a
131 fixUs m us
132   = (r,us')  where  (r,us') = m r us
133
134 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
135 thenUs expr cont us
136   = case (expr us) of { (result, us') -> cont result us' }
137
138 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
139 lazyThenUs expr cont us
140   = let (result, us') = expr us in cont result us'
141
142 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
143 thenUs_ expr cont us
144   = case (expr us) of { (_, us') -> cont us' }
145
146
147 returnUs :: a -> UniqSM a
148 returnUs result us = (result, us)
149
150 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
151 withUs f us = f us      -- Ha ha!
152                 
153 getUs :: UniqSM UniqSupply
154 getUs us = splitUniqSupply us
155
156 getUniqueUs :: UniqSM Unique
157 getUniqueUs us = case splitUniqSupply us of
158                    (us1,us2) -> (uniqFromSupply us1, us2)
159
160 getUniquesUs :: Int -> UniqSM [Unique]
161 getUniquesUs n us = case splitUniqSupply us of
162                       (us1,us2) -> (uniqsFromSupply n us1, us2)
163 \end{code}
164
165 \begin{code}
166 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
167 mapUs f []     = returnUs []
168 mapUs f (x:xs)
169   = f x         `thenUs` \ r  ->
170     mapUs f xs  `thenUs` \ rs ->
171     returnUs (r:rs)
172
173 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
174 lazyMapUs f []     = returnUs []
175 lazyMapUs f (x:xs)
176   = f x             `lazyThenUs` \ r  ->
177     lazyMapUs f xs  `lazyThenUs` \ rs ->
178     returnUs (r:rs)
179
180 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
181 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
182
183 mapAndUnzipUs f [] = returnUs ([],[])
184 mapAndUnzipUs f (x:xs)
185   = f x                 `thenUs` \ (r1,  r2)  ->
186     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
187     returnUs (r1:rs1, r2:rs2)
188
189 mapAndUnzip3Us f [] = returnUs ([],[],[])
190 mapAndUnzip3Us f (x:xs)
191   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
192     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
193     returnUs (r1:rs1, r2:rs2, r3:rs3)
194
195 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
196 thenMaybeUs m k
197   = m   `thenUs` \ result ->
198     case result of
199       Nothing -> returnUs Nothing
200       Just x  -> k x
201
202 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
203             -> acc
204             -> [x]
205             -> UniqSM (acc, [y])
206
207 mapAccumLUs f b []     = returnUs (b, [])
208 mapAccumLUs f b (x:xs)
209   = f b x                           `thenUs` \ (b__2, x__2) ->
210     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
211     returnUs (b__3, x__2:xs__2)
212 \end{code}