2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsMonad]{@DsMonad@: monadery used in desugaring}
9 initDs, returnDs, thenDs, andDs, mapDs, listDs,
10 mapAndUnzipDs, zipWithDs, foldlDs,
12 newTyVarsDs, cloneTyVarsDs,
13 duplicateLocalDs, newSysLocalDs, newSysLocalsDs,
15 getSrcLocDs, putSrcLocDs,
23 DsMatchContext(..), DsMatchKind(..)
26 #include "HsVersions.h"
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 )
34 import SrcLoc ( noSrcLoc, SrcLoc )
35 import TcHsSyn ( TypecheckedPat )
36 import TcEnv ( ValueEnv )
38 import UniqSupply ( initUs_, splitUniqSupply, uniqFromSupply, uniqsFromSupply,
40 import Unique ( Unique )
41 import UniqFM ( lookupWithDefaultUFM_Directly )
42 import Util ( zipWithEqual )
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:
53 -> (Name -> Id) -- Lookup well-known Ids
54 -> SrcLoc -- to put in pattern-matching error msgs
55 -> Module -- module: for SCC profiling
57 -> (result, DsWarnings)
59 type DsWarnings = Bag WarnMsg -- The desugarer reports matches which are
60 -- completely shadowed or incomplete patterns
64 {-# INLINE returnDs #-}
66 -- initDs returns the UniqSupply out the end (not just the result)
69 -> (HomeSymbolTable, PersistentCompilerState, TypeEnv)
70 -> Module -- module name: for profiling
74 initDs init_us (hst,pcs,local_type_env) mod action
75 = action init_us lookup noSrcLoc mod emptyBag
77 -- This lookup is used for well-known Ids,
78 -- such as fold, build, cons etc, so the chances are
79 -- it'll be found in the package symbol table. That's
80 -- why we don't merge all these tables
82 lookup n = case lookupTypeEnv pst n of {
85 case lookupTypeEnv hst n of {
88 case lookupNameEnv local_type_env n of
90 other -> pprPanic "initDS: lookup:" (ppr n)
92 thenDs :: DsM a -> (a -> DsM b) -> DsM b
93 andDs :: (a -> a -> a) -> DsM a -> DsM a -> DsM a
95 thenDs m1 m2 us genv loc mod warns
96 = case splitUniqSupply us of { (s1, s2) ->
97 case (m1 s1 genv loc mod warns) of { (result, warns1) ->
98 m2 result s2 genv loc mod warns1}}
100 andDs combiner m1 m2 us genv loc mod warns
101 = case splitUniqSupply us of { (s1, s2) ->
102 case (m1 s1 genv loc mod warns) of { (result1, warns1) ->
103 case (m2 s2 genv loc mod warns1) of { (result2, warns2) ->
104 (combiner result1 result2, warns2) }}}
106 returnDs :: a -> DsM a
107 returnDs result us genv loc mod warns = (result, warns)
109 listDs :: [DsM a] -> DsM [a]
110 listDs [] = returnDs []
113 listDs xs `thenDs` \ rs ->
116 mapDs :: (a -> DsM b) -> [a] -> DsM [b]
118 mapDs f [] = returnDs []
120 = f x `thenDs` \ r ->
121 mapDs f xs `thenDs` \ rs ->
124 foldlDs :: (a -> b -> DsM a) -> a -> [b] -> DsM a
126 foldlDs k z [] = returnDs z
127 foldlDs k z (x:xs) = k z x `thenDs` \ r ->
130 mapAndUnzipDs :: (a -> DsM (b, c)) -> [a] -> DsM ([b], [c])
132 mapAndUnzipDs f [] = returnDs ([], [])
133 mapAndUnzipDs f (x:xs)
134 = f x `thenDs` \ (r1, r2) ->
135 mapAndUnzipDs f xs `thenDs` \ (rs1, rs2) ->
136 returnDs (r1:rs1, r2:rs2)
138 zipWithDs :: (a -> b -> DsM c) -> [a] -> [b] -> DsM [c]
140 zipWithDs f [] ys = returnDs []
141 zipWithDs f (x:xs) (y:ys)
142 = f x y `thenDs` \ r ->
143 zipWithDs f xs ys `thenDs` \ rs ->
147 And all this mysterious stuff is so we can occasionally reach out and
148 grab one or more names. @newLocalDs@ isn't exported---exported
149 functions are defined with it. The difference in name-strings makes
150 it easier to read debugging output.
153 newSysLocalDs, newFailLocalDs :: Type -> DsM Id
154 newSysLocalDs ty us genv loc mod warns
155 = case uniqFromSupply us of { assigned_uniq ->
156 (mkSysLocal SLIT("ds") assigned_uniq ty, warns) }
158 newSysLocalsDs tys = mapDs newSysLocalDs tys
160 newFailLocalDs ty us genv loc mod warns
161 = case uniqFromSupply us of { assigned_uniq ->
162 (mkSysLocal SLIT("fail") assigned_uniq ty, warns) }
163 -- The UserLocal bit just helps make the code a little clearer
165 getUniqueDs :: DsM Unique
166 getUniqueDs us genv loc mod warns
167 = case (uniqFromSupply us) of { assigned_uniq ->
168 (assigned_uniq, warns) }
170 duplicateLocalDs :: Id -> DsM Id
171 duplicateLocalDs old_local us genv loc mod warns
172 = case uniqFromSupply us of { assigned_uniq ->
173 (setIdUnique old_local assigned_uniq, warns) }
175 cloneTyVarsDs :: [TyVar] -> DsM [TyVar]
176 cloneTyVarsDs tyvars us genv loc mod warns
177 = case uniqsFromSupply (length tyvars) us of { uniqs ->
178 (zipWithEqual "cloneTyVarsDs" setTyVarUnique tyvars uniqs, warns) }
182 newTyVarsDs :: [TyVar] -> DsM [TyVar]
184 newTyVarsDs tyvar_tmpls us genv loc mod warns
185 = case uniqsFromSupply (length tyvar_tmpls) us of { uniqs ->
186 (zipWithEqual "newTyVarsDs" setTyVarUnique tyvar_tmpls uniqs, warns) }
189 We can also reach out and either set/grab location information from
190 the @SrcLoc@ being carried around.
192 uniqSMtoDsM :: UniqSM a -> DsM a
194 uniqSMtoDsM u_action us genv loc mod warns
195 = (initUs_ us u_action, warns)
197 getSrcLocDs :: DsM SrcLoc
198 getSrcLocDs us genv loc mod warns
201 putSrcLocDs :: SrcLoc -> DsM a -> DsM a
202 putSrcLocDs new_loc expr us genv old_loc mod warns
203 = expr us genv new_loc mod warns
205 dsWarn :: WarnMsg -> DsM ()
206 dsWarn warn us genv loc mod warns = ((), warns `snocBag` warn)
211 getModuleDs :: DsM Module
212 getModuleDs us genv loc mod warns = (mod, warns)
216 dsLookupGlobalValue :: Name -> DsM Id
217 dsLookupGlobalValue key us genv loc mod warns
220 result = case lookupNameEnv genv name of
222 Nothing -> pprPanic "dsLookupGlobalValue:" (ppr name)
226 %************************************************************************
228 \subsection{Type synonym @EquationInfo@ and access functions for its pieces}
230 %************************************************************************
234 = DsMatchContext DsMatchKind [TypecheckedPat] SrcLoc