4b83b52a610b005ae6a2330af0b9a327e07326a1
[ghc-hetmet.git] / ghc / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
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         getUnique, getUniques,  -- basic ops
12
13         UniqSM,         -- type: unique supply monad
14         initUs, thenUs, returnUs, fixUs,
15         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
16         thenMaybeUs, mapAccumLUs,
17
18         mkSplitUniqSupply,
19         splitUniqSupply
20   ) where
21
22 #include "HsVersions.h"
23
24 import Unique
25 import Util
26
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 getUnique :: UniqSupply -> Unique
69 getUniques :: 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 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
98
99 getUniques (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
114
115 -- the initUs function also returns the final UniqSupply
116
117 initUs :: UniqSupply -> UniqSM a -> a
118
119 initUs init_us m = m init_us
120
121 {-# INLINE thenUs #-}
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  where  r = m r us
131
132 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
133
134 thenUs expr cont us
135   = case (splitUniqSupply us) of { (s1, s2) ->
136     case (expr s1)            of { result ->
137     cont result s2 }}
138 \end{code}
139
140 \begin{code}
141 returnUs :: a -> UniqSM a
142 returnUs result us = result
143
144 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
145
146 mapUs f []     = returnUs []
147 mapUs f (x:xs)
148   = f x         `thenUs` \ r  ->
149     mapUs f xs  `thenUs` \ rs ->
150     returnUs (r:rs)
151
152 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
153 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
154
155 mapAndUnzipUs f [] = returnUs ([],[])
156 mapAndUnzipUs f (x:xs)
157   = f x                 `thenUs` \ (r1,  r2)  ->
158     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
159     returnUs (r1:rs1, r2:rs2)
160
161 mapAndUnzip3Us f [] = returnUs ([],[],[])
162 mapAndUnzip3Us f (x:xs)
163   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
164     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
165     returnUs (r1:rs1, r2:rs2, r3:rs3)
166
167 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
168 thenMaybeUs m k
169   = m   `thenUs` \ result ->
170     case result of
171       Nothing -> returnUs Nothing
172       Just x  -> k x
173
174 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
175             -> acc
176             -> [x]
177             -> UniqSM (acc, [y])
178
179 mapAccumLUs f b []     = returnUs (b, [])
180 mapAccumLUs f b (x:xs)
181   = f b x                           `thenUs` \ (b__2, x__2) ->
182     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
183     returnUs (b__3, x__2:xs__2)
184 \end{code}