简体   繁体   English

Prolog 中的 Lambda 表达式?

[英]Lambdas in Prolog?

I normally was able to figure out some usages of Lambda with maplist, but in general have hard time using lambda in prolog.我通常能够弄清楚 Lambda 与 maplist 的一些用法,但通常很难在 prolog 中使用 lambda。 It is probably because it differ from other languages, because of unification.可能是因为它与其他语言不同,因为统一。

Here is one of the sticking points: How do you apply declared Lambda expression?这是症结之一:您如何应用声明的 Lambda 表达式? fe

LAM = \X^R^(....)

(weird, there have to be space between = and \) (奇怪,= 和 \ 之间必须有空格)

how do you do:你好吗:

\(LAM)(abc,R)

Another thing which I want to do is to store Lambda expression like a FACT, so that I can query it, but also have it like a lambda function so I can do later binding..fe just of the top of my head... i could think it wrong..我想做的另一件事是将 Lambda 表达式像 FACT 一样存储,以便我可以查询它,但也可以像 lambda function 一样拥有它,这样我就可以稍后绑定我的头部了。我可能认为它错了..

move = \Obj^From^To(move(Obj,From,To))

instantiate:实例化:

?- \(move)(ball,ground,table).

or partially:或部分:

?- L2 = \(move)(ball).
?- L3 = \(L2)(table,floor)

query:询问:

?- move(ball,F,T).

Let's consider the following lambda term:让我们考虑以下 lambda 术语:

?- LAM_2 = \X^R^atom_chars(X,R).
LAM_2 = \X^R^atom_chars(X, R).

The _2 reminds us that two arguments are lacking in order to make this a real goal. _2提醒我们缺少两个 arguments 以使其成为真正的目标。

To use it, you have now to supply these two arguments.要使用它,您现在必须提供这两个 arguments。 Either with some meta-predicate like maplist/3 or with call/3 .使用一些元谓词,例如maplist/3call/3

?- LAM_2 = \X^R^atom_chars(X,R), call(LAM_2, abc, Res).
LAM_2 = \X^R^atom_chars(X, R),
Res = [a, b, c].
?- LAM_2 = \X^R^atom_chars(X,R), maplist(LAM_2, [abc,def], Xss).
LAM_2 = \X^R^atom_chars(X, R),
Xss = [[a, b, c], [d, e, f]].

Note that the variables X and R do not get instantiated!请注意,变量XR不会被实例化!

What is really a bit unusual is that there is no direct scoping of logic variables.真正有点不寻常的是没有直接的逻辑变量范围。 They are all global within a clause, so we have to pay attention that these variables are not accidentally reused.它们在一个子句中都是全局的,所以我们必须注意这些变量不会被意外重用。

Also note that we could have written for above example, just:另请注意,我们可以为上面的示例编写,只是:

?- LAM_2 = atom_chars, call(LAM_2, abc, Res).
LAM_2 = atom_chars,
Res = [a, b, c].

to store Lambda expression like a FACT, so that I can query it像 FACT 一样存储 Lambda 表达式,以便我可以查询它

Why not define the fact directly?为什么不直接定义事实呢? There is no way to do this otherwise.否则没有办法做到这一点。

The example you give makes sense, provided we have already the fact defined:你给出的例子是有道理的,只要我们已经定义了事实:

move(ball, table, floor).

Now we can construct the query incrementally by adding arguments by wrapping call/2 around.现在我们可以通过将call/2包裹起来添加 arguments 来增量构造查询。

?- L1_3 = move, L2_2 = call(move, ball), L3_0 = call(L2_2, table, X), L3_0.
L1_3 = move,
L2_2 = call(move, ball),
L3_0 = call(call(move, ball), table, floor),
X = floor.

However, please note that such partial goals are rarely used today.但是,请注意,今天很少使用这样的部分目标。 Also the direct usage of call/N is mostly reserved to meta-predicates like maplist/3 .此外, call/N的直接使用主要保留给诸如maplist/3之类的元谓词。

Besides the SWI-Prolog package that's already been mentioned you might be also interested in λProlog .除了已经提到的SWI-Prolog package 之外,您可能还对λProlog感兴趣。

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

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