| Alex Ferguson, quoting Phil Wadler, quoting Koen Claessen:
|
| > > At the moment you make an instance of a class that has default definitions
| > > for one of its superclasses, and there is no corresponding instance for
| > > that class, we implicitly insert the specified default instance. If
| > > there are more instances to choose from at that point, we report an error.
|
| > If I read this properly, the intent is to make the meaning of the
| > program depend on the order in which it is compiled.
|
| I'm sure that wasn't what Koen intended, but I see Phil's point: it
| seems to raise the question of what happens if some _later_ (in the
| compilation order) instance declaration gives an explicit definition
| of the much-defaulted method.
| (...)
| However, it's at least possible to give a loud warning, or arguably
| an error, for things like (Lennart's example):
|
| > class (C1 a, C2 a) => C12 a
What Phil says already applies at less complex examples, such as the
following:
module A where
class Foo a where
foo :: a -> Int
class Foo a => Bar a where
bar :: a -> Int
foo = bar
module B where
import A
instance Bar Int where
bar = id
module C where
import A
instance Foo Int where
foo = negate
If you compile module B before module C, the compiler hasn't seen any
instance of Foo Int, so it invokes the default definition, which will be
the identity. Later it will give an error if you try to compile C.
If you compile C first, then the instance is there, and the default
definition is not invoked.
There are two solutions:
- We don't allow instance declarations of superclasses in different
modules then where the instance of the subclass is given. (Isn't this
already done, if so, then I see no problem at all...)
- The dictionaries are created at link-time.
I discussed this issue of instance declarations with Erik Meijer, and we
came to the conclusion that all these problems are caused because of the
fact that in Haskell instance declarations have to be visible everywhere.
This means that even modules can't protect instance declarations.
Maybe we should look at explicitly importable and exportable instance
declarations?
Regards,
Koen.
--
| Koen Claessen, [EMAIL PROTECTED] |
| http://www.cse.ogi.edu/~kcclaess/ |
|------------------------------------------------------|
| Visiting student at OGI, Portland, Oregon, USA. |