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

标签 syntax prolog operators iso-prolog

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

?- 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/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 ;

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

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,:-)持有。
  • T是以功能表示法提供的复合术语。
  • T以上规则涵盖term = atom, open ct, arg list, close;
  • atom匹配 op ,它是 T 的仿函数.
  • open ct 匹配一个开括号。
  • “中间部分”( T 的参数)包含在 arg list 的语法规则中.
  • arg listarg 的非空列表.
  • 什么是arg ?
  • 优先级小于1000的项,优先级为(',')/2。例如,1200fx .
  • 作为运算符的原子。 (没有附加条件!)
  • 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|','].

    关于syntax - 为什么作为高优先级运算符的原子不需要圆括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30898041/

    相关文章:

    php - <?php echo $var; 之间有速度差异吗? ?> 和 <?=$var?>?

    list - Prolog 合并两个列表

    c++ - 为什么 C/C++ 中没有 ^^ 运算符?

    python - 如何在Python 2中实现中缀运算符矩阵乘法?

    unit-testing - 如何使用 dart mockito 模拟索引运算符?

    javascript - 用于在第 n 个字符处拆分字符串的正则表达式单行代码,其中 n 是可变长度

    将字节列转换或显示为 MB 的 SQL 查询?

    twitter - ODL的推文元语法

    prolog - Prolog 99瓶啤酒

    prolog - 理解规则 - 错误作为答案