On 21-Feb-2000, Brian Boutel <[EMAIL PROTECTED]> wrote:
> On Sunday, February 20, 2000 4:13 PM, Fergus Henderson 
> >
> > Well, you can always defined a type using Tree which _is_ an instance of 
> > Ord:
> >
> >     newtype OrdTree = MkOrdTree Tree
> >     instance Ord OrdTree where ...
> >
> > So I don't see this as a disaster.
> >
> 
> The example of when you might want to hide an instance (of Read, to prevent 
> forgery) is persuasive.
> 
> However, how would you prevent the client following the advice given above 
> of using a newtype declaration to redefine your type, making that an 
> instance of Read, and thereby forging values of your type?

Well, they can certainly use such a newtype declaration,
but doing so won't help them forge values of my type `Tree';
they still need to go via my exported interface to construct
such values.  They can declare this new type to be an instance
of Read, but the implementation of the `readsPrec' method
has no special access to the private parts of my module,
and so it can only construct values of type Tree using the
publically exported interface.

> Actually the situation is worse than I thought. If your module defines a 
> type T to be an instance of Ord, but fails to export that instance, then a) 
> If I can't see the body code of your module, I probably (depending in the 
> interface information) won't even know until link time that I can't declare 
> this instance.

Yes, that is unfortunate.  But it seems to me that it is better to err
on the side of safety by reporting link errors for such cases
than to allow client code to use such an instance when the module
implementor didn't intend to export it.

> b)  I will be unable to declare an instance, not just of 
> Ord, but also of all the many subclasses of Ord.

But the solution for the subclasses is the same as the solution for Ord:
you can always declare a new type with `newtype' and make that type
an instance of Ord and its subclasses.

> ** The Haskell report appears to be silent on this, although it seems 
> obvious that an instance cannot be declared or derived if the type's 
> constructors are not visible.

No, you can certainly declare an instance for an ADT.
It's just that your method definitions must be defined
using that ADT's interface.

-- 
Fergus Henderson <[EMAIL PROTECTED]>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger [EMAIL PROTECTED]        |     -- the last words of T. S. Garp.

Reply via email to