4

我对序言很陌生。据我所知,Pure Prolog 仅限于 Horn 子句。这是一个非常简单的序言程序 -

 % student( Snr    , FirstName , LastName   , Semester ).
  student(  1000  , 'Anna'    , 'Arm'      , 'ti2'    ) .
  student(  1001  , 'Rita'    , 'Reich'    , 'ti2'    ) .
  student(  1002  , 'Peter'   , 'Reich'    , 'ti2'    ) .
  student(  1003  , 'Peter'   , 'Petersen' , 'ti7'    ) .


% course( Semester , Course     ) .
  course( 'ti2'    , 'Mathe2'   ) .
  course( 'ti2'    , 'Physics2' ) .
  course( 'ti7'    , 'pdv2'     ) .

 musttake(M,V,N,S,C) :- student(M,V,N,S),  course(S,C).

musttakereverse(M,V,N,S,C) :- course(S,C), student(M,V,N,S).

我的大学幻灯片说,即使我们颠倒 Pure Prolog 规则中目标的顺序,结果的顺序也不应该改变。在上面的代码中,我实现了 2 条规则。musttakemusttakereverse我只是改变了目标的顺序。因此,根据幻灯片,运行时不应更改结果的顺序。但是,当我运行代码时,它们会以不同的顺序给出结果。(根据我的理解,上面的程序在 中pure prolog)。

所以,我想知道这是不是真的

Goal 中的顺序更改不会更改 Pure Prolog 代码中结果的顺序。

谢谢!

4

2 回答 2

4

这是一个最小的示例来说明“结果的顺序”,即生成的答案替换的顺序受子句中目标顺序的影响:

p(X) :- p123(X), p321(X), p213(X).

p123(1). p123(2). p123(3).

p321(3). p321(2). p321(1).

p213(2). p213(1). p213(3).

请注意,所有四个谓词都描述了完全相同的一组解决方案。在这种情况下,精确的顺序p/1由第一个目标决定。

目标顺序不影响的是解决方案的集合。这是最有趣的属性。

可能无法保留的最有趣的属性是终止。通过交换目标,终止属性可能会受到影响。

然后,还有另一个保留的属性,我承认,它不是很有趣:冗余答案/解决方案的存在也被保留了。

于 2016-02-28T13:35:11.830 回答
3

You are right.

If you use the query

?-musttake(M,V,N,S,C).

The goal student(M,V,N,S) is satisfied through the first fact, then course(S,C) is satisfied through the 5th fact. If we track the evolution of M, it will have the value 1000.

The next possible answer will come from investigating the last backtrack point which is at course(S,C), not student(M,V,N,S). The value of C is changed through the 6th fact, but the value of M isn't, so M is still 1000 in the second solution.

If you use the other query however:

 ?-musttakereverse(M,V,N,S,C)

the goal course(S,C) is satisfied through the 5th fact, then the goal student(M,V,N,S) is satisfied through the first fact, which, once again, gives M the value 1000, but the next solution investigates the last backtrack point, which is, this time, student(M,V,N,S), the 2nd fact is used, and the value of M is 1001.

Prolog uses a depth first search and orders the clauses and goals from the top-down and from left to right, I can only assume that your slides contain a typo. You can read more on the clause and goal ordering here.

于 2016-02-28T11:15:14.160 回答