Rocq (Coq) Basics 6. Case Analysis (destruct), Induction (induction), and Logical Connectives
Split constructors with destruct and use mathematical induction to prove universal properties over natural numbers (n + 0 = n) and polymorphic list functions (my_app length conservation).
In Part 5, 0 + n = n was proved in a single line with reflexivity.. Yet if you flip the order to n + 0 = n while n is a variable, running the exact same tactic gets completely stuck! Compare which argument addition inspects first in the two expressions.
Start in an empty Induction.v. The list functions from Part 4 are defined again here, so using the old file would give duplicate names. Append the Rocq blocks from Sections 2 and 3 in order and check each with rocq compile Induction.v.
1. Why Doesn’t n + 0 = n Simplify by Computation Alone?
The reason is that standard-library addition (Nat.add) is a Fixpoint that pattern-matches on its **first argument**. When the first argument is 0, it immediately returns the second argument n no matter what n is; when the first argument is an unknown variable n, Rocq cannot tell whether n is 0 or S n', so match n with ... cannot take a single reduction step.
Writing the two computation rules of addition as formulas makes it clear why argument order shapes the proof. Here marks where unfolding the definition reduces two terms to the same form:
In , Rocq cannot pick between the first line and the second line without knowing the left operand . In Lean 4, addition recurses on its second operand instead, so reduces definitionally to but with a variable needs a proof step, such as Lean’s simplifier using its library theorem. The mathematical truth of the theorem has not changed, only which argument the definition inspects first.12
2. Unblocking Stuck Pattern Matches with destruct and induction
destruct splits a value into one subgoal for each of its constructors: true and false for a boolean, or 0 and S n' for a natural number n. Solving the second case of n + 0 = n requires knowing that the equation already holds for the smaller predecessor n', so we use induction, which splits the cases and also provides that induction hypothesis.
In our first theorem below, we prove that negating a boolean twice returns the original boolean; because evaluating true and false finishes each branch immediately, destruct alone is enough.3
negb flips true to false and false to true. An operation that gets you back to the original value when applied twice is called an involution. That's where the theorem name negb_involutive comes from.
Theorem negb_involutive : forall b : bool,
negb (negb b) = b.
Proof.
intros b.
destruct b.
- reflexivity.
- reflexivity.
Qed.
Theorem add_zero_right : forall n : nat,
n + 0 = n.
Proof.
intros n.
induction n as [| n' IHn'].
- simpl. reflexivity.
- simpl. rewrite -> IHn'. reflexivity.
Qed.In induction n as [| n' IHn']., the bracketed pattern after as names the variables introduced in each branch. To the left of | is the 0 case, which takes no new parameters. To the right, n' names the one-step smaller predecessor inside S n', and IHn' names the induction hypothesis for n'. We focus on each subgoal in turn using - bullets.
A - bullet focuses on one goal at a time. The second bullet contains simpl. rewrite -> IHn'. reflexivity., which runs three tactics, one ending at each period. In VsRocq, move the cursor past those periods in turn to see how the goal changes.
With only two boolean values true and false, the first theorem negb_involutive finishes by simple case analysis (destruct b.). For the second theorem over infinitely many natural numbers, letting stand for the equation invokes the inference rule of mathematical induction below.
Above the bar on the left, is the base case at ; on the right, is the inductive step saying “if the property holds at (, named IHn' in code), then it also holds at the next number ().” Establishing both premises above the bar yields the conclusion below the bar: holds for all natural numbers .
The premise in the second obligation is IHn' in the code. After unfolding to , we need to replace with , an equation that plain case analysis (destruct) would not give us.4
First bullet (
n = 0base case): the goal is0 + 0 = 0, which closes immediately byreflexivity..Second bullet (
n = S n'inductive step): the induction hypothesisIHn' : n' + 0 = n'appears above the horizontal line and the goal below the line isS n' + 0 = S n'. Runningsimpl.unfolds addition by one step toS (n' + 0) = S n', andrewrite -> IHn'.turns the goal intoS n' = S n', whichreflexivity.closes.
3. Proving Conjunctions (/\) and Our List Functions (my_length, my_app) from Part 4
When a conjunction P /\ Q is your goal, split. breaks it into two separate subgoals P and Q; conversely, when H : P /\ Q is in your context, destruct H as [HP HQ]. unpacks it into two individual hypotheses. Finally, we use list induction to prove that the list functions my_length and my_app we wrote in Part 4 satisfy my_length (my_app xs ys) = my_length xs + my_length ys for all lists xs and ys.
Calling destruct on a conjunction does not split the proof into two cases; instead, it unpacks the two pieces of evidence bundled inside a single hypothesis. After extracting and , we run split on the goal and supply to the first subgoal and to the second.5
From Stdlib Require Import List.
Import ListNotations.
Theorem and_comm : forall P Q : Prop,
P /\ Q -> Q /\ P.
Proof.
intros P Q H.
destruct H as [HP HQ].
split.
- exact HQ.
- exact HP.
Qed.
Fixpoint my_length {A : Type} (xs : list A) : nat :=
match xs with
| [] => 0
| _ :: rest => S (my_length rest)
end.
Fixpoint my_app {A : Type} (xs ys : list A) : list A :=
match xs with
| [] => ys
| x :: rest => x :: my_app rest ys
end.
Theorem my_app_length : forall (A : Type) (xs ys : list A),
my_length (my_app xs ys) = my_length xs + my_length ys.
Proof.
intros A xs ys.
induction xs as [| x rest IH].
- simpl. reflexivity.
- simpl. rewrite -> IH. reflexivity.
Qed.List induction works by reducing to a smaller subterm in the exact same way as natural-number induction! Inducting on the first list xs leaves only the length of the second list in the empty case [], and in the x :: rest case prepending one element adds 1 (`S`) to the length on both sides, establishing that the length of two appended lists always equals the sum of their lengths. In Part 7, we will split these functions and theorems across multiple files and extract my_app into OCaml and Haskell source code.
Let denote list length and denote list concatenation. The equation to prove is . When the first list is , the left-hand side transforms in three steps:
The first equality comes from unfolding the definitions of concatenation and length, and the second comes from applying the induction hypothesis via rewrite -> IH. The in the formula appears as the constructor S in code, so after rewriting with IH both sides have the identical S expression and close by reflexivity.6
Footnotes
- Rocq Core Library 9.2. Corelib.Init.Nat. Shows
Nat.addmatching on its first argument: returning the second argument at0and recursing on the predecessor atS. ↩ - Lean Language Reference 4.36.0. Natural Numbers: Arithmetic (`Nat.add`) · Structural Recursion: Recursion vs Recursors. Lean’s logical definition of addition (
Nat.add) recurses on its second argument. ↩ - The Rocq Prover Reference Manual 9.2. Reasoning with inductive types. Compares
destructandinductionand illustrates how induction hypotheses are generated. ↩ - The Rocq Prover Reference Manual 9.2. Reasoning with Inductive Types: Case Analysis · Reasoning with Inductive Types: Induction.
destructsplits by constructors, whereasinductionadditionally supplies induction hypotheses for recursive subterms. ↩ - The Rocq Prover Reference Manual 9.2. Reasoning with Inductive Types: Applying Constructors · Reasoning with Inductive Types: Case Analysis.
splitproves the two components of a conjunction, whiledestructextracts both components from a conjunction in context. ↩ - The Rocq Prover Reference Manual 9.2. Reasoning with Inductive Types: Induction · Reasoning with Equalities: Rewriting. Shows structural induction over list constructors combined with equality rewriting. ↩