2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[TcInstUtil]{Utilities for typechecking instance declarations}
6 The bits common to TcInstDcls and TcDeriv.
15 #include "HsVersions.h"
17 import RnHsSyn ( RenamedMonoBinds, RenamedSig )
19 import CmdLineOpts ( opt_AllowOverlappingInstances )
21 import TcEnv ( InstEnv, emptyInstEnv, addToInstEnv )
22 import Bag ( bagToList, Bag )
23 import Class ( Class )
24 import Var ( TyVar, Id, idName )
25 import Maybes ( MaybeErr(..), mkLookupFunDef )
26 import Name ( getSrcLoc, nameModule, isLocallyDefined )
27 import SrcLoc ( SrcLoc )
28 import Type ( Type, ClassContext )
29 import PprType ( pprConstraint )
30 import Class ( classTyCon )
31 import DataCon ( DataCon )
32 import TyCon ( tyConDataCons )
33 import Unique ( Unique, getUnique )
34 import Util ( equivClassesByUniq )
38 instance c => k (t tvs) where b
44 [TyVar] -- Type variables, tvs
45 [Type] -- The types at which the class is being instantiated
46 ClassContext -- inst_decl_theta: the original context, c, from the
47 -- instance declaration. It constrains (some of)
50 RenamedMonoBinds -- Bindings, b
51 SrcLoc -- Source location assoc'd with this instance's defn
52 [RenamedSig] -- User pragmas recorded for generating specialised instances
56 %************************************************************************
58 \subsection{Creating instance related Ids}
60 %************************************************************************
62 A tiny function which doesn't belong anywhere else.
63 It makes a nasty mutual-recursion knot if you put it in Class.
66 classDataCon :: Class -> DataCon
67 classDataCon clas = case tyConDataCons (classTyCon clas) of
68 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
71 %************************************************************************
73 \subsection{Converting instance info into suitable InstEnvs}
75 %************************************************************************
78 buildInstanceEnv :: Bag InstInfo -> NF_TcM s InstEnv
80 buildInstanceEnv info = foldrNF_Tc addClassInstance emptyInstEnv (bagToList info)
83 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
84 based on information from a single instance declaration. It complains
85 about any overlap with an existing instance.
94 (InstInfo clas inst_tyvars inst_tys _
97 = -- Add the instance to the class's instance environment
98 case addToInstEnv opt_AllowOverlappingInstances
99 inst_env clas inst_tyvars inst_tys dfun_id of
100 Failed (tys', dfun_id') -> addErrTc (dupInstErr clas (inst_tys, dfun_id)
105 Succeeded inst_env' -> returnNF_Tc inst_env'
109 dupInstErr clas info1@(tys1, dfun1) info2@(tys2, dfun2)
110 -- Overlapping/duplicate instances for given class; msg could be more glamourous
111 = hang (ptext SLIT("Duplicate or overlapping instance declarations"))
112 4 (sep [ptext SLIT("for") <+> quotes (pprConstraint clas tys1),
113 nest 4 (sep [ppr_loc dfun1, ptext SLIT("and") <+> ppr_loc dfun2])])
116 | isLocallyDefined dfun = ptext SLIT("defined at") <+> ppr (getSrcLoc dfun)
117 | otherwise = ptext SLIT("imported from module") <+> quotes (ppr (nameModule (idName dfun)))