# Curious: How does Prolog "Byte code" compare to .NET IL (and some thoughts about enterprise ready systems)

**URL:** <https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101>\
**Category:** Help!\
**Created:** [August 21, 2019, 3:27pm UTC](https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101 "2019-08-21T15:27:02Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [August 21, 2019, 3:50pm UTC](https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101/6 "2019-08-21T15:50:47Z")

</div>

The keyword I use to search the SWI-Prolog source code for where the Prolog predicates are implemented in C is [PRED\_IMPL](https://github.com/SWI-Prolog/swipl-devel/search?q=PRED_IMPL&unscoped_q=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](https://github.com/SWI-Prolog/swipl-devel/blob/9ed270918b425d123be31c323366dc5e871f52cc/src/pl-prims.c#L207-L396) 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](https://en.wikipedia.org/wiki/Warren_Abstract_Machine) 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?](https://swi-prolog.discourse.group/t/how-can-a-foreign-language-predicate-also-be-a-built-in-predicate/961)  
[“decompiled prolog”](https://swi-prolog.discourse.group/t/decompiled-prolog/729)

[Efficiency of Prolog](https://www.metalevel.at/prolog/efficiency)

* * *

> **Personal Notes**
>
> [vm\_list/1](https://www.swi-prolog.org/pldoc/man?predicate=vm_list/1).
> 
> ```prolog
> ?- 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](https://github.com/SWI-Prolog/swipl-devel/blob/ea88f5f4b6bfe3ad87bbc86a930125fdf2b18842/src/pl-wam.c)  
> [pl-vmi.c](https://github.com/SWI-Prolog/swipl-devel/blob/ea88f5f4b6bfe3ad87bbc86a930125fdf2b18842/src/pl-vmi.c)  
> [pl-supervisor.c](https://github.com/SWI-Prolog/swipl-devel/blob/ea88f5f4b6bfe3ad87bbc86a930125fdf2b18842/src/pl-supervisor.c)  
> [pl-comp.c](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/pl-comp.c)  
> [pl-export](https://github.com/SWI-Prolog/swipl-devel/blob/master/src/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](https://www.it.uu.se/edu/course/homepage/logpro/ht04/LP_Impl.pdf)  
> [Limits on memory areas](https://www.swi-prolog.org/pldoc/man?section=memlimit) - Notes trail stack which is also part of WAM.  
> [Warren’s Abstract Machine - A Tutorial Reconstruction](http://wambook.sourceforge.net/wam-slides.pdf) - List the basic instructions of WAM such as `put_structure`, `set variable`, `unify_value`, `unify_variable`.  
> [An Abstract Prolog Instruction Set](http://www.ai.sri.com/pubs/files/641.pdf)
> 
> 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](https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101/8)) ([2](https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101/9)) by Jan W.
> 
> References:
> 
> [SWI-Prolog Implementation history](https://www.swi-prolog.org/pldoc/man?section=implhistory)
> 
> **Bowen _et al._ , 1983**  
> D. L. Bowen, L. M. Byrd, and WF. Clocksin. [A portable Prolog compiler](https://www.researchgate.net/publication/273888197_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](http://www.complang.tuwien.ac.at/ulrich/papers/PDF/binwam-nov93.pdf) - Notes design concepts of Prolog abstract machines (as opposed to virtual machine VM) at time, including ZIP.
> 
> EDIT:
> 
> This reminds me of [minProlog](https://github.com/andrejbauer/plzoo/tree/master/src/miniprolog) and specifically the execution engine ([solve.ml](https://github.com/andrejbauer/plzoo/blob/master/src/miniprolog/solve.ml))  
> This also reminds me of the MIT open couseware lecture of [advanced data structures](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-851-advanced-data-structures-spring-2012/) ([videos](https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-851-advanced-data-structures-spring-2012/lecture-videos/)) . There was one in particular but I can’t remember the exact one.

---

_[View the full topic](https://swi-prolog.discourse.group/t/curious-how-does-prolog-byte-code-compare-to-net-il-and-some-thoughts-about-enterprise-ready-systems/1101)._
