如何为系统生成的变量名显示人类可读的变量名?
作为一个简单的例子:
?- length(Ls,N).
Ls = [],
N = 0 ;
Ls = [_5112],
N = 1 ;
Ls = [_5112, _5118],
N = 2 ;
Ls = [_5112, _5118, _5124],
N = 3
会更好
?- length(Ls,N).
Ls = [],
N = 0 ;
Ls = [a],
N = 1 ;
Ls = [a, b],
N = 2 ;
Ls = [a, b, c],
N = 3
映射
_5112 = a
_5118 = b
_5124 = c
详细信息
我找到的最接近的解决方案使用read_term/2,并在answer中使用variable_names(Vars)
选项,但是我的问题不是使用read_term
从控制台获取术语
如果这是重复的,请告诉我;我找不到一个。
真正的问题是基于生成测试用例数据:
?- length(Ls,N),list_partitionedNU(Ls,Ps).
Ls = Ps, Ps = [],
N = 0 ;
Ls = [_5242],
N = 1,
Ps = [[_5242]] ;
Ls = [_5242, _5248],
N = 2,
Ps = [[_5242], [_5248]] ;
Ls = [_5242, _5248],
N = 2,
Ps = [[_5242, _5248]] ;
Ls = [_5242, _5248, _5254],
...
请参阅this和this中的list_partitionedNU/2
。
跟进答案。
基于William的answer
partitions(Ps) :-
length(Ls,N),
assign(Ls),
list_partitionedNU(Ls,Ps).
?- partitions(Ps).
Ps = [] ;
Ps = [[a]] ;
Ps = [[a], [b]] ;
Ps = [[a, b]] ;
Ps = [[a], [b], [c]] ;
Ps = [[a], [b, c]] ;
Ps = [[a, b], [c]] ;
Ps = [[a, c], [b]] ;
Ps = [[a, b, c]] ;
Ps = [[a], [b], [c], [d]] ;
Ps = [[a], [b], [c, d]] ;
Ps = [[a], [b, c], [d]] ;
Ps = [[a], [b, d], [c]] ;
Ps = [[a], [b, c, d]] ;
Ps = [[a, b], [c], [d]] ;
Ps = [[a, c], [b], [d]] ;
Ps = [[a, d], [b], [c]] ;
Ps = [[a, b], [c, d]] ;
Ps = [[a, c], [b, d]] ;
Ps = [[a, d], [b, c]] ;
Ps = [[a, b, c], [d]] ;
Ps = [[a, b, d], [c]] ;
Ps = [[a, c, d], [b]] ;
Ps = [[a, b, c, d]] ;
...
基于CapelliC的answer
partitions(Ps) :-
length(Ls,N),
numbervars(Ls,0,N),
list_partitionedNU(Ls,Ps).
?- partitions(Ps).
Ps = [] ;
Ps = [[A]] ;
Ps = [[A], [B]] ;
Ps = [[A, B]] ;
Ps = [[A], [B], [C]] ;
Ps = [[A], [B, C]] ;
Ps = [[A, B], [C]] ;
Ps = [[A, C], [B]] ;
Ps = [[A, B, C]] ;
Ps = [[A], [B], [C], [D]] ;
Ps = [[A], [B], [C, D]] ;
Ps = [[A], [B, C], [D]] ;
Ps = [[A], [B, D], [C]] ;
Ps = [[A], [B, C, D]] ;
Ps = [[A, B], [C], [D]] ;
Ps = [[A, C], [B], [D]] ;
Ps = [[A, D], [B], [C]] ;
Ps = [[A, B], [C, D]] ;
Ps = [[A, C], [B, D]] ;
Ps = [[A, D], [B, C]] ;
Ps = [[A, B, C], [D]] ;
Ps = [[A, B, D], [C]] ;
Ps = [[A, C, D], [B]] ;
Ps = [[A, B, C, D]] ;
...
答案 0 :(得分:4)
让我们不要忘记一个明显的选择:另一个Prolog处理器。
使用SICStus Prolog 4.5.0(try full-featured SICStus Prolog 30 days for free):
| ?- Xs = [_C,f(_E)|_], length(Xs,N). N = 2, Xs = [_C,f(_E)] ? ; N = 3, Xs = [_C,f(_E),_A] ? ; N = 4, Xs = [_C,f(_E),_A,_B] ? ; N = 5, Xs = [_C,f(_E),_A,_B,_D] ? ; N = 6, Xs = [_C,f(_E),_A,_B,_D,_F] ? ; N = 7, Xs = [_C,f(_E),_A,_B,_D,_F,_G] ? ...
答案 1 :(得分:2)
已故的老人numbervars/3已被重新访问:
?- length(L,5),numbervars(L,0,N).
L = [A, B, C, D, E],
N = 5.
不确定它是否真的有用,但是可以探索...
?- length(L,5),numbervars(L,0'a,N).
L = [T3, U3, V3, W3, X3],
N = 102.
答案 2 :(得分:1)
我们可以构造一个谓词,以递归方式将一个值“分配”给一个函子中的变量,并使用累加器来跟踪分配的最新“名称”。
例如:
assign(Term) :-
assign(Term, 97, _).
assign(X, N, N1) :-
var(X),
!,
char_code(X, N),
N1 is N+1.
assign(F, N, N1) :-
F =.. [_|A],
assign_list(A, N, N1).
assign_list([], N, N).
assign_list([H|T], N, NT) :-
assign(H, N, N1),
assign_list(T, N1, NT).
例如:
?- length(L, _), assign(L).
L = [] ;
L = [a] ;
L = [a, b] ;
L = [a, b, c] ;
L = [a, b, c, d] ;
L = [a, b, c, d, e] ;
L = [a, b, c, d, e, f] .
?- Q = [L, [_, _]], length(L, _), assign(Q).
Q = [[], [a, b]],
L = [] ;
Q = [[a], [b, c]],
L = [a] ;
Q = [[a, b], [c, d]],
L = [a, b] ;
Q = [[a, b, c], [d, e]],
L = [a, b, c] .
因此,我们“遍历”树并将值分配给变量。通过上述实现,我们不将现有常量考虑在内。因此,我们可以为变量分配已经存在的值。此外,我们只是不断增加字符代码,因此最终我们将获得符号,控制字符和字符。
但是,可以通过首先检查函子并获取要跳过的常量列表来“解决”上述缺点。此外,术语生成器当然可以得到改进,例如通过在aa
之后产生z
。
@DanielLyons指向term_variables/2
谓词,它可以大大简化assign/1
谓词,例如:
assign(Term) :-
term_variables(Term, Vars),
assign_list(Vars, 97).
assign_list([], _).
assign_list([H|T], N) :-
char_code(H, N),
N1 is N+1,
assign_list(T, N1).
这当然仍然不能解决上述缺点,尽管如上所述,我们可以通过使用char_code
之外的其他方法来获取常量名称,并首先进行wak查找常量来解决这些缺点。在使用中。