On Thursday, February 17, 2000 3:03 PM, Fergus Henderson 
[SMTP:[EMAIL PROTECTED]] wrote:
>>
> If Haskell had explicit imports and exports of instance declarations,
> then I could perhaps buy this argument.  But it doesn't.  In Haskell,
> all instance declarations defined in a module are always exported;
> there's no way to hide instance declarations that are intended to
> be private to the module:
>

This comes up occasionally. There really should be a FAQ for this sort of 
thing.
It is worth reminding people that there is a good reason for this rule - 
not the original 1988 reason, but good enough.

Originally, this was part of the mechanism that ensured that it was 
impossible to get two different instance declarations for the same 
Class/Type in scope at the same place. The other part of that mechanism, 
insisting that instance declarations could only be declared in modules 
where either the Class or Type were declared, and attaching instances to 
the Class and the Type so that they were exported with them, has 
subsequently been repealed, and it is now simply illegal to have multiple 
instance declarations for the same Class/Type.

If the remaining restriction were also abandoned, and explicit control of 
import/export used to prevent having more than one C-T instance in scope, 
or even if it were just possible to declare a "private" C-T instance which 
shadowed any imported instance and was not exported, there would still be a 
problem.

Consider a function, which is exported, and which uses a local "private" 
instance decl. What happens in the importing module?
There are several possibilities:
1. The "private" instance decl from the other module is used. Then 
referential transparency is lost, because if the name of the imported 
function is replaced by its definition, any need for the instance will not 
see the "private" instance declaration,  and if it finds another instance 
decl in scope, will use that, and the semantics of the function will be 
changed.
2. The local "private" decl is not used, and another decl is in scope. Then 
Haskell no longer has static scope, as the semantics of a function now 
depend on the point of use, not on the point of definition. Rather like 
Original Lisp. Nobody defends that anymore.
3. Even without "private" instances, but with explicit import/export 
control, you can get similar cases where a function defined in one scope is 
exported to another scope with a different C-T instance.  And you can't 
even rely on Library functions behaving as expected either, if you are not 
using the instance decl that was in scope in the Library.

--brian

Reply via email to