"Logal": Algorithmic Control Structures for Prolog

D. C. Dodson, Alan L. Rector

Abstract

In the early 1980s, Prolog was celebrated for its logical elegance but criticized for its "assembler-level" control flow when implementing complex algorithms. This paper introduces Logal, a library of high-level control predicates designed to make Prolog programming more readable, maintainable, and "respectable" for large-scale applications. By abstracting low-level primitives like cut and repeat into structured loops and conditionals, Logal allows developers to express algorithmic routines naturally without sacrificing Prolog's inherent backtracking power.

Introduction

Early Prolog development often oscillated between two extremes: mathematically pure logic and "spaghetti code" laden with opaque cut (!), fail, and repeat predicates. While the language is powerful for symbolic reasoning, its native control flow can be difficult to manage for standard procedural algorithms. This lack of structure often led to code that was error-prone and hard to comprehend.

The authors of Logal sought to bridge this gap by providing a preliminary set of high-level control predicates. These constructs allow programmers to express loops and complex conditionals in a style reminiscent of ALGOL 68 or Pascal, while remaining fully compatible with the logic programming paradigm.

The Generalized Loop Construct

The centerpiece of Logal is the Generalized Loop, a modular "building block" syntax that replaces the traditional repeat-fail pattern. A loop in Logal is composed of four optional phrases that must appear in a specific order, as illustrated in Table 1.

Table 1: The four modular components of the Logal generalized loop.

The loop is terminated by a mandatory od keyword. To accommodate the nuances of logic programming, the authors introduced two variants for the loop body:

  • do_any: The loop continues even if the body fails during an iteration.
  • do_one: The entire loop fails if the body fails a single time.

This modularity allows for a wide variety of loop types (e.g., for-do, while-do, do-until) to be constructed from a single unified syntax.

Backtracking and Control

Logal leverages Prolog's unique features rather than fighting them. A key insight is the use of Backtracking as a Generator. The <for-phrase> does not simply increment a counter; it calls a predicate and uses backtracking to find every possible instantiation, effectively transforming a logic search into a loop generator.

Furthermore, Logal acts as "syntactic sugar" for the cut operator. Much of the underlying implementation uses cuts to manage the search space and prevent unwanted backtracking once a specific branch or loop iteration has been decided. By encapsulating these cuts within high-level constructs, Logal prevents common logic errors associated with manual cut placement.

Implementation and Practical Application

The Logal system was implemented on a PDP-11 running UNIX Prolog (version NU7). Despite the high-level nature of the constructs, the implementation was remarkably efficient, requiring only 15 top-level Prolog clauses. This demonstrates that sophisticated control structures can be built on top of low-level primitives with minimal overhead.

The practical utility of Logal was proven through its use in developing medical decision support systems at the University of Nottingham. In these complex diagnostic environments, the structured syntax was found to be of "great value" for managing intricate algorithms that would otherwise have been difficult to maintain in pure Prolog.

Conclusion

Logal represents a significant step toward making Prolog a viable language for complex, algorithm-heavy applications. While the implementation faced some syntactic constraints of the era—such as requiring slightly different keywords based on phrase position—it successfully demonstrated that ergonomic improvements could make the "algorithmic use of Prolog respectable." Future work suggested further refinements to operator declarations to achieve even cleaner syntax and greater flexibility in loop sequencing.

Summary updated

BibTeX
@inproceedings{dodson1983logal,
  author = {D. C. Dodson and Alan L. Rector},
  title = {{"Logal": Algorithmic Control Structures for Prolog}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {1983},
}
Figure