[project @ 1998-01-08 18:03:08 by simonm]
[ghc-hetmet.git] / ghc / compiler / basicTypes / UniqSupply.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[UniqSupply]{The @UniqueSupply@ data type and a (monadic) supply thereof}
5
6 \begin{code}
7 module UniqSupply (
8
9         UniqSupply,             -- Abstractly
10
11         getUnique, getUniques,  -- basic ops
12
13         UniqSM,         -- type: unique supply monad
14         initUs, thenUs, returnUs, fixUs,
15         mapUs, mapAndUnzipUs, mapAndUnzip3Us,
16         thenMaybeUs, mapAccumLUs,
17
18         mkSplitUniqSupply,
19         splitUniqSupply
20   ) where
21
22 #include "HsVersions.h"
23
24 import Unique
25 import Util
26
27
28 import GlaExts
29 import IOBase   ( IO(..), IOResult(..) )
30 import PrelBase ( Char(..) )
31
32 w2i x = word2Int# x
33 i2w x = int2Word# x
34 i2w_s x = (x :: Int#)
35 \end{code}
36
37
38 %************************************************************************
39 %*                                                                      *
40 \subsection{Splittable Unique supply: @UniqSupply@}
41 %*                                                                      *
42 %************************************************************************
43
44 %************************************************************************
45 %*                                                                      *
46 \subsubsection[UniqSupply-type]{@UniqSupply@ type and operations}
47 %*                                                                      *
48 %************************************************************************
49
50 A value of type @UniqSupply@ is unique, and it can
51 supply {\em one} distinct @Unique@.  Also, from the supply, one can
52 also manufacture an arbitrary number of further @UniqueSupplies@,
53 which will be distinct from the first and from all others.
54
55 \begin{code}
56 data UniqSupply
57   = MkSplitUniqSupply Int       -- make the Unique with this
58                    UniqSupply UniqSupply
59                                 -- when split => these two supplies
60 \end{code}
61
62 \begin{code}
63 mkSplitUniqSupply :: Char -> IO UniqSupply
64
65 splitUniqSupply :: UniqSupply -> (UniqSupply, UniqSupply)
66 getUnique :: UniqSupply -> Unique
67 getUniques :: Int -> UniqSupply -> [Unique]
68 \end{code}
69
70 \begin{code}
71 mkSplitUniqSupply (C# c#)
72   = let
73         mask# = (i2w (ord# c#)) `shiftL#` (i2w_s 24#)
74
75         -- here comes THE MAGIC:
76
77         -- This is one of the most hammered bits in the whole compiler
78         mk_supply#
79           = unsafeInterleaveIO (
80                 mk_unique   >>= \ uniq ->
81                 mk_supply#  >>= \ s1 ->
82                 mk_supply#  >>= \ s2 ->
83                 return (MkSplitUniqSupply uniq s1 s2)
84             )
85
86         mk_unique = _ccall_ genSymZh            >>= \ (W# u#) ->
87                     return (I# (w2i (mask# `or#` u#)))
88     in
89     mk_supply#
90
91 splitUniqSupply (MkSplitUniqSupply _ s1 s2) = (s1, s2)
92 \end{code}
93
94 \begin{code}
95 getUnique (MkSplitUniqSupply (I# n) _ _) = mkUniqueGrimily n
96
97 getUniques (I# i) supply = i `get_from` supply
98   where
99     get_from 0# _ = []
100     get_from n (MkSplitUniqSupply (I# u) _ s2)
101       = mkUniqueGrimily u : get_from (n -# 1#) s2
102 \end{code}
103
104 %************************************************************************
105 %*                                                                      *
106 \subsubsection[UniqSupply-monad]{@UniqSupply@ monad: @UniqSM@}
107 %*                                                                      *
108 %************************************************************************
109
110 \begin{code}
111 type UniqSM result = UniqSupply -> result
112
113 -- the initUs function also returns the final UniqSupply
114
115 initUs :: UniqSupply -> UniqSM a -> a
116
117 initUs init_us m = m init_us
118
119 {-# INLINE thenUs #-}
120 {-# INLINE returnUs #-}
121 {-# INLINE splitUniqSupply #-}
122 \end{code}
123
124 @thenUs@ is where we split the @UniqSupply@.
125 \begin{code}
126 fixUs :: (a -> UniqSM a) -> UniqSM a
127 fixUs m us
128   = r  where  r = m r us
129
130 thenUs :: UniqSM a -> (a -> UniqSM b) -> UniqSM b
131
132 thenUs expr cont us
133   = case (splitUniqSupply us) of { (s1, s2) ->
134     case (expr s1)            of { result ->
135     cont result s2 }}
136 \end{code}
137
138 \begin{code}
139 returnUs :: a -> UniqSM a
140 returnUs result us = result
141
142 mapUs :: (a -> UniqSM b) -> [a] -> UniqSM [b]
143
144 mapUs f []     = returnUs []
145 mapUs f (x:xs)
146   = f x         `thenUs` \ r  ->
147     mapUs f xs  `thenUs` \ rs ->
148     returnUs (r:rs)
149
150 mapAndUnzipUs  :: (a -> UniqSM (b,c))   -> [a] -> UniqSM ([b],[c])
151 mapAndUnzip3Us :: (a -> UniqSM (b,c,d)) -> [a] -> UniqSM ([b],[c],[d])
152
153 mapAndUnzipUs f [] = returnUs ([],[])
154 mapAndUnzipUs f (x:xs)
155   = f x                 `thenUs` \ (r1,  r2)  ->
156     mapAndUnzipUs f xs  `thenUs` \ (rs1, rs2) ->
157     returnUs (r1:rs1, r2:rs2)
158
159 mapAndUnzip3Us f [] = returnUs ([],[],[])
160 mapAndUnzip3Us f (x:xs)
161   = f x                 `thenUs` \ (r1,  r2,  r3)  ->
162     mapAndUnzip3Us f xs `thenUs` \ (rs1, rs2, rs3) ->
163     returnUs (r1:rs1, r2:rs2, r3:rs3)
164
165 thenMaybeUs :: UniqSM (Maybe a) -> (a -> UniqSM (Maybe b)) -> UniqSM (Maybe b)
166 thenMaybeUs m k
167   = m   `thenUs` \ result ->
168     case result of
169       Nothing -> returnUs Nothing
170       Just x  -> k x
171
172 mapAccumLUs :: (acc -> x -> UniqSM (acc, y))
173             -> acc
174             -> [x]
175             -> UniqSM (acc, [y])
176
177 mapAccumLUs f b []     = returnUs (b, [])
178 mapAccumLUs f b (x:xs)
179   = f b x                           `thenUs` \ (b__2, x__2) ->
180     mapAccumLUs f b__2 xs           `thenUs` \ (b__3, xs__2) ->
181     returnUs (b__3, x__2:xs__2)
182 \end{code}