[mercury-users] Present state of type classes - especially for parametrized types

Jörg Roman Rudnick joerg.rudnick at t-online.de
Sun Oct 8 20:30:37 AEST 2006


Ralph Becket schrieb:
> J?rg Roman Rudnick, Saturday,  7 October 2006:
> The reference manual is quite clear on this one, I thought.  A
> declaration of the form
>
> :- instance my_type_class(T) ...
>
> would indicate that all types are instances of my_type_class since T
> unifies with anything.
>   
I am sorry, I was just a little verbose - I agree the manual is quite 
good so far... :-)
>
> The current rules are as generous as we know how to enforce them.  One
> of the problems with your request is how to handle cases where an
> instance declaration in another module declares
>
> :- instance fringe(list(T), ...) ...
>
> How should the compiler reconcile this with a declaration
>
> :- instance fringe(list(container(T)), ...) ...?
>
>   
Aha... :-)
>> but I hope there are ways to 
>> write such an instance declaration like
>>
>> :-instance fringe(list(T_1), container(T2)) ... ?? ...,
>>
>> so that, maybe by setting a type class for T_1, a T_1 can be handled as 
>> a container(T_2).
>>
>> Has anybody any idea how to realize that?
>>     
>
> Can you solve your problem using functional dependencies?
>   
The short answer: At the moment, no.

The longer answer:

The straightforward way,

	:-instance fringe(list(T1), container(T2)) <= (T1 -> T2) where [ ... ]

leads to:

Error: constraints on instance declarations may only constrain type 
variables and ground types: _1 -> _2.


Another idea is delegating the dependency to a second type class, e.g.,

:-typeclass dependingOn(D1, D2) <= (D1 -> D1) where [].
:-instance dependingOn(string, string) where [].

 :-instance fringe(list(T1), container(T2)) <= dependingOn(T1,T2) where [ ... ]

leading to:
functional dependency not satisfied: type variable T2 occurs in
the range of the functional dependency, but is not in the
domain.


Some further experimentation didn't lead to notable enlightenment.

Although looking somewhat strange, application of containers is a 
compromise not too painful for most things, bat: May I stay optimistic??


All the best + a nice weekend,

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20061008/53f0a8fe/attachment.html>


More information about the users mailing list