Tojra
Tojra

Reputation: 683

Negation of absent fact in prolog

let's say I have;

goal(A,B) :- goal1(A,C), \+ goal2(C,B).

but now let's say goal(c,b) cannot be inferred.

If I query, \+ goal(c,b) then I get true but, if I query goal(a,B), then I don't get B=b, because, it doesn't even check for goal2(c,b) (as it can't be inferred). It just checks all present facts goal2(C,B) and picks only those that are false.

Now that is the problem. I want to B=b as the answer when I query something like goal(a,B). Is it possible in prolog. Note that I don't want to insert negative fact like goal(c,b):-false in prolog.

Thanks.

Upvotes: 0

Views: 123

Answers (1)

TA_intern
TA_intern

Reputation: 2422

Attempting to guess from your question, say we have:

p(a, 1).
p(c, 2).
p(d, 1).
p(e, 2).

and you wanted to query all p/2 where the second argument is not 2, you can either do:

?- p(X, Y), Y \== 2.

or:

?- dif(Y, 2), p(X, Y).

Read the docs to see what is the difference. There are many a question here on SO that discuss this.

Upvotes: 1

Related Questions