暗示否定谓词的序言 [英] Prolog implying a negative predicate

查看:64
本文介绍了暗示否定谓词的序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PROLOG 中编写以下规则:if P then not Q

How can I write the following rule in PROLOG: if P then not Q

我知道您可以轻松地编写if P then Q谓词,例如q(X) :- p(X),但是如何否定q/1 谓词?我不想定义具有其他语义的新谓词,例如 non_q/1.

I understand that you can easily write if P then Q the predicates like q(X) :- p(X), but how can you negate the q/1 predicate? I don't want to define new predicates with other semantics like non_q/1.

推荐答案

子句if P then not Q"在逻辑上等同于否定子句not P OR not Q".因此,它是一个 Horn 子句,没有正字面量,并且作为 SLD 定理证明和Horn 子句,可以在 Prolog 编程中表示为目标子句或查询":

The clause "if P then not Q" is logically equivalent to the negative clause "not P OR not Q". As such it is a Horn clause without a positive literal, and as an application of the correspondence of SLD theorem proving and Horn clauses, can be represented in Prolog programming as a goal clause or "query":

?- P, Q.

一分钟后让我们回到这个想法.

Let's come back to this idea in a minute.

但是目标子句可能不是您想到的那种表示形式.构成 Prolog知识库"的事实和规则是确定的子句,即 Horn 子句每个都有一个正字面量.If P then not Q"没有正字面量,所以在这个意义上它不能被表示(作为一个定语从句).

But the goal clause is perhaps not the sort of representation you have in mind. The facts and rules that constitute a Prolog "knowledgebase" are definite clauses, i.e. Horn clauses each with exactly one positive literal. "If P then not Q" has no positive literal, so in this sense it cannot be represented (as a definite clause).

上面显示的目标子句询问"是否可以证明 P 和 Q.Prolog 提供了否定即失败"的概念,因此更自然的询问"not P OR not Q"是否成立的方法是:

The goal clause shown above "asks" if P and Q can both be proven. Prolog provides a notion of "negation as failure", so a more natural way to "ask" whether "not P OR not Q" holds would be:

?- not((P,Q)).

那么如果 P 或 Q 失败,我们就会成功,如果两者都成功,我们就会失败.

Then we would get success if either P or Q fails, and failure if both succeed.

但是,如果您的目的是断言否定知识库中的某些内容,Prolog 自然不会支持这一点.根据您的应用程序,可能有一种合理的方法来解决 Prolog 语法并完成所需的工作(总是有一种不合理的方法,正如您用 non_q 谓词所暗示的那样).

However if your purpose is to assert the negation something in the knowledgebase, Prolog doesn't naturally support this. Depending on your application, there may be a reasonable way to work around the Prolog syntax and accomplish what is needed (there's always an unreasonable way to do it, as you've hinted as with a non_q predicate).

这篇关于暗示否定谓词的序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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