How the Interpreter of SWIPL works ...?

Hello,
I just would ask, how the Interpreter of SWIPL works:
I have two Variables:
N = R.
R = 42.

at first run, the Interpreter does not know, what R is - or ?
in the next Line R = 42. there.
Then give it a second run, so N becomes 42 ?
How many passes did the Interpreter do ?
what is, when R = N. ?
a circular circumstance ?

Can you show that with a screenshot? I know you are new to Prolog and do not know the terminology so what you are describing has me guessing at the meaning, which could mean many things and more likely my guess would be incorrect.

The = operator is one of the more confusing points of learning Prolog. Many take it that = is the same as other programming languages where it means assignment, in Prolog it is unification, or more specifically syntactic unification.

See: SWI-Prolog -- (=)/2

Over the decades I have read many intros and books to Prolog and IMO many do not explain syntactic unification at all or in enough detail before using it with examples. Many find learning it hard because they bring along ideas from elsewhere that causes confusion but once you learn syntactic unification you find how easy it is and how powerful an idea it is.

Here are some ChatGPT prompts related to syntactic unification.

When learning syntactic unification consider using this as a cheat sheet

Examples of syntactic unification of first-order terms

Here are the examples demonstrated using SWI-Prolog.

Many find the use of either the line debugger trace/0 or the GUI debugger gtrace/0 to be helpful.

Overview of the Debugger


To also help in learning syntatic unification see

It demonstrates the terms as trees

image

and also demonstrates the variables and values using other symbols to help one transition to thinking in other patterns.

image


As for your title question

How the Interpreter of SWIPL works

Towards the end of this topic is related information

In paticular https://www.swi-prolog.org/download/publications/A_Portable_Prolog_Compiler.pdf

2 Likes

Thank you for the Information’s.
You provide a ScreeShot, so it is not neccassary, yet.
SFAH (So far as I heard), N = R is “not” a Assignment, okay. This also means “((x = y) or (x # y))”:
the Ball is green, or thre Ball is not green.
when X is Ball, and Y the Color.
Each sentences are TRUE: 1. (the Ball is green), and 2. (the Ball is not green).
So, I can interpret it:
N = R → Number N “is” 42 (true), OR, Number N “is not” 42 (true) ?

The boolean Wallet: TRUE or TRUE equals TRUE ?

So, N becomes TRUE. okay, so far.

One of the major items that needs to be considered with those statements is if the variables are bound, until then I would not jump to the conclusion that something is NOT.
Also be careful with comparisons, more often (==)/2 is what is needed instead of =/2 and that can cause lots of headaches.

What kind of “run” do you mean? Can you demonstrate it somehow, maybe on the top level?

So, if you are at the prompt that looks like this:

?-

what would you type there to test your question?

@Boris this like:
grafik

Is this what you are trying to do?

image

The first line is the query ?- R = 42, N=R,write(N).
The next line 42 is the output of write(N)
The last line R = N, N = 42. shows the MGU (Most General Unifier), (not sure if that is the correct terminology to use here)

The query is actually three predicates combined into a logic statement (goal), connected by the AND operator ,/2.
Note: In Prolog ;/2 is the OR operator.

?- =(R,42),=(N,R),write(N).
42
R = N, N = 42.

Taking that and using ,/2 as a prefix operator instead of an infix operator.
Note: To use ,/2 as a prefix operator need to quote the comma operator, i.e. ','
Note: This is only for demonstration purposes, it is not what most users would expect or have even seen.

?- ','(','(=(R,42),=(N,R)),write(N)).
42
R = N, N = 42.

For other ways to view the terms.
For the goals think abstract syntax tree (AST), for the terms think pretty print.
Note: The result of write_canonical/1 is not an MGU it is a linear representation of the expression with the operators in prefix form.
Note: The result of print_term/2 is not an MGU it is a pretty print of a term.

Ě€?- write_canonical((R = 42, N=R,write(N))).
','(=(A,42),','(=(B,A),write(B)))
true.

?- write_canonical(','(','(=(R,42),=(N,R)),write(N))).
','(','(=(A,42),=(B,A)),write(B))
true.

?- print_term((R = 42, N=R,write(N)),[]).
A=42,B = A,write(B)
true.

?- print_term(','(','(=(R,42),=(N,R)),write(N)),[]).
(A=42,B = A),write(B)
true.

Note: write_canonical/1 is nice for understanding the structure of goals and print_term/2 is nice for understanding and viewing complex terms.

2 Likes

okay, yes.
So, we have three Situation’s:

  1. ?- N = 42, R = N, write( N ). → 42 RET R = N, N = 42. % RET stands for RETURN - Linebreak
  2. ?- N = 42, R = N. → R = N, N = 42
    3:
    ?- N = 42.
    ?- R = N.
    ?- write( R ).
    _12345
    true.

as I see this. I interpret it, to get the above output:

 |  push on stack --> =(R, 42).
 |  push on stack --> =(N, R).
\ / push on stack --> write( N ).
 '

The interpreter get the source from right, and push the result to left destination on a Stack.
Then, when the Information’s collected, the Interpreter pops, and decrease the Stack.

In this case, the Stack in size is limited - or ?

So, it would be, that the Interpreter works with Lists instead ?
Which order has then the List in Context of “order”.
See this (as List);

1. foo, foo, foo
2. foo, foo
3. foo.
4. foo, foo, foo, foo

…
then, the Top of order is on the End (Level 4,).
Or will “order” the Interpreter, the sets of Commands (Facts, Rules, …) ?

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