Hello fellow prologgers,

I’ve been trying to implement a WAM-based Prolog for fun and just out of pure curiosity. I’ve been following the famous WAM Tutorial reconstruction and I am really stuck/confused on Exercise 2.3. In other words:

Verify that the effect of executing the sequence of instructions shown in Figure 2.4 right after that in Figure 2.3 produces the MGU of the terms

p(Z, h(Z, W), f(W))andp(f(X), h(Y, f(a)), Y). That is, the (dereferenced) bindings corresponding toW = f(a),X = f(a),Y = f(f(a)),Z = f(f(a)).

I have a unit-test that executes these machine instructions at line 712.

The log-output of my heap-cell representation looks like this:

[DEBUG bfg_prolog::tests] [Str(1), Func(Functor(“h”, 2)), Str(13), Str(16), Str(5), Func(Functor(“f”, 1)), Ref(3), Str(8), Func(Functor(“p”, 3)), Ref(2), Str(1), Str(5), Str(13), Func(Functor(“f”, 1)), Ref(14), Str(16), Func(Functor(“f”, 1)), Str(19), Str(19), Func(Functor(“a”, 0))]

Here’s my x-register:

[DEBUG bfg_prolog::tests] [1: Str(8), 2: Ref(2), 3: Str(1), 4: Str(5), 5: Ref(14), 6: Ref(3), 7: Ref(17)]

For the most part my heap-cell state seems to agree with the exercises’s expected output, with one crucial difference: observe the cell at address 14, i.e. `Ref(14)`

. It’s a self-referential cell, in other words, an unbound variable. The way I see it, it should not be `Ref(14)`

, but should actually be `Ref(3)`

on the heap, which would effectively resolve `Str(13)`

to `f(f(a))`

. I’ve been banging my head against this problem for several days and I can’t really see what I’m doing wrong with the implementation of my instructions, deref, bind function, etc.

Can any of the WAM/compiler gurus give me any insight as to what might be going on here? I decided to implement this in Rust (FWIW, it looks really similar to other languages in the C family, so it shouldn’t be too hard to figure out what’s going on), so I apologize if this causes difficulty. Let me know if you’d like some additional information, debug-output, etc. Any tips/advice would be greatly appreciated.

Thanks in Advance,

Ebrahim Azarisooreh