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