TL;DR
This paper introduces "Logal," a set of high-level control structures for Prolog that aim to make algorithmic programming within Prolog more respectable and accessible. By providing constructs like generalized loops and conditionals, Logal bridges the gap between Prolog's declarative elegance and the practical need for structured algorithmic expression, potentially broadening Prolog's appeal.
What Problem Does This Paper Address?
Prolog, while powerful for declarative programming, often becomes cumbersome when implementing algorithms. Conventional Prolog programming can lead to "opaque and difficult uses of 'cut', 'fail' and 'repeat'," which obscure the code's self-documentation and increase the likelihood of errors. The authors argue that while high-level logic programming is a distant goal, there's a pressing need for structured algorithmic control within Prolog, akin to what's found in conventional programming languages. This is important both for clarity at the specification level (e.g., relating algorithms to time complexity) and for practical reasons, as programmers often need to implement well-understood algorithms without resorting to inelegant Prolog formulations.
Methods
The core contribution of Logal lies in its two main constructs: a generalized loop and a conditional.
Generalized Loop: This construct borrows from languages like ALGOL68, using a "building block" syntax with four optional "phrases":
<for-phrase>: For generating instances, implemented via backtracking.<while-phrase>: An exit test before the main body.<do-phrase>: The main body of the loop.<until-phrase>: An exit test after the main body.
These phrases are executed in a fixed order, repetitively, until a termination condition is met. Key adaptations for Prolog include:
do_anyvs.do_one:do_anyloops succeed even if thedo-phrasefails, whiledo_oneloops fail immediately if theirdo-phrasefails. This offers flexibility and efficiency.- Monadic vs. Dyadic Operators: To handle situations where phrases aren't the first in a loop, Logal uses distinct keywords (e.g.,
repeat_onevs.do_one) and operator declarations to manage syntax and avoid ambiguity. for-phraseImplementation: Instead of generating number series directly,for-phraseuses a predicate that is repeatedly backtracked until it fails, similar to "generators" in Icon.
Conditional Construct: Logal provides an If_any X then Y else_if_any Z fi structure, extending a previously described "cond" predicate. This allows for case-like structures and prevents backtracking through antecedents while allowing it through consequents.
Datasets
Not discussed in the paper.
Key Insights and Ideas
- Bridging the Declarative/Algorithmic Divide: The most significant insight is that Prolog doesn't have to be purely declarative to be elegant. Structured algorithmic constructs can coexist with logic programming, enhancing its practical utility.
do_anyvs.do_oneSemantics: The distinction betweendo_anyanddo_oneis crucial for handling failures gracefully in algorithmic loops within Prolog, offering a more nuanced control flow than a simplecall.- Operator Declarations for Readability: Using operator declarations for keywords significantly cleans up the syntax, minimizing parentheses and improving the readability of complex control structures.
- "Generator" Style for
for-phrase: Implementingfor-phrasevia backtracking is a clever way to achieve generator-like behavior without explicit state management, fitting naturally into Prolog's paradigm.
Key Results
The paper presents the implementation of the Logal control structures, which have been in regular use by the authors. While no quantitative performance benchmarks are provided, the authors state that the implementation is relatively concise despite the ergonomic difficulties of writing it. They find the Logal constructs to be of "great value" in developing medical decision support systems.
Limitations and Future Work
- Number Series Generators: The authors note that they have not initially provided for-phrases that generate number series directly, though they suggest this could be easily added.
- Further Refinement of Loops: They suggest extending the building block approach to allow an arbitrary sequence of
do-phrasesand loop termination phrases, potentially preceded by afor-phrase, using recursive definitions. - Syntax Simplification: The authors believe that extensions to Prolog interpreters, such as allowing binary prefix operators and eliminating remaining parentheses around control predicate arguments, could further simplify the syntax.
- Wider Applicability: The authors hope Logal will make the mixed use of algorithmic and logic programming more open and respectable, potentially attracting users who find Prolog too "exotic."
Summary updated