liveBook · Manning

Chapter 10: Unification and anti-unification

What you will learn in chapter 10 of Fabulous Adventures in Data Structures and Algorithms

This chapter introduces unification as filling holes to make structures equal and anti-unification as replacing differences with holes to find shared generalizations.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces unification as filling holes to make structures equal and anti-unification as replacing differences with holes to find shared generalizations.

Key terms: unification, holes, substitution, anti-unification, generalizations, binary trees, logic problems, type inference, logic programming, code analysis

10.1 Unifying binary terms

This section introduces binary terms, substitutions, and the first-order unification problem by translating an immutable-stack matching puzzle into terms.

Key terms: binary term, term, hole, constant, symbol, first-order binary term, substitution, bound hole, free hole, Substitute

10.1.1 Unifying binary terms, first attempt

This section presents a plausible recursive unification algorithm for binary terms, then exposes how rebinding a hole can create contradictory or cyclic substitutions that require an occurs check.

Key terms: unification, binary terms, substitution, hole, symbol, Substitute, bound holes, occurs check, unbounded recursion, stack-overflow exception

10.1.2 Unifying binary terms, this time with an occurs check

This subsection corrects binary-term unification by following hole aliases during the occurs check and by dereferencing already bound holes before continuing unification.

Key terms: occurs check, bound hole, substitution, alias, Substitute, free hole, unification, first-order binary-term unification, symbol

10.2 The performance of binary term unification

This section analyzes why binary-term unification can have exponential worst-case cost, outlines optimizations, and explains why Robinson’s algorithm remains practically useful.

Key terms: Substitute, substitution, bound hole, binary term, O(2^n), exponential space, structural equality, occurs check, immutable terms, memoized factory functions

10.3 Type inference and logic programming

This section shows how unification solves type-inference constraints and, combined with backtracking, answers logic-programming queries.

Key terms: unification, type inference, generic type parameters, holes, substitution, List, backtracking search, facts, rules, query

10.4 Anti-unifying binary terms

This section introduces anti-unification as finding the most specific binary term pattern shared by two unequal terms, using holes for their differences.

Key terms: unification, anti-unification, holes, substitution, binary term, structural similarities, Mult, Div, most specific generalization, terms

10.5 The first-order binary-term anti-unification algorithm

This section derives and implements a recursive first-order anti-unification algorithm for binary terms, including fresh-hole generation and its performance trade-offs.

Key terms: anti-unification, binary terms, result, sub1, sub2, substitutions, reuse, hole, substitution dictionary, previous recursive step

10.6 Clone detection and fix deduction

This section shows how anti-unification can detect code clones and learn recurring bug-fix patterns that can be instantiated to suggest fixes for new code.

Key terms: anti-unification, parse trees, terms, holes, clone detection, code smell, clones, refactoring, plagiarism, Change term

10.7 Summary

This summary contrasts unification and anti-unification of binary terms, their algorithms and performance, and their uses in logic, type inference, and program analysis.

Concepts explained in this chapter

← Chapter 9: Greedy iterative pretty printing Chapter 11: Second abstract nonsense interlude: Monads →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.