liveBook · Manning

Chapter 8: Chapter 8. Equality: expressing relationships between data

What you will learn in chapter 8 of Type-Driven Development with Idris

This introduction explains how types can express relationships between data and why a compiler may need help establishing them.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains how types can express relationships between data and why a compiler may need help establishing them.

Key terms: first-class types, properties of data, relationships between data, precision, dependent types, reverse, vector lengths, equality

8.1 Guaranteeing equivalence of data with equality types

This section explains why the type of Boolean equality does not guarantee a relationship between inputs and output, using vector length checking as motivation for more expressive types.

Key terms: ==, Bool, Eq interface, type checker, type level, exactLength, Vect, Nothing, Just input

8.1.1 Implementing exactLength, first attempt

This section attempts to implement exactLength and shows why a Boolean equality check cannot establish the vector length required by its return type.

Key terms: exactLength, implicit argument, Vect, Maybe, Nothing, Just, Bool, ==, Eq, Vect m a

8.1.2 Expressing equality of Nats as a type

This section uses the dependent type EqNat to represent equality of natural numbers and explains why unequal arguments produce an empty type.

Key terms: EqNat, Same, dependent type, Nat, empty type, type error, type mismatch, Z, S

8.1.3 Testing for equality of Nats

This section develops a Nat equality test that returns equality evidence and uses a helper to lift that evidence through successors.

Key terms: checkEqNat, EqNat, Maybe, Just, Nothing, Nat, Z, S, case splitting, Same

8.1.4 Functions as proofs: manipulating equalities

This section develops sameS as a function that proves equality of successor values and relates equality evidence to precise function types.

Key terms: EqNat, sameS, S, proof, case-split, Same, hole, checkEqNat, ==

8.1.5 Implementing exactLength, second attempt

This section implements exactLength using evidence of equal natural numbers to return a vector at the requested length.

Key terms: exactLength, checkEqNat, Maybe, EqNat, Boolean comparison, Same, case-split, type refinement, Vect

8.1.6 Equality in general: the = type

This section introduces Idris’s generic equality type, reflexive equality proofs, and the use of cong to preserve equality through a function.

Key terms: generic equality type, =, Refl, reflexive, cong, sameS, implicit function

8.2 Equality in practice: types and reasoning

This section introduces the usefulness of equality proofs when defining functions over dependent types, particularly number-indexed vectors.

Key terms: equality proofs, dependent types, sameS, Vect

8.2.1 Reversing a vector

This section attempts a length-preserving vector reverse and exposes a type mismatch in its recursive case.

Key terms: Vect, myReverse, case-split, type mismatch, Vect (k + 1), Vect (S k)

8.2.2 Type checking and evaluation

This section explains how type evaluation exposes a vector-length mismatch in myReverse and identifies an equality that can resolve it.

Key terms: type checking, evaluation, expected type, plus, pattern matching, :printdef, anonymous function, myReverse, Vect, plusCommutative

8.2.3 The rewrite construct: rewriting a type using equality

This section uses an equality proof and Idris’s rewrite construct to complete a length-preserving vector reverse.

Key terms: rewrite, plusCommutative, Vect, equality proofs, proof, result

8.2.4 Delegating proofs and rewriting to holes

This section uses a hole and interactive editing to move the vector reversal length proof into a helper function.

Key terms: hole, ?reverseProof, lift, Vect, reverseProof, rewrite, plusCommutative, where

8.2.5 Appending vectors, revisited

This section shows how the order of addition in vector types affects reduction and when equality rewrites are needed.

Key terms: Vect, plus, rewriting, append_nil, append_xs, plusZeroRightNeutral, plusSuccRightSucc, sym, myReverse, reverse'

8.3 The empty type and decidability

This section introduces equality as evidence and the empty type as a way to express impossibility and inequality.

Key terms: equality type, =, Refl, Void, empty type, contradiction, x = y

8.3.1 Void: a type with no values

This section uses the empty Void type to express impossibility, demonstrate a negative equality proof, and explain why totality matters.

Key terms: Void, empty type, twoPlusTwoNotFive, Refl, impossible, case split, total function

8.3.2 Decidability: checking properties with precision

This section uses decidable equality for natural numbers to show how Idris can return evidence for both equality and inequality.

Key terms: decidability, Dec, Yes, No, Maybe, Void, checkEqNat, Nat, Refl, cong

8.3.3 DecEq: an interface for decidable equality

This section introduces the DecEq interface and its evidence-backed equality test, using vector equality as an exercise.

Key terms: DecEq, checkEqNat, exactLength, Prelude, Yes prf, structurally equal, Boolean equality operator, headUnequal, tailUnequal, Vect

8.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Chapter 7. Interfaces: using constrained generic types Chapter 9: Chapter 9. Predicates: expressing assumptions and contracts in types →

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.