arXiv:2610.03234cs.PLOct 2026

Manuscript Offprint

arXiv:2610.03234

WAMpy: Efficient Synthesis of Prolog Programs in Python

Dominik Magiera, Lukas Röhrig, Frank Jäkel

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original. This text is shared under the same licence.

Dominik Magiera [0009−0002−0643−9973]^{[0009-0002-0643-9973]}, Lukas Röhrig [0009−0009−9664−8226]^{[0009-0009-9664-8226]}, and Frank Jäkel [0000−0002−1355−7663]^{[0000-0002-1355-7663]}

Centre for Cognitive Science & Institute of Psychology, Technische Universität Darmstadt, Germany

Abstract. We present WAMpy, a Python framework optimized for synthesizing Prolog programs. Unlike general-purpose Prolog systems, WAMpy targets workloads that repeatedly generate and evaluate small candidate programs. WAMpy compiles Prolog clauses into NumPy array-based WAM instructions and supports partial recompilation of hypotheses against fixed background knowledge. Performance-critical routines are accelerated using Numba just-in-time (JIT) compilation. In a benchmark of repeated compilation-and-evaluation workloads, WAMpy improves end-to-end performance compared with SWI-Prolog accessed from Python using Janus.

WAMpy is available on github.com/cognitive-modeling/WAMpy.

1 Usage and Implementation

Program synthesis constructs programs from incomplete specifications, such as examples or constraints [1]. This process often involves repeatedly compiling and evaluating candidate programs while the background knowledge remains unchanged. Existing general-purpose Prolog systems such as SWI-Prolog [2] are not optimized for this workload. We therefore present WAMpy, a specialized execution framework based on the Warren Abstract Machine (WAM) [3,4]. WAMpy is implemented in Python and uses Prolog as its source language, enabling direct integration with program-synthesis and machine-learning workflows. It compiles programs into a targeted subset of array-based WAM instructions. Performance-critical routines are accelerated using Numba JIT compilation [5].

Usage. WAMpy is used as a Python library rather than as a standalone Prolog interpreter. The following example compiles Prolog source into WAM instructions and evaluates a query through the high-level API:

import wampy as wam

prolog = wam.Prolog("parent(anakin, luke).")

list(prolog.query("parent(X, luke)."))

The high-level API manages parsing, compilation, and query backtracking.

D. Magiera et al.

Figure

Fig. 1. WAMpy compilation and execution workflow. a. Pipeline from Prolog source code to query results. b. NumPy-based representation of a parsed term. c. Partial recompilation adds a candidate clause to precompiled background knowledge.

Implementation. Uncompiled programs are represented as fixed-size arrays of top-level term trees. Each parsed term is stored as a separate AST with its own root node and integer identifier. Programs can be parsed from Prolog source code, but are primarily intended to be modified directly at the AST level during synthesis. Each program is constrained by fixed upper bounds on the number of terms, nodes per term, and children per node. Memory is allocated according to these bounds, so all programs share the same array layout. This increases memory use, but enables efficient processing by Numba-compiled routines. Internally, programs use fixed-size NumPy arrays: a symbol array indexed by term and node, and a child array indexed by term, node, and child slot. The number of child slots is configured in advance (Fig. 1b).

Compilation lowers these AST's into a two-dimensional array, where each row represents one instruction in our WAM subset. Each instruction consists of an opcode followed by three arguments. The compiler also constructs a predicate entry table that maps functor-arity pairs to program locations. The resulting instruction array and predicate entry table are illustrated in Fig. 1a. Multiple clauses for the same predicate are handled using WAM-style choice instructions to support backtracking.

At runtime, queries are evaluated using Selective Linear Definite (SLD) clause resolution [6], where unification produces bindings and backtracking explores alternative clauses. The framework provides built-in tracing of predicate calls during execution, which can be used to evaluate candidate hypotheses.

WAMpy: Efficient Synthesis of Prolog Programs in Python

3

Efficient Partial Recompilation. WAMpy reduces compilation overhead by compiling background knowledge once and recompiling only candidate hypotheses. Candidate clauses are appended to the fixed instruction sequence and tried before background clauses for the same predicate. In the single-clause case shown in Fig. 1c, the compiler appends a JMP_TRUST bridge that transfers backtracking to the precompiled background clause. Predicates with multiple background clauses use JMP_RETRY to preserve further alternatives.

Limitations. WAMpy implements only an X-register subset of the WAM [4], for repeated compilation and evaluation in logic program-synthesis workloads. This subset supports atoms, variables, compound structures, predicate calls, tail calls, unification, and linear clause backtracking. It does not provide a general SWI-Prolog built-in predicate library, including facilities such as arithmetic and meta-calls.