[project @ 1999-08-20 13:12: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
19         mkSplitUniqSupply,
20         splitUniqSupply
21   ) where
22
23 #include "HsVersions.h"
24
25 import Unique
26 import Panic    ( panic )
27
28 import GlaExts
29
30 #if __GLASGOW_HASKELL__ < 301
31 import IOBase           ( IO(..), IOResult(..) )
32 #else
33 #endif
34
35 w2i x = word2Int# x
36 i2w x = int2Word# x
37 i2w_s x = (x :: Int#)
38 \end{code}
39
40
41 %************************************************************************
42 %*                                                                      *
43 \subsection{Splittable Unique supply: @UniqSupply@}
44 %*                                                                      *
45 %************************************************************************
46
47 %************************************************************************
48 %*                                                                      *
49 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
50 %*                                                                      *
51 %************************************************************************
52
53 A value of type @UniqSupply@ is unique, and it can
54 supply {\em one} distinct @Unique@.  Also, from the supply, one can
55 also manufacture an arbitrary number of further @UniqueSupplies@,
56 which will be distinct from the first and from all others.
57
58 \begin{code}
59 data UniqSupply
60   = MkSplitUniqSupply Int       -- make the Unique with this
61                    UniqSupply UniqSupply
62                                 -- when split => these two supplies
63 \end{code}
64
65 \begin{code}
66 mkSplitUniqSupply :: Char -> IO UniqSupply
67
68 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
69 uniqFromSupply  :: UniqSupply -> Unique
70 uniqsFromSupply :: Int -> UniqSupply -> [Unique]
71 \end{code}
72
73 \begin{code}
74 mkSplitUniqSupply (C# c#)
75   = let
76         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
77
78         -- here comes THE MAGIC:
79
80         -- This is one of the most hammered bits in the whole compiler
81         mk_supply#
82           = unsafeInterleaveIO (
83                 mk_unique   >>= \ uniq ->
84                 mk_supply#  >>= \ s1 ->
85                 mk_supply#  >>= \ s2 ->
86                 return (MkSplitUniqSupply uniq s1 s2)
87             )
88
89         mk_unique = _ccall_ genSymZh            >>= \ (W# u#) ->
90                     return (I# (w2i (mask# `or#` u#)))
91     in
92     mk_supply#
93
94 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
95 \end{code}
96
97 \begin{code}
98 uniqFromSupply (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
99
100 uniqsFromSupply (I# i) supply = i `get_from` supply
101   where
102     get_from 0# _ = []
103     get_from n (MkSplitUniqSupply (I# u) _ s2)
104       = mkUniqueGrimily u : get_from (n -# 1#) s2
105 \end{code}
106
107 %************************************************************************
108 %*                                                                      *
109 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
110 %*                                                                      *
111 %************************************************************************
112
113 \begin{code}
114 type UniqSM result = UniqSupply -> (result, UniqSupply)
115
116 -- the initUs function also returns the final UniqSupply; initUs_ drops it
117 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
118 initUs init_us m = case m init_us of { (r,us) -> (r,us) }
119
120 initUs_ :: UniqSupply -> UniqSM a -> a
121 initUs_ init_us m = case m init_us of { (r,us) -> r }
122
123 {-# INLINE thenUs #-}
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 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
139 thenUs_ expr cont us
140   = case (expr us) of { (_, us') -> cont us' }
141
142 returnUs :: a -> UniqSM a
143 returnUs result us = (result, us)
144
145 getUs :: UniqSM UniqSupply
146 getUs us = (us, panic "getUs: bad supply")
147
148 setUs :: UniqSupply -> UniqSM ()
149 setUs us old_us = ((), us)
150
151 getUniqueUs :: UniqSM Unique
152 getUniqueUs us = case splitUniqSupply us of
153                    (us1,us2) -> (uniqFromSupply us1, us2)
154
155 getUniquesUs :: Int -> UniqSM [Unique]
156 getUniquesUs n us = case splitUniqSupply us of
157                       (us1,us2) -> (uniqsFromSupply n us1, us2)
158 \end{code}
159
160 \begin{code}
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}