X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcDeriv.lhs;h=e26c97d2c6ce9189e075f340d76c8e125e85938a;hb=00b6d2567426ec52a113b1d3687e1d61368cafda;hp=58db6185f09ed8de4baaf9cf765350bec1364909;hpb=7edaba2692e2c3c47e24d5746b5cf16233bca6e5;p=ghc-hetmet.git diff --git a/compiler/typecheck/TcDeriv.lhs b/compiler/typecheck/TcDeriv.lhs index 58db618..e26c97d 100644 --- a/compiler/typecheck/TcDeriv.lhs +++ b/compiler/typecheck/TcDeriv.lhs @@ -47,6 +47,8 @@ import Util import ListSetOps import Outputable import Bag + +import Monad (unless) \end{code} %************************************************************************ @@ -395,11 +397,14 @@ mkEqnHelp orig tvs cls cls_tys tc_app full_tc_args = tc_args ++ mkTyVarTys extra_tvs full_tvs = tvs ++ extra_tvs - ; (rep_tc, rep_tc_args) <- tcLookupFamInst tycon full_tc_args + ; (rep_tc, rep_tc_args) <- tcLookupFamInstExact tycon full_tc_args ; gla_exts <- doptM Opt_GlasgowExts ; overlap_flag <- getOverlapFlag - ; if isDataTyCon tycon then + + -- Be careful to test rep_tc here: in the case of families, we want + -- to check the instance tycon, not the family tycon + ; if isDataTyCon rep_tc then mkDataTypeEqn orig gla_exts full_tvs cls cls_tys tycon full_tc_args rep_tc rep_tc_args else @@ -412,6 +417,27 @@ mkEqnHelp orig tvs cls cls_tys tc_app baleOut err = addErrTc err >> returnM (Nothing, Nothing) \end{code} +Auxiliary lookup wrapper which requires that looked up family instances are +not type instances. + +\begin{code} +tcLookupFamInstExact :: TyCon -> [Type] -> TcM (TyCon, [Type]) +tcLookupFamInstExact tycon tys + = do { result@(rep_tycon, rep_tys) <- tcLookupFamInst tycon tys + ; let { tvs = map (Type.getTyVar + "TcDeriv.tcLookupFamInstExact") + rep_tys + ; variable_only_subst = all Type.isTyVarTy rep_tys && + sizeVarSet (mkVarSet tvs) == length tvs + -- renaming may have no repetitions + } + ; unless variable_only_subst $ + famInstNotFound tycon tys [result] + ; return result + } + +\end{code} + %************************************************************************ %* * @@ -453,7 +479,8 @@ mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args ; let ordinary_constraints = [ mkClassPred cls [arg_ty] | data_con <- tyConDataCons rep_tc, - arg_ty <- dataConInstOrigArgTys data_con rep_tc_args, + arg_ty <- ASSERT( isVanillaDataCon data_con ) + dataConInstOrigArgTys data_con rep_tc_args, not (isUnLiftedType arg_ty) ] -- No constraints for unlifted types? tiresome_subst = zipTopTvSubst (tyConTyVars rep_tc) rep_tc_args @@ -543,7 +570,7 @@ cond_isProduct (gla_exts, rep_tc) | isProductTyCon rep_tc = Nothing | otherwise = Just why where - why = (pprSourceTyCon rep_tc) <+> + why = quotes (pprSourceTyCon rep_tc) <+> ptext SLIT("has more than one constructor") cond_typeableOK :: Condition @@ -578,7 +605,7 @@ std_class_via_iso clas -- These standard classes can be derived for a newtype new_dfun_name clas tycon -- Just a simple wrapper - = newDFunName clas [mkTyConApp tycon []] (getSrcLoc tycon) + = newDFunName clas [mkTyConApp tycon []] (getSrcSpan tycon) -- The type passed to newDFunName is only used to generate -- a suitable string; hence the empty type arg list \end{code} @@ -840,11 +867,11 @@ solveDerivEqns overlap_flag orig_eqns gen_soln :: DerivEqn -> TcM [PredType] gen_soln (loc, orig, _, tyvars, clas, inst_ty, deriv_rhs) = setSrcSpan loc $ + addErrCtxt (derivInstCtxt clas [inst_ty]) $ do { theta <- tcSimplifyDeriv orig tyvars deriv_rhs - ; addErrCtxt (derivInstCtxt theta clas [inst_ty]) $ - do { checkNoErrs (checkValidInstance tyvars theta clas [inst_ty]) - -- See Note [Deriving context] - -- If this fails, don't continue + -- checkValidInstance tyvars theta clas [inst_ty] + -- Not necessary; see Note [Exotic derived instance contexts] + -- in TcSimplify -- Check for a bizarre corner case, when the derived instance decl should -- have form instance C a b => D (T a) where ... @@ -858,7 +885,7 @@ solveDerivEqns overlap_flag orig_eqns -- Claim: the result instance declaration is guaranteed valid -- Hence no need to call: -- checkValidInstance tyvars theta clas inst_tys - ; return (sortLe (<=) theta) } } -- Canonicalise before returning the solution + ; return (sortLe (<=) theta) } -- Canonicalise before returning the solution ------------------------------------------------------------------ mk_inst_spec :: DerivEqn -> DerivSoln -> Instance @@ -877,25 +904,6 @@ extendLocalInstEnv dfuns thing_inside ; setGblEnv env' thing_inside } \end{code} -Note [Deriving context] -~~~~~~~~~~~~~~~~~~~~~~~ -With -fglasgow-exts, we allow things like (C Int a) in the simplified -context for a derived instance declaration, because at a use of this -instance, we might know that a=Bool, and have an instance for (C Int -Bool) - -We nevertheless insist that each predicate meets the termination -conditions. If not, the deriving mechanism generates larger and larger -constraints. Example: - data Succ a = S a - data Seq a = Cons a (Seq (Succ a)) | Nil deriving Show - -Note the lack of a Show instance for Succ. First we'll generate - instance (Show (Succ a), Show a) => Show (Seq a) -and then - instance (Show (Succ (Succ a)), Show (Succ a), Show a) => Show (Seq a) -and so on. Instead we want to complain of no instance for (Show (Succ a)). - %************************************************************************ %* * @@ -977,7 +985,7 @@ genInst spec -- In case of a family instance, we need to use the representation -- tycon (after all, it has the data constructors) - ; (tycon, _) <- tcLookupFamInst visible_tycon tyArgs + ; (tycon, _) <- tcLookupFamInstExact visible_tycon tyArgs ; let (meth_binds, aux_binds) = genDerivBinds clas fix_env tycon -- Bring the right type variables into @@ -1111,10 +1119,8 @@ derivingThingErr clas tys ty why standaloneCtxt :: LHsType Name -> SDoc standaloneCtxt ty = ptext SLIT("In the stand-alone deriving instance for") <+> quotes (ppr ty) -derivInstCtxt theta clas inst_tys - = hang (ptext SLIT("In the derived instance:")) - 2 (pprThetaArrow theta <+> pprClassPred clas inst_tys) --- Used for the ...Thetas variants; all top level +derivInstCtxt clas inst_tys + = ptext SLIT("When deriving the instance for") <+> parens (pprClassPred clas inst_tys) badDerivedPred pred = vcat [ptext SLIT("Can't derive instances where the instance context mentions"), @@ -1122,4 +1128,4 @@ badDerivedPred pred nest 2 (ptext SLIT("Offending constraint:") <+> ppr pred)] \end{code} - \ No newline at end of file +