Hello, 2011/1/12 Simon Peyton-Jones <simo...@microsoft.com>
> > > True. I don't think the generic default has to be separated from the class > declaration; I think it is important that we can explicitly supply the > context for it. Simon's earlier proposal seemed fine, but I don't understand > how we can differentiate between generic and standard default methods. The > prototypical example is the Eq class: > > class Eq a where > (==) , (/=) :: a -> a -> Bool > > a == b = not (a /= b) > a /= b = not (a == b) > > generic (==) :: (Representable a) => a -> a -> Bool > a == b = genericEqDefault a b > > > There are two default definitions for (==), one H98 and one generic, and we > want it this way. If a user writes > > instance Eq T where (/=) = ... > > > *then the H98 default is used for (==).* *WHY???* However, if the user > writes > > deriving instance Eq T > > > *then the generic default is used for (==),* WHY??? and the H98 default > for (/=). > > > > Whoa!! What makes you say that? In my proposal > > · there is one and only one default method: EITHER a H98 > “polymorphic default” OR a new-style derived default > Ah, then I misunderstood. I thought we wanted to allow H98 defaults as well, so we'd have to deal with the case when we have both. But if we allow only one default definition per method, be it H98 or generic, then this is not a problem, and your suggestion will work fine: class D a where > dop :: a -> a > generic dop :: Representable a => a -> a > dop = ...code... > If everyone finds this acceptable, I will proceed to implement it in this way. In summary: - Generic defaults are marked by the (new) keyword "generic" - Generic defaults have a type which has to be the same as the class method they refer to. The context, however, can differ. - A method can have at most one default declaration, be it H98-style or the new generic default. - A class can have any number of methods with (H98 or generic) defaults. One thing remains: how do we actually specify the derived instance? I think there are two options. For the class D above, we can do deriving instance C T > or instance D T > The second option is justified by dop having a default; generic defaults would work like H98 defaults do in this matter. However, consider a more general case: class C a where > noDef :: a > > h98Def :: a > h98Def = ... > > genDef :: a > generic genDef :: (Representable a) => a > genDef = ... > Now, how do we derive instances of C for the user-defined datatype T? We need to provide a definition for noDef, so we can only do something like instance C T where > noDef = ... -- we need to write this! > h98Def = ... -- if we don't write this line, the H98 default is used > genDef = ... -- if we don't write this line, the generic default is used > The standalone deriving syntax won't work in this case. Another option is to forbid generic deriving in classes which have methods without defaults (be them H98 or generic). Then we can stick to standalone deriving. Thoughts? Cheers, Pedro
_______________________________________________ Cvs-ghc mailing list Cvs-ghc@haskell.org http://www.haskell.org/mailman/listinfo/cvs-ghc