14

在较旧的教科书1中,经常会遇到如下运算符声明:

?- op(1200,fx,(:-)).
              ^  ^

这些圆括号曾经是必要的。但今天,它们不再需要:

| ?- writeq(op(1200,fx,(:-))).     
op(1200,fx,:-)

为什么不再需要它们?标准如何应对这种情况?


1 p.97 6. MU-Prolog 3.2db 参考手册的标准操作员声明,出现在 Lee Naish 的 Negation and Control in Prolog 中,LNCS 238,Springer-Verlag 1985。

4

2 回答 2

6

以下所有内容均参考 ISO/IEC 13211-1:1995。让我从里到外...

6.5.1     graphic char       = ":";
          graphic char       = "-";

6.4.2     graphic token char = graphic char;

          graphic token      = graphic token char, { graphic token char };

          name token         = graphic token;

6.4       name               = [ layout text sequence (* 6.4.1 *) ], name token;

6.3.1.3   atom               = name;

6.5.3     open  char         = "(";
          close char         = ")";
          comma char         = ",";

6.4.8     open  token        = open  char; 
          close token        = close char;
          comma token        = comma char;

6.4.1     (* grammar rules for layout text sequence were omitted *)

6.4       comma              = comma token;
          open ct            = open  token;
          close              = [ layout text sequence ], close token;

6.3.3.1   arg                = atom; (* if that atom is an operator *)
          arg                = term; (* otherwise: priority = 999   *)

6.3.3     arg list           = arg;
          arg list           = arg, comma, arg list;

6.3.3     term               = atom, open ct, arg list, close ;

所以我们回到最初的问题:

这些圆括号曾经是必要的。但是今天,它们不再需要了。为什么不再需要它们?标准如何应对这种情况?

让我们假设T = op(1200,fx,:-)成立。

  1. T是在功能符号中提供的复合术语。

  2. T被上述规则覆盖term = atom, open ct, arg list, close;

  3. atom匹配op,它是 的函子T

  4. open ct 匹配一个左括号。

  5. “中间部分”( 的参数T)包含在 的语法规则中arg list

  6. arg list是 的非空列表arg

  7. 是什么arg

    • 优先级小于 1000 的词条,优先级为 (',')/2。例如,1200fx

    • 作为运算符的原子。(没有任何附加条件!)

  8. close 匹配右括号。

报价:

参数(arg在语法规则中由,其优先级应小于','(逗号)运算符的优先级,以便逗号作为中缀运算符和逗号作为参数或列表元素分隔符之间没有冲突。

笔记:

“参数”的这个概念确保f(x,y)f(:-, ;, [:-, :-|:-])无论当前定义的运算符定义如何,术语语法都是有效的。逗号不是原子,以下“术语”有语法错误:f(,,a)[a,,|v][a,b|,]; 但以下两个术语在语法上是有效的:f(',',a)[a,','|v][a,b|','].

于 2015-06-17T21:23:32.710 回答
6

op(1200,fx,:-)是功能符号中的复合术语。

引用6.3.3 复合术语 --- 功能符号

用函数表示法编写的复合术语的形式f(A1,...,An)是每个参数Ai都是一个arg ,它们用 ,(逗号)分隔。

term = atom, open ct, arg list, close;

arg list = arg;
arg list = arg, comma, arg list;

引用6.3.3.1 参数

参数(在语法规则中由arg表示)作为复合术语或列表元素的参数出现。它可以是作为运算符的原子,也可以是优先级不大于 999 的术语。

arg = atom;如果 atom 是运算符(具有任意优先级)
arg = term;(优先级为 999)

由于上面突出显示的情况arg = atom;:-不需要在op(1200,fx,:-).

如果不是上述特殊情况,我们需要圆括号,因为推导必须遵循6.3.1.3 Atoms

term = atom;优先级为 0,如果 atom 不是
term = atom;优先级为 1201 的运算符,如果 atom 是运算符。


于 2015-06-25T19:51:37.260 回答