Skip to content
Exercises · Assignment 2

FCPLCD Assignment 2Download PDF

In the previous exercise, we defined a simple data type as:

Rocq
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:

Rocq
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.

Inductive Data Types

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.

Natural Numbers

EXERCISE 2.1
Define the Natural Numbers

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.

EXERCISE 2.2
Addition and Multiplication

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.

Trees

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

EXERCISE 2.3
Functions on Trees

Define a tree type NatTree, then define functions on NatTree to

  1. calculate the size of a tree, i.e. the number of its nodes,
  2. calculate the sum of all values stored in a tree,
  3. mirror a tree, i.e. recursively swap all left and right subtrees.

Test your functions on a small example tree using Compute.

Inductive Proofs

Now we have everything together to practice inductive proofs. Recall that the principal structure of such a proof is:

  1. prove your theorem for the base case(s) of your data type,
  2. assume your theorem holds for a given instance of your type, then show that it also holds for immediately “larger” instances.

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:

Rocq
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.

Induction on Natural Numbers

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.

EXERCISE 2.4
Associativity

Prove the following theorem by induction:

EXERCISE 2.5
Commutativity

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.

EXTRA 2.6
Associativity of Multiplication

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.

Induction on Trees

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.

EXTRA 2.7
Proof Sketch

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.

  1. We prove the theorems by induction on . What are the base and recursive cases that we will have to prove?
  2. Write down the statement we have to prove for the base case. What can we use to prove the equality?
  3. Now write down the statement we have to prove for the recursive case. Also write down both induction hypotheses.
  4. Try to transform either side of the equality you stated in step 3 to prove it.
EXERCISE 2.8
Mirror

Prove that mirroring a tree is self-inverse:

EXTRA 2.9
Size and Sum

As the final exercise for this week, prove that mirroring a tree preserves its size and the sum of its values: