练习中对 Prolog 运算符的解释 [英] Interpretation of Prolog's operator in an exercise

查看:46
本文介绍了练习中对 Prolog 运算符的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下关于 Prolog 中自然语言的练习:

I have the following exercise about natural language in Prolog:

以如下方式实现以下两个运算符 hasof:peter has car of john 回答以下问题:谁有什么 X

Implement the following two operators has and of in such a way that with phrases like: peter has car of john answers to questions such as: Who has What of X

现在,我知道这在英语中听起来很糟糕,因为在英语中我们通常会说:peter has john's car",但我是意大利人,我尝试将意大利语要求翻译成英语.我希望这个概念很清楚.

Now, I know that in English language this sound bad because in English we usually say: "peter has john's car", but I am Italian and I have tried to translate the Italian request in English. I hope the concept is however clear.

所以我找到了以下 Prolog 解决方案,保存在一个文件中:

So I have found the following Prolog solution, saved in a file:

:-op(200,xfx,has).
:-op(100,xfx,of).

peter has car of john.

之后我在 Prolog shell 中查阅这个文件,我可以执行以下操作:

After that I consult this file in Prolog shell and I can perform the following operations:

?- 彼得有约翰的车.

正确.

或者:

?- X 具有 Z 中的 Y.

X = 彼得,

Y = 汽车,

Z = 约翰.

太好了,它有效,但我不太清楚它是如何工作的,我对运营商优先级有一些疑问:

Great, it work, but I have not so clear how it work and I have some questions about the operator priority:

has 运算符将 200 作为 priority 值.of 运算符将 100 作为 priority 值.

has operator have 200 as priority value. of operator have 100 as priority value.

参考短语:peter has car of john 所以这意味着 Prolog 首先评估这部分句子:car of john(因为运算符具有较低的优先级尊重有运营商),如果它是真的,那么它评估:pater has(先前评估的结果).

Referring to the phrase: peter has car of john so it means that Prolog first evaluate this part of the sentence: car of john (because of operator have lower priority respect has operator) and, if it is true, then it evaluate: pater has (result of the previous evalutatuion).

简单来说,我可以这样解释原句:

In a few words, I can interpret the original sentence in this way:

彼得有(约翰的车)

正确吗?

另一个问题与运营商的类型有关.

An other question is related to the operator's typology.

在我的解决方案中,我对两个运算符都使用 xfx 类型学,因为在这种句子中,我没有与具有相同优先级的多个运算符的潜在歧义相关的问题(例如:a - b - c 其中运算符 - 必须具有 yfx)

In my solution I use the xfx typology for both operators because in this kind of sentences I have no problems related to potential ambiguity with several operators having the same precedence (as in the case: a - b - c in which the operator - must have the form yfx)

我的问题是:我可以在我的 has 和 of 运算符上以某种方式使用 xfy 和/或 yfx 混合获得相同的结果吗?

推荐答案

正确吗?是的.

xfx 表示运算符支配"其分支,而 xfyyfx 代表列表构造",从某种意义上说允许相同优先级的链接表达式.

xfx means that the operator 'dominates' upon its branches, while xfy or yfx stand for 'list construction', in sense that allow chaining expressions of same priority.

对于您的短语,您使用的是正确的 op 声明,但如果您认为有必要,您也可以使用其他关联性说明符.

With your phrase, you're using the correct op declaration, but you could do with other associativity specifiers, if you see necessity.

我看到 xfy 可能用于 of 的用法,例如peter has car of john of mary".之后

I see a possible usage of xfy for of, like 'peter has car of john of mary`. After

:- op(100, xfy, of).
:- op(200, xfx, has).

我明白

?- write_canonical(peter has car of john of mary).
has(peter,of(car,of(john,mary)))
true.

这篇关于练习中对 Prolog 运算符的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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