+\begin{code}
+mkDictSelId :: Bool -- True <=> don't include the unfolding
+ -- Little point on imports without -O, because the
+ -- dictionary itself won't be visible
+ -> Name -- Name of one of the *value* selectors
+ -- (dictionary superclass or method)
+ -> Class -> Id
+mkDictSelId no_unf name clas
+ = mkGlobalId (ClassOpId clas) name sel_ty info
+ where
+ sel_ty = mkForAllTys tyvars (mkFunTy (idType dict_id) (idType the_arg_id))
+ -- We can't just say (exprType rhs), because that would give a type
+ -- C a -> C a
+ -- for a single-op class (after all, the selector is the identity)
+ -- But it's type must expose the representation of the dictionary
+ -- to get (say) C a -> (a -> a)
+
+ base_info = noCafIdInfo
+ `setArityInfo` 1
+ `setStrictnessInfo` Just strict_sig
+ `setUnfoldingInfo` (if no_unf then noUnfolding
+ else mkImplicitUnfolding rhs)
+ -- In module where class op is defined, we must add
+ -- the unfolding, even though it'll never be inlined
+ -- becuase we use that to generate a top-level binding
+ -- for the ClassOp
+
+ info = base_info `setSpecInfo` mkSpecInfo [rule]
+ `setInlinePragInfo` neverInlinePragma
+ -- Add a magic BuiltinRule, and never inline it
+ -- so that the rule is always available to fire.
+ -- See Note [ClassOp/DFun selection] in TcInstDcls
+
+ n_ty_args = length tyvars
+
+ -- This is the built-in rule that goes
+ -- op (dfT d1 d2) ---> opT d1 d2
+ rule = BuiltinRule { ru_name = fsLit "Class op " `appendFS`
+ occNameFS (getOccName name)
+ , ru_fn = name
+ , ru_nargs = n_ty_args + 1
+ , ru_try = dictSelRule val_index n_ty_args n_eq_args }
+
+ -- The strictness signature is of the form U(AAAVAAAA) -> T
+ -- where the V depends on which item we are selecting
+ -- It's worth giving one, so that absence info etc is generated
+ -- even if the selector isn't inlined
+ strict_sig = mkStrictSig (mkTopDmdType [arg_dmd] TopRes)
+ arg_dmd | new_tycon = evalDmd
+ | otherwise = Eval (Prod [ if the_arg_id == id then evalDmd else Abs
+ | id <- arg_ids ])
+
+ tycon = classTyCon clas
+ new_tycon = isNewTyCon tycon
+ [data_con] = tyConDataCons tycon
+ tyvars = dataConUnivTyVars data_con
+ arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
+ eq_theta = dataConEqTheta data_con
+ n_eq_args = length eq_theta
+
+ -- 'index' is a 0-index into the *value* arguments of the dictionary
+ val_index = assoc "MkId.mkDictSelId" sel_index_prs name
+ sel_index_prs = map idName (classAllSelIds clas) `zip` [0..]
+
+ the_arg_id = arg_ids !! val_index
+ pred = mkClassPred clas (mkTyVarTys tyvars)
+ dict_id = mkTemplateLocal 1 $ mkPredTy pred
+ arg_ids = mkTemplateLocalsNum 2 arg_tys
+ eq_ids = map mkWildEvBinder eq_theta
+
+ rhs = mkLams tyvars (Lam dict_id rhs_body)
+ rhs_body | new_tycon = unwrapNewTypeBody tycon (map mkTyVarTy tyvars) (Var dict_id)
+ | otherwise = Case (Var dict_id) dict_id (idType the_arg_id)
+ [(DataAlt data_con, eq_ids ++ arg_ids, Var the_arg_id)]
+
+dictSelRule :: Int -> Arity -> Arity
+ -> IdUnfoldingFun -> [CoreExpr] -> Maybe CoreExpr
+-- Oh, very clever
+-- sel_i t1..tk (df s1..sn d1..dm) = op_i_helper s1..sn d1..dm
+-- sel_i t1..tk (D t1..tk op1 ... opm) = opi
+--
+-- NB: the data constructor has the same number of type and
+-- coercion args as the selector
+--
+-- This only works for *value* superclasses
+-- There are no selector functions for equality superclasses
+dictSelRule val_index n_ty_args n_eq_args id_unf args
+ | (dict_arg : _) <- drop n_ty_args args
+ , Just (_, _, con_args) <- exprIsConApp_maybe id_unf dict_arg
+ , let val_args = drop n_eq_args con_args
+ = Just (val_args !! val_index)
+ | otherwise
+ = Nothing
+\end{code}