2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcModule]{Typechecking a whole module}
12 #include "HsVersions.h"
14 import CmdLineOpts ( opt_D_dump_tc, opt_D_dump_types, opt_PprStyle_Debug )
15 import HsSyn ( HsModule(..), HsBinds(..), MonoBinds(..), HsDecl(..) )
16 import HsTypes ( toHsType )
17 import RnHsSyn ( RenamedHsModule )
18 import TcHsSyn ( TypecheckedMonoBinds,
19 TypecheckedForeignDecl, TypecheckedRuleDecl,
20 zonkTopBinds, zonkForeignExports, zonkRules
24 import Inst ( emptyLIE, plusLIE )
25 import TcBinds ( tcTopBindsAndThen )
26 import TcClassDcl ( tcClassDecls2, mkImplicitClassBinds )
27 import TcDefaults ( tcDefaults )
28 import TcEnv ( tcExtendGlobalValEnv,
29 tcEnvTyCons, tcEnvClasses,
30 tcSetValueEnv, tcSetInstEnv, initEnv,
33 import TcRules ( tcRules )
34 import TcForeign ( tcForeignImports, tcForeignExports )
35 import TcIfaceSig ( tcInterfaceSigs )
36 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
37 import TcInstUtil ( buildInstanceEnv, InstInfo )
38 import TcSimplify ( tcSimplifyTop )
39 import TcTyClsDecls ( tcTyAndClassDecls )
40 import TcTyDecls ( mkImplicitDataBinds )
42 import CoreUnfold ( unfoldingTemplate )
43 import Type ( funResultTy, splitForAllTys )
44 import RnMonad ( RnNameSupply, FixityEnv )
45 import Bag ( isEmptyBag )
46 import ErrUtils ( printErrorsAndWarnings, dumpIfSet )
47 import Id ( idType, idName, idUnfolding )
48 import Module ( pprModuleName, mkThisModule )
49 import Name ( nameOccName, isLocallyDefined, isGlobalName,
50 toRdrName, nameEnvElts,
52 import TyCon ( TyCon, isDataTyCon, tyConName, tyConGenInfo )
53 import OccName ( isSysOcc )
54 import TyCon ( TyCon, isClassTyCon )
55 import Class ( Class )
56 import PrelNames ( mAIN_Name, mainKey )
57 import UniqSupply ( UniqSupply )
58 import Maybes ( maybeToBool )
60 import BasicTypes ( EP(..) )
61 import Bag ( Bag, isEmptyBag )
66 Outside-world interface:
69 -- Convenient type synonyms first:
72 tc_binds :: TypecheckedMonoBinds,
74 tc_classes :: [Class],
75 tc_insts :: Bag InstInfo, -- Instance declaration information
76 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
77 tc_rules :: [TypecheckedRuleDecl], -- Transformation rules
87 -> IO (Maybe TcResults)
89 typecheckModule us rn_name_supply fixity_env mod
90 = initTc us initEnv (tcModule rn_name_supply fixity_env mod) >>= \ (maybe_result, warns, errs) ->
92 printErrorsAndWarnings errs warns >>
96 Just results -> dumpIfSet opt_D_dump_types "Type signatures" (dump_sigs results) >>
97 dumpIfSet opt_D_dump_tc "Typechecked" (dump_tc results)
100 return (if isEmptyBag errs then
107 The internal monster:
109 tcModule :: RnNameSupply -- for renaming derivings
110 -> FixityEnv -- needed for Show/Read derivings.
111 -> RenamedHsModule -- input
112 -> TcM TcResults -- output
114 tcModule rn_name_supply fixities
115 (HsModule mod_name _ _ _ decls _ src_loc)
116 = tcAddSrcLoc src_loc $ -- record where we're starting
118 fixTc (\ ~(unf_env ,_) ->
119 -- unf_env is used for type-checking interface pragmas
120 -- which is done lazily [ie failure just drops the pragma
121 -- without having any global-failure effect].
123 -- unf_env is also used to get the pragam info
124 -- for imported dfuns and default methods
126 -- Type-check the type and class decls
127 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
130 -- Typecheck the instance decls, includes deriving
131 tcInstDecls1 unf_env decls
132 (mkThisModule mod_name)
133 fixities rn_name_supply `thenTc` \ (inst_info, deriv_binds) ->
135 buildInstanceEnv inst_info `thenNF_Tc` \ inst_env ->
137 tcSetInstEnv inst_env $
139 classes = tcEnvClasses env
140 tycons = tcEnvTyCons env -- INCLUDES tycons derived from classes
141 local_classes = filter isLocallyDefined classes
142 local_tycons = [ tc | tc <- tycons,
144 not (isClassTyCon tc)
146 -- For local_tycons, filter out the ones derived from classes
147 -- Otherwise the latter show up in interface files
150 -- Default declarations
151 tcDefaults decls `thenTc` \ defaulting_tys ->
152 tcSetDefaultTys defaulting_tys $
154 -- Interface type signatures
155 -- We tie a knot so that the Ids read out of interfaces are in scope
156 -- when we read their pragmas.
157 -- What we rely on is that pragmas are typechecked lazily; if
158 -- any type errors are found (ie there's an inconsistency)
159 -- we silently discard the pragma
160 -- We must do this before mkImplicitDataBinds (which comes next), since
161 -- the latter looks up unpackCStringId, for example, which is usually
163 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
164 tcExtendGlobalValEnv sig_ids $
166 -- Create any necessary record selector Ids and their bindings
167 -- "Necessary" includes data and newtype declarations
168 -- We don't create bindings for dictionary constructors;
169 -- they are always fully applied, and the bindings are just there
170 -- to support partial applications
171 mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
172 mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
174 -- Extend the global value environment with
176 -- (b) record selectors
177 -- (c) class op selectors
178 -- (d) default-method ids... where? I can't see where these are
179 -- put into the envt, and I'm worried that the zonking phase
180 -- will find they aren't there and complain.
181 tcExtendGlobalValEnv data_ids $
182 tcExtendGlobalValEnv cls_ids $
184 -- foreign import declarations next.
185 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
186 tcExtendGlobalValEnv fo_ids $
188 -- Value declarations next.
189 -- We also typecheck any extra binds that came out of the "deriving" process
191 (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
192 (get_val_decls decls `ThenBinds` deriv_binds)
193 ( tcGetEnv `thenNF_Tc` \ env ->
194 tcGetUnique `thenNF_Tc` \ uniq ->
195 returnTc ((EmptyMonoBinds, env), emptyLIE)
196 ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
199 -- foreign export declarations next.
200 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
202 -- Second pass over class and instance declarations,
203 -- to compile the bindings themselves.
204 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
205 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
206 tcRules decls `thenNF_Tc` \ (lie_rules, rules) ->
209 -- Deal with constant or ambiguous InstIds. How could
210 -- there be ambiguous ones? They can only arise if a
211 -- top-level decl falls under the monomorphism
212 -- restriction, and no subsequent decl instantiates its
213 -- type. (Usually, ambiguous type variables are resolved
214 -- during the generalisation step.)
216 lie_alldecls = lie_valdecls `plusLIE`
217 lie_instdecls `plusLIE`
218 lie_clasdecls `plusLIE`
219 lie_fodecls `plusLIE`
222 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
224 -- Check that Main defines main
225 (if mod_name == mAIN_Name then
226 tcLookupGlobal_maybe mainName `thenNF_Tc` \ maybe_main ->
228 Just (AnId _) -> returnTc ()
229 other -> addErrTc noMainErr
234 -- Backsubstitution. This must be done last.
235 -- Even tcSimplifyTop may do some unification.
237 all_binds = imp_data_binds `AndMonoBinds`
238 imp_cls_binds `AndMonoBinds`
239 val_binds `AndMonoBinds`
240 inst_binds `AndMonoBinds`
241 cls_dm_binds `AndMonoBinds`
242 const_inst_binds `AndMonoBinds`
245 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
246 tcSetValueEnv really_final_env $
247 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
248 zonkRules rules `thenNF_Tc` \ rules' ->
250 returnTc (really_final_env,
251 (TcResults { tc_binds = all_binds',
252 tc_tycons = local_tycons,
253 tc_classes = local_classes,
254 tc_insts = inst_info,
255 tc_fords = foi_decls ++ foe_decls',
257 tc_env = really_final_env
260 -- End of outer fix loop
261 ) `thenTc` \ (final_env, stuff) ->
264 get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
270 = hsep [ptext SLIT("Module"), quotes (pprModuleName mAIN_Name),
271 ptext SLIT("must include a definition for"), quotes (ptext SLIT("main"))]
275 %************************************************************************
277 \subsection{Dumping output}
279 %************************************************************************
283 = vcat [ppr (tc_binds results),
284 pp_rules (tc_rules results),
285 ppr_gen_tycons (tc_tycons results)
288 dump_sigs results -- Print type signatures
289 = -- Convert to HsType so that we get source-language style printing
290 -- And sort by RdrName
291 vcat $ map ppr_sig $ sortLt lt_sig $
292 [(toRdrName id, toHsType (idType id)) | id <- nameEnvElts (tc_env results),
296 lt_sig (n1,_) (n2,_) = n1 < n2
297 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
299 want_sig id | opt_PprStyle_Debug = True
300 | otherwise = isLocallyDefined n &&
302 not (isSysOcc (nameOccName n))
306 ppr_gen_tycons tcs = vcat [ptext SLIT("{-# Generic type constructor details"),
307 vcat (map ppr_gen_tycon (filter isLocallyDefined tcs)),
311 -- x&y are now Id's, not CoreExpr's
313 | Just ep <- tyConGenInfo tycon
314 = (ppr tycon <> colon) $$ nest 4 (ppr_ep ep)
316 | otherwise = ppr tycon <> colon <+> ptext SLIT("Not derivable")
319 = vcat [ ptext SLIT("Rep type:") <+> ppr (funResultTy from_tau),
320 ptext SLIT("From:") <+> ppr (unfoldingTemplate (idUnfolding from)),
321 ptext SLIT("To:") <+> ppr (unfoldingTemplate (idUnfolding to))
324 (_,from_tau) = splitForAllTys (idType from)
327 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
328 nest 4 (vcat (map ppr rs)),