问题
In the query below, firstly I'm getting X = H128
, where does that come from? Also why is it returning yes? Is it because the variable X
is actually not defined and we are testing for that condition?
?- not(X==3).
X = H128
yes
回答1:
Your query is using an uninstantiated variable (X). When checking whether X is instantiated with the term 3 it (X==3) it fails because X is uninstantiated.
Therefore, not(X==3) will succeed as the prolog engine cannot prove X==3. Your prolog interpreter is thus returning 'yes' (due to the negation as failure approach of the interpreter), and X remains uninstantiated.
That is why the interpreter shows X = H128, where H128 is a dummy uninstantiated variable.
回答2:
What was your original intention? It could be that you wanted to state that X
is not equal to 3. For inequality many Prolog systems offer dif/2
:
?- dif(X,3).
dif(X,3).
In this query we ask for values for X
that are not equal to 3. So which values are not equal? Actually, quite a lot: Think of 1
, 2
, the term 3+3
, c
, the list [2,3,4]
and many more. So giving a concrete answer like X = 4
would exclude many other valid answers. The answer here is however: The query holds for all X
that are not equal to 3. The actual evaluation is therefore delayed to a later moment.
?- dif(X,3), X = 3.
false.
Here we got in a situation where X
got the value 3 - which does not hold.
?- dif(X,3), X = 4.
X = 4.
And here a concrete valid value is accepted, and the restriciton dif(4,3)
is removed.
回答3:
Yes, it is because the variable X
is not bound by the first goal, not(X==3)
. Actually the not/1
metapredicate can never produce a binding, even if it succeeds. That's because success of not
means the inner goal fails. Note that not(X=3)
would fail because X=3
can succeed when X
is free (and can be bound to value 3).
来源:https://stackoverflow.com/questions/6669690/prolog-query-returning-h128