How the Interpreter of SWIPL works ...?

Instead of explaining each step will guide you to

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:

  1. [trace] ?- combo(mike,alice,drew).

    • The user queries if mike, alice, and drew form a valid combo.
  2. Call: (8) combo(mike, alice, drew)

    • Prolog starts by calling the combo/3 predicate with the given arguments.
  3. Unify: (8) combo(mike, alice, drew)

    • Prolog is trying to unify the arguments with the combo/3 predicate.
  4. Call: (9) plays(mike, _7040)

    • Prolog checks if mike plays any instrument. This corresponds to line 10 in the Prolog code.
  5. Unify: (9) plays(mike, leadguitar)

    • Prolog finds that mike plays leadguitar (from line 3).
  6. Exit: (9) plays(mike, leadguitar)

    • Prolog successfully exits the above unification.
  7. Call: (9) plays(alice, _7040)

    • Prolog checks if alice plays any instrument. This corresponds to line 11.
  8. Unify: (9) plays(alice, leadguitar)

    • Prolog finds that alice plays leadguitar (from line 1).
  9. Exit: (9) plays(alice, leadguitar)

    • Prolog successfully exits the above unification.
  10. Call: (9) plays(drew, _7040)

    • Prolog checks if drew plays any instrument. This corresponds to line 12.
  11. Unify: (9) plays(drew, leadguitar)

    • Prolog finds that drew plays leadguitar (from line 6).
  12. Exit: (9) plays(drew, leadguitar)

    • Prolog successfully exits the above unification.
  13. 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 mike and alice play leadguitar, this check fails.
  14. Fail: (9) leadguitar\=leadguitar

    • The above check fails.
  15. Redo: (9) plays(drew, _7040)

    • Prolog backtracks to find another instrument that drew plays.
  16. Unify: (9) plays(drew, baseguitar)

    • Prolog finds that drew also plays baseguitar (from line 7).
  17. Exit: (9) plays(drew, baseguitar)

    • Prolog successfully exits the above unification.
  18. Call: (9) leadguitar\=leadguitar

    • Prolog checks again if the instruments played by the first and second persons are different. This still fails.
  19. Fail: (9) leadguitar\=leadguitar

    • The above check fails again.
  20. Redo: (9) plays(mike, _7040)

    • Prolog backtracks to find another instrument that mike plays.
  21. Unify: (9) plays(mike, drums)

    • Prolog finds that mike also plays drums (from line 4).
  22. Exit: (9) plays(mike, drums)

    • Prolog successfully exits the above unification.
  23. Call: (9) plays(alice, _7040)

    • Prolog checks again if alice plays any instrument.
  24. Unify: (9) plays(alice, leadguitar)

    • Prolog finds (again) that alice plays leadguitar.
  25. Exit: (9) plays(alice, leadguitar)

    • Prolog successfully exits the above unification.
  26. Call: (9) plays(drew, _7040)

    • Prolog checks again if drew plays any instrument.
  27. Unify: (9) plays(drew, leadguitar)

    • Prolog finds that drew plays leadguitar.
  28. Exit: (9) plays(drew, leadguitar)

    • Prolog successfully exits the above unification.
  29. Call: (9) drums\=leadguitar

    • Prolog checks if the instruments played by the first and second persons are different. This corresponds to line 13. Since mike plays drums and alice plays leadguitar, this check succeeds.
  30. Exit: (9) drums\=leadguitar

    • Prolog successfully exits the above check.
  31. 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 alice and drew play leadguitar, this check fails.
  32. Fail: (9) leadguitar\=leadguitar

    • The above check fails.
  33. Redo: (9) plays(drew, _7040)

    • Prolog backtracks to find another instrument that drew plays.
  34. Unify: (9) plays(drew, baseguitar)

    • Prolog finds that drew also plays baseguitar.
  35. Exit: (9) plays(drew, baseguitar)

    • Prolog successfully exits the above unification.
  36. Call: (9) drums\=leadguitar

    • Prolog checks if the instruments played by the first and second persons are different. This succeeds.
  37. Exit: (9) drums\=leadguitar

    • Prolog successfully exits the above check.
  38. Call: (9) leadguitar\=baseguitar

    • Prolog checks if the instruments played by the second and third persons are different. This corresponds to line 14. Since alice plays leadguitar and drew plays baseguitar, this check succeeds.
  39. Exit: (9) leadguitar\=baseguitar

    • Prolog successfully exits the above check.
  40. Call: (9) drums\=baseguitar

    • Prolog checks if the instruments played by the first and third persons are different. This corresponds to line 15. Since mike plays drums and drew plays baseguitar, this check succeeds.
  41. Exit: (9) drums\=baseguitar

    • Prolog successfully exits the above check.
  42. Exit: (8) combo(mike, alice, drew)

    • Prolog successfully finds that mike, alice, and drew form a valid combo.
2 Likes