liveBook · Manning

Predicate

As explained in Type-Driven Development with Idris

A predicate is a condition on values that can be represented as a proposition whose truth can be established or refuted.

A predicate is a type that describes a property of data. Constructing a value of that type provides a proof that the property holds. section 9.0 section 9.3

Predicates make assumptions explicit in function types. A function can require a proof about its inputs, or return a value paired with a proof about that value, so the type checker checks the contract at the call site. section 9.0 section 9.3

readGuess : IO (x ** ValidInput x)
readGuess = do putStr "Guess:"
               x <- getLine                                         #1
               case isValidString (toUpper x) of                    #2
                    Yes prf => pure (_ ** prf)                      #3
                    No contra => do putStrLn "Invalid guess"
                                    readGuess                       #4

The input reader checks a guess and returns it with a proof that it contains exactly one character; invalid guesses trigger another attempt. section 9.2.3

Predicates can also describe vector membership, whether a property is decidable, and rules about system state. Decidable validation can distinguish a proof of validity from evidence that a value cannot be valid. section 9.0 section 9.3

How Type-Driven Development with Idris covers it

How deep it goes
Defines predicates as property-bearing types and applies them to vector membership, input validation, decidability, and system state.
Examples
Uses vector membership, one-character guesses, and assumptions about open files, network protocols, and authentication.
What it assumes
Builds on dependent types and equality predicates, then develops how to express properties of data in types.
What you can do after
Build Idris functions that require proofs of input properties or return values paired with proofs of validity.
How it connects
Connects predicates to dependent types, equality, vectors, decidability, dependent pairs, and function contracts.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Type-Driven Development with Idris. The section links point to the parts of the book it draws on.