- fixTc (\ ~(rec_gve_sigs, _, _, _, _, _, _, _, _, _) ->
- let
- e2 = plusE_GVE e1 rec_gve_sigs
- in
-
- -- The knot for instance information. This isn't used at all
- -- till we type-check value declarations.
- fixTc ( \ ~(rec_inst_mapper, _, _, _, _, _, _, _, _) ->
-
- -- The knot for TyCons and Classes
- fixTc ( \ ~(_, rec_tce, rec_ce, rec_datacons_gve, rec_ops_gve, _, _) ->
- let
- e3 = e2
- `plusE_GVE` rec_datacons_gve
- `plusE_GVE` rec_ops_gve
- `plusE_TCE` rec_tce
- `plusE_CE` rec_ce
- in
- -- DO THE TYPE DECLS
- -- Including the pragmas: {-# ABSTRACT TypeSyn #-}
- -- {-# SPECIALIZE data DataType ... #-}
- let
- (absty_sigs, specdata_sigs) = partition is_absty_sig ty_sigs
- is_absty_sig (AbstractTypeSig _ _) = True
- is_absty_sig (SpecDataSig _ _ _) = False
-
- is_abs_syn :: Name -> Bool -- a lookup fn for abs synonyms
- is_abs_syn n
- = n `is_elem` [ tc | (AbstractTypeSig tc _) <- absty_sigs ]
- where
- is_elem = isIn "tcModule"
-
- get_spec_sigs :: Name -> [RenamedDataTypeSig]
- get_spec_sigs n
- = [ sig | sig@(SpecDataSig tc _ _) <- specdata_sigs, n == tc]
- in
- babyTcMtoTcM (tcTyDecls e3 is_abs_syn get_spec_sigs tydecls)
- `thenTc` \ (tce, datacons_gve, tycon_specs) ->
-
- -- DO THE CLASS DECLS
- tcClassDecls1 e3 rec_inst_mapper classdecls
- `thenTc` \ (class_info, ce, ops_gve) ->
-
- -- End of TyCon/Class knot
- -- Augment whatever TCE/GVE/CE stuff was in orig_e
- returnTc (e3, tce, ce, datacons_gve, ops_gve, class_info, tycon_specs)
-
- -- End of inner fixTc
- ) `thenTc` ( \ (e3, tce_here, ce_here, _, _, class_info, tycon_specs) ->
- -- The "here" things are the extra decls defined in this
- -- module or its imports; but not including whatever was
- -- in the incoming e.
-
- -- Grab completed tce/ce and check for type/class cycles
- -- The tce/ce are now stable and lookable-at, with the
- -- exception of the instance information inside classes
- let
- ce3 = getE_CE e3
- tce3 = getE_TCE e3
- in
- checkMaybeErrTc (checkTypeCycles tce3) id `thenTc_`
- checkMaybeErrTc (checkClassCycles ce3) id `thenTc_`
-
- -- Now instance declarations
- tcInstDecls1 e3 ce3 tce3 instdecls `thenNF_Tc` \ decl_inst_info ->
-
- -- Handle "derived" instances; note that we only do derivings
- -- for things in this module; we ignore deriving decls from
- -- interfaces! We pass fixities, because they may be used in
- -- doing Text.
-
- tcDeriving mod_name renamer_name_funs decl_inst_info tce3 fixities
- `thenTc` \ (deriv_inst_info, extra_deriv_binds, ddump_deriv) ->
-
- let
- inst_info = deriv_inst_info `unionBags` decl_inst_info
- in
- -- Handle specialise instance pragmas
--- getSwitchCheckerTc `thenNF_Tc` \ sw_chkr ->
--- (if sw_chkr GlasgowExts then
- tcSpecInstSigs e3 ce3 tce3 inst_info specinst_sigs
--- else
--- returnTc emptyBag)
- `thenTc` \ spec_inst_info ->
- let
- full_inst_info = inst_info `unionBags` spec_inst_info
- in
- -- OK, now do the inst-mapper stuff
- buildInstanceEnvs full_inst_info `thenTc` \ all_insts_mapper ->
-
- returnTc (all_insts_mapper, e3, ce_here, tce_here, class_info, tycon_specs,
- full_inst_info, extra_deriv_binds, ddump_deriv)
-
- -- End of outer fixTc
- )) `thenTc` ( \ (_, e3, ce_here, tce_here, class_info, tycon_specs,
- full_inst_info, extra_deriv_binds, ddump_deriv) ->
-
- -- Default declarations
- tcDefaults e3 default_decls `thenTc` \ defaulting_tys ->
- setDefaultingTys defaulting_tys ( -- for the iface sigs...
-
- -- 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
-
- babyTcMtoTcM (tcInterfaceSigs e3 sigs) `thenTc` \ gve_sigs ->
-
- returnTc (gve_sigs, e3, ce_here, tce_here, class_info, tycon_specs, defaulting_tys,
- full_inst_info, extra_deriv_binds, ddump_deriv)
-
- -- End of extremely outer fixTc
- ))) `thenTc` \ (_, e3, ce_here, tce_here, class_info, tycon_specs, defaulting_tys,
- full_inst_info, extra_deriv_binds, ddump_deriv) ->
-
- setDefaultingTys defaulting_tys ( -- to the end...
+-- Convenient type synonyms first:
+data TcResults
+ = TcResults {
+ tc_binds :: TypecheckedMonoBinds,
+ tc_tycons :: [TyCon],
+ tc_classes :: [Class],
+ tc_insts :: Bag InstInfo, -- Instance declaration information
+ tc_fords :: [TypecheckedForeignDecl], -- Foreign import & exports.
+ tc_rules :: [TypecheckedRuleDecl], -- Transformation rules
+ tc_env :: ValueEnv,
+ tc_thinair :: [Id] -- The thin-air Ids
+ }
+
+---------------
+typecheckModule
+ :: UniqSupply
+ -> RnNameSupply
+ -> InterfaceDetails
+ -> RenamedHsModule
+ -> IO (Maybe TcResults)
+
+typecheckModule us rn_name_supply iface_det mod
+ = initTc us initEnv (tcModule rn_name_supply (getIfaceFixities iface_det) mod)
+ >>= \ (maybe_result, warns, errs) ->
+
+ printErrorsAndWarnings errs warns >>
+
+ -- write the thin-air Id map
+ (case maybe_result of
+ Just results -> setThinAirIds (tc_thinair results)
+ Nothing -> return ()
+ ) >>
+
+ dumpIfSet opt_D_dump_tc "Typechecked"
+ (case maybe_result of
+ Just results -> ppr (tc_binds results)
+ $$
+ pp_rules (tc_rules results)
+ Nothing -> text "Typecheck failed") >>
+
+ return (if isEmptyBag errs then
+ maybe_result
+ else
+ Nothing)
+
+pp_rules [] = empty
+pp_rules rs = vcat [ptext SLIT("{-# RULES"),
+ nest 4 (vcat (map ppr rs)),
+ ptext SLIT("#-}")]
+\end{code}
+
+The internal monster:
+\begin{code}
+tcModule :: RnNameSupply -- for renaming derivings
+ -> Fixities -- needed for Show/Read derivings.
+ -> RenamedHsModule -- input
+ -> TcM s TcResults -- output
+
+tcModule rn_name_supply fixities
+ (HsModule mod_name verion exports imports decls _ src_loc)
+ = tcAddSrcLoc src_loc $ -- record where we're starting
+
+ fixTc (\ ~(unf_env ,_) ->
+ -- unf_env is used for type-checking interface pragmas
+ -- which is done lazily [ie failure just drops the pragma
+ -- without having any global-failure effect].
+ --
+ -- unf_env is also used to get the pragam info
+ -- for imported dfuns and default methods
+
+ -- The knot for instance information. This isn't used at all
+ -- till we type-check value declarations
+ fixTc ( \ ~(rec_inst_mapper, _, _, _) ->
+
+ -- Type-check the type and class decls
+ tcTyAndClassDecls unf_env rec_inst_mapper decls `thenTc` \ env ->
+
+ -- Typecheck the instance decls, includes deriving
+ tcSetEnv env (
+ tcInstDecls1 unf_env decls mod_name fixities rn_name_supply
+ ) `thenTc` \ (inst_info, deriv_binds) ->
+
+ buildInstanceEnvs inst_info `thenNF_Tc` \ inst_mapper ->
+
+ returnTc (inst_mapper, env, inst_info, deriv_binds)
+
+ -- End of inner fix loop
+ ) `thenTc` \ (_, env, inst_info, deriv_binds) ->
+
+ 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
+ mkImplicitDataBinds tycons `thenTc` \ (data_ids, imp_data_binds) ->
+ mkImplicitClassBinds classes `thenNF_Tc` \ (cls_ids, imp_cls_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 cls_ids $
+
+ -- Extend the TyCon envt with the tycons corresponding to
+ -- the classes.
+ -- They are mentioned in types in interface files.
+ 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 $