[mercury-users] Superclasses and "solutions"

Ralph Becket rafe at cs.mu.OZ.AU
Wed Feb 27 12:07:38 AEDT 2002


Kerry Raymond, Wednesday, 27 February 2002:
> 
> What I am wanting to do is to get all the solutions for both types 'a' and 'b'
> (i.e. all instance classes of the type class super). How do I do that?

I'm not clear what it is you want to achieve.

I can think of three possibilities:

(1) You want to identify all *types* that are instances of a given type
class.

* Currently Mercury's RTTI scheme does not support reflection over type
class information, so this is not possible at the moment.

[There are some problems that would benefit from being able to test in
the opposite direction (i.e. decide whether a given value is of a type
that is an instance of a certain type class), but we haven't yet
identified quite how/if this can be done.]

(2) You want to identify all *values* in a program that are of a type
that is an instance of a particular type class.

* This is right out!  And why anyone would want such a mechanism is
unclear.

(3) You want to cast a value of a type that is an instance of a type
class b into a value of a type that is an instance of a super-type class
a.

* This happens automatically when you pass the value from a context in
which it is known to be in b to one which only requires that it be in b
(of course, you can't go in the opposite direction.)

(4) You want to do reflection on the type class hierarchy.

* As in (1), you can't currently do this.  However, you could write code
that records the type class hierarchy and examine that structure.  For
example,

	:- type tch ---> some [T] tch(T) => tc_hierarchy(T).
	:- type tchs == list(tch).

	:- typeclass tc_hierarchy(T) where [
		func supers(T) = tchs
	].

Then every time you define a type class

	:- typeclass a(T) <= (b(T), c(T)) where ...

you also define

	:- type typeclass_a ---> typeclass_a.

	:- instance tc_hierarchy(typeclass_a) where [
		supers(_) = [tch(typeclass_b), tch(typeclass_c)]
	].

But it's hard to see why you would want to do all this.  Can you provide
more detail (e.g. are you trying to mimic SmallTalk style reflection?)

- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list