1 回答
So, as @lurker said, this is the result of choice points - cases where there is a rule that has not been evaluated, which may yield more solutions.
Let's go through an even simpler version of your example, max([0,1],X).
vs max([1,0],X).
.
max([0,1],X).
:
This goes to acc_max([1],0,X).
, which matches both acc_max([H|T], A, Max) :-
rules. We evaluate them in the order that they appear:
First we see that 1 > 0
is true, and call acc_max([],1,X)
. This matches only acc_max([], A, A).
, and we therefore unify X with 1. We have a solution! But we also have a rule that has not been evaluated yet. This is where you see:
X = 1 ▮
So now we type ;, and evaluate the second acc_max([H|T], A, Max) :-
rule. We see that 1 =< 0
is not true, so this rule fails. We are now out of rules to try, so it turns out there are no more solutions. Hence:
X = 1 ;
false.
Now we'll look at max([1,0],X).
:
This is now acc_max([0],1,X).
. Again, we have two rules, evaluated in the order they appear:
First we see that 0 > 1
is not true, so the first rule fails and we evaluate the second.
Now we see that 0 =< 1
is true, and call acc_max([],1,X)
. This matches only acc_max([], A, A).
, and we therefore unify X with 1 (again). We have the solution, and this time we have no unevaluated rules (ie. no unexplored choice points). So now we see:
X = 1.
... as there is no doubt in Prolog's "mind" that there are no other solutions. If you were to invert the order of the rules:
acc_max([], A, A).
acc_max([H|T], A, Max) :- H =< A, acc_max(T, A, Max).
acc_max([H|T], A, Max) :- H > A, acc_max(T, H, Max).
... you would see the behaviour invert as well.
Hopefully this helps to demonstrate that this is a consistent and predictable interaction, and should be one shared by all variants of Prolog.