序言:缺少功能? [英] Prolog: missing feature?

查看:48
本文介绍了序言:缺少功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何具有 Prolog 经验的程序员都知道对数字使用一元表示法的优势.例如,如果我们将一个数字表示为 1" 的列表(4"是列表[1,1,1,1]"等),我们可以定义:

Any programmer with some experience in Prolog knows the advantages of use unary notation for numbers. By example, if we represent a number as list of 1" ("4" is list "[1,1,1,1]" and so on), we can define:

unary_succ(X,[1|X]).

以下查询符合预期:

?- X=[1,1],unary_succ(X,Y).
X = [1, 1],
Y = [1, 1, 1].

?- unary_succ(X,Y),X=[1,1].
X = [1, 1],
Y = [1, 1, 1].

?- unary_succ(X,Y),Y=[1,1].
X = [1],
Y = [1, 1].

这样,语句 unary_succ(X,Y) 以一种方式绑定"了 X 和 Y,如果在陈述事实之后,这些变量中的一个绑定到一个值,另一个绑定.

In this way, statement unary_succ(X,Y) "binds" X and Y in a way that, if after the fact is stated, one of these variables is bound to a value, the other one does.

然而,如果我们使用内部数字表示,这种行为是不可能的:

However, this behaviour is not possible if we use the internal number representation:

?- X=2,succ(X,Y).
X = 2,
Y = 3.

?- succ(X,Y),X=2.
ERROR: succ/2: Arguments are not sufficiently instantiated

?- succ(X,Y),Y=2.
ERROR: succ/2: Arguments are not sufficiently instantiated

在我看来,以前的陈述和类似的陈述符合预期会非常有用.也就是说,我们需要以一种方式链接两个变量,当其中一个绑定到一个值时,另一个遵循先前建立的规则.

In my opinion, it will be very useful that previous statements and similar ones does what is expected. That is, we need to link two variables in a way that, when one of them is bound to a value, the other does following the previously established rule.

我的问题是:

a) 在 Prolog 中使用一些简单的方法来做到这一点.

a) some easy way to do that in Prolog.

b) 如果不可能,还有其他支持此功能的编程语言吗?

b) if not possible, any other programming language that supports this feature?

欢迎任何评论.

谢谢大家.

* 附录 I *

另一个例子是:

user_id(john,1234).
user_id(tom,5678).

和查询:

X=john,user_id(X,Y).
user_id(X,Y),X=john

目前通过回溯解决.

推荐答案

这个话题被称为 协同,并且要以相当通用的方式解决 - afaik - 需要扩展到基本的 Prolog 计算模型.幸运的是,大多数 Prolog 都有这样的扩展......所以,让我们试试 in SWISH 构建您自己的反应式"扩展:

This topic is known as coroutining, and to be solved in fairly general way - afaik - requires extension to the basic Prolog computation model. Fortunately, most Prologs have such extension... So, let's try in SWISH to build your own 'reactive' extension:

my_succ(X, Y) :- when((nonvar(X);nonvar(Y)), succ(X, Y)).

edit 不是完全正确,但 Jan 在 SWI-Prolog 邮件列表上发布了一个简单的协同应用程序示例:

edit not completely on point, but Jan posted on SWI-Prolog mailing list a simple example of coroutining application:

?- freeze(X, writeln(X)), findall(X, between(1,3,X), Xs).
1
2
3
Xs = [1, 2, 3],
freeze(X, writeln(X)).

这篇关于序言:缺少功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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