Introduction
Author: Daniel Diaz
Keywords: GNU Prolog, manual, Prolog, compiler, constraints, finite domains

## 3 Introduction

GNU Prolog [5] is a free Prolog compiler with constraint solving over finite domains developed by Daniel Diaz. For recent information about GNU Prolog please consult the GNU Prolog page.

GNU Prolog is a Prolog compiler based on the Warren Abstract Machine (WAM) [9, 1]. It first compiles a Prolog program to a WAM file which is then translated to a low-level machine independent language called mini-assembly specifically designed for GNU Prolog. The resulting file is then translated to the assembly language of the target machine (from which an object is obtained). This allows GNU Prolog to produce a native stand alone executable from a Prolog source (similarly to what does a C compiler from a C program). The main advantage of this compilation scheme is to produce native code and to be fast. Another interesting feature is that executables are small. Indeed, the code of most unused built-in predicates is not included in the executables at link-time.

A lot of work has been devoted to the ISO compatibility. Indeed, GNU Prolog is very close to the ISO standard for Prolog [6].

GNU Prolog also offers various extensions very useful in practice (global variables, OS interface, sockets,...). In particular, GNU Prolog contains an efficient constraint solver over Finite Domains (FD). This opens constraint logic programming to the user combining the power of constraint programming to the declarativity of logic programming. The key feature of the GNU Prolog solver is the use of a single (low-level) primitive to define all (high-level) FD constraints. There are many advantages of this approach: constraints can be compiled, the user can define his own constraints (in terms of the primitive), the solver is open and extensible (as opposed to black-box solvers like CHIP),…Moreover, the GNU Prolog solver is rather efficient, often more than commercial solvers.

GNU Prolog is inspired from two systems developed by the same author:

- wamcc: a Prolog to C compiler [3]. the key point of wamcc was its ability to produce stand alone executables using an original compilation scheme: the translation of Prolog to C via the WAM. Its drawback was the time needed by gcc to compile the produced sources. GNU Prolog can also produce stand alone executables but using a faster compilation scheme.
- clp(FD): a constraint programming language over FD [4]. Its key feature was the use of a single primitive to define FD constraints. GNU Prolog is based on the same idea but offers an extended constraint definition language. In comparison to clp(FD), GNU Prolog offers new predefined constraints, new predefined heuristics, reified constraints,…

Here are some features of GNU Prolog:

- Prolog system:
  - conforms to the ISO standard for Prolog (floating point numbers, streams, dynamic code,…).
  - a lot of extensions: global variables, definite clause grammars (DCG), sockets interface, operating system interface,…
  - more than 300 Prolog built-in predicates.
  - Prolog debugger and a low-level WAM debugger.
  - line editing facility under the interactive interpreter with completion on atoms.
  - powerful bidirectional interface between Prolog and C.
- Compiler:
  - native-code compiler producing stand alone executables.
  - simple command-line compiler accepting a wide variety of files: Prolog files, C files, WAM files,…
  - direct generation of assembly code 15 times faster than wamcc + gcc.
  - most of unused built-in predicates are not linked (to reduce the size of the executables).
  - compiled predicates (native-code) as fast as wamcc on average.
  - consulted predicates (byte-code) 5 times faster than wamcc.
- Constraint solver:
  - FD variables well integrated into the Prolog environment (full compatibility with Prolog variables and integers). No need for explicit FD declarations.
  - very efficient FD solver (comparable to commercial solvers).
  - high-level constraints can be described in terms of simple primitives.
  - a lot of predefined constraints: arithmetic constraints, boolean constraints, symbolic constraints, reified constraints,…
  - several predefined enumeration heuristics.
  - the user can define his own new constraints.
  - more than 50 FD built-in constraints/predicates.

Copyright (C) 1999-2018 Daniel Diaz Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. More about the copyright