fjh at cs.mu.oz.au
Sat Sep 27 23:25:32 AEST 1997
Bart Demoen, you wrote:
> Peter Schachte wrote
> > So even when the code for two different directions of the same
> > predicate is entirely different, you may be able to share a lot more
> > code if you code it as one predicate rather than two.
> Sorry for being slow, but I don't understand this: how can the code
> for two different directions of the same predicate be entirely
> different (at the source level) ? Isn't the only way by defining
> another predicate for the other mode ?
Consider my example again:
:- 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 ...
Here the `in(forward)' mode for the first argument in the first mode
declaration tells the compiler that for that mode, the first argument
must be bound to the constant `forward'.
In one sense, the code is the same for both modes.
However, for the first mode, only the first clause can
succeed, and for the second mode, only the second clause
can succeed. The compiler is smart enough to notice this
and generate code accordingly.
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 188.8.131.52 | -- the last words of T. S. Garp.
More information about the users