以编程方式编写和阅读术语时是否可以保留变量名? [英] Is it possible to preserve variable names when writing and reading term programatically?

查看:90
本文介绍了以编程方式编写和阅读术语时是否可以保留变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个SWI-Prolog谓词,该谓词将numbervars/3应用于术语的匿名变量,但保留用户提供的其非匿名变量的名称.我最终计划在term_expansion(或类似的东西)中添加某种挂钩.

I'm trying to write an SWI-Prolog predicate that applies numbervars/3 to a term's anonymous variables but preserves the user-supplied names of its non-anonymous variables. I eventually plan on adding some kind of hook to term_expansion (or something like that).

所需输出示例:

    ?- TestList=[X,Y,Z,_,_].
       > TestList=[X,Y,Z,A,B].

>将术语转换为原子以在YAP Prolog中保留变量名,展示了如何使用read_term作为原子获得术语中使用的变量名.与term_variables获得的变量列表不同,此列表(格式为[X ='X',Y ='Y',...])不包含匿名变量,从而使匿名变量的隔离非常简单.

This answer to the question Converting Terms to Atoms preserving variable names in YAP prolog shows how to use read_term to obtain as atoms the names of the variables used in a term. This list (in the form [X='X',Y='Y',...]) does not contain the anonymous variables, unlike the variable list obtained by term_variables, making isolation of the anonymous variables fairly straightforward.

但是,如果仅将其应用于直接从终端读取的术语,则此功能的实用性将受到一定程度的限制.我注意到答案中的所有示例都涉及该术语的直接用户输入. 是否有可能(通过原子获取)不是通过直接用户输入获得的 术语的变量名称?,也就是说,是否可以通过某种方式写"一个术语(保留变量名)到一些不可见的流,然后像从终端输入一样读取"它?

However, the usefulness of this great feature is somewhat limited if it can only be applied to terms read directly from the terminal. I noticed that all of the examples in the answer involve direct user input of the term. Is it possible to get (as atoms) the variable names for terms that are not obtained through direct user input? That is, is there some way to 'write' a term (preserving variable names) to some invisible stream and then 'read' it as if it were input from the terminal?

或者...也许这更像是LaTeX式的思路,但是在Prolog扩展/尝试将它们统一为变量之前,有什么方法可以将变量包装"在单引号中(从而原子化它们). ,最终结果是将它们视为以大写字母开头的原子而不是变量?

Alternatively... Perhaps this is more of a LaTeX-ish line of thinking, but is there some way to "wrap" variables inside single quotes (thereby atom-ifying them) before Prolog expands/tries to unify them as variables, with the end result that they're treated as atoms that start with uppercase letters rather than as variables?

推荐答案

您可以使用ISO核心标准variable_names/1读写选项.下面是一些示例代码,它替换了变量名称映射中的匿名变量:

You can use the ISO core standard variable_names/1 read and write option. Here is some example code, that replaces anonymous variables in a variable name mapping:

% replace_anon(+Map, +Map, -Map)
replace_anon([_=V|M], S, ['_'=V|N]) :- member(_=W, S), W==V, !, 
   replace_anon(M, S, N).
replace_anon([A=V|M], S, [A=V|N]) :- 
   replace_anon(M, S, N).
replace_anon([], _, []). 

variable_names/1是ISO核心标准.它始终是一个读取选项.然后,它也变成了写选项.另请参阅: https://www.complang.tuwien.ac.at /ulrich/iso-prolog/WDCor3

variable_names/1 is ISO core standard. It was always a read option. It then became a write option as well. See also: https://www.complang.tuwien.ac.at/ulrich/iso-prolog/WDCor3

这是一个示例运行:

Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.25)

?- read_term(X,[variable_names(M),singletons(S)]), 
   replace_anon(M,S,N), 
   write_term(X,[variable_names(N)]).
|: p(X,Y,X).
p(X,_,X)

不建议使用旧的numbervars/3,因为它与属性变量不兼容.例如,您不能在CLP(FD)存在的情况下使用它.

To use the old numbervars/3 is not recommended, since its not compatible with attribute variables. You cannot use it for example in the presence of CLP(FD).

这篇关于以编程方式编写和阅读术语时是否可以保留变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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