2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcModule]{Typechecking a whole module}
13 #include "HsVersions.h"
15 import CmdLineOpts ( opt_D_dump_tc, opt_D_dump_deriv )
16 import HsSyn ( HsModule(..), HsBinds(..), MonoBinds(..), HsDecl(..) )
17 import RnHsSyn ( RenamedHsModule )
18 import TcHsSyn ( TcMonoBinds, TypecheckedMonoBinds, zonkTopBinds )
21 import Inst ( Inst, emptyLIE, plusLIE )
22 import TcBinds ( tcTopBindsAndThen )
23 import TcClassDcl ( tcClassDecls2 )
24 import TcDefaults ( tcDefaults )
25 import TcEnv ( TcIdOcc(..), tcExtendGlobalValEnv, tcExtendTyConEnv,
26 getEnv_TyCons, getEnv_Classes, tcLookupLocalValue,
27 tcLookupTyCon, initEnv )
28 import TcExpr ( tcId )
29 import TcIfaceSig ( tcInterfaceSigs )
30 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
31 import TcInstUtil ( buildInstanceEnvs, classDataCon, InstInfo )
32 import TcSimplify ( tcSimplifyTop )
33 import TcTyClsDecls ( tcTyAndClassDecls1 )
34 import TcTyDecls ( mkDataBinds )
35 import TcType ( TcType, tcInstType )
36 import TcKind ( TcKind, kindToTcKind )
38 import RnMonad ( RnNameSupply(..) )
39 import Bag ( isEmptyBag )
40 import ErrUtils ( WarnMsg, ErrMsg,
41 pprBagOfErrors, dumpIfSet
43 import Id ( idType, GenId )
44 import Name ( Name, isLocallyDefined, pprModule, NamedThing(..) )
45 import TyCon ( TyCon, tyConKind )
46 import Class ( Class, classSelIds, classTyCon )
47 import Type ( mkTyConApp, Type )
48 import TyVar ( emptyTyVarEnv )
49 import TysWiredIn ( unitTy )
50 import PrelMods ( mAIN )
51 import PrelInfo ( main_NAME, ioTyCon_NAME )
52 import Unify ( unifyTauTy )
53 import Unique ( Unique )
54 import UniqSupply ( UniqSupply )
56 import Bag ( Bag, isEmptyBag )
57 import FiniteMap ( FiniteMap )
61 Outside-world interface:
64 -- Convenient type synonyms first:
66 = (TypecheckedMonoBinds,
68 Bag InstInfo, -- Instance declaration information
71 type TcDDumpDeriv = SDoc
78 -> IO (Maybe TcResults)
80 typecheckModule us rn_name_supply mod
82 (maybe_result, warns, errs) =
83 initTc us initEnv (tcModule rn_name_supply mod)
88 dumpIfSet opt_D_dump_tc "Typechecked"
90 Just (binds, _, _, _, _) -> ppr binds
91 Nothing -> text "Typecheck failed") >>
93 dumpIfSet opt_D_dump_deriv "Derived instances"
95 Just (_, _, _, _, dump_deriv) -> dump_deriv
98 return (if isEmptyBag errs then
104 | isEmptyBag errs = return ()
105 | otherwise = printErrs (pprBagOfErrors errs)
108 The internal monster:
110 tcModule :: RnNameSupply -- for renaming derivings
111 -> RenamedHsModule -- input
112 -> TcM s TcResults -- output
114 tcModule rn_name_supply
115 (HsModule mod_name verion exports imports fixities 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 -- The knot for instance information. This isn't used at all
127 -- till we type-check value declarations
128 fixTc ( \ ~(rec_inst_mapper, _, _, _, _) ->
130 -- Type-check the type and class decls
131 -- trace "tcTyAndClassDecls:" $
132 tcTyAndClassDecls1 unf_env rec_inst_mapper decls `thenTc` \ env ->
135 -- Typecheck the instance decls, includes deriving
137 -- trace "tcInstDecls:" $
138 tcInstDecls1 unf_env decls mod_name rn_name_supply
139 ) `thenTc` \ (inst_info, deriv_binds, ddump_deriv) ->
142 buildInstanceEnvs inst_info `thenNF_Tc` \ inst_mapper ->
144 returnTc (inst_mapper, env, inst_info, deriv_binds, ddump_deriv)
146 -- End of inner fix loop
147 ) `thenTc` \ (_, env, inst_info, deriv_binds, ddump_deriv) ->
152 -- Default declarations
153 tcDefaults decls `thenTc` \ defaulting_tys ->
154 tcSetDefaultTys defaulting_tys $
156 -- Create any necessary record selector Ids and their bindings
157 -- "Necessary" includes data and newtype declarations
158 -- We don't create bindings for dictionary constructors;
159 -- they are always fully applied, and the bindings are just there
160 -- to support partial applications
162 tycons = getEnv_TyCons env
163 classes = getEnv_Classes env
164 local_tycons = filter isLocallyDefined tycons
165 local_classes = filter isLocallyDefined classes
167 mkDataBinds tycons `thenTc` \ (data_ids, data_binds) ->
169 -- Extend the global value environment with
171 -- (b) record selectors
172 -- (c) class op selectors
173 -- (d) default-method ids
174 tcExtendGlobalValEnv data_ids $
175 tcExtendGlobalValEnv (concat (map classSelIds classes)) $
177 -- Extend the TyCon envt with the tycons corresponding to
178 -- the classes, and the global value environment with the
179 -- corresponding data cons.
180 -- They are mentioned in types in interface files.
181 tcExtendGlobalValEnv (map classDataCon classes) $
182 tcExtendTyConEnv [ (getName tycon, (kindToTcKind (tyConKind tycon), Nothing, tycon))
184 let tycon = classTyCon clas
187 -- Interface type signatures
188 -- We tie a knot so that the Ids read out of interfaces are in scope
189 -- when we read their pragmas.
190 -- What we rely on is that pragmas are typechecked lazily; if
191 -- any type errors are found (ie there's an inconsistency)
192 -- we silently discard the pragma
193 tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
194 tcExtendGlobalValEnv sig_ids $
197 -- Value declarations next.
198 -- We also typecheck any extra binds that came out of the "deriving" process
199 -- trace "tcBinds:" $
201 (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
202 (get_val_decls decls `ThenBinds` deriv_binds)
203 ( tcGetEnv `thenNF_Tc` \ env ->
204 returnTc ((EmptyMonoBinds, env), emptyLIE)
205 ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
209 -- Second pass over class and instance declarations,
210 -- to compile the bindings themselves.
212 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
213 tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_binds) ->
217 -- Check that "main" has the right signature
218 tcCheckMainSig mod_name `thenTc_`
220 -- Deal with constant or ambiguous InstIds. How could
221 -- there be ambiguous ones? They can only arise if a
222 -- top-level decl falls under the monomorphism
223 -- restriction, and no subsequent decl instantiates its
224 -- type. (Usually, ambiguous type variables are resolved
225 -- during the generalisation step.)
228 lie_alldecls = lie_valdecls `plusLIE` lie_instdecls `plusLIE` lie_clasdecls
230 tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
233 -- Backsubstitution. This must be done last.
234 -- Even tcCheckMainSig and tcSimplifyTop may do some unification.
236 all_binds = data_binds `AndMonoBinds`
237 val_binds `AndMonoBinds`
238 inst_binds `AndMonoBinds`
239 cls_binds `AndMonoBinds`
242 zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
244 returnTc (really_final_env,
245 (all_binds', local_tycons, local_classes, inst_info, ddump_deriv))
247 -- End of outer fix loop
248 ) `thenTc` \ (final_env, stuff) ->
251 get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
256 tcCheckMainSig mod_name
258 = returnTc () -- A non-main module
261 = -- Check that main is defined
262 tcLookupTyCon ioTyCon_NAME `thenTc` \ (_,_,ioTyCon) ->
263 tcLookupLocalValue main_NAME `thenNF_Tc` \ maybe_main_id ->
264 case maybe_main_id of {
265 Nothing -> failWithTc noMainErr ;
268 -- Check that it has the right type (or a more general one)
270 expected_ty = mkTyConApp ioTyCon [unitTy]
272 tcInstType emptyTyVarEnv expected_ty `thenNF_Tc` \ expected_tau ->
273 tcId main_NAME `thenNF_Tc` \ (_, lie, main_tau) ->
274 tcSetErrCtxt mainTyCheckCtxt $
275 unifyTauTy expected_tau
277 checkTc (isEmptyBag lie) (mainTyMisMatch expected_ty (idType main_id))
282 = hsep [ptext SLIT("When checking that"), ppr main_NAME, ptext SLIT("has the required type")]
285 = hsep [ptext SLIT("Module"), quotes (pprModule mAIN),
286 ptext SLIT("must include a definition for"), quotes (ppr main_NAME)]
288 mainTyMisMatch :: Type -> TcType s -> ErrMsg
289 mainTyMisMatch expected actual
290 = hang (hsep [ppr main_NAME, ptext SLIT("has the wrong type")])
292 hsep [ptext SLIT("Expected:"), ppr expected],
293 hsep [ptext SLIT("Inferred:"), ppr actual]