-tcDefMeth :: SkolemInfo -> Class -> [TyVar] -> ThetaType -> Inst -> LHsBinds Name
- -> TcSigFun -> TcPragFun -> Id
- -> TcM (LHsBinds Id, Id)
-tcDefMeth rigid_info clas tyvars theta this_dict binds_in sig_fn prag_fn sel_id
- = do { let sel_name = idName sel_id
- ; local_dm_name <- newLocalName sel_name
- ; let meth_bind = findMethodBind sel_name local_dm_name binds_in
- `orElse` pprPanic "tcDefMeth" (ppr sel_id)
- -- We only call tcDefMeth on selectors for which
- -- there is a binding in binds_in
-
- meth_sig_fn _ = sig_fn sel_name
- meth_prag_fn _ = prag_fn sel_name
-
- ; (top_dm_id, bind) <- tcInstanceMethodBody rigid_info
- clas tyvars [this_dict] theta (mkTyVarTys tyvars)
- Nothing sel_id
- local_dm_name
- meth_sig_fn meth_prag_fn
- meth_bind
-
- ; return (bind, top_dm_id) }
-
-mkDefMethRdrName :: Name -> RdrName
-mkDefMethRdrName sel_name = mkDerivedRdrName sel_name mkDefaultMethodOcc
+tcDefMeth :: Class -> [TyVar] -> EvVar -> LHsBinds Name -> [LSig Name]
+ -> SigFun -> PragFun -> ClassOpItem
+ -> TcM (LHsBinds TcId)
+-- Generate code for polymorphic default methods only (hence DefMeth)
+-- (Generic default methods have turned into instance decls by now.)
+-- This is incompatible with Hugs, which expects a polymorphic
+-- default method for every class op, regardless of whether or not
+-- the programmer supplied an explicit default decl for the class.
+-- (If necessary we can fix that, but we don't have a convenient Id to hand.)
+tcDefMeth _ tyvars _ binds_in sigs sig_fn prag_fn (sel_id, dm_info)
+ | NoDefMeth <- dm_info = return emptyBag
+ | otherwise
+ = do { (dm_id, tvs, sig_loc) <- tc_dm_id dm_info
+ ; let L loc meth_bind = findMethodBind sel_name binds_in
+ `orElse` pprPanic "tcDefMeth" (ppr sel_id)
+ dm_bind = L loc (meth_bind { fun_id = L loc (idName dm_id) })
+ -- Substitute the meth_name for the binder
+ -- NB: the binding is always a FunBind
+
+ dm_sig_fn _ = Just (clas_tv_names ++ tvs, sig_loc)
+ dm_prag_fn _ = prag_fn sel_name
+
+ ; (binds,_) <- tcExtendIdEnv [dm_id] $
+ tcPolyBinds TopLevel dm_sig_fn dm_prag_fn
+ NonRecursive NonRecursive
+ [dm_bind]
+ ; return binds }
+ where
+ sel_name = idName sel_id
+ clas_tv_names = map getName tyvars
+
+ -- Find the 'generic op :: ty' signature among the sigs
+ -- If dm_info is GenDefMeth, the corresponding signature
+ -- should jolly well exist! Hence the panic
+ genop_lhs_ty = case [lty | L _ (GenericSig (L _ n) lty) <- sigs
+ , n == sel_name ] of
+ [lty] -> lty
+ _ -> pprPanic "tcDefMeth" (ppr sel_name $$ ppr sigs)
+
+ tc_dm_id :: DefMeth -> TcM (Id, [Name], SrcSpan)
+ -- Make a default-method Id of the appropriate type
+ -- That may entail getting the generic-default signature
+ -- from the type signatures.
+ -- Also return the in-scope tyvars for the default method, and their binding site
+ tc_dm_id NoDefMeth = panic "tc_dm_id"
+ tc_dm_id (DefMeth dm_name)
+ | Just (tvs, loc) <- sig_fn sel_name
+ = return (mkDefaultMethodId sel_id dm_name, tvs, loc)
+ | otherwise
+ = pprPanic "No sig for" (ppr sel_name)
+ tc_dm_id (GenDefMeth dm_name)
+ = setSrcSpan loc $
+ do { tau <- tcHsKindedType genop_lhs_ty
+ ; checkValidType (FunSigCtxt sel_name) tau
+ ; return ( mkExportedLocalId dm_name (mkForAllTys tyvars tau)
+ , hsExplicitTvs genop_lhs_ty, loc ) }
+ where
+ loc = getLoc genop_lhs_ty
+
+---------------
+tcInstanceMethodBody :: SkolemInfo -> [TcTyVar] -> [EvVar]
+ -> Id -> Id
+ -> SigFun -> TcSpecPrags -> LHsBind Name
+ -> TcM (LHsBind Id)
+tcInstanceMethodBody skol_info tyvars dfun_ev_vars
+ meth_id local_meth_id
+ meth_sig_fn specs
+ (L loc bind)
+ = do { -- Typecheck the binding, first extending the envt
+ -- so that when tcInstSig looks up the local_meth_id to find
+ -- its signature, we'll find it in the environment
+ let lm_bind = L loc (bind { fun_id = L loc (idName local_meth_id) })
+ -- Substitute the local_meth_name for the binder
+ -- NB: the binding is always a FunBind
+ ; traceTc "TIM" (ppr local_meth_id $$ ppr (meth_sig_fn (idName local_meth_id)))
+ ; (ev_binds, (tc_bind, _))
+ <- checkConstraints skol_info tyvars dfun_ev_vars $
+ tcExtendIdEnv [local_meth_id] $
+ tcPolyBinds TopLevel meth_sig_fn no_prag_fn
+ NonRecursive NonRecursive
+ [lm_bind]
+
+ ; let full_bind = AbsBinds { abs_tvs = tyvars, abs_ev_vars = dfun_ev_vars
+ , abs_exports = [(tyvars, meth_id, local_meth_id, specs)]
+ , abs_ev_binds = ev_binds
+ , abs_binds = tc_bind }
+
+ ; return (L loc full_bind) }
+ where
+ no_prag_fn _ = [] -- No pragmas for local_meth_id;
+ -- they are all for meth_id
+\end{code}
+
+\begin{code}
+instantiateMethod :: Class -> Id -> [TcType] -> TcType
+-- Take a class operation, say
+-- op :: forall ab. C a => forall c. Ix c => (b,c) -> a
+-- Instantiate it at [ty1,ty2]
+-- Return the "local method type":
+-- forall c. Ix x => (ty2,c) -> ty1
+instantiateMethod clas sel_id inst_tys
+ = ASSERT( ok_first_pred ) local_meth_ty
+ where
+ (sel_tyvars,sel_rho) = tcSplitForAllTys (idType sel_id)
+ rho_ty = ASSERT( length sel_tyvars == length inst_tys )
+ substTyWith sel_tyvars inst_tys sel_rho
+
+ (first_pred, local_meth_ty) = tcSplitPredFunTy_maybe rho_ty
+ `orElse` pprPanic "tcInstanceMethod" (ppr sel_id)
+
+ ok_first_pred = case getClassPredTys_maybe first_pred of
+ Just (clas1, _tys) -> clas == clas1
+ Nothing -> False
+ -- The first predicate should be of form (C a b)
+ -- where C is the class in question
+