2

假设我列出了事实:

letter(a).
letter(b).
letter(c).
...
letter(z).
vowel(a).
consonant(b).
consonant(c).
consonant(d).
vowel(e).
consonant(f).
...
consonant(z).

如果我按“字母”顺序声明规则,我会在控制台中收到以下警告:

Warning: /Users/…/prolog-example.pl:31:
  Clauses of vowel/1 are not together in the source-file
Warning: /Users/…/prolog-example.pl:32:
  Clauses of consonant/1 are not together in the source-file
Warning: /Users/…/prolog-example.pl:35:
  Clauses of vowel/1 are not together in the source-file
Warning: /Users/…/prolog-example.pl:36:
  Clauses of consonant/1 are not together in the source-file
Warning: /Users/…/prolog-example.pl:51:
  Clauses of vowel/1 are not together in the source-file

但是,如果我执行以下操作:

letter(a).
letter(b).
letter(c).
...
letter(z).
consonant(b).
consonant(c).
consonant(d).
...
consonant(z).
vowel(a).
vowel(e).
vowel(i).
vowel(o).
vowel(u).
vowel(y).

我没有收到警告。这些警告只是warnings错误还是实际错误?

4

3 回答 3

4

当谓词定义不连续时,应discontiguous/1在其子句之前使用标准指令声明谓词。在你的情况下:

:- discontiguous([
    letter/1,
    vowel/,
    consonant/1
]).

如果您有不连续的谓词而没有相应discontiguous/1的指令,则后果取决于使用的 Prolog 系统。例如,SWI-Prolog 和 YAP 将打印警告但接受所有子句。GNU Prolog 将忽略子句。ECLiPSe 将报告编译错误。在 Prolog 系统不抛出错误的情况下,通常仍会打印警告,因为谓词可能会被检测为不连续,例如子句头部中的简单拼写错误。

于 2014-10-10T00:50:16.280 回答
2

标准 ISO/IEC 13211-1:1995 内容如下:

7.4.3 条款

...

用户定义过程的所有子句P都应是
单个 Prolog 文本的连续阅读术语,除非该 Prolog 文本中有
指示discontiguous(UP) 指令P

因此标准要求 (»shall«) 所有子句默认是连续的。现在依赖于添加的子句的程序员或程序不依赖于标准行为。

于 2014-10-10T09:09:27.017 回答
2

它们只是某些系统上的警告。这是为了防止您在想编写一个新的谓词时不小心将子句添加到谓词中。你可以在 SWI 中去掉那些(这些消息看起来像你从 SWI 得到的消息,我没有过多地使用其他方言)。

您可以使用style_check/1, 或discontiugous指令。

:- discontiguous vowel/1,consonant/1,letter/1.
% alternative:
:- style_check(-discontiguous).
于 2014-10-10T00:55:30.017 回答