Nested HEX-Programs

Thomas Eiter, Thomas Krennwallner, Christoph RedlarXiv 2011

Abstract

Answer-Set Programming (ASP) is an established declarative programming paradigm. However, classical ASP lacks subprogram calls as in procedural programming, and access to external computations (like remote procedure calls) in general. The feature is desired for increasing modularity and---assuming proper access in place---(meta-)reasoning over subprogram results. While HEX-programs extend classical ASP with external source access, they do not support calls of (sub-)programs upfront. We present nested HEX-programs, which extend HEX-programs to serve the desired feature, in a user-friendly manner. Notably, the answer sets of called sub-programs can be individually accessed. This is particularly useful for applications that need to reason over answer sets like belief set merging, user-defined aggregate functions, or preferences of answer sets.

Introduction

Answer-Set Programming (ASP) is a powerful paradigm for solving hard combinatorial problems, but it has traditionally struggled with modularity. Most ASP programs are "monolithic," viewed as a single, giant set of rules. This makes it difficult to reuse code, use third-party libraries, or break a complex problem into smaller, manageable pieces.

A specific technical challenge in ASP is meta-reasoning. Because an ASP program can result in multiple "answer sets" (solutions), it is notoriously difficult for a program to reason about its own output. For example, if a developer wants to count how many shortest paths exist in a graph, a standard ASP program can find the paths, but it cannot easily count the resulting answer sets from within the same program. Developers often have to write external "glue code" in procedural languages like C++ or Python to handle these interactions, which breaks the declarative nature of the logic.

System Architecture

The authors extend HEX-programs—an existing flavor of ASP that allows "external atoms" to query outside data sources—to support nesting. The system, implemented as a plugin for the DLVHEX reasoner, introduces a "Nested HEX" architecture that allows for modular subprogram calls.

Architecture of the Nested HEX-program system within the DLVHEX framework.

As illustrated in the architecture above, the system consists of four primary components:

  1. Main HEX Program: The "host" program that coordinates the overall logic.
  2. Subprograms: Independent ASP modules that can be stored in separate files or embedded as strings.
  3. Answer Set Cache: A storage layer that tracks subprogram results to avoid redundant computations.
  4. External Atoms for Interfacing: This core innovation provides specialized atoms such as &callhex (to execute a subprogram), &answersets (to map a program handle to its resulting answer sets), and &predicates/&arguments (to inspect the contents of an answer set).

When the reasoner encounters a &callhex atom, it pauses the host program, executes the subprogram, collects the results into the cache, and then resumes the host program with the new data.

Key Insights and Applications

By representing answer sets as "handles" (unique IDs), the framework allows the logic program to treat a set of solutions as data that can be filtered, counted, or compared. This provides a "declarative glue" for complex tasks. Furthermore, the host program can pass facts into subprograms as input predicates, enabling parameterized modules.

The power of this approach is demonstrated through several high-level applications:

  • Belief Set Merging: The MELD system uses Nested HEX to implement complex strategies where multiple conflicting sets of information are combined based on a tree-shaped plan.
  • Generalized Quantifiers: The system easily implements "brave" vs. "cautious" reasoning (e.g., determining if a property is true in at least half of the solutions).
  • Preference Handling: A host program can generate candidates using one subprogram and then use another subprogram to rank or filter them based on user-defined preference rules.

Conclusion

Nested HEX-programs bridge the gap between declarative logic and procedural modularity. While the authors acknowledge performance overhead from spawning reasoner instances—mitigated somewhat by the Answer Set Cache—the framework offers a significant step forward in ASP modularity. Future work may explore the implications of recursive subprogram calls and the security of dynamic code generation within logic environments.

Summary updated

BibTeX
@misc{eiter2011nested,
  author = {Thomas Eiter and Thomas Krennwallner and Christoph Redl},
  title = {{Nested HEX-Programs}},
  year = {2011},
  eprint = {1108.5626},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1108.5626},
}
Figure