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

Jörg Roman Rudnick joerg.rudnick at t-online.de
Sun Oct 8 06:55:05 AEST 2006


Hi Ralph,


it seems you did not promise too much constants/factory methods are 
running fine, and I my made considerable progress extraction and 
insertion. :-)

But a question remains: Frankly, I assume 'naked' type variables are not 
acceptable by the instance declarations, so that the element type of a 
collection needs either to be ground or some parametrized type. Ok, ok...

But how about letting the collection be parametrized by the same type -- 
e.g., in the example below 'list(container(T))'??

Of course, such a nesting appears illegal, 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?

Thank you very much in advance and a nice weekend,


Nick


(see also attachment)

%%% snip %%%%%%%%%%%%%%%%%%%%%%%%%%%%
:-module fringe.
:-interface.
:-import_module io.
:-pred main(io::di, io::uo) is det.

:-typeclass fringe(Q, T) <= (Q -> T) where [
func first(Q)= T,
func insert(Q, T)= Q
].

:-type container(T) ---> container(T).

%%===========================
:-implementation.
:-import_module list, exception.

:-instance fringe(list(T), container(T)) where [
func(first/1) is list_first,
func(insert/2) is list_insert
].

:-func list_first(list(T))= container(T).
list_first(List)= First :-
(if List = [F|_Rest] then First = container(F)
else throw("! empty list...")).

:-func list_insert(list(T), container(T))= list(T).
list_insert(List, container(Thinc))= [Thinc|List].

main(!IO) :-
L = [2,3,4],
write_string("first of L: ", !IO),
write(first(L), !IO),
nl(!IO),
write(insert(L, container(1)), !IO),
nl(!IO).
%%% snap %%%%%%%%%%%%%%%%%%%%%%%%%%%%
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fringe.m
Type: text/x-objcsrc
Size: 842 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/users/attachments/20061007/e8094d12/attachment.bin>


More information about the users mailing list