[project @ 2002-03-18 09:44:46 by simonmar]
[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 :: UniqSupply -> [Unique]       -- Infinite
70 \end{code}
71
72 \begin{code}
73 mkSplitUniqSupply (C# c#)
74   = let
75 #if __GLASGOW_HASKELL__ >= 503
76         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
77 #else
78         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
79 #endif
80         -- here comes THE MAGIC:
81
82         -- This is one of the most hammered bits in the whole compiler
83         mk_supply#
84           = unsafeInterleaveIO (
85                 mk_unique   >>= \ uniq ->
86                 mk_supply#  >>= \ s1 ->
87                 mk_supply#  >>= \ s2 ->
88                 return (MkSplitUniqSupply uniq s1 s2)
89             )
90
91         mk_unique = _ccall_ genSymZh            >>= \ (W# u#) ->
92                     return (I# (w2i (mask# `or#` u#)))
93     in
94     mk_supply#
95
96 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
97 \end{code}
98
99 \begin{code}
100 uniqFromSupply  (MkSplitUniqSupply (I# n) _ _)  = mkUniqueGrimily n
101 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
102 \end{code}
103
104 %************************************************************************
105 %*                                                                      *
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
107 %*                                                                      *
108 %************************************************************************
109
110 \begin{code}
111 type UniqSM result = UniqSupply -> (result, UniqSupply)
112
113 -- the initUs function also returns the final UniqSupply; initUs_ drops it
114 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
115 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
116
117 initUs_ :: UniqSupply -> UniqSM a -> a
118 initUs_ init_us m = case m init_us of { (r,us) -> r }
119
120 {-# INLINE thenUs #-}
121 {-# INLINE lazyThenUs #-}
122 {-# INLINE returnUs #-}
123 {-# INLINE splitUniqSupply #-}
124 \end{code}
125
126 @thenUs@ is where we split the @UniqSupply@.
127 \begin{code}
128 fixUs :: (a -> UniqSM a) -> UniqSM a
129 fixUs m us
130   = (r,us')  where  (r,us') = m r us
131
132 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
133 thenUs expr cont us
134   = case (expr us) of { (result, us') -> cont result us' }
135
136 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
137 lazyThenUs expr cont us
138   = let (result, us') = expr us in cont result us'
139
140 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
141 thenUs_ expr cont us
142   = case (expr us) of { (_, us') -> cont us' }
143
144
145 returnUs :: a -> UniqSM a
146 returnUs result us = (result, us)
147
148 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
149 withUs f us = f us      -- Ha ha!
150                 
151 getUs :: UniqSM UniqSupply
152 getUs us = splitUniqSupply us
153
154 getUniqueUs :: UniqSM Unique
155 getUniqueUs us = case splitUniqSupply us of
156                    (us1,us2) -> (uniqFromSupply us1, us2)
157
158 getUniquesUs :: UniqSM [Unique]
159 getUniquesUs us = case splitUniqSupply us of
160                       (us1,us2) -> (uniqsFromSupply us1, us2)
161 \end{code}
162
163 \begin{code}
164 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165 mapUs f []     = returnUs []
166 mapUs f (x:xs)
167   = f x         `thenUs` \ r  ->
168     mapUs f xs  `thenUs` \ rs ->
169     returnUs (r:rs)
170
171 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 lazyMapUs f []     = returnUs []
173 lazyMapUs f (x:xs)
174   = f x             `lazyThenUs` \ r  ->
175     lazyMapUs f xs  `lazyThenUs` \ rs ->
176     returnUs (r:rs)
177
178 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
179 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
180
181 mapAndUnzipUs f [] = returnUs ([],[])
182 mapAndUnzipUs f (x:xs)
183   = f x                 `thenUs` \ (r1,  r2)  ->
184     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
185     returnUs (r1:rs1, r2:rs2)
186
187 mapAndUnzip3Us f [] = returnUs ([],[],[])
188 mapAndUnzip3Us f (x:xs)
189   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
190     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
191     returnUs (r1:rs1, r2:rs2, r3:rs3)
192
193 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
194 thenMaybeUs m k
195   = m   `thenUs` \ result ->
196     case result of
197       Nothing -> returnUs Nothing
198       Just x  -> k x
199
200 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
201             -> acc
202             -> [x]
203             -> UniqSM (acc, [y])
204
205 mapAccumLUs f b []     = returnUs (b, [])
206 mapAccumLUs f b (x:xs)
207   = f b x                           `thenUs` \ (b__2, x__2) ->
208     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
209     returnUs (b__3, x__2:xs__2)
210 \end{code}