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.
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.
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.