Prolog

71 views
Skip to first unread message

Cubreacov Misha

unread,
Apr 18, 2018, 2:12:23 AM4/18/18
to SWI-Prolog
Hello, I have a question like this: I would like to know how the prolog analyzes the .pl file ie the C code if it can

Barb Knox

unread,
Apr 18, 2018, 5:47:37 PM4/18/18
to SWI-Prolog, Cubreacov Misha
On 18/04/2018, at 18:12, Cubreacov Misha <mcubr...@gmail.com> wrote:

Hello, I have a question like this: I would like to know how the prolog analyzes the .pl file ie the C code if it can

I infer (with less than full confidence) that the question is how a Prolog system translates Prolog code (from a .pl file) into something executable by a computer.

First, SWI Prolog (and I believe most other Prolog systems) does not translate the Prolog code into directly-executable "machine code" like C does for C code.  Instead, it translates the Prolog code into a “bytecode”, such as the “Warren Abstract Machine”.

Then to actually execute the bytecode it is read by an “interpreter” program (part of the Prolog system) which performs the actions specified by the bytecode.

(See Wikipedia or other Net resources for explanations of the quoted terms.)


HTH.

-- 
---------------------------
|  BBB                b    \    Barbara at LivingHistory stop co stop uk
|  B  B   aa     rrr  b     |
|  BBB   a  a   r     bbb   |   ,008015L080180,022036,029037
|  B  B  a  a   r     b  b  |   ,047045,L014114L4.
|  BBB    aa a  r     bbb   |
-----------------------------

Cubreacov Misha

unread,
Apr 19, 2018, 1:39:43 AM4/19/18
to SWI-Prolog

But the source code of this "interpreter", you do not know where I could find .

Jan Wielemaker

unread,
Apr 19, 2018, 11:35:09 AM4/19/18
to Cubreacov Misha, SWI-Prolog
On 18/04/18 22:39, Cubreacov Misha wrote:
> But the source code of this "interpreter", you do not know where I could
> find .

https://github.com/SWI-Prolog/

In particular the files pl-wam.c and pl-vmi.c from swipl-devel.git

I consider "interpreter" a bit misleading term. This term was used long
ago for languages that executed the statements directly from the source
code. That is rare these days. Possibly shells still do that. Most
modern high level languages use the `virtual machine' design where the
source is translated to instructions for a virtual machine that is
typically implemented in C/C++. That holds for Prolog, Python, Java,
JavaScript, Perl and many others. Some of these perform `hotspot
optimization' where heavily used bits of code are detected at runtime,
optimized and compiled to machine code using e.g., LLVM.

The most polular VM is the WAM (Warren Abstract Machine). Despite the
naming, the SWI-Prolog VM is based on a minimal VM described by Pereira
and Byrd which is closely related to the `ZIP' VM. The main difference
is that this VM passes arguments over the stack rather than using
registers.

I'm not entirely sure what the status here is wrt. Prolog. Surely
SWI-Prolog uses a VM but no hotspot optimization. The only (but crucial)
runtime optimization peformed is creating `clause indexes':
(hash-)tables that allow it to find candidate clauses from a given goal
quickly.

Some Prolog systems do compile to native code, often using the VM code
as intermediate. GNU-Prolog is a good example. All in all, the advantage
of native code for Prolog is not obvious. The VM instructions have a
larger granularity than for imperative languages which significantly
reduces the overhead of the VM. Using VM code allows for decompilation
and runtime reachability analysis for GC.

If you want to study this stuff, I'd start with some minimal WAM based
implementation as this is all well described in the literature.

Cheers --- Jan

German Vidal

unread,
Apr 19, 2018, 11:39:59 AM4/19/18
to Cubreacov Misha, Jan Wielemaker, SWI-Prolog
Just let me add that Hassan Aït-Kaci's book is still an excellent
starting point for understading the WAM:

http://wambook.sourceforge.net/

Cheers,
German
> --
> You received this message because you are subscribed to the Google Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to swi-prolog+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Peter Ludemann

unread,
Apr 19, 2018, 1:06:28 PM4/19/18
to SWI-Prolog
If you're interested in more advanced techniques, do a web search for "Aquarius Prolog", "BinProlog", "Project Mercury" (and I apologize for omissions).
Reply all
Reply to author
Forward
0 new messages