0

I am trying to program a WAM implementation of Prolog in C. I have noticed that the Prolog datatypes are described in only four token types: REF, STR, CON and LIS.

Each cell in the execution heap contains the type of the token, and its position in the heap.

enter image description here

Notice that there isn't any reference to its actual name (i.e. Z, W). Shouldn't the heap point to a symbol and its value in a symbol table? Is there a symbol table in the pure prolog implementation? Does my parser create a symbol table or construct a heap? The WAM A Tutorial Implementation doesn't mention any of that.

Skalwalker
  • 299
  • 3
  • 23
  • 1
    `Z` and `W` are logical variables. The WAM does not need to keep track of the variable names. In this example Z was assigned the cell 2 and W the cell 3, so what needs to be consistent is the cell assigned to them as shown in the figure you posted. There should be some sort of atom table to keep track of the atoms (in the example `p`, `h` and `f`). – gusbro Oct 02 '20 at 20:50

1 Answers1

3

Think of WAM as a kind of machine code -- there are no symbol tables in the machine code, although there might be a separate section in the executable that provides information that a debugger or other tools1 can use to display values by name. Many Prolog implementations can also show the local variable names, but that's outside the scope of WAM.

Of course, a local symbol table is used when compiling a clause to WAM, but it's local to a single clause and there's non of the complications about scope that you see in conventional programming langauges.

Consider the following (using SWI-Prolog):

1 ?- [user].
|: foo(Char) --> [Char], {check(Char)}, bar(Char). 
|: 
% user://1 compiled 0.03 sec, 1 clauses
true.

2 ?- listing(foo).
foo(A, [A|B], C) :-
    check(A),
    D=B,
    bar(A, D, C).

A clever implementation could render listing(foo) as:

foo(Char, [Char|B], C) :-
    check(Char),
    bar(Char, B, C).

but internally, it's exactly the same. The extra variables for the DCG expansion never had names, so they're just given arbitrary names such as B and C in the listing.

Peter Ludemann
  • 985
  • 6
  • 8
  • So after I parse my language, I should use the syntax tree to directly create my heap? – Skalwalker Oct 02 '20 at 23:00
  • I don't understand your question. Do you mean using the syntax to determine what kind of instruction to emit? If so, it's a bit like compiling C++ or Python: some code results in allocating on the stack and other code allocates on the heap - the syntax tree isn't needed when executing the machine code (for C++) or byte code (for Python) (I haven't looked at WAM for literally decades, so perhaps someone who's worked on this more recently can help you better.) – Peter Ludemann Oct 04 '20 at 00:08