Lean으로 배우는 증명가능한 프로그래밍

D.7 7장 연습문제 해설🔗

연습문제 7.1.1 귀납 증명의 구조

기초 단계의 명제는 \sum_{k=1}^{0}(2k-1)=0^2이다. 빈 합과 0^2가 모두 0이므로 성립한다. 귀납 가정은 임의의 자연수 n에서 \sum_{k=1}^{n}(2k-1)=n^2이라는 등식이다. 이 가정으로부터 다음 목표를 증명한다.

\sum_{k=1}^{n+1}(2k-1)=(n+1)^2

왼쪽을 처음 n개의 합과 새 항 2(n+1)-1=2n+1로 나눈다. 처음 n개의 합을 귀납 가정에 따라 n^2로 바꾸면 n^2+2n+1=(n+1)^2가 된다.

연습문제 7.1.2 재귀와 귀납

첫째 #check는 기초 단계의 명제다. square 0이 0으로 계산되므로 양변은 같다. n을 선언한 뒤의 첫 명제는 귀납 가정이고, 그다음 명제가 귀납 단계의 목표다. 목표의 왼쪽을 한 번 펼치면 square n + 2 * n + 1이 된다. square n을 귀납 가정으로 n * n으로 바꾸면 곱셈을 전개한 (n + 1) * (n + 1)과 같아진다.

square 0 = 0 * 0 : Prop#check (square 0 = 0 * 0) section variable (n : Nat) square n = n * n : Prop#check (square n = n * n) square (n + 1) = (n + 1) * (n + 1) : Prop#check (square (n + 1) = (n + 1) * (n + 1)) end
연습문제 7.2.1 귀납 증명의 문법

induction n 뒤에 case zero로 첫째 목표 0 + 0 = 0을 선택한다. case succ n ih에서 n은 후속자에 넣은 자연수이고, ih : 0 + n = n은 그 수에 대한 증명이다. 이때 목표는 0 + (n + 1) = n + 1이다.

example (n : Nat) : 0 + n = n := n:Nat⊢ 0 + n = n ⊢ 0 + 0 = 0n✝:Nata✝:0 + n✝ = n✝⊢ 0 + (n✝ + 1) = n✝ + 1 case zero ⊢ 0 + 0 = 0 All goals completed! 🐙 case succ n ih n:Natih:0 + n = n⊢ 0 + (n + 1) = n + 1 All goals completed! 🐙
연습문제 7.2.2 계산과 귀납 증명

n + 0에서 둘째 인수는 0이므로 바로 n으로 계산된다. 0 + n에서 덧셈의 둘째 인수는 변수다. induction n으로 그 인수의 생성자를 드러내면 zero에서는 계산할 수 있고, succ에서는 덧셈을 전개하여 귀납 가정을 쓸 수 있다.

example (n : Nat) : n + 0 = n := n:Nat⊢ n + 0 = n All goals completed! 🐙 example (n : Nat) : 0 + n = n := n:Nat⊢ 0 + n = n induction n with ⊢ 0 + 0 = 0 All goals completed! 🐙 n:Natih:0 + n = n⊢ 0 + (n + 1) = n + 1 n:Natih:0 + n = n⊢ (0 + n).succ = n + 1 All goals completed! 🐙
연습문제 7.2.3 0과 자연수의 곱

둘째 인수가 0인 곱은 0으로 계산된다. 귀납 단계에서 Nat.mul_succ를 적용하면 왼쪽은 0 * n + 0이 된다. ih : 0 * n = 0으로 곱을 바꾸면 0 + 0 = 0이 계산되어 목표가 닫힌다.

Nat.mul_zero (n : Nat) : n * 0 = 0#check Nat.mul_zero Nat.mul_succ (n m : Nat) : n * m.succ = n * m + n#check Nat.mul_succ theorem zeroMul (n : Nat) : 0 * n = 0 := n:Nat⊢ 0 * n = 0 induction n with ⊢ 0 * 0 = 0 All goals completed! 🐙 n:Natih:0 * n = 0⊢ 0 * (n + 1) = 0 All goals completed! 🐙 'Chapter07Solutions.zeroMul' does not depend on any axioms#print axioms Chapter07Solutions.zeroMul

succ 분기에서 귀납 가정을 쓴 자리를 자신의 풀이와 비교한다. 위 명령은 해설의 zeroMul을 검사하며, 이 증명이 공리에 의존하지 않는다고 표시한다.

연습문제 7.2.4 1과 자연수의 곱

귀납 단계에서 1 * (n + 1)은 Nat.mul_succ를 쓰면 1 * n + 1이 된다. ih : 1 * n = n으로 곱을 바꾸면 양변이 n + 1로 같아진다.

theorem oneMul (n : Nat) : 1 * n = n := n:Nat⊢ 1 * n = n induction n with ⊢ 1 * 0 = 0 All goals completed! 🐙 n:Natih:1 * n = n⊢ 1 * (n + 1) = n + 1 All goals completed! 🐙
연습문제 7.2.5 곱셈의 분배법칙

기초 단계에서 왼쪽은 m * (n + 0), 오른쪽은 m * n + m * 0이다. 두 식은 모두 m * n으로 계산된다. 귀납 단계에서는 먼저 Nat.add_succ n k로 왼쪽 곱셈의 둘째 인수를 (n + k).succ로 바꾼다. Nat.mul_succ를 양변에 적용하면 왼쪽에 m * (n + k)가 드러난다. 이 곱을 ih로 바꾸면 (m * n + m * k) + m = m * n + (m * k + m)이 남는다. 7.2.3절의 addAssoc으로 이 등식을 증명한다.

theorem mulAdd (m n k : Nat) : m * (n + k) = m * n + m * k := m:Natn:Natk:Nat⊢ m * (n + k) = m * n + m * k induction k with m:Natn:Nat⊢ m * (n + 0) = m * n + m * 0 All goals completed! 🐙 m:Natn:Natk:Natih:m * (n + k) = m * n + m * k⊢ m * (n + (k + 1)) = m * n + m * (k + 1) m:Natn:Natk:Natih:m * (n + k) = m * n + m * k⊢ m * (n + k).succ = m * n + m * (k + 1) m:Natn:Natk:Natih:m * (n + k) = m * n + m * k⊢ m * (n + k) + m = m * n + (m * k + m) All goals completed! 🐙
연습문제 7.3.1 자연수의 세 배

n = 0에서 양변은 0이다. 귀납 단계에서는 tripleRec의 재귀식으로 왼쪽을 tripleRec n + 3으로 바꾸고 ih를 적용한다. Nat.mul_succ로 오른쪽을 전개하면 양변이 3 * n + 3이다. doubleRecEq에서 두 재귀식에 나타났던 2가 여기서는 3에 해당한다.

theorem tripleRecEq (n : Nat) : tripleRec n = 3 * n := n:Nat⊢ tripleRec n = 3 * n induction n with ⊢ tripleRec 0 = 3 * 0 All goals completed! 🐙 n:Natih:tripleRec n = 3 * n⊢ tripleRec (n + 1) = 3 * (n + 1) All goals completed! 🐙
연습문제 7.3.2 2의 거듭제곱

powerTwo 0이 1로 계산되므로 기초 단계는 decide로 끝난다. 귀납 단계에서 정의를 펼치면 곱의 두 인수가 2와 powerTwo n이다. Nat.mul_pos의 첫째 증명 인수 0 < 2는 by decide로 구성하고, 둘째 증명 인수 0 < powerTwo n에는 ih를 전달한다.

theorem powerTwoPos (n : Nat) : 0 < powerTwo n := n:Nat⊢ 0 < powerTwo n induction n with ⊢ 0 < powerTwo 0 All goals completed! 🐙 n:Natih:0 < powerTwo n⊢ 0 < powerTwo (n + 1) n:Natih:0 < powerTwo n⊢ 0 < 2 * powerTwo n exact Nat.mul_pos (n:Natih:0 < powerTwo n⊢ 0 < 2 All goals completed! 🐙) ih
연습문제 7.3.3 홀수의 합

기초 단계는 0 = 0으로 계산된다. 귀납 단계에서 square의 정의를 펼치고 ih를 적용하면 왼쪽은 n * n + 2 * n + 1이다. 첫째 등식에서 2 * n을 n * 2로 바꾼 뒤 Nat.mul_succ와 Nat.mul_one으로 펼친다. 둘째 등식에서는 7.2.3절의 addAssoc으로 합의 묶는 순서를 바꾼다. 마지막 등식에서는 오른쪽의 (n + 1) * (n + 1)에 분배법칙을 적용하여 왼쪽과 맞춘다.

theorem squareEq (n : Nat) : square n = n * n := n:Nat⊢ square n = n * n induction n with ⊢ square 0 = 0 * 0 All goals completed! 🐙 n:Natih:square n = n * n⊢ square (n + 1) = (n + 1) * (n + 1) n:Natih:square n = n * n⊢ n * n + 2 * n + 1 = (n + 1) * (n + 1) calc n * n + 2 * n + 1 = n * n + (n + n) + 1 := n:Natih:square n = n * n⊢ n * n + 2 * n + 1 = n * n + (n + n) + 1 All goals completed! 🐙 _ = (n * n + n) + (n + 1) := n:Natih:square n = n * n⊢ n * n + (n + n) + 1 = n * n + n + (n + 1) n:Natih:square n = n * n⊢ n * n + n + n + 1 = n * n + n + (n + 1) All goals completed! 🐙 _ = (n + 1) * (n + 1) := n:Natih:square n = n * n⊢ n * n + n + (n + 1) = (n + 1) * (n + 1) All goals completed! 🐙
연습문제 7.3.4 짝수의 합

첫 정리의 기초 단계에서 양변은 0이다. 귀납 단계에서는 두 함수를 전개한 뒤 Nat.mul_add의 인수를 지정해 오른쪽의 곱을 펼친다. 그러면 양변에 2 * (n + 1)이 더해진 형태가 된다. 왼쪽의 evenSum n을 귀납 가정으로 2 * triangular n으로 바꾸면 양변이 같다. 둘째 정리에서는 첫 정리와 triangularFormula가 공통으로 쓰는 2 * triangular n을 calc의 중간 항으로 놓는다.

theorem evenSumTriangular (n : Nat) : evenSum n = 2 * triangular n := n:Nat⊢ evenSum n = 2 * triangular n induction n with ⊢ evenSum 0 = 2 * triangular 0 All goals completed! 🐙 n:Natih:evenSum n = 2 * triangular n⊢ evenSum (n + 1) = 2 * triangular (n + 1) n:Natih:evenSum n = 2 * triangular n⊢ evenSum n + 2 * (n + 1) = 2 * (triangular n + (n + 1)) n:Natih:evenSum n = 2 * triangular n⊢ evenSum n + 2 * (n + 1) = 2 * triangular n + 2 * (n + 1) All goals completed! 🐙 theorem evenSumFormula (n : Nat) : evenSum n = n * (n + 1) := n:Nat⊢ evenSum n = n * (n + 1) calc evenSum n = 2 * triangular n := evenSumTriangular n _ = n * (n + 1) := triangularFormula n
연습문제 7.4.1 귀납적 구조와 귀납 가정

leaf 분기의 목표는 P Tree3.leaf다. node l m r 분기에서는 ihL : P l, ihM : P m, ihR : P r이 주어지고, 목표는 P (Tree3.node l m r)이다. 아래는 두 분기의 증명이 hLeaf와 hNode로 주어졌을 때 induction이 각 증명을 어떤 목표에 사용하는지를 나타낸다.

example (P : Tree3 → Prop) (hLeaf : P .leaf) (hNode : ∀ l m r : Tree3, P l → P m → P r → P (.node l m r)) (t : Tree3) : P t := P:Tree3 → ProphLeaf:P Tree3.leafhNode:∀ (l m r : Tree3), P l → P m → P r → P (l.node m r)t:Tree3⊢ P t induction t with P:Tree3 → ProphLeaf:P Tree3.leafhNode:∀ (l m r : Tree3), P l → P m → P r → P (l.node m r)⊢ P Tree3.leaf All goals completed! 🐙 P:Tree3 → ProphLeaf:P Tree3.leafhNode:∀ (l m r : Tree3), P l → P m → P r → P (l.node m r)l:Tree3m:Tree3r:Tree3ihL:P lihM:P mihR:P r⊢ P (l.node m r) All goals completed! 🐙
연습문제 7.4.2 리스트 연결의 결합법칙

xs는 왼쪽의 안쪽 연결과 오른쪽의 바깥쪽 연결에서 첫째 인수다. nil 분기에서는 양변이 ys ++ zs로 계산된다. cons 분기에서 List.cons_append를 적용하면 양변의 맨 앞에 x가 놓인다. 두 꼬리가 같다는 증명이 ih다. 자연수 덧셈의 결합법칙도 양변에서 재귀식이 드러나는 인수를 골랐다.

theorem appendAssoc {α : Type} (xs ys zs : List α) : (xs ++ ys) ++ zs = xs ++ (ys ++ zs) := α:Typexs:List αys:List αzs:List α⊢ xs ++ ys ++ zs = xs ++ (ys ++ zs) induction xs with α:Typeys:List αzs:List α⊢ [] ++ ys ++ zs = [] ++ (ys ++ zs) All goals completed! 🐙 α:Typeys:List αzs:List αx:αxs:List αih:xs ++ ys ++ zs = xs ++ (ys ++ zs)⊢ x :: xs ++ ys ++ zs = x :: xs ++ (ys ++ zs) All goals completed! 🐙
연습문제 7.4.3 리스트 역순의 두 번 적용

reverseAppend의 기초 단계에서는 오른쪽에 붙은 빈 리스트를 appendNil로 없앤다. 귀납 단계에서는 왼쪽을 전개하고 ih를 적용하면 (revRec ys ++ revRec xs) ++ [x]를 얻는다. 이 식을 연습문제 7.4.2의 appendAssoc로 재작성하면 오른쪽과 같다.

reverseReverse에서는 안쪽의 revRec (x :: xs)를 펼친 뒤 reverseAppend로 바깥쪽 역순 적용을 두 부분으로 나눈다. 이때 나타난 revRec (revRec xs)를 ih로 바꾸고, revRec [x] ++ xs가 x :: xs로 계산되는 것을 rfl로 확인한다.

theorem reverseAppend {α : Type} (xs ys : List α) : revRec (xs ++ ys) = revRec ys ++ revRec xs := α:Typexs:List αys:List α⊢ revRec (xs ++ ys) = revRec ys ++ revRec xs induction xs with α:Typeys:List α⊢ revRec ([] ++ ys) = revRec ys ++ revRec [] All goals completed! 🐙 α:Typeys:List αx:αxs:List αih:revRec (xs ++ ys) = revRec ys ++ revRec xs⊢ revRec (x :: xs ++ ys) = revRec ys ++ revRec (x :: xs) All goals completed! 🐙 theorem reverseReverse {α : Type} (xs : List α) : revRec (revRec xs) = xs := α:Typexs:List α⊢ revRec (revRec xs) = xs induction xs with α:Type⊢ revRec (revRec []) = [] All goals completed! 🐙 α:Typex:αxs:List αih:revRec (revRec xs) = xs⊢ revRec (revRec (x :: xs)) = x :: xs α:Typex:αxs:List αih:revRec (revRec xs) = xs⊢ revRec [x] ++ xs = x :: xs All goals completed! 🐙
연습문제 7.4.4 이진 트리의 높이 보존

leaf 분기에서는 양변이 0이다. node 분기에서 mirror와 BinTree.height의 정의를 펼친다. 두 귀납 가정으로 반전한 하위 트리의 높이를 원래 높이로 바꾸면 1 + max r.height l.height = 1 + max l.height r.height가 남는다. Nat.max_comm으로 왼쪽의 max에 주어진 두 인수를 바꾼다.

theorem mirrorHeight {α : Type} (t : BinTree α) : (mirror t).height = t.height := α:Typet:BinTree α⊢ (mirror t).height = t.height induction t with α:Type⊢ (mirror BinTree.leaf).height = BinTree.leaf.height All goals completed! 🐙 α:Typel:BinTree αx:αr:BinTree αihL:(mirror l).height = l.heightihR:(mirror r).height = r.height⊢ (mirror (l.node x r)).height = (l.node x r).height α:Typel:BinTree αx:αr:BinTree αihL:(mirror l).height = l.heightihR:(mirror r).height = r.height⊢ 1 + max r.height l.height = 1 + max l.height r.height All goals completed! 🐙
연습문제 7.4.5 논리식의 부정 제거

lit과 var에서는 부정 연산자 수가 0으로 계산된다. neg p에서는 eraseNeg가 바깥의 neg를 제거한다. 전개한 목표의 증명은 ih다. conj p q의 경우에는 두 하위 식의 부정 연산자 수를 세어서 더한다. 두 귀납 가정으로 각 개수를 0으로 바꾼 뒤 합을 계산한다.

theorem eraseNegCount (p : Formula) : p.eraseNeg.negCount = 0 := p:Formula⊢ p.eraseNeg.negCount = 0 induction p with b:Bool⊢ (Formula.lit b).eraseNeg.negCount = 0 All goals completed! 🐙 n:Nat⊢ (Formula.var n).eraseNeg.negCount = 0 All goals completed! 🐙 p:Formulaih:p.eraseNeg.negCount = 0⊢ p.neg.eraseNeg.negCount = 0 All goals completed! 🐙 p:Formulaq:FormulaihP:p.eraseNeg.negCount = 0ihQ:q.eraseNeg.negCount = 0⊢ (p.conj q).eraseNeg.negCount = 0 All goals completed! 🐙
연습문제 7.5.1 귀납 가정의 분석

귀납 가정은 ih : sumAcc xs acc = acc + sumRec xs이고, 두 정의를 전개한 뒤의 목표는 sumAcc xs (acc + x) = acc + (x + sumRec xs)다. 두 호출의 첫째 인수는 xs다. rw [ih]가 찾는 식은 sumAcc xs acc인데, 목표에 있는 호출의 둘째 인수는 acc + x다. 명제의 인수로 acc를 추가했어도, induction xs는 그 값을 분기의 문맥에 고정한다. generalizing acc를 쓰면 꼬리의 귀납 가정에 acc + x를 넘길 수 있다.

연습문제 7.5.2 리스트의 길이

초기 누적값 acc에 리스트의 원소 수만큼 1을 더한다. xs에 대해 귀납하고 acc를 일반화하면 귀납 가정에 acc + 1을 넘길 수 있다. 귀납 단계에서 귀납 가정과 길이의 정의를 적용하면 (acc + 1) + xs.length = acc + (xs.length + 1)이 남는다. addAssoc로 왼쪽의 괄호를 옮기고 addComm으로 안쪽 두 항의 순서를 맞춘다.

theorem lengthAccGeneral {α : Type} (xs : List α) (acc : Nat) : lengthAcc xs acc = acc + xs.length := α:Typexs:List αacc:Nat⊢ lengthAcc xs acc = acc + xs.length induction xs generalizing acc with α:Typeacc:Nat⊢ lengthAcc [] acc = acc + [].length All goals completed! 🐙 α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ lengthAcc (x :: xs) acc = acc + (x :: xs).length α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ acc + 1 + xs.length = acc + (xs.length + 1) All goals completed! 🐙 theorem lengthAccEq {α : Type} (xs : List α) : lengthAcc xs 0 = xs.length := α:Typexs:List α⊢ lengthAcc xs 0 = xs.length All goals completed! 🐙
연습문제 7.5.3 누적 리스트의 길이

직접 귀납할 때는 귀납 가정에 x :: acc를 넣는다. 왼쪽에서 늘어난 1은 누적 리스트의 길이에, 오른쪽에서 늘어난 1은 입력 리스트의 길이에 나타난다. addAssoc로 왼쪽을 (xs.length + acc.length) + 1의 형태로 바꾼 뒤, Nat.add_right_comm으로 acc.length와 1의 순서를 맞춘다.

theorem revAccLength {α : Type} (xs acc : List α) : (revAcc xs acc).length = xs.length + acc.length := α:Typexs:List αacc:List α⊢ (revAcc xs acc).length = xs.length + acc.length induction xs generalizing acc with α:Typeacc:List α⊢ (revAcc [] acc).length = [].length + acc.length All goals completed! 🐙 α:Typex:αxs:List αih:∀ (acc : List α), (revAcc xs acc).length = xs.length + acc.lengthacc:List α⊢ (revAcc (x :: xs) acc).length = (x :: xs).length + acc.length α:Typex:αxs:List αih:∀ (acc : List α), (revAcc xs acc).length = xs.length + acc.lengthacc:List α⊢ xs.length + (acc.length + 1) = xs.length + 1 + acc.length α:Typex:αxs:List αih:∀ (acc : List α), (revAcc xs acc).length = xs.length + acc.lengthacc:List α⊢ xs.length + acc.length + 1 = xs.length + 1 + acc.length All goals completed! 🐙

앞의 정리를 활용하면 revAccGeneral이 revAcc xs acc를 revRec xs ++ acc로 바꾼다. appendLength를 적용하면 acc.length가 합의 둘째 항으로 드러난다. reverseLength로 첫째 항을 xs.length로 바꾼다.

example {α : Type} (xs acc : List α) : (revAcc xs acc).length = xs.length + acc.length := α:Typexs:List αacc:List α⊢ (revAcc xs acc).length = xs.length + acc.length All goals completed! 🐙
연습문제 7.5.4 누적 매개변수를 이용한 팩토리얼

factorialAcc n acc는 초기값 acc에 n부터 1까지 차례로 곱한다. n에 대해 귀납하고 acc를 일반화한다. 기초 단계의 오른쪽은 acc * 1이다. 귀납 단계에서는 acc * (n + 1)을 귀납 가정에 넘기면 왼쪽이 (acc * (n + 1)) * factorial n이 된다. 곱셈의 결합법칙으로 acc * ((n + 1) * factorial n)을 얻는다. 마지막에 acc = 1을 대입하면 Nat.one_mul로 원래 목표를 닫을 수 있다.

theorem factorialAccGeneral (n acc : Nat) : factorialAcc n acc = acc * factorial n := n:Natacc:Nat⊢ factorialAcc n acc = acc * factorial n induction n generalizing acc with acc:Nat⊢ factorialAcc 0 acc = acc * factorial 0 All goals completed! 🐙 n:Natih:∀ (acc : Nat), factorialAcc n acc = acc * factorial nacc:Nat⊢ factorialAcc (n + 1) acc = acc * factorial (n + 1) All goals completed! 🐙 theorem factorialAccEq (n : Nat) : factorialAcc n 1 = factorial n := n:Nat⊢ factorialAcc n 1 = factorial n All goals completed! 🐙
연습문제 7.6.1 두 귀납법의 관계

강한 귀납법의 단계에 해당하는 규칙을 step이라 하자. Q(0)$에서 m\le 0$인 자연수는 0$뿐이다. 0$보다 작은 자연수가 없으므로 step에 넘길 전제가 성립하고 P(0)$을 얻는다.

Q(n)$을 가정하고 m\le n+1$에 대해 P(m)$을 보인다. m\le n$이면 Q(n)$을 바로 쓴다. 나머지는 m=n+1$이다. j<n+1$인 자연수는 모두 j\le n$이므로 Q(n)$에서 P(j)$를 얻을 수 있다. 이 증명들을 step에 주면 P(n+1)$이 성립한다. 따라서 모든 n$에서 Q(n)$을 얻고, m=n$을 넣어 P(n)$을 얻는다.

아래 증명의 allUpTo k가 Q(k)$의 증명이다. Nat.lt_or_ge k m으로 k < m과 m ≤ k를 나눈다.

theorem strongInductionFromNatRec (P : Nat → Prop) (step : ∀ n : Nat, (∀ m : Nat, m < n → P m) → P n) (n : Nat) : P n := P:Nat → Propstep:∀ (n : Nat), (∀ (m : Nat), m < n → P m) → P nn:Nat⊢ P n P:Nat → Propstep:∀ (n : Nat), (∀ (m : Nat), m < n → P m) → P nn:NatallUpTo:∀ (k m : Nat), m ≤ k → P m⊢ P n All goals completed! 🐙
연습문제 7.6.2 피보나치 수의 양수 성질

입력이 0이면 전제 0 < 0이 거짓이므로 함의가 성립한다. 1이면 fib 1이 1로 계산된다. k + 2에서는 귀납 가정에 k + 1을 넣는다. k + 1 < k + 2는 귀납 가정을 적용할 근거이고, 0 < k + 1은 적용하여 얻은 함의의 전제다. fib (k + 1)이 양수이고 fib k는 자연수이므로 그 합도 양수다.

theorem fibPos (n : Nat) : 0 < n → 0 < fib n := n:Nat⊢ 0 < n → 0 < fib n induction n using Nat.strongRecOn with n:Natih:∀ (m : Nat), m < n → 0 < m → 0 < fib m⊢ 0 < n → 0 < fib n cases n with ih:∀ (m : Nat), m < 0 → 0 < m → 0 < fib m⊢ 0 < 0 → 0 < fib 0 All goals completed! 🐙 n:Natih:∀ (m : Nat), m < n + 1 → 0 < m → 0 < fib m⊢ 0 < n + 1 → 0 < fib (n + 1) cases n with ih:∀ (m : Nat), m < 0 + 1 → 0 < m → 0 < fib m⊢ 0 < 0 + 1 → 0 < fib (0 + 1) All goals completed! 🐙 k:Natih:∀ (m : Nat), m < k + 1 + 1 → 0 < m → 0 < fib m⊢ 0 < k + 1 + 1 → 0 < fib (k + 1 + 1) k:Natih:∀ (m : Nat), m < k + 1 + 1 → 0 < m → 0 < fib ma✝:0 < k + 1 + 1⊢ 0 < fib (k + 1 + 1) k:Natih:∀ (m : Nat), m < k + 1 + 1 → 0 < m → 0 < fib ma✝:0 < k + 1 + 1h:0 < fib (k + 1)⊢ 0 < fib (k + 1 + 1) k:Natih:∀ (m : Nat), m < k + 1 + 1 → 0 < m → 0 < fib ma✝:0 < k + 1 + 1h:0 < fib (k + 1)⊢ 0 < fib (k + 1) + fib k All goals completed! 🐙
연습문제 7.6.3 절반 크기의 문제에 대한 귀납 증명

0일 때는 주어진 h₀를 쓴다. k + 1일 때는 입력이 양수이고 나누는 수 2가 1보다 크므로 Nat.div_lt_self로 몫이 입력보다 작음을 증명한다. ih를 몫에 적용해 얻은 증명을 hstep에 넘긴다.

theorem halfInduction (P : Nat → Prop) (h₀ : P 0) (hstep : ∀ n : Nat, 0 < n → P (n / 2) → P n) (n : Nat) : P n := P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nn:Nat⊢ P n induction n using Nat.strongRecOn with P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nn:Natih:∀ (m : Nat), m < n → P m⊢ P n cases n with P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nih:∀ (m : Nat), m < 0 → P m⊢ P 0 All goals completed! 🐙 P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nk:Natih:∀ (m : Nat), m < k + 1 → P m⊢ P (k + 1) P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nk:Natih:∀ (m : Nat), m < k + 1 → P mhpos:0 < k + 1⊢ P (k + 1) P:Nat → Proph₀:P 0hstep:∀ (n : Nat), 0 < n → P (n / 2) → P nk:Natih:∀ (m : Nat), m < k + 1 → P mhpos:0 < k + 1hlt:(k + 1) / 2 < k + 1⊢ P (k + 1) All goals completed! 🐙
연습문제 7.7.1 피보나치 함수의 양수 성질

처음 두 분기는 decide로 닫는다. 셋째 분기에서 ih₁ : 0 < k + 1 → 0 < fib (k + 1)을 이용한다. Nat.succ_pos k를 넘겨 fib (k + 1)이 양수임을 얻고, omega로 이 항을 포함한 두 자연수의 합이 양수임을 보인다. 연습문제 7.6.2에서는 강한 귀납 가정에서 이 함의를 얻기 위해 k + 1 < k + 2도 먼저 증명했다.

theorem fibPosFun (n : Nat) : 0 < n → 0 < fib n := n:Nat⊢ 0 < n → 0 < fib n fun_induction fib n with ⊢ 0 < 0 → 0 < 0 All goals completed! 🐙 ⊢ 0 < 1 → 0 < 1 All goals completed! 🐙 k:Natih₁:0 < k + 1 → 0 < fib (k + 1)ih₀:0 < k → 0 < fib k⊢ 0 < k.succ.succ → 0 < fib (k + 1) + fib k k:Natih₁:0 < k + 1 → 0 < fib (k + 1)ih₀:0 < k → 0 < fib ka✝:0 < k.succ.succ⊢ 0 < fib (k + 1) + fib k k:Natih₁:0 < k + 1 → 0 < fib (k + 1)ih₀:0 < k → 0 < fib ka✝:0 < k.succ.succh:0 < fib (k + 1)⊢ 0 < fib (k + 1) + fib k All goals completed! 🐙
연습문제 7.7.2 리스트에서 첫 번째 0까지의 길이

구조적 귀납법은 빈 리스트와 x :: xs의 경우를 만든다. cons 분기에는 꼬리에 대한 귀납 가정이 들어 있다. by_cases로 x = 0인 경우를 나누고, 그 경우에는 반환값이 0이므로 Nat.zero_le를 쓴다. x ≠ 0이면 반환값과 길이를 전개한다. 양변에서 더하는 1의 위치를 맞춘 뒤, 귀납 가정의 양변에 1을 더한다.

theorem countUntilZeroLeLength (xs : List Nat) : countUntilZero xs ≤ xs.length := xs:List Nat⊢ countUntilZero xs ≤ xs.length induction xs with ⊢ countUntilZero [] ≤ [].length All goals completed! 🐙 x:Natxs:List Natih:countUntilZero xs ≤ xs.length⊢ countUntilZero (x :: xs) ≤ (x :: xs).length x:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:x = 0⊢ countUntilZero (x :: xs) ≤ (x :: xs).lengthx:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:¬x = 0⊢ countUntilZero (x :: xs) ≤ (x :: xs).length x:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:x = 0⊢ countUntilZero (x :: xs) ≤ (x :: xs).length All goals completed! 🐙 x:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:¬x = 0⊢ countUntilZero (x :: xs) ≤ (x :: xs).length x:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:¬x = 0⊢ 1 + countUntilZero xs ≤ xs.length + 1 x:Natxs:List Natih:countUntilZero xs ≤ xs.lengthh:¬x = 0⊢ countUntilZero xs + 1 ≤ xs.length + 1 All goals completed! 🐙

countUntilZero.induct에는 빈 리스트, 0 :: xs, x ≠ 0인 x :: xs에 대응하는 분기가 있다. 셋째 분기에서만 재귀 호출하므로 그 분기에서만 꼬리에 대한 귀납 가정을 얻는다. fun_induction은 분기별로 countUntilZero의 호출을 전개한다. 셋째 분기에 남은 산술은 위 증명과 같다.

countUntilZero.induct (motive : List Nat → Prop) (case1 : motive []) (case2 : ∀ (xs : List Nat), motive (0 :: xs)) (case3 : ∀ (x : Nat) (xs : List Nat), ¬x = 0 → motive xs → motive (x :: xs)) (a✝ : List Nat) : motive a✝#check countUntilZero.induct theorem countUntilZeroLeLengthFun (xs : List Nat) : countUntilZero xs ≤ xs.length := xs:List Nat⊢ countUntilZero xs ≤ xs.length fun_induction countUntilZero xs with ⊢ 0 ≤ [].length All goals completed! 🐙 xs:List Nat⊢ 0 ≤ (0 :: xs).length All goals completed! 🐙 x:Natxs:List Nath:¬x = 0ih:countUntilZero xs ≤ xs.length⊢ 1 + countUntilZero xs ≤ (x :: xs).length x:Natxs:List Nath:¬x = 0ih:countUntilZero xs ≤ xs.length⊢ countUntilZero xs + 1 ≤ xs.length + 1 All goals completed! 🐙