序言.结构(复杂术语)与谓词,我真的不明白 [英] Prolog. Structure(complex term) vs predicate, i dont really get the difference

查看:61
本文介绍了序言.结构(复杂术语)与谓词,我真的不明白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 prolog 的新手,我似乎无法理解结构和谓词之间的区别.真的有区别吗?

在挖掘时我发现有些人认为f(X):-a(X) 作为谓词,有些人认为jealous(X,Y):-loves(X,Z),loves(Y,Z) 是一个结构(或一个复杂的术语).他们在我看来几乎一样.

有人愿意解释吗?

解决方案

在 Prolog 中,term 是常量、原子、变量或复合词.>

一个复合词由一个函子和一个或多个参数组成.以下是条款:

a.% 带有函子 'a' 和 0 个参数的项a(b,c).% 带有函子a"和 2 个参数 b 和 c 的项

空列表[]term,更具体地说,是一个原子.列表 [H|T] 本质上表示为 '.'(H, T),因此是一个复合词,因此,也是一个术语.

您还可以有更复杂的复合词:

a(b(c,d), e(f,g(h)))

这里,a 是一个带有两个参数的函子:b(c,d)e(f,g(h)),等等.

复合词也可以称为结构,因为它们为您提供了一种构建事实的方法:

customer(name(john,doe), address(street('123 Main St'), city('Framusville'), ...), ...).

谓语从句是一个特定的结构术语.在 Prolog 中,一切都是一个 structureterm 形式:functor(arg1, arg2, ...).

让我们看看谓词从句:

f(X) :- a(X).

它本身是一个结构,内部表示为这个::-(f(X), (a(X))).句点 (.) 是一个终止符.使它成为谓词的原因是:

  • 它处于顶级"(不是更高级别术语中的参数)

  • 它的函子是 :-

一个谓词子句也被称为规则,因为术语:-(A, B)定义了relation:如果 B 为真,则 A 为真.术语f(X) 被称为谓词从句的head.

一个或多个谓词子句的集合,它们的头部都具有相同的函子arity(参数数量)to 作为谓词.

看你的第二个例子:

jealous(X,Y) :- love(X,Z), love(Y,Z).

这也是谓词 jealous/2(其函子为谓词谓词的一个谓词子句code>jealous 并且具有 2).它会在内部表示为这个复合词::-(jealous(X,Y), ','(loves(X,Z), love(Y,Z))).这意味着上述表达式也是一个复合词.

您可以使用 write_canonical/1 来查看 Prolog 如何以规范形式查看表达式:

<代码>|?- write_canonical((jealous(X,Y) :- love(X,Z), love(Y,Z))).:-(嫉妒(_17,_18),','(爱(_17,_22),爱(_18,_22)))

SWI Prolog 站点有一个非常好的Prolog 术语表.

I'm new to prolog and i cant seem to understand the difference between a structure and a predicate. Is there really any difference?

While digging around i found that some people consider f(X):-a(X) to be a predicate and some consider jealous(X,Y):-loves(X,Z), loves(Y,Z) to be a structure (or a complex term). They look pretty much the same to me.

Somebody care to explain?

解决方案

In Prolog, a term is a constant, atom, variable, or compound term.

A compound term consists of a functor with 1 or more arguments. The following are terms:

a.          % a term with functor 'a' and 0 arguments
a(b,c).     % a term with functor 'a' and 2 arguments, b and c

The empty list [] is term and, more specifically, an atom. A list [H|T] is represented intrinsically as '.'(H, T) and is, therefore a compound term and, therefore, also a term.

You can also have more complex compound terms:

a(b(c,d), e(f,g(h)))

Here, a is a functor with two arguments: b(c,d), and e(f,g(h)), and so on.

A compound term can also be called a structure as they give you a way to structure facts:

customer(name(john,doe), address(street('123 Main St'), city('Framusville'), ...), ...).

A predicate clause is a specific structure or term. In Prolog, everything is a structure or term of the form: functor(arg1, arg2, ...).

Let's look at the predicate clause:

f(X) :- a(X).

It is itself a structure which internally is represented as this term: :-(f(X), (a(X))). The period (.) is a terminator. What makes it a predicate, as @false indicates, is:

  • It's at the "top level" (not an argument in a higher level term)

  • It's functor is :-

A predicate clause is also referred to as a rule since the the term, :-(A, B) defines the relation: A is true if B is true. The term f(X) is referred to as the head of the predicate clause.

A collection of one or more predicate clauses which all have the same functor and arity (number of arguments) for their heads is referred to as a predicate.

Looking at your second example:

jealous(X,Y) :- loves(X,Z), loves(Y,Z).

This is also one predicate clause for the predicate jealous/2 (the predicate whose functor is jealous and has arity of 2). It would internally be expressed as this compound term: :-(jealous(X,Y), ','(loves(X,Z), loves(Y,Z))). This means that the above expression is also a compound term.

You can see how Prolog views an expression in its canonical form by using write_canonical/1:

| ?- write_canonical((jealous(X,Y) :- loves(X,Z), loves(Y,Z))).
:-(jealous(_17,_18),','(loves(_17,_22),loves(_18,_22)))

The SWI Prolog site has a very good glossary of Prolog terms.

这篇关于序言.结构(复杂术语)与谓词,我真的不明白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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