Prolog - 列表的不寻常 cons 语法 [英] Prolog - unusual cons syntax for lists

查看:63
本文介绍了Prolog - 列表的不寻常 cons 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Lee Naish 的论文中遇到了一个不熟悉的 Prolog 语法高阶逻辑在 Prolog 中编程.这是论文中的第一个代码示例:

I have come across an unfamiliar bit of Prolog syntax in Lee Naish's paper Higher-order logic programming in Prolog. Here is the first code sample from the paper:

% insertion sort (simple version)
isort([], []).
isort(A.As, Bs) :-
    isort(As, Bs1),
    isort(A, Bs1, Bs).

% insert number into sorted list
insert(N, [], [N]).
insert(N, H.L, N.H.L) :-
    N =< H.
insert(N, H.LO, H.L) :-
    N > H,
    insert(N, LO, L).

我的困惑在于 isort(A.As, Bs) :- 中的 A.As.从上下文来看,它似乎是列表的替代 cons 语法,相当于 isort([A|As], Bs) :-.

My confusion is with A.As in isort(A.As, Bs) :-. From the context, it appears to be an alternate cons syntax for lists, the equivalent of isort([A|As], Bs) :-.

同样,N.H.L 似乎是一种更方便的表达方式 [N|[H|L]].

As well N.H.L appears to be a more convenient way to say [N|[H|L]].

但是 SWI Prolog 不会接受这种不寻常的语法(除非我做错了什么).

But SWI Prolog won't accept this unusual syntax (unless I'm doing something wrong).

有人认识吗?我的假设正确吗?哪个 Prolog 解释器接受它作为有效语法?

Does anyone recognize it? is my hypothesis correct? Which Prolog interpreter accepts that as valid syntax?

推荐答案

点运算符在 1972 年的第一个 Prolog 系统中用于列表,该系统用 Algol-W 编写,有时称为 Prolog 0.它受到类似符号的启发在 LISP 系统中.以下示例来自 Alain Colmerauer 和 Philippe 的论文Prolog 的诞生鲁塞尔Prolog 的创造者.

The dot operator was used for lists in the very first Prolog system of 1972, written in Algol-W, sometimes called Prolog 0. It is inspired by similar notation in LISP systems. The following exemple is from the paper The birth of Prolog by Alain Colmerauer and Philippe Roussel – the very creators of Prolog.

+ELEMENT(*X, *X.*Y).
+ELEMENT(*X, *Y.*Z) -ELEMENT(*X, *Z).

那个时候[]曾经是NIL.

下一个 Prolog 版本,由 Battani & 用 Fortran 编写Meloni,使用案例来区分原子和变量.然后 DECsystem 10 Prolog 引入了方括号表示法,将 nilX.Xs 替换为 [][X,..Xs] 在更高版本的 DECsystem 10 中收到 [X|Xs] 作为替代.在 ISO Prolog 中,只有 [X|Xs].(X,Xs) 和作为规范语法的 '.'(X,Xs).

The next Prolog version, written in Fortran by Battani & Meloni, used cases to distinguish atoms and variables. Then DECsystem 10 Prolog introduced the square bracket notation replacing nil and X.Xs with [] and [X,..Xs] which in later versions of DECsystem 10 received [X|Xs] as an alternative. In ISO Prolog, there is only [X|Xs], .(X,Xs), and as canonical syntax '.'(X,Xs).

请注意,点在 ISO Prolog 中有许多不同的角色.它已经作为

Please note that the dot has many different rôles in ISO Prolog. It serves already as

  • 结束标记,后跟 % 或空格、换行、制表符等布局字符.

  • end token when followed by a % or a layout character like SPACE, NEWLINE, TAB.

小数点,如3.14159

graphic token char 形成图形标记为 =..

因此,如果您现在将 . 声明为中缀运算符,则必须非常小心.包括您编写的内容和 Prolog 系统将读取的内容.一个额外的空格可以改变一个术语的含义.考虑两种符号中的两个数字列表:

So if you are now declaring . as an infix operator, you have to be very careful. Both with what you write and what Prolog systems will read. A single additional space can change the meaning of a term. Consider two lists of numbers in both notations:

[1,2.3,4]. [5].
1 .2.3.4.[]. 5.[].

请注意,1 后必须加一个空格.在这种情况下,数字前面的额外空格可能会改变术语的含义.像这样:

Please note that you have to add a space after 1. In this context, an additional white space in front of a number may change the meaning of your terms. Like so:

[1|2.3]. [4]. 5. [].
1 .2.3. 4.[]. 5. [].

这是另一个可能更有说服力的例子:

Here is another example which might be even more convincing:

[1,-2].
1.(-2).[].

负数需要在点列表中使用圆括号.

Negative numbers require round brackets within dot-lists.

今天,只有 YAP 和 XSB 仍然提供中缀 . 默认情况下 –和他们的做法不同.而且 XSB 甚至无法识别上面的点语法:您需要在一些非负数周围使用圆括号.

Today, there is only YAP and XSB left that still offer infix . by default – and they do it differently. And XSB does not even recognize above dot syntax: you need round brackets around some of the nonnegative numbers.

您写道,N.H.L 似乎是一种更方便的表达方式 [N|[H|L]].有一个简单的经验法则可以简化 ISO Prolog 中的此类表达式:每当您在列表中看到标记 |[ 紧随其后时,您可以替换它们由 , (并删除右侧相应的 ] ).所以你现在可以写:[N,H|L] 这看起来并不糟糕.

You wrote that N.H.L appears to be a more convenient way to say [N|[H|L]]. There is a simple rule-of-thumb to simplify such expressions in ISO Prolog: Whenever you see within a list the tokens | and [ immediately after each other, you can replace them by , (and remove the corresponding ] on the right side). So you can now write: [N,H|L] which does not look that bad.

您也可以在另一个方向使用该规则.如果我们有一个列表 [1,2,3,4,5] 我们可以像这样使用 | 作为剃须刀": [1,2,3|[4,5]].

You can use that rule also in the other direction. If we have a list [1,2,3,4,5] we can use | as a "razor blade" like so: [1,2,3|[4,5]].

另一句话,因为您正在阅读 Naish 的论文:同时,它是 好吧明白只需要call/N!ISO Prolog 支持 call/1call/2call/8.

Another remark, since you are reading Naish's paper: In the meantime, it is well understood that only call/N is needed! And ISO Prolog supports call/1, call/2 up to call/8.

这篇关于Prolog - 列表的不寻常 cons 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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