How to define a meta-logical predicate that tests (thus succeeds or fails only) if two lists of unique variables contain exactly the same variables using the built-ins from the current ISO standard (ISO/IEC 13211-1:1995 including Cor.2).
Stated differently, the predicate should succeed if one list of unique variables is a permutation of the other. In analogy to library(ordsets)
, let's call this meta-logical predicate varset_seteq(As, Bs).
Note that, in contrast to ord_seteq/2
, this predicate cannot be simply As == Bs
.
Using list.sort() method sorts the two lists and the == operator compares the two lists item by item which means they have equal data items at equal positions. This checks if the list contains equal data item values but it does not take into account the order of elements in the list.
Python sort() method and == operator to compare lists Further, the == operator is used to compare the list, element by element.
A straightforward way to check the equality of the two lists in Python is by using the equality == operator. When the equality == is used on the list type in Python, it returns True if the lists are equal and False if they are not.
The solution I propose uses term_variables/2
to check if Bs
has no extra variables over As
and that As
has no variable that doesn't appear in Bs
.
varset_seteq(As, Bs):-
term_variables(As-Bs, As),
term_variables(Bs-As, Bs).
The above solution can be tricked to succeed with arguments that are not sets of free variables:
| ?- varset_seteq([A], [a]).
A = a
yes
To avoid that, unification can be replaced with equivalence test:
varset_seteq(As, Bs):-
term_variables(As-Bs, A0),
A0 == As,
term_variables(Bs-As, B0),
B0 == Bs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With