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.
9 #include "HsVersions.h"
19 import HsSyn ( MonoBinds, Fake, InPat, Sig )
20 import RnHsSyn ( SYN_IE(RenamedMonoBinds), RenamedSig(..),
21 RenamedInstancePragmas(..) )
23 import TcEnv ( tcAddImportedIdInfo )
25 import Inst ( SYN_IE(InstanceMapper) )
27 import Bag ( bagToList, Bag )
28 import Class ( GenClass, GenClassOp, SYN_IE(ClassInstEnv),
29 classBigSig, classOps, classOpLocalType,
30 SYN_IE(ClassOp), SYN_IE(Class)
32 import CoreSyn ( GenCoreExpr(..), mkValLam, mkTyApp )
33 import Id ( GenId, mkDictFunId, mkConstMethodId, mkSysLocal, SYN_IE(Id) )
34 import MatchEnv ( nullMEnv, insertMEnv )
35 import Maybes ( MaybeErr(..), mkLookupFunDef )
36 import Name ( getSrcLoc, Name{--O only-} )
37 import PprType ( GenClass, GenType, GenTyVar )
39 import SpecEnv ( SpecEnv, nullSpecEnv, addOneToSpecEnv )
40 import SrcLoc ( SrcLoc )
41 import Type ( mkSigmaTy, mkForAllTys, mkDictTy, mkTyVarTys,
42 instantiateTy, matchTy, SYN_IE(ThetaType),
44 import TyVar ( GenTyVar, SYN_IE(TyVar) )
45 import Unique ( Unique )
46 import Util ( equivClasses, zipWithEqual, panic{-, pprTrace-}, Ord3(..) )
48 #if __GLASGOW_HASKELL__ >= 202
54 --import TcPragmas ( tcDictFunPragmas, tcGenPragmas )
57 instance c => k (t tvs) where b
63 [TyVar] -- Type variables, tvs
64 Type -- The type at which the class is being instantiated
65 ThetaType -- inst_decl_theta: the original context, c, from the
66 -- instance declaration. It constrains (some of)
68 ThetaType -- dfun_theta: the inst_decl_theta, plus one
69 -- element for each superclass; the "Mark
70 -- Jones optimisation"
72 RenamedMonoBinds -- Bindings, b
73 SrcLoc -- Source location assoc'd with this instance's defn
74 [RenamedSig] -- User pragmas recorded for generating specialised instances
77 %************************************************************************
79 \subsection{Creating instance related Ids}
81 %************************************************************************
84 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
89 -> NF_TcM s (Id, ThetaType)
91 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_ty inst_decl_theta
92 = tcAddImportedIdInfo dfun_id `thenNF_Tc` \ new_dfun_id ->
93 returnNF_Tc (new_dfun_id, dfun_theta)
95 (_, super_classes, _, _, _, _) = classBigSig clas
96 super_class_theta = super_classes `zip` repeat inst_ty
98 dfun_theta = case inst_decl_theta of
99 [] -> [] -- If inst_decl_theta is empty, then we don't
100 -- want to have any dict arguments, so that we can
101 -- expose the constant methods.
103 other -> inst_decl_theta ++ super_class_theta
104 -- Otherwise we pass the superclass dictionaries to
105 -- the dictionary function; the Mark Jones optimisation.
107 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_ty)
109 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_ty
113 %************************************************************************
115 \subsection{Converting instance info into suitable InstEnvs}
117 %************************************************************************
120 buildInstanceEnvs :: Bag InstInfo
121 -> TcM s InstanceMapper
123 buildInstanceEnvs info
125 icmp :: InstInfo -> InstInfo -> TAG_
126 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
129 info_by_class = equivClasses icmp (bagToList info)
131 mapTc buildInstanceEnv info_by_class `thenTc` \ inst_env_entries ->
133 class_lookup_fn = mkLookupFunDef (==) inst_env_entries
134 (nullMEnv, \ o -> nullSpecEnv)
136 returnTc class_lookup_fn
140 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
141 -> TcM s (Class, (ClassInstEnv, (ClassOp -> SpecEnv)))
143 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
144 = foldlTc addClassInstance
145 (nullMEnv, [(op, nullSpecEnv) | op <- classOps clas])
147 `thenTc` \ (class_inst_env, op_inst_envs) ->
148 returnTc (clas, (class_inst_env,
149 mkLookupFunDef (==) op_inst_envs
150 (panic "buildInstanceEnv")))
153 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
154 based on information from a single instance declaration. It complains
155 about any overlap with an existing instance.
159 :: (ClassInstEnv, [(ClassOp,SpecEnv)])
161 -> TcM s (ClassInstEnv, [(ClassOp,SpecEnv)])
164 input_stuff@(class_inst_env, op_spec_envs)
165 (InstInfo clas inst_tyvars inst_ty _ _
169 -- We only add specialised/overlapped instances
170 -- if we are specialising the overloading
171 -- ToDo ... This causes getConstMethodId errors!
173 -- if not (is_plain_instance inst_ty) && not opt_SpecialiseOverloaded
175 -- -- Drop this specialised/overlapped instance
176 -- returnTc (class_inst_env, op_spec_envs)
179 -- Add the instance to the class's instance environment
180 case insertMEnv matchTy class_inst_env inst_ty dfun_id of {
181 Failed (ty', dfun_id') -> recoverTc (returnTc input_stuff) $
182 dupInstFailure clas (inst_ty, src_loc)
183 (ty', getSrcLoc dfun_id');
184 Succeeded class_inst_env' ->
186 returnTc (class_inst_env', op_spec_envs)
188 {- OLD STUFF FOR CONSTANT METHODS
190 -- If there are any constant methods, then add them to
191 -- the SpecEnv of each class op (ie selector)
193 -- Example. class Foo a where { op :: Baz b => a -> b; ... }
194 -- instance Foo (p,q) where { op (x,y) = ... ; ... }
196 -- The class decl means that
197 -- op :: forall a. Foo a => forall b. Baz b => a -> b
199 -- The constant method from the instance decl will be:
200 -- op_Pair :: forall p q b. Baz b => (p,q) -> b
202 -- What we put in op's SpecEnv is
203 -- (p,q) |--> (\d::Foo (p,q) -> op_Pair p q)
205 -- Here, [p,q] are the inst_tyvars, and d is a dict whose only
206 -- purpose is to cancel with the dict to which op is applied.
208 -- NOTE THAT this correctly deals with the case where there are
209 -- constant methods even though there are type variables in the
210 -- instance declaration.
212 tcGetUnique `thenNF_Tc` \ uniq ->
214 dict = mkSysLocal SLIT("dict_tpl") uniq (mkDictTy clas inst_ty) src_loc
215 -- Slightly disgusting, but it's only a placeholder for
216 -- a dictionary to be chucked away.
218 op_spec_envs' | null const_meth_ids = op_spec_envs
219 | otherwise = zipWithEqual "add_const_meth" add_const_meth op_spec_envs const_meth_ids
221 add_const_meth (op,spec_env) meth_id
222 = (op, case addOneToSpecEnv spec_env [inst_ty] rhs of
223 Failed (tys', rhs') -> panic "TcInstDecls:add_const_meth"
224 Succeeded spec_env' -> spec_env' )
226 rhs = mkValLam [dict] (mkTyApp (Var meth_id) (mkTyVarTys inst_tyvars))
228 returnTc (class_inst_env', op_spec_envs')
235 dupInstFailure clas info1@(ty1, locn1) info2@(ty2, locn2)
236 -- Overlapping/duplicate instances for given class; msg could be more glamourous
237 = tcAddErrCtxt ctxt $
238 failTc (\sty -> ptext SLIT("Duplicate or overlapping instance declarations"))
240 ctxt sty = hang (sep [ptext SLIT("Class"), ppr sty clas,
241 ptext SLIT("type"), ppr sty ty1])
242 4 (sep [hcat [ptext SLIT("at "), ppr sty locn1],
243 hcat [ptext SLIT("and "), ppr sty locn2]])