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 ( TcMonoBinds, TypecheckedMonoBinds,
19 TypecheckedForeignDecl, TypecheckedRuleDecl,
20 zonkTopBinds, zonkForeignExports, zonkRules
24 import Inst ( Inst, emptyLIE, plusLIE )
25 import TcBinds ( tcTopBindsAndThen )
26 import TcClassDcl ( tcClassDecls2, mkImplicitClassBinds )
27 import TcDefaults ( tcDefaults )
28 import TcEnv ( tcExtendGlobalValEnv, tcExtendTypeEnv,
29 getEnvTyCons, getEnvClasses, tcLookupValueByKeyMaybe,
30 explicitLookupValueByKey, tcSetValueEnv, tcSetInstEnv,
32 ValueEnv, TcTyThing(..)
34 import TcRules ( tcRules )
35 import TcForeign ( tcForeignImports, tcForeignExports )
36 import TcIfaceSig ( tcInterfaceSigs )
37 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
38 import TcInstUtil ( buildInstanceEnv, InstInfo )
39 import TcSimplify ( tcSimplifyTop )
40 import TcTyClsDecls ( tcTyAndClassDecls )
41 import TcTyDecls ( mkImplicitDataBinds )
42 import TcType ( TcType, typeToTcType,
47 import RnMonad ( RnNameSupply, FixityEnv )
48 import Bag ( isEmptyBag )
49 import ErrUtils ( Message, printErrorsAndWarnings, dumpIfSet )
50 import Id ( Id, idType, idName )
51 import Module ( pprModuleName )
52 import OccName ( isSysOcc )
53 import Name ( Name, nameUnique, nameOccName, isLocallyDefined,
54 toRdrName, nameEnvElts, NamedThing(..)
56 import TyCon ( TyCon, tyConKind )
57 import Class ( Class, classSelIds, classTyCon )
58 import PrelInfo ( mAIN_Name )
59 import Unique ( Unique, mainKey )
60 import UniqSupply ( UniqSupply )
61 import Maybes ( maybeToBool )
63 import Bag ( Bag, isEmptyBag )
67 Outside-world interface:
70 -- Convenient type synonyms first:
73 tc_binds :: TypecheckedMonoBinds,
75 tc_classes :: [Class],
76 tc_insts :: Bag InstInfo, -- Instance declaration information
77 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
78 tc_rules :: [TypecheckedRuleDecl], -- Transformation rules
88 -> IO (Maybe TcResults)
90 typecheckModule us rn_name_supply fixity_env mod
91 = initTc us initEnv (tcModule rn_name_supply fixity_env mod) >>= \ (maybe_result, warns, errs) ->
93 printErrorsAndWarnings errs warns >>
97 Just results -> dumpIfSet opt_D_dump_types "Type signatures" (dump_sigs results) >>
98 dumpIfSet opt_D_dump_tc "Typechecked" (dump_tc results)
101 return (if isEmptyBag errs then
107 = ppr (tc_binds results) $$ pp_rules (tc_rules results)
109 dump_sigs results -- Print type signatures
110 = -- Convert to HsType so that we get source-language style printing
111 -- And sort by RdrName
112 vcat $ map ppr_sig $ sortLt lt_sig $
113 [(toRdrName id, toHsType (idType id)) | id <- nameEnvElts (tc_env results),
117 lt_sig (n1,_) (n2,_) = n1 < n2
118 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
120 want_sig id | opt_PprStyle_Debug = True
121 | otherwise = isLocallyDefined n && not (isSysOcc (nameOccName n))
126 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
127 nest 4 (vcat (map ppr rs)),
131 The internal monster:
133 tcModule :: RnNameSupply -- for renaming derivings
134 -> FixityEnv -- needed for Show/Read derivings.
135 -> RenamedHsModule -- input
136 -> TcM s TcResults -- output
138 tcModule rn_name_supply fixities
139 (HsModule mod_name _ _ _ decls _ src_loc)
140 = tcAddSrcLoc src_loc $ -- record where we're starting
142 fixTc (\ ~(unf_env ,_) ->
143 -- unf_env is used for type-checking interface pragmas
144 -- which is done lazily [ie failure just drops the pragma
145 -- without having any global-failure effect].
147 -- unf_env is also used to get the pragam info
148 -- for imported dfuns and default methods
150 -- Type-check the type and class decls
151 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
153 -- Typecheck the instance decls, includes deriving
156 tcInstDecls1 unf_env decls
158 rn_name_supply `thenTc` \ (inst_info, deriv_binds) ->
160 buildInstanceEnv inst_info `thenNF_Tc` \ inst_env ->
162 tcSetInstEnv inst_env $
164 tycons = getEnvTyCons env
165 classes = getEnvClasses env
166 local_tycons = filter isLocallyDefined tycons
167 local_classes = filter isLocallyDefined classes
170 -- Default declarations
171 tcDefaults decls `thenTc` \ defaulting_tys ->
172 tcSetDefaultTys defaulting_tys $
174 -- Extend the TyCon envt with the tycons corresponding to
176 -- They are mentioned in types in interface files.
177 tcExtendTypeEnv [ (getName tycon, (kindToTcKind (tyConKind tycon), ADataTyCon tycon))
179 let tycon = classTyCon clas
182 -- Interface type signatures
183 -- We tie a knot so that the Ids read out of interfaces are in scope
184 -- when we read their pragmas.
185 -- What we rely on is that pragmas are typechecked lazily; if
186 -- any type errors are found (ie there's an inconsistency)
187 -- we silently discard the pragma
188 -- We must do this before mkImplicitDataBinds (which comes next), since
189 -- the latter looks up unpackCStringId, for example, which is usually
191 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
192 tcExtendGlobalValEnv sig_ids $
194 -- Create any necessary record selector Ids and their bindings
195 -- "Necessary" includes data and newtype declarations
196 -- We don't create bindings for dictionary constructors;
197 -- they are always fully applied, and the bindings are just there
198 -- to support partial applications
199 mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
200 mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
202 -- Extend the global value environment with
204 -- (b) record selectors
205 -- (c) class op selectors
206 -- (d) default-method ids... where? I can't see where these are
207 -- put into the envt, and I'm worried that the zonking phase
208 -- will find they aren't there and complain.
209 tcExtendGlobalValEnv data_ids $
210 tcExtendGlobalValEnv cls_ids $
212 -- foreign import declarations next.
213 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
214 tcExtendGlobalValEnv fo_ids $
216 -- Value declarations next.
217 -- We also typecheck any extra binds that came out of the "deriving" process
219 (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
220 (get_val_decls decls `ThenBinds` deriv_binds)
221 ( tcGetEnv `thenNF_Tc` \ env ->
222 tcGetUnique `thenNF_Tc` \ uniq ->
223 returnTc ((EmptyMonoBinds, env), emptyLIE)
224 ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
227 -- foreign export declarations next.
228 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
230 -- Second pass over class and instance declarations,
231 -- to compile the bindings themselves.
232 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
233 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
234 tcRules decls `thenNF_Tc` \ (lie_rules, rules) ->
237 -- Deal with constant or ambiguous InstIds. How could
238 -- there be ambiguous ones? They can only arise if a
239 -- top-level decl falls under the monomorphism
240 -- restriction, and no subsequent decl instantiates its
241 -- type. (Usually, ambiguous type variables are resolved
242 -- during the generalisation step.)
244 lie_alldecls = lie_valdecls `plusLIE`
245 lie_instdecls `plusLIE`
246 lie_clasdecls `plusLIE`
247 lie_fodecls `plusLIE`
250 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
252 -- Check that Main defines main
253 (if mod_name == mAIN_Name then
254 tcLookupValueByKeyMaybe mainKey `thenNF_Tc` \ maybe_main ->
255 checkTc (maybeToBool maybe_main) noMainErr
260 -- Backsubstitution. This must be done last.
261 -- Even tcSimplifyTop may do some unification.
263 all_binds = imp_data_binds `AndMonoBinds`
264 imp_cls_binds `AndMonoBinds`
265 val_binds `AndMonoBinds`
266 inst_binds `AndMonoBinds`
267 cls_dm_binds `AndMonoBinds`
268 const_inst_binds `AndMonoBinds`
271 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
272 tcSetValueEnv really_final_env $
273 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
274 zonkRules rules `thenNF_Tc` \ rules' ->
276 returnTc (really_final_env,
277 (TcResults { tc_binds = all_binds',
278 tc_tycons = local_tycons,
279 tc_classes = local_classes,
280 tc_insts = inst_info,
281 tc_fords = foi_decls ++ foe_decls',
283 tc_env = really_final_env
286 -- End of outer fix loop
287 ) `thenTc` \ (final_env, stuff) ->
290 get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
296 = hsep [ptext SLIT("Module"), quotes (pprModuleName mAIN_Name),
297 ptext SLIT("must include a definition for"), quotes (ptext SLIT("main"))]