为什么作为高优先级运算符的原子不需要圆括号?

Why are round brackets not needed for atoms that are high priority operators?

在较早的教科书中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.

以下均参考ISO/IEC13211-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 ;

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

These round brackets used to be necessary. But today, they are no longer needed. Why are they no longer needed? How does the standard cope with this situation?

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

  1. T 是以函数表示法提供的复合术语。

  2. T 包含在上述规则中 term = atom, open ct, arg list, close;

  3. atom 匹配 op,这是 T.

  4. 的函子
  5. open ct 匹配左括号。

  6. "middle part"(T 的参数)包含在 arg list.

  7. 的语法规则中
  8. arg listarg.

  9. 的非空列表
  10. 什么是arg

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

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

  11. close 匹配右括号。

引用:

An argument (represented by arg in the syntax rules occurs as the argument of a compount term or element of a list. It can be an atom which is an operator,or a term with priority not greater than 999. When an argument is an arbitrary term, its priority shall be less than the priority of the ',' (comma) operator so that there is no conflict between comma as an infix operator and comma as an argument or list element separator.

注:

This concept of an "argument" ensures that both the terms f(x,y) and f(:-, ;, [:-, :-|:-]) are syntactically valid whatever operator definitions are currently defined. Comma is not an atom and the following "terms" have syntax errors: f(,,a), [a,,|v], and [a,b|,]; but the following two terms are syntactically valid: f(',',a), [a,','|v], and [a,b|','].

op(1200,fx,:-) 是函数符号中的复合词。

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

A compound term written in functional notation has the form f(A1,...,An) where each argument Ai is an arg and they are separated by , (comma).

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

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

引用 6.3.3.1 参数:

An argument (represented by arg in the syntax rules) occurs as the argument of a compound term or element of a list. It can be an atom which is an operator, or a term with priority not greater than 999.

arg = atom; if atom is an operator (with arbitrary priority)
arg = term; (with priority 999)

由于上面突出显示的情况 arg = atom;:-op(1200,fx,:-) 中不需要圆括号。

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

term = atom; with priority 0, if atom is not an operator
term = atom; with priority 1201, if atom is an operator.