Can someone explain to me why is:
notmarried(P) :- \+(married(P)), male(P).
different then:
notmarried(P) :- male(P), \+(married(P)).
The subject is that a variable in prolog can be bound to some value (X=foo) or unbound (not yet known value).
Now, assume the following facts:
married(tom).
married(john).
What must be after "not married" in
\+married(P), male(P)
?
P can be any value except "tom" or "john". But prolog has no way to store this fact in "P" (not with the basic statements). So, the result of "not married" is "yes, it is possible there some people not married" and P unbound. With P unbound, male(P) takes the first male, and we have the first answer.
Now, the second query:
male(P), \+married(P).
After male, prolog will bound P to one of the males. Now, it will check if this male is married or not, answering yes/not. In case of not, it will backtract to another male, and so on.
\+/1is generally only sound if its argument is ground. For example:?- \+ married(P), P = test.fails if there is a married person, regardless of whether or not?- married(test).itself succeeds. In contrast,?- P = test, \+ married(P).and hence?- \+ married(test).succeeds unless?- married(test).succeeds. Thus, better use\+/1only if its argument is ground (second version). - mat