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, zonkLIE,
9 plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE,
11 Inst, OverloadedLit(..),
12 pprInst, pprInsts, pprInstsInFull, tidyInst, tidyInsts,
16 newDictFromOld, newDicts, newClassDicts, newDictsAtLoc,
17 newMethod, newMethodWithGivenTy, newOverloadedLit,
18 newIPDict, instOverloadedFun,
20 tyVarsOfInst, tyVarsOfInsts, tyVarsOfLIE, instLoc, getDictClassTys,
21 getDictPred_maybe, getMethodTheta_maybe,
22 getFunDeps, getFunDepsOfLIE,
24 getAllFunDeps, getAllFunDepsOfLIE,
26 lookupInst, lookupSimpleInst, LookupInstResult(..),
28 isDict, isClassDict, isMethod,
29 isTyVarDict, isStdClassTyVarDict, isMethodFor, notFunDep,
30 instBindingRequired, instCanBeGeneralised,
32 zonkInst, zonkInsts, zonkFunDeps, zonkTvFunDeps,
33 instToId, instToIdBndr, ipToId,
35 InstOrigin(..), InstLoc, pprInstLoc
38 #include "HsVersions.h"
40 import HsSyn ( HsLit(..), HsExpr(..) )
41 import RnHsSyn ( RenamedArithSeqInfo, RenamedHsExpr, RenamedPat )
42 import TcHsSyn ( TcExpr, TcId,
43 mkHsTyApp, mkHsDictApp, mkHsConApp, zonkId
46 import TcEnv ( TcIdSet, tcLookupValueByKey, tcLookupTyConByKey )
47 import TcType ( TcThetaType,
48 TcType, TcTauType, TcTyVarSet,
49 zonkTcTyVars, zonkTcType, zonkTcTypes,
53 import Class ( classInstEnv, Class )
54 import FunDeps ( instantiateFdClassTys )
55 import Id ( Id, idFreeTyVars, idType, mkUserLocal, mkSysLocal )
56 import PrelInfo ( isStandardClass, isCcallishClass, isNoDictClass )
57 import Name ( OccName, Name, mkDictOcc, mkMethodOcc, mkIPOcc,
58 getOccName, nameUnique )
59 import PprType ( pprPred )
60 import InstEnv ( InstEnv, lookupInstEnv )
61 import SrcLoc ( SrcLoc )
62 import Type ( Type, PredType(..), ThetaType,
63 mkTyVarTy, isTyVarTy, mkDictTy, mkPredTy,
64 splitForAllTys, splitSigmaTy,
65 splitRhoTy, tyVarsOfType, tyVarsOfTypes, tyVarsOfPred,
66 mkSynTy, tidyOpenType, tidyOpenTypes
68 import InstEnv ( InstEnv )
69 import Subst ( emptyInScopeSet, mkSubst,
70 substTy, substClasses, mkTyVarSubst, mkTopTyVarSubst
72 import TyCon ( TyCon )
73 import Literal ( inIntRange )
75 import VarEnv ( lookupVarEnv, TidyEnv,
76 lookupSubstEnv, SubstResult(..)
78 import VarSet ( elemVarSet, emptyVarSet, unionVarSet )
79 import TysPrim ( intPrimTy, floatPrimTy, doublePrimTy )
80 import TysWiredIn ( intDataCon, isIntTy,
81 floatDataCon, isFloatTy,
82 doubleDataCon, isDoubleTy,
83 integerTy, isIntegerTy
85 import Unique ( fromRationalClassOpKey, rationalTyConKey,
86 fromIntClassOpKey, fromIntegerClassOpKey, Unique
88 import Maybes ( expectJust )
89 import Maybe ( catMaybes )
90 import Util ( thenCmp, zipWithEqual, mapAccumL )
94 %************************************************************************
96 \subsection[Inst-collections]{LIE: a collection of Insts}
98 %************************************************************************
103 isEmptyLIE = isEmptyBag
105 unitLIE inst = unitBag inst
106 mkLIE insts = listToBag insts
107 plusLIE lie1 lie2 = lie1 `unionBags` lie2
108 consLIE inst lie = inst `consBag` lie
109 plusLIEs lies = unionManyBags lies
110 lieToList = bagToList
111 listToLIE = listToBag
113 zonkLIE :: LIE -> NF_TcM s LIE
114 zonkLIE lie = mapBagNF_Tc zonkInst lie
116 pprInsts :: [Inst] -> SDoc
117 pprInsts insts = parens (sep (punctuate comma (map pprInst insts)))
121 = vcat (map go insts)
123 go inst = quotes (ppr inst) <+> pprInstLoc (instLoc inst)
126 %************************************************************************
128 \subsection[Inst-types]{@Inst@ types}
130 %************************************************************************
132 An @Inst@ is either a dictionary, an instance of an overloaded
133 literal, or an instance of an overloaded value. We call the latter a
134 ``method'' even though it may not correspond to a class operation.
135 For example, we might have an instance of the @double@ function at
136 type Int, represented by
138 Method 34 doubleId [Int] origin
150 TcId -- The overloaded function
151 -- This function will be a global, local, or ClassOpId;
152 -- inside instance decls (only) it can also be an InstId!
153 -- The id needn't be completely polymorphic.
154 -- You'll probably find its name (for documentation purposes)
155 -- inside the InstOrigin
157 [TcType] -- The types to which its polymorphic tyvars
158 -- should be instantiated.
159 -- These types must saturate the Id's foralls.
161 TcThetaType -- The (types of the) dictionaries to which the function
162 -- must be applied to get the method
164 TcTauType -- The type of the method
168 -- INVARIANT: in (Method u f tys theta tau loc)
169 -- type of (f tys dicts(from theta)) = tau
174 TcType -- The type at which the literal is used
178 Class -- the class from which this arises
179 [([TcType], [TcType])]
183 = OverloadedIntegral Integer -- The number
184 | OverloadedFractional Rational -- The number
189 @Insts@ are ordered by their class/type info, rather than by their
190 unique. This allows the context-reduction mechanism to use standard finite
191 maps to do their stuff.
194 instance Ord Inst where
196 instance Ord PredType where
199 instance Eq Inst where
200 (==) i1 i2 = case i1 `cmpInst` i2 of
203 instance Eq PredType where
204 (==) p1 p2 = case p1 `cmpPred` p2 of
208 cmpInst (Dict _ pred1 _) (Dict _ pred2 _)
209 = (pred1 `cmpPred` pred2)
210 cmpInst (Dict _ _ _) other
213 cmpInst (Method _ _ _ _ _ _) (Dict _ _ _)
215 cmpInst (Method _ id1 tys1 _ _ _) (Method _ id2 tys2 _ _ _)
216 = (id1 `compare` id2) `thenCmp` (tys1 `compare` tys2)
217 cmpInst (Method _ _ _ _ _ _) other
220 cmpInst (LitInst _ lit1 ty1 _) (LitInst _ lit2 ty2 _)
221 = (lit1 `cmpOverLit` lit2) `thenCmp` (ty1 `compare` ty2)
222 cmpInst (LitInst _ _ _ _) (FunDep _ _ _)
224 cmpInst (LitInst _ _ _ _) other
227 cmpInst (FunDep clas1 fds1 _) (FunDep clas2 fds2 _)
228 = (clas1 `compare` clas2) `thenCmp` (fds1 `compare` fds2)
229 cmpInst (FunDep _ _ _) other
232 cmpPred (Class c1 tys1) (Class c2 tys2)
233 = (c1 `compare` c2) `thenCmp` (tys1 `compare` tys2)
234 cmpPred (IParam n1 ty1) (IParam n2 ty2)
235 = (n1 `compare` n2) `thenCmp` (ty1 `compare` ty2)
236 cmpPred (Class _ _) (IParam _ _) = LT
239 cmpOverLit (OverloadedIntegral i1) (OverloadedIntegral i2) = i1 `compare` i2
240 cmpOverLit (OverloadedFractional f1) (OverloadedFractional f2) = f1 `compare` f2
241 cmpOverLit (OverloadedIntegral _) (OverloadedFractional _) = LT
242 cmpOverLit (OverloadedFractional _) (OverloadedIntegral _) = GT
249 instLoc (Dict u pred loc) = loc
250 instLoc (Method u _ _ _ _ loc) = loc
251 instLoc (LitInst u lit ty loc) = loc
252 instLoc (FunDep _ _ loc) = loc
254 getDictPred_maybe (Dict _ p _) = Just p
255 getDictPred_maybe _ = Nothing
257 getMethodTheta_maybe (Method _ _ _ theta _ _) = Just theta
258 getMethodTheta_maybe _ = Nothing
260 getDictClassTys (Dict u (Class clas tys) _) = (clas, tys)
262 getFunDeps (FunDep clas fds _) = Just (clas, fds)
263 getFunDeps _ = Nothing
265 getFunDepsOfLIE lie = catMaybes (map getFunDeps (lieToList lie))
267 getIPsOfPred (IParam n ty) = [(n, ty)]
269 getIPsOfTheta theta = concatMap getIPsOfPred theta
271 getIPs (Dict u (IParam n ty) loc) = [(n, ty)]
272 getIPs (Method u id _ theta t loc) = getIPsOfTheta theta
275 getIPsOfLIE lie = concatMap getIPs (lieToList lie)
277 getAllFunDeps (FunDep clas fds _) = fds
278 getAllFunDeps inst = map (\(n,ty) -> ([], [ty])) (getIPs inst)
280 getAllFunDepsOfLIE lie = concat (map getAllFunDeps (lieToList lie))
282 tyVarsOfInst :: Inst -> TcTyVarSet
283 tyVarsOfInst (Dict _ pred _) = tyVarsOfPred pred
284 tyVarsOfInst (Method _ id tys _ _ _) = tyVarsOfTypes tys `unionVarSet` idFreeTyVars id
285 -- The id might have free type variables; in the case of
286 -- locally-overloaded class methods, for example
287 tyVarsOfInst (LitInst _ _ ty _) = tyVarsOfType ty
288 tyVarsOfInst (FunDep _ fds _)
289 = foldr unionVarSet emptyVarSet (map tyVarsOfFd fds)
290 where tyVarsOfFd (ts1, ts2) =
291 tyVarsOfTypes ts1 `unionVarSet` tyVarsOfTypes ts2
294 = foldr unionVarSet emptyVarSet (map tyVarsOfInst insts)
297 = foldr unionVarSet emptyVarSet (map tyVarsOfInst insts)
298 where insts = lieToList lie
304 isDict :: Inst -> Bool
305 isDict (Dict _ _ _) = True
307 isClassDict :: Inst -> Bool
308 isClassDict (Dict _ (Class _ _) _) = True
309 isClassDict other = False
311 isMethod :: Inst -> Bool
312 isMethod (Method _ _ _ _ _ _) = True
313 isMethod other = False
315 isMethodFor :: TcIdSet -> Inst -> Bool
316 isMethodFor ids (Method uniq id tys _ _ loc)
317 = id `elemVarSet` ids
321 isTyVarDict :: Inst -> Bool
322 isTyVarDict (Dict _ (Class _ tys) _) = all isTyVarTy tys
323 isTyVarDict other = False
325 isStdClassTyVarDict (Dict _ (Class clas [ty]) _)
326 = isStandardClass clas && isTyVarTy ty
327 isStdClassTyVarDict other
330 notFunDep :: Inst -> Bool
331 notFunDep (FunDep _ _ _) = False
332 notFunDep other = True
335 Two predicates which deal with the case where class constraints don't
336 necessarily result in bindings. The first tells whether an @Inst@
337 must be witnessed by an actual binding; the second tells whether an
338 @Inst@ can be generalised over.
341 instBindingRequired :: Inst -> Bool
342 instBindingRequired (Dict _ (Class clas _) _) = not (isNoDictClass clas)
343 instBindingRequired (Dict _ (IParam _ _) _) = False
344 instBindingRequired other = True
346 instCanBeGeneralised :: Inst -> Bool
347 instCanBeGeneralised (Dict _ (Class clas _) _) = not (isCcallishClass clas)
348 instCanBeGeneralised other = True
356 newDicts :: InstOrigin
358 -> NF_TcM s (LIE, [TcId])
360 = tcGetInstLoc orig `thenNF_Tc` \ loc ->
361 newDictsAtLoc loc theta `thenNF_Tc` \ (dicts, ids) ->
362 returnNF_Tc (listToBag dicts, ids)
364 newClassDicts :: InstOrigin
365 -> [(Class,[TcType])]
366 -> NF_TcM s (LIE, [TcId])
367 newClassDicts orig theta
368 = newDicts orig (map (uncurry Class) theta)
370 -- Local function, similar to newDicts,
371 -- but with slightly different interface
372 newDictsAtLoc :: InstLoc
374 -> NF_TcM s ([Inst], [TcId])
375 newDictsAtLoc loc theta =
376 tcGetUniques (length theta) `thenNF_Tc` \ new_uniqs ->
378 mk_dict u pred = Dict u pred loc
379 dicts = zipWithEqual "newDictsAtLoc" mk_dict new_uniqs theta
381 returnNF_Tc (dicts, map instToId dicts)
383 newDictFromOld :: Inst -> Class -> [TcType] -> NF_TcM s Inst
384 newDictFromOld (Dict _ _ loc) clas tys
385 = tcGetUnique `thenNF_Tc` \ uniq ->
386 returnNF_Tc (Dict uniq (Class clas tys) loc)
389 newMethod :: InstOrigin
392 -> NF_TcM s (LIE, TcId)
393 newMethod orig id tys
394 = -- Get the Id type and instantiate it at the specified types
396 (tyvars, rho) = splitForAllTys (idType id)
397 rho_ty = substTy (mkTyVarSubst tyvars tys) rho
398 (theta, tau) = splitRhoTy rho_ty
400 newMethodWithGivenTy orig id tys theta tau `thenNF_Tc` \ meth_inst ->
401 returnNF_Tc (unitLIE meth_inst, instToId meth_inst)
403 instOverloadedFun orig (HsVar v) arg_tys theta tau
404 = newMethodWithGivenTy orig v arg_tys theta tau `thenNF_Tc` \ inst ->
405 instFunDeps orig theta `thenNF_Tc` \ fds ->
406 returnNF_Tc (HsVar (instToId inst), mkLIE (inst : fds))
408 instFunDeps orig theta
409 = tcGetInstLoc orig `thenNF_Tc` \ loc ->
410 let ifd (Class clas tys) =
411 let fds = instantiateFdClassTys clas tys in
412 if null fds then Nothing else Just (FunDep clas fds loc)
414 in returnNF_Tc (catMaybes (map ifd theta))
416 newMethodWithGivenTy orig id tys theta tau
417 = tcGetInstLoc orig `thenNF_Tc` \ loc ->
418 newMethodWith id tys theta tau loc
420 newMethodWith id tys theta tau loc
421 = tcGetUnique `thenNF_Tc` \ new_uniq ->
422 returnNF_Tc (Method new_uniq id tys theta tau loc)
424 newMethodAtLoc :: InstLoc
426 -> NF_TcM s (Inst, TcId)
427 newMethodAtLoc loc real_id tys -- Local function, similar to newMethod but with
428 -- slightly different interface
429 = -- Get the Id type and instantiate it at the specified types
430 tcGetUnique `thenNF_Tc` \ new_uniq ->
432 (tyvars,rho) = splitForAllTys (idType real_id)
433 rho_ty = ASSERT( length tyvars == length tys )
434 substTy (mkTopTyVarSubst tyvars tys) rho
435 (theta, tau) = splitRhoTy rho_ty
436 meth_inst = Method new_uniq real_id tys theta tau loc
438 returnNF_Tc (meth_inst, instToId meth_inst)
441 In newOverloadedLit we convert directly to an Int or Integer if we
442 know that's what we want. This may save some time, by not
443 temporarily generating overloaded literals, but it won't catch all
444 cases (the rest are caught in lookupInst).
447 newOverloadedLit :: InstOrigin
450 -> NF_TcM s (TcExpr, LIE)
451 newOverloadedLit orig (OverloadedIntegral i) ty
452 | isIntTy ty && inIntRange i -- Short cut for Int
453 = returnNF_Tc (int_lit, emptyLIE)
455 | isIntegerTy ty -- Short cut for Integer
456 = returnNF_Tc (integer_lit, emptyLIE)
459 intprim_lit = HsLitOut (HsIntPrim i) intPrimTy
460 integer_lit = HsLitOut (HsInt i) integerTy
461 int_lit = mkHsConApp intDataCon [] [intprim_lit]
463 newOverloadedLit orig lit ty -- The general case
464 = tcGetInstLoc orig `thenNF_Tc` \ loc ->
465 tcGetUnique `thenNF_Tc` \ new_uniq ->
467 lit_inst = LitInst new_uniq lit ty loc
469 returnNF_Tc (HsVar (instToId lit_inst), unitLIE lit_inst)
473 newIPDict name ty loc
474 = tcGetUnique `thenNF_Tc` \ new_uniq ->
475 let d = Dict new_uniq (IParam name ty) loc in
480 instToId :: Inst -> TcId
481 instToId inst = instToIdBndr inst
483 instToIdBndr :: Inst -> TcId
484 instToIdBndr (Dict u (Class clas tys) (_,loc,_))
485 = mkUserLocal (mkDictOcc (getOccName clas)) u (mkDictTy clas tys) loc
486 instToIdBndr (Dict u (IParam n ty) (_,loc,_))
489 instToIdBndr (Method u id tys theta tau (_,loc,_))
490 = mkUserLocal (mkMethodOcc (getOccName id)) u tau loc
492 instToIdBndr (LitInst u list ty loc)
493 = mkSysLocal SLIT("lit") u ty
495 instToIdBndr (FunDep clas fds _)
496 = panic "FunDep escaped!!!"
499 = mkUserLocal (mkIPOcc (getOccName n)) (nameUnique n) (mkPredTy (IParam n ty)) loc
505 Zonking makes sure that the instance types are fully zonked,
506 but doesn't do the same for the Id in a Method. There's no
507 need, and it's a lot of extra work.
510 zonkPred :: TcPredType -> NF_TcM s TcPredType
511 zonkPred (Class clas tys)
512 = zonkTcTypes tys `thenNF_Tc` \ new_tys ->
513 returnNF_Tc (Class clas new_tys)
514 zonkPred (IParam n ty)
515 = zonkTcType ty `thenNF_Tc` \ new_ty ->
516 returnNF_Tc (IParam n new_ty)
518 zonkInst :: Inst -> NF_TcM s Inst
519 zonkInst (Dict u pred loc)
520 = zonkPred pred `thenNF_Tc` \ new_pred ->
521 returnNF_Tc (Dict u new_pred loc)
523 zonkInst (Method u id tys theta tau loc)
524 = zonkId id `thenNF_Tc` \ new_id ->
525 -- Essential to zonk the id in case it's a local variable
526 -- Can't use zonkIdOcc because the id might itself be
527 -- an InstId, in which case it won't be in scope
529 zonkTcTypes tys `thenNF_Tc` \ new_tys ->
530 zonkTcThetaType theta `thenNF_Tc` \ new_theta ->
531 zonkTcType tau `thenNF_Tc` \ new_tau ->
532 returnNF_Tc (Method u new_id new_tys new_theta new_tau loc)
534 zonkInst (LitInst u lit ty loc)
535 = zonkTcType ty `thenNF_Tc` \ new_ty ->
536 returnNF_Tc (LitInst u lit new_ty loc)
538 zonkInst (FunDep clas fds loc)
539 = zonkFunDeps fds `thenNF_Tc` \ fds' ->
540 returnNF_Tc (FunDep clas fds' loc)
542 zonkPreds preds = mapNF_Tc zonkPred preds
543 zonkInsts insts = mapNF_Tc zonkInst insts
545 zonkFunDeps fds = mapNF_Tc zonkFd fds
548 = zonkTcTypes ts1 `thenNF_Tc` \ ts1' ->
549 zonkTcTypes ts2 `thenNF_Tc` \ ts2' ->
550 returnNF_Tc (ts1', ts2')
552 zonkTvFunDeps fds = mapNF_Tc zonkFd fds
555 = zonkTcTyVars tvs1 `thenNF_Tc` \ tvs1' ->
556 zonkTcTyVars tvs2 `thenNF_Tc` \ tvs2' ->
557 returnNF_Tc (tvs1', tvs2')
563 ToDo: improve these pretty-printing things. The ``origin'' is really only
564 relevant in error messages.
567 instance Outputable Inst where
568 ppr inst = pprInst inst
570 pprInst (LitInst u lit ty loc)
572 OverloadedIntegral i -> integer i
573 OverloadedFractional f -> rational f,
578 pprInst (Dict u pred loc) = pprPred pred <+> show_uniq u
580 pprInst m@(Method u id tys theta tau loc)
581 = hsep [ppr id, ptext SLIT("at"),
582 brackets (interppSP tys) {- ,
587 pprInst (FunDep clas fds loc)
588 = hsep [ppr clas, ppr fds]
590 tidyPred :: TidyEnv -> TcPredType -> (TidyEnv, TcPredType)
591 tidyPred env (Class clas tys)
592 = (env', Class clas tys')
594 (env', tys') = tidyOpenTypes env tys
595 tidyPred env (IParam n ty)
596 = (env', IParam n ty')
598 (env', ty') = tidyOpenType env ty
600 tidyInst :: TidyEnv -> Inst -> (TidyEnv, Inst)
601 tidyInst env (LitInst u lit ty loc)
602 = (env', LitInst u lit ty' loc)
604 (env', ty') = tidyOpenType env ty
606 tidyInst env (Dict u pred loc)
607 = (env', Dict u pred' loc)
609 (env', pred') = tidyPred env pred
611 tidyInst env (Method u id tys theta tau loc)
612 = (env', Method u id tys' theta tau loc)
613 -- Leave theta, tau alone cos we don't print them
615 (env', tys') = tidyOpenTypes env tys
617 -- this case shouldn't arise... (we never print fundeps)
618 tidyInst env fd@(FunDep clas fds loc)
621 tidyInsts env insts = mapAccumL tidyInst env insts
623 show_uniq u = ifPprDebug (text "{-" <> ppr u <> text "-}")
627 %************************************************************************
629 \subsection[InstEnv-types]{Type declarations}
631 %************************************************************************
634 type InstanceMapper = Class -> InstEnv
637 A @ClassInstEnv@ lives inside a class, and identifies all the instances
638 of that class. The @Id@ inside a ClassInstEnv mapping is the dfun for
641 There is an important consistency constraint between the @MatchEnv@s
642 in and the dfun @Id@s inside them: the free type variables of the
643 @Type@ key in the @MatchEnv@ must be a subset of the universally-quantified
644 type variables of the dfun. Thus, the @ClassInstEnv@ for @Eq@ might
645 contain the following entry:
647 [a] ===> dfun_Eq_List :: forall a. Eq a => Eq [a]
649 The "a" in the pattern must be one of the forall'd variables in
653 data LookupInstResult s
655 | SimpleInst TcExpr -- Just a variable, type application, or literal
656 | GenInst [Inst] TcExpr -- The expression and its needed insts
659 -> NF_TcM s (LookupInstResult s)
663 lookupInst dict@(Dict _ (Class clas tys) loc)
664 = case lookupInstEnv (ppr clas) (classInstEnv clas) tys of
668 subst = mkSubst (tyVarsOfTypes tys) tenv
669 (tyvars, rho) = splitForAllTys (idType dfun_id)
670 ty_args = map subst_tv tyvars
671 dfun_rho = substTy subst rho
672 (theta, tau) = splitRhoTy dfun_rho
673 ty_app = mkHsTyApp (HsVar dfun_id) ty_args
674 subst_tv tv = case lookupSubstEnv tenv tv of
675 Just (DoneTy ty) -> ty
676 -- tenv should bind all the tyvars
679 returnNF_Tc (SimpleInst ty_app)
681 newDictsAtLoc loc theta `thenNF_Tc` \ (dicts, dict_ids) ->
683 rhs = mkHsDictApp ty_app dict_ids
685 returnNF_Tc (GenInst dicts rhs)
687 Nothing -> returnNF_Tc NoInstance
688 lookupInst dict@(Dict _ _ loc) = returnNF_Tc NoInstance
692 lookupInst inst@(Method _ id tys theta _ loc)
693 = newDictsAtLoc loc theta `thenNF_Tc` \ (dicts, dict_ids) ->
694 returnNF_Tc (GenInst dicts (mkHsDictApp (mkHsTyApp (HsVar id) tys) dict_ids))
698 lookupInst inst@(LitInst u (OverloadedIntegral i) ty loc)
699 | isIntTy ty && in_int_range -- Short cut for Int
700 = returnNF_Tc (GenInst [] int_lit)
701 -- GenInst, not SimpleInst, because int_lit is actually a constructor application
703 | isIntegerTy ty -- Short cut for Integer
704 = returnNF_Tc (GenInst [] integer_lit)
706 | in_int_range -- It's overloaded but small enough to fit into an Int
707 = tcLookupValueByKey fromIntClassOpKey `thenNF_Tc` \ from_int ->
708 newMethodAtLoc loc from_int [ty] `thenNF_Tc` \ (method_inst, method_id) ->
709 returnNF_Tc (GenInst [method_inst] (HsApp (HsVar method_id) int_lit))
711 | otherwise -- Alas, it is overloaded and a big literal!
712 = tcLookupValueByKey fromIntegerClassOpKey `thenNF_Tc` \ from_integer ->
713 newMethodAtLoc loc from_integer [ty] `thenNF_Tc` \ (method_inst, method_id) ->
714 returnNF_Tc (GenInst [method_inst] (HsApp (HsVar method_id) integer_lit))
716 in_int_range = inIntRange i
717 intprim_lit = HsLitOut (HsIntPrim i) intPrimTy
718 integer_lit = HsLitOut (HsInt i) integerTy
719 int_lit = mkHsConApp intDataCon [] [intprim_lit]
721 -- similar idea for overloaded floating point literals: if the literal is
722 -- *definitely* a float or a double, generate the real thing here.
723 -- This is essential (see nofib/spectral/nucleic).
725 lookupInst inst@(LitInst u (OverloadedFractional f) ty loc)
726 | isFloatTy ty = returnNF_Tc (GenInst [] float_lit)
727 | isDoubleTy ty = returnNF_Tc (GenInst [] double_lit)
730 = tcLookupValueByKey fromRationalClassOpKey `thenNF_Tc` \ from_rational ->
732 -- The type Rational isn't wired in so we have to conjure it up
733 tcLookupTyConByKey rationalTyConKey `thenNF_Tc` \ rational_tycon ->
735 rational_ty = mkSynTy rational_tycon []
736 rational_lit = HsLitOut (HsFrac f) rational_ty
738 newMethodAtLoc loc from_rational [ty] `thenNF_Tc` \ (method_inst, method_id) ->
739 returnNF_Tc (GenInst [method_inst] (HsApp (HsVar method_id) rational_lit))
742 floatprim_lit = HsLitOut (HsFloatPrim f) floatPrimTy
743 float_lit = mkHsConApp floatDataCon [] [floatprim_lit]
744 doubleprim_lit = HsLitOut (HsDoublePrim f) doublePrimTy
745 double_lit = mkHsConApp doubleDataCon [] [doubleprim_lit]
747 -- there are no `instances' of functional dependencies or implicit params
749 lookupInst _ = returnNF_Tc NoInstance
753 There is a second, simpler interface, when you want an instance of a
754 class at a given nullary type constructor. It just returns the
755 appropriate dictionary if it exists. It is used only when resolving
756 ambiguous dictionaries.
759 lookupSimpleInst :: InstEnv
761 -> [Type] -- Look up (c,t)
762 -> NF_TcM s (Maybe [(Class,[Type])]) -- Here are the needed (c,t)s
764 lookupSimpleInst class_inst_env clas tys
765 = case lookupInstEnv (ppr clas) class_inst_env tys of
766 Nothing -> returnNF_Tc Nothing
769 -> returnNF_Tc (Just (substClasses (mkSubst emptyInScopeSet tenv) theta'))
771 (_, theta, _) = splitSigmaTy (idType dfun)
772 theta' = map (\(Class clas tys) -> (clas,tys)) theta