How to write in a standard conforming manner avs_term_rearranged(AVs, T, AVsR)
with given AVs
and T
such that AVsR
is a permutation of AVs
with the elements arranged in same order as their variables occur in left-to-right order in T
.
AVs
is a list of elements of the form A = V
where A
is an atom designating a variable name like 'X'
and V
is a corresponding variable. Such lists are produced by read_term/2,3
with the read-option variable_names/1
(7.10.3). Additionally, the precise order of elements is not defined.
| ?- read_term(T,[variable_names(AVs)]).
A+B+A+_+C.
AVs = ['A'=A,'B'=B,'C'=C]
T = A+B+A+_+C
T
is a term that contains all variables of AVs
plus some more.
Note that in a standard conforming program one cannot rely on the term order for variables (7.2.1):
7.2.1 Variable
If
X
andY
are variables which are not identical thenX
term_precedesY
shall be implementation dependent except that during the creation of a sorted list (7.1.6.5, 8.10.3.1 j) the ordering shall remain constant.NOTE — If
X
andY
are both anonymous variables then they are not identical terms (see 6.1.2 a).
Consider as an example from 8.4.3.4:
sort([f(U),U,U,f(V),f(U),V],L).
Succeeds, unifying L with [U,V,f(U),f(V)] or
[V,U,f(V),f(U)].
[The solution is implementation dependent.]
So there are two possible ways how sort/2
will work, and one cannot even rely on the success of:
sort([f(U),U,U,f(V),f(U),V],L), sort(L, K), L == K.
As an example:
?- avs_term_rearranged(['A'=A,'B'=B,'C'=C], A+C+F+B, AVsR).
AVsR = ['A'=A,'C'=C,'B'=B].
T
an arbitrary term or is it of the same form? is it, e.g., also obtained fromread_term
?T
twice in your question with different meanings. Might help to rename one of them to avoid confusion.T
is used as an argument for the sought predicate, and once it is used withread_term
which in this case producesT
andAVs
such that they fit to the predicate.avs_term_rearranged(['A'=A,'B'=B,'C'=C], A+C+F+B, ['A'=A,'C'=C,'B'=B])
?ream_term/3
to read from a stream that you write to usingwrite_term/3
then you can get a representation of the variables inT
that conform with that ofAVs
, and at that point it's a simple matter of discarding from theT
-variables all those that do not appear in theAVs
variables.