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