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

查看:16
本文介绍了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

  • 结束标记,后跟 % 或诸如 SPACE、NEWLINE、TAB 之类的布局字符.

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

小数点,如3.14159

图形标记 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天全站免登陆