Type Inference in Prolog and Its Application

Tadashi Kanamori, Kenji Horiuchi

Introduction

Prolog is traditionally a dynamically typed logic programming language. While this offers significant flexibility, it complicates early error detection and prevents systems from performing certain optimizations. Without explicit type information, it is challenging to determine the expected structure of data—such as whether a variable should hold a list, an integer, or a complex term—when a specific query succeeds. This paper addresses this limitation by developing a formal type inference system designed to find a "covering success set," a superset of all ground terms for which a program can succeed.

Methodology

The core of the proposed system lies in extending standard Herbrand interpretations with type-specific constructs. Instead of mapping variables to single terms, the authors introduce type substitutions ($\Sigma$), which map variables to sets of possible ground terms (types).

A critical component of this framework is the formal definition of how a term $t$ is evaluated under a type substitution $\Sigma$, denoted as $t/\Sigma$. This evaluation determines the set of ground terms represented by $t$ given the types assigned to its variables, handling variables, constants, and complex structures recursively.

Formal definition of term type evaluation $t/\Sigma$, showing cases for variables, constructors, and type conditions.

Complementary to term evaluation is the mechanism for extracting the type of a specific variable $X$ from a term $t$ that has been assigned a type set $\underline{t}$. This projection, denoted as $X / <t \Leftarrow \underline{t}>$, is essential for propagating type information from clause heads back to their constituent variables during the inference process.

Definition of variable type projection $X / <t \Leftarrow \underline{t}>$, used to derive variable types from term types.

To compute types for an entire program, the authors define the $T^t$ transformation. This operator is analogous to the standard $T_P$ operator in logic programming semantics but operates on type sets. It iteratively applies the program's clauses to "grow" the inferred types until a least fixed point is reached, ensuring convergence to a stable type assignment.

Key Insights

  • Superset Approximation: Since finding the exact success set is undecidable, the method focuses on a safe "covering" superset. This ensures that any term leading to success is included in the inferred type, providing a reliable boundary for program verification.
  • Clause-Level Granularity: Unlike previous approaches that typed entire predicates, this method associates types with the heads of individual clauses. This allows for much higher precision, especially in programs where different clauses for the same predicate handle distinct data shapes.
  • Verification Integration: The paper demonstrates that type inference is a prerequisite for formal verification. By automatically generating type requirements, the system can simplify complex logical proofs about program behavior, preventing "vacuous" truths in proofs.

Results and Limitations

The authors prove that the $T^t$ operator is guaranteed to converge. Testing on standard Prolog predicates like append/3 and reverse/2 showed that the system accurately identifies required data structures (e.g., lists) for success.

However, the framework currently has limitations. It is monomorphic, meaning it does not natively support polymorphic types (like a list of any type) common in modern functional languages. It also requires some initial user-provided type definitions and can be computationally expensive for very large, complex programs. Future work aims to integrate these techniques with automated induction for full program verification.

Summary updated

BibTeX
@inproceedings{kanamori1985type,
  author = {Tadashi Kanamori and Kenji Horiuchi},
  title = {{Type Inference in Prolog and Its Application}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {1985},
}
Figure