- -- The *caller* put the class/inst decl tyvars into the envt
- tcExtendGlobalTyVars (mkVarSet inst_tyvars) (
- tcAddErrCtxt (methodCtxt sel_id) $
- tcBindWithSigs NotTopLevel meth_bind
- [sig_info] meth_prags NonRecursive
- ) `thenTc` \ (binds, insts, _) ->
-
-
- tcExtendLocalValEnv [(meth_name, meth_id)] (
- tcSpecSigs meth_prags
- ) `thenTc` \ (prag_binds1, prag_lie) ->
-
- -- The prag_lie for a SPECIALISE pragma will mention the function
- -- itself, so we have to simplify them away right now lest they float
- -- outwards!
- bindInstsOfLocalFuns prag_lie [meth_id] `thenTc` \ (prag_lie', prag_binds2) ->
-
-
- -- Now check that the instance type variables
- -- (or, in the case of a class decl, the class tyvars)
- -- have not been unified with anything in the environment
- tcAddErrCtxtM (sigCtxt sig_msg (mkSigmaTy inst_tyvars inst_theta (idType meth_id))) $
- checkSigTyVars inst_tyvars `thenTc_`
-
- returnTc (binds `AndMonoBinds` prag_binds1 `AndMonoBinds` prag_binds2,
- insts `plusLIE` prag_lie',
- meth)
- where
- sig_msg ty = sep [ptext SLIT("When checking the expected type for"),
- nest 4 (ppr sel_name <+> dcolon <+> ppr ty)]
-
- sel_name = idName sel_id
-
- -- The renamer just puts the selector ID as the binder in the method binding
- -- but we must use the method name; so we substitute it here. Crude but simple.
- find_bind meth_name (FunMonoBind op_name fix matches loc)
- | op_name == sel_name = Just (FunMonoBind meth_name fix matches loc)
- find_bind meth_name (PatMonoBind (VarPatIn op_name) grhss loc)
- | op_name == sel_name = Just (PatMonoBind (VarPatIn meth_name) grhss loc)
- find_bind meth_name (AndMonoBinds b1 b2)
- = find_bind meth_name b1 `seqMaybe` find_bind meth_name b2
- find_bind meth_name other = Nothing -- Default case
-
-
- -- Find the prags for this method, and replace the
- -- selector name with the method name
- find_prags meth_name [] = []
- find_prags meth_name (SpecSig name ty loc : prags)
- | name == sel_name = SpecSig meth_name ty loc : find_prags meth_name prags
- find_prags meth_name (InlineSig name phase loc : prags)
- | name == sel_name = InlineSig meth_name phase loc : find_prags meth_name prags
- find_prags meth_name (NoInlineSig name phase loc : prags)
- | name == sel_name = NoInlineSig meth_name phase loc : find_prags meth_name prags
- find_prags meth_name (prag:prags) = find_prags meth_name prags
-
- mk_default_bind local_meth_name loc
- = PatMonoBind (VarPatIn local_meth_name)
- (GRHSs (unguardedRHS (default_expr loc) loc) EmptyBinds Nothing)
- loc
-
- default_expr loc
- | explicit_dm = HsVar (getName dm_id) -- There's a default method
- | otherwise = error_expr loc -- No default method
-
- error_expr loc = HsApp (HsVar (getName nO_METHOD_BINDING_ERROR_ID))
- (HsLit (HsString (_PK_ (error_msg loc))))
-
- error_msg loc = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
+ tcExtendTyVarEnv2 xtve (
+ tcAddErrCtxt (methodCtxt sel_id) $
+ tcMonoBinds meth_bind [meth_sig] NonRecursive
+ ) `thenTc` \ (meth_bind, meth_lie, _, _) ->
+
+ -- Now do context reduction. We simplify wrt both the local tyvars
+ -- and the ones of the class/instance decl, so that there is
+ -- no problem with
+ -- class C a where
+ -- op :: Eq a => a -> b -> a
+ --
+ -- We do this for each method independently to localise error messages
+
+ let
+ TySigInfo meth_id meth_tvs meth_theta _ local_meth_id _ _ = meth_sig
+ in
+ tcAddErrCtxtM (sigCtxt sel_id inst_tyvars inst_theta (idType meth_id)) $
+ newDicts SignatureOrigin meth_theta `thenNF_Tc` \ meth_dicts ->
+ let
+ all_tyvars = meth_tvs ++ inst_tyvars
+ all_insts = avail_insts ++ meth_dicts
+ in
+ tcSimplifyCheck
+ (ptext SLIT("class or instance method") <+> quotes (ppr sel_id))
+ all_tyvars all_insts meth_lie `thenTc` \ (lie, lie_binds) ->
+
+ checkSigTyVars all_tyvars `thenTc` \ all_tyvars' ->
+
+ let
+ meth_tvs' = take (length meth_tvs) all_tyvars'
+ poly_meth_bind = AbsBinds meth_tvs'
+ (map instToId meth_dicts)
+ [(meth_tvs', meth_id, local_meth_id)]
+ emptyNameSet -- Inlines?
+ (lie_binds `andMonoBinds` meth_bind)
+ in
+ returnTc (poly_meth_bind, lie)
+
+
+mkMethodBind :: InstOrigin
+ -> Class -> [TcType] -- Class and instance types
+ -> RenamedMonoBinds -- Method binding (pick the right one from in here)
+ -> ClassOpItem
+ -> TcM (Inst, -- Method inst
+ (Id, -- Global selector Id
+ TcSigInfo, -- Signature
+ RenamedMonoBinds)) -- Binding for the method
+
+mkMethodBind origin clas inst_tys meth_binds (sel_id, dm_info)
+ = tcGetSrcLoc `thenNF_Tc` \ loc ->
+ newMethod origin sel_id inst_tys `thenNF_Tc` \ meth_inst ->
+ let
+ meth_id = instToId meth_inst
+ meth_name = idName meth_id
+ in
+ -- Figure out what method binding to use
+ -- If the user suppplied one, use it, else construct a default one
+ (case find_bind (idName sel_id) meth_name meth_binds of
+ Just user_bind -> returnTc user_bind
+ Nothing -> mkDefMethRhs origin clas inst_tys sel_id loc dm_info `thenTc` \ rhs ->
+ returnTc (FunMonoBind meth_name False -- Not infix decl
+ [mkSimpleMatch [] rhs placeHolderType loc] loc)
+ ) `thenTc` \ meth_bind ->
+
+ mkTcSig meth_id loc `thenNF_Tc` \ meth_sig ->
+
+ returnTc (meth_inst, (sel_id, meth_sig, meth_bind))
+
+
+ -- The user didn't supply a method binding,
+ -- so we have to make up a default binding
+ -- The RHS of a default method depends on the default-method info
+mkDefMethRhs origin clas inst_tys sel_id loc (DefMeth dm_name)
+ = -- An polymorphic default method
+ returnTc (HsVar dm_name)
+
+mkDefMethRhs origin clas inst_tys sel_id loc NoDefMeth
+ = -- No default method
+ -- Warn only if -fwarn-missing-methods
+ doptsTc Opt_WarnMissingMethods `thenNF_Tc` \ warn ->
+ warnTc (isInstDecl origin && warn)
+ (omittedMethodWarn sel_id) `thenNF_Tc_`
+ returnTc error_rhs
+ where
+ error_rhs = HsApp (HsVar (getName nO_METHOD_BINDING_ERROR_ID))
+ (HsLit (HsStringPrim (_PK_ (stringToUtf8 error_msg))))
+ error_msg = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
+
+
+mkDefMethRhs origin clas inst_tys sel_id loc GenDefMeth
+ = -- A generic default method
+ -- If the method is defined generically, we can only do the job if the
+ -- instance declaration is for a single-parameter type class with
+ -- a type constructor applied to type arguments in the instance decl
+ -- (checkTc, so False provokes the error)
+ ASSERT( isInstDecl origin ) -- We never get here from a class decl
+
+ checkTc (isJust maybe_tycon)
+ (badGenericInstance sel_id (notSimple inst_tys)) `thenTc_`
+ checkTc (isJust (tyConGenInfo tycon))
+ (badGenericInstance sel_id (notGeneric tycon)) `thenTc_`
+
+ ioToTc (dumpIfSet opt_PprStyle_Debug "Generic RHS" stuff) `thenNF_Tc_`
+ returnTc rhs
+ where
+ rhs = mkGenericRhs sel_id clas_tyvar tycon
+
+ stuff = vcat [ppr clas <+> ppr inst_tys,
+ nest 4 (ppr sel_id <+> equals <+> ppr rhs)]
+
+ -- The tycon is only used in the generic case, and in that
+ -- case we require that the instance decl is for a single-parameter
+ -- type class with type variable arguments:
+ -- instance (...) => C (T a b)
+ clas_tyvar = head (classTyVars clas)
+ Just tycon = maybe_tycon
+ maybe_tycon = case inst_tys of
+ [ty] -> case tcSplitTyConApp_maybe ty of
+ Just (tycon, arg_tys) | all tcIsTyVarTy arg_tys -> Just tycon
+ other -> Nothing
+ other -> Nothing
+
+isInstDecl InstanceDeclOrigin = True
+isInstDecl ClassDeclOrigin = False