Overview

Chapter 8. Equality: expressing relationships between data

This chapter explains how to use dependent types to express not just what values are, but how values relate to one another. It begins by showing why a simple Boolean equality test is too weak for type-driven development: even if a comparison returns True, the type system cannot rely on that result to treat two values as interchangeable. To solve this, the chapter builds equality as data, first for natural numbers and then with the built-in equality type, so that successful comparisons carry actual proof that two values are the same. It also shows how proofs of equality can be manipulated with ordinary functions, turning them into practical tools for programming.

Using this idea, the chapter develops functions such as a length-checking function for vectors and a reverse function whose type requires the compiler to understand arithmetic relationships on vector lengths. When Idris cannot automatically see that two type-level expressions are equal, the chapter introduces rewriting with equality proofs, showing how to transform one type into another so a definition can type-check. It also highlights that proof-heavy code can be separated from computational code by using helper functions and holes, making programs clearer while still preserving correctness guarantees. Along the way, the chapter emphasizes how type checking and evaluation interact, especially when type-level arithmetic does not reduce in the way a programmer might expect.

The final part turns to impossibility and decidability. It introduces the empty type Void as a way to express contradictions and prove that certain cases can never happen, such as a natural number being equal to its successor. Building on this, the chapter defines a more precise notion of decidable equality, where a comparison returns either a proof that two values are equal or a proof that they cannot be equal. This leads to the Dec type and the DecEq interface, which generalize equality checking across types and make it possible to write more trustworthy programs that depend on exact structural comparisons.

Figure 8.1. Rewriting a type using an equality proof

Summary

  • You can write a type to express a proof that two values must be equal.
  • You can write functions that take equality types as inputs to prove additional properties of data.
  • Using Maybe, you can test for equality of values at runtime.
  • The generic = type allows you to describe equality between values of any type.
  • Proof requirements arise naturally when programming with dependent types, such as to show that lengths are preserved when reversing a vector.
  • The rewrite construct allows you to update a type using an equality proof.
  • Using holes and interactive editing, you can delegate the details of rewriting types to a separate function.
  • Void is a type with no values, used to show that inputs to a function can’t all occur at once.
  • A property is decidable if you can always say whether the property holds for some specific values.
  • Using Dec, you can compute at runtime whether a property is guaranteed to hold or guaranteed not to hold.

FAQ

What is the main idea of expressing equality as a type in Idris?Using an equality type lets you state in the type system that two values are guaranteed to be equal, not just compare them at runtime. This gives the compiler enough information to safely use one value where the other is expected.
Why isn’t the Boolean operator == enough for proving equality?== only returns Bool, which tells you true or false but gives no proof that the values are really equal. The type says nothing about how the result was computed, so Idris cannot use it as evidence.
What does the dependent type EqNat num1 num2 represent?EqNat num1 num2 represents evidence that the two natural numbers are equal. It can only be constructed when both numbers are the same, so a value of this type acts like a proof of equality.
Why is EqNat 3 4 an empty type?Because there is no way to construct a value of that type. The only constructor, Same, requires both indices to be identical, so EqNat 3 4 has no valid inhabitants.
How does checkEqNat use equality evidence instead of a Boolean result?checkEqNat returns Maybe (EqNat num1 num2) or, more precisely in the later version, Dec (num1 = num2). If the numbers are equal, it returns a proof; if not, it returns evidence that equality is impossible.
What is the purpose of the helper function sameS?sameS shows that if two natural numbers are equal, then their successors are also equal. In other words, it transforms proof of EqNat k j into proof of EqNat (S k) (S j).
Why does exactLength need equality proofs to work?When comparing a vector’s actual length with a desired length, a Boolean result is not enough to convince Idris that the vector type has changed. An equality proof lets Idris safely treat Vect m a as Vect len a when the lengths are proven equal.
What does the built-in equality type = and constructor Refl mean?x = y is the generic equality type in Idris, and Refl is the only constructor. It can only build values of the form x = x, meaning reflexive equality.
How does rewrite help with type checking in equality proofs?rewrite uses an equality proof to replace one expression in a type with an equivalent one. This is useful when Idris cannot simplify two expressions to the same form on its own, such as S k versus k + 1.
What role does Void play in proving that values are not equal?Void is the empty type, so returning it means a situation is impossible. If you can show that assuming equality leads to Void, then you have proved the two values cannot be equal.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Type-Driven Development with Idris Type-Driven Development with Idris ebook for free