Building Libraries in Prolog

Alan R. Feuer

Abstract

Early Prolog implementations, such as those defined by Clocksin and Mellish, operated within a single, flat database namespace. This architecture hindered the development of reusable libraries due to name collisions, redundant memory usage, and the lack of private routines. This paper by Alan Feuer proposes a modular framework using "modules" and "views" to provide scoped execution contexts and efficient library management.

Introduction: The Flat Namespace Problem

In standard Prolog, the database is essentially one giant, flat namespace. This creates significant challenges for library developers:

  1. Lack of Encapsulation: Helper routines (e.g., revzap for a fast reverse) are exposed to the entire program, risking accidental invocation or name clashes.
  2. Redundant Loading: Multiple libraries depending on a common utility (like a list library) often cause the utility to be loaded multiple times, wasting memory and increasing search complexity.
  3. Name Overloading: It is difficult to maintain distinct versions of common routines (e.g., delete for sets vs. queues) without interference.

Proposed Modular Architecture

Feuer suggests partitioning the Prolog database into distinct modules. Each routine belongs to exactly one module, providing a natural boundary for code organization.

Private Routines and Scoping

To support encapsulation, the author introduces a mechanism for private routines. By tagging a routine as private (often denoted by a naming convention like $routine), it becomes invisible outside its home module. This ensures that internal implementation details do not leak into the global scope.

Internal structure of routines showing the PRIVATE tag for scoped visibility.

As shown in the figure above, routines like revzap can be explicitly marked as PRIVATE, preventing external modules from accessing them directly while allowing other routines within the same module, such as reverse, to utilize them.

Scoped Contexts through Views

The core innovation of the paper is the concept of a view. A view is an ordered sequence of modules, represented internally as a chain of ModuleRef objects. When Prolog attempts to satisfy a goal, it searches through the modules in the current view in a top-down fashion.

The complete representation of a module, its view (ModuleRef chain), and its routines.

The use(X) Directive

Instead of the traditional consult, the use(X) directive is proposed. This command adds a module to the current view. If the module is already in memory, it simply updates the view pointer, effectively solving the "double loading" problem and ensuring shared dependencies are stored only once.

Absolute Referencing

For cases where the search path must be bypassed, the syntax module$routine allows for absolute referencing, enabling a programmer to call a specific implementation regardless of the current view's order.

Key Insights and Results

By treating the search path as a dynamic context, Feuer's system enables a form of logical inheritance. A module can prioritize its own routines, followed by library routines, and finally built-ins.

Qualitative results of this architecture include:

  • Namespace Safety: Auxiliary routines are hidden, protecting library integrity.
  • Conflict Resolution: Different modules can define routines with the same name (e.g., set$delete vs queue$delete) without collision.
  • Memory Efficiency: Shared modules are loaded once and referenced multiple times via the use directive.

Conclusion

While the proposed system introduces some search overhead by traversing the view list, the organizational and efficiency benefits for large-scale Prolog development are substantial. This framework moved Prolog away from a "global variable" style of programming toward a modern, modular software engineering paradigm.

Summary updated

BibTeX
@inproceedings{feuer1983building,
  author = {Alan R. Feuer},
  title = {{Building Libraries in Prolog}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {1983},
}
Figure