The keyword I use to search the SWI-Prolog source code for where the Prolog predicates are implemented in C is PRED_IMPL
The way I think of the code is that what can be done with Prolog is done in Prolog, the low level actions such as unification are done in C. Then if a predicate needs to be faster it is done with calls straight into C.
Sometimes, as Jan notes, the code is so tricky that it is better to implement in C than Prolog.
That is why I say AFAIK it does not use a Virtual Machine such as WAM or an Intermediate Language such as JVM or CIL.
I know Jan will correct me if this is wrong and then I will have learned something.
Related questions:
How can a foreign language predicate also be a built-in predicate?
“decompiled prolog”
Personal Notes
?- vm_list(append).
========================================================================
append/2
========================================================================
0 s_trustme(clause(638140))
----------------------------------------
clause 1 (<clause>(000001707FC6F2F0)):
----------------------------------------
0 i_enter
1 b_atom(list)
3 b_var0
4 i_call(error:must_be/2)
6 b_var0
7 b_var1
8 i_depart(lists:append_/2)
10 i_exit
========================================================================
append/3
========================================================================
0 s_list(clause(735160),clause(641508))
----------------------------------------
clause 1 (<clause>(000001707FCCDEE0)):
----------------------------------------
0 h_nil
1 h_void
2 h_var(1)
4 i_exitfact
----------------------------------------
clause 2 (<clause>(000001707FC72790)):
----------------------------------------
0 h_list_ff(3,4)
3 h_void
4 h_list
5 h_var(3)
7 h_firstvar(5)
9 h_pop
10 i_enter
11 b_var(4)
13 b_var1
14 b_var(5)
16 i_depart(lists:append/3)
18 i_exit
========================================================================
append/1
========================================================================
0 i_fopen
1 i_fcalldetva(-4611686413639185926)
3 i_fexitdet
true.
Source code:
pl-wam.c
pl-vmi.c
pl-supervisor.c
pl-comp.c
pl-export
From: pl-vmi.c
Virtual machine instruction names. Prefixes:
I_ | General instructions |
B_ | Body specific version |
H_ | Head specific version |
A_ | Arithmetic compilation specific |
C_ | Control (compilation of ;/2, etc.) |
S_ | Supervisor instructions. See pl-supervisor.c |
References:
Logic Programming Implementation - Part I: The WAM
Limits on memory areas - Notes trail stack which is also part of WAM.
Warren’s Abstract Machine - A Tutorial Reconstruction - List the basic instructions of WAM such as put_structure
, set variable
, unify_value
, unify_variable
.
An Abstract Prolog Instruction Set
From pl-comp.c
This module (pl-comp.c) forms together with the module ‘pl-wam.c’ the complete
kernel of SWI-Prolog.
Excerpts from: _ Bowen et al. , 1983 D. L. Bowen, L. M. Byrd, and WF. Clocksin. A portable Prolog compiler.
We have opted for the structure-copying method of [Mellish 80] and [Bryunooghe 80], rather than the structure-sharing [Warren 77].
Our storage management strategy is basically that of [Warren 77], i.e. there is a heap containing the program, a “local” stack for control information and variable bindings, a “global” stack for structures, and a "trail stack which keeps track of when variables are bound so that they can be reset to “uninstantiated” at the appropriate time on backtracking. One change is that a reference count is maintained for each clause so that pointers to clauses can safely be included in asserted terms.
As our run-time system is based on previously published work [Warren 77] [warren 80]
EDIT: After responses (1) (2) by Jan W.
References:
SWI-Prolog Implementation history
Bowen et al. , 1983
D. L. Bowen, L. M. Byrd, and WF. Clocksin. A portable Prolog compiler. In L. M. Pereira, editor, Proceedings of the Logic Programming Workshop 1983 , Lisabon, Portugal, 1983. Universidade nova de Lisboa. - Explains low level concepts such as control instructions: enter
, call
, and exit
.
Neumerkel, 1993
Ulrich Neumerkel. The binary WAM, a simplified Prolog engine. Technical report, Technische Universität Wien , 1993 - The binary WAM, a simplified Prolog engine - Notes design concepts of Prolog abstract machines (as opposed to virtual machine VM) at time, including ZIP.
EDIT:
After response by Jan B.
This reminds me of minProlog and specifically the execution engine (solve.ml)
This also reminds me of the MIT open couseware lecture of advanced data structures (videos) . There was one in particular but I can’t remember the exact one.