Updated April 5, 2023
Introduction to Prolog trace
Prolog trace is tracing the execution of the Prolog query, which allows to see all the goals being executed as part of the query in sequential order, independent of the query being succeeding or not. It also allows users to see steps occurring as Prolog backtracks. It will allow users to trace the program’s route while looking for a solution for a particular query. For this, there are 2 different ways, one being graphical and the other being non-graphical trace. Kind of traces give the same solution but are done in two different ways.
Syntax of Prolog trace
To turn on tracing mode in Prolog, we need to execute the below “goal”.
?- trace.
true.
When there is no need for tracing required further, we need to execute the below “goal”.
?- notrace.
An example which will show how trace actually looks like:
Example:
Factorial operator in Prolog.
Code:
factorial(0, 1).
factorial(N, NF) :-
N > 0,
N1 is N - 1,
factorial(N1, N1F),
NF is N1F * N.
In the above code, the factorial of N is computed, which means the product of numbers from 1 to N. As per convention, the factorial of 0 is 1, and the factorial of N is written as N!, exclamation mark represents “factorial operator”.
Examples of Prolog trace
Different examples are mentioned below:
Example #1
Prolog Trace. //start
Code:
?- trace.
Output:
Here, it shows true with trace command, which means the tracing goal has been executed successful.
Example #2
Prolog trace //end.
Code:
?- notrace.
Output:
Here, it shows true with notrace command, which means no tracing goal has been executed successfully.
Example #3
Prolog trace of factorial.
Let us consider the following factorial logic; we will have the below code in a .pl file, say factorial.pl else we can have it in the console itself as shown in the screenshot.
Code:
factorial(0, 1).
factorial(N, NF) :-
N > 0,
N1 is N - 1,
factorial(N1, N1F),
NF is N1F * N.
Output:
With below query
Code:
prolog -s factorial.pl.
File factorial.pl will be executed and then call trace goal.
Code:
?- trace.
true
[trace] ?- factorial(3,X).
Below trace is generated.
Output:
^ Call: (6) factorial(4, _G286) ? creep*
^ Call: (7) 4>0 ? creep
^ Call: (7) 4>0 ? creep
^ Call: (7) _G283 is 4-1 ? creep
^ Call: (7) 3 is 4-1 ? creep
^ Call: (7) factorial(3, _G284) ? creep*
^ Call: (8) 3>0 ? creep
^ Exit: (8) 3>0 ? creep
^ Call: (8) _L205 is 3-1 ? creep
^ Exit: (8) 2 is 3-1 ? creep
Call: (8) factorial(2, _L206) ? creep
^ Call: (9) 2>0 ? creep
^ Exit: (9) 2>0 ? creep
^ Call: (9) _L224 is 2-1 ? creep
^ Exit: (9) 1 is 2-1 ? creep
Call: (9) factorial(1, _L225) ? creep
^ Call: (10) 1>0 ? creep
^ Exit: (10) 1>0 ? creep
^ Call: (10) _L243 is 1-1 ? creep
^ Exit: (10) 0 is 1-1 ? creep
Call: (10) factorial(0, _L244) ? creep
Exit: (10) factorial(0, 1) ? creep
^ Call: (10) _L225 is 1*1 ? creep
^ Exit: (10) 1 is 1*1 ? creep
Exit: (9) factorial(1, 1) ? creep
^ Call: (9) _L206 is 1*2 ? creep
^ Exit: (9) 2 is 1*2 ? creep
Exit: (8) factorial(2, 2) ? creep
^ Call: (8) _G284 is 2*3 ? creep
^ Exit: (8) 6 is 2*3 ? creep
^ Call: (7) _G286 is 6*4 ? creep
^ Exit: (7) 24 is 6*4 ? creep
Exit: (6) factorial(4, 24) ? creep
X = 24 ;
false.
[debug] ?- notrace.
true.
Here, a trace of a factorial of 4 is generated.
What is creep?
In Prolog, implementation of prolog, which dictionary uses its syntax, and when the user gives return at the end of a line tracing, prolog prints the word ‘creep’ on the same line, and then prints out the trace of next line. Then, giving return again, prints creep on the line and moves to other lines, and so on.
There are also further tracing facilities, with below goal.
?- help(trace).
Conclusion
We have seen what Prolog trace is and how it is implemented. We have also seen goals needed to implement tracing of prolog programs and goals needed to stop tracing of prolog programs. We have also seen few examples for tracing, which will be useful to understand the tracing concept further. We can call the .pl file directly and run it under the ‘Consult’ option in the SWI prolog terminal or implement it as above.
Recommended Articles
We hope that this EDUCBA information on “Prolog trace” was beneficial to you. You can view EDUCBA’s recommended articles for more information.