Updated April 5, 2023
Introduction to Prolog not
The prolog not is defined as the logical operator which is used in prolog; its behavior is finer than in other programming languages, it indicates negation in prolog and that can be placed before any goal as a prefix, it uses cut notation to define negation, if the original goal fails then the negation goal succeed and if the original goal succeeds then negation goal fails, it is used to retain the similarity, in modern prolog interpreters the negation in prolog is represented by using the symbol \+ whereas the current prolog interpreters support the older operator ‘not’ as well, it is in-built in most prolog interpreters.
Syntax of Prolog not
By defining ‘not(Goal)’, we can implement negation in prolog, we can define the ‘not’ predicate as written below, ‘not(Goal)’ is true if Goal is not true. If the Goal is successful, then ‘not(Goal)’ will get failed; otherwise, the ‘not(Goal)’ is successful. If the Goal is failed, then ‘not(Goal)’ is true in this case, so the syntax for ‘not’ is given below.
not(U) : U, !, fail; true.
The above line can be read as not(U) cut(!) fail otherwise true, that means if the U is true, then we shall go for U, then cut and then fail otherwise, if the U is false, then it will be going for true, where, not(U) does not mean that U is false, it means that U cannot be proven true.
- o not(U): Can also be written as \ U
- o not(U=V): Can also be written as U \= V
How not Works in Prolog?
In prolog, the ‘not’ has a logical notation, negation, not, \+, which depends on the prolog interpreters. The concept of logical notation in prolog is might be problematic; prolog uses the method to tell about that if a theory is false, which is got from the facts and rules, and if this attempt fails, then the theory is false; this is called negation as failure. The prolog may not have been told about the facts and rules, so that it cannot prove the theory.
The negation-as-failure which has the ‘not’ predicate which can be defined as follows:
not(R) :- call(R), !, fail.
not(R).
The goal of the above example is to call(R), which means that this call attempt is to satisfy the goal of R, and most of the prolog interpreters have this predicate in-built, and many other prologs interpreters use the notation ‘\+’ instead of ‘not’.
There is another way to defining the ‘not’ operator by using implication (->) operator, that is:
not(R) :- (call (R) -> fail ; true)
This can be read as if call(R) succeeds, then it get fail otherwise it will succeed, and the semicolon(;) is using to make it logical, we may use many semicolons as per ‘not’ requirement. There is an important thing about the goal: if we have ‘not(a)’, and if ‘a’ has variables with some instance of variables used for the satisfaction of ‘a’, then ‘not(a)’ fails.
An example for negation as a failure:
“Hary likes all animals but snakes.”
The above line can be written as, “If Z is an animal, then harry likes any Z” and likes(hary, Z):- animal(Z).
In this way, we can write the rules which prove that hary likes all animals. But how to tell that if the animal is a snake which hary does not likes that animal.
If we put a condition that Z is a snake, then the sentence ‘Hary likes Z’ is not true, and also the variable Z is an animal, then hary likes Z.
We can write this in prolog as,
likes(hary, Z):- snake(Z),! , fail.
likes(hary, Z) :- animal(Z).
Above two lines can be written as likes(hary, Z), if snake(Z), cut, fail and when the snake(Z) is condition is true when the cut(!) getting executed and when we are going at the right side of the code till then other clauses of the code will not get executed, and it is failing that is why we have to write the second line of it in prolog statement.
Examples of Prolog not
Different examples are mentioned below:
Example #1
Code:
:- initialization(main).
main :- write('Prolog not').
not(S) :- S, !, fail; true.
Input:
not(fail).
not(true).
Output:
In the above program, respected code has been written, and we have compiled it and execute the first query ‘not(fail).’ As given in input, it will give the output as yes because the not(fail) means yes in this case, and if we go for true that is ‘not(true).’, it will give output as no accordingly. So in this way, it is just doing in what of the fail and true and correspondingly the outputs are get operated.
Example #2
To check the use of cut and fail.
Code:
:- initialization(main).
main :- write('Prolog example').
bird(parrot).
bird(crow).
bird(sparrow).
bird(pigeon).
bird(barn).
bird(penguin).
owl(barn).
owl(penguin).
likes(fary, W) :- owl(W), !, fail.
likes(fary, W) :- bird(W).
Input:
likes(fary, pigeon).
likes(fary, sparrow).
likes(fary, barn).
likes(fary, penguin).
likes(fary, parrot).
Output:
In the above program code has been written to check negation as a failure for that we have tested it by some inputs; first, we go through the input ‘likes(fary, pigeon).’, it will give output, yes, and then we will go for ‘likes(fary, sparrow).’, then it will also test and provide yes, and if we go for barn such as ‘likes(fary, barn).’, we will get no, and if we go for penguin ‘likes(fary, penguin).’, we will also get no but if we go for parrot ‘likes(fary, parrot).’, then it will give yes, in this way we can test the program and that is the use of cut and fail.
Conclusion
In the above article, we conclude that prolog has ‘not’ logical operator to check negation, and the notations of the negation are varied according to the prolog interpreters; interpreters respond to the queries which asking prolog that whether it can prove that the query is true or false, as we see the prolog ‘not’ behaves more finely in prolog than in other languages.
Recommended Articles
We hope that this EDUCBA information on “Prolog not” was beneficial to you. You can view EDUCBA’s recommended articles for more information.