Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of Goals in Pure Prolog

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!

like image 286
Srijani Ghosh Avatar asked Mar 13 '23 03:03

Srijani Ghosh


1 Answers

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.

like image 154
false Avatar answered Mar 21 '23 07:03

false