Mildly. I’d start reading https://www.swi-prolog.org/download/publications/A_Portable_Prolog_Compiler.pdf. After all, that is what started SWI-Prolog. Next is listing virtual machine code, see how it is implemented in pl-vmi.c and how the clause indexing works from SWI-Prolog -- Manual and, if your brave, from pl-index.c
. The backtracking stuff is in PL_next_solution() of pl-wam.c
and you’ve covered most of the VM.
That should take most seasoned C programmers a couple of weeks at the max. As several people have argued here, that is probably not worth the trouble if you want to use Prolog. Yes, one has to understand roughly the clause indexing of the implementation you use (the above docs should suffice). Structure sharing implementations are out of fashion these days. They do have impact though. For example, where
member(X, [a,b,c,d])
may not be too bad in a structure sharing system, in a copying implementation you much better write the code as below as the member/2 solution first pushes [a,b,c,d]
onto the stack and than calls the non-indexed member/2 (also for structure sharing). If the member/2 succeeds you most likely leave a choicepoint (also for structure sharing) and eventually GC has to discard the list (not for structure sharing).
valid(a).
valid(b).
valid(c).
valid(d)
and use
valid(X).
Of course, it isn’t really hard to write macro expansion to do this for you
In most cases I prefer to write the code by hand as it also allows me to invent a good name that makes clear what I want to state about X