[m-dev.] type classes and the color blue

David Glen JEFFERY dgj at cs.mu.OZ.AU
Mon Jan 10 14:33:10 AEDT 2000


On 08-Jan-2000, Michael Day <mcda at cat.cs.mu.OZ.AU> wrote:
> 
> Does the following code make any sense at all:
> 
> :- typeclass thing(T) where
> 	[
> 		pred is_blue,
> 		mode is_blue is semidet
> 	].
> 
> :- instance thing(sky) where
> 	[
> 		pred(is_blue/1) is semidet_succeed
> 	].
> 
> :- instance thing(sun) where
> 	[
> 		pred(is_blue/1) is semidet_fail
> 	].

I think you mean `is_blue/0' in both instances.

Anyhow, your typeclass declaration looks mighty suspect. The fact that the
type of the method is_blue/0 does not mention the type variable `T' is the
worry.

Consider the type of the method is_blue/0. It is really a predicate (which
happens to be a class method) with type:

:- pred is_blue <= thing(T).

which doesn't really make much sense. The thing(T) constraint can never 
be satisfied. Perhaps the typeclass decl. above should produce a warning.

> :- pred both_blue(A, B) <= (thing(A), thing(B)).
> :- mode both_blue(in, in) is semidet.
> 
> both_blue(A, B) :- ... what can go here? ...

You can see here the difficulty in applying is_blue/0. eg. if we had:

both_blue(A, B) :- 
	...
	is_blue,
	...

There is no way of knowing which instance of is_blue you are intending to use.

The solution is to make is_blue/0 into is_blue/1:

:- typeclass thing(T) where
[
	pred is_blue(T),
	mode is_blue(in) is semidet
].


dgj
-- 
David Jeffery (dgj at cs.mu.oz.au) | If your thesis is utterly vacuous
PhD student,                    | Use first-order predicate calculus.
Dept. of Comp. Sci. & Soft. Eng.|     With sufficient formality
The University of Melbourne     |     The sheerist banality
Australia                       | Will be hailed by the critics: "Miraculous!"
                                |     -- Anon.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list