[mercury-users] Typeclasses & parameters

Tomas By tomas at basun.net
Fri May 25 19:26:11 AEST 2007


Hello everybody,

Suppose I have two different types of trees:

:- type tree1(V,E).

:- type tree2(V,E).

where V & E are vertex and edge labels, so it might be
`tree1(string,string)' or enumeration types:

:- type mytree == tree1(myvertex,myedge).
:- type myvertex ---> a ; b ; c.
:- type myedge ---> d ; e ; f.

and I then define a typeclass, in order to write search functions etc
that operate on all trees.

:- typeclass tree(T) where [...].
:- instance tree(tree1(_,_)).
:- instance tree(tree2(_,_)).

and that I also want a procedure such as:

:- pred edge_label(T::in,E::out) is det.

to get the edge label (of whatever type) from the tree (of whatever sort).

Now, how do I tell the compiler that the E in the procedure is the
same as the E in the type class?

Attempt 1:

:- typeclass tree(T,E) where [ pred edge_label(T::in,E::out) is det ].
:- instance tree(tree1(_,E),E).
:- instance tree(tree2(_,E),E).

|Error: types in instance declarations must be functors with distinct
|variables as arguments: tree(tree1(_1, _2), _2).

Attempt 2:

:- typeclass tree(T) where [ pred edge_label(T::in,E::out) is det ].

|In clause for type class method implementation:
|  in argument 2 of predicate `edge_label/2':
|  type error: variable `HeadVar__2' has type `(some [E] E)',
|  expected type was `(some [E] E)'.

Any help appreciated.

/Tomas


--------------------------------------------------------------------------
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the users mailing list