Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

What is the logical 'not' in Prolog?

The problem that I face, is a bit trivial. I want to use logical not in Prolog, but it seems that not/1 is not the thing that I want:

course(ai).
course(pl).
course(os).

have(X,Y) :- course(X),course(Y),not(X = Y).

I query:

have(X,Y), write(X-Y), nl , fail.

And I do not get the result I want :(

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In place of not(X = Y) you need to write + X = Y or X = Y. But consider to use dif(X,Y) instead. dif/2 is present in B, SWI, YAP, SICStus. To see the difference:

?- X = b, dif(a, X).
X = b.

?- X = b, + a = X.
X = b.

So up to now everything seems to be fine. But what, if we simply exchange the order of the two goals?

?- + a = X, X = b.
false.

?- dif(a, X), X = b.
X = b.

(+)/1 now gives us a different result, because there is an answer for a = X, the goal + a = X will fail.

(+)/1 is thus not negation, but means not provable at this point in time.

A safe approximation of dif/2 is possible in ISO Prolog, too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...