问题描述
如何以符合标准的方式编写 avs_term_rearranged(AVs, T, AVsR)
与给定的 AVs
和 T
使得 AVsR
是 AVs
的排列,元素的排列顺序与它们的变量在 T
中从左到右的顺序相同.
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
是形式为 A = V
的元素列表,其中 A
是指定变量名称的原子,例如 'X'
和 V
是对应的变量.此类列表由带有读取选项 variable_names/1
(7.10.3) 的 read_term/2,3
生成.此外,未定义元素的精确顺序.
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
是包含 AVs
的所有变量以及更多变量的术语.
T
is a term that contains all variables of AVs
plus some more.
请注意,在符合标准的程序中,不能依赖变量的术语顺序 (7.2.1):
Note that in a standard conforming program one cannot rely on the term order for variables (7.2.1):
7.2.1 变量
如果 X
和 Y
是不相同的变量,则 X
term_precedes Y
应依赖于实现,除了在创建排序列表期间(7.1.6.5,8.10.3.1 j) 顺序应保持不变.
If X
and Y
are variables which are not identical then X
term_precedes Y
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.
注意—如果 X
和 Y
都是匿名变量,那么它们不是相同的术语(见 6.1.2 a).
NOTE — If X
and Y
are both anonymous variables then they are not identical terms (see 6.1.2 a).
以 来自 8.4.3.4 为例:>
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.]
所以 sort/2
有两种可能的工作方式,一种甚至不能依赖于:
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.
举个例子:
?- avs_term_rearranged(['A'=A,'B'=B,'C'=C], A+C+F+B, AVsR).
AVsR = ['A'=A,'C'=C,'B'=B].
推荐答案
avs_term_rearranged(AVs, T, AVsR) :-
term_variables(T, Vs),
copy_term(Vs+AVs, Vs1+AVs1),
bind_names(AVs1),
build_vn_list(Vs, Vs1, AVsR).
bind_names([]).
bind_names([N=V|AVs]) :-
N = V,
bind_names(AVs).
build_vn_list([], [], []).
build_vn_list([V|Vs],[N|Ns],NVs) :-
( atom(N) ->
NVs = [N=V|NVs1]
; var(N) ->
NVs = NVs1
),
build_vn_list(Vs, Ns, NVs1).
这篇关于重新排列 variable_names的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!