序言中的连词与析取优先级 [英] Conjunction vs Disjunction precedence in prolog

查看:50
本文介绍了序言中的连词与析取优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,合取比分离优先.

From what i know, conjunction has greater precedence than disjunction.

我们拥有以下知识库:

a(1).
a(2,3).
a(3,2).
a(4,5,3).
b.
b(1,2).
b(3).
c.

我们的目标是:

?- a(X,Y);b(Z),c,fail,d.

问题来了:我的老师说目标分为 4 个子目标,通过连词连接:

So here's the problem: My teacher says that the goal gets broken into 4 subgoals, connected by conjunction:

(a(X,Y);b(Z)),
c,
fail,
d.

并且在执行此查询时我不应该得到任何结果.但是连词有更大的优先级,对吧?是不是应该将目标分解为两个子目标(而不是四个),并通过分离连接:

and that i shouldn't get no results when executing this query. But conjunction has greater precedence, right? Isn't the goal supposed to be broken into two subgoals(not four), connected by disjunction:

a(X,Y);
b(Z),c,fail,d.

所以现在我想知道谁对谁错.有人能解释一下吗?

So now I'm wondering who's right and who's wrong. Can somebody explain?

**当我执行查询时,我实际上得到了结果.

**I'm actually getting results when i execute the query.

推荐答案

我认为问题主要是由于过度使用优先级"一词造成的误解.

I think the problem is primarily a misunderstanding that's due to the overloaded use of the word "precedence".

Prolog 运算符由可扩展谓词 op(+Precedence, +Type, :Name) 定义,其中 Precedence 是 0 到 1200 之间的整数,Type 指定结合性,Name 给出实际操作符.

Prolog operators are defined by the extensible predicate op(+Precedence, +Type, :Name) where Precedence is an integer between 0 and 1200, Type specifies associativity, and Name gives the actual operator.

此表显示了一些预定义的运算符:

This table shows some predefined operators:

1100    xfy ;, |
1000    xfy ,

对于歇斯底里的葡萄干,Precedence较小整数值意味着运算符绑定更紧密(即,它具有更高的 优先).

For hysterical raisins, a smaller integer value of Precedence means that an operator binds tighter (i.e., that it has higher precedence).

所以析取运算符的优先级低于比合取运算符之一,因为它的Precedence值更高.

So the precedence of the disjunction operator is lower than the one of the conjunction operator, because its value of Precedence is higher.

这篇关于序言中的连词与析取优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆