Abstract
We present a scheme for translating logic programs, which may use aggregation and arithmetic, into algebraic expressions that denote bag relations over ground terms of the Herbrand universe. To evaluate queries against these relations, we develop an operational semantics based on term rewriting of the algebraic expressions. This approach can exploit arithmetic identities and recovers a range of useful strategies, including lazy strategies that defer work until it becomes possible or necessary.
Introduction
Traditional logic programming languages like Datalog and Prolog offer a declarative approach to computation, but they often struggle with "the infinite." Specifically, they face challenges with infinite breadth (variables over infinite domains), infinite depth (non-terminating recursion), and complex aggregation (e.g., combining recursion with sum or min). Standard execution strategies like forward or backward chaining often fail to terminate or require hand-tuned constraints to operate safely.
This paper introduces a formal calculus for "bag relations" (multisets) that allows logic programs to be translated into algebraic expressions. By applying a suite of term-rewriting rules to these expressions, the authors create a flexible execution engine capable of discovering efficient evaluation strategies—such as lazy evaluation and short-circuiting—automatically.
The Calculus of Bag Relations
The authors propose a system based on R-exprs (Relational Expressions). This relational algebra operates on bags where items can appear multiple times or even infinitely.
- Algebraic Operations: The calculus includes standard operations like union (
+), join (*), and projection, but extends them to handle aggregation operators such assum,min, andcount. - Multiplicity as a Bridge: By treating relations as mappings to "multiplicities" (the count of a tuple's existence), the system provides a unified treatment of sets, bags, and weighted logic.
- Translation: The paper provides a mechanical scheme to translate programs from languages like Dyna, Datalog, and Prolog into these R-exprs.
Operational Semantics: Execution as Rewriting
The core contribution is an operational semantics based on term rewriting. Instead of "running" a program in the traditional sense, the system treats a query as a mathematical expression to be simplified.
By viewing a logic program as a single algebraic expression, the system can use mathematical properties—such as associativity, commutativity, and distributivity—to rearrange computation. The authors formalize over 50 rewrite rules that preserve the denotation of bag relations. This approach recovers classic database strategies (like nested-iterator joins) and logic programming strategies (like unification) as specific sequences of rewrites.
The SIMPLIFY Algorithm
The authors describe a top-down implementation called the SIMPLIFY algorithm. This algorithm maintains an "environment" of currently known constraints, allowing the system to:
- Short-circuit: Stop evaluation early if a contradiction is found.
- Delay: Postpone constraints that cannot yet be solved until more information arrives.
- Materialize: Gradually transform an "intensional" description (the code) into an "extensional" description (the results), stopping as soon as the query is satisfied.
Evaluation and Applications
As a theoretical and algorithmic contribution, the paper evaluates the approach using complex programmatic examples rather than standard datasets:
- All-Pairs Shortest Path: A classic recursive graph problem requiring
minaggregation. - Convolutional Neural Networks (CNNs): The authors demonstrate how a CNN can be expressed as a logic program over an infinite grid of neurons, where rewriting handles sparse connections efficiently.
- Peano Numerals: Used to show how the system handles infinite streams of data without crashing.
Conclusion and Future Work
The proposed calculus provides a robust framework for evaluating logic programs with built-ins and aggregation. While the system can answer queries about infinite relations that would cause traditional engines to loop, several challenges remain. The current Python-based interpreter is slower than specialized Datalog compilers, and the order in which rewrite rules are applied (the strategy) is crucial for performance. Future research will explore using machine learning to learn optimal rewriting strategies and formalizing the interaction between memoization and the rewrite rules.
Summary updated