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