I need to match one pattern with several different terms in Prolog, but I don't want to unify any of the variables when matching them. I found one possible way to do this, but it seems inefficient:
:- initialization(main).
:- set_prolog_flag('double_quotes','chars').
main :-
Pattern = (A>B;B<A),
match_without_unify(Pattern,(1>A1;A1<1)),
match_without_unify(Pattern,(2>4;4<2)).
match_without_unify(A,B) :-
%copy the terms, unify them, and count the number of variables
copy_term([A,B],[A1,B1]),
term_variables(B1,L1),
length(L1,L),
A1=B1,
term_variables(B1,L2),
length(L2,L),
writeln([L1,L2]).
Is it possible to solve this problem without calling term_variables/2
and length/2
twice?