2.6. Inductive Predicates

In the previous chapters we defined functions and proved properties by induction on data: natural numbers, lists, trees. Often, however, it is more natural to define a property declaratively, listing only the rules that characterize it, without describing an algorithm that decides it. This is how we proceed in mathematics when we say that a set is "the smallest set closed under such and such rules", and this is how type systems and operational semantics are presented, by means of inference rules.

In Lean, this style of definition is realized by inductive predicates: a predicate P : α → Prop defined by a collection of introduction rules, which are the constructors of an inductive type. By the Curry-Howard isomorphism, the terms of type P a are the proofs that P holds for a, and these proofs are trees built from the rules. A suggestive way to summarize the situation is:

\text{Lean} = \text{functional programming} + \underbrace{\text{logic programming}}_{\text{inductive predicates}} + \text{more logic}

In this chapter we study several classic examples of inductive predicates — even numbers, transition systems, reflexive-transitive closure, sorted lists and palindromes —, we see that Lean's logical connectives are themselves inductive predicates, and we learn to reason about them by rule induction and by inversion.

namespace Ch13

2.6.1. Even numbers

The first example is the predicate that characterizes the even numbers. It is defined by two rules: 0 is even; and if k is even, then k + 2 is even.

\frac{}{\mathsf{Even}\ 0}\,(\mathsf{zero}) \qquad \frac{\mathsf{Even}\ k}{\mathsf{Even}\ (k+2)}\,(\mathsf{add\_two})

In Lean, the rules become constructors:

inductive Even : ℕ → Prop where | zero : Even 0 | add_two : ∀ k : ℕ, Even k → Even (k + 2)

The term Even.zero is a proof of Even 0, and, given hk : Even k, the term Even.add_two k hk is a proof of Even (k + 2). By the No Junk principle, these are the only ways to prove Even n: every proof of Even n is a finite tree built from these two rules. This is what will allow us to do induction on proofs.

A proof of Even 4 can be written as a term, in structured style, or with tactics, applying the rules backwards:

theorem Even_4 : Even 4 := have h0 : Even 0 := Even.zero have h2 : Even 2 := Even.add_two 0 h0 show Even 4 from Even.add_two 2 h2 example : Even 4 := ⊢ Even 4 ⊢ Even 2 ⊢ Even 0 All goals completed! 🐙

Proving that a number is not even requires reasoning about all possible proofs. To show ¬ Even 3, we assume a proof h : Even 3 and analyze it with cases. The zero rule cannot have produced Even 3, since 3 ≠ 0; that leaves add_two, which would require a proof of Even 1. Analyzing this new hypothesis, none of the rules applies, and the proof is done.

example : ¬ Even 3 := ⊢ ¬Even 3 h:Even 3⊢ False cases h with h1:Even (0 + 1)⊢ False All goals completed! 🐙

2.6.1.1. Three definitional styles

The inductive predicate is not the only way to define "being even". We could also write a recursive Boolean function or a non-recursive definition based on the remainder of division:

def evenRec : ℕ → Bool | 0 => true | 1 => false | k + 2 => evenRec k def evenNR (k : ℕ) : Prop := k % 2 = 0

Each style has its advantages. The recursive function is executable; the non-recursive definition is concise and can be decided by arithmetic tactics. The inductive predicate, in turn, is the most modular: each rule is stated independently, and adding a rule does not require rewriting the others. Moreover, it comes with an induction principle that is often the most convenient one for proofs.

2.6.1.2. First proofs

Twice any number is even. The proof is by induction on n; in the step, we rewrite 2 * (m + 1) as 2 * m + 2 and apply the add_two rule:

theorem Even_twice (n : ℕ) : Even (2 * n) := n:ℕ⊢ Even (2 * n) induction n with ⊢ Even (2 * 0) All goals completed! 🐙 m:ℕih:Even (2 * m)⊢ Even (2 * (m + 1)) m:ℕih:Even (2 * m)⊢ Even (2 * m + 2) All goals completed! 🐙

The sum of two even numbers is even. Now the induction is on the proof of Even n, not on the number n. In the zero case, we have 0 + m = m; in the add_two case, we have n = p + 2 with an induction hypothesis for p, and it suffices to reassociate the sum: (p + 2) + m = (p + m) + 2.

theorem Even_add (n m : ℕ) (hn : Even n) (hm : Even m) : Even (n + m) := n:ℕm:ℕhn:Even nhm:Even m⊢ Even (n + m) induction hn with n:ℕm:ℕhm:Even m⊢ Even (0 + m) All goals completed! 🐙 n:ℕm:ℕhm:Even mp:ℕhp:Even pih:Even (p + m)⊢ Even (p + 2 + m) have h : p + 2 + m = (p + m) + 2 := n:ℕm:ℕhn:Even nhm:Even m⊢ Even (n + m) All goals completed! 🐙 n:ℕm:ℕhm:Even mp:ℕhp:Even pih:Even (p + m)h:p + 2 + m = p + m + 2⊢ Even (p + m + 2) All goals completed! 🐙

2.6.1.3. Deciding Even

An inductive predicate is a proposition, not a Boolean, and therefore cannot be used directly in an if. We can, however, write a decision procedure: a function that, for each n, returns either a proof of Even n or a proof of ¬ Even n. This is what the type Decidable (Even n) represents; its constructors are isTrue and isFalse.

def evenDec (n : ℕ) : Decidable (Even n) := match n with | 0 => isTrue Even.zero | 1 => isFalse (n:ℕ⊢ ¬Even 1 n:ℕh:Even 1⊢ False; All goals completed! 🐙) | k + 2 => match evenDec k with | isTrue p => isTrue (Even.add_two _ p) | isFalse np => isFalse (n:ℕk:ℕnp:¬Even k⊢ ¬Even (k + 2) n:ℕk:ℕnp:¬Even kh:Even (k + 2)⊢ False n:ℕk:ℕnp:¬Even ka:Even k⊢ False All goals completed! 🐙)

The procedure is at the same time a program and a proof that the predicate is decidable. By registering it as an instance, we can use decide to prove concrete statements and even evaluate Even n as a Boolean:

instance (n : ℕ) : Decidable (Even n) := evenDec n example : Even 10 := ⊢ Even 10 All goals completed! 🐙 false#eval decide (Even 7)
false

2.6.2. Transition systems

A transition system is a binary predicate R : α → α → Prop that relates a "before" state to an "after" state. As an example, we model the scoring of a tennis game. A score is either a pair of point counts, an advantage situation, or the end of the game:

inductive Score : Type where | vs : ℕ → ℕ → Score | advServ : Score | advRecv : Score | gameServ : Score | gameRecv : Score local infixr:50 " – " => Score.vs

The legal transitions are described by an inductive predicate with fourteen rules: seven for the case in which the server wins the point and seven, symmetric, for the case in which the receiver wins it.

inductive Step : Score → Score → Prop where | serv_0_15 : ∀ n, Step (0–n) (15–n) | serv_15_30 : ∀ n, Step (15–n) (30–n) | serv_30_40 : ∀ n, Step (30–n) (40–n) | serv_40_game : ∀ n, n < 40 → Step (40–n) Score.gameServ | serv_40_adv : Step (40–40) Score.advServ | serv_adv_40 : Step Score.advServ (40–40) | serv_adv_game : Step Score.advServ Score.gameServ | recv_0_15 : ∀ n, Step (n–0) (n–15) | recv_15_30 : ∀ n, Step (n–15) (n–30) | recv_30_40 : ∀ n, Step (n–30) (n–40) | recv_40_game : ∀ n, n < 40 → Step (n–40) Score.gameRecv | recv_40_adv : Step (40–40) Score.advRecv | recv_adv_40 : Step Score.advRecv (40–40) | recv_adv_game : Step Score.advRecv Score.gameRecv local infixr:45 " ↝ " => Step

With this specification we can prove properties of the system. For example, the score never returns to 0–0:

theorem no_Step_to_0_0 (s : Score) : ¬ s ↝ (0–0) := s:Score⊢ ¬s ↝ 0 – 0 s:Scoreh:s ↝ 0 – 0⊢ False All goals completed! 🐙

The proof is instructive. The cases h tactic generates a subgoal for each rule of Step whose conclusion can be unified with s ↝ 0–0. None of the fourteen rules has this form — all of them produce a score in which some player has at least 15 points, or an advantage or end-of-game score —, so no subgoal remains and the proof is complete.

2.6.3. Reflexive-transitive closure

Step describes a single step of the game. To talk about sequences of steps, we use the reflexive-transitive closure of a relation R: the smallest relation that contains R and is reflexive and transitive. This verbal definition translates directly into three rules:

inductive Star {α : Type} (R : α → α → Prop) : α → α → Prop where | base {a b : α} : R a b → Star R a b | refl {a : α} : Star R a a | trans {a b c : α} : Star R a b → Star R b c → Star R a c

The base rule embeds R into Star R, refl ensures reflexivity and trans, transitivity. Thus, Star Step s s' states that the score s' is reachable from s in zero or more points:

example : Star Step (0–0) (30–15) := ⊢ Star Step (0 – 0) (30 – 15) ⊢ Star Step (0 – 0) (15 – 0)⊢ Star Step (15 – 0) (30 – 15) ⊢ Star Step (0 – 0) (15 – 0) All goals completed! 🐙 ⊢ Star Step (15 – 0) (30 – 15) ⊢ Star Step (15 – 0) (30 – 0)⊢ Star Step (30 – 0) (30 – 15) ⊢ Star Step (15 – 0) (30 – 0) All goals completed! 🐙 ⊢ Star Step (30 – 0) (30 – 15) All goals completed! 🐙

A natural property is that taking the closure is an idempotent operation: the closure of the closure is the closure itself.

theorem Star_Star_Iff_Star {α : Type} (R : α → α → Prop) (a b : α) : Star (Star R) a b ↔ Star R a b := α:TypeR:α → α → Propa:αb:α⊢ Star (Star R) a b ↔ Star R a b α:TypeR:α → α → Propa:αb:α⊢ Star (Star R) a b → Star R a bα:TypeR:α → α → Propa:αb:α⊢ Star R a b → Star (Star R) a b α:TypeR:α → α → Propa:αb:α⊢ Star (Star R) a b → Star R a b α:TypeR:α → α → Propa:αb:αh:Star (Star R) a b⊢ Star R a b induction h with α:TypeR:α → α → Propa:αb:αa_1:αb_1:αhab:Star R a_1 b_1⊢ Star R a_1 b_1 All goals completed! 🐙 α:TypeR:α → α → Propa:αb:αa_1:α⊢ Star R a_1 a_1 All goals completed! 🐙 α:TypeR:α → α → Propa:αb:αa_1:αb_1:αc:αa_2:Star (Star R) a_1 b_1a_3:Star (Star R) b_1 cih_ab:Star R a_1 b_1ih_bc:Star R b_1 c⊢ Star R a_1 c All goals completed! 🐙 α:TypeR:α → α → Propa:αb:α⊢ Star R a b → Star (Star R) a b α:TypeR:α → α → Propa:αb:αh:Star R a b⊢ Star (Star R) a b All goals completed! 🐙

The right-to-left direction is immediate by the base rule. The other direction is an induction on the proof of Star (Star R) a b: there is one case for each rule. In the base case, the hypothesis is already Star R a b; in the refl case, we use Star.refl; in the trans case, the induction hypotheses provide Star R a b and Star R b c, which we combine with Star.trans.

2.6.4. Logical connectives as inductive predicates

A surprising discovery is that almost all of Lean's logical connectives are themselves inductive predicates defined in the library. We reproduce their definitions below (in a separate namespace, so as not to clash with the originals):

namespace LogicalSymbols inductive And (a b : Prop) : Prop where | intro : a → b → And a b inductive Or (a b : Prop) : Prop where | inl : a → Or a b | inr : b → Or a b inductive Iff (a b : Prop) : Prop where | intro : (a → b) → (b → a) → Iff a b inductive Exists {α : Type} (P : α → Prop) : Prop where | intro : ∀ a : α, P a → Exists P inductive True : Prop where | intro : True inductive False : Prop where inductive Eq {α : Type} : α → α → Prop where | refl : ∀ a : α, Eq a a end LogicalSymbols

These definitions explain the natural deduction rules we have been using since the beginning of the course. Conjunction has a single introduction rule, which requires proofs of both components; disjunction has two, one for each side; the existential is introduced by a witness a and a proof of P a; True has a trivial proof; and False has no constructor at all, which expresses exactly that there is no way to prove it. The elimination of False (ex falso) is case analysis with zero cases.

Equality is inductive too, with a single rule: every value is equal to itself. The notation x = y abbreviates Eq x y, and ∃ x : α, P x abbreviates Exists (fun x : α ↦ P x). The only connectives that are not inductive are ∀ and →, which are primitives of type theory (dependent function types). Negation ¬ P is defined as P → False.

2.6.5. Rule induction

Since the proofs of an inductive predicate are trees built from the rules, we can do induction on them, exactly as we do induction on lists or trees. This technique, called rule induction, is identical to structural induction; the only difference is that the object we do induction on is a proof rather than a piece of data. We have already used it in Even_add and in Star_Star_Iff_Star.

As a further example, we show that every even number has remainder zero when divided by two. There is one case for each rule of Even:

theorem mod_two_Eq_zero_of_Even (n : ℕ) (h : Even n) : n % 2 = 0 := n:ℕh:Even n⊢ n % 2 = 0 induction h with n:ℕ⊢ 0 % 2 = 0 All goals completed! 🐙 n:ℕk:ℕhk:Even kih:k % 2 = 0⊢ (k + 2) % 2 = 0 All goals completed! 🐙

In the add_two case, the induction hypothesis is ih : k % 2 = 0, and the goal is (k + 2) % 2 = 0, which follows by arithmetic.

This result makes it easy to prove that numbers of the form 2n + 1 are not even: if they were, they would have remainder zero, but they have remainder one.

theorem Not_Even_two_mul_add_one (m n : ℕ) (hm : m = 2 * n + 1) : ¬ Even m := m:ℕn:ℕhm:m = 2 * n + 1⊢ ¬Even m m:ℕn:ℕhm:m = 2 * n + 1h:Even m⊢ False m:ℕn:ℕhm:m = 2 * n + 1h:Even mh1:m % 2 = 0⊢ False All goals completed! 🐙

It is also possible to prove the same fact directly by induction on h, without the auxiliary lemma. In that case, the variable n occurring in the hypothesis hm must vary during the induction: in the step, m = k + 2 and we want to apply the induction hypothesis with n - 1 in place of n. For this we use induction h generalizing n, which makes the induction hypothesis hold for every n. In the zero case, the hypothesis 0 = 2 * n + 1 is contradictory.

theorem Not_Even_two_mul_add_one' (m n : ℕ) (hm : m = 2 * n + 1) : ¬ Even m := m:ℕn:ℕhm:m = 2 * n + 1⊢ ¬Even m m:ℕn:ℕhm:m = 2 * n + 1h:Even m⊢ False induction h generalizing n with m:ℕn:ℕhm:0 = 2 * n + 1⊢ False All goals completed! 🐙 m:ℕk:ℕhk:Even kih:∀ (n : ℕ), k = 2 * n + 1 → Falsen:ℕhm:k + 2 = 2 * n + 1⊢ False m:ℕk:ℕhk:Even kih:∀ (n : ℕ), k = 2 * n + 1 → Falsen:ℕhm:k + 2 = 2 * n + 1⊢ k = 2 * (n - 1) + 1 cases n with m:ℕk:ℕhk:Even kih:∀ (n : ℕ), k = 2 * n + 1 → Falsehm:k + 2 = 2 * 0 + 1⊢ k = 2 * (0 - 1) + 1 All goals completed! 🐙 m:ℕk:ℕhk:Even kih:∀ (n : ℕ), k = 2 * n + 1 → Falsen':ℕhm:k + 2 = 2 * (n' + 1) + 1⊢ k = 2 * (n' + 1 - 1) + 1 All goals completed! 🐙

2.6.5.1. The linarith tactic

In the proofs above we used omega, a decision procedure for linear arithmetic over the integers and the naturals. Mathlib also offers the linarith tactic, which closes goals that are a linear consequence of the hypotheses in the context: combinations of sums, subtractions and multiplications by constants, over ordered fields such as the rationals and the reals (and also over ℤ and ℕ).

theorem linarith_example (i : ℤ) (hi : i > 5) : 2 * i + 3 > 11 := i:ℤhi:i > 5⊢ 2 * i + 3 > 11 All goals completed! 🐙 example (a b c : ℚ) (h1 : a ≤ b) (h2 : b < c) : a < c := a:ℚb:ℚc:ℚh1:a ≤ bh2:b < c⊢ a < c All goals completed! 🐙 example (n : ℕ) (h1 : n = 0) (h2 : n > 0) : False := n:ℕh1:n = 0h2:n > 0⊢ False All goals completed! 🐙

The linarith tactic inspects the hypotheses and looks for a linear combination of them that yields a contradiction with the negation of the goal. It does not handle products of variables, such as n \cdot m; for non-linear arithmetic there is the nlinarith variant, which is more powerful and slower.

2.6.6. Elimination and inversion

The rules of an inductive predicate are introduction rules: they say how to prove P x. To use a hypothesis h : P x we need elimination. The cases and induction tactics apply to proofs of inductive predicates in the same way as to data.

The case of equality is especially useful. Since Eq has a single rule, refl, applying cases to h : l = r unifies l and r, substituting one variable for the other throughout the goal:

theorem cases_Eq_example {α : Type} (l r : α) (h : l = r) (P : α → α → Prop) (hp : ∀ x, P x x) : P l r := α:Typel:αr:αh:l = rP:α → α → Prophp:∀ (x : α), P x x⊢ P l r α:Typel:αP:α → α → Prophp:∀ (x : α), P x x⊢ P l l All goals completed! 🐙

After cases h, the goal becomes P l l, which follows from the hypothesis hp.

2.6.6.1. Inversion rules

An inversion rule characterizes when an inductive predicate holds, expressing that the only way to prove it is through one of its rules. For Even, the inversion rule says that Even n holds if and only if n = 0 or n = m + 2 for some even m:

theorem Even_Iff (n : ℕ) : Even n ↔ n = 0 ∨ (∃ m : ℕ, n = m + 2 ∧ Even m) := n:ℕ⊢ Even n ↔ n = 0 ∨ ∃ m, n = m + 2 ∧ Even m n:ℕ⊢ Even n → n = 0 ∨ ∃ m, n = m + 2 ∧ Even mn:ℕ⊢ (n = 0 ∨ ∃ m, n = m + 2 ∧ Even m) → Even n n:ℕ⊢ Even n → n = 0 ∨ ∃ m, n = m + 2 ∧ Even m n:ℕhn:Even n⊢ n = 0 ∨ ∃ m, n = m + 2 ∧ Even m cases hn with ⊢ 0 = 0 ∨ ∃ m, 0 = m + 2 ∧ Even m All goals completed! 🐙 k:ℕhk:Even k⊢ k + 2 = 0 ∨ ∃ m, k + 2 = m + 2 ∧ Even m All goals completed! 🐙 n:ℕ⊢ (n = 0 ∨ ∃ m, n = m + 2 ∧ Even m) → Even n n:ℕhor:n = 0 ∨ ∃ m, n = m + 2 ∧ Even m⊢ Even n ⊢ Even 0m:ℕhm:Even m⊢ Even (m + 2) ⊢ Even 0 All goals completed! 🐙 m:ℕhm:Even m⊢ Even (m + 2) All goals completed! 🐙

From left to right, we use cases on the proof of Even n — an elimination without an induction hypothesis. From right to left, the rcases tactic takes apart the disjunction and the existential, and the rfl pattern substitutes the corresponding expression for n; in each case we apply the appropriate rule. Inversion rules are useful as rewrite lemmas, since they let us replace an inductive predicate by an explicit logical formula.

2.6.7. List membership

Inductive predicates can be parameterized by arbitrary types. The predicate Mem x xs, which states that x occurs in the list xs, has two rules: x occurs in a list that starts with x; and, if x occurs in ys, it also occurs in y :: ys.

inductive Mem {α : Type} (x : α) : List α → Prop where | here : ∀ {xs}, Mem x (x :: xs) | there : ∀ {y ys}, Mem x ys → Mem x (y :: ys) example : Mem 1 [2, 3, 1, 5] := ⊢ Mem 1 [2, 3, 1, 5] ⊢ Mem 1 [3, 1, 5] ⊢ Mem 1 [1, 5] All goals completed! 🐙

By induction on the proof of Mem x xs, we show that an element of xs is still an element of xs ++ ys:

theorem Mem_append_left {α : Type} (x : α) (xs ys : List α) (h : Mem x xs) : Mem x (xs ++ ys) := α:Typex:αxs:List αys:List αh:Mem x xs⊢ Mem x (xs ++ ys) induction h with α:Typex:αxs:List αys:List αxs_1:List α⊢ Mem x (x :: xs_1 ++ ys) All goals completed! 🐙 α:Typex:αxs:List αys:List αy:αys_1:List αa:Mem x ys_1ih:Mem x (ys_1 ++ ys)⊢ Mem x (y :: ys_1 ++ ys) All goals completed! 🐙

2.6.8. Sorted lists

An important example, which will reappear in the next chapters, is the predicate stating that a list of natural numbers is in increasing order. It has three rules: the empty list is sorted; every singleton list is sorted; and, if y :: zs is sorted and x ≤ y, then x :: y :: zs is sorted.

inductive Sorted : List ℕ → Prop where | nil : Sorted [] | single (x : ℕ) : Sorted [x] | two_or_more (x y : ℕ) {zs : List ℕ} (hle : x ≤ y) : Sorted (y :: zs) → Sorted (x :: y :: zs)

Positive proofs are built by applying the rules; the numerical subgoals (7 ≤ 9 etc.) are closed by simp or decide. Negative proofs use cases: the only rule that could produce Sorted [17, 13] is two_or_more, which requires 17 ≤ 13, and that is false.

theorem sorted_7_9_9_11 : Sorted [7, 9, 9, 11] := Sorted.two_or_more 7 9 (⊢ 7 ≤ 9 All goals completed! 🐙) (Sorted.two_or_more 9 9 (⊢ 9 ≤ 9 All goals completed! 🐙) (Sorted.two_or_more 9 11 (⊢ 9 ≤ 11 All goals completed! 🐙) (Sorted.single 11))) theorem Not_Sorted_17_13 : ¬ Sorted [17, 13] := ⊢ ¬Sorted [17, 13] h:Sorted [17, 13]⊢ False cases h with hle:17 ≤ 13a:Sorted [13]⊢ False All goals completed! 🐙

2.6.9. Palindromes

A palindrome is a list that reads the same in both directions. A natural inductive definition is: the empty list and the singleton lists are palindromes; and, if xs is a palindrome, then so is [x] ++ xs ++ [x].

inductive Palindrome {α : Type} : List α → Prop where | nil : Palindrome [] | single (x : α) : Palindrome [x] | sandwich (x : α) (xs : List α) : Palindrome xs → Palindrome ([x] ++ xs ++ [x])

Why not define palindromes with a recursive function? Because the pattern [x] ++ xs ++ [x] cannot appear on the left-hand side of a pattern-matching equation: pattern matching only decomposes values according to their constructors, and a list is built from its head, not from both ends. The inductive predicate gets around the problem elegantly.

theorem Palindrome_aa {α : Type} (a : α) : Palindrome [a, a] := Palindrome.sandwich a _ Palindrome.nil theorem Palindrome_aba {α : Type} (a b : α) : Palindrome [a, b, a] := Palindrome.sandwich a _ (Palindrome.single b)

The reverse of a palindrome is a palindrome. The proof is by induction on the proof of Palindrome xs. The first two cases are immediate. In the sandwich case, simp rewrites reverse ([x] ++ xs ++ [x]) as x :: (reverse xs ++ [x]), and the result follows from the induction hypothesis and the sandwich rule:

theorem Palindrome_reverse {α : Type} (xs : List α) (hxs : Palindrome xs) : Palindrome (List.reverse xs) := α:Typexs:List αhxs:Palindrome xs⊢ Palindrome xs.reverse induction hxs with α:Typexs:List α⊢ Palindrome [].reverse All goals completed! 🐙 α:Typexs:List αx:α⊢ Palindrome [x].reverse All goals completed! 🐙 α:Typexs✝:List αx:αxs:List αh:Palindrome xsih:Palindrome xs.reverse⊢ Palindrome ([x] ++ xs ++ [x]).reverse All goals completed! 🐙

2.6.10. Full binary trees

As a last example, we consider binary trees and the predicate that characterizes full binary trees: those in which every node has either zero or two non-empty children.

inductive Tree (α : Type) : Type where | nil : Tree α | node : α → Tree α → Tree α → Tree α def mirror {α : Type} : Tree α → Tree α | Tree.nil => Tree.nil | Tree.node a l r => Tree.node a (mirror r) (mirror l) inductive IsFull {α : Type} : Tree α → Prop where | nil : IsFull Tree.nil | node (a : α) (l r : Tree α) : IsFull l → IsFull r → (l = Tree.nil ↔ r = Tree.nil) → IsFull (Tree.node a l r)

The condition l = Tree.nil ↔ r = Tree.nil requires both children to be either simultaneously empty or simultaneously non-empty. We want to prove that the mirror image of a full tree is full. For this we need an auxiliary lemma: a mirrored tree is empty if and only if the original one is empty, which follows by case analysis.

theorem mirror_Eq_nil_Iff {α : Type} (t : Tree α) : mirror t = Tree.nil ↔ t = Tree.nil := α:Typet:Tree α⊢ mirror t = Tree.nil ↔ t = Tree.nil cases t with α:Type⊢ mirror Tree.nil = Tree.nil ↔ Tree.nil = Tree.nil All goals completed! 🐙 α:Typea:αl:Tree αr:Tree α⊢ mirror (Tree.node a l r) = Tree.nil ↔ Tree.node a l r = Tree.nil All goals completed! 🐙 theorem IsFull_mirror {α : Type} (t : Tree α) (ht : IsFull t) : IsFull (mirror t) := α:Typet:Tree αht:IsFull t⊢ IsFull (mirror t) induction ht with α:Typet:Tree α⊢ IsFull (mirror Tree.nil) All goals completed! 🐙 α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (mirror (Tree.node a l r)) α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (Tree.node a (mirror r) (mirror l)) α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (mirror r)α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (mirror l)α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ mirror r = Tree.nil ↔ mirror l = Tree.nil α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (mirror r) All goals completed! 🐙 α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ IsFull (mirror l) All goals completed! 🐙 α:Typet:Tree αa:αl:Tree αr:Tree αhl:IsFull lhr:IsFull rhiff:l = Tree.nil ↔ r = Tree.nilihl:IsFull (mirror l)ihr:IsFull (mirror r)⊢ mirror r = Tree.nil ↔ mirror l = Tree.nil All goals completed! 🐙

The proof is an induction on the proof of IsFull t. In the node case, the mirror image is node a (mirror r) (mirror l), and we must check the three premises of the rule: the first two are the induction hypotheses (with their places swapped) and the third, mirror r = nil ↔ mirror l = nil, follows from the auxiliary lemma and the hypothesis hiff.

end Ch13

2.6.11. Exercises

  1. Prove the converse of mod_two_Eq_zero_of_Even: if n % 2 = 0, then Even n. Hint: use strong induction or first prove that Even (2 * k).

  2. Prove the correctness of evenRec with respect to Even:

    theorem Even_evenRec (n : ℕ) : Even n → evenRec n = true
    theorem evenRec_Even (n : ℕ) : evenRec n = true → Even n
    

    For the second one, use induction with the Nat.strong_induction_on principle or prove the cases n and n + 1 simultaneously.

  3. Prove that if Even (n + m) and Even n, then Even m.

  4. Prove the following facts about Mem:

    theorem Mem_append_right (x : α) (xs ys : List α) :
        Mem x ys → Mem x (xs ++ ys)
    theorem Mem_append_inv (x : α) (xs ys : List α) :
        Mem x (xs ++ ys) → Mem x xs ∨ Mem x ys
    
  5. Write a decision procedure Decidable (Mem x xs) assuming [DecidableEq α].

  6. Prove that Star R is transitive and that, if R ⊆ S (that is, ∀ a b, R a b → S a b), then Star R ⊆ Star S.

  7. Show that in tennis it is possible to go from 40–40 to 40–40 in two steps, that is, prove Star Step (40–40) (40–40) without using Star.refl.

  8. Prove that if Sorted (x :: xs), then Sorted xs.