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