On Sun, Apr 8, 2012 at 2:06 PM, Jonathan S. Shapiro <[email protected]> wrote:

> The first problem is strictly practical and entirely obvious: two groups
> build libraries, each defines instances of Mumble(char). Years later their
> libraries get linked into a single program, and Kaboom.

Right. That is why type-class instances are supposed to be declare
either in the module that defines the type or in the module that
defines the class. That avoids the problem.

Unfortunately, that is not always practical -- so it is not really a
solution. Sometimes library A defines a type and library B defines a
type-class, and you want an instance of that type for the type-class,
but neither A nor B provides it. If you can't convince the authors of
library A or B to add the instance, then you have to create an orphan
instance (an instance declare outsided of library A or B), which
generates a warning because of the potential for future Kaboom. In
practice, I have only had to do that a few times. And, when I have,
the only times it has broken is when the library A or B finally did
add the instance, in which case, I just deleted my orphan. So, even
though it is wrong.. it works well enough that we continue to live
with it.

Both the problems you mention are certainly well-known deficiencies of
type-classes. But, we continue to use type-classes because they are
still so darn useful, and the problem cases arise infrequently enough
that they are not show stoppers. But, the problems do arise enough to
be a constant source of minor irritation.

Maybe a solution is something more like what Agda does, which is
similar to OCaml module functors. In Agda you have a MonadPlus module
instead of a MonadPlus class:

> module RawMonadPlus {M : Set → Set} (Mon : RawMonadPlus M) where
>   ...

And then when you open the module you instantiate things then:

> open RawMonadPlus  (State.StateTMonadPlus  (List Char) Maybe.monadPlus) using 
> (return ; _>>=_ ; _>>_ ; ∅ ; _∣_ )

In Agda, you can open a module at the top-level, but you can also open
it over a limited scope such as a let expression.

I'm leaving out a lot of details here.. because I have forgotten them,
or never knew them in the first place. This is really more of a
question than an answer.. I have often wondered if Agda's solution
would ultimately make me happier than Haskell's type-classes.

- jeremy

_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to