Chapter 9. Predicates: expressing assumptions and contracts in types
Predicates are introduced as dependent types that exist to describe properties of data, turning assumptions into compile-time checked contracts. The chapter explains that when a value of such a type can be constructed, the stated property must hold, and this lets programmers make expectations about inputs and outputs explicit in function signatures. This is especially useful for safety-critical or state-sensitive code, because the compiler can verify that required conditions have been established before a function is called.
The main technical example is removing an element from a vector only when that element is known to be present. A first attempt at the function type fails because the recursive case may reach an empty tail, so the implementation is refined by introducing a predicate that witnesses membership in a vector. That predicate is represented by a dependent type with constructors for the element being at the head or appearing later in the vector, and once this proof is added to the function’s type, the compiler can guide the implementation and rule out impossible cases. The chapter also shows how to make such proofs easier to use with auto-implicit arguments and how to decide membership at runtime by defining a decidable version of the predicate.
The second half applies the same idea to a small Hangman-style game, where the current game state is encoded in types so that the number of guesses and the number of remaining letters are tracked precisely. A predicate is used to validate user input, ensuring that only single-character guesses are accepted, and another function determines whether a guess is correct by returning either a new state with one fewer missing letter or a new state with one fewer guess. By threading these predicates through the program, the implementation avoids invalid states such as continuing after the game is already over or updating the wrong part of the state, demonstrating how types can express program invariants, contracts, and assumptions directly.
Summary
- You can write types that express assumptions about how values relate.
- The Elem dependent type is a predicate that expresses that a value must be contained in a vector.
- By passing a predicate as an argument to a function, you can express a contract that the inputs to the function must follow.
- You can write a function to show that a predicate is decidable, using Dec.
- Idris will attempt to find values for arguments marked auto by expression search.
- You can capture properties of a system’s state (such as the rules of a game) in a type.
- Predicates can describe the validity of user input and ensure that the user input is validated when necessary.
FAQ
What is a predicate in Idris, and why is it useful?
A predicate is a data type used to describe a property of some data. If you can construct a value of that type, then the property is proven to hold. Predicates are useful because they let Idris check assumptions and contracts in types at compile time.How does the Elem predicate express that a value is contained in a vector?
Elem value xs is a type that represents a proof that value occurs somewhere in the vector xs. Its constructors, Here and There, build evidence for being at the head of the vector or further down the tail.Why was the first attempt at defining removeElem not precise enough?
The first version only stated that the input vector was non-empty and that the output vector was one element shorter. It did not guarantee that the element to remove actually appeared in the vector, so recursive removal could fail when the value was absent.How does adding Elem value xs to removeElem's type improve the function?
It turns the assumption “the value is in the vector” into an explicit precondition. This lets the type checker ensure the function is only called when removal is valid, and it also provides enough information to complete the recursive implementation safely.How is removeElem implemented once the membership proof is available?
The function case-splits on the proof. If the proof is Here, the head is removed. If it is There later, the function recursively removes the element from the tail, using later as proof that the value is still present there.What is the purpose of Uninhabited in this chapter?
Uninhabited is used to describe types that have no possible values. It is helpful for proving impossible cases, such as membership in an empty vector or impossible equalities, so those branches can be ruled out safely.What does isElem do, and what does it return?
isElem decides whether a value is in a vector. It returns Dec (Elem value xs), meaning it gives either Yes with a proof of membership or No with a proof that membership is impossible.Why are auto-implicit arguments useful with predicates?
auto-implicit arguments let Idris try to construct proofs automatically. This reduces clutter in function calls, because proofs like Elem value xs can often be inferred by the compiler instead of being written explicitly.How does the chapter use predicates to model the state of a Hangman-style game?
The game state is encoded in types using values such as the number of guesses remaining and the number of missing letters. This makes the game rules part of the type system, so illegal states and invalid transitions are rejected by the compiler.How does ValidInput help validate user input in the guessing game?
ValidInput is a predicate that describes strings containing exactly one character. By checking this predicate before accepting user input, the program can ensure that only valid guesses are used in the game logic.
Type-Driven Development with Idris ebook for free