3
votes

Can someone explain to me why is:

notmarried(P) :- \+(married(P)), male(P). 

different then:

notmarried(P) :- male(P), \+(married(P)).
2
\+/1 is 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 \+/1 only if its argument is ground (second version). - mat
Do keep in mind that "statements" are from imperative programming. Prolog has goals, rules, and facts, and not statements. - Enigmativity

2 Answers

2
votes

Good question!

The answer has to do with logical purity: in Prolog negation is implemented as negation-as-failure. In general a goal \+ G states that G cannot be proven at this point of time---not that G is logically false.

As a consequence the conjunctions you wrote may not be commutative.

1
votes

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.