- tcSetEnv env (
-
- -- Default declarations
- tcDefaults decls `thenTc` \ defaulting_tys ->
- tcSetDefaultTys defaulting_tys $
-
- -- Create any necessary record selector Ids and their bindings
- -- "Necessary" includes data and newtype declarations
- -- We don't create bindings for dictionary constructors;
- -- they are always fully applied, and the bindings are just there
- -- to support partial applications
- let
- tycons = getEnvTyCons env
- classes = getEnvClasses env
- local_tycons = filter isLocallyDefined tycons
- local_classes = filter isLocallyDefined classes
- in
- mkDataBinds tycons `thenTc` \ (data_ids, data_binds) ->
-
- -- Extend the global value environment with
- -- (a) constructors
- -- (b) record selectors
- -- (c) class op selectors
- -- (d) default-method ids... where? I can't see where these are
- -- put into the envt, and I'm worried that the zonking phase
- -- will find they aren't there and complain.
- tcExtendGlobalValEnv data_ids $
- tcExtendGlobalValEnv (concat (map classSelIds classes)) $
-
- -- Extend the TyCon envt with the tycons corresponding to
- -- the classes, and the global value environment with the
- -- corresponding data cons.
- -- They are mentioned in types in interface files.
- tcExtendGlobalValEnv (map (dataConId . classDataCon) classes) $
- tcExtendTypeEnv [ (getName tycon, (kindToTcKind (tyConKind tycon), Nothing, ATyCon tycon))
- | clas <- classes,
- let tycon = classTyCon clas
- ] $
-
- -- Interface type signatures
- -- We tie a knot so that the Ids read out of interfaces are in scope
- -- when we read their pragmas.
- -- What we rely on is that pragmas are typechecked lazily; if
- -- any type errors are found (ie there's an inconsistency)
- -- we silently discard the pragma
- tcInterfaceSigs unf_env decls `thenTc` \ sig_ids ->
- tcExtendGlobalValEnv sig_ids $
-
- -- foreign import declarations next.
- tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
- tcExtendGlobalValEnv fo_ids $
-
- -- Value declarations next.
- -- We also typecheck any extra binds that came out of the "deriving" process
- tcTopBindsAndThen
- (\ is_rec binds1 (binds2, thing) -> (binds1 `AndMonoBinds` binds2, thing))
- (get_val_decls decls `ThenBinds` deriv_binds)
- ( tcGetEnv `thenNF_Tc` \ env ->
- tcGetUnique `thenNF_Tc` \ uniq ->
- returnTc ((EmptyMonoBinds, env), emptyLIE)
- ) `thenTc` \ ((val_binds, final_env), lie_valdecls) ->
- tcSetEnv final_env $
-
- -- foreign export declarations next.
- tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
-
- -- Second pass over class and instance declarations,
- -- to compile the bindings themselves.
- tcInstDecls2 inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
- tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_binds) ->
-
- -- Check that "main" has the right signature
- tcCheckMainSig mod_name `thenTc_`
-
- -- Deal with constant or ambiguous InstIds. How could
- -- there be ambiguous ones? They can only arise if a
- -- top-level decl falls under the monomorphism
- -- restriction, and no subsequent decl instantiates its
- -- type. (Usually, ambiguous type variables are resolved
- -- during the generalisation step.)
- let
- lie_alldecls = lie_valdecls `plusLIE`
- lie_instdecls `plusLIE`
- lie_clasdecls `plusLIE`
- lie_fodecls
- in
- tcSimplifyTop lie_alldecls `thenTc` \ const_inst_binds ->
-
-
- -- Backsubstitution. This must be done last.
- -- Even tcCheckMainSig and tcSimplifyTop may do some unification.
- let
- all_binds = data_binds `AndMonoBinds`
- val_binds `AndMonoBinds`
- inst_binds `AndMonoBinds`
- cls_binds `AndMonoBinds`
- const_inst_binds `AndMonoBinds`
- foe_binds
- in
- zonkTopBinds all_binds `thenNF_Tc` \ (all_binds', really_final_env) ->
- tcSetValueEnv really_final_env $
- zonkForeignExports foe_decls `thenNF_Tc` \ foe_decls' ->
-
- let
- thin_air_ids = map (explicitLookupValueByKey really_final_env . nameUnique) thinAirIdNames
- -- When looking up the thin-air names we must use
- -- a global env that includes the zonked locally-defined Ids too
- -- Hence using really_final_env
- in
- returnTc (really_final_env,
- (all_binds', local_tycons, local_classes, inst_info,
- (foi_decls ++ foe_decls'),
- really_final_env,
- thin_air_ids))
- )
-
- -- End of outer fix loop
- ) `thenTc` \ (final_env, stuff) ->
- returnTc stuff
-
-get_val_decls decls = foldr ThenBinds EmptyBinds [binds | ValD binds <- decls]
-\end{code}
-
-
-\begin{code}
-tcCheckMainSig mod_name
- | mod_name /= mAIN
- = returnTc () -- A non-main module
-
- | otherwise
- = -- Check that main is defined
- tcLookupTyCon ioTyCon_NAME `thenTc` \ ioTyCon ->
- tcLookupValueMaybe main_NAME `thenNF_Tc` \ maybe_main_id ->
- case maybe_main_id of {
- Nothing -> failWithTc noMainErr ;
- Just main_id ->
-
- -- Check that it has the right type (or a more general one)
- -- As of Haskell 98, anything that unifies with (IO a) is OK.
- newTyVarTy boxedTypeKind `thenNF_Tc` \ t_tv ->
- let
- tv = getTyVar "tcCheckMainSig" t_tv
- expected_tau = typeToTcType ((mkTyConApp ioTyCon [t_tv]))
+ -- Foreign import declarations next
+ tcForeignImports decls `thenTc` \ (fo_ids, foi_decls) ->
+ tcExtendGlobalValEnv fo_ids $
+
+ -- Value declarations next.
+ -- We also typecheck any extra binds that came out of the "deriving" process
+ tcTopBinds (get_binds decls `ThenBinds` deriv_binds) `thenTc` \ ((val_binds, env), lie_valdecls) ->
+ tcSetEnv env $
+
+ -- Foreign export declarations next
+ tcForeignExports decls `thenTc` \ (lie_fodecls, foe_binds, foe_decls) ->
+
+ -- Second pass over class and instance declarations,
+ -- to compile the bindings themselves.
+ tcInstDecls2 local_inst_info `thenNF_Tc` \ (lie_instdecls, inst_binds) ->
+ tcClassDecls2 decls `thenNF_Tc` \ (lie_clasdecls, cls_dm_binds) ->
+ tcRules (pcs_rules pcs) this_mod decls `thenNF_Tc` \ (new_pcs_rules, lie_rules, local_rules) ->
+
+ -- Deal with constant or ambiguous InstIds. How could
+ -- there be ambiguous ones? They can only arise if a
+ -- top-level decl falls under the monomorphism
+ -- restriction, and no subsequent decl instantiates its
+ -- type. (Usually, ambiguous type variables are resolved
+ -- during the generalisation step.)
+ let
+ lie_alldecls = lie_valdecls `plusLIE`
+ lie_instdecls `plusLIE`
+ lie_clasdecls `plusLIE`
+ lie_fodecls `plusLIE`
+ lie_rules