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 )
20 import CmdLineOpts ( opt_AllowOverlappingInstances )
22 import Inst ( InstanceMapper )
24 import Bag ( bagToList, Bag )
25 import Class ( ClassInstEnv, Class, classBigSig )
26 import MkId ( mkDictFunId )
28 import SpecEnv ( emptySpecEnv, addToSpecEnv )
29 import Maybes ( MaybeErr(..), mkLookupFunDef )
30 import Name ( getSrcLoc, Name )
31 import SrcLoc ( SrcLoc )
32 import Type ( mkSigmaTy, mkDictTy, instantiateThetaTy,
35 import PprType ( pprConstraint )
36 import Class ( classTyCon )
37 import TyCon ( tyConDataCons )
38 import TyVar ( TyVar, zipTyVarEnv )
39 import Unique ( Unique )
40 import Util ( equivClasses, panic, assertPanic )
45 instance c => k (t tvs) where b
51 [TyVar] -- Type variables, tvs
52 [Type] -- The types at which the class is being instantiated
53 ThetaType -- inst_decl_theta: the original context, c, from the
54 -- instance declaration. It constrains (some of)
56 ThetaType -- dfun_theta: the inst_decl_theta, plus one
57 -- element for each superclass; the "Mark
58 -- Jones optimisation"
60 RenamedMonoBinds -- Bindings, b
61 SrcLoc -- Source location assoc'd with this instance's defn
62 [RenamedSig] -- User pragmas recorded for generating specialised instances
66 %************************************************************************
68 \subsection{Creating instance related Ids}
70 %************************************************************************
72 A tiny function which doesn't belong anywhere else.
73 It makes a nasty mutual-recursion knot if you put it in Class.
76 classDataCon :: Class -> Id
77 classDataCon clas = case tyConDataCons (classTyCon clas) of
78 (dict_constr:no_more) -> ASSERT( null no_more ) dict_constr
81 %************************************************************************
83 \subsection{Creating instance related Ids}
85 %************************************************************************
88 mkInstanceRelatedIds :: Name -- Name to use for the dict fun;
95 mkInstanceRelatedIds dfun_name clas inst_tyvars inst_tys inst_decl_theta
96 = (dfun_id, dfun_theta)
98 (class_tyvars, sc_theta, _, _, _) = classBigSig clas
99 sc_theta' = instantiateThetaTy (zipTyVarEnv class_tyvars inst_tys) sc_theta
101 dfun_theta = case inst_decl_theta of
102 [] -> [] -- If inst_decl_theta is empty, then we don't
103 -- want to have any dict arguments, so that we can
104 -- expose the constant methods.
106 other -> nub (inst_decl_theta ++ sc_theta')
107 -- Otherwise we pass the superclass dictionaries to
108 -- the dictionary function; the Mark Jones optimisation.
110 -- NOTE the "nub". I got caught by this one:
111 -- class Monad m => MonadT t m where ...
112 -- instance Monad m => MonadT (EnvT env) m where ...
113 -- Here, the inst_decl_theta has (Monad m); but so
114 -- does the sc_theta'!
116 dfun_ty = mkSigmaTy inst_tyvars dfun_theta (mkDictTy clas inst_tys)
118 dfun_id = mkDictFunId dfun_name dfun_ty clas inst_tys
122 %************************************************************************
124 \subsection{Converting instance info into suitable InstEnvs}
126 %************************************************************************
129 buildInstanceEnvs :: Bag InstInfo
130 -> NF_TcM s InstanceMapper
132 buildInstanceEnvs info
134 icmp :: InstInfo -> InstInfo -> Ordering
135 (InstInfo c1 _ _ _ _ _ _ _ _) `icmp` (InstInfo c2 _ _ _ _ _ _ _ _)
138 info_by_class = equivClasses icmp (bagToList info)
140 mapNF_Tc buildInstanceEnv info_by_class `thenNF_Tc` \ inst_env_entries ->
142 class_lookup_fn = mkLookupFunDef (==) inst_env_entries emptySpecEnv
144 returnNF_Tc class_lookup_fn
148 buildInstanceEnv :: [InstInfo] -- Non-empty, and all for same class
149 -> NF_TcM s (Class, ClassInstEnv)
151 buildInstanceEnv inst_infos@((InstInfo clas _ _ _ _ _ _ _ _) : _)
152 = foldrNF_Tc addClassInstance
154 inst_infos `thenNF_Tc` \ class_inst_env ->
155 returnNF_Tc (clas, class_inst_env)
158 @addClassInstance@ adds the appropriate stuff to the @ClassInstEnv@
159 based on information from a single instance declaration. It complains
160 about any overlap with an existing instance.
166 -> NF_TcM s ClassInstEnv
169 (InstInfo clas inst_tyvars inst_tys _ _
172 = -- Add the instance to the class's instance environment
173 case addToSpecEnv opt_AllowOverlappingInstances
174 class_inst_env inst_tyvars inst_tys dfun_id of
175 Failed (ty', dfun_id') -> addErrTc (dupInstErr clas (inst_tys, src_loc)
176 (ty', getSrcLoc dfun_id'))
178 returnNF_Tc class_inst_env
180 Succeeded class_inst_env' -> returnNF_Tc class_inst_env'
184 dupInstErr clas info1@(tys1, locn1) info2@(tys2, locn2)
185 -- Overlapping/duplicate instances for given class; msg could be more glamourous
186 = hang (ptext SLIT("Duplicate or overlapping instance declarations"))
187 4 (sep [ptext SLIT("for") <+> quotes (pprConstraint clas tys1),
188 nest 4 (sep [ptext SLIT("at") <+> ppr locn1,
189 ptext SLIT("and") <+> ppr locn2])])