[project @ 2002-04-01 08:22:38 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 A value of type @UniqSupply@ is unique, and it can
47 supply {\em one} distinct @Unique@.  Also, from the supply, one can
48 also manufacture an arbitrary number of further @UniqueSupplies@,
49 which will be distinct from the first and from all others.
50
51 \begin{code}
52 data UniqSupply
53   = MkSplitUniqSupply Int       -- make the Unique with this
54                    UniqSupply UniqSupply
55                                 -- when split => these two supplies
56 \end{code}
57
58 \begin{code}
59 mkSplitUniqSupply :: Char -> IO UniqSupply
60
61 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
62 uniqFromSupply  :: UniqSupply -> Unique
63 uniqsFromSupply :: UniqSupply -> [Unique]       -- Infinite
64 \end{code}
65
66 \begin{code}
67 mkSplitUniqSupply (C# c#)
68   = let
69 #if __GLASGOW_HASKELL__ >= 503
70         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
71 #else
72         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
73 #endif
74         -- here comes THE MAGIC:
75
76         -- This is one of the most hammered bits in the whole compiler
77         mk_supply#
78           = unsafeInterleaveIO (
79                 mk_unique   >>= \ uniq ->
80                 mk_supply#  >>= \ s1 ->
81                 mk_supply#  >>= \ s2 ->
82                 return (MkSplitUniqSupply uniq s1 s2)
83             )
84
85         mk_unique = _ccall_ genSymZh            >>= \ (W# u#) ->
86                     return (I# (w2i (mask# `or#` u#)))
87     in
88     mk_supply#
89
90 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
91 \end{code}
92
93 \begin{code}
94 uniqFromSupply  (MkSplitUniqSupply (I# n) _ _)  = mkUniqueGrimily n
95 uniqsFromSupply (MkSplitUniqSupply (I# n) _ s2) = mkUniqueGrimily n : uniqsFromSupply s2
96 \end{code}
97
98 %************************************************************************
99 %*                                                                      *
100 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
101 %*                                                                      *
102 %************************************************************************
103
104 \begin{code}
105 type UniqSM result = UniqSupply -> (result, UniqSupply)
106
107 -- the initUs function also returns the final UniqSupply; initUs_ drops it
108 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
109 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
110
111 initUs_ :: UniqSupply -> UniqSM a -> a
112 initUs_ init_us m = case m init_us of { (r,us) -> r }
113
114 {-# INLINE thenUs #-}
115 {-# INLINE lazyThenUs #-}
116 {-# INLINE returnUs #-}
117 {-# INLINE splitUniqSupply #-}
118 \end{code}
119
120 @thenUs@ is where we split the @UniqSupply@.
121 \begin{code}
122 fixUs :: (a -> UniqSM a) -> UniqSM a
123 fixUs m us
124   = (r,us')  where  (r,us') = m r us
125
126 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
127 thenUs expr cont us
128   = case (expr us) of { (result, us') -> cont result us' }
129
130 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
131 lazyThenUs expr cont us
132   = let (result, us') = expr us in cont result us'
133
134 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
135 thenUs_ expr cont us
136   = case (expr us) of { (_, us') -> cont us' }
137
138
139 returnUs :: a -> UniqSM a
140 returnUs result us = (result, us)
141
142 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
143 withUs f us = f us      -- Ha ha!
144                 
145 getUs :: UniqSM UniqSupply
146 getUs us = splitUniqSupply us
147
148 getUniqueUs :: UniqSM Unique
149 getUniqueUs us = case splitUniqSupply us of
150                    (us1,us2) -> (uniqFromSupply us1, us2)
151
152 getUniquesUs :: UniqSM [Unique]
153 getUniquesUs us = case splitUniqSupply us of
154                       (us1,us2) -> (uniqsFromSupply us1, us2)
155 \end{code}
156
157 \begin{code}
158 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
159 mapUs f []     = returnUs []
160 mapUs f (x:xs)
161   = f x         `thenUs` \ r  ->
162     mapUs f xs  `thenUs` \ rs ->
163     returnUs (r:rs)
164
165 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
166 lazyMapUs f []     = returnUs []
167 lazyMapUs f (x:xs)
168   = f x             `lazyThenUs` \ r  ->
169     lazyMapUs f xs  `lazyThenUs` \ rs ->
170     returnUs (r:rs)
171
172 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
173 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
174
175 mapAndUnzipUs f [] = returnUs ([],[])
176 mapAndUnzipUs f (x:xs)
177   = f x                 `thenUs` \ (r1,  r2)  ->
178     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
179     returnUs (r1:rs1, r2:rs2)
180
181 mapAndUnzip3Us f [] = returnUs ([],[],[])
182 mapAndUnzip3Us f (x:xs)
183   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
184     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
185     returnUs (r1:rs1, r2:rs2, r3:rs3)
186
187 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
188 thenMaybeUs m k
189   = m   `thenUs` \ result ->
190     case result of
191       Nothing -> returnUs Nothing
192       Just x  -> k x
193
194 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
195             -> acc
196             -> [x]
197             -> UniqSM (acc, [y])
198
199 mapAccumLUs f b []     = returnUs (b, [])
200 mapAccumLUs f b (x:xs)
201   = f b x                           `thenUs` \ (b__2, x__2) ->
202     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
203     returnUs (b__3, x__2:xs__2)
204 \end{code}