Remove code that is dead, as we require __GLASGOW_HASKELL__ >= 504
[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         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
67         -- here comes THE MAGIC:
68
69         -- This is one of the most hammered bits in the whole compiler
70         mk_supply#
71           = unsafeInterleaveIO (
72                 genSymZh    >>= \ (I# u#) ->
73                 mk_supply#  >>= \ s1 ->
74                 mk_supply#  >>= \ s2 ->
75                 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
76             )
77     in
78     mk_supply#
79
80 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
81
82 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
83 \end{code}
84
85 \begin{code}
86 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (I# n)
87 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
88 \end{code}
89
90 %************************************************************************
91 %*                                                                      *
92 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
93 %*                                                                      *
94 %************************************************************************
95
96 \begin{code}
97 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
98
99 instance Monad UniqSM where
100   return = returnUs
101   (>>=) = thenUs
102   (>>)  = thenUs_
103
104 -- the initUs function also returns the final UniqSupply; initUs_ drops it
105 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
106 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
107
108 initUs_ :: UniqSupply -> UniqSM a -> a
109 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
110
111 {-# INLINE thenUs #-}
112 {-# INLINE lazyThenUs #-}
113 {-# INLINE returnUs #-}
114 {-# INLINE splitUniqSupply #-}
115 \end{code}
116
117 @thenUs@ is where we split the @UniqSupply@.
118 \begin{code}
119 fixUs :: (a -> UniqSM a) -> UniqSM a
120 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
121
122 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
123 thenUs (USM expr) cont
124   = USM (\us -> case (expr us) of 
125                    (result, us') -> unUSM (cont result) us')
126
127 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
128 lazyThenUs (USM expr) cont
129   = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
130
131 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
132 thenUs_ (USM expr) (USM cont)
133   = USM (\us -> case (expr us) of { (_, us') -> cont us' })
134
135
136 returnUs :: a -> UniqSM a
137 returnUs result = USM (\us -> (result, us))
138
139 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
140 withUs f = USM (\us -> f us)    -- Ha ha!
141                 
142 getUs :: UniqSM UniqSupply
143 getUs = USM (\us -> splitUniqSupply us)
144
145 getUniqueUs :: UniqSM Unique
146 getUniqueUs = USM (\us -> case splitUniqSupply us of
147                            (us1,us2) -> (uniqFromSupply us1, us2))
148
149 getUniquesUs :: UniqSM [Unique]
150 getUniquesUs = USM (\us -> case splitUniqSupply us of
151                               (us1,us2) -> (uniqsFromSupply us1, us2))
152 \end{code}
153
154 \begin{code}
155 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
156 mapUs f []     = returnUs []
157 mapUs f (x:xs)
158   = f x         `thenUs` \ r  ->
159     mapUs f xs  `thenUs` \ rs ->
160     returnUs (r:rs)
161
162 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
163 lazyMapUs f []     = returnUs []
164 lazyMapUs f (x:xs)
165   = f x             `lazyThenUs` \ r  ->
166     lazyMapUs f xs  `lazyThenUs` \ 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}