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 )
28 import Class ( GenClass, GenClassOp, SYN_IE(ClassInstEnv),
29 classBigSig, classOps, classOpLocalType,
32 import CoreSyn ( GenCoreExpr(..), mkValLam, mkTyApp )
33 import Id ( GenId, mkDictFunId, mkConstMethodId, mkSysLocal )
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 splitForAllTy, instantiateTy, matchTy, SYN_IE(ThetaType) )
43 import TyVar ( GenTyVar )
44 import Unique ( Unique )
45 import Util ( equivClasses, zipWithEqual, panic{-, pprTrace-} )
48 --import TcPragmas ( tcDictFunPragmas, tcGenPragmas )
51 instance c => k (t tvs) where b
57 [TyVar] -- Type variables, tvs
58 Type -- The type at which the class is being instantiated
59 ThetaType -- inst_decl_theta: the original context, c, from the
60 -- instance declaration. It constrains (some of)
62 ThetaType -- dfun_theta: the inst_decl_theta, plus one
63 -- element for each superclass; the "Mark
64 -- Jones optimisation"
66 RenamedMonoBinds -- Bindings, b
67 SrcLoc -- Source location assoc'd with this instance's defn
68 [RenamedSig] -- User pragmas recorded for generating specialised instances
71 %************************************************************************
73 \subsection{Creating instance related Ids}
75 %************************************************************************
78 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
83 -> NF_TcM s (Id, ThetaType)
85 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_ty inst_decl_theta
86 = tcAddImportedIdInfo dfun_id `thenNF_Tc` \ new_dfun_id ->
87 returnNF_Tc (new_dfun_id, dfun_theta)
89 (_, super_classes, _, _, _, _) = classBigSig clas
90 super_class_theta = super_classes `zip` repeat inst_ty
92 dfun_theta = case inst_decl_theta of
93 [] -> [] -- If inst_decl_theta is empty, then we don't
94 -- want to have any dict arguments, so that we can
95 -- expose the constant methods.
97 other -> inst_decl_theta ++ super_class_theta
98 -- Otherwise we pass the superclass dictionaries to
99 -- the dictionary function; the Mark Jones optimisation.
101 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_ty)
103 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_ty
107 %************************************************************************
109 \subsection{Converting instance info into suitable InstEnvs}
111 %************************************************************************
114 buildInstanceEnvs :: Bag InstInfo
115 -> TcM s InstanceMapper
117 buildInstanceEnvs info
119 icmp :: InstInfo -> InstInfo -> TAG_
120 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
123 info_by_class = equivClasses icmp (bagToList info)
125 mapTc buildInstanceEnv info_by_class `thenTc` \ inst_env_entries ->
127 class_lookup_fn = mkLookupFunDef (==) inst_env_entries
128 (nullMEnv, \ o -> nullSpecEnv)
130 returnTc class_lookup_fn
134 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
135 -> TcM s (Class, (ClassInstEnv, (ClassOp -> SpecEnv)))
137 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
138 = foldlTc addClassInstance
139 (nullMEnv, [(op, nullSpecEnv) | op <- classOps clas])
141 `thenTc` \ (class_inst_env, op_inst_envs) ->
142 returnTc (clas, (class_inst_env,
143 mkLookupFunDef (==) op_inst_envs
144 (panic "buildInstanceEnv")))
147 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
148 based on information from a single instance declaration. It complains
149 about any overlap with an existing instance.
153 :: (ClassInstEnv, [(ClassOp,SpecEnv)])
155 -> TcM s (ClassInstEnv, [(ClassOp,SpecEnv)])
158 input_stuff@(class_inst_env, op_spec_envs)
159 (InstInfo clas inst_tyvars inst_ty _ _
163 -- We only add specialised/overlapped instances
164 -- if we are specialising the overloading
165 -- ToDo ... This causes getConstMethodId errors!
167 -- if not (is_plain_instance inst_ty) && not opt_SpecialiseOverloaded
169 -- -- Drop this specialised/overlapped instance
170 -- returnTc (class_inst_env, op_spec_envs)
173 -- Add the instance to the class's instance environment
174 case insertMEnv matchTy class_inst_env inst_ty dfun_id of {
175 Failed (ty', dfun_id') -> recoverTc (returnTc input_stuff) $
176 dupInstFailure clas (inst_ty, src_loc)
177 (ty', getSrcLoc dfun_id');
178 Succeeded class_inst_env' ->
180 returnTc (class_inst_env', op_spec_envs)
182 {- OLD STUFF FOR CONSTANT METHODS
184 -- If there are any constant methods, then add them to
185 -- the SpecEnv of each class op (ie selector)
187 -- Example. class Foo a where { op :: Baz b => a -> b; ... }
188 -- instance Foo (p,q) where { op (x,y) = ... ; ... }
190 -- The class decl means that
191 -- op :: forall a. Foo a => forall b. Baz b => a -> b
193 -- The constant method from the instance decl will be:
194 -- op_Pair :: forall p q b. Baz b => (p,q) -> b
196 -- What we put in op's SpecEnv is
197 -- (p,q) |--> (\d::Foo (p,q) -> op_Pair p q)
199 -- Here, [p,q] are the inst_tyvars, and d is a dict whose only
200 -- purpose is to cancel with the dict to which op is applied.
202 -- NOTE THAT this correctly deals with the case where there are
203 -- constant methods even though there are type variables in the
204 -- instance declaration.
206 tcGetUnique `thenNF_Tc` \ uniq ->
208 dict = mkSysLocal SLIT("dict_tpl") uniq (mkDictTy clas inst_ty) src_loc
209 -- Slightly disgusting, but it's only a placeholder for
210 -- a dictionary to be chucked away.
212 op_spec_envs' | null const_meth_ids = op_spec_envs
213 | otherwise = zipWithEqual "add_const_meth" add_const_meth op_spec_envs const_meth_ids
215 add_const_meth (op,spec_env) meth_id
216 = (op, case addOneToSpecEnv spec_env [inst_ty] rhs of
217 Failed (tys', rhs') -> panic "TcInstDecls:add_const_meth"
218 Succeeded spec_env' -> spec_env' )
220 rhs = mkValLam [dict] (mkTyApp (Var meth_id) (mkTyVarTys inst_tyvars))
222 returnTc (class_inst_env', op_spec_envs')
229 dupInstFailure clas info1@(ty1, locn1) info2@(ty2, locn2)
230 -- Overlapping/duplicate instances for given class; msg could be more glamourous
231 = tcAddErrCtxt ctxt $
232 failTc (\sty -> ppPStr SLIT("Duplicate or overlapping instance declarations"))
234 ctxt sty = ppHang (ppSep [ppBesides[ppPStr SLIT("Class `"), ppr sty clas, ppChar '\''],
235 ppBesides[ppPStr SLIT("type `"), ppr sty ty1, ppChar '\'']])
236 4 (ppSep [ppBesides [ppPStr SLIT("at "), ppr sty locn1],
237 ppBesides [ppPStr SLIT("and "), ppr sty locn2]])