Use OPTIONS rather than OPTIONS_GHC for pragmas
[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 {-# OPTIONS -w #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 --     http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
12 -- for details
13
14 module UniqSupply (
15
16         UniqSupply,             -- Abstractly
17
18         uniqFromSupply, uniqsFromSupply,        -- basic ops
19
20         UniqSM,         -- type: unique supply monad
21         initUs, initUs_, thenUs, thenUs_, returnUs, fixUs, getUs, withUs,
22         getUniqueUs, getUniquesUs,
23         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
24         thenMaybeUs, mapAccumLUs,
25         lazyThenUs, lazyMapUs,
26
27         mkSplitUniqSupply,
28         splitUniqSupply, listSplitUniqSupply
29   ) where
30
31 #include "HsVersions.h"
32
33 import Unique
34
35 import GHC.Exts
36 import System.IO.Unsafe ( unsafeInterleaveIO )
37
38 #if __GLASGOW_HASKELL__ >= 607
39 import GHC.IOBase (unsafeDupableInterleaveIO)
40 #else
41 unsafeDupableInterleaveIO :: IO a -> IO a
42 unsafeDupableInterleaveIO = unsafeInterleaveIO
43 #endif
44
45 w2i x = word2Int# x
46 i2w x = int2Word# x
47 i2w_s x = (x :: Int#)
48 \end{code}
49
50
51 %************************************************************************
52 %*                                                                      *
53 \subsection{Splittable Unique supply: @UniqSupply@}
54 %*                                                                      *
55 %************************************************************************
56
57 A value of type @UniqSupply@ is unique, and it can
58 supply {\em one} distinct @Unique@.  Also, from the supply, one can
59 also manufacture an arbitrary number of further @UniqueSupplies@,
60 which will be distinct from the first and from all others.
61
62 \begin{code}
63 data UniqSupply
64   = MkSplitUniqSupply Int#      -- make the Unique with this
65                    UniqSupply UniqSupply
66                                 -- when split => these two supplies
67 \end{code}
68
69 \begin{code}
70 mkSplitUniqSupply :: Char -> IO UniqSupply
71
72 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
73 listSplitUniqSupply :: UniqSupply -> [UniqSupply]   -- Infinite
74 uniqFromSupply  :: UniqSupply -> Unique
75 uniqsFromSupply :: UniqSupply -> [Unique]       -- Infinite
76 \end{code}
77
78 \begin{code}
79 mkSplitUniqSupply (C# c#)
80   = let
81         mask# = (i2w (ord# c#)) `uncheckedShiftL#` (i2w_s 24#)
82         -- here comes THE MAGIC:
83
84         -- This is one of the most hammered bits in the whole compiler
85         mk_supply#
86           = unsafeDupableInterleaveIO (
87                 genSymZh    >>= \ (I# u#) ->
88                 mk_supply#  >>= \ s1 ->
89                 mk_supply#  >>= \ s2 ->
90                 return (MkSplitUniqSupply (w2i (mask# `or#` (i2w u#))) s1 s2)
91             )
92     in
93     mk_supply#
94
95 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
96
97 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
98 listSplitUniqSupply  (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
99 \end{code}
100
101 \begin{code}
102 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (I# n)
103 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (I# n) : uniqsFromSupply s2
104 \end{code}
105
106 %************************************************************************
107 %*                                                                      *
108 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
109 %*                                                                      *
110 %************************************************************************
111
112 \begin{code}
113 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
114
115 instance Monad UniqSM where
116   return = returnUs
117   (>>=) = thenUs
118   (>>)  = thenUs_
119
120 -- the initUs function also returns the final UniqSupply; initUs_ drops it
121 initUs :: UniqSupply -> UniqSM a -> (a,UniqSupply)
122 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
123
124 initUs_ :: UniqSupply -> UniqSM a -> a
125 initUs_ init_us m = case unUSM m init_us of { (r,us) -> r }
126
127 {-# INLINE thenUs #-}
128 {-# INLINE lazyThenUs #-}
129 {-# INLINE returnUs #-}
130 {-# INLINE splitUniqSupply #-}
131 \end{code}
132
133 @thenUs@ is where we split the @UniqSupply@.
134 \begin{code}
135 fixUs :: (a -> UniqSM a) -> UniqSM a
136 fixUs m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
137
138 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
139 thenUs (USM expr) cont
140   = USM (\us -> case (expr us) of 
141                    (result, us') -> unUSM (cont result) us')
142
143 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
144 lazyThenUs (USM expr) cont
145   = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
146
147 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
148 thenUs_ (USM expr) (USM cont)
149   = USM (\us -> case (expr us) of { (_, us') -> cont us' })
150
151
152 returnUs :: a -> UniqSM a
153 returnUs result = USM (\us -> (result, us))
154
155 withUs :: (UniqSupply -> (a, UniqSupply)) -> UniqSM a
156 withUs f = USM (\us -> f us)    -- Ha ha!
157                 
158 getUs :: UniqSM UniqSupply
159 getUs = USM (\us -> splitUniqSupply us)
160
161 getUniqueUs :: UniqSM Unique
162 getUniqueUs = USM (\us -> case splitUniqSupply us of
163                            (us1,us2) -> (uniqFromSupply us1, us2))
164
165 getUniquesUs :: UniqSM [Unique]
166 getUniquesUs = USM (\us -> case splitUniqSupply us of
167                               (us1,us2) -> (uniqsFromSupply us1, us2))
168 \end{code}
169
170 \begin{code}
171 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
172 mapUs f []     = returnUs []
173 mapUs f (x:xs)
174   = f x         `thenUs` \ r  ->
175     mapUs f xs  `thenUs` \ rs ->
176     returnUs (r:rs)
177
178 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
179 lazyMapUs f []     = returnUs []
180 lazyMapUs f (x:xs)
181   = f x             `lazyThenUs` \ r  ->
182     lazyMapUs f xs  `lazyThenUs` \ rs ->
183     returnUs (r:rs)
184
185 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
186 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
187
188 mapAndUnzipUs f [] = returnUs ([],[])
189 mapAndUnzipUs f (x:xs)
190   = f x                 `thenUs` \ (r1,  r2)  ->
191     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
192     returnUs (r1:rs1, r2:rs2)
193
194 mapAndUnzip3Us f [] = returnUs ([],[],[])
195 mapAndUnzip3Us f (x:xs)
196   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
197     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
198     returnUs (r1:rs1, r2:rs2, r3:rs3)
199
200 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
201 thenMaybeUs m k
202   = m   `thenUs` \ result ->
203     case result of
204       Nothing -> returnUs Nothing
205       Just x  -> k x
206
207 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
208             -> acc
209             -> [x]
210             -> UniqSM (acc, [y])
211
212 mapAccumLUs f b []     = returnUs (b, [])
213 mapAccumLUs f b (x:xs)
214   = f b x                           `thenUs` \ (b__2, x__2) ->
215     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
216     returnUs (b__3, x__2:xs__2)
217 \end{code}