On 24-Feb-2000, Jeffrey R. Lewis <[EMAIL PROTECTED]> wrote:
> The example with polymorphic recursion is a nice example. [...]
> Especially given the above example, I don't think that trying to make overlapping 
>behave
> consistently, regardless of instance scope, is the right approach.

For Mercury, what we have been planning to do is to disallow overlapping
instance declarations, but to allow dynamic type class casts,
which give you a somewhat similar kind of expressive power.

For example, instead of

        class Foo t where
                foo :: t -> Int
        instance Foo t where            -- a catch-all case for everything
                foo = 0         
        instance Bar t => Foo t where   -- a special case for `Bar'
                foo = 1         
        instance Baz t => Foo t where   -- a special case for `Baz'
                foo = 2         
        instance Bar t, Baz t => Foo t where
                foo = 1         -- We need this instance declaration to ensure 
                                -- that the special case for `Bar' takes 
                                -- priority over the special case for `Baz'
                                -- Without this, this case would otherwise
                                -- be ambiguous

you would use something like this:

        foo :: Typeable t => t -> Int
        foo x = if x ::? (Bar t1 => t1) then 1
                else if x ::? (Baz t2 => t2) then 2
                else 0

Here `::?' is a dynamic cast operator; `x ::? t' returns True
iff the type of `x' matches the (possibly class-constrained non-ground)
type `t'.  

Implementing dynamic type class casts would require the implementation
to keep a global table of all the instances for a given type class,
and to search that table whenever you do a dynamic type class cast.
So the efficiency cost here is similar to the cost of doing method calls
using a non-dictionary-passing implementation.  But you only pay
that cost if you're doing dynamic type class casts, not for ordinary
method calls, which can be implemented using the traditional
dictionary-passing scheme.

-- 
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