+dsTcEvBinds :: TcEvBinds -> DsM [DsEvBind]
+dsTcEvBinds (TcEvBinds {}) = panic "dsEvBinds" -- Zonker has got rid of this
+dsTcEvBinds (EvBinds bs) = dsEvBinds bs
+
+dsEvBinds :: Bag EvBind -> DsM [DsEvBind]
+dsEvBinds bs = return (map dsEvGroup sccs)
+ where
+ sccs :: [SCC EvBind]
+ sccs = stronglyConnCompFromEdgedVertices edges
+
+ edges :: [(EvBind, EvVar, [EvVar])]
+ edges = foldrBag ((:) . mk_node) [] bs
+
+ mk_node :: EvBind -> (EvBind, EvVar, [EvVar])
+ mk_node b@(EvBind var term) = (b, var, free_vars_of term)
+
+ free_vars_of :: EvTerm -> [EvVar]
+ free_vars_of (EvId v) = [v]
+ free_vars_of (EvCast v co) = v : varSetElems (tyCoVarsOfCo co)
+ free_vars_of (EvCoercion co) = varSetElems (tyCoVarsOfCo co)
+ free_vars_of (EvDFunApp _ _ vs) = vs
+ free_vars_of (EvSuperClass d _) = [d]
+
+dsEvGroup :: SCC EvBind -> DsEvBind
+dsEvGroup (AcyclicSCC (EvBind co_var (EvSuperClass dict n)))
+ | isCoVar co_var -- An equality superclass
+ = ASSERT( null other_data_cons )
+ CaseEvBind dict (DataAlt data_con) bndrs
+ where
+ (cls, tys) = getClassPredTys (evVarPred dict)
+ (data_con:other_data_cons) = tyConDataCons (classTyCon cls)
+ (ex_tvs, theta, rho) = tcSplitSigmaTy (applyTys (dataConRepType data_con) tys)
+ (arg_tys, _) = splitFunTys rho
+ bndrs = ex_tvs ++ map mk_wild_pred (theta `zip` [0..])
+ ++ map mkWildValBinder arg_tys
+ mk_wild_pred (p, i) | i==n = ASSERT( p `eqPred` (coVarPred co_var))
+ co_var
+ | otherwise = mkWildEvBinder p
+
+dsEvGroup (AcyclicSCC (EvBind v r))
+ = LetEvBind (NonRec v (dsEvTerm r))
+
+dsEvGroup (CyclicSCC bs)
+ = LetEvBind (Rec (map ds_pair bs))
+ where
+ ds_pair (EvBind v r) = (v, dsEvTerm r)
+
+dsEvTerm :: EvTerm -> CoreExpr
+dsEvTerm (EvId v) = Var v
+dsEvTerm (EvCast v co) = Cast (Var v) co
+dsEvTerm (EvDFunApp df tys vars) = Var df `mkTyApps` tys `mkVarApps` vars
+dsEvTerm (EvCoercion co) = Coercion co
+dsEvTerm (EvSuperClass d n)
+ = ASSERT( isClassPred (classSCTheta cls !! n) )
+ -- We can only select *dictionary* superclasses
+ -- in terms. Equality superclasses are dealt with
+ -- in dsEvGroup, where they can generate a case expression
+ Var sc_sel_id `mkTyApps` tys `App` Var d
+ where
+ sc_sel_id = classSCSelId cls n -- Zero-indexed
+ (cls, tys) = getClassPredTys (evVarPred d)
+
+------------------------
+makeCorePair :: Id -> Bool -> Arity -> CoreExpr -> (Id, CoreExpr)
+makeCorePair gbl_id is_default_method dict_arity rhs
+ | is_default_method -- Default methods are *always* inlined
+ = (gbl_id `setIdUnfolding` mkCompulsoryUnfolding rhs, rhs)
+
+ | otherwise
+ = case inlinePragmaSpec inline_prag of
+ EmptyInlineSpec -> (gbl_id, rhs)
+ NoInline -> (gbl_id, rhs)
+ Inlinable -> (gbl_id `setIdUnfolding` inlinable_unf, rhs)
+ Inline -> inline_pair
+
+ where
+ inline_prag = idInlinePragma gbl_id
+ inlinable_unf = mkInlinableUnfolding rhs
+ inline_pair
+ | Just arity <- inlinePragmaSat inline_prag
+ -- Add an Unfolding for an INLINE (but not for NOINLINE)
+ -- And eta-expand the RHS; see Note [Eta-expanding INLINE things]
+ , let real_arity = dict_arity + arity
+ -- NB: The arity in the InlineRule takes account of the dictionaries
+ = ( gbl_id `setIdUnfolding` mkInlineUnfolding (Just real_arity) rhs
+ , etaExpand real_arity rhs)
+
+ | otherwise
+ = pprTrace "makeCorePair: arity missing" (ppr gbl_id) $
+ (gbl_id `setIdUnfolding` mkInlineUnfolding Nothing rhs, rhs)
+
+
+dictArity :: [Var] -> Arity
+-- Don't count coercion variables in arity
+dictArity dicts = count isId dicts