Debugging Functional Programs

Markus Stumptner, Franz WotawaIJCAI 1999

Abstract

This paper presents a novel approach to software debugging by adapting Model-Based Diagnosis (MBD) from the hardware domain to functional programming. By treating code expressions as individual components within a structural model, the authors enable the identification of faults through "expression replacement." The system utilizes multiple test cases and a logic-based consistency check to narrow down potential bugs and propose automated repairs. The methodology is evaluated on small functional programs, demonstrating a significant reduction in the diagnostic search space.

Introduction

Traditional software debugging is often an ad-hoc, manual process involving execution tracing and variable checking. In contrast, hardware diagnostics has long benefited from Model-Based Diagnosis (MBD), a rigorous framework used to isolate faults in physical circuits. However, applying MBD to software is challenging because software "components" (lines of code) lack the fixed, well-defined physical connections of hardware gates. Previous attempts often resulted in too many "suspects," failing to provide actionable insights to developers. This research aims to bridge this gap by creating an executable logic model capable of narrowing down diagnoses using multiple test cases and potential code fixes.

Methodology

The authors introduce a logic-based model for a simple functional language called EXP. The core of this approach involves two primary innovations: structural modeling and fault mode definition through replacement.

The EXPSD Model

The program is represented as a system description (SD) using a Prolog-like syntax. Every expression in the program is treated as a component. The model tracks the environment (variable values) and evaluation steps, allowing it to handle constants, variables, built-in functions, and user-defined recursive functions. This treats the syntax tree of a program as a structural diagram where data flow is analogous to electrical current in a circuit.

Figure 1: Structural representation of a functional program as a tree of components, where nodes are treated as diagnosable units.

Fault Modes and Replacement Ordering

In hardware MBD, a component might have a fault mode like being "stuck-at-zero." In this software model, fault modes are defined as expression replacements. For example, a fault in the expression x > 0 might be diagnosed as the replacement x >= 0. Because the space of possible code rewrites is theoretically infinite, the authors define a heuristic ordering based on "term depth."

Figure 2: The ordering of expression replacements, starting from simple substitutions (Order 0) to more complex functional changes.

Pruning via Non-Replaceability

To manage the search space, the system uses multiple test cases to filter out incorrect diagnoses. The authors introduce the concept of non-replaceability, the software equivalent of physical impossibility. If a proposed code fix requires a deterministic function to return two different values for the same input across different test cases, that diagnosis is discarded. This consistency check is a powerful tool for pruning the search space.

Key Insights and Results

The evaluation focused on small, illustrative functional programs such as twodimln (logarithmic logic), an equality predicate, and myMult (a recursive multiplication function).

  • Diagnosis Reduction: The combination of MBD and the non-replaceability filter significantly reduces the number of suspects. By analyzing how different parts of the code contribute to the final output across multiple test cases, the system can eliminate high-level functions as suspects if no possible replacement could satisfy the input/output constraints.
  • Automated Repair: A successful diagnosis often yields a suggested fix—the specific replacement that satisfied all test cases. This moves the tool beyond simple debugging toward automated program repair.
  • Recursion Handling: The model successfully handles recursive calls by incorporating a "step" counter to prevent infinite loops, a common challenge when modeling functional languages.

Limitations and Future Work

While promising, the approach faces computational hurdles. Diagnosis is NP-hard, and the time required to find a solution grows exponentially with the number of components and the depth of replacements. Current applications are limited to smaller programs or sub-modules. Future research directions include incorporating program slicing to focus the engine on relevant code segments and developing better heuristics for "measurement selection"—identifying which new test cases would most effectively narrow down remaining suspects.

Summary updated

BibTeX
@inproceedings{stumptner1999debugging,
  author = {Markus Stumptner and Franz Wotawa},
  title = {{Debugging Functional Programs}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {1999},
}
Figure