Fibo Kowalsky
Fibo Kowalsky

Reputation: 1248

An efficient match_chk/2 predicate in Prolog

I want to check two terms if they are matchable by =/2, and during the checking no variable should be bound.

For example: match_chk/2

| ?- match_chk(X, a).
true. % without any binding

This can be done by using asymmetric subsumes_term/2 twice but this seems inefficient since might need to scan terms 2 times.

match_chk(A, B) :-
    ( subsumes_term(A, B)
    ; subsumes_term(B, A)
    ), !.

Upvotes: 2

Views: 56

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

As Prolog implements negation as negation as failure, when a \+ Goal succeeds, no bindings are returned. As you want to know if two terms are unifiable, you can then simple use double negation:

unifiable(Term1, Term2) :-
    \+ \+ Term1 = Term2.

Or, if you prefer, as in @passaba por aqui comment:

unifiable(Term1, Term2) :-
    \+ Term1 \= Term2.

Upvotes: 1

Related Questions