X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcDeriv.lhs;h=e534ebd2030fecf4dc9b42f8cf5b670525ac4e96;hp=ba110790c099e311de80335de2a5bcd0add450e1;hb=ad94d40948668032189ad22a0ad741ac1f645f50;hpb=878924ac03219f02c857b0c3a95bb47a4a427d55 diff --git a/compiler/typecheck/TcDeriv.lhs b/compiler/typecheck/TcDeriv.lhs index ba11079..e534ebd 100644 --- a/compiler/typecheck/TcDeriv.lhs +++ b/compiler/typecheck/TcDeriv.lhs @@ -6,6 +6,13 @@ Handles @deriving@ clauses on @data@ declarations. \begin{code} +{-# OPTIONS -w #-} +-- The above warning supression flag is a temporary kludge. +-- While working on this module you are encouraged to remove it and fix +-- any warnings in the module. See +-- http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings +-- for details + module TcDeriv ( tcDeriving ) where #include "HsVersions.h" @@ -47,8 +54,6 @@ import Util import ListSetOps import Outputable import Bag - -import Monad (unless) \end{code} %************************************************************************ @@ -238,10 +243,11 @@ tcDeriving tycl_decls inst_decls deriv_decls ; gen_binds <- mkGenericBinds tycl_decls -- Rename these extra bindings, discarding warnings about unused bindings etc - -- Set -fglasgow exts so that we can have type signatures in patterns, - -- which is used in the generic binds + -- Type signatures in patterns are used in the generic binds ; rn_binds - <- discardWarnings $ setOptM Opt_GlasgowExts $ do + <- discardWarnings $ + setOptM Opt_PatternSignatures $ + do { (rn_deriv, _dus1) <- rnTopBinds (ValBindsIn deriv_binds []) ; (rn_gen, dus_gen) <- rnTopBinds (ValBindsIn gen_binds []) ; keepAliveSetTc (duDefs dus_gen) -- Mark these guys to @@ -362,17 +368,27 @@ makeDerivEqns tycl_decls inst_decls deriv_decls ------------------------------------------------------------------ deriveStandalone :: LDerivDecl Name -> TcM (Maybe DerivEqn, Maybe InstInfo) -- Standalone deriving declarations --- e.g. derive instance Show T +-- e.g. deriving instance show a => Show (T a) -- Rather like tcLocalInstDecl deriveStandalone (L loc (DerivDecl deriv_ty)) = setSrcSpan loc $ addErrCtxt (standaloneCtxt deriv_ty) $ - do { (tvs, theta, tau) <- tcHsInstHead deriv_ty - ; (cls, inst_tys) <- checkValidInstHead tau - ; let cls_tys = take (length inst_tys - 1) inst_tys - inst_ty = last inst_tys - - ; mkEqnHelp StandAloneDerivOrigin tvs cls cls_tys inst_ty } + do { traceTc (text "standalone deriving decl for" <+> ppr deriv_ty) + ; (tvs, theta, tau) <- tcHsInstHead deriv_ty + ; traceTc (text "standalone deriving;" + <+> text "tvs:" <+> ppr tvs + <+> text "theta:" <+> ppr theta + <+> text "tau:" <+> ppr tau) + ; (cls, inst_tys) <- checkValidInstHead tau + ; let cls_tys = take (length inst_tys - 1) inst_tys + inst_ty = last inst_tys + + ; traceTc (text "standalone deriving;" + <+> text "class:" <+> ppr cls + <+> text "class types:" <+> ppr cls_tys + <+> text "type:" <+> ppr inst_ty) + ; mkEqnHelp StandAloneDerivOrigin tvs cls cls_tys inst_ty + (Just theta) } ------------------------------------------------------------------ deriveTyData :: (LHsType Name, LTyClDecl Name) -> TcM (Maybe DerivEqn, Maybe InstInfo) @@ -391,12 +407,15 @@ deriveTyData (deriv_pred, L loc decl@(TyData { tcdLName = L _ tycon_name, do { (deriv_tvs, cls, cls_tys) <- tcHsDeriv deriv_pred -- 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 } } + ; mkEqnHelp DerivOrigin (tvs++deriv_tvs) cls cls_tys tc_app Nothing } } deriveTyData (deriv_pred, other_decl) = panic "derivTyData" -- Caller ensures that only TyData can happen ------------------------------------------------------------------ -mkEqnHelp orig tvs cls cls_tys tc_app +mkEqnHelp :: InstOrigin -> [TyVar] -> Class -> [Type] -> Type + -> Maybe DerivRhs + -> TcRn (Maybe DerivEqn, Maybe InstInfo) +mkEqnHelp orig tvs cls cls_tys tc_app mtheta | Just (tycon, tc_args) <- tcSplitTyConApp_maybe tc_app = do { -- Make tc_app saturated, because that's what the -- mkDataTypeEqn things expect @@ -408,17 +427,19 @@ mkEqnHelp orig tvs cls cls_tys tc_app ; (rep_tc, rep_tc_args) <- tcLookupFamInstExact tycon full_tc_args - ; gla_exts <- doptM Opt_GlasgowExts + ; mayDeriveDataTypeable <- doptM Opt_DeriveDataTypeable + ; newtype_deriving <- doptM Opt_GeneralizedNewtypeDeriving ; overlap_flag <- getOverlapFlag -- 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 + mkDataTypeEqn orig mayDeriveDataTypeable full_tvs cls cls_tys + tycon full_tc_args rep_tc rep_tc_args mtheta else - mkNewTypeEqn orig gla_exts overlap_flag full_tvs cls cls_tys - tycon full_tc_args rep_tc rep_tc_args } + mkNewTypeEqn orig mayDeriveDataTypeable newtype_deriving overlap_flag + full_tvs cls cls_tys + tycon full_tc_args rep_tc rep_tc_args mtheta } | otherwise = baleOut (derivingThingErr cls cls_tys tc_app (ptext SLIT("Last argument of the instance must be a type application"))) @@ -427,24 +448,29 @@ baleOut err = addErrTc err >> returnM (Nothing, Nothing) \end{code} Auxiliary lookup wrapper which requires that looked up family instances are -not type instances. +not type instances. If called with a vanilla tycon, the old type application +is simply returned. \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 + | not (isOpenTyCon tycon) + = return (tycon, tys) + | otherwise + = do { maybeFamInst <- tcLookupFamInst tycon tys + ; case maybeFamInst of + Nothing -> famInstNotFound tycon tys False + Just famInst@(_, rep_tys) + | not variable_only_subst -> famInstNotFound tycon tys True + | otherwise -> return famInst + where + 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} @@ -455,20 +481,26 @@ tcLookupFamInstExact tycon tys %************************************************************************ \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 rep_tc +mkDataTypeEqn :: InstOrigin -> Bool -> [Var] -> Class -> [Type] + -> TyCon -> [Type] -> TyCon -> [Type] -> Maybe DerivRhs + -> TcRn (Maybe DerivEqn, Maybe InstInfo) +mkDataTypeEqn orig mayDeriveDataTypeable tvs cls cls_tys + tycon tc_args rep_tc rep_tc_args mtheta + | Just err <- checkSideConditions mayDeriveDataTypeable cls cls_tys rep_tc -- NB: pass the *representation* tycon to checkSideConditions = baleOut (derivingThingErr cls cls_tys (mkTyConApp tycon tc_args) err) | otherwise = ASSERT( null cls_tys ) do { loc <- getSrcSpanM - ; eqn <- mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args + ; eqn <- mk_data_eqn loc orig tvs cls tycon tc_args rep_tc + rep_tc_args mtheta ; return (Just eqn, Nothing) } mk_data_eqn :: SrcSpan -> InstOrigin -> [TyVar] -> Class - -> TyCon -> [TcType] -> TyCon -> [TcType] -> TcM DerivEqn -mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args + -> TyCon -> [TcType] -> TyCon -> [TcType] -> Maybe DerivRhs + -> TcM DerivEqn +mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args mtheta | cls `hasKey` typeableClassKey = -- The Typeable class is special in several ways -- data T a b = ... deriving( Typeable ) @@ -481,7 +513,9 @@ mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args -- Typeable; it depends on the arity of the type do { real_clas <- tcLookupClass (typeableClassNames !! tyConArity tycon) ; dfun_name <- new_dfun_name real_clas tycon - ; return (loc, orig, dfun_name, [], real_clas, mkTyConApp tycon [], []) } + ; let theta = fromMaybe [] mtheta + ; return (loc, orig, dfun_name, [], real_clas, mkTyConApp tycon [], theta) + } | otherwise = do { dfun_name <- new_dfun_name cls tycon @@ -491,13 +525,14 @@ mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args arg_ty <- ASSERT( isVanillaDataCon data_con ) dataConInstOrigArgTys data_con rep_tc_args, not (isUnLiftedType arg_ty) ] -- No constraints for unlifted types? + theta = fromMaybe ordinary_constraints mtheta tiresome_subst = zipTopTvSubst (tyConTyVars rep_tc) rep_tc_args stupid_constraints = substTheta tiresome_subst (tyConStupidTheta rep_tc) -- see note [Data decl contexts] above ; return (loc, orig, dfun_name, tvs, cls, mkTyConApp tycon tc_args, - stupid_constraints ++ ordinary_constraints) + stupid_constraints ++ theta) } ------------------------------------------------------------------ @@ -509,13 +544,13 @@ mk_data_eqn loc orig tvs cls tycon tc_args rep_tc rep_tc_args -- family tycon (with indexes) in error messages. checkSideConditions :: Bool -> Class -> [TcType] -> TyCon -> Maybe SDoc -checkSideConditions gla_exts cls cls_tys rep_tc +checkSideConditions mayDeriveDataTypeable 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, rep_tc) + [cond] -> cond (mayDeriveDataTypeable, rep_tc) other -> pprPanic "checkSideConditions" (ppr cls) where ty_args_why = quotes (ppr (mkClassPred cls cls_tys)) <+> ptext SLIT("is not a class") @@ -531,12 +566,12 @@ sideConditions (enumClassKey, cond_std `andCond` cond_isEnumeration), (ixClassKey, cond_std `andCond` (cond_isEnumeration `orCond` cond_isProduct)), (boundedClassKey, cond_std `andCond` (cond_isEnumeration `orCond` cond_isProduct)), - (typeableClassKey, cond_glaExts `andCond` cond_typeableOK), - (dataClassKey, cond_glaExts `andCond` cond_std) + (typeableClassKey, cond_mayDeriveDataTypeable `andCond` cond_typeableOK), + (dataClassKey, cond_mayDeriveDataTypeable `andCond` cond_std) ] type Condition = (Bool, TyCon) -> Maybe SDoc - -- Bool is gla-exts flag + -- Bool is whether or not we are allowed to derive Data and Typeable -- TyCon is the *representation* tycon if the -- data type is an indexed one -- Nothing => OK @@ -555,7 +590,7 @@ andCond c1 c2 tc = case c1 tc of Just x -> Just x -- c1 fails cond_std :: Condition -cond_std (gla_exts, rep_tc) +cond_std (_, rep_tc) | any (not . isVanillaDataCon) data_cons = Just existential_why | null data_cons = Just no_cons_why | otherwise = Nothing @@ -567,7 +602,7 @@ cond_std (gla_exts, rep_tc) ptext SLIT("has non-Haskell-98 constructor(s)") cond_isEnumeration :: Condition -cond_isEnumeration (gla_exts, rep_tc) +cond_isEnumeration (_, rep_tc) | isEnumerationTyCon rep_tc = Nothing | otherwise = Just why where @@ -575,7 +610,7 @@ cond_isEnumeration (gla_exts, rep_tc) ptext SLIT("has non-nullary constructors") cond_isProduct :: Condition -cond_isProduct (gla_exts, rep_tc) +cond_isProduct (_, rep_tc) | isProductTyCon rep_tc = Nothing | otherwise = Just why where @@ -586,7 +621,7 @@ cond_typeableOK :: Condition -- OK for Typeable class -- Currently: (a) args all of kind * -- (b) 7 or fewer args -cond_typeableOK (gla_exts, rep_tc) +cond_typeableOK (_, rep_tc) | tyConArity rep_tc > 7 = Just too_many | not (all (isSubArgTypeKind . tyVarKind) (tyConTyVars rep_tc)) = Just bad_kind @@ -600,9 +635,10 @@ cond_typeableOK (gla_exts, rep_tc) fam_inst = quotes (pprSourceTyCon rep_tc) <+> ptext SLIT("is a type family") -cond_glaExts :: Condition -cond_glaExts (gla_exts, _rep_tc) | gla_exts = Nothing - | otherwise = Just why +cond_mayDeriveDataTypeable :: Condition +cond_mayDeriveDataTypeable (mayDeriveDataTypeable, _) + | mayDeriveDataTypeable = Nothing + | otherwise = Just why where why = ptext SLIT("You need -fglasgow-exts to derive an instance for this class") @@ -627,10 +663,13 @@ new_dfun_name clas tycon -- Just a simple wrapper %************************************************************************ \begin{code} -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) +mkNewTypeEqn :: InstOrigin -> Bool -> Bool -> OverlapFlag -> [Var] -> Class + -> [Type] -> TyCon -> [Type] -> TyCon -> [Type] + -> Maybe DerivRhs + -> TcRn (Maybe DerivEqn, Maybe InstInfo) +mkNewTypeEqn orig mayDeriveDataTypeable newtype_deriving overlap_flag tvs + cls cls_tys tycon tc_args rep_tycon rep_tc_args mtheta + | can_derive_via_isomorphism && (newtype_deriving || 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 @@ -639,17 +678,18 @@ mkNewTypeEqn orig gla_exts overlap_flag tvs cls cls_tys | 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 + ; eqn <- mk_data_eqn loc orig tvs cls tycon tc_args rep_tycon + rep_tc_args mtheta ; return (Just eqn, Nothing) } -- Otherwise we can't derive - | gla_exts = baleOut cant_derive_err -- Too hard + | newtype_deriving = baleOut cant_derive_err -- Too hard | otherwise = baleOut std_err -- Just complain about being a non-std instance where - mb_std_err = checkSideConditions gla_exts cls cls_tys rep_tycon + mb_std_err = checkSideConditions mayDeriveDataTypeable 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")] + ptext SLIT("Try -XGeneralizedNewtypeDeriving 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, ...) @@ -1135,6 +1175,11 @@ badDerivedPred pred = vcat [ptext SLIT("Can't derive instances where the instance context mentions"), ptext SLIT("type variables that are not data type parameters"), nest 2 (ptext SLIT("Offending constraint:") <+> ppr pred)] -\end{code} - +famInstNotFound tycon tys notExact + = failWithTc (msg <+> quotes (pprTypeApp tycon (ppr tycon) tys)) + where + msg = ptext $ if notExact + then SLIT("No family instance exactly matching") + else SLIT("More than one family instance for") +\end{code}