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ます)。

だから、それが本当かどうか知りたい

ゴールでの順序の変更は、純粋な 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).

4 つの述語はすべて、まったく同じ一連のソリューションを記述していることに注意してください。この場合、の正確な順序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 に答える