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(..),
19 RenamedInstancePragmas(..) )
22 import Inst ( InstanceMapper )
24 import Bag ( bagToList, Bag )
25 import Class ( ClassInstEnv, Class, classBigSig )
26 import Id ( mkDictFunId, Id )
27 import SpecEnv ( emptySpecEnv, addToSpecEnv )
28 import Maybes ( MaybeErr(..), mkLookupFunDef )
29 import Name ( getSrcLoc, Name )
30 import SrcLoc ( SrcLoc )
31 import Type ( mkSigmaTy, mkForAllTys, mkDictTy, mkTyVarTys, instantiateThetaTy,
34 import PprType ( pprConstraint )
35 import Class ( classTyCon )
36 import TyCon ( tyConDataCons )
37 import TyVar ( TyVar, zipTyVarEnv )
38 import Unique ( Unique )
39 import Util ( equivClasses, zipWithEqual, panic{-, pprTrace-}, assertPanic )
43 instance c => k (t tvs) where b
49 [TyVar] -- Type variables, tvs
50 [Type] -- The types at which the class is being instantiated
51 ThetaType -- inst_decl_theta: the original context, c, from the
52 -- instance declaration. It constrains (some of)
54 ThetaType -- dfun_theta: the inst_decl_theta, plus one
55 -- element for each superclass; the "Mark
56 -- Jones optimisation"
58 RenamedMonoBinds -- Bindings, b
59 SrcLoc -- Source location assoc'd with this instance's defn
60 [RenamedSig] -- User pragmas recorded for generating specialised instances
64 %************************************************************************
66 \subsection{Creating instance related Ids}
68 %************************************************************************
70 A tiny function which doesn't belong anywhere else.
71 It makes a nasty mutual-recursion knot if you put it in Class.
74 classDataCon :: Class -> Id
75 classDataCon clas = case tyConDataCons (classTyCon clas) of
76 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
79 %************************************************************************
81 \subsection{Creating instance related Ids}
83 %************************************************************************
86 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
93 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_tys inst_decl_theta
94 = (dfun_id, dfun_theta)
96 (class_tyvars, sc_theta, _, _, _) = classBigSig clas
97 sc_theta' = instantiateThetaTy (zipTyVarEnv class_tyvars inst_tys) sc_theta
99 dfun_theta = case inst_decl_theta of
100 [] -> [] -- If inst_decl_theta is empty, then we don't
101 -- want to have any dict arguments, so that we can
102 -- expose the constant methods.
104 other -> inst_decl_theta ++ sc_theta'
105 -- Otherwise we pass the superclass dictionaries to
106 -- the dictionary function; the Mark Jones optimisation.
108 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_tys)
110 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_tys
114 %************************************************************************
116 \subsection{Converting instance info into suitable InstEnvs}
118 %************************************************************************
121 buildInstanceEnvs :: Bag InstInfo
122 -> NF_TcM s InstanceMapper
124 buildInstanceEnvs info
126 icmp :: InstInfo -> InstInfo -> Ordering
127 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
130 info_by_class = equivClasses icmp (bagToList info)
132 mapNF_Tc buildInstanceEnv info_by_class `thenNF_Tc` \ inst_env_entries ->
134 class_lookup_fn = mkLookupFunDef (==) inst_env_entries emptySpecEnv
136 returnNF_Tc class_lookup_fn
140 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
141 -> NF_TcM s (Class, ClassInstEnv)
143 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
144 = foldrNF_Tc addClassInstance
146 inst_infos `thenNF_Tc` \ class_inst_env ->
147 returnNF_Tc (clas, class_inst_env)
150 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
151 based on information from a single instance declaration. It complains
152 about any overlap with an existing instance.
158 -> NF_TcM s ClassInstEnv
161 (InstInfo clas inst_tyvars inst_tys _ _
164 = -- Add the instance to the class's instance environment
165 case addToSpecEnv class_inst_env inst_tys dfun_id of
166 Failed (ty', dfun_id') -> addErrTc (dupInstErr clas (inst_tys, src_loc)
167 (ty', getSrcLoc dfun_id'))
169 returnNF_Tc class_inst_env
171 Succeeded class_inst_env' -> returnNF_Tc class_inst_env'
175 dupInstErr clas info1@(tys1, locn1) info2@(tys2, locn2)
176 -- Overlapping/duplicate instances for given class; msg could be more glamourous
177 = hang (ptext SLIT("Duplicate or overlapping instance declarations"))
178 4 (sep [ptext SLIT("for") <+> quotes (pprConstraint clas tys1),
179 nest 4 (sep [ptext SLIT("at") <+> ppr locn1,
180 ptext SLIT("and") <+> ppr locn2])])