SAT-DIFF: A Tree Diffing Framework Using SAT Solving

Chuqin Geng, Haolin Ye, Yihan Zhang, Brigitte Pientka, Xujie SiarXiv 2024 · DOI

Abstract

Traditional code differencing tools often rely on line-by-line text comparisons or heuristics that fail to capture the underlying structural changes in source code. This paper presents SAT-DIFF, a novel framework that treats tree-structured differencing as a mathematical optimization problem. By encoding the diffing process into a Maximum Satisfiability (MaxSAT) problem, SAT-DIFF generates minimal edit scripts while ensuring formal type-safety throughout the transformation. Experimental results on Python and OCaml datasets demonstrate that SAT-DIFF produces significantly more concise scripts than state-of-the-art tools like Gumtree and truediff, typically in under one second.

1. Introduction

In modern software development, understanding changes between code versions is critical for code review, version control, and automated program analysis. While standard text-based diff tools (e.g., Git diff) are efficient, they operate on lines of text rather than the hierarchical structure of code represented by Abstract Syntax Trees (ASTs). This often results in "noisy" diffs where structural changes, such as moving a block of code, are represented as a series of unrelated deletions and insertions.

Structural diffing seeks to find an edit script—a sequence of operations like Move, Insert, Update, and Delete—that transforms one AST into another. Finding the minimum edit script is an NP-hard problem. Consequently, existing tools like Gumtree rely on heuristics. While fast, these heuristics often produce suboptimal scripts and may result in intermediate states that violate language type constraints, complicating downstream automated analysis.

2. The SAT-DIFF Framework

SAT-DIFF addresses these challenges by reformulating tree diffing as a formal optimization problem. The framework operates through a multi-stage pipeline:

2.1 MaxSAT Encoding

The core of SAT-DIFF is the translation of the tree transformation into a MaxSAT formula. This involves two types of constraints:

  • Hard Constraints: These ensure the validity of the transformation. They guarantee that the resulting tree is isomorphic to the target AST and that the structure remains a valid tree (e.g., no cycles, single parentage).
  • Soft Constraints: These are used to achieve minimality. Every edit operation (addition or deletion) is assigned a penalty. The MaxSAT solver seeks to satisfy all hard constraints while minimizing the total penalty of the soft constraints, effectively finding the shortest possible edit script.

2.2 Virtual Nodes and Pruning

To represent additions within the SAT solver's search space, SAT-DIFF introduces "virtual nodes" into the source space. Adding a node is thus modeled as activating and connecting a pre-existing virtual node. To maintain performance, the framework employs pruning techniques that filter potential edge connections based on node labels and values, preventing the search space from becoming computationally intractable.

2.3 Decoding and Synthesis

Once the MaxSAT solver (CASHW-MaxSAT) identifies the optimal truth assignments, the framework decodes these into low-level actions. These actions are then synthesized into high-level, human-readable operations. A dependency graph ensures that these operations are ordered such that every intermediate step results in a well-typed tree, a feature often missing in heuristic-based tools.

3. Experimental Evaluation

The researchers evaluated SAT-DIFF using two real-world datasets:

  1. Python (Keras): 649 files from a major deep learning library, representing professional software evolution.
  2. OCaml (NATE): 1,139 snapshots of student homework, capturing the high-variance edits typical of novice programmers.

3.1 Conciseness

SAT-DIFF demonstrated a significant reduction in edit script length compared to existing tools:

  • Against truediff: On Python code, truediff scripts were 5.16x longer on average.
  • Against Gumtree: Gumtree scripts were 1.72x longer on average.
  • Against NATE: On the OCaml dataset, the baseline tool required 14.3x more edits.

3.2 Performance

Despite the theoretical complexity of SAT solving, SAT-DIFF is highly efficient in practice. The average computation time for a minimum edit script was less than one second, making it viable for integration into developer workflows.

4. Discussion and Future Work

The primary limitation of the current approach is the potential for timeouts on exceptionally large or complex ASTs. In this study, a 60-second timeout was applied; while SAT-DIFF still returns a valid script upon timeout, the guarantee of absolute minimality is lost. However, this occurred in only ~0.8% of cases.

Future work involves integrating SAT-DIFF with standard Unix diff tools to handle large blocks of identical text more efficiently and extending the framework to support syntactically incomplete or "broken" code. Additionally, the development of IDE plugins is planned to provide developers with more meaningful, structural insights into their code changes.

Summary updated

BibTeX
@misc{geng2024sat,
  author = {Chuqin Geng and Haolin Ye and Yihan Zhang and Brigitte Pientka and Xujie Si},
  title = {{SAT-DIFF: A Tree Diffing Framework Using SAT Solving}},
  year = {2024},
  eprint = {2404.04731},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2404.04731},
}
Figure