[mercury-users] polymorphism

Fergus Henderson fjh at cs.mu.oz.au
Fri Sep 26 14:08:57 AEST 1997

Tomas By, you wrote:
> In general, if I have a procedure with several modes, eg:
>   :- pred stream_tree(stream(T),tree(T)).
>   :- mode stream_tree(in,out) is det.
>   :- mode stream_tree(out,in) is det.
> but I can't use the same code for both modes, how do I write
> the "top-level"?

The way to do this is to pass a argument specifying the direction:

	:- type direction ---> forward ; backward.
	:- inst forward ---> forward.
	:- inst backward ---> backward.

	:- pred stream_tree(direction,stream(T),tree(T)).
	:- mode stream_tree(in(forward),in,out) is det.
	:- mode stream_tree(in(backward),out,in) is det.

	stream_tree(forward, Stream, Tree) :-
		... code for the (in, out) mode ...
	stream_tree(backward, Stream, Tree) :-
		... code for the (out, in) mode ...

Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at         |     -- the last words of T. S. Garp.

More information about the users mailing list