Appearance
In the previous exercise, we defined a simple data type as:
Inductive temperature :=
| Kelvin (n : nat)
| Celsius (n : nat).This type has only two constructors and no recursive structure. Every value of type temperature is created in a single step by applying either of the two constructors.
However, many useful data types – e.g. lists, trees, or expressions of a programming language (ASTs) – have an inductive structure: they can represent an infinite number of different values. For instance, consider this type of lists over natural numbers:
Inductive list :=
| nil
| cons (head : nat) (tail : list).Likewise, Fixpoint generalizes Definition s.t. a function may refer to itself. This definition uses Inductive (instead of Variant, which could have been used for temperature) to make the list identifier available to its own constructors. Because of their recursive nature, functions that operate on inductive data types must also be defined recursively, for which Rocq provides the Fixpoint command. The same also holds for proofs over inductive data types and recursive functions. These proofs often need to use a principle called structural induction as well. This induction is the central reasoning principle for inductive types: it mirrors the way values of these types are constructed.
In this exercise, we will take a closer look at the principle of induction and how it arises naturally from the structure of inductive data types such as natural numbers and trees. Additionally, we are going to look “under the hood” of Rocq to see what it automatically provides for these types and how these built-in principles guide recursive definitions and proofs.
Natural numbers are a prime example of inductive data types, constructed from either a basic element (representing zero) or inductively as the successor of an existing number.
Exercises marked in blue are mandatory and need to be handed in. Start by defining a type MyNat representing the natural numbers. Also define one, two, three and four based on this new type.
Use the levels 50 and 40 for +' and *', respectively. Define addition add and multiplication mult as recursive functions on MyNat. Test them by computing an example addition, e.g. add one two, using Rocq’s Compute command. For convenience in later exercises, also define the plus notation +' for your addition and star *' for your multiplication function using the Infix command.
You can define trees in a similar fashion to natural numbers, namely as inductive types. For example, a binary tree that stores a natural number in each of its nodes can be defined by the two rules
Define a tree type NatTree, then define functions on NatTree to
size of a tree, i.e. the number of its nodes,sum of all values stored in a tree,mirror a tree, i.e. recursively swap all left and right subtrees.Test your functions on a small example tree using Compute.
Now we have everything together to practice inductive proofs. Recall that the principal structure of such a proof is:
This principle underlies all inductive proofs and can be applied to all inductive data types. Therefore, Rocq comes with some proof automation for it. Specifically, whenever you define an Inductive, Rocq will automatically derive an induction principle for it. To reference these principles you need to append “_ind” to the name of your type. For examples:
Print MyNat_ind.
Print NatTree_ind.For a more in-depth tutorial on induction, see here. These principles are used internally by the induction tactic to derive the base and induction cases of your proof. You will see this in action in the following exercises.
Commutativity and associativity are interesting properties of functions, so let’s prove them for addition on natural numbers. Again we use our self-defined type from the first exercise, and +' as the infix notation for addition.
rewrite is the arguably most important tactic for equalities. Keep it in mind for later exercises. Note that we are now transitioning to first order logic. Our natural numbers are no propositions on their own, we need a connective like equality = to construct a statement. Fortunately, Rocq also provides tactics for reasoning with equalities. rewrite matches one side of an equality with a sub-term of the goal and substitutes this sub-term with the other side of the equality.
Prove the following theorem by induction:
Prove commutativity of addition by induction:
The proof requires two lemmas, whose exact statements depend on how you have defined your addition function. The first is either or , and the second is either or . You will probably notice which one you need when trying to prove commutativity.
Exercises marked in yellow are optional. Prove that multiplication is associative as well:
As for commutativity of addition, you will need further lemmas, e.g. one that distributes multiplication over addition.
Compared to MyNat, the induction principle for NatTree has a twist: a node has two subtrees, so the recursive case comes with two induction hypotheses – one for each subtree.
Before you start implementing these proofs in Rocq, it is helpful to figure out the reasoning on pen and paper. If you get stuck, you can ask us any time. But first, try to derive the proof idea from the following hints.
Prove that mirroring a tree is self-inverse:
As the final exercise for this week, prove that mirroring a tree preserves its size and the sum of its values: