prolog - 将术语转换为原子并在 YAP prolog 中保留变量名称

标签 prolog

有没有办法配置 YAP(和/或 SWI prolog),以便它们在对 term_to_atom/2 的任何调用中保留变量名称?。

例如,当我执行此操作时:

term_to_atom(member(X, [1,2]), A).

我得到这个答案:

A = 'member(_131405,[1,2])'

其中 X 已被其内部表示替换。

但是,我想得到这个答案:

A = 'member(X,[1,2])'

感谢您的帮助!

最佳答案

涉及两个问题。如何将变量名 X 获取到系统中,以及如何将具有此类变量的术语获取到原子中。

您输入的X由顶层读取,并将其转换为没有关联名称的常规变量。让我们在 YAP 中看看:

   ?- read(Term).
   |: X+3*Y+X.
Term = _A+3*_B+_A

|:是YAP的输入提示。我们输入了 X+3*Y+X。 但是,变量 Term 包含 _A_B(由顶层选择的名称)代替 XY。所以一旦被read/1读取到,信息就丢失了并且无法恢复。

您必须使用更通用的内置读取 read_term/2,3 和选项 variable_names/1 来访问该信息。

   ?- read_term(T,[variable_names(Eqs)]).
   |: X+3*Y+X.
Eqs = ['X'=_A,'Y'=_B],
T = _A+3*_B+_A

因此读取选项variable_names/1为您提供了恢复变量名称的信息。对于 read_term/2 读取的每个命​​名变量,都有一个结构 Name = Variable,其中 Name 是表示变量名称的原子。上面,'X' 是名字大写的 X。

匿名变量,即名称为_的变量,不会出现在变量名称列表中。它们可以像这样快速提取:

 ?- read_term(T,[variable_names(Eqs)]),
    term_variables(Eqs, Named),
    term_variables(Named+T, Vars),
    append(Named, Anons, Vars).

阅读就这么多。

现在开始写作。我们不能直接写出该术语,而是必须将其与列表 Eqs 一起使用。我们将新谓词称为 term_to_atom(Term, Eqs, Atom)。在 YAP 和 SWI 中都有 with_output_to(Output, Goal)它将 Goal 的输出写入不同的目的地,例如 atom(A)。因此,您现在可以使用 write_term/2 随意编写该术语。一个例子:

?- with_output_to(atom(A),write_term('a b'+X,[quoted(true)])).
A = '\'a b\'+_131284'.

变量_131284看起来非常难看。要获取与其名称关联的变量以进行打印,我们可以实现 term_to_atom/3,如下所示:

term_to_atom(T, Eqs, A) :-
   with_output_to(atom(A), write_term(T,[variable_names(Eqs),quoted(true)]) ).

像这样使用它:

   ?- read_term(T,[variable_names(Eqs)]), term_to_atom(T, Eqs, Atom).
   |: X+3*Y+X.
Atom = 'X+3*Y+X',
Eqs = ['X'=_A,'Y'=_B],
T = _A+3*_B+_A

variable_names/1 exists as a write option ISO、Minerva、Jekejeke、GNU、B、SWI、YAP 和 SICStus。

在 SICStus(将术语写入列表的创始人)中,有人写道:

:- use_module(library(codesio)).

term_to_atom(T, Eqs, Atom) :-
   write_term_to_codes(T, Codes, [variable_names(Eqs),quoted(true)]),
   atom_codes(Atom, Codes).

以下是 6.3.4 之前的 YAP 的 ISO 不兼容解决方法。不再需要了。至于与单独写入选项的差异:下面定义的 term_to_atom/3 会干扰约束,并且无法正确渲染 '$VAR'/1

But for the moment we can only approximate the ideal option variable_names/1. To print terms with our own variable names, variables have to be substituted in YAP by '$VAR'(Codes) where Codes is a list of character codes. This does not do exactly the same, but it is very close. This goes into a file:

:- use_module(library(apply)).
:- use_module(library(lambda)).

write_eqs_term(T, Eqs) :-
   \+ \+ ( 
           maplist(\Eq^( Eq = (N='$VAR'(Chs)), atom_codes(N,Chs)), Eqs),
           write_term(T,[numbervars(true),quoted(true)])
   ).

term_to_atom(T, Eqs, A) :-
   with_output_to(atom(A), write_eqs_term(T, Eqs) ).

For SWI, you would have to replace atom_codes(N,Chs) by N = Ch. and install library(lambda) first. It's pre-installed in YAP.

关于prolog - 将术语转换为原子并在 YAP prolog 中保留变量名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7947910/

相关文章:

prolog - Prolog 中的不同素数分区

indexing - 为什么这个谓词留下一个选择点?

testing - 如何使用 Prolog 测试术语?

prolog - 从整数列表创建二叉搜索树

prolog - 有什么严肃的项目正在使用 PROLOG 吗?

Prolog - 从事实列表中获取集合的最大值(使用失败谓词)

Prolog:获取谓词解决方案并将其断言为事实

prolog - 在 Prolog 中解析多位数的数字

list - 如何检查 Prolog 列表中的两个元素

prolog - 我想用Prolog证明一些定理,但是它总是返回 "Out of global stack"