2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
4 \section[TcInstUtil]{Utilities for typechecking instance declarations}
6 The bits common to TcInstDcls and TcDeriv.
16 #include "HsVersions.h"
18 import RnHsSyn ( RenamedMonoBinds, RenamedSig(..) )
20 import CmdLineOpts ( opt_AllowOverlappingInstances )
22 import Inst ( InstanceMapper )
24 import Bag ( bagToList, Bag )
25 import Class ( ClassInstEnv, Class, classBigSig )
26 import MkId ( mkDictFunId )
28 import SpecEnv ( emptySpecEnv, addToSpecEnv )
29 import Maybes ( MaybeErr(..), mkLookupFunDef )
30 import Name ( getSrcLoc, Name )
31 import SrcLoc ( SrcLoc )
32 import Type ( mkSigmaTy, mkDictTy, instantiateThetaTy,
35 import PprType ( pprConstraint )
36 import Class ( classTyCon )
37 import TyCon ( tyConDataCons )
38 import TyVar ( TyVar, zipTyVarEnv )
39 import Unique ( Unique )
40 import Util ( equivClasses, panic, assertPanic )
44 instance c => k (t tvs) where b
50 [TyVar] -- Type variables, tvs
51 [Type] -- The types at which the class is being instantiated
52 ThetaType -- inst_decl_theta: the original context, c, from the
53 -- instance declaration. It constrains (some of)
55 ThetaType -- dfun_theta: the inst_decl_theta, plus one
56 -- element for each superclass; the "Mark
57 -- Jones optimisation"
59 RenamedMonoBinds -- Bindings, b
60 SrcLoc -- Source location assoc'd with this instance's defn
61 [RenamedSig] -- User pragmas recorded for generating specialised instances
65 %************************************************************************
67 \subsection{Creating instance related Ids}
69 %************************************************************************
71 A tiny function which doesn't belong anywhere else.
72 It makes a nasty mutual-recursion knot if you put it in Class.
75 classDataCon :: Class -> Id
76 classDataCon clas = case tyConDataCons (classTyCon clas) of
77 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
80 %************************************************************************
82 \subsection{Creating instance related Ids}
84 %************************************************************************
87 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
94 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_tys inst_decl_theta
95 = (dfun_id, dfun_theta)
97 (class_tyvars, sc_theta, _, _, _) = classBigSig clas
98 sc_theta' = instantiateThetaTy (zipTyVarEnv class_tyvars inst_tys) sc_theta
100 dfun_theta = case inst_decl_theta of
101 [] -> [] -- If inst_decl_theta is empty, then we don't
102 -- want to have any dict arguments, so that we can
103 -- expose the constant methods.
105 other -> inst_decl_theta ++ sc_theta'
106 -- Otherwise we pass the superclass dictionaries to
107 -- the dictionary function; the Mark Jones optimisation.
109 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_tys)
111 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_tys
115 %************************************************************************
117 \subsection{Converting instance info into suitable InstEnvs}
119 %************************************************************************
122 buildInstanceEnvs :: Bag InstInfo
123 -> NF_TcM s InstanceMapper
125 buildInstanceEnvs info
127 icmp :: InstInfo -> InstInfo -> Ordering
128 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
131 info_by_class = equivClasses icmp (bagToList info)
133 mapNF_Tc buildInstanceEnv info_by_class `thenNF_Tc` \ inst_env_entries ->
135 class_lookup_fn = mkLookupFunDef (==) inst_env_entries emptySpecEnv
137 returnNF_Tc class_lookup_fn
141 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
142 -> NF_TcM s (Class, ClassInstEnv)
144 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
145 = foldrNF_Tc addClassInstance
147 inst_infos `thenNF_Tc` \ class_inst_env ->
148 returnNF_Tc (clas, class_inst_env)
151 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
152 based on information from a single instance declaration. It complains
153 about any overlap with an existing instance.
159 -> NF_TcM s ClassInstEnv
162 (InstInfo clas inst_tyvars inst_tys _ _
165 = -- Add the instance to the class's instance environment
166 case addToSpecEnv opt_AllowOverlappingInstances
167 class_inst_env inst_tyvars inst_tys dfun_id of
168 Failed (ty', dfun_id') -> addErrTc (dupInstErr clas (inst_tys, src_loc)
169 (ty', getSrcLoc dfun_id'))
171 returnNF_Tc class_inst_env
173 Succeeded class_inst_env' -> returnNF_Tc class_inst_env'
177 dupInstErr clas info1@(tys1, locn1) info2@(tys2, locn2)
178 -- Overlapping/duplicate instances for given class; msg could be more glamourous
179 = hang (ptext SLIT("Duplicate or overlapping instance declarations"))
180 4 (sep [ptext SLIT("for") <+> quotes (pprConstraint clas tys1),
181 nest 4 (sep [ptext SLIT("at") <+> ppr locn1,
182 ptext SLIT("and") <+> ppr locn2])])