2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[Inst]{The @Inst@ type: dictionaries or method instances}
8 LIE, emptyLIE, unitLIE, plusLIE, consLIE,
9 plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
13 pprInst, pprInsts, pprInstsInFull, tidyInsts, tidyMoreInsts,
15 newDictsFromOld, newDicts, cloneDict,
16 newMethod, newMethodFromName, newMethodWithGivenTy,
17 newMethodWith, newMethodAtLoc,
18 newOverloadedLit, newIPDict,
19 tcInstCall, tcInstDataCon, tcSyntaxName,
21 tyVarsOfInst, tyVarsOfInsts, tyVarsOfLIE,
22 ipNamesOfInst, ipNamesOfInsts, fdPredsOfInst, fdPredsOfInsts,
23 instLoc, getDictClassTys, dictPred,
25 lookupInst, lookupSimpleInst, LookupInstResult(..),
27 isDict, isClassDict, isMethod,
28 isLinearInst, linearInstType, isIPDict, isInheritableInst,
29 isTyVarDict, isStdClassTyVarDict, isMethodFor,
30 instBindingRequired, instCanBeGeneralised,
35 InstOrigin(..), InstLoc, pprInstLoc
38 #include "HsVersions.h"
40 import {-# SOURCE #-} TcExpr( tcExpr )
42 import HsSyn ( HsLit(..), HsOverLit(..), HsExpr(..) )
43 import TcHsSyn ( TcExpr, TcId, TcIdSet, TypecheckedHsExpr,
44 mkHsTyApp, mkHsDictApp, mkHsConApp, zonkId
47 import TcEnv ( tcGetInstEnv, tcLookupId, tcLookupTyCon )
48 import InstEnv ( InstLookupResult(..), lookupInstEnv )
49 import TcMType ( zonkTcType, zonkTcTypes, zonkTcPredType, zapToType,
50 zonkTcThetaType, tcInstTyVar, tcInstType, tcInstTyVars
52 import TcType ( Type, TcType, TcThetaType, TcTyVarSet,
53 SourceType(..), PredType, ThetaType, TyVarDetails(VanillaTv),
54 tcSplitForAllTys, tcSplitForAllTys, mkTyConApp,
55 tcSplitMethodTy, tcSplitPhiTy, mkGenTyConApp,
56 isIntTy,isFloatTy, isIntegerTy, isDoubleTy,
57 tcIsTyVarTy, mkPredTy, mkTyVarTy, mkTyVarTys,
58 tyVarsOfType, tyVarsOfTypes, tyVarsOfPred, tidyPred,
59 isClassPred, isTyVarClassPred, isLinearPred, predHasFDs,
60 getClassPredTys, getClassPredTys_maybe, mkPredName,
61 isInheritablePred, isIPPred,
62 tidyType, tidyTypes, tidyFreeTyVars, tcSplitSigmaTy
64 import CoreFVs ( idFreeTyVars )
65 import Class ( Class )
66 import DataCon ( DataCon,dataConSig )
67 import Id ( Id, idName, idType, mkUserLocal, mkSysLocal, mkLocalId, setIdUnique )
68 import PrelInfo ( isStandardClass, isCcallishClass, isNoDictClass )
69 import Name ( Name, mkMethodOcc, getOccName )
70 import PprType ( pprPred, pprParendType )
71 import Subst ( emptyInScopeSet, mkSubst,
72 substTy, substTyWith, substTheta, mkTyVarSubst, mkTopTyVarSubst
74 import Literal ( inIntRange )
76 import VarEnv ( TidyEnv, emptyTidyEnv, lookupSubstEnv, SubstResult(..) )
77 import VarSet ( elemVarSet, emptyVarSet, unionVarSet )
78 import TysWiredIn ( floatDataCon, doubleDataCon )
79 import PrelNames( fromIntegerName, fromRationalName, rationalTyConName )
80 import Util ( equalLength )
81 import BasicTypes( IPName(..), mapIPName, ipNameName )
82 import UniqSupply( uniqsFromSupply )
90 instName :: Inst -> Name
91 instName inst = idName (instToId inst)
93 instToId :: Inst -> TcId
94 instToId (Dict id _ _) = id
95 instToId (Method id _ _ _ _ _) = id
96 instToId (LitInst id _ _ _) = id
98 instLoc (Dict _ _ loc) = loc
99 instLoc (Method _ _ _ _ _ loc) = loc
100 instLoc (LitInst _ _ _ loc) = loc
102 dictPred (Dict _ pred _ ) = pred
103 dictPred inst = pprPanic "dictPred" (ppr inst)
105 getDictClassTys (Dict _ pred _) = getClassPredTys pred
107 -- fdPredsOfInst is used to get predicates that contain functional
108 -- dependencies; i.e. should participate in improvement
109 fdPredsOfInst (Dict _ pred _) | predHasFDs pred = [pred]
111 fdPredsOfInst (Method _ _ _ theta _ _) = filter predHasFDs theta
112 fdPredsOfInst other = []
114 fdPredsOfInsts :: [Inst] -> [PredType]
115 fdPredsOfInsts insts = concatMap fdPredsOfInst insts
117 isInheritableInst (Dict _ pred _) = isInheritablePred pred
118 isInheritableInst (Method _ _ _ theta _ _) = all isInheritablePred theta
119 isInheritableInst other = True
122 ipNamesOfInsts :: [Inst] -> [Name]
123 ipNamesOfInst :: Inst -> [Name]
124 -- Get the implicit parameters mentioned by these Insts
125 -- NB: ?x and %x get different Names
126 ipNamesOfInsts insts = [n | inst <- insts, n <- ipNamesOfInst inst]
128 ipNamesOfInst (Dict _ (IParam n _) _) = [ipNameName n]
129 ipNamesOfInst (Method _ _ _ theta _ _) = [ipNameName n | IParam n _ <- theta]
130 ipNamesOfInst other = []
132 tyVarsOfInst :: Inst -> TcTyVarSet
133 tyVarsOfInst (LitInst _ _ ty _) = tyVarsOfType ty
134 tyVarsOfInst (Dict _ pred _) = tyVarsOfPred pred
135 tyVarsOfInst (Method _ id tys _ _ _) = tyVarsOfTypes tys `unionVarSet` idFreeTyVars id
136 -- The id might have free type variables; in the case of
137 -- locally-overloaded class methods, for example
140 tyVarsOfInsts insts = foldr (unionVarSet . tyVarsOfInst) emptyVarSet insts
141 tyVarsOfLIE lie = tyVarsOfInsts (lieToList lie)
147 isDict :: Inst -> Bool
148 isDict (Dict _ _ _) = True
151 isClassDict :: Inst -> Bool
152 isClassDict (Dict _ pred _) = isClassPred pred
153 isClassDict other = False
155 isTyVarDict :: Inst -> Bool
156 isTyVarDict (Dict _ pred _) = isTyVarClassPred pred
157 isTyVarDict other = False
159 isIPDict :: Inst -> Bool
160 isIPDict (Dict _ pred _) = isIPPred pred
161 isIPDict other = False
163 isMethod :: Inst -> Bool
164 isMethod (Method _ _ _ _ _ _) = True
165 isMethod other = False
167 isMethodFor :: TcIdSet -> Inst -> Bool
168 isMethodFor ids (Method uniq id tys _ _ loc) = id `elemVarSet` ids
169 isMethodFor ids inst = False
171 isLinearInst :: Inst -> Bool
172 isLinearInst (Dict _ pred _) = isLinearPred pred
173 isLinearInst other = False
174 -- We never build Method Insts that have
175 -- linear implicit paramters in them.
176 -- Hence no need to look for Methods
179 linearInstType :: Inst -> TcType -- %x::t --> t
180 linearInstType (Dict _ (IParam _ ty) _) = ty
183 isStdClassTyVarDict (Dict _ pred _) = case getClassPredTys_maybe pred of
184 Just (clas, [ty]) -> isStandardClass clas && tcIsTyVarTy ty
188 Two predicates which deal with the case where class constraints don't
189 necessarily result in bindings. The first tells whether an @Inst@
190 must be witnessed by an actual binding; the second tells whether an
191 @Inst@ can be generalised over.
194 instBindingRequired :: Inst -> Bool
195 instBindingRequired (Dict _ (ClassP clas _) _) = not (isNoDictClass clas)
196 instBindingRequired other = True
198 instCanBeGeneralised :: Inst -> Bool
199 instCanBeGeneralised (Dict _ (ClassP clas _) _) = not (isCcallishClass clas)
200 instCanBeGeneralised other = True
204 %************************************************************************
206 \subsection{Building dictionaries}
208 %************************************************************************
211 newDicts :: InstOrigin
215 = getInstLoc orig `thenM` \ loc ->
216 newDictsAtLoc loc theta
218 cloneDict :: Inst -> TcM Inst
219 cloneDict (Dict id ty loc) = newUnique `thenM` \ uniq ->
220 returnM (Dict (setIdUnique id uniq) ty loc)
222 newDictsFromOld :: Inst -> TcThetaType -> TcM [Inst]
223 newDictsFromOld (Dict _ _ loc) theta = newDictsAtLoc loc theta
225 -- Local function, similar to newDicts,
226 -- but with slightly different interface
227 newDictsAtLoc :: InstLoc
230 newDictsAtLoc inst_loc@(_,loc,_) theta
231 = newUniqueSupply `thenM` \ us ->
232 returnM (zipWith mk_dict (uniqsFromSupply us) theta)
234 mk_dict uniq pred = Dict (mkLocalId (mkPredName uniq loc pred) (mkPredTy pred)) pred inst_loc
236 -- For vanilla implicit parameters, there is only one in scope
237 -- at any time, so we used to use the name of the implicit parameter itself
238 -- But with splittable implicit parameters there may be many in
239 -- scope, so we make up a new name.
240 newIPDict :: InstOrigin -> IPName Name -> Type
241 -> TcM (IPName Id, Inst)
242 newIPDict orig ip_name ty
243 = getInstLoc orig `thenM` \ inst_loc@(_,loc,_) ->
244 newUnique `thenM` \ uniq ->
246 pred = IParam ip_name ty
247 id = mkLocalId (mkPredName uniq loc pred) (mkPredTy pred)
249 returnM (mapIPName (\n -> id) ip_name, Dict id pred inst_loc)
254 %************************************************************************
256 \subsection{Building methods (calls of overloaded functions)}
258 %************************************************************************
262 tcInstCall :: InstOrigin -> TcType -> TcM (TypecheckedHsExpr -> TypecheckedHsExpr, TcType)
263 tcInstCall orig fun_ty -- fun_ty is usually a sigma-type
264 = tcInstType VanillaTv fun_ty `thenM` \ (tyvars, theta, tau) ->
265 newDicts orig theta `thenM` \ dicts ->
266 extendLIEs dicts `thenM_`
268 inst_fn e = mkHsDictApp (mkHsTyApp e (mkTyVarTys tyvars)) (map instToId dicts)
270 returnM (inst_fn, tau)
272 tcInstDataCon :: InstOrigin -> DataCon
273 -> TcM ([TcType], -- Types to instantiate at
274 [Inst], -- Existential dictionaries to apply to
275 [TcType], -- Argument types of constructor
276 TcType, -- Result type
277 [TyVar]) -- Existential tyvars
278 tcInstDataCon orig data_con
280 (tvs, stupid_theta, ex_tvs, ex_theta, arg_tys, tycon) = dataConSig data_con
281 -- We generate constraints for the stupid theta even when
282 -- pattern matching (as the Report requires)
284 tcInstTyVars VanillaTv (tvs ++ ex_tvs) `thenM` \ (all_tvs', ty_args', tenv) ->
286 stupid_theta' = substTheta tenv stupid_theta
287 ex_theta' = substTheta tenv ex_theta
288 arg_tys' = map (substTy tenv) arg_tys
290 n_normal_tvs = length tvs
291 ex_tvs' = drop n_normal_tvs all_tvs'
292 result_ty = mkTyConApp tycon (take n_normal_tvs ty_args')
294 newDicts orig stupid_theta' `thenM` \ stupid_dicts ->
295 newDicts orig ex_theta' `thenM` \ ex_dicts ->
297 -- Note that we return the stupid theta *only* in the LIE;
298 -- we don't otherwise use it at all
299 extendLIEs stupid_dicts `thenM_`
301 returnM (ty_args', ex_dicts, arg_tys', result_ty, ex_tvs')
304 newMethodFromName :: InstOrigin -> TcType -> Name -> TcM TcId
305 newMethodFromName origin ty name
306 = tcLookupId name `thenM` \ id ->
307 -- Use tcLookupId not tcLookupGlobalId; the method is almost
308 -- always a class op, but with -fno-implicit-prelude GHC is
309 -- meant to find whatever thing is in scope, and that may
310 -- be an ordinary function.
311 newMethod origin id [ty]
313 newMethod :: InstOrigin
317 newMethod orig id tys
318 = -- Get the Id type and instantiate it at the specified types
320 (tyvars, rho) = tcSplitForAllTys (idType id)
321 rho_ty = substTyWith tyvars tys rho
322 (pred, tau) = tcSplitMethodTy rho_ty
324 newMethodWithGivenTy orig id tys [pred] tau
326 newMethodWithGivenTy orig id tys theta tau
327 = getInstLoc orig `thenM` \ loc ->
328 newMethodWith loc id tys theta tau `thenM` \ inst ->
329 extendLIE inst `thenM_`
330 returnM (instToId inst)
332 --------------------------------------------
333 -- newMethodWith and newMethodAtLoc do *not* drop the
334 -- Inst into the LIE; they just returns the Inst
335 -- This is important because they are used by TcSimplify
338 newMethodWith inst_loc@(_,loc,_) id tys theta tau
339 = newUnique `thenM` \ new_uniq ->
341 meth_id = mkUserLocal (mkMethodOcc (getOccName id)) new_uniq tau loc
342 inst = Method meth_id id tys theta tau inst_loc
346 newMethodAtLoc :: InstLoc
349 newMethodAtLoc inst_loc real_id tys
350 -- This actually builds the Inst
351 = -- Get the Id type and instantiate it at the specified types
353 (tyvars,rho) = tcSplitForAllTys (idType real_id)
354 rho_ty = ASSERT( equalLength tyvars tys )
355 substTy (mkTopTyVarSubst tyvars tys) rho
356 (theta, tau) = tcSplitPhiTy rho_ty
358 newMethodWith inst_loc real_id tys theta tau
361 In newOverloadedLit we convert directly to an Int or Integer if we
362 know that's what we want. This may save some time, by not
363 temporarily generating overloaded literals, but it won't catch all
364 cases (the rest are caught in lookupInst).
367 newOverloadedLit :: InstOrigin
371 newOverloadedLit orig lit@(HsIntegral i fi) expected_ty
372 | fi /= fromIntegerName -- Do not generate a LitInst for rebindable
373 -- syntax. Reason: tcSyntaxName does unification
374 -- which is very inconvenient in tcSimplify
375 = tcSyntaxName orig expected_ty fromIntegerName fi `thenM` \ (expr, _) ->
376 returnM (HsApp expr (HsLit (HsInteger i)))
378 | Just expr <- shortCutIntLit i expected_ty
382 = newLitInst orig lit expected_ty
384 newOverloadedLit orig lit@(HsFractional r fr) expected_ty
385 | fr /= fromRationalName -- c.f. HsIntegral case
386 = tcSyntaxName orig expected_ty fromRationalName fr `thenM` \ (expr, _) ->
387 mkRatLit r `thenM` \ rat_lit ->
388 returnM (HsApp expr rat_lit)
390 | Just expr <- shortCutFracLit r expected_ty
394 = newLitInst orig lit expected_ty
396 newLitInst orig lit expected_ty
397 = getInstLoc orig `thenM` \ loc ->
398 newUnique `thenM` \ new_uniq ->
399 zapToType expected_ty `thenM_`
400 -- The expected type might be a 'hole' type variable,
401 -- in which case we must zap it to an ordinary type variable
403 lit_inst = LitInst lit_id lit expected_ty loc
404 lit_id = mkSysLocal FSLIT("lit") new_uniq expected_ty
406 extendLIE lit_inst `thenM_`
407 returnM (HsVar (instToId lit_inst))
409 shortCutIntLit :: Integer -> TcType -> Maybe TcExpr
411 | isIntTy ty && inIntRange i -- Short cut for Int
412 = Just (HsLit (HsInt i))
413 | isIntegerTy ty -- Short cut for Integer
414 = Just (HsLit (HsInteger i))
415 | otherwise = Nothing
417 shortCutFracLit :: Rational -> TcType -> Maybe TcExpr
420 = Just (mkHsConApp floatDataCon [] [HsLit (HsFloatPrim f)])
422 = Just (mkHsConApp doubleDataCon [] [HsLit (HsDoublePrim f)])
423 | otherwise = Nothing
425 mkRatLit :: Rational -> TcM TcExpr
427 = tcLookupTyCon rationalTyConName `thenM` \ rat_tc ->
429 rational_ty = mkGenTyConApp rat_tc []
431 returnM (HsLit (HsRat r rational_ty))
435 %************************************************************************
439 %************************************************************************
441 Zonking makes sure that the instance types are fully zonked,
442 but doesn't do the same for any of the Ids in an Inst. There's no
443 need, and it's a lot of extra work.
446 zonkInst :: Inst -> TcM Inst
447 zonkInst (Dict id pred loc)
448 = zonkTcPredType pred `thenM` \ new_pred ->
449 returnM (Dict id new_pred loc)
451 zonkInst (Method m id tys theta tau loc)
452 = zonkId id `thenM` \ new_id ->
453 -- Essential to zonk the id in case it's a local variable
454 -- Can't use zonkIdOcc because the id might itself be
455 -- an InstId, in which case it won't be in scope
457 zonkTcTypes tys `thenM` \ new_tys ->
458 zonkTcThetaType theta `thenM` \ new_theta ->
459 zonkTcType tau `thenM` \ new_tau ->
460 returnM (Method m new_id new_tys new_theta new_tau loc)
462 zonkInst (LitInst id lit ty loc)
463 = zonkTcType ty `thenM` \ new_ty ->
464 returnM (LitInst id lit new_ty loc)
466 zonkInsts insts = mappM zonkInst insts
470 %************************************************************************
472 \subsection{Printing}
474 %************************************************************************
476 ToDo: improve these pretty-printing things. The ``origin'' is really only
477 relevant in error messages.
480 instance Outputable Inst where
481 ppr inst = pprInst inst
483 pprInsts :: [Inst] -> SDoc
484 pprInsts insts = parens (sep (punctuate comma (map pprInst insts)))
487 = vcat (map go insts)
489 go inst = quotes (ppr inst) <+> pprInstLoc (instLoc inst)
491 pprInst (LitInst u lit ty loc)
492 = hsep [ppr lit, ptext SLIT("at"), ppr ty, show_uniq u]
494 pprInst (Dict u pred loc) = pprPred pred <+> show_uniq u
496 pprInst m@(Method u id tys theta tau loc)
497 = hsep [ppr id, ptext SLIT("at"),
498 brackets (sep (map pprParendType tys)) {- ,
499 ptext SLIT("theta"), ppr theta,
500 ptext SLIT("tau"), ppr tau
504 show_uniq u = ifPprDebug (text "{-" <> ppr u <> text "-}")
506 tidyInst :: TidyEnv -> Inst -> Inst
507 tidyInst env (LitInst u lit ty loc) = LitInst u lit (tidyType env ty) loc
508 tidyInst env (Dict u pred loc) = Dict u (tidyPred env pred) loc
509 tidyInst env (Method u id tys theta tau loc) = Method u id (tidyTypes env tys) theta tau loc
511 tidyMoreInsts :: TidyEnv -> [Inst] -> (TidyEnv, [Inst])
512 -- This function doesn't assume that the tyvars are in scope
513 -- so it works like tidyOpenType, returning a TidyEnv
514 tidyMoreInsts env insts
515 = (env', map (tidyInst env') insts)
517 env' = tidyFreeTyVars env (tyVarsOfInsts insts)
519 tidyInsts :: [Inst] -> (TidyEnv, [Inst])
520 tidyInsts insts = tidyMoreInsts emptyTidyEnv insts
522 showLIE :: String -> TcM () -- Debugging
524 = do { lie_var <- getLIEVar ;
525 lie <- readMutVar lie_var ;
526 traceTc (text str <+> pprInstsInFull (lieToList lie)) }
530 %************************************************************************
532 \subsection{Looking up Insts}
534 %************************************************************************
537 data LookupInstResult s
539 | SimpleInst TcExpr -- Just a variable, type application, or literal
540 | GenInst [Inst] TcExpr -- The expression and its needed insts
542 lookupInst :: Inst -> TcM (LookupInstResult s)
543 -- It's important that lookupInst does not put any new stuff into
544 -- the LIE. Instead, any Insts needed by the lookup are returned in
545 -- the LookupInstResult, where they can be further processed by tcSimplify
549 lookupInst dict@(Dict _ (ClassP clas tys) loc)
550 = getDOpts `thenM` \ dflags ->
551 tcGetInstEnv `thenM` \ inst_env ->
552 case lookupInstEnv dflags inst_env clas tys of
554 FoundInst tenv dfun_id
555 -> -- It's possible that not all the tyvars are in
556 -- the substitution, tenv. For example:
557 -- instance C X a => D X where ...
558 -- (presumably there's a functional dependency in class C)
559 -- Hence the mk_ty_arg to instantiate any un-substituted tyvars.
561 (tyvars, rho) = tcSplitForAllTys (idType dfun_id)
562 mk_ty_arg tv = case lookupSubstEnv tenv tv of
563 Just (DoneTy ty) -> returnM ty
564 Nothing -> tcInstTyVar VanillaTv tv `thenM` \ tc_tv ->
565 returnM (mkTyVarTy tc_tv)
567 mappM mk_ty_arg tyvars `thenM` \ ty_args ->
569 dfun_rho = substTy (mkTyVarSubst tyvars ty_args) rho
570 (theta, _) = tcSplitPhiTy dfun_rho
571 ty_app = mkHsTyApp (HsVar dfun_id) ty_args
574 returnM (SimpleInst ty_app)
576 newDictsAtLoc loc theta `thenM` \ dicts ->
578 rhs = mkHsDictApp ty_app (map instToId dicts)
580 returnM (GenInst dicts rhs)
582 other -> returnM NoInstance
584 lookupInst (Dict _ _ _) = returnM NoInstance
588 lookupInst inst@(Method _ id tys theta _ loc)
589 = newDictsAtLoc loc theta `thenM` \ dicts ->
590 returnM (GenInst dicts (mkHsDictApp (mkHsTyApp (HsVar id) tys) (map instToId dicts)))
594 -- Look for short cuts first: if the literal is *definitely* a
595 -- int, integer, float or a double, generate the real thing here.
596 -- This is essential (see nofib/spectral/nucleic).
597 -- [Same shortcut as in newOverloadedLit, but we
598 -- may have done some unification by now]
601 lookupInst inst@(LitInst u (HsIntegral i from_integer_name) ty loc)
602 | Just expr <- shortCutIntLit i ty
603 = returnM (GenInst [] expr) -- GenInst, not SimpleInst, because
604 -- expr may be a constructor application
606 = ASSERT( from_integer_name == fromIntegerName ) -- A LitInst invariant
607 tcLookupId fromIntegerName `thenM` \ from_integer ->
608 newMethodAtLoc loc from_integer [ty] `thenM` \ method_inst ->
609 returnM (GenInst [method_inst]
610 (HsApp (HsVar (instToId method_inst)) (HsLit (HsInteger i))))
613 lookupInst inst@(LitInst u (HsFractional f from_rat_name) ty loc)
614 | Just expr <- shortCutFracLit f ty
615 = returnM (GenInst [] expr)
618 = ASSERT( from_rat_name == fromRationalName ) -- A LitInst invariant
619 tcLookupId fromRationalName `thenM` \ from_rational ->
620 newMethodAtLoc loc from_rational [ty] `thenM` \ method_inst ->
621 mkRatLit f `thenM` \ rat_lit ->
622 returnM (GenInst [method_inst] (HsApp (HsVar (instToId method_inst)) rat_lit))
625 There is a second, simpler interface, when you want an instance of a
626 class at a given nullary type constructor. It just returns the
627 appropriate dictionary if it exists. It is used only when resolving
628 ambiguous dictionaries.
631 lookupSimpleInst :: Class
632 -> [Type] -- Look up (c,t)
633 -> TcM (Maybe ThetaType) -- Here are the needed (c,t)s
635 lookupSimpleInst clas tys
636 = getDOpts `thenM` \ dflags ->
637 tcGetInstEnv `thenM` \ inst_env ->
638 case lookupInstEnv dflags inst_env clas tys of
640 -> returnM (Just (substTheta (mkSubst emptyInScopeSet tenv) theta))
642 (_, rho) = tcSplitForAllTys (idType dfun)
643 (theta,_) = tcSplitPhiTy rho
645 other -> returnM Nothing
649 %************************************************************************
653 %************************************************************************
656 Suppose we are doing the -fno-implicit-prelude thing, and we encounter
657 a do-expression. We have to find (>>) in the current environment, which is
658 done by the rename. Then we have to check that it has the same type as
659 Control.Monad.(>>). Or, more precisely, a compatible type. One 'customer' had
662 (>>) :: HB m n mn => m a -> n b -> mn b
664 So the idea is to generate a local binding for (>>), thus:
666 let then72 :: forall a b. m a -> m b -> m b
667 then72 = ...something involving the user's (>>)...
669 ...the do-expression...
671 Now the do-expression can proceed using then72, which has exactly
674 In fact tcSyntaxName just generates the RHS for then72, because we only
675 want an actual binding in the do-expression case. For literals, we can
676 just use the expression inline.
679 tcSyntaxName :: InstOrigin
680 -> TcType -- Type to instantiate it at
681 -> Name -> Name -- (Standard name, user name)
682 -> TcM (TcExpr, TcType) -- Suitable expression with its type
684 -- NB: tcSyntaxName calls tcExpr, and hence can do unification.
685 -- So we do not call it from lookupInst, which is called from tcSimplify
687 tcSyntaxName orig ty std_nm user_nm
689 = newMethodFromName orig ty std_nm `thenM` \ id ->
690 returnM (HsVar id, idType id)
693 = tcLookupId std_nm `thenM` \ std_id ->
695 -- C.f. newMethodAtLoc
696 ([tv], _, tau) = tcSplitSigmaTy (idType std_id)
697 tau1 = substTy (mkTopTyVarSubst [tv] [ty]) tau
699 addErrCtxtM (syntaxNameCtxt user_nm orig tau1) $
700 tcExpr (HsVar user_nm) tau1 `thenM` \ user_fn ->
701 returnM (user_fn, tau1)
703 syntaxNameCtxt name orig ty tidy_env
704 = getInstLoc orig `thenM` \ inst_loc ->
706 msg = vcat [ptext SLIT("When checking that") <+> quotes (ppr name) <+>
707 ptext SLIT("(needed by a syntactic construct)"),
708 nest 2 (ptext SLIT("has the required type:") <+> ppr (tidyType tidy_env ty)),
709 nest 2 (pprInstLoc inst_loc)]
711 returnM (tidy_env, msg)