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