-mkMethodBind origin clas inst_tys meth_binds (sel_id, dm_info) = do
- (mb_inst, meth_id) <- mkMethId origin clas sel_id inst_tys
- let
- meth_name = idName meth_id
-
- -- Figure out what method binding to use
- -- If the user suppplied one, use it, else construct a default one
- loc <- getSrcSpanM
- meth_bind
- <- case find_bind (idName sel_id) meth_name meth_binds of
- Just user_bind -> return user_bind
- Nothing -> do
- rhs <- mkDefMethRhs origin clas inst_tys sel_id loc dm_info
- -- Not infix decl
- return (noLoc $ mkFunBind (noLoc meth_name) [mkSimpleMatch [] rhs])
-
- return (mb_inst, (sel_id, meth_id, meth_bind))
-
-mkMethId :: InstOrigin -> Class
- -> Id -> [TcType] -- Selector, and instance types
- -> TcM (Maybe Inst, Id)
-
--- mkMethId instantiates the selector Id at the specified types
-mkMethId origin clas sel_id inst_tys
- = let
- (tyvars,rho) = tcSplitForAllTys (idType sel_id)
- rho_ty = ASSERT( length tyvars == length inst_tys )
- substTyWith tyvars inst_tys rho
- (preds,tau) = tcSplitPhiTy rho_ty
- first_pred = ASSERT( not (null preds)) head preds
- in
- -- The first predicate should be of form (C a b)
- -- where C is the class in question
- ASSERT( not (null preds) &&
- case getClassPredTys_maybe first_pred of
- { Just (clas1,tys) -> clas == clas1 ; Nothing -> False }
- )
- if isSingleton preds then do
- -- If it's the only one, make a 'method'
- inst_loc <- getInstLoc origin
- meth_inst <- newMethod inst_loc sel_id inst_tys
- return (Just meth_inst, instToId meth_inst)
- else do
- -- If it's not the only one we need to be careful
- -- For example, given 'op' defined thus:
- -- class Foo a where
- -- op :: (?x :: String) => a -> a
- -- (mkMethId op T) should return an Inst with type
- -- (?x :: String) => T -> T
- -- That is, the class-op's context is still there.
- -- BUT: it can't be a Method any more, because it breaks
- -- INVARIANT 2 of methods. (See the data decl for Inst.)
- uniq <- newUnique
- loc <- getSrcSpanM
- let
- real_tau = mkPhiTy (tail preds) tau
- meth_id = mkUserLocal (getOccName sel_id) uniq real_tau loc
-
- return (Nothing, meth_id)
-
- -- 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 = do
- -- An polymorphic default method
- dm_name <- lookupImportedName (mkDefMethRdrName sel_id)
- -- Might not be imported, but will be an OrigName
- traceRn (text "mkDefMeth" <+> ppr dm_name)
- return (nlHsVar dm_name)
-
-mkDefMethRhs origin clas inst_tys sel_id loc NoDefMeth = do
- -- No default method
- -- Warn only if -fwarn-missing-methods
- warn <- doptM Opt_WarnMissingMethods
- warnTc (isInstDecl origin
- && warn
- && reportIfUnused (getOccName sel_id))
- (omittedMethodWarn sel_id)
- return error_rhs
- where
- error_rhs = noLoc $ HsLam (mkMatchGroup [mkSimpleMatch wild_pats simple_rhs])
- simple_rhs = nlHsApp (nlHsVar (getName nO_METHOD_BINDING_ERROR_ID))
- (nlHsLit (HsStringPrim (mkFastString error_msg)))
- error_msg = showSDoc (hcat [ppr loc, text "|", ppr sel_id ])
-
- -- When the type is of form t1 -> t2 -> t3
- -- make a default method like (\ _ _ -> noMethBind "blah")
- -- rather than simply (noMethBind "blah")
- -- Reason: if t1 or t2 are higher-ranked types we get n
- -- silly ambiguity messages.
- -- Example: f :: (forall a. Eq a => a -> a) -> Int
- -- f = error "urk"
- -- Here, tcSub tries to force (error "urk") to have the right type,
- -- thus: f = \(x::forall a. Eq a => a->a) -> error "urk" (x t)
- -- where 't' is fresh ty var. This leads directly to "ambiguous t".
- --
- -- NB: technically this changes the meaning of the default-default
- -- method slightly, because `seq` can see the lambdas. Oh well.
- (_,_,tau1) = tcSplitSigmaTy (idType sel_id)
- (_,_,tau2) = tcSplitSigmaTy tau1
- -- Need two splits because the selector can have a type like
- -- forall a. Foo a => forall b. Eq b => ...
- (arg_tys, _) = tcSplitFunTys tau2
- wild_pats = [nlWildPat | ty <- arg_tys]
-
-mkDefMethRhs origin clas inst_tys sel_id loc GenDefMeth