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
53 instance c => k (t tvs) where b
59 [TyVar] -- Type variables, tvs
60 Type -- The type at which the class is being instantiated
61 ThetaType -- inst_decl_theta: the original context, c, from the
62 -- instance declaration. It constrains (some of)
64 ThetaType -- dfun_theta: the inst_decl_theta, plus one
65 -- element for each superclass; the "Mark
66 -- Jones optimisation"
68 RenamedMonoBinds -- Bindings, b
69 SrcLoc -- Source location assoc'd with this instance's defn
70 [RenamedSig] -- User pragmas recorded for generating specialised instances
73 %************************************************************************
75 \subsection{Creating instance related Ids}
77 %************************************************************************
80 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
85 -> NF_TcM s (Id, ThetaType)
87 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_ty inst_decl_theta
88 = tcAddImportedIdInfo dfun_id `thenNF_Tc` \ new_dfun_id ->
89 returnNF_Tc (new_dfun_id, dfun_theta)
91 (_, super_classes, _, _, _, _) = classBigSig clas
92 super_class_theta = super_classes `zip` repeat inst_ty
94 dfun_theta = case inst_decl_theta of
95 [] -> [] -- If inst_decl_theta is empty, then we don't
96 -- want to have any dict arguments, so that we can
97 -- expose the constant methods.
99 other -> inst_decl_theta ++ super_class_theta
100 -- Otherwise we pass the superclass dictionaries to
101 -- the dictionary function; the Mark Jones optimisation.
103 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_ty)
105 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_ty
109 %************************************************************************
111 \subsection{Converting instance info into suitable InstEnvs}
113 %************************************************************************
116 buildInstanceEnvs :: Bag InstInfo
117 -> TcM s InstanceMapper
119 buildInstanceEnvs info
121 icmp :: InstInfo -> InstInfo -> TAG_
122 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
125 info_by_class = equivClasses icmp (bagToList info)
127 mapTc buildInstanceEnv info_by_class `thenTc` \ inst_env_entries ->
129 class_lookup_fn = mkLookupFunDef (==) inst_env_entries
130 (nullMEnv, \ o -> nullSpecEnv)
132 returnTc class_lookup_fn
136 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
137 -> TcM s (Class, (ClassInstEnv, (ClassOp -> SpecEnv)))
139 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
140 = foldlTc addClassInstance
141 (nullMEnv, [(op, nullSpecEnv) | op <- classOps clas])
143 `thenTc` \ (class_inst_env, op_inst_envs) ->
144 returnTc (clas, (class_inst_env,
145 mkLookupFunDef (==) op_inst_envs
146 (panic "buildInstanceEnv")))
149 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
150 based on information from a single instance declaration. It complains
151 about any overlap with an existing instance.
155 :: (ClassInstEnv, [(ClassOp,SpecEnv)])
157 -> TcM s (ClassInstEnv, [(ClassOp,SpecEnv)])
160 input_stuff@(class_inst_env, op_spec_envs)
161 (InstInfo clas inst_tyvars inst_ty _ _
165 -- We only add specialised/overlapped instances
166 -- if we are specialising the overloading
167 -- ToDo ... This causes getConstMethodId errors!
169 -- if not (is_plain_instance inst_ty) && not opt_SpecialiseOverloaded
171 -- -- Drop this specialised/overlapped instance
172 -- returnTc (class_inst_env, op_spec_envs)
175 -- Add the instance to the class's instance environment
176 case insertMEnv matchTy class_inst_env inst_ty dfun_id of {
177 Failed (ty', dfun_id') -> recoverTc (returnTc input_stuff) $
178 dupInstFailure clas (inst_ty, src_loc)
179 (ty', getSrcLoc dfun_id');
180 Succeeded class_inst_env' ->
182 returnTc (class_inst_env', op_spec_envs)
184 {- OLD STUFF FOR CONSTANT METHODS
186 -- If there are any constant methods, then add them to
187 -- the SpecEnv of each class op (ie selector)
189 -- Example. class Foo a where { op :: Baz b => a -> b; ... }
190 -- instance Foo (p,q) where { op (x,y) = ... ; ... }
192 -- The class decl means that
193 -- op :: forall a. Foo a => forall b. Baz b => a -> b
195 -- The constant method from the instance decl will be:
196 -- op_Pair :: forall p q b. Baz b => (p,q) -> b
198 -- What we put in op's SpecEnv is
199 -- (p,q) |--> (\d::Foo (p,q) -> op_Pair p q)
201 -- Here, [p,q] are the inst_tyvars, and d is a dict whose only
202 -- purpose is to cancel with the dict to which op is applied.
204 -- NOTE THAT this correctly deals with the case where there are
205 -- constant methods even though there are type variables in the
206 -- instance declaration.
208 tcGetUnique `thenNF_Tc` \ uniq ->
210 dict = mkSysLocal SLIT("dict_tpl") uniq (mkDictTy clas inst_ty) src_loc
211 -- Slightly disgusting, but it's only a placeholder for
212 -- a dictionary to be chucked away.
214 op_spec_envs' | null const_meth_ids = op_spec_envs
215 | otherwise = zipWithEqual "add_const_meth" add_const_meth op_spec_envs const_meth_ids
217 add_const_meth (op,spec_env) meth_id
218 = (op, case addOneToSpecEnv spec_env [inst_ty] rhs of
219 Failed (tys', rhs') -> panic "TcInstDecls:add_const_meth"
220 Succeeded spec_env' -> spec_env' )
222 rhs = mkValLam [dict] (mkTyApp (Var meth_id) (mkTyVarTys inst_tyvars))
224 returnTc (class_inst_env', op_spec_envs')
231 dupInstFailure clas info1@(ty1, locn1) info2@(ty2, locn2)
232 -- Overlapping/duplicate instances for given class; msg could be more glamourous
233 = tcAddErrCtxt ctxt $
234 failTc (\sty -> ptext SLIT("Duplicate or overlapping instance declarations"))
236 ctxt sty = hang (sep [ptext SLIT("Class"), ppr sty clas,
237 ptext SLIT("type"), ppr sty ty1])
238 4 (sep [hcat [ptext SLIT("at "), ppr sty locn1],
239 hcat [ptext SLIT("and "), ppr sty locn2]])