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