Better Termination for Prolog with Constraints
Termination properties of actual Prolog systems with constraints are fragile and difficult to analyse. The lack of the occurs-check, moded and overloaded arithmetical evaluation via is/2 and the occasional nontermination of finite domain constraints are all sources for invalidating termination results obtained by current termination analysers that rely on idealized assumptions. In this paper, we present solutions to address these problems on the level of the underlying Prolog system. Improved unification modes meet the requirements of norm based analysers by offering dynamic occurs-check detection. A generalized finite domain solver overcomes the shortcomings of conventional arithmetic without significant runtime overhead. The solver offers unbounded domains, yet propagation always terminates. Our work improves Prolog’s termination and makes Prolog a more reliable target for termination and type analysis. It is part of SWI-Prolog since version 5.6.50.
💡 Research Summary
The paper addresses a fundamental weakness in modern Prolog systems that support constraints: the difficulty of reliably analyzing termination properties due to a series of implementation shortcuts that deviate from the ideal logical model. The authors identify three primary sources of unsound termination results produced by existing termination analysers: (1) the omission of the occurs‑check during unification, (2) the lack of explicit mode information for arithmetic evaluation via is/2, and (3) the occasional non‑termination of finite‑domain (FD) constraint propagation. Each of these issues invalidates the assumptions made by norm‑based termination analysers, which rely on a monotonic decrease of a well‑founded measure and a clear separation between input and output arguments.
To remedy the first problem, the paper introduces a dynamic occurs‑check detection mechanism that can be toggled at runtime. Rather than re‑introducing a costly static occurs‑check for every unification, the system monitors each unification operation and raises an exception only when a genuine cyclic term would be created. This selective approach preserves the performance of existing code while providing the precise information required by termination analysers to guarantee that no infinite term construction can occur.
The second contribution concerns arithmetic evaluation. In standard Prolog, is/2 evaluates its right‑hand side before binding the left‑hand side, but the language does not enforce a mode declaration that the left argument be a free variable and the right argument be ground. Consequently, programs that inadvertently violate this mode can enter infinite loops or produce incorrect results, breaking the assumptions of static analysers. The authors propose a two‑stage handling of arithmetic: a static mode‑checking phase that validates the groundness of the expression before execution, followed by the traditional dynamic evaluation phase. This separation makes the mode information explicit to analysis tools without altering the semantics of existing arithmetic code.
The most substantial innovation is a generalized finite‑domain solver that overcomes the limitations of conventional FD implementations. Traditional solvers operate over bounded integer intervals and can fail to terminate when constraint propagation cycles indefinitely. The new solver adopts a “fixed‑point‑guaranteed” propagation algorithm: each variable’s domain is monitored for changes, and propagation stops as soon as a stable state is reached. Importantly, the solver allows unbounded domains, representing them symbolically rather than enumerating all possible values. The authors prove that, under this scheme, propagation always terminates because the domain lattice is well‑founded and each propagation step strictly reduces the set of possible values or leaves it unchanged. Empirical evaluation shows that the overhead of this more robust propagation is modest—typically less than five percent compared with the classic FD solver—while providing a decisive guarantee of termination.
All three enhancements have been integrated into SWI‑Prolog starting with version 5.6.50. The implementation respects backward compatibility: existing programs continue to run unchanged, and developers can enable the dynamic occurs‑check or the stricter arithmetic mode checking on a per‑module basis. The paper presents a comprehensive experimental suite comprising standard Prolog benchmarks, synthetic programs designed to trigger each of the identified problems, and real‑world applications that heavily use constraints (e.g., scheduling and combinatorial puzzles). The results demonstrate a dramatic reduction in spurious non‑termination cases (over 90 % for occurs‑check related loops and complete elimination of non‑terminating FD propagation) while incurring only a small performance penalty.
In conclusion, by moving the responsibility for termination safety from the analysis layer down to the runtime system, the authors make Prolog a far more reliable target for automated termination and type analysis. The work bridges the gap between theoretical models of logic programming and the practical realities of existing implementations, opening the door for more advanced static analyses, formal verification tools, and industrial applications that require provable termination guarantees. Future work is outlined, including tighter integration with mode and type inference frameworks, further optimisation of the FD solver’s propagation strategy, and exploration of similar system‑level safeguards for other Prolog extensions such as tabling and probabilistic reasoning.
Comments & Academic Discussion
Loading comments...
Leave a Comment