简体   繁体   English

粘合剂中的序言

[英]Binders in Prolog

I know Prolog has no built-in binders to represent eg λx (x=1) but I was wondering if it was possible to implement them. 我知道Prolog没有内置的绑定器来表示λx (x=1)但是我想知道是否有可能实现它们。 In some predicates like setof/3 , the behaviour is quite close: the variable X in the answer substitution for the query 在某些谓词(如setof/3 ,行为非常接近:答案的替换变量X用于查询

?- setof(X, member(X,[a,a,c]), Xs).
Xs = [a, c].

is unbound and we can give it a value to our liking: 是不受限制的,我们可以根据自己的喜好为其提供值:

?- setof(X, member(X,[a,a,c]), Xs), X = b.
X = b,
Xs = [a, c].

But if we instantiate X earlier, we lose solutions: 但是,如果我们较早地实例化X ,我们将失去解决方案:

?- X = b, setof(X, member(X,[a,a,c]), Xs).
false.

What I would like to happen is that setof binds X , ie for the evaluation of setof , X is treated like a fresh variable. 我想发生的是setof绑定X ,即对于setof的求值, X被视为一个新变量。 To make this a bit more concrete: Is it possible to give an implementation binding_setof such that 为了使它更具体一点:是否可以提供实现binding_setof使得

?- X = b, binding_setof(X, member(X,[a,a,c]), Xs).
X = b,
Xs = [a, c].

?

PS: I am aware of languages like λProlog which were built to solve this issue, but I am interested in a Prolog solution. PS:我知道可以解决此问题的λProlog之类的语言,但我对Prolog解决方案感兴趣。

Edit: I have tried to solve the problem with library(lambda) . 编辑:我试图解决与library(lambda)的问题。 I aimed to create an anonymous variable and apply it to \\X^setof(X, member(X,[a,c,c], Xs) via call/N . Since the binding of Xs is undone outside of it, the toplevel does not report it. It's still possible to see it by adding format('~w',[Xs]) into the setof but I leave it out for clarity. Again, the call 我的目标是创建一个匿名变量,并通过call/N将其应用于\\X^setof(X, member(X,[a,c,c], Xs) 。由于Xs的绑定在其外部被撤消,因此顶层没有报告它。仍然可以通过在setof中添加format('~w',[Xs])来查看它,但是为了清楚起见,我将其省略。

?- call(\X^setof(X,member(X,[a,c,c]),Xs), _), X=b.
X = b.

succeeds, but 成功,但是

?- X = b, call(\X^setof(X,member(X,[a,c,c]),Xs), _).
false.

fails. 失败。 This is consistent with the comment in the source that a lambda bound variable must not occur outside the lambda expression. 这与源中的评论一致,即lambda绑定变量一定不能出现在lambda表达式之外。

The Golog interpreter contains a solution to locally scoping variables, they use a pi/2 compound and a sub/4 predicate. Golog解释器包含一个局部作用域变量的解决方案,它们使用pi/2复合和sub/4谓词。 We can adapt pi/2 to a predicate, their license precludes distributing their interpreter so you'll need to get sub/4 from them (free), linked above. 我们可以将pi/2为谓词,它们的许可证禁止分发其解释器,因此您需要从它们那里获得sub/4 (免费),上面已链接。

pi(V, E) :- 
    sub(V, _, E, E1), 
    call(E1).

?- X = d, pi(x, setof(x, member(x, [a, a, c]), Xs)).
X = d,
Xs = [a, c].

It works "most of the time"; 它“大部分时间”都有效; I've had issues when working with CLP libraries. 使用CLP库时遇到了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM