Instead of explaining each step will guide you to
- Overview of the SWI Prolog Command Line Debugger | Eric Zinda Blog
- Protocol/1 (Used to capture screen to file)
- Tool for drawing SLD trees in Prolog
- Overview of the SWI Prolog Graphical Debugger | Eric Zinda Blog
and give you a simple demonstration of trace/1 for your example goal.
?- leash(-all),visible(+all).
true.
?- trace.
true.
[trace] ?- =(R,42),=(N,R),write(N).
Call: (11) _36544=42
Exit: (11) 42=42
Call: (11) _36550=42
Exit: (11) 42=42
Call: (11) write(42)
42
Exit: (11) write(42)
R = N, N = 42.
[trace] ?- nodebug.
true.
?-
I also gave some detailed answers on StackOverflow about understanding trace/1 output.
Here is the list
This one is one of the more useful answers
Asked ChatGPT to explain the example query from the SO question Prolog - Explain trace steps in English here is the result
(Click triangle to expand)
Note: Did not check this for accuracy but at a glance it looks good.
ChatGPT August 3 Version
Model: GPT-3.5
Prompt
For this Prolog code
1. plays(alice, leadguitar).
2. plays(noah, drums).
3. plays(mike, leadguitar).
4. plays(mike, drums).
5. plays(katie, baseguitar).
6. plays(drew, leadguitar).
7. plays(drew, baseguitar).
9. combo(Person1,Person2,Person3):-
10. plays(Person1,X),
11. plays(Person2,Y),
12. plays(Person3,Z),
13. X \= Y,
14. Y \= Z,
15. X\=Z.
here is the trace output
[trace] ?- combo(mike,alice,drew).
Call: (8) combo(mike, alice, drew)
Unify: (8) combo(mike, alice, drew)
Call: (9) plays(mike, _7040)
Unify: (9) plays(mike, leadguitar)
Exit: (9) plays(mike, leadguitar)
Call: (9) plays(alice, _7040)
Unify: (9) plays(alice, leadguitar)
Exit: (9) plays(alice, leadguitar)
Call: (9) plays(drew, _7040)
Unify: (9) plays(drew, leadguitar)
Exit: (9) plays(drew, leadguitar)
Call: (9) leadguitar\=leadguitar
Fail: (9) leadguitar\=leadguitar
Redo: (9) plays(drew, _7040)
Unify: (9) plays(drew, baseguitar)
Exit: (9) plays(drew, baseguitar)
Call: (9) leadguitar\=leadguitar
Fail: (9) leadguitar\=leadguitar
Redo: (9) plays(mike, _7040)
Unify: (9) plays(mike, drums)
Exit: (9) plays(mike, drums)
Call: (9) plays(alice, _7040)
Unify: (9) plays(alice, leadguitar)
Exit: (9) plays(alice, leadguitar)
Call: (9) plays(drew, _7040)
Unify: (9) plays(drew, leadguitar)
Exit: (9) plays(drew, leadguitar)
Call: (9) drums\=leadguitar
Exit: (9) drums\=leadguitar
Call: (9) leadguitar\=leadguitar
Fail: (9) leadguitar\=leadguitar
Redo: (9) plays(drew, _7040)
Unify: (9) plays(drew, baseguitar)
Exit: (9) plays(drew, baseguitar)
Call: (9) drums\=leadguitar
Exit: (9) drums\=leadguitar
Call: (9) leadguitar\=baseguitar
Exit: (9) leadguitar\=baseguitar
Call: (9) drums\=baseguitar
Exit: (9) drums\=baseguitar
Exit: (8) combo(mike, alice, drew)
true.
Please explain each step of the trace and associate it with the line number in the Prolog code.
Completion
Certainly! Let’s break down the trace output step by step and associate each step with the corresponding line in the Prolog code:
-
[trace] ?- combo(mike,alice,drew).- The user queries if
mike,alice, anddrewform a valid combo.
- The user queries if
-
Call: (8) combo(mike, alice, drew)- Prolog starts by calling the
combo/3predicate with the given arguments.
- Prolog starts by calling the
-
Unify: (8) combo(mike, alice, drew)- Prolog is trying to unify the arguments with the
combo/3predicate.
- Prolog is trying to unify the arguments with the
-
Call: (9) plays(mike, _7040)- Prolog checks if
mikeplays any instrument. This corresponds to line 10 in the Prolog code.
- Prolog checks if
-
Unify: (9) plays(mike, leadguitar)- Prolog finds that
mikeplaysleadguitar(from line 3).
- Prolog finds that
-
Exit: (9) plays(mike, leadguitar)- Prolog successfully exits the above unification.
-
Call: (9) plays(alice, _7040)- Prolog checks if
aliceplays any instrument. This corresponds to line 11.
- Prolog checks if
-
Unify: (9) plays(alice, leadguitar)- Prolog finds that
aliceplaysleadguitar(from line 1).
- Prolog finds that
-
Exit: (9) plays(alice, leadguitar)- Prolog successfully exits the above unification.
-
Call: (9) plays(drew, _7040)- Prolog checks if
drewplays any instrument. This corresponds to line 12.
- Prolog checks if
-
Unify: (9) plays(drew, leadguitar)- Prolog finds that
drewplaysleadguitar(from line 6).
- Prolog finds that
-
Exit: (9) plays(drew, leadguitar)- Prolog successfully exits the above unification.
-
Call: (9) leadguitar\=leadguitar- Prolog checks if the instruments played by the first and second persons are different. This corresponds to line 13. Since both
mikeandaliceplayleadguitar, this check fails.
- Prolog checks if the instruments played by the first and second persons are different. This corresponds to line 13. Since both
-
Fail: (9) leadguitar\=leadguitar- The above check fails.
-
Redo: (9) plays(drew, _7040)- Prolog backtracks to find another instrument that
drewplays.
- Prolog backtracks to find another instrument that
-
Unify: (9) plays(drew, baseguitar)- Prolog finds that
drewalso playsbaseguitar(from line 7).
- Prolog finds that
-
Exit: (9) plays(drew, baseguitar)- Prolog successfully exits the above unification.
-
Call: (9) leadguitar\=leadguitar- Prolog checks again if the instruments played by the first and second persons are different. This still fails.
-
Fail: (9) leadguitar\=leadguitar- The above check fails again.
-
Redo: (9) plays(mike, _7040)- Prolog backtracks to find another instrument that
mikeplays.
- Prolog backtracks to find another instrument that
-
Unify: (9) plays(mike, drums)- Prolog finds that
mikealso playsdrums(from line 4).
- Prolog finds that
-
Exit: (9) plays(mike, drums)- Prolog successfully exits the above unification.
-
Call: (9) plays(alice, _7040)- Prolog checks again if
aliceplays any instrument.
- Prolog checks again if
-
Unify: (9) plays(alice, leadguitar)- Prolog finds (again) that
aliceplaysleadguitar.
- Prolog finds (again) that
-
Exit: (9) plays(alice, leadguitar)- Prolog successfully exits the above unification.
-
Call: (9) plays(drew, _7040)- Prolog checks again if
drewplays any instrument.
- Prolog checks again if
-
Unify: (9) plays(drew, leadguitar)- Prolog finds that
drewplaysleadguitar.
- Prolog finds that
-
Exit: (9) plays(drew, leadguitar)- Prolog successfully exits the above unification.
-
Call: (9) drums\=leadguitar- Prolog checks if the instruments played by the first and second persons are different. This corresponds to line 13. Since
mikeplaysdrumsandaliceplaysleadguitar, this check succeeds.
- Prolog checks if the instruments played by the first and second persons are different. This corresponds to line 13. Since
-
Exit: (9) drums\=leadguitar- Prolog successfully exits the above check.
-
Call: (9) leadguitar\=leadguitar- Prolog checks again if the instruments played by the second and third persons are different. This corresponds to line 14. Since both
aliceanddrewplayleadguitar, this check fails.
- Prolog checks again if the instruments played by the second and third persons are different. This corresponds to line 14. Since both
-
Fail: (9) leadguitar\=leadguitar- The above check fails.
-
Redo: (9) plays(drew, _7040)- Prolog backtracks to find another instrument that
drewplays.
- Prolog backtracks to find another instrument that
-
Unify: (9) plays(drew, baseguitar)- Prolog finds that
drewalso playsbaseguitar.
- Prolog finds that
-
Exit: (9) plays(drew, baseguitar)- Prolog successfully exits the above unification.
-
Call: (9) drums\=leadguitar- Prolog checks if the instruments played by the first and second persons are different. This succeeds.
-
Exit: (9) drums\=leadguitar- Prolog successfully exits the above check.
-
Call: (9) leadguitar\=baseguitar- Prolog checks if the instruments played by the second and third persons are different. This corresponds to line 14. Since
aliceplaysleadguitaranddrewplaysbaseguitar, this check succeeds.
- Prolog checks if the instruments played by the second and third persons are different. This corresponds to line 14. Since
-
Exit: (9) leadguitar\=baseguitar- Prolog successfully exits the above check.
-
Call: (9) drums\=baseguitar- Prolog checks if the instruments played by the first and third persons are different. This corresponds to line 15. Since
mikeplaysdrumsanddrewplaysbaseguitar, this check succeeds.
- Prolog checks if the instruments played by the first and third persons are different. This corresponds to line 15. Since
-
Exit: (9) drums\=baseguitar- Prolog successfully exits the above check.
-
Exit: (8) combo(mike, alice, drew)- Prolog successfully finds that
mike,alice, anddrewform a valid combo.
- Prolog successfully finds that