Goanna: Resolving Haskell Type Errors With Minimal Correction Subsets

Shuai Fu, Tim Dwyer, Peter J. Stuckey, John GrundyarXiv 2024 · DOI

Introduction

Haskell's powerful type system is a double-edged sword: while it ensures program correctness, it is equally infamous for producing cryptic and "unforgiving" error messages. Traditional compilers like the Glasgow Haskell Compiler (GHC) often suffer from fixation, pointing to a single possible cause even when multiple valid fixes exist. This can lead to a "perplexing" experience for developers, especially novices, who may find the compiler's suggestions disconnected from their actual mistakes.

Goanna is a new type-debugging tool designed to move beyond the "first error found" approach. By treating type checking as a constraint satisfaction problem, it identifies a comprehensive, ranked list of potential causes for type errors, providing an interactive and multi-cause diagnosis.

The Goanna Approach

The Goanna pipeline consists of three main phases:

  1. Constraint Generation: Source code is translated into a constraint program using Prolog predicates, modeling type definitions and usage across the program, including cross-module dependencies.
  2. MCS Enumeration: Instead of stopping at the first contradiction, Goanna identifies Minimal Correction Subsets (MCS). An MCS is a minimal set of code locations that, if modified, would make the program well-typed. This avoids the "left-to-right bias" inherent in standard Hindley-Milner unification.
  3. Post-Analysis: To prevent "suggestion fatigue," Goanna groups interconnected errors, ranks suggestions using heuristics (e.g., preferring fewer locations or more concrete types), and eliminates redundant insights.
Figure 1: Goanna identifying the first of six possible causes for a type mismatch in the 'students' list.

Interactive Debugging and Insights

Unlike static hints in standard IDEs, Goanna’s hints change dynamically based on which potential cause the user is currently inspecting. This allows programmers to "trace" the logic of different fix strategies. For instance, in a list of tuples where types mismatch, Goanna might suggest changing the literals in the list or adjusting the arguments of a downstream function.

Figure 2: Goanna diagnosing a list with inconsistent types, suggesting that the numeric literals should be strings to match the final element.

Key Results

The researchers evaluated Goanna using a dataset of 86 defective Haskell programs sourced from real-world online discourse (StackOverflow, Reddit, and Discord). Key findings include:

  • Superior Accuracy: Goanna's top-3 suggestions matched the human-accepted fix 84.8% of the time. In contrast, GHC’s exact match rate was only 11.6%.
  • Manageable Suggestions: Despite searching for all causes, Goanna averaged only 3.29 suggestions per error, keeping the list concise.
  • Practical Performance: With a mean diagnosis time of 0.98 seconds, Goanna is well within the threshold for on-demand debugging.

Limitations and Future Directions

While Goanna excels at suggesting changes to existing code nodes, it currently struggles with "edge cases" requiring code insertion or deletion (e.g., a missing function in a pipeline). Additionally, while sub-second for small programs, the computational cost of MCS enumeration suggests a need for parallelization in larger codebases. Future work aims to extend these modular MCS-based techniques to other languages like TypeScript and Rust.

Summary updated

BibTeX
@misc{fu2024goanna,
  author = {Shuai Fu and Tim Dwyer and Peter J. Stuckey and John Grundy},
  title = {{Goanna: Resolving Haskell Type Errors With Minimal Correction Subsets}},
  year = {2024},
  eprint = {2405.12697},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2405.12697},
}
Figure