Remove legacy code that isn't used now that we require GHC >= 6.8
[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         -- * Main data type
9         UniqSupply, -- Abstractly
10
11         -- ** Operations on supplies 
12         uniqFromSupply, uniqsFromSupply, -- basic ops
13         
14         mkSplitUniqSupply,
15         splitUniqSupply, listSplitUniqSupply,
16
17         -- * Unique supply monad and its abstraction
18         UniqSM, MonadUnique(..),
19         
20         -- ** Operations on the monad
21         initUs, initUs_,
22         lazyThenUs, lazyMapUs,
23
24         -- ** Deprecated operations on 'UniqSM'
25         getUniqueUs, getUs, returnUs, thenUs, mapUs
26   ) where
27
28 import Unique
29 import FastTypes
30
31 import MonadUtils
32 import Control.Monad
33 import Control.Monad.Fix
34 import GHC.IOBase (unsafeDupableInterleaveIO)
35
36 \end{code}
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection{Splittable Unique supply: @UniqSupply@}
41 %*                                                                      *
42 %************************************************************************
43
44 \begin{code}
45 -- | A value of type 'UniqSupply' is unique, and it can
46 -- supply /one/ distinct 'Unique'.  Also, from the supply, one can
47 -- also manufacture an arbitrary number of further 'UniqueSupply' values,
48 -- which will be distinct from the first and from all others.
49 data UniqSupply
50   = MkSplitUniqSupply FastInt   -- 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 -- ^ Create a unique supply out of thin air. The character given must
58 -- be distinct from those of all calls to this function in the compiler
59 -- for the values generated to be truly unique.
60
61 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
62 -- ^ Build two 'UniqSupply' from a single one, each of which
63 -- can supply its own 'Unique'.
64 listSplitUniqSupply :: UniqSupply -> [UniqSupply]
65 -- ^ Create an infinite list of 'UniqSupply' from a single one
66 uniqFromSupply  :: UniqSupply -> Unique
67 -- ^ Obtain the 'Unique' from this particular 'UniqSupply'
68 uniqsFromSupply :: UniqSupply -> [Unique] -- Infinite
69 -- ^ Obtain an infinite list of 'Unique' that can be generated by constant splitting of the supply
70 \end{code}
71
72 \begin{code}
73 mkSplitUniqSupply c
74   = case fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24) of
75      mask -> let
76         -- here comes THE MAGIC:
77
78         -- This is one of the most hammered bits in the whole compiler
79         mk_supply
80           = unsafeDupableInterleaveIO (
81                 genSymZh    >>= \ u_ -> case iUnbox u_ of { u -> (
82                 mk_supply   >>= \ s1 ->
83                 mk_supply   >>= \ s2 ->
84                 return (MkSplitUniqSupply (mask `bitOrFastInt` u) s1 s2)
85             )})
86        in
87        mk_supply
88
89 foreign import ccall unsafe "genSymZh" genSymZh :: IO Int
90
91 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
92 listSplitUniqSupply  (MkSplitUniqSupply _ s1 s2) = s1 : listSplitUniqSupply s2
93 \end{code}
94
95 \begin{code}
96 uniqFromSupply  (MkSplitUniqSupply n _ _)  = mkUniqueGrimily (iBox n)
97 uniqsFromSupply (MkSplitUniqSupply n _ s2) = mkUniqueGrimily (iBox n) : uniqsFromSupply s2
98 \end{code}
99
100 %************************************************************************
101 %*                                                                      *
102 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
103 %*                                                                      *
104 %************************************************************************
105
106 \begin{code}
107 -- | A monad which just gives the ability to obtain 'Unique's
108 newtype UniqSM result = USM { unUSM :: UniqSupply -> (result, UniqSupply) }
109
110 instance Monad UniqSM where
111   return = returnUs
112   (>>=) = thenUs
113   (>>)  = thenUs_
114
115 instance Functor UniqSM where
116     fmap f (USM x) = USM (\us -> case x us of
117                                  (r, us') -> (f r, us'))
118
119 instance Applicative UniqSM where
120     pure = returnUs
121     (USM f) <*> (USM x) = USM $ \us -> case f us of
122                             (ff, us')  -> case x us' of
123                               (xx, us'') -> (ff xx, us'')
124
125 -- | Run the 'UniqSM' action, returning the final 'UniqSupply'
126 initUs :: UniqSupply -> UniqSM a -> (a, UniqSupply)
127 initUs init_us m = case unUSM m init_us of { (r,us) -> (r,us) }
128
129 -- | Run the 'UniqSM' action, discarding the final 'UniqSupply'
130 initUs_ :: UniqSupply -> UniqSM a -> a
131 initUs_ init_us m = case unUSM m init_us of { (r, _) -> r }
132
133 {-# INLINE thenUs #-}
134 {-# INLINE lazyThenUs #-}
135 {-# INLINE returnUs #-}
136 {-# INLINE splitUniqSupply #-}
137 \end{code}
138
139 @thenUs@ is where we split the @UniqSupply@.
140 \begin{code}
141 instance MonadFix UniqSM where
142     mfix m = USM (\us -> let (r,us') = unUSM (m r) us in (r,us'))
143
144 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
145 thenUs (USM expr) cont
146   = USM (\us -> case (expr us) of
147                    (result, us') -> unUSM (cont result) us')
148
149 lazyThenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
150 lazyThenUs (USM expr) cont
151   = USM (\us -> let (result, us') = expr us in unUSM (cont result) us')
152
153 thenUs_ :: UniqSM a -> UniqSM b -> UniqSM b
154 thenUs_ (USM expr) (USM cont)
155   = USM (\us -> case (expr us) of { (_, us') -> cont us' })
156
157 returnUs :: a -> UniqSM a
158 returnUs result = USM (\us -> (result, us))
159
160 getUs :: UniqSM UniqSupply
161 getUs = USM (\us -> splitUniqSupply us)
162
163 -- | A monad for generating unique identifiers
164 class Monad m => MonadUnique m where
165     -- | Get a new UniqueSupply
166     getUniqueSupplyM :: m UniqSupply
167     -- | Get a new unique identifier
168     getUniqueM  :: m Unique
169     -- | Get an infinite list of new unique identifiers
170     getUniquesM :: m [Unique]
171
172     getUniqueM  = liftM uniqFromSupply  getUniqueSupplyM
173     getUniquesM = liftM uniqsFromSupply getUniqueSupplyM
174
175 instance MonadUnique UniqSM where
176     getUniqueSupplyM = USM (\us -> splitUniqSupply us)
177     getUniqueM  = getUniqueUs
178     getUniquesM = getUniquesUs
179
180 getUniqueUs :: UniqSM Unique
181 getUniqueUs = USM (\us -> case splitUniqSupply us of
182                           (us1,us2) -> (uniqFromSupply us1, us2))
183
184 getUniquesUs :: UniqSM [Unique]
185 getUniquesUs = USM (\us -> case splitUniqSupply us of
186                            (us1,us2) -> (uniqsFromSupply us1, us2))
187
188 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
189 mapUs _ []     = returnUs []
190 mapUs f (x:xs)
191   = f x         `thenUs` \ r  ->
192     mapUs f xs  `thenUs` \ rs ->
193     returnUs (r:rs)
194 \end{code}
195
196 \begin{code}
197 -- {-# SPECIALIZE mapM          :: (a -> UniqSM b) -> [a] -> UniqSM [b] #-}
198 -- {-# SPECIALIZE mapAndUnzipM  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c]) #-}
199 -- {-# SPECIALIZE mapAndUnzip3M :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d]) #-}
200
201 lazyMapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
202 lazyMapUs _ []     = returnUs []
203 lazyMapUs f (x:xs)
204   = f x             `lazyThenUs` \ r  ->
205     lazyMapUs f xs  `lazyThenUs` \ rs ->
206     returnUs (r:rs)
207 \end{code}