liveBook · Manning

Equality

As explained in Fabulous Adventures in Data Structures and Algorithms

Term equality is structural equality in which constants have equal values, holes have equal names, and symbols have equal names and equal corresponding children.

Term equality is structural. Constants match when their values match, holes when their names match, and symbols when their names and corresponding left and right children match. section 10.1

This equality makes two binary terms interchangeable only when their complete shapes and contents agree. It supports unification, where substitutions replace bound holes so that otherwise different terms become equal. section 10.1

Comparing binary terms

ImStack(X,ImStack(X,EmptyStack)) and ImStack(2,Y) are unequal: their corresponding children differ, even though both have the same outer symbol name. section 10.1

Structural equality has practical implementation consequences:

  • Record classes can generate value equality automatically, including recursive comparisons of nested terms. section 1.2.1 section 10.1
  • Recursive equality can be expensive for long structures and may overflow the call stack, motivating nonrecursive custom implementations. section 1.2.1

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines recursive structural rules and notes generated implementations, but defers equality extensions such as commutative symbols.
Examples
Uses nested immutable-stack terms with holes, constants, and symbols to demonstrate unequal structures.
What it assumes
Introduces constants, holes, symbols, and substitutions before applying structural equality to binary terms.
What you can do after
Evaluate whether binary terms match and anticipate recursive equality costs in record-based implementations.
How it connects
Connects constants, holes, symbols, binary terms, substitutions, and unification.

Also explained in

Compare how 4 books explain equality →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.