[project @ 2000-07-11 16:12:11 by simonmar]
[ghc-hetmet.git] / ghc / compiler / deSugar / DsMonad.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
3 %
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
5
6 \begin{code}
7 module DsMonad (
8         DsM,
9         initDs, returnDs, thenDs, andDs, mapDs, listDs,
10         mapAndUnzipDs, zipWithDs, foldlDs,
11         uniqSMtoDsM,
12         newTyVarsDs, cloneTyVarsDs,
13         duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
14         newFailLocalDs,
15         getSrcLocDs, putSrcLocDs,
16         getModuleDs,
17         getUniqueDs,
18         dsLookupGlobalValue,
19
20         ValueEnv,
21         dsWarn, 
22         DsWarnings,
23         DsMatchContext(..), DsMatchKind(..)
24     ) where
25
26 #include "HsVersions.h"
27
28 import Bag              ( emptyBag, snocBag, Bag )
29 import ErrUtils         ( WarnMsg )
30 import Id               ( mkSysLocal, setIdUnique, Id )
31 import Module           ( Module )
32 import Var              ( TyVar, setTyVarUnique )
33 import Outputable
34 import SrcLoc           ( noSrcLoc, SrcLoc )
35 import TcHsSyn          ( TypecheckedPat )
36 import TcEnv            ( ValueEnv )
37 import Type             ( Type )
38 import UniqSupply       ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
39                           UniqSM, UniqSupply )
40 import Unique           ( Unique )
41 import UniqFM           ( lookupWithDefaultUFM_Directly )
42 import Util             ( zipWithEqual )
43
44 infixr 9 `thenDs`
45 \end{code}
46
47 Now the mondo monad magic (yes, @DsM@ is a silly name)---carry around
48 a @UniqueSupply@ and some annotations, which
49 presumably include source-file location information:
50 \begin{code}
51 type DsM result =
52         UniqSupply
53         -> ValueEnv
54         -> SrcLoc                -- to put in pattern-matching error msgs
55         -> Module                -- module: for SCC profiling
56         -> DsWarnings
57         -> (result, DsWarnings)
58
59 type DsWarnings = Bag WarnMsg           -- The desugarer reports matches which are
60                                         -- completely shadowed or incomplete patterns
61
62 {-# INLINE andDs #-}
63 {-# INLINE thenDs #-}
64 {-# INLINE returnDs #-}
65
66 -- initDs returns the UniqSupply out the end (not just the result)
67
68 initDs  :: UniqSupply
69         -> ValueEnv
70         -> Module   -- module name: for profiling
71         -> DsM a
72         -> (a, DsWarnings)
73
74 initDs init_us genv mod action
75   = action init_us genv noSrcLoc mod emptyBag
76
77 thenDs :: DsM a -> (a -> DsM b) -> DsM b
78 andDs  :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
79
80 thenDs m1 m2 us genv loc mod warns
81   = case splitUniqSupply us                 of { (s1, s2) ->
82     case (m1 s1 genv loc mod warns)  of { (result, warns1) ->
83     m2 result s2 genv loc mod warns1}}
84
85 andDs combiner m1 m2 us genv loc mod warns
86   = case splitUniqSupply us                 of { (s1, s2) ->
87     case (m1 s1 genv loc mod warns)  of { (result1, warns1) ->
88     case (m2 s2 genv loc mod warns1) of { (result2, warns2) ->
89     (combiner result1 result2, warns2) }}}
90
91 returnDs :: a -> DsM a
92 returnDs result us genv loc mod warns = (result, warns)
93
94 listDs :: [DsM a] -> DsM [a]
95 listDs []     = returnDs []
96 listDs (x:xs)
97   = x           `thenDs` \ r  ->
98     listDs xs   `thenDs` \ rs ->
99     returnDs (r:rs)
100
101 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
102
103 mapDs f []     = returnDs []
104 mapDs f (x:xs)
105   = f x         `thenDs` \ r  ->
106     mapDs f xs  `thenDs` \ rs ->
107     returnDs (r:rs)
108
109 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
110
111 foldlDs k z []     = returnDs z
112 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
113                      foldlDs k r xs
114
115 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
116
117 mapAndUnzipDs f []     = returnDs ([], [])
118 mapAndUnzipDs f (x:xs)
119   = f x                 `thenDs` \ (r1, r2)  ->
120     mapAndUnzipDs f xs  `thenDs` \ (rs1, rs2) ->
121     returnDs (r1:rs1, r2:rs2)
122
123 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
124
125 zipWithDs f []     ys = returnDs []
126 zipWithDs f (x:xs) (y:ys)
127   = f x y               `thenDs` \ r  ->
128     zipWithDs f xs ys   `thenDs` \ rs ->
129     returnDs (r:rs)
130 \end{code}
131
132 And all this mysterious stuff is so we can occasionally reach out and
133 grab one or more names.  @newLocalDs@ isn't exported---exported
134 functions are defined with it.  The difference in name-strings makes
135 it easier to read debugging output.
136
137 \begin{code}
138 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
139 newSysLocalDs ty us genv loc mod warns
140   = case uniqFromSupply us of { assigned_uniq ->
141     (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
142
143 newSysLocalsDs tys = mapDs newSysLocalDs tys
144
145 newFailLocalDs ty us genv loc mod warns
146   = case uniqFromSupply us of { assigned_uniq ->
147     (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
148         -- The UserLocal bit just helps make the code a little clearer
149
150 getUniqueDs :: DsM Unique
151 getUniqueDs us genv loc mod warns
152   = case (uniqFromSupply us) of { assigned_uniq ->
153     (assigned_uniq, warns) }
154
155 duplicateLocalDs :: Id -> DsM Id
156 duplicateLocalDs old_local us genv loc mod warns
157   = case uniqFromSupply us of { assigned_uniq ->
158     (setIdUnique old_local assigned_uniq, warns) }
159
160 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
161 cloneTyVarsDs tyvars us genv loc mod warns
162   = case uniqsFromSupply (length tyvars) us of { uniqs ->
163     (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
164 \end{code}
165
166 \begin{code}
167 newTyVarsDs :: [TyVar] -> DsM [TyVar]
168
169 newTyVarsDs tyvar_tmpls us genv loc mod warns
170   = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
171     (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
172 \end{code}
173
174 We can also reach out and either set/grab location information from
175 the @SrcLoc@ being carried around.
176 \begin{code}
177 uniqSMtoDsM :: UniqSM a -> DsM a
178
179 uniqSMtoDsM u_action us genv loc mod warns
180   = (initUs_ us u_action, warns)
181
182 getSrcLocDs :: DsM SrcLoc
183 getSrcLocDs us genv loc mod warns
184   = (loc, warns)
185
186 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
187 putSrcLocDs new_loc expr us genv old_loc mod warns
188   = expr us genv new_loc mod warns
189
190 dsWarn :: WarnMsg -> DsM ()
191 dsWarn warn us genv loc mod warns = ((), warns `snocBag` warn)
192
193 \end{code}
194
195 \begin{code}
196 getModuleDs :: DsM Module
197 getModuleDs us genv loc mod warns = (mod, warns)
198 \end{code}
199
200 \begin{code}
201 dsLookupGlobalValue :: Unique -> DsM Id
202 dsLookupGlobalValue key us genv loc mod warns
203   = (lookupWithDefaultUFM_Directly genv def key, warns)
204   where
205     def = pprPanic "tcLookupGlobalValue:" (ppr key)
206 \end{code}
207
208
209 %************************************************************************
210 %*                                                                      *
211 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
212 %*                                                                      *
213 %************************************************************************
214
215 \begin{code}
216 data DsMatchContext
217   = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc
218   | NoMatchContext
219   deriving ()
220
221 data DsMatchKind
222   = FunMatch Id
223   | CaseMatch
224   | LambdaMatch
225   | PatBindMatch
226   | DoBindMatch
227   | ListCompMatch
228   | LetMatch
229   | RecUpdMatch
230   deriving ()
231 \end{code}