Professional Documents
Culture Documents
Prolog14 - Different and Not
Prolog14 - Different and Not
Here we will define two predicates — different and not. The different predicate will
check whether two given arguments are same or not. If they are same, it will return
false, otherwise it will return true. The not predicate is used to negate some
statement, which means, when a statement is true, then not(statement) will be
false, otherwise if the statement is false, then not(statement) will be true.
So the term ‘different’ can be expressed in three different ways as given below −
different(X, X) :- !, fail.
different(X, Y).
Program
different(X, X) :- !, fail.
different(X, Y).
Output
| ?- [diff_rel].
compiling D:/TP Prolog/Sample_Codes/diff_rel.pl for byte code...
D:/TP Prolog/Sample_Codes/diff_rel.pl:2: warning: singleton variables [X,Y] for different
D:/TP Prolog/Sample_Codes/diff_rel.pl compiled, 2 lines read - 327 bytes written, 11 ms
yes
| ?- different(100,200).
yes
| ?- different(100,100).
no
| ?- different(abc,def).
yes
| ?- different(abc,abc).
no
| ?-
Program
Output
| ?- [diff_rel].
compiling D:/TP Prolog/Sample_Codes/diff_rel.pl for byte code...
D:/TP Prolog/Sample_Codes/diff_rel.pl compiled, 0 lines read - 556 bytes written, 17 ms
yes
| ?- different(100,200).
yes
| ?- different(100,100).
no
| ?- different(abc,def).
yes
| ?- different(abc,abc).
no
| ?-
So if P is true, then cut and fail, this will return false, otherwise it will be true. Now
let us see one simple code to understand this concept.
Program
Output
| ?- [not_rel].
compiling D:/TP Prolog/Sample_Codes/not_rel.pl for byte code...
D:/TP Prolog/Sample_Codes/not_rel.pl compiled, 0 lines read - 630 bytes written, 17 ms
yes
| ?- not(true).
no
| ?- not(fail).
yes
y
| ?-