Hi Simon,

2011/1/7 Simon Peyton-Jones <simo...@microsoft.com>

>  Several thoughts
>
>
>
> First, about type families:
>
> deriving instance (Representable0 a rep, GenericEq rep) => Eq a where
>   eq = genericEqDefault
>
>  Didn’t we decide to use type families, in the end ?  So this would be
>
>
>
> deriving instance (Representable0 a, GenericEq (Rep a)) => Eq a where
>   eq = genericEqDefault
>
>  where Rep is the type family?
>

Yes. This is orthogonal to my main concern now, though. Regarding this part:


>
> ...
>
>
>
> I think we should just do the same.  Add a fourth choice (well, the third
> might disappear!)   Something like
>
>             class C a where
>
>               op4 :: [a] -> a
>
>               *deriving* op4 = genericOp4Default  -- Syntax?
>
>
>
> Then, in an instance decl, if op4 is specified with a Pedro-deriving spec
> as above in the class decl, and the instance decl gives no defn for op4,
> then we fill in with the specified code.
>
>
>
> Thoughts:
>
> ·         The definition for op4 in the class decl should have form Cxt =>
> [a] -> a, *but there’s no inherent reason that Cxt must be (Representable
> ... ).*  I suppose we could simply infer it.   Then we could say, for
> example,
>
> class C a where
>
>   op4 :: [a] -> a
>
>   deriving op4 = minimum :: Ord a => [a] -> a
>
>
>
>                         instance Ord a => C (T a)
>
>
>
>             The instance would behave exactly as if you’d written
>
>
>
>                         instance Ord a => C (T a) where
>
>                           op4 = minimum
>
>
>
>             which would be fine if (T a) was an instance of Ord.
>
>
If we put the deriving defaults together with the class definition, as
normal defaults and as op4 in your code above, we have a somewhat strange
situation, since the code for op4 can have some "extra context" not easily
visible; |minimum| could be an imported function, for instance.

A naive end-user, wanting to derive C for his/her datatype T, may then write

instance C (T a)
>

hoping that op4 will be filled-in generically, knowing nothing about the Ord
constraint. An error message would probably tell the user that we lack an
instance for Ord a, which would still be very surprising since there might
be no Ord anywhere near the definition of class C!

However, if we force the deriving defaults to be specified separately from
the class, like

derivable instance (Ord a) => C a where
>   op4 = minimum
>

it would hopefully be more clear to end-users that they can only derive C if
they can satisfy the Ord constraint. (Note that in my previous email I wrote
|deriving instance| for the same idea, but this might be confused with
standalone deriving. That was not intentional: this is really something
different, it is a way to indicate what is the implementation for a generic
default method.)

I hope I made it more clear now exactly what my concern is. I believe that
we can implement things the way you suggested; I just want to make sure that
we are aware of the issues it may raise. The way we've implemented this in
UHC (using the DERIVABLE pragma) suffers from the exact same problem, but we
always considered that a temporary solution.


Cheers,
Pedro
_______________________________________________
Cvs-ghc mailing list
Cvs-ghc@haskell.org
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to