属性变量:库接口/实现/可移植性 [英] Attributed variables: library interfaces / implementations / portability

查看:39
本文介绍了属性变量:库接口/实现/可移植性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我浏览一些 相关问题时最近,我偶然发现了 @mat 对问题的回答 如何在 Prolog 中表示直接访问邻居顶点的有向循环图 .

When I was skimming some prolog related questions recently, I stumbled upon this answer by @mat to question How to represent directed cyclic graph in Prolog with direct access to neighbour verticies .

到目前为止,我个人在 Prolog 中使用属性变量的经验非常有限.但是@mat 给出的用例激发了我的兴趣.所以我尝试用它来回答另一个问题,使用约束逻辑编程的排序列表.

So far, my personal experience with attributed variables in Prolog has been very limited. But the use-case given by @mat sparked my interest. So I tried using it for answering another question, ordering lists with constraint logic programming.

首先,好消息:我第一次使用属性变量就如我所愿.

First, the good news: My first use of attributed variables worked out like I wanted it to.

然后,不是那么好消息:当我通过 answer 发布时,我意识到 Prolog 中有几个 API 和属性变量的实现.

Then, the not so good news: When I had posted by answer, I realized there were several API's and implementations for attributed variables in Prolog.

我觉得我在这里不知所措...特别是我想知道以下内容:

I feel I'm over my head here... In particular I want to know the following:

  • 哪些 API 被广泛使用?到目前为止,我发现了两个:SICStus 和 SWI.
  • 不同的属性变量实现提供哪些功能?一样的吗?还是一个包含另一个?
  • 语义上是否存在差异?
  • 实际实施情况如何?有些比其他的更有效率吗?
  • 是否(或正在)使用属性变量会导致可移植性问题?

很多问号,这里...请分享您的经验/立场?提前致谢!

Lots of question marks, here... Please share your experience / stance? Thank you in advance!

这是上面提到的答案的代码片段:

Here's a code snippet of the answer mentioned above:

init_att_var(X,Z) :-
    put_attr(Z,value,X).

get_att_value(Var,Value) :-
    get_attr(Var,value,Value).

到目前为止我只"使用 put_attr/3get_attr/3,但是---根据关于属性变量的 SICStus Prolog 文档---SICStus 提供 put_attr/2get_attr/2.

So far I "only" use put_attr/3 and get_attr/3, but---according to the SICStus Prolog documentation on attributed variables---SICStus offers put_attr/2 and get_attr/2.

所以即使这个非常浅的用例需要一些仿真层(一种或另一种方式).

So even this very shallow use-case requires some emulation layer (one way or the other).

推荐答案

我想重点关注在使用不同的属性变量接口时注意到的一个重要的一般点:在为属性变量设计接口时,实现者还应该请记住以下几点:

I would like to focus on one important general point I noticed when working with different interfaces for attributes variables: When designing an interface for attributed variables, an implementor should also keep in mind the following:

  • 在推理同时统一时是否可以考虑属性,如[X,Y] = [0,1]?
  • Is it possible to take attributes into account when reasoning about simultaneous unifications, as in [X,Y] = [0,1]?

例如在 SICStus Prolog 中这是可能的,因为在调用 verify_attributes/3 之前,此类绑定撤销.在 hProlog 提供的接口中(attr_unify_hook/2,在统一之后调用并且所有绑定都已经到位)很难考虑Y 在推理attr_unify_hook/2X的统一时,因为此时Y不再是变量观点!这对于可以仅根据地面值做出决策的求解器来说可能就足够了,但对于需要额外数据(通常存储在属性中)来查看统一是否应该成功的求解器来说,这是一个严重的限制,然后不再容易获得.一个明显的例子:布尔统一与决策图.

This is possible for example in SICStus Prolog, because such bindings are undone before verify_attributes/3 is called. In the interface provided by hProlog (attr_unify_hook/2, called after the unification and with all bindings already in place) it is hard to take into account the (previous) attributes of Y when reasoning about the unification of X in attr_unify_hook/2, because Y is no longer a variable at this point! This may be sufficient for solvers that can make decisions based on ground values alone, but it is a serious limitation for solvers that need additional data, typically stored in attributes, to see whether a unification should succeed, and which are then no longer easily available. One obvious example: Boolean unification with decision diagrams.

截至 2016 年,verify-attributes SWI-Prolog 的分支 也支持 verify_attributes/3,这要归功于 的出色实现工作道格拉斯·迈尔斯.该分支已准备好进行测试,并打算在其正确有效地工作后立即合并到 master 中.为了与 hProlog 兼容,该分支还支持 attr_unify_hook/2:它通过在编译时将这些定义重写为更通用的 verify_attributes/3 来实现.

As of 2016, the verify-attributes branch of SWI-Prolog also supports verify_attributes/3, thanks to great implementation work by Douglas Miles. The branch is ready for testing and intended to be merged into master as soon as it works correctly and efficiently. For compatibility with hProlog, the branch also supports attr_unify_hook/2: It does so by rewriting such definitions to the more general verify_attributes/3 at compilation time.

在性能方面,很明显 verify_attributes/3 可能有一个缺点,因为同时使多个变量接地可能会让你更快地看到(在 attr_unify_hook/2) 统一不能成功.但是,我很乐意随时将这种通常可以忽略不计的优势换成更通用的界面为您提供的改进的可靠性、易用性和增加的功能,并且无论如何这已经是 SICStus Prolog 中最重要的标准行为它的通用性也是速度更快的 Prolog 系统之一.

Performance-wise, it is clear that there may be a downside to verify_attributes/3, because making several variables ground at the same time may let you sooner see (in attr_unify_hook/2) that a unification cannot succeed. However, I will gladly and any time exchange this typically negligible advantage for the improved reliability, ease of use, and increased functionality that the more general interface gives you, and which is in any case already the standard behaviour in SICStus Prolog which is on top of its generality also one of the faster Prolog systems around.

SICStus Prolog 还具有一个名为 project_attributes/2 的重要谓词:顶层使用它来投射约束以查询变量.SWI-Prolog 在最近的版本中也支持这一点.

SICStus Prolog also features an important predicate called project_attributes/2: It is used by the toplevel to project constraints to query variables. SWI-Prolog also supports this in recent versions.

SWI 接口还有一个巨大的优势:attribute_goals//1 和因此 copy_term/3 给你的剩余目标总是一个列表.这有助于用户避免其代码中的默认值,并鼓励使用更具声明性的界面,因为纯约束目标列表不能包含控制结构.

There is also one huge advantage of the SWI interface: The residual goals that attribute_goals//1 and hence copy_term/3 give you are always a list. This helps users to avoid defaultyness in their code, and encourages a more declarative interface, because a list of pure constraint goals cannot contain control structures.

有趣的是,除了语法之外,这两个界面都不允许您解释统一.就我个人而言,我认为在某些情况下,您可能希望以不同于句法的方式来解释统一,但是,也可能存在反对的很好的论据.

Interestingly, neither interface lets you interpret unifications other than syntactically. Personally, I think there are cases where you may want to interpret unifications differently than syntactically, however, there may also be good arguments against that.

属性变量的其他接口谓词大多可以很容易地与不同系统的简单包装谓词互换.

The other interface predicates for attributed variables are mostly easily interchangable with simple wrapper predicates for different systems.

这篇关于属性变量:库接口/实现/可移植性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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