Search code examples
prolog

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.


Solution

  • 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.