2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcModule]{Typechecking a whole module}
7 #include "HsVersions.h"
12 SYN_IE(TcResultBinds),
14 SYN_IE(TcSpecialiseRequests),
20 import HsSyn ( HsModule(..), HsBinds(..), Bind, HsExpr,
21 TyDecl, SpecDataSig, ClassDecl, InstDecl,
22 SpecInstSig, DefaultDecl, Sig, Fake, InPat,
23 FixityDecl, IE, ImportDecl
25 import RnHsSyn ( SYN_IE(RenamedHsModule), RenamedFixityDecl(..) )
26 import TcHsSyn ( SYN_IE(TypecheckedHsBinds), SYN_IE(TypecheckedHsExpr),
27 TcIdOcc(..), zonkBinds, zonkDictBinds )
29 import TcMonad hiding ( rnMtoTcM )
30 import Inst ( Inst, plusLIE )
31 import TcBinds ( tcBindsAndThen )
32 import TcClassDcl ( tcClassDecls2 )
33 import TcDefaults ( tcDefaults )
34 import TcEnv ( tcExtendGlobalValEnv, getEnv_LocalIds,
35 getEnv_TyCons, getEnv_Classes,
36 tcLookupLocalValueByKey, tcLookupTyConByKey )
37 import SpecEnv ( SpecEnv )
38 import TcIfaceSig ( tcInterfaceSigs )
39 import TcInstDcls ( tcInstDecls1, tcInstDecls2 )
40 import TcInstUtil ( buildInstanceEnvs, InstInfo )
41 import TcSimplify ( tcSimplifyTop )
42 import TcTyClsDecls ( tcTyAndClassDecls1 )
43 import TcTyDecls ( mkDataBinds )
45 import Bag ( listToBag )
46 import Class ( GenClass, classSelIds )
47 import ErrUtils ( SYN_IE(Warning), SYN_IE(Error) )
48 import Id ( idType, isMethodSelId, isTopLevId, GenId, SYN_IE(IdEnv), nullIdEnv )
49 import Maybes ( catMaybes )
50 import Name ( isLocallyDefined )
52 import RnUtils ( SYN_IE(RnEnv) )
53 import TyCon ( TyCon )
54 import Type ( applyTyCon )
55 import TysWiredIn ( unitTy, mkPrimIoTy )
56 import TyVar ( SYN_IE(TyVarEnv), nullTyVarEnv )
57 import Unify ( unifyTauTy )
58 import UniqFM ( lookupUFM_Directly, lookupWithDefaultUFM_Directly,
60 import Unique ( iOTyConKey )
63 import FiniteMap ( emptyFM, FiniteMap )
67 Outside-world interface:
69 -- Convenient type synonyms first:
77 = (TypecheckedHsBinds, -- record selector binds
78 TypecheckedHsBinds, -- binds from class decls; does NOT
79 -- include default-methods bindings
80 TypecheckedHsBinds, -- binds from instance decls; INCLUDES
81 -- class default-methods binds
82 TypecheckedHsBinds, -- binds from value decls
84 [(Id, TypecheckedHsExpr)]) -- constant instance binds
86 type TcIfaceInfo -- things for the interface generator
87 = ([Id], [TyCon], [Class], Bag InstInfo)
89 type TcSpecialiseRequests
90 = FiniteMap TyCon [(Bool, [Maybe Type])]
91 -- source tycon specialisation requests
99 -> RnEnv -- for renaming derivings
102 (TcResults, -- if all goes well...
103 Bag Warning) -- (we can still get warnings)
104 (Bag Error, -- if we had errors...
107 typecheckModule us rn_env mod
108 = initTc us (tcModule rn_env mod)
111 The internal monster:
113 tcModule :: RnEnv -- for renaming derivings
114 -> RenamedHsModule -- input
115 -> TcM s TcResults -- output
118 (HsModule mod_name verion exports imports fixities
119 ty_decls specdata_sigs cls_decls inst_decls specinst_sigs
120 default_decls val_decls sigs src_loc)
122 = ASSERT(null imports)
124 tcAddSrcLoc src_loc $ -- record where we're starting
126 -- Tie the knot for inteface-file value declaration signatures
127 -- This info is only used inside the knot for type-checking the
128 -- pragmas, which is done lazily [ie failure just drops the pragma
129 -- without having any global-failure effect].
133 fixTc (\ ~(_, _, _, _, _, _, sig_ids) ->
136 tcExtendGlobalValEnv sig_ids (
138 -- The knot for instance information. This isn't used at all
139 -- till we type-check value declarations
140 fixTc ( \ ~(rec_inst_mapper, _, _, _, _) ->
142 -- Type-check the type and class decls
143 --trace "tcTyAndClassDecls:" $
144 tcTyAndClassDecls1 rec_inst_mapper ty_decls_bag cls_decls_bag
148 -- Typecheck the instance decls, includes deriving
150 --trace "tcInstDecls:" $
151 tcInstDecls1 inst_decls_bag specinst_sigs
152 mod_name rn_env fixities
153 ) `thenTc` \ (inst_info, deriv_binds, ddump_deriv) ->
156 buildInstanceEnvs inst_info `thenTc` \ inst_mapper ->
158 returnTc (inst_mapper, env, inst_info, deriv_binds, ddump_deriv)
160 ) `thenTc` \ (_, env, inst_info, deriv_binds, ddump_deriv) ->
165 -- Default declarations
166 tcDefaults default_decls `thenTc` \ defaulting_tys ->
167 tcSetDefaultTys defaulting_tys ( -- for the iface sigs...
169 -- Create any necessary record selector Ids and their bindings
170 -- "Necessary" includes data and newtype declarations
172 tycons = getEnv_TyCons env
173 classes = getEnv_Classes env
175 mkDataBinds tycons `thenTc` \ (data_ids, data_binds) ->
177 -- Extend the global value environment with
179 -- b) record selectors
180 -- c) class op selectors
181 tcExtendGlobalValEnv data_ids $
182 tcExtendGlobalValEnv (concat (map classSelIds classes)) $
184 -- Interface type signatures
185 -- We tie a knot so that the Ids read out of interfaces are in scope
186 -- when we read their pragmas.
187 -- What we rely on is that pragmas are typechecked lazily; if
188 -- any type errors are found (ie there's an inconsistency)
189 -- we silently discard the pragma
190 tcInterfaceSigs sigs `thenTc` \ sig_ids ->
191 tcGetEnv `thenNF_Tc` \ env ->
194 returnTc (env, inst_info, data_binds, deriv_binds, ddump_deriv, defaulting_tys, sig_ids)
196 )))) `thenTc` \ (env, inst_info, data_binds, deriv_binds, ddump_deriv, defaulting_tys, _) ->
199 tcSetEnv env ( -- to the end...
200 tcSetDefaultTys defaulting_tys ( -- ditto
202 -- Value declarations next.
203 -- We also typecheck any extra binds that came out of the "deriving" process
206 (\ binds1 (binds2, thing) -> (binds1 `ThenBinds` binds2, thing))
207 (val_decls `ThenBinds` deriv_binds)
208 ( -- Second pass over instance declarations,
209 -- to compile the bindings themselves.
211 tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
212 tcClassDecls2 cls_decls_bag `thenNF_Tc` \ (lie_clasdecls, cls_binds) ->
213 tcGetEnv `thenNF_Tc` \ env ->
214 returnTc ( (EmptyBinds, (inst_binds, cls_binds, env)),
215 lie_instdecls `plusLIE` lie_clasdecls,
218 `thenTc` \ ((val_binds, (inst_binds, cls_binds, final_env)), lie_alldecls, _) ->
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.)
227 tcSimplifyTop lie_alldecls `thenTc` \ const_insts ->
229 -- Backsubstitution. Monomorphic top-level decls may have
230 -- been instantiated by subsequent decls, and the final
231 -- simplification step may have instantiated some
232 -- ambiguous types. So, sadly, we need to back-substitute
233 -- over the whole bunch of bindings.
235 -- More horrible still, we have to do it in a careful order, so that
236 -- all the TcIds are in scope when we come across them.
238 -- These bindings ought really to be bundled together in a huge
239 -- recursive group, but HsSyn doesn't have recursion among Binds, only
240 -- among MonoBinds. Sigh again.
241 zonkDictBinds nullTyVarEnv nullIdEnv const_insts `thenNF_Tc` \ (const_insts', ve1) ->
242 zonkBinds nullTyVarEnv ve1 val_binds `thenNF_Tc` \ (val_binds', ve2) ->
244 zonkBinds nullTyVarEnv ve2 data_binds `thenNF_Tc` \ (data_binds', _) ->
245 zonkBinds nullTyVarEnv ve2 inst_binds `thenNF_Tc` \ (inst_binds', _) ->
246 zonkBinds nullTyVarEnv ve2 cls_binds `thenNF_Tc` \ (cls_binds', _) ->
249 localids = getEnv_LocalIds final_env
250 tycons = getEnv_TyCons final_env
251 classes = getEnv_Classes final_env
253 local_tycons = filter isLocallyDefined tycons
254 local_classes = filter isLocallyDefined classes
255 local_vals = [ v | v <- eltsUFM ve2, isLocallyDefined v && isTopLevId v ]
256 -- the isTopLevId is doubtful...
260 (data_binds', cls_binds', inst_binds', val_binds', const_insts'),
262 -- the next collection is just for mkInterface
263 (local_vals, local_tycons, local_classes, inst_info),
270 ty_decls_bag = listToBag ty_decls
271 cls_decls_bag = listToBag cls_decls
272 inst_decls_bag = listToBag inst_decls