Order of Goals in Pure Prolog

323 views Asked by At

I am very new to prolog. As per my knowledge Pure Prolog is restricted to Horn clauses. Here is a very simple prolog program -

 % 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).

My university slide says that even if we reverse the order of the goals in a rule in Pure Prolog, the order of the results should not be changed. In the above code, there are 2 rules that I have implemented. musttake and musttakereverse in which I have simply changed the order of the goals. So, according to the slides, the the order of the results should not be changes when running. But, when I run the code, they give results in different orders.(As per my understanding the above program is in pure prolog).

So, I would like to know if it's true that

Change of orders in Goal does not change the order of the result in Pure Prolog code.

Thanks!

2

There are 2 answers

0
ZakC On BEST ANSWER

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.

0
false On

Here is a minimal example to illustrate that the "order of the result", that is the order of answer substitutions generated is affected by the order of the goals in a clause:

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).

Note that all four predicates describe exactly the same set of solutions. The precise order for p/1 is determined in this case by the very first goal.

What the order of goals does not affect is the set of solutions. That is the most interesting property.

The most interesting property that may not be preserved is termination. By exchanging goals the termination properties may be affected.

And then, there is another preserved property which, I admit, is not very interesting: The presence of redundant answers/solutions is preserved, too.