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 tcSetValueEnv, tcSetInstEnv,
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, TcKind )
44 import RnMonad ( RnNameSupply, FixityEnv )
45 import Bag ( isEmptyBag )
46 import ErrUtils ( Message, printErrorsAndWarnings, dumpIfSet )
47 import Id ( Id, idType, idName )
48 import Module ( pprModuleName )
49 import OccName ( isSysOcc )
50 import Name ( Name, nameUnique, nameOccName, isLocallyDefined,
51 toRdrName, nameEnvElts, NamedThing(..)
53 import TyCon ( TyCon, tyConKind )
54 import Class ( Class, classSelIds, classTyCon )
55 import PrelInfo ( mAIN_Name )
56 import Unique ( Unique, mainKey )
57 import UniqSupply ( UniqSupply )
58 import Maybes ( maybeToBool )
60 import Bag ( Bag, isEmptyBag )
64 Outside-world interface:
67 -- Convenient type synonyms first:
70 tc_binds :: TypecheckedMonoBinds,
72 tc_classes :: [Class],
73 tc_insts :: Bag InstInfo, -- Instance declaration information
74 tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
75 tc_rules :: [TypecheckedRuleDecl], -- Transformation rules
85 -> IO (Maybe TcResults)
87 typecheckModule us rn_name_supply fixity_env mod
88 = initTc us initEnv (tcModule rn_name_supply fixity_env mod) >>= \ (maybe_result, warns, errs) ->
90 printErrorsAndWarnings errs warns >>
94 Just results -> dumpIfSet opt_D_dump_types "Type signatures" (dump_sigs results) >>
95 dumpIfSet opt_D_dump_tc "Typechecked" (dump_tc results)
98 return (if isEmptyBag errs then
104 = ppr (tc_binds results) $$ pp_rules (tc_rules results)
106 dump_sigs results -- Print type signatures
107 = -- Convert to HsType so that we get source-language style printing
108 -- And sort by RdrName
109 vcat $ map ppr_sig $ sortLt lt_sig $
110 [(toRdrName id, toHsType (idType id)) | id <- nameEnvElts (tc_env results),
114 lt_sig (n1,_) (n2,_) = n1 < n2
115 ppr_sig (n,t) = ppr n <+> dcolon <+> ppr t
117 want_sig id | opt_PprStyle_Debug = True
118 | otherwise = isLocallyDefined n && not (isSysOcc (nameOccName n))
123 pp_rules rs = vcat [ptext SLIT("{-# RULES"),
124 nest 4 (vcat (map ppr rs)),
128 The internal monster:
130 tcModule :: RnNameSupply -- for renaming derivings
131 -> FixityEnv -- needed for Show/Read derivings.
132 -> RenamedHsModule -- input
133 -> TcM s TcResults -- output
135 tcModule rn_name_supply fixities
136 (HsModule mod_name _ _ _ decls _ src_loc)
137 = tcAddSrcLoc src_loc $ -- record where we're starting
139 fixTc (\ ~(unf_env ,_) ->
140 -- unf_env is used for type-checking interface pragmas
141 -- which is done lazily [ie failure just drops the pragma
142 -- without having any global-failure effect].
144 -- unf_env is also used to get the pragam info
145 -- for imported dfuns and default methods
147 -- Type-check the type and class decls
148 tcTyAndClassDecls unf_env decls `thenTc` \ env ->
150 -- Typecheck the instance decls, includes deriving
153 tcInstDecls1 unf_env decls
155 rn_name_supply `thenTc` \ (inst_info, deriv_binds) ->
157 buildInstanceEnv inst_info `thenNF_Tc` \ inst_env ->
159 tcSetInstEnv inst_env $
161 tycons = getEnvTyCons env
162 classes = getEnvClasses env
163 local_tycons = filter isLocallyDefined tycons
164 local_classes = filter isLocallyDefined classes
167 -- Default declarations
168 tcDefaults decls `thenTc` \ defaulting_tys ->
169 tcSetDefaultTys defaulting_tys $
171 -- Interface type signatures
172 -- We tie a knot so that the Ids read out of interfaces are in scope
173 -- when we read their pragmas.
174 -- What we rely on is that pragmas are typechecked lazily; if
175 -- any type errors are found (ie there's an inconsistency)
176 -- we silently discard the pragma
177 -- We must do this before mkImplicitDataBinds (which comes next), since
178 -- the latter looks up unpackCStringId, for example, which is usually
180 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
181 tcExtendGlobalValEnv sig_ids $
183 -- Create any necessary record selector Ids and their bindings
184 -- "Necessary" includes data and newtype declarations
185 -- We don't create bindings for dictionary constructors;
186 -- they are always fully applied, and the bindings are just there
187 -- to support partial applications
188 mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
189 mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_binds) ->
191 -- Extend the global value environment with
193 -- (b) record selectors
194 -- (c) class op selectors
195 -- (d) default-method ids... where? I can't see where these are
196 -- put into the envt, and I'm worried that the zonking phase
197 -- will find they aren't there and complain.
198 tcExtendGlobalValEnv data_ids $
199 tcExtendGlobalValEnv cls_ids $
201 -- foreign import declarations next.
202 tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
203 tcExtendGlobalValEnv fo_ids $
205 -- Value declarations next.
206 -- We also typecheck any extra binds that came out of the "deriving" process
208 (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
209 (get_val_decls decls `ThenBinds` deriv_binds)
210 ( tcGetEnv `thenNF_Tc` \ env ->
211 tcGetUnique `thenNF_Tc` \ uniq ->
212 returnTc ((EmptyMonoBinds, env), emptyLIE)
213 ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
216 -- foreign export declarations next.
217 tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
219 -- Second pass over class and instance declarations,
220 -- to compile the bindings themselves.
221 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
222 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
223 tcRules decls `thenNF_Tc` \ (lie_rules, rules) ->
226 -- Deal with constant or ambiguous InstIds. How could
227 -- there be ambiguous ones? They can only arise if a
228 -- top-level decl falls under the monomorphism
229 -- restriction, and no subsequent decl instantiates its
230 -- type. (Usually, ambiguous type variables are resolved
231 -- during the generalisation step.)
233 lie_alldecls = lie_valdecls `plusLIE`
234 lie_instdecls `plusLIE`
235 lie_clasdecls `plusLIE`
236 lie_fodecls `plusLIE`
239 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
241 -- Check that Main defines main
242 (if mod_name == mAIN_Name then
243 tcLookupValueByKeyMaybe mainKey `thenNF_Tc` \ maybe_main ->
244 checkTc (maybeToBool maybe_main) noMainErr
249 -- Backsubstitution. This must be done last.
250 -- Even tcSimplifyTop may do some unification.
252 all_binds = imp_data_binds `AndMonoBinds`
253 imp_cls_binds `AndMonoBinds`
254 val_binds `AndMonoBinds`
255 inst_binds `AndMonoBinds`
256 cls_dm_binds `AndMonoBinds`
257 const_inst_binds `AndMonoBinds`
260 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
261 tcSetValueEnv really_final_env $
262 zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
263 zonkRules rules `thenNF_Tc` \ rules' ->
265 returnTc (really_final_env,
266 (TcResults { tc_binds = all_binds',
267 tc_tycons = local_tycons,
268 tc_classes = local_classes,
269 tc_insts = inst_info,
270 tc_fords = foi_decls ++ foe_decls',
272 tc_env = really_final_env
275 -- End of outer fix loop
276 ) `thenTc` \ (final_env, stuff) ->
279 get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
285 = hsep [ptext SLIT("Module"), quotes (pprModuleName mAIN_Name),
286 ptext SLIT("must include a definition for"), quotes (ptext SLIT("main"))]