Introduction to Different and Not in Prolog Programming Language
Hello, and welcome to my blog! Today, I’m going to introduce you to a very interesting and powerful programming language called
Hello, and welcome to my blog! Today, I’m going to introduce you to a very interesting and powerful programming language called
In Prolog, “different” or “not equal” comparisons can be achieved using the built-in predicate dif/2
. The dif/2
predicate is used to assert that two terms must be different, ensuring that they do not unify or match with each other. It is often used when you want to express inequality between two terms in a Prolog rule or query.
Here’s the syntax for using dif/2
:
dif(Term1, Term2)
Term1
and Term2
are the two terms you want to compare for inequality.Term1
and Term2
are instantiated (i.e., they have specific values), dif/2
checks if the terms are different. If they are not, it fails.dif/2
establishes a constraint that they should not take the same value when they become instantiated.Here are some examples of using dif/2
:
?- dif(apple, banana).
true.
?- dif(apple, apple).
false.
?- X = apple, dif(X, banana).
X = apple.
?- X = apple, dif(X, apple).
false.
dif/2
in rules to ensure different values: % Define a rule that ensures X and Y are different
different(X, Y) :- dif(X, Y).
?- different(apple, banana).
true.
?- different(apple, apple).
false.
dif/2
with more complex terms: ?- dif(fruit(apple), fruit(banana)).
true.
?- dif(fruit(apple), fruit(apple)).
false.
Subscribe to get the latest posts sent to your email.