+ -- Typecheck the methods
+ ; let prag_fn = mkPragFun uprags monobinds
+ tc_meth = tcInstanceMethod loc standalone_deriv
+ clas inst_tyvars'
+ dfun_dicts inst_tys'
+ nested_this_pair
+ prag_fn spec_inst_prags monobinds
+
+ ; (meth_ids, meth_binds) <- tcExtendTyVarEnv inst_tyvars' $
+ mapAndUnzipM tc_meth op_items
+
+ -- Figure out bindings for the superclass context
+ ; sc_loc <- getInstLoc InstScOrigin
+ ; sc_dicts <- newDictOccs sc_loc sc_theta' -- These are wanted
+ ; let tc_sc = tcSuperClass inst_loc inst_tyvars' dfun_dicts nested_this_pair
+ ; (sc_ids, sc_binds) <- mapAndUnzipM tc_sc (sc_sels `zip` sc_dicts)
+
+ -- It's possible that the superclass stuff might unified
+ -- something in the envt with one of the inst_tyvars'
+ ; checkSigTyVars inst_tyvars'
+
+ -- Create the result bindings
+ ; let dict_constr = classDataCon clas
+ this_dict_id = instToId this_dict
+ dict_bind = mkVarBind this_dict_id dict_rhs
+ dict_rhs = foldl mk_app inst_constr sc_meth_ids
+ sc_meth_ids = sc_ids ++ meth_ids
+ inst_constr = L loc $ wrapId (mkWpTyApps inst_tys')
+ (dataConWrapId dict_constr)
+ -- We don't produce a binding for the dict_constr; instead we
+ -- rely on the simplifier to unfold this saturated application
+ -- We do this rather than generate an HsCon directly, because
+ -- it means that the special cases (e.g. dictionary with only one
+ -- member) are dealt with by the common MkId.mkDataConWrapId code rather
+ -- than needing to be repeated here.
+
+ mk_app :: LHsExpr Id -> Id -> LHsExpr Id
+ mk_app fun arg_id = L loc (HsApp fun (L loc (wrapId arg_wrapper arg_id)))
+ arg_wrapper = mkWpApps dfun_lam_vars <.> mkWpTyApps (mkTyVarTys inst_tyvars')
+
+ -- Do not inline the dfun; instead give it a magic DFunFunfolding
+ -- See Note [ClassOp/DFun selection]
+ -- See also note [Single-method classes]
+ dfun_id_w_fun = dfun_id
+ `setIdUnfolding` mkDFunUnfolding inst_ty (map Var sc_meth_ids)
+ `setInlinePragma` dfunInlinePragma
+
+ main_bind = AbsBinds
+ inst_tyvars'
+ dfun_lam_vars
+ [(inst_tyvars', dfun_id_w_fun, this_dict_id, SpecPrags spec_inst_prags)]
+ (unitBag dict_bind)
+
+ ; showLIE (text "instance")
+ ; return (unitBag (L loc main_bind) `unionBags`
+ listToBag meth_binds `unionBags`
+ listToBag sc_binds)
+ }
+
+{-
+ -- Create the result bindings
+ ; let this_dict_id = instToId this_dict
+ arg_ids = sc_ids ++ meth_ids
+ arg_binds = listToBag meth_binds `unionBags`
+ listToBag sc_binds
+
+ ; showLIE (text "instance")
+ ; case newTyConCo_maybe (classTyCon clas) of
+ Nothing -- A multi-method class
+ -> return (unitBag (L loc data_bind) `unionBags` arg_binds)
+ where
+ data_dfun_id = dfun_id -- Do not inline; instead give it a magic DFunFunfolding
+ -- See Note [ClassOp/DFun selection]
+ `setIdUnfolding` mkDFunUnfolding dict_constr arg_ids
+ `setInlinePragma` dfunInlinePragma
+
+ data_bind = AbsBinds inst_tyvars' dfun_lam_vars
+ [(inst_tyvars', data_dfun_id, this_dict_id, spec_inst_prags)]
+ (unitBag dict_bind)
+
+ dict_bind = mkVarBind this_dict_id dict_rhs
+ dict_rhs = foldl mk_app inst_constr arg_ids
+ dict_constr = classDataCon clas
+ inst_constr = L loc $ wrapId (mkWpTyApps inst_tys')
+ (dataConWrapId dict_constr)
+ -- We don't produce a binding for the dict_constr; instead we
+ -- rely on the simplifier to unfold this saturated application
+ -- We do this rather than generate an HsCon directly, because
+ -- it means that the special cases (e.g. dictionary with only one
+ -- member) are dealt with by the common MkId.mkDataConWrapId code rather
+ -- than needing to be repeated here.
+
+ mk_app :: LHsExpr Id -> Id -> LHsExpr Id
+ mk_app fun arg_id = L loc (HsApp fun (L loc (wrapId arg_wrapper arg_id)))
+ arg_wrapper = mkWpApps dfun_lam_vars <.> mkWpTyApps (mkTyVarTys inst_tyvars')
+
+ Just the_nt_co -- (Just co) for a single-method class
+ -> return (unitBag (L loc nt_bind) `unionBags` arg_binds)
+ where
+ nt_dfun_id = dfun_id -- Just let the dfun inline; see Note [Single-method classes]
+ `setInlinePragma` alwaysInlinePragma
+
+ local_nt_dfun = setIdType this_dict_id inst_ty -- A bit of a hack, but convenient
+
+ nt_bind = AbsBinds [] []
+ [([], nt_dfun_id, local_nt_dfun, spec_inst_prags)]
+ (unitBag (mkVarBind local_nt_dfun (L loc (wrapId nt_cast the_meth_id))))
+
+ the_meth_id = ASSERT( length arg_ids == 1 ) head arg_ids
+ nt_cast = WpCast $ mkPiTypes (inst_tyvars' ++ dfun_lam_vars) $
+ mkSymCoercion (mkTyConApp the_nt_co inst_tys')
+-}
+
+------------------------------
+tcSuperClass :: InstLoc -> [TyVar] -> [Inst]
+ -> (Inst, LHsBinds Id)
+ -> (Id, Inst) -> TcM (Id, LHsBind Id)
+-- Build a top level decl like
+-- sc_op = /\a \d. let this = ... in
+-- let sc = ... in
+-- sc
+-- The "this" part is just-in-case (discarded if not used)
+-- See Note [Recursive superclasses]
+tcSuperClass inst_loc tyvars dicts (this_dict, this_bind)
+ (sc_sel, sc_dict)
+ = addErrCtxt superClassCtxt $
+ do { sc_binds <- tcSimplifySuperClasses inst_loc
+ this_dict dicts [sc_dict]
+ -- Don't include this_dict in the 'givens', else
+ -- sc_dicts get bound by just selecting from this_dict!!
+
+ ; uniq <- newUnique
+ ; let sc_op_ty = mkSigmaTy tyvars (map dictPred dicts)
+ (mkPredTy (dictPred sc_dict))
+ sc_op_name = mkDerivedInternalName mkClassOpAuxOcc uniq
+ (getName sc_sel)
+ sc_op_id = mkLocalId sc_op_name sc_op_ty
+ sc_id = instToVar sc_dict
+ sc_op_bind = AbsBinds tyvars
+ (map instToVar dicts)
+ [(tyvars, sc_op_id, sc_id, noSpecPrags)]
+ (this_bind `unionBags` sc_binds)
+
+ ; return (sc_op_id, noLoc sc_op_bind) }