假设我有以下规则:

unify('test', 'this is a test').
run :- write('Enter something: '), 
       read(X), 
       unify(X, Y), 
       write('The answer is '), write(Y).

然后我按照以下方式运行:

?- ['unify.pl'].
% unify.pl compiled 0.00 sec, -48 bytes
true.

?- run.
Enter something: test.
The answer is this is a test
true.

?- run.
Enter something: 'test'.
The answer is this is a test
true.

Swi-Prolog为什么两者都统一 test'test'unify('test', 'this is a test').?我在回答关于这样的序法问题的同时遇到了这个问题。虽然我能够回答这个人的问题,但我无法解释这种特殊的行为,我想知道是否有人可以。

有帮助吗?

解决方案

虽然可以使用单语引号表示Swi-Prolog中的原子,例如 'This is an atom', ,单引号是 不需要 当Swi-Prolog Parser可以从一系列字符中识别原子时,通常以小写字母字符开始,例如 test. 。如果序列包含空格(或其他一些字符),则需要单个引号来正确表示原子。字母数字字符和某些标点符号(例如下划线) _ 很好,例如 test5_6.

如果没有单引号的字符序列是从其他任何东西开始的,例如一个数字 6k, ,解析器将其视为 number;如果它是大写字母字符,例如 Test, ,解析器将其视为变量。

其他提示

这不是SWI特定的行为 - 标准需要。有一种简单的方法可以看到这一点。您也可以将其用于任何其他语法不明显的术语。任何一个类型都在Toplevel:

?- X = 'test'.
X = test.

?- X = 'this is a test'.
X = 'this is a test'.

答案始终是有效的Prolog文本 - 这是针对SWI的特定于SWI的,但也针对许多其他Prolog Systems,例如YAP,GNU,B,如果是Sicstus。

看到这一点的另一种方法是使用write_caronical/1:

?- write_canonical('this is a test').
'this is a test'
true.

?- write_canonical([a,b,(c,d),{e,f}]).
'.'(a,'.'(b,'.'(','(c,d),'.'({}(','(e,f)),[]))))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top