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!
You are right.
If you use the query
The goal
student(M,V,N,S)
is satisfied through the first fact, thencourse(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)
, notstudent(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:
the goal
course(S,C)
is satisfied through the 5th fact, then the goalstudent(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.