On Friday, February 18, 2000 7:17 PM, Fergus Henderson 
[SMTP:[EMAIL PROTECTED]] wrote:
> It's just a question of information hiding.
> It lets you declare a type to be an instance of a public type class
> without exporting that fact (and hence exporting those methods).
>
> > In Haskell, you can have at most 1 C-T instance, and that is visible in 
> > every module in the program which can be reached by a chain of
> > import/export declarations from the declaring module.
> > In Mercury you can have at most 1 C-T instance, but there may be more
> > places where it is not visible. There does not seem to be anything you 
can
> > do in those places that you could not do if the C-T instance were 
visible
> > there.
>
> You could say the same about e.g. abstract data types.
> The advantage is that the author of module A can ensure that
> module B will not access the private parts of module A.
> Certainly there's nothing more you could do in module B
> than if those parts were public, indeed what you can do is strictly
> less.  But the whole point of making things private is to *restrict*
> what other modules can do with them.
>

Obviously, in general, information hiding is useful. Here, the specific 
question is about instance declarations.Is there any value in being able to 
hide them?

I think the answer is no, for the following reasons:

1) There is complete control of the visibility of Classes and Types. If C 
or T is not visible at a point in the program, the visibility of the C-T 
instance is immaterial. If C and T are both visible, and the C-T instance 
is not, what have you gained? Can you give an example of when you would 
want to do this?

2) In terms of ADTs, Haskell allows (approximations to) ADTs by allowing a 
type to be exported without its constructors. This by itself gives you a 
sort of ADT with only one possible implementation. If you want ADTs where 
more than one implementation is possible, the mechanism is to use a Class C 
and its operators as the ADT, with various Types T as implementation types, 
and the C-T instance functions as the *public* methods. It is essential 
that they are not hidden. Of course, you can still have private methods, 
which are not exported, but these are not part of the Class.

3) If you do have private instances, then a program which needs to import a 
module with such a private C-T instance is prevented from declaring another 
instance for the same C and T (under the rule that allows only one C-T 
instance per program). Suppose, for example, that there is a type of Tree, 
and you have written a module, with some good stuff that I want to use, but 
in which you declare Tree to be an instance of Ord, but don't export it. 
Then I am prevented from using Tree as an ordered type. This would be a 
disaster.

--brian

Reply via email to