Reputation: 11
I've just started Prolog and truly wonder why the following lines, specifically the 'is' part, always produces false:
highest(L) :-
path(_,_,Z),
Z >= L,
L is Z.
highestWrap :-
highest(0).
highestWrap is called.
Thanks in advance and have a beautiful day!
Upvotes: 1
Views: 272
Reputation: 476527
Unless there is a path with length 0
, this will not work, and even then, it will likely not yield what you want: it will just say true.
In Prolog variables can only be set once, that means that if L
is set to 0
, then it remains 0
, unless you backtrack over that assignment.
Here it thus means that you call highest(0)
, next you instruct Prolog to call path(_, _, Z)
and this can result in zero, one or more solutions. In case there are no path(_, _, Z)
s, then the call will fail. But in case there are, then Z
will (if I make correct assumptions about the predicate), have a numerical value, for example 7
.
Now the condition Z >= L
of course holds in that case (if Z
is 7
), so that is nog the problem. But now you specify L is Z
. That thus means that you call 0 is 7
. The is/2
[swi-doc] predicate aims to solve the expression of the second argument (that expression is 7
in the example, so there is not much to solve), and then aims to unify it with the term on the left side. But since 0
is not equal to 7
that fails.
If you want to obtain the highest path, you can for example make use of the aggregate
[swi-doc] library:
:- use_module(library(aggregate)).
highest(L) :-
aggregate(Max(Z), path(_,_,Z), Max(L)).
You can then call it with higest(X)
to unify X
with the highest value for Z
in a call to path(_, _, Z)
.
Upvotes: 1