An Environment Model for the Integration of Logic and Functional Programming

Pierre E. Bonzon

Abstract

This paper presents a unified environment model designed to integrate functional and logic programming paradigms. By extending a Scheme meta-interpreter, the model maintains dual environments for functional lexical scope and logical variables. This architecture allows functions and predicates to be interleaved, enabling predicates to function as first-class objects and function results to serve as logic variables. The implementation details include clause closures, a modified search algorithm, and a backtracking mechanism utilizing a trail.

Introduction

During the late 1980s, a significant research effort was directed toward bridging the gap between functional programming (e.g., Lisp, Scheme) and logic programming (e.g., Prolog). While functional programming excels at mathematical transformations and state-free computation, logic programming offers powerful capabilities for relational queries and search-based problem solving. This paper proposes a "complete environment embedding" to unify these paradigms within a single execution model.

Problem Statement

Historically, attempts to combine these paradigms often resulted in "shallow embeddings," where one language functioned merely as a library within the other. Such systems lacked a shared understanding of the program's state or environment, leading to clunky interfaces. The goal of this work is to create a system where both paradigms share the same scope, allowing for the fluid use of function results as logic variables and predicates to define functional behaviors.

Methodology

The integration is achieved by extending a lexically scoped Core Scheme meta-interpreter. Rather than relying on macros, the underlying interpreter is redefined to handle logical constructs natively.

Dual Environments

The core of the model is the maintenance of two distinct environments: a FunEnv (Functional Environment) for standard lexical variables and a LogEnv (Logical Environment) for logical variables, typically identified by a specific prefix (e.g., ?).

The extended Eval function manages both functional and logical environments.

Similar to functional closures, "clause closures" are introduced to capture a logical relation alongside its associated functional and logical environments. The system employs a modified depth-first search and unification algorithm. If the interpreter encounters a function call during a logic search, it evaluates the function first, continuing unification with the resulting value.

Backtracking and the Trail

To support the non-deterministic nature of logic programming, the system implements a "trail" to track variable bindings. This allows the interpreter to "undo" changes to the logical environment when a search path fails, facilitating backtracking.

The Try function handles unification and manages the trail for backtracking.

Key Insights

  • Interleaved Execution: Function applications can be used directly within logical clauses. For instance, a logical relation can include terms derived from standard functional operations like append.
  • First-Class Predicates: Since predicates are represented as closures, they can be passed as arguments to functions, allowing for higher-order logical constructs such as a functional AND that composes two predicates.
  • Unified Query Language: A full query language supporting AND, OR, and NOT (implemented via the closed-world assumption) emerges as a natural extension of the Scheme syntax.

Results

The effectiveness of the model is demonstrated through the implementation of both functional and relational versions of common operations. For example, a functional APPEND returns a single concatenated list, while a relational APPENDR acts as a query, returning all possible pairs of lists that satisfy the concatenation. This demonstrates a true interleaving of paradigms where the two systems are semantically unified.

Limitations and Future Work

While the model achieves a high degree of integration, it does not yet allow the logic component to access or modify functional control information, such as continuations. Furthermore, as a meta-interpreter-based approach, performance remains a concern compared to compiled implementations. Future research will focus on developing a rigorous denotational semantics to complement the current operational model.

Summary updated

BibTeX
@inproceedings{bonzon1987environment,
  author = {Pierre E. Bonzon},
  title = {{An Environment Model for the Integration of Logic and Functional Programming}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {1987},
}
Figure