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 TcMonad hiding ( rnMtoTcM )
24 import Inst ( SYN_IE(InstanceMapper) )
26 import Bag ( bagToList )
27 import Class ( GenClass, GenClassOp, SYN_IE(ClassInstEnv),
28 classBigSig, classOps, classOpLocalType,
31 import CoreSyn ( GenCoreExpr(..), mkValLam, mkTyApp )
32 import Id ( GenId, mkDictFunId, mkConstMethodId, mkSysLocal )
33 import MatchEnv ( nullMEnv, insertMEnv )
34 import Maybes ( MaybeErr(..), mkLookupFunDef )
35 import Name ( getSrcLoc, Name{--O only-} )
36 import PprType ( GenClass, GenType, GenTyVar )
38 import SpecEnv ( SpecEnv, nullSpecEnv, addOneToSpecEnv )
39 import SrcLoc ( SrcLoc )
40 import Type ( mkSigmaTy, mkForAllTys, mkDictTy, mkTyVarTys,
41 splitForAllTy, instantiateTy, matchTy, SYN_IE(ThetaType) )
42 import TyVar ( GenTyVar )
43 import Unique ( Unique )
44 import Util ( equivClasses, zipWithEqual, panic{-, pprTrace-} )
47 import IdInfo ( noIdInfo )
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 [Id] -- Constant methods (either all or none)
67 RenamedMonoBinds -- Bindings, b
68 Bool -- True <=> local instance decl
69 Module -- Name of module where this instance defined
70 SrcLoc -- Source location assoc'd with this instance's defn
71 [RenamedSig] -- User pragmas recorded for generating specialised instances
74 %************************************************************************
76 \subsection{Creating instance related Ids}
78 %************************************************************************
81 mkInstanceRelatedIds :: Bool
84 -> RenamedInstancePragmas
90 -> TcM s (Id, ThetaType, [Id])
92 mkInstanceRelatedIds from_here src_loc inst_mod inst_pragmas
93 clas inst_tyvars inst_ty inst_decl_theta uprags
96 dfun_theta = case inst_decl_theta of
97 [] -> [] -- If inst_decl_theta is empty, then we don't
98 -- want to have any dict arguments, so that we can
99 -- expose the constant methods.
101 other -> inst_decl_theta ++ super_class_theta
102 -- Otherwise we pass the superclass dictionaries to
103 -- the dictionary function; the Mark Jones optimisation.
105 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_ty)
107 tcGetUnique `thenNF_Tc` \ dfun_uniq ->
108 fixTc ( \ rec_dfun_id ->
111 tcDictFunPragmas dfun_ty rec_dfun_id inst_pragmas
112 `thenNF_Tc` \ dfun_pragma_info ->
114 dfun_specenv = mkInstSpecEnv clas inst_ty inst_tyvars dfun_theta
115 dfun_id_info = dfun_pragma_info `addInfo` dfun_specenv
118 let dfun_id_info = noIdInfo in -- For now
120 returnTc (mkDictFunId dfun_uniq clas inst_ty dfun_ty from_here src_loc inst_mod dfun_id_info)
121 ) `thenTc` \ dfun_id ->
123 -- pprTrace "DFUN: " (ppr PprDebug dfun_id) $
125 -- MAKE THE CONSTANT-METHOD IDS
126 -- if there are no type variables involved
127 (if (null inst_decl_theta)
129 mapTc mk_const_meth_id class_ops
132 ) `thenTc` \ const_meth_ids ->
134 returnTc (dfun_id, dfun_theta, const_meth_ids)
136 (class_tyvar, super_classes, _, class_ops, _, _) = classBigSig clas
137 tenv = [(class_tyvar, inst_ty)]
139 super_class_theta = super_classes `zip` repeat inst_ty
142 = tcGetUnique `thenNF_Tc` \ uniq ->
143 fixTc (\ rec_const_meth_id ->
146 -- Figure out the IdInfo from the pragmas
147 (case assocMaybe opname_prag_pairs (getName op) of
148 Nothing -> returnTc inline_info
149 Just prag -> tcGenPragmas (Just meth_ty) rec_const_meth_id prag
150 ) `thenNF_Tc` \ id_info ->
152 let id_info = noIdInfo -- For now
154 returnTc (mkConstMethodId uniq clas op inst_ty meth_ty
155 from_here src_loc inst_mod id_info)
158 op_ty = classOpLocalType op
159 meth_ty = mkForAllTys inst_tyvars (instantiateTy tenv op_ty)
161 inline_me = isIn "mkInstanceRelatedIds" op ops_to_inline
162 inline_info = if inline_me
163 then noIdInfo `addInfo_UF` (iWantToBeINLINEd UnfoldAlways)
166 opname_prag_pairs = case inst_pragmas of
167 ConstantInstancePragma _ name_prag_pairs -> name_prag_pairs
168 other_inst_pragmas -> []
170 ops_to_inline = [op | (InlineSig op _) <- uprags]
175 %************************************************************************
177 \subsection{Converting instance info into suitable InstEnvs}
179 %************************************************************************
182 buildInstanceEnvs :: Bag InstInfo
183 -> TcM s InstanceMapper
185 buildInstanceEnvs info
187 icmp :: InstInfo -> InstInfo -> TAG_
188 (InstInfo c1 _ _ _ _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _ _ _ _)
191 info_by_class = equivClasses icmp (bagToList info)
193 mapTc buildInstanceEnv info_by_class `thenTc` \ inst_env_entries ->
195 class_lookup_fn = mkLookupFunDef (==) inst_env_entries
196 (nullMEnv, \ o -> nullSpecEnv)
198 returnTc class_lookup_fn
202 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
203 -> TcM s (Class, (ClassInstEnv, (ClassOp -> SpecEnv)))
205 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _ _ _ _) : _)
206 = foldlTc addClassInstance
207 (nullMEnv, [(op, nullSpecEnv) | op <- classOps clas])
209 `thenTc` \ (class_inst_env, op_inst_envs) ->
210 returnTc (clas, (class_inst_env,
211 mkLookupFunDef (==) op_inst_envs
212 (panic "buildInstanceEnv")))
215 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
216 based on information from a single instance declaration. It complains
217 about any overlap with an existing instance.
221 :: (ClassInstEnv, [(ClassOp,SpecEnv)])
223 -> TcM s (ClassInstEnv, [(ClassOp,SpecEnv)])
226 (class_inst_env, op_spec_envs)
227 (InstInfo clas inst_tyvars inst_ty _ _
228 dfun_id const_meth_ids _ _ _ src_loc _)
231 -- We only add specialised/overlapped instances
232 -- if we are specialising the overloading
233 -- ToDo ... This causes getConstMethodId errors!
235 -- if not (is_plain_instance inst_ty) && not opt_SpecialiseOverloaded
237 -- -- Drop this specialised/overlapped instance
238 -- returnTc (class_inst_env, op_spec_envs)
241 -- Add the instance to the class's instance environment
242 case insertMEnv matchTy class_inst_env inst_ty dfun_id of {
243 Failed (ty', dfun_id') -> dupInstFailure clas (inst_ty, src_loc)
244 (ty', getSrcLoc dfun_id');
245 Succeeded class_inst_env' ->
247 -- If there are any constant methods, then add them to
248 -- the SpecEnv of each class op (ie selector)
250 -- Example. class Foo a where { op :: Baz b => a -> b; ... }
251 -- instance Foo (p,q) where { op (x,y) = ... ; ... }
253 -- The class decl means that
254 -- op :: forall a. Foo a => forall b. Baz b => a -> b
256 -- The constant method from the instance decl will be:
257 -- op_Pair :: forall p q b. Baz b => (p,q) -> b
259 -- What we put in op's SpecEnv is
260 -- (p,q) |--> (\d::Foo (p,q) -> op_Pair p q)
262 -- Here, [p,q] are the inst_tyvars, and d is a dict whose only
263 -- purpose is to cancel with the dict to which op is applied.
265 -- NOTE THAT this correctly deals with the case where there are
266 -- constant methods even though there are type variables in the
267 -- instance declaration.
269 tcGetUnique `thenNF_Tc` \ uniq ->
271 dict = mkSysLocal SLIT("dict_tpl") uniq (mkDictTy clas inst_ty) src_loc
272 -- Slightly disgusting, but it's only a placeholder for
273 -- a dictionary to be chucked away.
275 op_spec_envs' | null const_meth_ids = op_spec_envs
276 | otherwise = zipWithEqual "add_const_meth" add_const_meth op_spec_envs const_meth_ids
278 add_const_meth (op,spec_env) meth_id
279 = (op, case addOneToSpecEnv spec_env [inst_ty] rhs of
280 Failed (tys', rhs') -> panic "TcInstDecls:add_const_meth"
281 Succeeded spec_env' -> spec_env' )
283 rhs = mkValLam [dict] (mkTyApp (Var meth_id) (mkTyVarTys inst_tyvars))
285 returnTc (class_inst_env', op_spec_envs')
290 dupInstFailure clas info1@(ty1, locn1) info2@(ty2, locn2)
291 -- Overlapping/duplicate instances for given class; msg could be more glamourous
292 = tcAddErrCtxt ctxt $
293 failTc (\sty -> ppStr "Duplicate or overlapping instance declarations")
295 ctxt sty = ppHang (ppSep [ppBesides[ppStr "Class `", ppr sty clas, ppStr "'"],
296 ppBesides[ppStr "type `", ppr sty ty1, ppStr "'"]])
297 4 (ppSep [ppBesides [ppStr "at ", ppr sty locn1],
298 ppBesides [ppStr "and ", ppr sty locn2]])