Freddy J
Freddy J

Reputation: 137

Prolog: Predicate that checks yes \= yes outputs yes

foo(X) :-
    bar(X) \= baz(X).

?- bar(2).
yes
?- baz(2).
yes
?- foo(2).
yes

Why does foo(2) output yes? If we query yes \= yes we get a no.

Upvotes: 1

Views: 84

Answers (2)

Will Ness
Will Ness

Reputation: 71065

With bar(X) \= bar(X) we also get a no. But baz is not bar.

In Prolog, there is no "evaluation" of terms. Terms are terms.

  • If we call bar(X) we get X=2 back (evidently).
  • If we call bar(2) we get a yes back.
  • If we call 1 \= 2 we get a yes back.
  • If we call bar \= baz we get a yes back.

Same if we call bar(1) \= baz(2) or bar(2) \= baz(2) or bar(X) \= baz(X).

The \= predicate means "can't be unified". yes can be unified with yes, but bar(2) can not be unified with baz(2). Same with X instead of 2.

The \= is the predicate being called here, and both bar(X) and baz(X) are its arguments -- which are symbolic terms.

There is no automatic "evaluation" of terms in Prolog.

Upvotes: 2

gusbro
gusbro

Reputation: 22585

foo(2) outputs yes because \=/2 succeeds when it cannot unify the left argument with the right argument.

The left argument is bar(2) and the right one is baz(2) so they will not unify as the principal functor of each term is diffent (bar vs baz). It doesn't matter the the argument (2) is the same for both terms.

Upvotes: 2

Related Questions