Abstract
Standard Prolog execution relies on a rigid left-to-right, depth-first search strategy known as SLD resolution. While simple, this approach often leads to significant inefficiencies, including infinite loops and the exhaustive exploration of redundant search spaces. This paper synthesizes various control primitives from historical Prolog dialects—such as IC-PROLOG, Prolog II, and MU-PROLOG—to propose an idealized computation rule. By implementing a seven-level priority hierarchy that favors deterministic calls and early failure detection, logic programs can achieve greater efficiency and robustness without requiring manual "hacking" or complex reordering by the programmer.
Introduction
Prolog is celebrated for its declarative nature, yet its procedural execution often betrays its simplicity. The standard execution model follows a strict order that can lead to catastrophic failures in logic. For instance, a simple recursive definition like ancestor(X, Y) :- ancestor(X, Z), parent(Z, Y) will trigger an infinite loop in standard Prolog before the system even attempts to consult the underlying data.
Beyond infinite recursion, the "left-to-right" constraint struggles with the common "generator and tester" paradigm. In many logic problems, one predicate generates potential solutions while another tests their validity. If the generator runs to completion before the tester begins, the program may explore millions of dead ends that could have been pruned early. To solve this, researchers have sought "flexible control" mechanisms that allow the computer to intelligently decide which part of a program to execute next.
Evolution of Control Primitives
Lee Naish evaluates several historical attempts to introduce flexibility into logic programming:
- IC-PROLOG: Introduced "Eager Consumers" and "Lazy Producers," using annotations to allow goals to either wait for data or jump ahead in the execution queue.
- Prolog II: Introduced the
freezeprimitive, which delays the execution of a goal until a specific variable is bound (instantiated). - MU-PROLOG: Developed "Wait Declarations," a more robust approach where procedures are delayed until a specific set of arguments is available. This allows procedures to be "multi-use," functioning efficiently regardless of which arguments are provided as input.
The Idealized Priority Hierarchy
The core contribution of this work is the synthesis of these primitives into an Idealized Computation Rule. Rather than a flat execution order, Naish proposes a seven-level hierarchy to determine which "atom" (goal) the system should process next:
- Tests: Predicates highly likely to fail early should be prioritized to prune the search tree.
- Other Deterministic Calls: Calls that match only one clause provide information and bindings without increasing the branching factor of the search.
- Nondeterministic Calls: The standard work of the program where multiple paths are possible.
- Database Queries: Optimized based on the known size and indexing of the facts.
- Wait-restricted Procedures: Goals that are currently restricted by wait declarations.
- Delayed Calls: Goals specifically waiting on variable instantiation (e.g.,
freeze). - System Predicates: Low-priority checks such as arithmetic or inequality.
By following this hierarchy, a Prolog system can automatically achieve "coroutining." In this state, the generator and tester pass control back and forth dynamically, minimizing the search space without requiring the programmer to manually interleave the code.
Key Insights and Heuristics
Two primary objectives drive the design of these control rules: detecting failure quickly and avoiding unnecessary failure branches.
Determinism as a Heuristic
One of the most powerful insights in the paper is the use of determinism. A deterministic call is one that matches exactly one clause in the program. Executing these first is almost always optimal because they provide essential data bindings without creating new branches in the search tree. This reduces the overall complexity of the computation.
The Requirement of Fairness
To solve the "ancestor" problem and avoid infinite loops, a computation rule must be "fair." A fair rule ensures that every goal in the queue eventually receives processing time. Naish suggests incorporating a breadth-first component within the priority levels to ensure that the system does not get stuck in a single infinite branch while other successful paths exist.
Limitations and Future Work
While the proposed hierarchy offers a significant theoretical advancement, implementation challenges remain. Some rules, such as checking if a goal has zero matching clauses (to detect immediate failure), can be computationally expensive to perform in real-time. Furthermore, while the hierarchy acts as an excellent rule of thumb, there are rare edge cases where a lower-priority call might be more efficient.
Future work involves the full integration of this seven-level hierarchy into production-grade compilers. Measuring the actual speedups across diverse applications—from natural language processing to expert systems—will be essential to refining these heuristics into a standard for the next generation of logic programming languages.
Conclusion
By moving away from the rigid SLD resolution and toward a priority-based selection rule, Prolog can become both more powerful and more user-friendly. Naish’s framework provides a roadmap for a system that intelligently manages its own execution, allowing programmers to focus on the logic of their problems rather than the mechanics of the search.
Summary updated