将术语转换为原子保留 YAP prolog 中的变量名称 [英] Converting Terms to Atoms preserving variable names in YAP prolog

查看:52
本文介绍了将术语转换为原子保留 YAP prolog 中的变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to configure YAP (and/or SWI prolog) so they will preserve variable names in any call to term_to_atom/2 ?.

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

For example, when I execute this:

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

我得到了这个答案:

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

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

Where X has been replaced by its internal representation.

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

However, I would like to get this answer instead:

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

感谢您的帮助!

推荐答案

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

There are two issues involved. How to get the variable name X into the system, and how to get a term with such a variable into the atom.

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

The X you type in is read by the top level which converts it to a regular variable which does not have a name associated. Let's see that in YAP:

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

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

The |: is YAP's prompt for input. And we have entered X+3*Y+X. However, the variable Term contains _A and _B (names chosen by the top level) in place of X and Y. So the information is lost and cannot be restored once it is read by read/1.

您必须使用更通用的内置读取read_term/2,3 和选项variable_names/1 以不同方式访问该信息.

You have to access that information differently with the more general built-in for reading read_term/2,3 and the option 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.

So the read option variable_names/1 gives you the information to restore the variable names. For each named variable read by read_term/2 there is a structure Name = Variable where Name is an atom representing the variable name. Above, 'X' is the name capital X.

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

Anonymous variables, that is variables whose name is _, do not occur in the list of variable names. They can be rapidly extracted like so:

 ?- 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 随心所欲地编写术语.一个例子:

Now for the writing. We cannot write the term directly but have to accompany it with the list Eqs. Let's call the new predicate term_to_atom(Term, Eqs, Atom). In both YAP and SWI there is with_output_to(Output, Goal) which writes the output of Goal to different destinations like atom(A). So you can now use write_term/2 to write the term as you please. An example:

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

变量_131284 看起来很丑.要获取与其名称关联的变量以进行打印,我们可以按如下方式实现 term_to_atom/3:

The variable _131284 looks very ugly. To get variables associated with their names for printing we can implement term_to_atom/3 as follows:

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 作为在 ISO、Minerva、Jekejeke、GNU、B、SWI、YAP 和 SICStus 中写入选项.

variable_names/1 exists as a write option in ISO, Minerva, Jekejeke, GNU, B, SWI, YAP, and SICStus.

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

In SICStus, the originator of writing terms to lists, one writes:

:- 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.

The following was an ISO incompatible work around for YAP prior to 6.3.4. It is no longer necessary. As for the differences to a separate write option: term_to_atom/3 as defined below interferes with constraints and does not correctly render '$VAR'/1.

但目前我们只能近似于理想的选择variable_names/1.要使用我们自己的变量名打印术语,变量必须在 YAP 中替换为 '$VAR'(Codes) 其中Codes 是一个字符代码列表.这并不完全正确相同,但非常接近.这进入一个文件:

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) ).

对于 SWI,您必须将 atom_codes(N,Chs) 替换为 N = Ch.并安装 library(lambda) 首先.它已预装在 YAP 中.

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.

这篇关于将术语转换为原子保留 YAP prolog 中的变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆