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 TcExpr ( tcId )
35 import TcRules ( tcRules )
36 import TcForeign ( tcForeignImports, tcForeignExports )
37 import TcIfaceSig ( tcInterfaceSigs )
38 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
39 import TcInstUtil ( buildInstanceEnv, InstInfo )
40 import TcSimplify ( tcSimplifyTop )
41 import TcTyClsDecls ( tcTyAndClassDecls )
42 import TcTyDecls ( mkImplicitDataBinds )
43 import TcType ( TcType, typeToTcType,
48 import RnMonad ( RnNameSupply, FixityEnv )
49 import Bag ( isEmptyBag )
50 import ErrUtils ( Message, printErrorsAndWarnings, dumpIfSet )
51 import Id ( Id, idType, idName )
52 import Module ( pprModuleName )
53 import OccName ( isSysOcc )
54 import Name ( Name, nameUnique, nameOccName, isLocallyDefined,
55 toRdrName, nameEnvElts, NamedThing(..)
57 import TyCon ( TyCon, tyConKind )
58 import Class ( Class, classSelIds, classTyCon )
59 import Type ( mkTyConApp, mkForAllTy,
60 boxedTypeKind, getTyVar, Type )
61 import TysWiredIn ( unitTy )
62 import PrelInfo ( mAIN_Name )
63 import TcUnify ( unifyTauTy )
64 import Unique ( Unique, mainKey )
65 import UniqSupply ( UniqSupply )
66 import Maybes ( maybeToBool )
68 import Bag ( Bag, isEmptyBag )
74 Outside-world interface:
77 -- Convenient type synonyms first:
80 tc_binds :: TypecheckedMonoBinds,
82 tc_classes :: [Class],
83 tc_insts :: Bag InstInfo, -- Instance declaration information
84 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
85 tc_rules :: [TypecheckedRuleDecl], -- Transformation rules
95 -> IO (Maybe TcResults)
97 typecheckModule us rn_name_supply fixity_env mod
98 = initTc us initEnv (tcModule rn_name_supply fixity_env mod) >>= \ (maybe_result, warns, errs) ->
100 printErrorsAndWarnings errs warns >>
102 (case maybe_result of
104 Just results -> dumpIfSet opt_D_dump_types "Type signatures" (dump_sigs results) >>
105 dumpIfSet opt_D_dump_tc "Typechecked" (dump_tc results)
108 return (if isEmptyBag errs then
114 = ppr (tc_binds results) $$ pp_rules (tc_rules results)
116 dump_sigs results -- Print type signatures
117 = -- Convert to HsType so that we get source-language style printing
118 -- And sort by RdrName
119 vcat $ map ppr_sig $ sortLt lt_sig $
120 [(toRdrName id, toHsType (idType id)) | id <- nameEnvElts (tc_env results),
124 lt_sig (n1,_) (n2,_) = n1 < n2
125 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
127 want_sig id | opt_PprStyle_Debug = True
128 | otherwise = isLocallyDefined n && not (isSysOcc (nameOccName n))
133 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
134 nest 4 (vcat (map ppr rs)),
138 The internal monster:
140 tcModule :: RnNameSupply -- for renaming derivings
141 -> FixityEnv -- needed for Show/Read derivings.
142 -> RenamedHsModule -- input
143 -> TcM s TcResults -- output
145 tcModule rn_name_supply fixities
146 (HsModule mod_name _ _ _ decls _ src_loc)
147 = tcAddSrcLoc src_loc $ -- record where we're starting
149 fixTc (\ ~(unf_env ,_) ->
150 -- unf_env is used for type-checking interface pragmas
151 -- which is done lazily [ie failure just drops the pragma
152 -- without having any global-failure effect].
154 -- unf_env is also used to get the pragam info
155 -- for imported dfuns and default methods
157 -- Type-check the type and class decls
158 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
160 -- Typecheck the instance decls, includes deriving
163 tcInstDecls1 unf_env decls
165 rn_name_supply `thenTc` \ (inst_info, deriv_binds) ->
167 buildInstanceEnv inst_info `thenNF_Tc` \ inst_env ->
169 tcSetInstEnv inst_env $
171 tycons = getEnvTyCons env
172 classes = getEnvClasses env
173 local_tycons = filter isLocallyDefined tycons
174 local_classes = filter isLocallyDefined classes
177 -- Default declarations
178 tcDefaults decls `thenTc` \ defaulting_tys ->
179 tcSetDefaultTys defaulting_tys $
181 -- Extend the TyCon envt with the tycons corresponding to
183 -- They are mentioned in types in interface files.
184 tcExtendTypeEnv [ (getName tycon, (kindToTcKind (tyConKind tycon), ADataTyCon tycon))
186 let tycon = classTyCon clas
189 -- Interface type signatures
190 -- We tie a knot so that the Ids read out of interfaces are in scope
191 -- when we read their pragmas.
192 -- What we rely on is that pragmas are typechecked lazily; if
193 -- any type errors are found (ie there's an inconsistency)
194 -- we silently discard the pragma
195 -- We must do this before mkImplicitDataBinds (which comes next), since
196 -- the latter looks up unpackCStringId, for example, which is usually
198 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
199 tcExtendGlobalValEnv sig_ids $
201 -- Create any necessary record selector Ids and their bindings
202 -- "Necessary" includes data and newtype declarations
203 -- We don't create bindings for dictionary constructors;
204 -- they are always fully applied, and the bindings are just there
205 -- to support partial applications
206 mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
207 mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
209 -- Extend the global value environment with
211 -- (b) record selectors
212 -- (c) class op selectors
213 -- (d) default-method ids... where? I can't see where these are
214 -- put into the envt, and I'm worried that the zonking phase
215 -- will find they aren't there and complain.
216 tcExtendGlobalValEnv data_ids $
217 tcExtendGlobalValEnv cls_ids $
219 -- foreign import declarations next.
220 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
221 tcExtendGlobalValEnv fo_ids $
223 -- Value declarations next.
224 -- We also typecheck any extra binds that came out of the "deriving" process
226 (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
227 (get_val_decls decls `ThenBinds` deriv_binds)
228 ( tcGetEnv `thenNF_Tc` \ env ->
229 tcGetUnique `thenNF_Tc` \ uniq ->
230 returnTc ((EmptyMonoBinds, env), emptyLIE)
231 ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
234 -- foreign export declarations next.
235 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
237 -- Second pass over class and instance declarations,
238 -- to compile the bindings themselves.
239 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
240 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
241 tcRules decls `thenNF_Tc` \ (lie_rules, rules) ->
244 -- Deal with constant or ambiguous InstIds. How could
245 -- there be ambiguous ones? They can only arise if a
246 -- top-level decl falls under the monomorphism
247 -- restriction, and no subsequent decl instantiates its
248 -- type. (Usually, ambiguous type variables are resolved
249 -- during the generalisation step.)
251 lie_alldecls = lie_valdecls `plusLIE`
252 lie_instdecls `plusLIE`
253 lie_clasdecls `plusLIE`
254 lie_fodecls `plusLIE`
257 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
259 -- Check that Main defines main
260 (if mod_name == mAIN_Name then
261 tcLookupValueByKeyMaybe mainKey `thenNF_Tc` \ maybe_main ->
262 checkTc (maybeToBool maybe_main) noMainErr
267 -- Backsubstitution. This must be done last.
268 -- Even tcSimplifyTop may do some unification.
270 all_binds = imp_data_binds `AndMonoBinds`
271 imp_cls_binds `AndMonoBinds`
272 val_binds `AndMonoBinds`
273 inst_binds `AndMonoBinds`
274 cls_dm_binds `AndMonoBinds`
275 const_inst_binds `AndMonoBinds`
278 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
279 tcSetValueEnv really_final_env $
280 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
281 zonkRules rules `thenNF_Tc` \ rules' ->
283 returnTc (really_final_env,
284 (TcResults { tc_binds = all_binds',
285 tc_tycons = local_tycons,
286 tc_classes = local_classes,
287 tc_insts = inst_info,
288 tc_fords = foi_decls ++ foe_decls',
290 tc_env = really_final_env
293 -- End of outer fix loop
294 ) `thenTc` \ (final_env, stuff) ->
297 get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
303 = hsep [ptext SLIT("Module"), quotes (pprModuleName mAIN_Name),
304 ptext SLIT("must include a definition for"), quotes (ptext SLIT("main"))]