8dcf595da004124b75fbee654b2b0da9c2aede9a
[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, listSplitUniqSupply
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 listSplitUniqSupply :: UniqSupply -> [UniqSupply]   -- Infinite
60 uniqFromSupply  :: UniqSupply -> Unique
61 uniqsFromSupply :: UniqSupply -> [Unique]       -- Infinite
62 \end{code}
63
64 \begin{code}
65 mkSplitUniqSupply (C# c#)
66   = let
67         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
68         -- here comes THE MAGIC:
69
70         -- This is one of the most hammered bits in the whole compiler
71         mk_supply#
72           = unsafeInterleaveIO (
73                 genSymZh    >>= \ (I# u#) ->
74                 mk_supply#  >>= \ s1 ->
75                 mk_supply#  >>= \ s2 ->
76                 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
77             )
78     in
79     mk_supply#
80
81 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
82
83 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
84 listSplitUniqSupply  (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
85 \end{code}
86
87 \begin{code}
88 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (I# n)
89 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
90 \end{code}
91
92 %************************************************************************
93 %*                                                                      *
94 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
95 %*                                                                      *
96 %************************************************************************
97
98 \begin{code}
99 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
100
101 instance Monad UniqSM where
102   return = returnUs
103   (>>=) = thenUs
104   (>>)  = thenUs_
105
106 -- the initUs function also returns the final UniqSupply; initUs_ drops it
107 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
108 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
109
110 initUs_ :: UniqSupply -> UniqSM a -> a
111 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
112
113 {-# INLINE thenUs #-}
114 {-# INLINE lazyThenUs #-}
115 {-# INLINE returnUs #-}
116 {-# INLINE splitUniqSupply #-}
117 \end{code}
118
119 @thenUs@ is where we split the @UniqSupply@.
120 \begin{code}
121 fixUs :: (a -> UniqSM a) -> UniqSM a
122 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
123
124 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
125 thenUs (USM expr) cont
126   = USM (\us -> case (expr us) of 
127                    (result, us') -> unUSM (cont result) us')
128
129 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
130 lazyThenUs (USM expr) cont
131   = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
132
133 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
134 thenUs_ (USM expr) (USM cont)
135   = USM (\us -> case (expr us) of { (_, us') -> cont us' })
136
137
138 returnUs :: a -> UniqSM a
139 returnUs result = USM (\us -> (result, us))
140
141 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
142 withUs f = USM (\us -> f us)    -- Ha ha!
143                 
144 getUs :: UniqSM UniqSupply
145 getUs = USM (\us -> splitUniqSupply us)
146
147 getUniqueUs :: UniqSM Unique
148 getUniqueUs = USM (\us -> case splitUniqSupply us of
149                            (us1,us2) -> (uniqFromSupply us1, us2))
150
151 getUniquesUs :: UniqSM [Unique]
152 getUniquesUs = USM (\us -> case splitUniqSupply us of
153                               (us1,us2) -> (uniqsFromSupply us1, us2))
154 \end{code}
155
156 \begin{code}
157 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
158 mapUs f []     = returnUs []
159 mapUs f (x:xs)
160   = f x         `thenUs` \ r  ->
161     mapUs f xs  `thenUs` \ rs ->
162     returnUs (r:rs)
163
164 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
165 lazyMapUs f []     = returnUs []
166 lazyMapUs f (x:xs)
167   = f x             `lazyThenUs` \ r  ->
168     lazyMapUs f xs  `lazyThenUs` \ rs ->
169     returnUs (r:rs)
170
171 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
172 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
173
174 mapAndUnzipUs f [] = returnUs ([],[])
175 mapAndUnzipUs f (x:xs)
176   = f x                 `thenUs` \ (r1,  r2)  ->
177     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
178     returnUs (r1:rs1, r2:rs2)
179
180 mapAndUnzip3Us f [] = returnUs ([],[],[])
181 mapAndUnzip3Us f (x:xs)
182   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
183     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
184     returnUs (r1:rs1, r2:rs2, r3:rs3)
185
186 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
187 thenMaybeUs m k
188   = m   `thenUs` \ result ->
189     case result of
190       Nothing -> returnUs Nothing
191       Just x  -> k x
192
193 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
194             -> acc
195             -> [x]
196             -> UniqSM (acc, [y])
197
198 mapAccumLUs f b []     = returnUs (b, [])
199 mapAccumLUs f b (x:xs)
200   = f b x                           `thenUs` \ (b__2, x__2) ->
201     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
202     returnUs (b__3, x__2:xs__2)
203 \end{code}