Title: M-Prolog SCBM compiler now runs the N-Queens problem
Title: M-Prolog SCBM compiler now runs the N-Queens problem
After about four months of experimenting with a new Prolog compiler architecture, I finally got the N-Queens problem working correctly in compiled M-Prolog.
I call the architecture SCBM (Success Continuation Backtracking Machine).
The basic idea is fairly simple: instead of compiling Prolog to an abstract machine such as the WAM, SCBM compiles nondeterministic predicates directly into C and implements control flow and backtracking using goto and GCC's computed goto extension.
The hardest problem was restoring local variables correctly after backtracking.
After a lot of trial and error, I ended up with a relatively simple solution: variable pointers are propagated through success continuations. When backtracking occurs, local variables are reconstructed from information preserved in the original success continuation.
It took a lot of debug output to find this solution. AI was also very useful as a second pair of eyes for analyzing traces and generated C code.
The result:
- 4-Queens: all solutions generated correctly by backtracking
- 8-Queens: all 92 solutions confirmed
- 9-Queens: working correctly as well
Performance is not yet the main focus. For the complete 9-Queens search, the current implementation is roughly 3–4x slower than SWI-Prolog.
There is still plenty of low-hanging fruit in the implementation, particularly in data structures, pointer handling, generated code, and builtin calls, so I think there is considerable room for improvement.
For me, getting Queens working is an important milestone because it exercises recursion, nondeterminism, nested backtracking, and restoration of local variables together.
I'm aiming for M-Prolog Ver. 1.0 on August 31, 2026.
SCBM is not intended as a replacement for the WAM. I'm exploring whether a much simpler direct-to-C approach can provide another practical way to implement a Prolog compiler.
I'll be interested to hear what experienced Prolog implementers think of the approach.