X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcDeriv.lhs;h=b973ec4a2cef1d64a0070b1bc34120d4aebd10fb;hb=970d5b88b1554bbdd7e459dae41aab3668ae897a;hp=de0f133a8481df3349852a2645a502666de07e17;hpb=84923cc7de2a93c22a2f72daf9ac863959efae13;p=ghc-hetmet.git diff --git a/compiler/typecheck/TcDeriv.lhs b/compiler/typecheck/TcDeriv.lhs index de0f133..b973ec4 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} %************************************************************************ @@ -381,6 +383,8 @@ deriveTyData (deriv_pred, L loc decl@(TyData { tcdLName = L _ tycon_name, -- The "deriv_pred" is a LHsType to take account of the fact that for -- newtype deriving we allow deriving (forall a. C [a]). ; mkEqnHelp DerivOrigin (tvs++deriv_tvs) cls cls_tys tc_app } } +deriveTyData (deriv_pred, other_decl) + = panic "derivTyData" -- Caller ensures that only TyData can happen ------------------------------------------------------------------ mkEqnHelp orig tvs cls cls_tys tc_app @@ -393,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 @@ -410,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} + %************************************************************************ %* * @@ -419,7 +447,8 @@ baleOut err = addErrTc err >> returnM (Nothing, Nothing) \begin{code} mkDataTypeEqn orig gla_exts tvs cls cls_tys tycon tc_args rep_tc rep_tc_args - | Just err <- checkSideConditions gla_exts cls cls_tys tycon tc_args + | Just err <- checkSideConditions gla_exts cls cls_tys rep_tc + -- NB: pass the *representation* tycon to checkSideConditions = baleOut (derivingThingErr cls cls_tys (mkTyConApp tycon tc_args) err) | otherwise @@ -464,15 +493,19 @@ mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args ------------------------------------------------------------------ -- Check side conditions that dis-allow derivability for particular classes -- This is *apart* from the newtype-deriving mechanism +-- +-- Here we get the representation tycon in case of family instances as it has +-- the data constructors - but we need to be careful to fall back to the +-- family tycon (with indexes) in error messages. -checkSideConditions :: Bool -> Class -> [TcType] -> TyCon -> [TcType] -> Maybe SDoc -checkSideConditions gla_exts cls cls_tys tycon tc_tys +checkSideConditions :: Bool -> Class -> [TcType] -> TyCon -> Maybe SDoc +checkSideConditions gla_exts cls cls_tys rep_tc | notNull cls_tys = Just ty_args_why -- e.g. deriving( Foo s ) | otherwise = case [cond | (key,cond) <- sideConditions, key == getUnique cls] of [] -> Just (non_std_why cls) - [cond] -> cond (gla_exts, tycon) + [cond] -> cond (gla_exts, rep_tc) other -> pprPanic "checkSideConditions" (ppr cls) where ty_args_why = quotes (ppr (mkClassPred cls cls_tys)) <+> ptext SLIT("is not a class") @@ -492,7 +525,11 @@ sideConditions (dataClassKey, cond_glaExts `andCond` cond_std) ] -type Condition = (Bool, TyCon) -> Maybe SDoc -- Nothing => OK +type Condition = (Bool, TyCon) -> Maybe SDoc + -- Bool is gla-exts flag + -- TyCon is the *representation* tycon if the + -- data type is an indexed one + -- Nothing => OK orCond :: Condition -> Condition -> Condition orCond c1 c2 tc @@ -508,66 +545,66 @@ andCond c1 c2 tc = case c1 tc of Just x -> Just x -- c1 fails cond_std :: Condition -cond_std (gla_exts, tycon) +cond_std (gla_exts, rep_tc) | any (not . isVanillaDataCon) data_cons = Just existential_why | null data_cons = Just no_cons_why | otherwise = Nothing where - data_cons = tyConDataCons tycon - no_cons_why = quotes (ppr tycon) <+> ptext SLIT("has no data constructors") - existential_why = quotes (ppr tycon) <+> ptext SLIT("has non-Haskell-98 constructor(s)") + data_cons = tyConDataCons rep_tc + no_cons_why = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("has no data constructors") + existential_why = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("has non-Haskell-98 constructor(s)") cond_isEnumeration :: Condition -cond_isEnumeration (gla_exts, tycon) - | isEnumerationTyCon tycon = Nothing - | otherwise = Just why +cond_isEnumeration (gla_exts, rep_tc) + | isEnumerationTyCon rep_tc = Nothing + | otherwise = Just why where - why = quotes (ppr tycon) <+> ptext SLIT("has non-nullary constructors") + why = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("has non-nullary constructors") cond_isProduct :: Condition -cond_isProduct (gla_exts, tycon) - | isProductTyCon tycon = Nothing - | otherwise = Just why +cond_isProduct (gla_exts, rep_tc) + | isProductTyCon rep_tc = Nothing + | otherwise = Just why where - why = quotes (ppr tycon) <+> ptext SLIT("has more than one constructor") + why = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("has more than one constructor") cond_typeableOK :: Condition -- OK for Typeable class -- Currently: (a) args all of kind * -- (b) 7 or fewer args -cond_typeableOK (gla_exts, tycon) - | tyConArity tycon > 7 = Just too_many - | not (all (isSubArgTypeKind . tyVarKind) (tyConTyVars tycon)) +cond_typeableOK (gla_exts, rep_tc) + | tyConArity rep_tc > 7 = Just too_many + | not (all (isSubArgTypeKind . tyVarKind) (tyConTyVars rep_tc)) = Just bad_kind - | isFamInstTyCon tycon = Just fam_inst -- no Typable for family insts + | isFamInstTyCon rep_tc = Just fam_inst -- no Typable for family insts | otherwise = Nothing where - too_many = quotes (ppr tycon) <+> ptext SLIT("has too many arguments") - bad_kind = quotes (ppr tycon) <+> + too_many = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("has too many arguments") + bad_kind = quotes (pprSourceTyCon rep_tc) <+> ptext SLIT("has arguments of kind other than `*'") - fam_inst = quotes (ppr tycon) <+> ptext SLIT("is a type family") + fam_inst = quotes (pprSourceTyCon rep_tc) <+> + ptext SLIT("is a type family") cond_glaExts :: Condition -cond_glaExts (gla_exts, tycon) | gla_exts = Nothing - | otherwise = Just why +cond_glaExts (gla_exts, _rep_tc) | gla_exts = Nothing + | otherwise = Just why where why = ptext SLIT("You need -fglasgow-exts to derive an instance for this class") -std_class gla_exts clas - = key `elem` derivableClassKeys - || (gla_exts && (key == typeableClassKey || key == dataClassKey)) - where - key = classKey clas - std_class_via_iso clas -- These standard classes can be derived for a newtype -- using the isomorphism trick *even if no -fglasgow-exts* = classKey clas `elem` [eqClassKey, ordClassKey, ixClassKey, boundedClassKey] -- Not Read/Show because they respect the type - -- Not Enum, becuase newtypes are never in Enum + -- Not Enum, because newtypes are never in Enum 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} @@ -584,18 +621,26 @@ mkNewTypeEqn orig gla_exts overlap_flag tvs cls cls_tys tycon tc_args rep_tycon rep_tc_args | can_derive_via_isomorphism && (gla_exts || std_class_via_iso cls) - = do { traceTc (text "newtype deriving:" <+> ppr tycon <+> ppr rep_tys) - ; -- Go ahead and use the isomorphism - dfun_name <- new_dfun_name cls tycon - ; return (Nothing, Just (InstInfo { iSpec = mk_inst_spec dfun_name, - iBinds = NewTypeDerived ntd_info })) } - | std_class gla_exts cls - = mkDataTypeEqn orig gla_exts tvs cls cls_tys tycon tc_args rep_tycon rep_tc_args -- Go via bale-out route - - -- Otherwise its a non-standard instance + = do { traceTc (text "newtype deriving:" <+> ppr tycon <+> ppr rep_tys) + ; -- Go ahead and use the isomorphism + dfun_name <- new_dfun_name cls tycon + ; return (Nothing, Just (InstInfo { iSpec = mk_inst_spec dfun_name, + iBinds = NewTypeDerived ntd_info })) } + + | isNothing mb_std_err -- Use the standard H98 method + = do { loc <- getSrcSpanM + ; eqn <- mk_data_eqn loc orig tvs cls tycon tc_args rep_tycon rep_tc_args + ; return (Just eqn, Nothing) } + + -- Otherwise we can't derive | gla_exts = baleOut cant_derive_err -- Too hard - | otherwise = baleOut non_std_err -- Just complain about being a non-std instance + | otherwise = baleOut std_err -- Just complain about being a non-std instance where + mb_std_err = checkSideConditions gla_exts cls cls_tys rep_tycon + std_err = derivingThingErr cls cls_tys tc_app $ + vcat [fromJust mb_std_err, + ptext SLIT("Try -fglasgow-exts for GHC's newtype-deriving extension")] + -- Here is the plan for newtype derivings. We see -- newtype T a1...an = MkT (t ak+1...an) deriving (.., C s1 .. sm, ...) -- where t is a type, @@ -737,7 +782,7 @@ mkNewTypeEqn orig gla_exts overlap_flag tvs cls cls_tys cant_derive_err = derivingThingErr cls cls_tys tc_app (vcat [ptext SLIT("even with cunning newtype deriving:"), if isRecursiveTyCon tycon then - ptext SLIT("the newtype is recursive") + ptext SLIT("the newtype may be recursive") else empty, if not right_arity then quotes (ppr (mkClassPred cls cls_tys)) <+> ptext SLIT("does not have arity 1") @@ -750,10 +795,6 @@ mkNewTypeEqn orig gla_exts overlap_flag tvs cls cls_tys ptext SLIT("the eta-reduction property does not hold") else empty ]) - - non_std_err = derivingThingErr cls cls_tys tc_app - (vcat [non_std_why cls, - ptext SLIT("Try -fglasgow-exts for GHC's newtype-deriving extension")]) \end{code} @@ -825,11 +866,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 ... @@ -843,7 +884,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 @@ -862,6 +903,7 @@ extendLocalInstEnv dfuns thing_inside ; setGblEnv env' thing_inside } \end{code} + %************************************************************************ %* * \subsection[TcDeriv-normal-binds]{Bindings for the various classes} @@ -942,7 +984,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 @@ -1076,10 +1118,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"), @@ -1087,3 +1127,4 @@ badDerivedPred pred nest 2 (ptext SLIT("Offending constraint:") <+> ppr pred)] \end{code} +