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

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

연습문제 8.1.1 두 자연수의 최솟값 명세

r ≤ a, r ≤ b, r = a ∨ r = b가 모두 성립해야 한다. 입력이 3과 5일 때 처음 두 조건은 2도 허용한다. 마지막 조건을 더하면 두 입력 중 작은 값인 3만 남는다.

연습문제 8.1.2 불충분한 최댓값 명세의 반례

자연수 덧셈에서 a + b는 항상 두 항 이상이다. 2와 4를 입력하면 결과는 6이다. 6은 두 입력 이상이지만 최댓값은 4다. r = a ∨ r = b를 더하면 이런 결과를 제외할 수 있다. 한 입력이 0일 때에는 합과 최댓값이 같다.

example (a b : Nat) : a ≤ maxCandidate a b ∧ b ≤ maxCandidate a b := a:Natb:Nat⊢ a ≤ maxCandidate a b ∧ b ≤ maxCandidate a b a:Natb:Nat⊢ a ≤ a + b ∧ b ≤ a + b All goals completed! 🐙 6#eval maxCandidate 2 4 4#eval maxOfTwo 2 4
연습문제 8.1.3 잘못된 프로그램의 반례 찾기

(1, 0)에서 maxOfTwoWrong은 0을 반환한다. 최댓값은 1이다. 자연수 입력에서 b = 0이고 a > 0이면 오류가 나는 분기에 들어간다. 앞의 세 테스트에는 둘째 입력이 0인 쌍이 없었다. 두 입력이 모두 0인 경우에는 첫 분기가 실행되어 올바른 결과인 0을 반환한다.

0#eval maxOfTwoWrong 1 0 1#eval maxOfTwo 1 0
연습문제 8.1.4 정수 절댓값 함수의 정확성

x < 0이면 반환값은 -x이므로 양수이고, 두 번째 조건의 오른쪽 등식이 성립한다. 0 ≤ x이면 반환값은 x이므로 두 조건을 만족한다. Lean에서는 정의를 전개하고 두 경우로 나눈 뒤 omega를 적용한다.

theorem absInt_correct (x : Int) : 0 ≤ absInt x ∧ (absInt x = x ∨ absInt x = -x) := x:Int⊢ 0 ≤ absInt x ∧ (absInt x = x ∨ absInt x = -x) x:Int⊢ (0 ≤ if x < 0 then -x else x) ∧ ((if x < 0 then -x else x) = x ∨ (if x < 0 then -x else x) = -x) x:Inth✝:x < 0⊢ 0 ≤ -x ∧ (-x = x ∨ -x = -x)x:Inth✝:¬x < 0⊢ 0 ≤ x ∧ (x = x ∨ x = -x) x:Inth✝:x < 0⊢ 0 ≤ -x ∧ (-x = x ∨ -x = -x) All goals completed! 🐙 x:Inth✝:¬x < 0⊢ 0 ≤ x ∧ (x = x ∨ x = -x) All goals completed! 🐙
연습문제 8.1.5 부분 정확성과 전체 정확성의 구분

모든 자연수를 허용하면 A는 부분 정확성과 전체 정확성을 모두 만족한다. B는 반환하는 경우마다 n을 반환하므로 부분 정확성을 만족하지만, 0에서 종료하지 않아 전체 정확성을 만족하지 않는다. C는 모든 입력에서 종료하지만 0에서 1을 반환하므로 두 정확성을 모두 만족하지 않는다.

0 < n을 사전조건으로 두면 0은 검사할 입력의 범위에서 빠진다. 그 범위에서는 세 프로그램이 모두 계산을 마치고 n을 반환하므로 부분 정확성과 전체 정확성을 모두 만족한다.

연습문제 8.2.1 두 자연수의 최솟값 명세 작성

반환값은 두 입력 이하이고 두 입력 가운데 하나다. Nat.min_def로 min a b를 조건식으로 바꾸면 a ≤ b인 경우에는 a가, 그 밖의 경우에는 b가 세 조건을 만족함을 보이면 된다.

def MinSpec (a b r : Nat) : Prop := r ≤ a ∧ r ≤ b ∧ (r = a ∨ r = b) theorem min_satisfies_spec (a b : Nat) : MinSpec a b (min a b) := a:Natb:Nat⊢ MinSpec a b (min a b) a:Natb:Nat⊢ min a b ≤ a ∧ min a b ≤ b ∧ (min a b = a ∨ min a b = b) a:Natb:Nat⊢ (if a ≤ b then a else b) ≤ a ∧ (if a ≤ b then a else b) ≤ b ∧ ((if a ≤ b then a else b) = a ∨ (if a ≤ b then a else b) = b) a:Natb:Nath✝:a ≤ b⊢ a ≤ a ∧ a ≤ b ∧ (a = a ∨ a = b)a:Natb:Nath✝:¬a ≤ b⊢ b ≤ a ∧ b ≤ b ∧ (b = a ∨ b = b) a:Natb:Nath✝:a ≤ b⊢ a ≤ a ∧ a ≤ b ∧ (a = a ∨ a = b) All goals completed! 🐙 a:Natb:Nath✝:¬a ≤ b⊢ b ≤ a ∧ b ≤ b ∧ (b = a ∨ b = b) All goals completed! 🐙
연습문제 8.2.2 자연수 제곱합의 함수적 명세

공식을 그대로 기준 함수로 적고 전칭 한정사를 사용하여 모든 입력의 반환값이 그 함수의 값과 같다는 명제를 쓴다. #check는 이 명제의 타입을 확인한다. 세 #guard는 두 함수의 값이 차례로 0, 1, 30인지 검사한다.

def sumSquaresSpec (n : Nat) : Nat := n * (n + 1) * (2 * n + 1) / 6 ∀ (n : Nat), sumSquares n = sumSquaresSpec n : Prop#check ∀ n : Nat, sumSquares n = sumSquaresSpec n #guard sumSquares 0 == 0 && sumSquaresSpec 0 == 0 #guard sumSquares 1 == 1 && sumSquaresSpec 1 == 1 #guard sumSquares 4 == 30 && sumSquaresSpec 4 == 30
연습문제 8.2.3 두 자연수의 차의 절댓값 명세

a가 작거나 같으면 a + r = b이고, b가 작거나 같으면 b + r = a다. 두 식을 ∨로 연결하면 입력의 순서가 바뀌어도 같은 값을 허용한다.

def AbsDiffSpec (a b r : Nat) : Prop := a + r = b ∨ b + r = a example : AbsDiffSpec 3 7 4 := ⊢ AbsDiffSpec 3 7 4 ⊢ 3 + 4 = 7 ∨ 7 + 4 = 3 All goals completed! 🐙 example : AbsDiffSpec 7 3 4 := ⊢ AbsDiffSpec 7 3 4 ⊢ 7 + 4 = 3 ∨ 3 + 4 = 7 All goals completed! 🐙
연습문제 8.2.4 범위 명세가 허용하는 반환값

BetweenSpec 2 5 r은 2, 3, 4, 5를 허용한다. BetweenSpec 4 4 r에서는 4 ≤ r과 r ≤ 4가 모두 필요하므로 4만 허용한다. BetweenSpec 7 3 r을 만족하는 값을 두 부등식에 넣으면 7 ≤ 3이 따라오므로 허용되는 값이 없다.

연습문제 8.2.5 불충분한 최솟값 명세의 반례

0은 모든 자연수 이하이므로 언제나 0을 반환하는 함수가 두 부등식을 만족한다. (3, 5)의 최솟값은 3이다. MinSpec의 r = a ∨ r = b 조건은 이 입력에서 0을 배제한다.

def minCandidate (_ _ : Nat) : Nat := 0 example (a b : Nat) : minCandidate a b ≤ a ∧ minCandidate a b ≤ b := ⟨Nat.zero_le a, Nat.zero_le b⟩ example : ¬ MinSpec 3 5 (minCandidate 3 5) := ⊢ ¬MinSpec 3 5 (minCandidate 3 5) ⊢ ¬(0 ≤ 3 ∧ 0 ≤ 5 ∧ (0 = 3 ∨ 0 = 5)) All goals completed! 🐙
연습문제 8.2.6 정렬 프로그램의 불충분한 명세

빈 리스트에는 비교할 원소 쌍이 없으므로 List.Pairwise.nil로 약한 명세를 증명할 수 있다. 예를 들어 badSort [3, 1]은 두 원소를 모두 버린다. 순서 조건에 더해 출력이 입력의 순열, 즉 각 원소의 개수를 보존하며 순서만 바꾼 리스트임을 요구해야 한다.

theorem badSort_satisfies_weak_spec (xs : List Nat) : WeakSortSpec xs (badSort xs) := List.Pairwise.nil
연습문제 8.2.7 함수적 명세를 관계적 명세로 바꾸기

SpecGraph는 후보 r이 기준값 spec x와 같을 때 성립한다. r에 f x를 대입해 정의를 전개하면 양쪽 전칭 명제가 정의적으로 같아진다. Iff.rfl은 같은 명제가 자신과 동치라는 증명이다.

def SpecGraph {α β : Type} (spec : α → β) (x : α) (r : β) : Prop := r = spec x example {α β : Type} (f spec : α → β) : (∀ x, f x = spec x) ↔ (∀ x, SpecGraph spec x (f x)) := Iff.rfl
연습문제 8.3.1 범위 명세의 사전조건

사전조건은 a ≤ b다. a = b이면 양쪽 부등식을 만족하는 값은 a뿐이다. a > b에서 가능한 값 r이 있다면 a ≤ r ≤ b로부터 a ≤ b가 따라 모순이 된다. BetweenPre a b를 펼치면 앞 절 정리의 오른쪽 a ≤ b와 같으므로 .symm으로 동치의 방향을 바꾼다.

theorem betweenPre_iff_has_result (a b : Nat) : BetweenPre a b ↔ (∃ r : Nat, BetweenSpec a b r) := (betweenSpec_has_result a b).symm
연습문제 8.3.2 자연수 뺄셈의 사후조건

사후조건은 r + b = a다. a = 7, b = 3일 때 r = 4는 4 + 3 = 7을 만족한다. r = 5를 넣으면 왼쪽이 8이 되어 등식이 성립하지 않는다.

연습문제 8.3.3 사전조건이 없는 뺄셈 정리의 반례

a = 2, b = 5이면 자연수 뺄셈의 결과가 0이다. 사후조건의 왼쪽 0 + 5는 5이고 오른쪽은 2다. 하나의 자연수 쌍에서 등식이 성립하지 않으므로 전칭 명제의 반례가 된다.

example : ¬ SubPost 2 5 (subtract 2 5) := ⊢ ¬SubPost 2 5 (subtract 2 5) ⊢ ¬2 - 5 + 5 = 2 All goals completed! 🐙
연습문제 8.3.4 짝수의 절반을 구하는 함수

n이 짝수라는 사전조건은 n % 2 = 0으로, 반환값 r이 n의 절반이라는 사후조건은 2 * r = n으로 적는다. Nat.div_add_mod n 2의 왼쪽에서 나머지를 0으로 바꾸고 Nat.add_zero를 적용한다.

def HalfPre (n : Nat) : Prop := n % 2 = 0 def HalfPost (n r : Nat) : Prop := 2 * r = n theorem half_correct (n : Nat) (h : HalfPre n) : HalfPost n (half n) := n:Nath:HalfPre n⊢ HalfPost n (half n) n:Nath:n % 2 = 0⊢ HalfPost n (half n) n:Nath:n % 2 = 0⊢ 2 * (n / 2) = n n:Nath:n % 2 = 0hdiv:2 * (n / 2) + n % 2 = n⊢ 2 * (n / 2) = n n:Nath:n % 2 = 0hdiv:2 * (n / 2) = n⊢ 2 * (n / 2) = n All goals completed! 🐙
연습문제 8.3.5 리스트의 첫 번째 원소 명세

사전조건은 입력이 빈 리스트와 다르다는 명제다. 사후조건은 반환값을 머리로 하며 입력의 나머지 부분을 꼬리로 하는 리스트가 입력과 같다는 명제다. 빈 리스트 분기에서는 사전조건과 모순이 생긴다. 머리와 꼬리가 있는 분기에서는 그 꼬리를 존재 명제의 증인으로 쓴다.

def HeadPre (xs : List Nat) : Prop := xs ≠ [] def HeadPost (xs : List Nat) (r : Nat) : Prop := ∃ rest : List Nat, xs = r :: rest theorem firstOrZero_correct (xs : List Nat) (h : HeadPre xs) : HeadPost xs (firstOrZero xs) := xs:List Nath:HeadPre xs⊢ HeadPost xs (firstOrZero xs) cases xs with h:HeadPre []⊢ HeadPost [] (firstOrZero []) All goals completed! 🐙 x:Natrest:List Nath:HeadPre (x :: rest)⊢ HeadPost (x :: rest) (firstOrZero (x :: rest)) All goals completed! 🐙
연습문제 8.3.6 사전조건의 강도와 보장 범위

False.elim은 가정 h : False에서 어떤 명제든 얻는다. 정리를 실제 입력에 적용하려면 False의 증명을 함께 제출해야 하므로 이 정리로 보장받는 자연수 입력은 없다. 사전조건을 강화할 때는 결과가 필요한 입력이 그 조건을 만족하는지 함께 확인해야 한다.

연습문제 8.3.7 Correct로 몫과 나머지의 정확성 표현하기

입력 p의 두 성분을 DivPre에 넘긴다. 사후조건에서는 반환값 qr의 두 성분까지 DivPost에 넘긴다. intro p h 뒤의 목표와 divMod_correct p.1 p.2 h의 결론은 divModPair를 펼치면 같다.

theorem divModPair_correct : Correct divModPair (fun p => DivPre p.1 p.2) (fun p qr => DivPost p.1 p.2 qr.1 qr.2) := ⊢ Correct divModPair (fun p => DivPre p.fst p.snd) fun p qr => DivPost p.fst p.snd qr.fst qr.snd p:Nat × Nath:DivPre p.fst p.snd⊢ DivPost p.fst p.snd (divModPair p).fst (divModPair p).snd All goals completed! 🐙
연습문제 8.4.1 두 자연수 덧셈 함수의 동등성

둘째 인수가 0이면 양쪽 모두 a다. 둘째 인수가 b + 1이면 두 정의를 펼쳤을 때 양쪽에 후속자가 붙는다. ih는 후속자 안의 addByRecursion a b를 a + b로 바꾸는 데 쓴다.

theorem addByRecursion_correct (a b : Nat) : addByRecursion a b = a + b := a:Natb:Nat⊢ addByRecursion a b = a + b induction b with a:Nat⊢ addByRecursion a 0 = a + 0 All goals completed! 🐙 a:Natb:Natih:addByRecursion a b = a + b⊢ addByRecursion a (b + 1) = a + (b + 1) All goals completed! 🐙
연습문제 8.4.2 두 최댓값 구현의 동등성

a < b일 때는 두 함수 모두 b를 반환한다. b < a이면 두 함수 모두 a를 반환한다. a = b인 경우 maxFromRight는 a를, maxOfTwo는 b를 반환하므로 이때도 값은 같다. 코드에서는 두 조건식을 split으로 나누고, 남은 등식과 모순을 omega로 처리한다.

theorem maxFromRight_eq_maxOfTwo (a b : Nat) : maxFromRight a b = maxOfTwo a b := a:Natb:Nat⊢ maxFromRight a b = maxOfTwo a b a:Natb:Nat⊢ (if b ≤ a then a else b) = if a ≤ b then b else a a:Natb:Nath✝:b ≤ a⊢ a = if a ≤ b then b else aa:Natb:Nath✝:¬b ≤ a⊢ b = if a ≤ b then b else a a:Natb:Nath✝:b ≤ a⊢ a = if a ≤ b then b else aa:Natb:Nath✝:¬b ≤ a⊢ b = if a ≤ b then b else a a:Natb:Nath✝¹:¬b ≤ ah✝:a ≤ b⊢ b = ba:Natb:Nath✝¹:¬b ≤ ah✝:¬a ≤ b⊢ b = a a:Natb:Nath✝¹:b ≤ ah✝:a ≤ b⊢ a = ba:Natb:Nath✝¹:b ≤ ah✝:¬a ≤ b⊢ a = aa:Natb:Nath✝¹:¬b ≤ ah✝:a ≤ b⊢ b = ba:Natb:Nath✝¹:¬b ≤ ah✝:¬a ≤ b⊢ b = a All goals completed! 🐙
연습문제 8.4.3 누산기로 두 리스트의 길이 세기

lengthAcc xs acc는 이미 센 개수 acc에 xs.length를 더한다. cons 분기에서 귀납 가정에 acc + 1을 넘긴 뒤, 덧셈의 결합법칙과 교환법칙으로 두 식의 항을 맞춘다. 초기 누산기에 ys.length를 넣으면 ys.length + xs.length를 얻고, 이는 연결한 리스트의 길이다.

theorem lengthAcc_matches_length {α : 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 lengthAcc_eq_append_length {α : Type} (xs ys : List α) : lengthAcc xs ys.length = (ys ++ xs).length := α:Typexs:List αys:List α⊢ lengthAcc xs ys.length = (ys ++ xs).length All goals completed! 🐙
연습문제 8.4.4 누산기를 이용한 리스트의 원소 합

sumAux의 재귀 호출은 acc + x를 넘긴다. 누산기를 일반화한 귀납 가정을 그 값에 적용하면 (acc + x) + sumSlow xs가 된다. Nat.add_assoc로 괄호를 맞춘다. sumFast는 초기 누산기가 0인 경우이므로 Nat.zero_add로 왼쪽의 0을 없앤다. 원소마다 한 번씩 더하므로 두 구현의 덧셈 횟수는 모두 xs.length다.

theorem sumAux_correct (xs : List Nat) (acc : Nat) : sumAux xs acc = acc + sumSlow xs := xs:List Natacc:Nat⊢ sumAux xs acc = acc + sumSlow xs induction xs generalizing acc with acc:Nat⊢ sumAux [] acc = acc + sumSlow [] All goals completed! 🐙 x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ sumAux (x :: xs) acc = acc + sumSlow (x :: xs) All goals completed! 🐙 theorem sum_correct (xs : List Nat) : sumFast xs = sumSlow xs := xs:List Nat⊢ sumFast xs = sumSlow xs xs:List Nat⊢ sumAux xs 0 = sumSlow xs All goals completed! 🐙
연습문제 8.4.5 리스트 연결 함수의 동등성

두 연결은 첫째 리스트의 생성자에 따라 계산하므로 xs에 대해 귀납한다. 빈 리스트를 연결하면 양변이 ys다. cons 분기에서는 양쪽에 먼저 x를 붙이고 각각의 방법으로 꼬리와 ys를 연결한다. 꼬리에서 두 결과가 같다는 증명이 ih다.

theorem appendRec_correct {α : Type} (xs ys : List α) : appendRec xs ys = xs ++ ys := α:Typexs:List αys:List α⊢ appendRec xs ys = xs ++ ys induction xs with α:Typeys:List α⊢ appendRec [] ys = [] ++ ys All goals completed! 🐙 α:Typeys:List αx:αxs:List αih:appendRec xs ys = xs ++ ys⊢ appendRec (x :: xs) ys = x :: xs ++ ys All goals completed! 🐙
연습문제 8.4.6 리스트를 두 번 뒤집는 함수의 정확성

두 번의 reverseFast를 List.reverse로 바꾸면 목표는 xs.reverse.reverse = xs가 된다. List.reverse_reverse가 이 등식의 증명이다.

theorem reverseFast_twice {α : Type} (xs : List α) : reverseFast (reverseFast xs) = xs := α:Typexs:List α⊢ reverseFast (reverseFast xs) = xs All goals completed! 🐙
연습문제 8.4.7 동등성을 이용한 길이 보존의 증명

사전조건은 항상 참이다. 기준 구현의 반환값을 표준 함수의 결과로 바꾸고 List.length_reverse를 적용하면 길이가 같음을 얻는다. correct_of_equiv에 그 증명과 두 구현의 동등성 정리를 넘기면 개선된 구현에서도 길이가 보존된다.

theorem reverseSlow_length {α : Type} : Correct (@reverseSlow α) (fun _ => True) (fun xs ys => ys.length = xs.length) := α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.length α:Typexs:List αa✝:True⊢ (reverseSlow xs).length = xs.length All goals completed! 🐙 theorem reverseFast_length {α : Type} : Correct (@reverseFast α) (fun _ => True) (fun xs ys => ys.length = xs.length) := α:Type⊢ Correct reverseFast (fun x => True) fun xs ys => ys.length = xs.length α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.lengthα:Type⊢ ∀ (x : List α), True → reverseFast x = reverseSlow x α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.length All goals completed! 🐙 α:Type⊢ ∀ (x : List α), True → reverseFast x = reverseSlow x α:Typexs:List αa✝:True⊢ reverseFast xs = reverseSlow xs All goals completed! 🐙
연습문제 8.5.1 선형 탐색의 실행 과정 추적

2를 찾을 때는 4, 7, 2를 차례로 비교한다. 세 번째 비교에서 일치하므로 true를 반환한다. 6을 찾을 때는 4, 7, 2, 9, 5를 모두 비교한다. 다섯 번째 비교 뒤 빈 리스트에 도달하여 false를 반환한다.

#guard linearSearch 2 [4, 7, 2, 9, 5] == true #guard linearSearch 6 [4, 7, 2, 9, 5] == false
연습문제 8.5.2 항상 성공하는 탐색의 완전성

대상의 포함을 가정하면 반환값이 true여야 한다. searchAlwaysTrue는 모든 입력에서 true로 계산되므로 가정을 받은 뒤 rfl로 증명한다. 빈 리스트를 주어도 true를 돌려주므로, 6과 빈 리스트는 건전성의 반례다.

theorem searchAlwaysTrue_complete (target : Nat) (xs : List Nat) : target ∈ xs → searchAlwaysTrue target xs = true := target:Natxs:List Nat⊢ target ∈ xs → searchAlwaysTrue target xs = true target:Natxs:List Nata✝:target ∈ xs⊢ searchAlwaysTrue target xs = true All goals completed! 🐙 example : ¬ SearchSpec 6 [] (searchAlwaysTrue 6 []) := ⊢ ¬SearchSpec 6 [] (searchAlwaysTrue 6 []) All goals completed! 🐙
연습문제 8.5.3 항상 실패하는 탐색의 건전성

반환값이 true라는 전제는 정의를 펼치면 false = true가 된다. 이 전제가 성립하는 입력이 없으므로 건전성의 함의는 공허하게 참이다. 7이 들어 있는 [7]에서도 false를 반환하므로 완전성에는 반례가 있다.

theorem searchAlwaysFalse_sound (target : Nat) (xs : List Nat) : searchAlwaysFalse target xs = true → target ∈ xs := target:Natxs:List Nat⊢ searchAlwaysFalse target xs = true → target ∈ xs All goals completed! 🐙 example : ¬ SearchSpec 7 [7] (searchAlwaysFalse 7 [7]) := ⊢ ¬SearchSpec 7 [7] (searchAlwaysFalse 7 [7]) All goals completed! 🐙
연습문제 8.5.4 탐색 실패의 동치

not_congr로 이미 증명한 동치의 양쪽에 부정을 붙인다. Bool.not_eq_true로 왼쪽의 ¬ (linearSearch target xs = true)를 linearSearch target xs = false로 바꾼다.

theorem linearSearch_false_iff (target : Nat) (xs : List Nat) : linearSearch target xs = false ↔ target ∉ xs := target:Natxs:List Nat⊢ linearSearch target xs = false ↔ ¬target ∈ xs target:Natxs:List Naths:SearchSpec target xs (linearSearch target xs)⊢ linearSearch target xs = false ↔ ¬target ∈ xs target:Natxs:List Naths:linearSearch target xs = true ↔ target ∈ xs⊢ linearSearch target xs = false ↔ ¬target ∈ xs All goals completed! 🐙
연습문제 8.5.5 짝수 탐색의 정확성

머리가 짝수면 그 머리가 존재 명제의 증인이다. 머리가 홀수인 분기에서는 짝수인 증인이 있다면 꼬리에 있어야 한다. simp는 cons의 존재 명제를 머리에 대한 경우와 꼬리에서의 존재로 나누므로, 꼬리의 존재와 탐색 결과는 ih로 연결할 수 있다.

theorem hasEven_correct (xs : List Nat) : hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0 := xs:List Nat⊢ hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0 induction xs with ⊢ hasEven [] = true ↔ ∃ x, x ∈ [] ∧ x % 2 = 0 All goals completed! 🐙 x:Natxs:List Natih:hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0⊢ hasEven (x :: xs) = true ↔ ∃ x_1, x_1 ∈ x :: xs ∧ x_1 % 2 = 0 x:Natxs:List Natih:hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0hx:x % 2 = 0⊢ hasEven (x :: xs) = true ↔ ∃ x_1, x_1 ∈ x :: xs ∧ x_1 % 2 = 0x:Natxs:List Natih:hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0hx:¬x % 2 = 0⊢ hasEven (x :: xs) = true ↔ ∃ x_1, x_1 ∈ x :: xs ∧ x_1 % 2 = 0 x:Natxs:List Natih:hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0hx:x % 2 = 0⊢ hasEven (x :: xs) = true ↔ ∃ x_1, x_1 ∈ x :: xs ∧ x_1 % 2 = 0 All goals completed! 🐙 x:Natxs:List Natih:hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0hx:¬x % 2 = 0⊢ hasEven (x :: xs) = true ↔ ∃ x_1, x_1 ∈ x :: xs ∧ x_1 % 2 = 0 All goals completed! 🐙
연습문제 8.5.6 모든 원소의 짝수 여부 검사

모든 원소가 짝수라는 조건은 머리가 짝수이고 꼬리의 모든 원소도 짝수라는 조건으로 나뉜다. 머리가 짝수인 분기에서는 귀납 가정으로 꼬리의 계산 결과와 전칭 명제를 연결한다. 머리가 홀수이면 전체 조건에 반례가 있다. 빈 리스트에서는 짝수를 찾는 존재 명제가 거짓이고, 모두 짝수라는 전칭 명제는 반례가 없어 참이다.

theorem allEven_correct (xs : List Nat) : allEven xs = true ↔ ∀ x, x ∈ xs → x % 2 = 0 := xs:List Nat⊢ allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0 induction xs with ⊢ allEven [] = true ↔ ∀ (x : Nat), x ∈ [] → x % 2 = 0 All goals completed! 🐙 x:Natxs:List Natih:allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0⊢ allEven (x :: xs) = true ↔ ∀ (x_1 : Nat), x_1 ∈ x :: xs → x_1 % 2 = 0 x:Natxs:List Natih:allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0hx:x % 2 = 0⊢ allEven (x :: xs) = true ↔ ∀ (x_1 : Nat), x_1 ∈ x :: xs → x_1 % 2 = 0x:Natxs:List Natih:allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0hx:¬x % 2 = 0⊢ allEven (x :: xs) = true ↔ ∀ (x_1 : Nat), x_1 ∈ x :: xs → x_1 % 2 = 0 x:Natxs:List Natih:allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0hx:x % 2 = 0⊢ allEven (x :: xs) = true ↔ ∀ (x_1 : Nat), x_1 ∈ x :: xs → x_1 % 2 = 0 All goals completed! 🐙 x:Natxs:List Natih:allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0hx:¬x % 2 = 0⊢ allEven (x :: xs) = true ↔ ∀ (x_1 : Nat), x_1 ∈ x :: xs → x_1 % 2 = 0 All goals completed! 🐙
연습문제 8.5.7 위치를 반환하는 선형 탐색

IndexSearchSpec은 반환값에 따라 어떤 명제를 증명할지 정한다. 머리가 대상과 같으면 인덱스 0의 원소가 바로 대상이다. 머리가 다르면 꼬리에 적용한 귀납 가정을 읽는다. 꼬리에서 얻은 위치에 1을 더하면 전체 리스트에서도 같은 원소를 가리킨다. 꼬리에 대상이 없고 머리도 대상과 다르면 전체 리스트에도 대상이 없다.

def IndexSearchSpec (target : Nat) (xs : List Nat) (r : Option Nat) : Prop := match r with | none => target ∉ xs | some i => xs[i]? = some target theorem findIndex_correct (target : Nat) (xs : List Nat) : IndexSearchSpec target xs (findIndex target xs) := target:Natxs:List Nat⊢ IndexSearchSpec target xs (findIndex target xs) induction xs with target:Nat⊢ IndexSearchSpec target [] (findIndex target []) All goals completed! 🐙 target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs))target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:¬x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) All goals completed! 🐙 target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:¬x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) cases h : findIndex target xs with target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:¬x = targeth:findIndex target xs = none⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) All goals completed! 🐙 target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:¬x = targeti:Nath:findIndex target xs = some i⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs)) All goals completed! 🐙

target ∈ xs일 때 none을 반환한다고 가정하면 명세의 target ∉ xs와 모순이다. 따라서 어떤 some i를 반환한다. 처음 발견한 위치까지 보장하려면 some i 분기에 ∀ j, j < i → xs[j]? ≠ some target 조건을 더한다.

연습문제 8.6.1 삽입 정렬의 실행 과정 추적

빈 리스트가 반환된 뒤 맨 오른쪽의 2를 넣어 [2]가 된다. 이어 1, 2, 3을 차례로 넣어 [1, 2], [1, 2, 2], [1, 2, 2, 3]을 얻는다. 세 번째 삽입에서 1을 지나 기존 2 앞에 새 2를 놓는다.

[1, 2, 2, 3]#eval insertionSort [3, 2, 1, 2]
연습문제 8.6.2 오름차순 조건 판별

[]와 [3]에서는 순서를 위반할 원소 쌍이 없다. [1, 2, 2, 4]에서는 이웃한 두 2에 대해서도 2 ≤ 2가 성립한다. [1, 3, 2]에서는 3이 2보다 앞에 있지만 3 ≤ 2가 성립하지 않는다.

example : Sorted [] := List.Pairwise.nil example : Sorted [3] := ⊢ Sorted [3] All goals completed! 🐙 example : Sorted [1, 2, 2, 4] := ⊢ Sorted [1, 2, 2, 4] ⊢ List.Pairwise (fun x1 x2 => x1 ≤ x2) [1, 2, 2, 4] All goals completed! 🐙 example : ¬ Sorted [1, 3, 2] := ⊢ ¬Sorted [1, 3, 2] ⊢ ¬List.Pairwise (fun x1 x2 => x1 ≤ x2) [1, 3, 2] All goals completed! 🐙
연습문제 8.6.3 인접한 원소를 교환하여 순열 증명하기

첫 교환의 결과는 [2, 1, 3]이다. 두 번째 교환은 꼬리 [1, 3]에 적용하므로 List.Perm.cons 2로 다시 머리를 붙인다.

example : ([1, 2, 3] : List Nat).Perm [2, 3, 1] := ⊢ [1, 2, 3].Perm [2, 3, 1] first:[1, 2, 3].Perm [2, 1, 3]⊢ [1, 2, 3].Perm [2, 3, 1] first:[1, 2, 3].Perm [2, 1, 3]second:[2, 1, 3].Perm [2, 3, 1]⊢ [1, 2, 3].Perm [2, 3, 1] All goals completed! 🐙
연습문제 8.6.4 불충분한 정렬 명세의 반례

badSort [3, 1, 1]은 []다. 비교할 쌍이 없으므로 정렬 조건은 성립한다. 순열이면 길이가 같아야 하는데 이 경우에는 3과 0이 다르다. 입력을 그대로 반환하면 List.Perm.refl로 순열을 증명할 수 있지만, 앞에 있는 3이 뒤의 1보다 크므로 정렬 조건은 성립하지 않는다.

연습문제 8.6.5 삽입 후 리스트의 길이

맨 앞에 삽입하면 길이가 곧바로 1 늘어난다. 꼬리에 삽입할 때에는 귀납 가정으로 꼬리의 길이가 1 늘어나고, 머리 y가 양쪽 리스트에 하나씩 남는다. 순열 정리로 시작하면 x :: xs와 길이가 같다는 성질만 꺼내면 된다.

theorem insertNat_length (x : Nat) (xs : List Nat) : (insertNat x xs).length = xs.length + 1 := x:Natxs:List Nat⊢ (insertNat x xs).length = xs.length + 1 induction xs with x:Nat⊢ (insertNat x []).length = [].length + 1 All goals completed! 🐙 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (x :: y :: ys).length = (y :: ys).length + 1 All goals completed! 🐙 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1 x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (y :: insertNat x ys).length = (y :: ys).length + 1 All goals completed! 🐙 example (x : Nat) (xs : List Nat) : (insertNat x xs).length = xs.length + 1 := x:Natxs:List Nat⊢ (insertNat x xs).length = xs.length + 1 All goals completed! 🐙
연습문제 8.6.6 정렬 전후의 길이 보존

빈 리스트에서는 길이가 0이다. 귀납 단계에서는 insertNat_length를 적용한 뒤 귀납 가정으로 정렬한 꼬리의 길이를 원래 꼬리의 길이로 바꾼다. 순열 정리를 사용하면 길이 보존을 바로 얻는다.

theorem insertionSort_length (xs : List Nat) : (insertionSort xs).length = xs.length := xs:List Nat⊢ (insertionSort xs).length = xs.length induction xs with ⊢ (insertionSort []).length = [].length All goals completed! 🐙 x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ (insertionSort (x :: xs)).length = (x :: xs).length All goals completed! 🐙 example (xs : List Nat) : (insertionSort xs).length = xs.length := (insertionSort_perm xs).length_eq
연습문제 8.6.7 정렬 전후의 원소 포함 관계

insertionSort_perm xs에 .mem_iff를 적용하면 주어진 방향의 동치를 얻는다. [1, 1, 2]와 [1, 2, 2]의 길이는 모두 3이고 두 리스트 모두 1과 2를 포함한다. 1이 나타나는 횟수가 다르므로 두 리스트는 순열 관계에 있지 않다.

example (a : Nat) (xs : List Nat) : a ∈ insertionSort xs ↔ a ∈ xs := (insertionSort_perm xs).mem_iff
연습문제 8.6.8 삽입 정렬의 멱등성

입력이 이미 정렬되어 있으면 꼬리도 정렬되어 있다. 꼬리가 y :: ys인 경우 ih htail로 정렬한 꼬리를 원래 꼬리로 되돌린다. 머리 x는 그 꼬리의 모든 원소 이하이므로 특히 x ≤ y가 성립한다. 따라서 삽입은 맨 앞에서 끝난다. 이 보조 정리에 insertionSort_sorted xs를 넘기면 정렬을 한 번 더 해도 값이 같다.

theorem insertionSort_eq_self (xs : List Nat) (h : Sorted xs) : insertionSort xs = xs := xs:List Nath:Sorted xs⊢ insertionSort xs = xs induction xs with h:Sorted []⊢ insertionSort [] = [] All goals completed! 🐙 x:Natxs:List Natih:Sorted xs → insertionSort xs = xsh:Sorted (x :: xs)⊢ insertionSort (x :: xs) = x :: xs cases xs with x:Natih:Sorted [] → insertionSort [] = []h:Sorted [x]⊢ insertionSort [x] = [x] All goals completed! 🐙 x:Naty:Natys:List Natih:Sorted (y :: ys) → insertionSort (y :: ys) = y :: ysh:Sorted (x :: y :: ys)⊢ insertionSort (x :: y :: ys) = x :: y :: ys x:Naty:Natys:List Natih:Sorted (y :: ys) → insertionSort (y :: ys) = y :: ysh:Sorted (x :: y :: ys)hhead:∀ (a' : Nat), a' ∈ y :: ys → x ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) (y :: ys)⊢ insertionSort (x :: y :: ys) = x :: y :: ys x:Naty:Natys:List Natih:Sorted (y :: ys) → insertionSort (y :: ys) = y :: ysh:Sorted (x :: y :: ys)hhead:∀ (a' : Nat), a' ∈ y :: ys → x ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) (y :: ys)hxy:x ≤ y⊢ insertionSort (x :: y :: ys) = x :: y :: ys All goals completed! 🐙 theorem insertionSort_idempotent (xs : List Nat) : insertionSort (insertionSort xs) = insertionSort xs := insertionSort_eq_self (insertionSort xs) (insertionSort_sorted xs)
연습문제 8.6.9 내림차순 삽입 정렬 검증

y ≤ x이면 새 값 x를 머리 y 앞에 끼워 넣는다. 분기 조건이 바뀌어도 리스트를 만드는 방법은 같으므로 순열 증명에는 머리를 붙이고 교환하는 방법을 그대로 쓴다.

def SortedDescending (xs : List Nat) : Prop := xs.Pairwise (· ≥ ·) def insertNatDescending (x : Nat) : List Nat → List Nat | [] => [x] | y :: ys => if y ≤ x then x :: y :: ys else y :: insertNatDescending x ys def insertionSortDescending : List Nat → List Nat | [] => [] | x :: xs => insertNatDescending x (insertionSortDescending xs) theorem insertDescending_perm (x : Nat) (xs : List Nat) : (insertNatDescending x xs).Perm (x :: xs) := x:Natxs:List Nat⊢ (insertNatDescending x xs).Perm (x :: xs) induction xs with x:Nat⊢ (insertNatDescending x []).Perm [x] All goals completed! 🐙 x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys) x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys) x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys) All goals completed! 🐙 x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys) x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (y :: insertNatDescending x ys).Perm (x :: y :: ys) All goals completed! 🐙

머리 y를 그대로 두는 분기에서는 새 꼬리의 원소가 모두 y 이하여야 한다. 순열 관계로 포함 여부를 옮겨 얻는 상한 보존 정리와, 꼬리에 대한 귀납 가정을 결합한다. 새 값 x를 맨 앞에 붙이는 분기에서는 꼬리의 값 z에 대해 z ≤ y ≤ x를 적용한다.

theorem insertDescending_preserves_upperBound (a x : Nat) (xs : List Nat) (hx : x ≤ a) (hxs : ∀ y, y ∈ xs → y ≤ a) : ∀ y, y ∈ insertNatDescending x xs → y ≤ a := a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ a⊢ ∀ (y : Nat), y ∈ insertNatDescending x xs → y ≤ a a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xs⊢ y ≤ a a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xs⊢ y ≤ a a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ y ≤ aa:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshmem:y ∈ xs⊢ y ≤ a a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ y ≤ a a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ x ≤ a All goals completed! 🐙 a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshmem:y ∈ xs⊢ y ≤ a All goals completed! 🐙 theorem insertDescending_sorted (x : Nat) (xs : List Nat) (h : SortedDescending xs) : SortedDescending (insertNatDescending x xs) := x:Natxs:List Nath:SortedDescending xs⊢ SortedDescending (insertNatDescending x xs) induction xs with x:Nath:SortedDescending []⊢ SortedDescending (insertNatDescending x []) All goals completed! 🐙 x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)⊢ SortedDescending (insertNatDescending x (y :: ys)) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) ys⊢ SortedDescending (insertNatDescending x (y :: ys)) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ x⊢ SortedDescending (insertNatDescending x (y :: ys))x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:¬y ≤ x⊢ SortedDescending (insertNatDescending x (y :: ys)) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ x⊢ SortedDescending (insertNatDescending x (y :: ys)) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ x⊢ SortedDescending (x :: y :: ys) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ x⊢ ∀ (a' : Nat), a' ∈ y :: ys → x ≥ a' x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: ys⊢ x ≥ z x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≥ zx:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: yshmem:z ∈ ys⊢ x ≥ z x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≥ z x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≥ y All goals completed! 🐙 x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:y ≤ xz:Nathz:z ∈ y :: yshmem:z ∈ ys⊢ x ≥ z All goals completed! 🐙 x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:¬y ≤ x⊢ SortedDescending (insertNatDescending x (y :: ys)) x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≥ a'htail:List.Pairwise (fun x1 x2 => x1 ≥ x2) yshyx:¬y ≤ x⊢ SortedDescending (y :: insertNatDescending x ys) All goals completed! 🐙

전체 정렬의 귀납 단계에서는 정렬한 꼬리에 삽입의 보조 정리를 적용한다. 마지막 정리에서 정렬과 순열의 증명을 묶는다.

theorem insertionSortDescending_sorted (xs : List Nat) : SortedDescending (insertionSortDescending xs) := xs:List Nat⊢ SortedDescending (insertionSortDescending xs) induction xs with ⊢ SortedDescending (insertionSortDescending []) All goals completed! 🐙 x:Natxs:List Natih:SortedDescending (insertionSortDescending xs)⊢ SortedDescending (insertionSortDescending (x :: xs)) x:Natxs:List Natih:SortedDescending (insertionSortDescending xs)⊢ SortedDescending (insertNatDescending x (insertionSortDescending xs)) All goals completed! 🐙 theorem insertionSortDescending_perm (xs : List Nat) : (insertionSortDescending xs).Perm xs := xs:List Nat⊢ (insertionSortDescending xs).Perm xs induction xs with ⊢ (insertionSortDescending []).Perm [] All goals completed! 🐙 x:Natxs:List Natih:(insertionSortDescending xs).Perm xs⊢ (insertionSortDescending (x :: xs)).Perm (x :: xs) x:Natxs:List Natih:(insertionSortDescending xs).Perm xs⊢ (insertNatDescending x (insertionSortDescending xs)).Perm (x :: xs) x:Natxs:List Natih:(insertionSortDescending xs).Perm xshi:(insertNatDescending x (insertionSortDescending xs)).Perm (x :: insertionSortDescending xs)⊢ (insertNatDescending x (insertionSortDescending xs)).Perm (x :: xs) All goals completed! 🐙 theorem insertionSortDescending_correct (xs : List Nat) : SortedDescending (insertionSortDescending xs) ∧ xs.Perm (insertionSortDescending xs) := xs:List Nat⊢ SortedDescending (insertionSortDescending xs) ∧ xs.Perm (insertionSortDescending xs) xs:List Nat⊢ SortedDescending (insertionSortDescending xs)xs:List Nat⊢ xs.Perm (insertionSortDescending xs) xs:List Nat⊢ SortedDescending (insertionSortDescending xs) All goals completed! 🐙 xs:List Nat⊢ xs.Perm (insertionSortDescending xs) All goals completed! 🐙
연습문제 8.7.1 불리언 식을 구문 트리로 표현하기

전체 식의 뿌리는 논리곱이다. 왼쪽은 p에 부정을 적용한 식이고, 오른쪽은 q와 false의 논리합이다.

def practiceExpr : BExpr := .conj (.neg (.var 0)) (.disj (.var 1) (.lit false)) BooleanSimplification.BExpr.conj (BooleanSimplification.BExpr.neg (BooleanSimplification.BExpr.var 0)) (BooleanSimplification.BExpr.disj (BooleanSimplification.BExpr.var 1) (BooleanSimplification.BExpr.lit false))#eval practiceExpr
연습문제 8.7.2 변수 할당을 바꾸어 식 계산하기

q만 참이면 왼쪽의 ¬p와 오른쪽의 q ∨ false가 둘 다 참이다. p만 참이면 두 하위 식이 모두 거짓이므로 논리곱의 값도 거짓이다.

true#eval eval (fun n => n == 1) practiceExpr false#eval eval sampleEnv practiceExpr
연습문제 8.7.3 드모르간 법칙을 의미적 동등성으로 증명하기

SemEquiv를 증명하려면 임의의 환경 env를 받는다. cases h₀ : env 0과 cases h₁ : env 1로 두 변수의 값 네 조합을 만든다. 두 식이 참과 거짓을 계산하는 데 필요한 정보가 이 두 등식에 모두 들어 있다.

def deMorganLeft : BExpr := .neg (.conj (.var 0) (.var 1)) def deMorganRight : BExpr := .disj (.neg (.var 0)) (.neg (.var 1)) theorem deMorgan_correct : SemEquiv deMorganLeft deMorganRight := ⊢ SemEquiv deMorganLeft deMorganRight env:Env⊢ eval env deMorganLeft = eval env deMorganRight env:Envh₀:env 0 = false⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = true⊢ eval env deMorganLeft = eval env deMorganRight env:Envh₀:env 0 = false⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = true⊢ eval env deMorganLeft = eval env deMorganRight env:Envh₀:env 0 = trueh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = trueh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRight env:Envh₀:env 0 = falseh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = falseh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = trueh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRightenv:Envh₀:env 0 = trueh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRight All goals completed! 🐙
연습문제 8.7.4 논리합 단순화의 의미 보존

어느 한쪽이 true인 분기에서는 양쪽 값이 모두 true다. 그 밖에 false와 결합하면 다른 쪽의 값이 남는다. 마지막 분기는 원래 논리합을 그대로 만들기 때문에 eval의 정의로 닫힌다.

example (env : Env) (a b : BExpr) : eval env (mkDisj a b) = (eval env a || eval env b) := env:Enva:BExprb:BExpr⊢ eval env (mkDisj a b) = (eval env a || eval env b) env:Enva:BExprb:BExpr⊢ eval env (match a, b with | BExpr.lit true, x => BExpr.lit true | x, BExpr.lit true => BExpr.lit true | BExpr.lit false, e => e | e, BExpr.lit false => e | a, b => a.disj b) = (eval env a || eval env b) env:Envb:BExprx✝¹:BExprx✝:BExpr⊢ eval env (BExpr.lit true) = (eval env (BExpr.lit true) || eval env b)env:Enva:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ eval env (BExpr.lit true) = (eval env a || eval env (BExpr.lit true))env:Envb:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ eval env b = (eval env (BExpr.lit false) || eval env b)env:Enva:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ eval env a = (eval env a || eval env (BExpr.lit false))env:Enva:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit true → Falsex✝²:a = BExpr.lit false → Falsex✝¹:b = BExpr.lit true → Falsex✝:b = BExpr.lit false → False⊢ eval env (a.disj b) = (eval env a || eval env b) env:Envb:BExprx✝¹:BExprx✝:BExpr⊢ eval env (BExpr.lit true) = (eval env (BExpr.lit true) || eval env b)env:Enva:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ eval env (BExpr.lit true) = (eval env a || eval env (BExpr.lit true))env:Envb:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ eval env b = (eval env (BExpr.lit false) || eval env b)env:Enva:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ eval env a = (eval env a || eval env (BExpr.lit false))env:Enva:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit true → Falsex✝²:a = BExpr.lit false → Falsex✝¹:b = BExpr.lit true → Falsex✝:b = BExpr.lit false → False⊢ eval env (a.disj b) = (eval env a || eval env b) All goals completed! 🐙
연습문제 8.7.5 부정 단순화의 의미 보존

리터럴에서는 mkNeg가 불리언 값을 직접 뒤집는다. 이중 부정에서는 오른쪽이 !(!(eval env e))로 펼쳐진다. simp는 두 번 부정한 불리언 값이 원래 값과 같다는 정리를 적용한다. 나머지 분기에서는 mkNeg가 입력을 .neg로 감싼다.

example (env : Env) (e : BExpr) : eval env (mkNeg e) = !(eval env e) := env:Enve:BExpr⊢ eval env (mkNeg e) = !eval env e env:Enva✝:Bool⊢ eval env (mkNeg (BExpr.lit a✝)) = !eval env (BExpr.lit a✝)env:Enva✝:Nat⊢ eval env (mkNeg (BExpr.var a✝)) = !eval env (BExpr.var a✝)env:Enva✝:BExpr⊢ eval env (mkNeg a✝.neg) = !eval env a✝.negenv:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.conj a✝)) = !eval env (a✝¹.conj a✝)env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.disj a✝)) = !eval env (a✝¹.disj a✝) env:Enva✝:Bool⊢ eval env (mkNeg (BExpr.lit a✝)) = !eval env (BExpr.lit a✝)env:Enva✝:Nat⊢ eval env (mkNeg (BExpr.var a✝)) = !eval env (BExpr.var a✝)env:Enva✝:BExpr⊢ eval env (mkNeg a✝.neg) = !eval env a✝.negenv:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.conj a✝)) = !eval env (a✝¹.conj a✝)env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.disj a✝)) = !eval env (a✝¹.disj a✝) All goals completed! 🐙
연습문제 8.7.6 단순화에서 식의 크기 비증가

각 보조 함수는 연산 노드를 그대로 만들거나, 하위 식 하나 또는 리터럴 하나를 반환한다. 예를 들어 mkConj가 오른쪽 식을 반환하는 분기에서는 그 크기가 1 + exprSize a + exprSize b 이하다. 분기마다 simp로 노드 수를 펼치고 omega로 남은 자연수 부등식을 증명한다.

theorem mkNeg_size_le (e : BExpr) : exprSize (mkNeg e) ≤ 1 + exprSize e := e:BExpr⊢ exprSize (mkNeg e) ≤ 1 + exprSize e e:BExpr⊢ exprSize (match e with | BExpr.lit b => BExpr.lit !b | e.neg => e | e => e.neg) ≤ 1 + exprSize e x✝:BExprb✝:Bool⊢ exprSize (BExpr.lit !b✝) ≤ 1 + exprSize (BExpr.lit b✝)x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + exprSize e✝.nege:BExprx✝²:BExprx✝¹:∀ (b : Bool), e = BExpr.lit b → Falsex✝:∀ (e_1 : BExpr), e = e_1.neg → False⊢ exprSize e.neg ≤ 1 + exprSize e x✝:BExprb✝:Bool⊢ exprSize (BExpr.lit !b✝) ≤ 1 + exprSize (BExpr.lit b✝)x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + exprSize e✝.nege:BExprx✝²:BExprx✝¹:∀ (b : Bool), e = BExpr.lit b → Falsex✝:∀ (e_1 : BExpr), e = e_1.neg → False⊢ exprSize e.neg ≤ 1 + exprSize e All goals completed! 🐙 x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + (1 + exprSize e✝) All goals completed! 🐙 theorem mkConj_size_le (a b : BExpr) : exprSize (mkConj a b) ≤ 1 + exprSize a + exprSize b := a:BExprb:BExpr⊢ exprSize (mkConj a b) ≤ 1 + exprSize a + exprSize b a:BExprb:BExpr⊢ exprSize (match a, b with | BExpr.lit false, x => BExpr.lit false | x, BExpr.lit false => BExpr.lit false | BExpr.lit true, e => e | e, BExpr.lit true => e | a, b => a.conj b) ≤ 1 + exprSize a + exprSize b b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize (BExpr.lit false) + exprSize ba:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit false → False⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize a + exprSize (BExpr.lit false)b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit false → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit true) + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit true)a:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit false → Falsex✝²:a = BExpr.lit true → Falsex✝¹:b = BExpr.lit false → Falsex✝:b = BExpr.lit true → False⊢ exprSize (a.conj b) ≤ 1 + exprSize a + exprSize b b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize (BExpr.lit false) + exprSize ba:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit false → False⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize a + exprSize (BExpr.lit false)b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit false → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit true) + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit true)a:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit false → Falsex✝²:a = BExpr.lit true → Falsex✝¹:b = BExpr.lit false → Falsex✝:b = BExpr.lit true → False⊢ exprSize (a.conj b) ≤ 1 + exprSize a + exprSize b All goals completed! 🐙 b:BExprx✝¹:BExprx✝:BExpr⊢ 1 ≤ 2 + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + 1 All goals completed! 🐙 theorem mkDisj_size_le (a b : BExpr) : exprSize (mkDisj a b) ≤ 1 + exprSize a + exprSize b := a:BExprb:BExpr⊢ exprSize (mkDisj a b) ≤ 1 + exprSize a + exprSize b a:BExprb:BExpr⊢ exprSize (match a, b with | BExpr.lit true, x => BExpr.lit true | x, BExpr.lit true => BExpr.lit true | BExpr.lit false, e => e | e, BExpr.lit false => e | a, b => a.disj b) ≤ 1 + exprSize a + exprSize b b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize (BExpr.lit true) + exprSize ba:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize a + exprSize (BExpr.lit true)b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit false) + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit false)a:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit true → Falsex✝²:a = BExpr.lit false → Falsex✝¹:b = BExpr.lit true → Falsex✝:b = BExpr.lit false → False⊢ exprSize (a.disj b) ≤ 1 + exprSize a + exprSize b b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize (BExpr.lit true) + exprSize ba:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize a + exprSize (BExpr.lit true)b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit false) + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit false)a:BExprb:BExprx✝⁵:BExprx✝⁴:BExprx✝³:a = BExpr.lit true → Falsex✝²:a = BExpr.lit false → Falsex✝¹:b = BExpr.lit true → Falsex✝:b = BExpr.lit false → False⊢ exprSize (a.disj b) ≤ 1 + exprSize a + exprSize b All goals completed! 🐙 b:BExprx✝¹:BExprx✝:BExpr⊢ 1 ≤ 2 + exprSize ba:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + 1 All goals completed! 🐙

전체 식의 귀납 단계에서는 먼저 단순화한 하위 식에 보조 정리를 적용한다. 예를 들어 논리곱에서 그 정리는 결과의 크기가 1 + exprSize (simplify a) + exprSize (simplify b) 이하라고 보장한다. 귀납 가정은 각 항이 원래 하위 식의 크기 이하임을 주므로, 두 종류의 부등식을 합치면 된다.

theorem simplify_size_le (e : BExpr) : exprSize (simplify e) ≤ exprSize e := e:BExpr⊢ exprSize (simplify e) ≤ exprSize e induction e with b:Bool⊢ exprSize (simplify (BExpr.lit b)) ≤ exprSize (BExpr.lit b) All goals completed! 🐙 n:Nat⊢ exprSize (simplify (BExpr.var n)) ≤ exprSize (BExpr.var n) All goals completed! 🐙 e:BExprih:exprSize (simplify e) ≤ exprSize e⊢ exprSize (simplify e.neg) ≤ exprSize e.neg e:BExprih:exprSize (simplify e) ≤ exprSize eh:exprSize (mkNeg (simplify e)) ≤ 1 + exprSize (simplify e)⊢ exprSize (simplify e.neg) ≤ exprSize e.neg e:BExprih:exprSize (simplify e) ≤ exprSize eh:exprSize (mkNeg (simplify e)) ≤ 1 + exprSize (simplify e)⊢ exprSize (mkNeg (simplify e)) ≤ 1 + exprSize e All goals completed! 🐙 a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize b⊢ exprSize (simplify (a.conj b)) ≤ exprSize (a.conj b) a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize bh:exprSize (mkConj (simplify a) (simplify b)) ≤ 1 + exprSize (simplify a) + exprSize (simplify b)⊢ exprSize (simplify (a.conj b)) ≤ exprSize (a.conj b) a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize bh:exprSize (mkConj (simplify a) (simplify b)) ≤ 1 + exprSize (simplify a) + exprSize (simplify b)⊢ exprSize (mkConj (simplify a) (simplify b)) ≤ 1 + exprSize a + exprSize b All goals completed! 🐙 a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize b⊢ exprSize (simplify (a.disj b)) ≤ exprSize (a.disj b) a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize bh:exprSize (mkDisj (simplify a) (simplify b)) ≤ 1 + exprSize (simplify a) + exprSize (simplify b)⊢ exprSize (simplify (a.disj b)) ≤ exprSize (a.disj b) a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize bh:exprSize (mkDisj (simplify a) (simplify b)) ≤ 1 + exprSize (simplify a) + exprSize (simplify b)⊢ exprSize (mkDisj (simplify a) (simplify b)) ≤ 1 + exprSize a + exprSize b All goals completed! 🐙
연습문제 8.7.7 단순화 프로그램의 멱등성

NotLit는 리터럴이 아닌 노드에서 성립하고, CanKeepNeg는 부정을 그대로 붙일 수 있는 노드에서 성립한다. Reduced는 현재 노드와 모든 하위 식에서 단순화 규칙을 더 적용할 수 없는 경우를 표시한다. 논리곱과 논리합은 두 하위 식이 모두 이 조건을 만족하고 두 식 모두 리터럴이 아닐 때 그대로 남는다.

def NotLit : BExpr → Prop | .lit _ => False | _ => True def CanKeepNeg : BExpr → Prop | .lit _ | .neg _ => False | _ => True def Reduced : BExpr → Prop | .lit _ | .var _ => True | .neg e => Reduced e ∧ CanKeepNeg e | .conj a b | .disj a b => Reduced a ∧ Reduced b ∧ NotLit a ∧ NotLit b

reduced_mkNeg에서 cases e로 나눈 neg 분기를 살펴보자. 첫 change 앞에 커서를 두면 가정은 h : Reduced (.neg e), 목표는 Reduced (mkNeg (.neg e))다. 가정을 펼치면 Reduced e ∧ CanKeepNeg e이고, mkNeg는 이중 부정을 없애므로 목표는 Reduced e로 바뀐다. 가정의 첫 성분 h.1로 목표를 닫는다.

나머지 분기에서는 simp_all로 가정과 목표를 함께 단순화한다. 논리곱과 논리합에서도 두 하위 식의 모양에 따라 같은 방식으로 조건을 검사한다. 이 사실들을 simplify의 재귀 구조에 따라 결합하면 단순화한 식 전체가 Reduced이다.

theorem reduced_mkNeg (e : BExpr) (h : Reduced e) : Reduced (mkNeg e) := e:BExprh:Reduced e⊢ Reduced (mkNeg e) a✝:Boolh:Reduced (BExpr.lit a✝)⊢ Reduced (mkNeg (BExpr.lit a✝))a✝:Nath:Reduced (BExpr.var a✝)⊢ Reduced (mkNeg (BExpr.var a✝))a✝:BExprh:Reduced a✝.neg⊢ Reduced (mkNeg a✝.neg)a✝¹:BExpra✝:BExprh:Reduced (a✝¹.conj a✝)⊢ Reduced (mkNeg (a✝¹.conj a✝))a✝¹:BExpra✝:BExprh:Reduced (a✝¹.disj a✝)⊢ Reduced (mkNeg (a✝¹.disj a✝)) case neg e e:BExprh:Reduced e.neg⊢ Reduced (mkNeg e.neg) e:BExprh:Reduced e ∧ CanKeepNeg e⊢ Reduced (mkNeg e.neg) e:BExprh:Reduced e ∧ CanKeepNeg e⊢ Reduced e All goals completed! 🐙 all_goals All goals completed! 🐙 theorem reduced_mkConj (a b : BExpr) (ha : Reduced a) (hb : Reduced b) : Reduced (mkConj a b) := a:BExprb:BExprha:Reduced ahb:Reduced b⊢ Reduced (mkConj a b) b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝) b)b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝) b)b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkConj a✝.neg b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝¹.conj a✝) b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝¹.disj a✝) b) b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝) b)b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝) b)b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkConj a✝.neg b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝¹.conj a✝) b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝¹.disj a✝) b) a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².disj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.disj a✝)) a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝¹) (BExpr.lit a✝))a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.lit a✝¹) (BExpr.var a✝))a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (BExpr.lit a✝¹) a✝.neg)a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (BExpr.lit a✝²) (a✝¹.conj a✝))a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (BExpr.lit a✝²) (a✝¹.disj a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.var a✝¹) (BExpr.lit a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝¹) (BExpr.var a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (BExpr.var a✝¹) a✝.neg)a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (BExpr.var a✝²) (a✝¹.conj a✝))a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (BExpr.var a✝²) (a✝¹.disj a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj a✝¹.neg (BExpr.lit a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj a✝¹.neg (BExpr.var a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj a✝¹.neg a✝.neg)a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj a✝².neg (a✝¹.conj a✝))a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj a✝².neg (a✝¹.disj a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².conj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².conj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².conj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.conj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.conj a✝²) (a✝¹.disj a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².disj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.disj a✝)) All goals completed! 🐙 all_goals a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝¹:BExprx✝:BExprheq✝:a✝².disj a✝¹ = BExpr.lit false⊢ Reduced (BExpr.lit false)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:BExpr.lit a✝ = BExpr.lit falsex✝:a✝².disj a✝¹ = BExpr.lit false → False⊢ Reduced (BExpr.lit false)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:a✝².disj a✝¹ = BExpr.lit truex✝:BExpr.lit a✝ = BExpr.lit false → False⊢ Reduced (BExpr.lit a✝)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝³:BExprx✝²:BExprheq✝:BExpr.lit a✝ = BExpr.lit truex✝¹:a✝².disj a✝¹ = BExpr.lit false → Falsex✝:a✝².disj a✝¹ = BExpr.lit true → False⊢ Reduced (a✝².disj a✝¹)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝⁵:BExprx✝⁴:BExprx✝³:a✝².disj a✝¹ = BExpr.lit false → Falsex✝²:a✝².disj a✝¹ = BExpr.lit true → Falsex✝¹:BExpr.lit a✝ = BExpr.lit false → Falsex✝:BExpr.lit a✝ = BExpr.lit true → False⊢ Reduced ((a✝².disj a✝¹).conj (BExpr.lit a✝)) a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝¹:BExprx✝:BExprheq✝:a✝².disj a✝¹ = BExpr.lit false⊢ Reduced (BExpr.lit false)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:BExpr.lit a✝ = BExpr.lit falsex✝:a✝².disj a✝¹ = BExpr.lit false → False⊢ Reduced (BExpr.lit false)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:a✝².disj a✝¹ = BExpr.lit truex✝:BExpr.lit a✝ = BExpr.lit false → False⊢ Reduced (BExpr.lit a✝)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝³:BExprx✝²:BExprheq✝:BExpr.lit a✝ = BExpr.lit truex✝¹:a✝².disj a✝¹ = BExpr.lit false → Falsex✝:a✝².disj a✝¹ = BExpr.lit true → False⊢ Reduced (a✝².disj a✝¹)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝⁵:BExprx✝⁴:BExprx✝³:a✝².disj a✝¹ = BExpr.lit false → Falsex✝²:a✝².disj a✝¹ = BExpr.lit true → Falsex✝¹:BExpr.lit a✝ = BExpr.lit false → Falsex✝:BExpr.lit a✝ = BExpr.lit true → False⊢ Reduced ((a✝².disj a✝¹).conj (BExpr.lit a✝)) All goals completed! 🐙 theorem reduced_mkDisj (a b : BExpr) (ha : Reduced a) (hb : Reduced b) : Reduced (mkDisj a b) := a:BExprb:BExprha:Reduced ahb:Reduced b⊢ Reduced (mkDisj a b) b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝) b)b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝) b)b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkDisj a✝.neg b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝¹.conj a✝) b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝¹.disj a✝) b) b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝) b)b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝) b)b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkDisj a✝.neg b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝¹.conj a✝) b)b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝¹.disj a✝) b) a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².disj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝)) a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝¹) (BExpr.lit a✝))a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.lit a✝¹) (BExpr.var a✝))a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (BExpr.lit a✝¹) a✝.neg)a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (BExpr.lit a✝²) (a✝¹.conj a✝))a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (BExpr.lit a✝²) (a✝¹.disj a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.var a✝¹) (BExpr.lit a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝¹) (BExpr.var a✝))a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (BExpr.var a✝¹) a✝.neg)a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (BExpr.var a✝²) (a✝¹.conj a✝))a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (BExpr.var a✝²) (a✝¹.disj a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj a✝¹.neg (BExpr.lit a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj a✝¹.neg (BExpr.var a✝))a✝¹:BExprha:Reduced a✝¹.nega✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj a✝¹.neg a✝.neg)a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj a✝².neg (a✝¹.conj a✝))a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj a✝².neg (a✝¹.disj a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².conj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².conj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².conj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.conj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.conj a✝²) (a✝¹.disj a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.lit a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.var a✝))a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².disj a✝¹) a✝.neg)a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝))a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝)) All goals completed! 🐙 all_goals a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝¹:BExprx✝:BExprheq✝:a✝².disj a✝¹ = BExpr.lit true⊢ Reduced (BExpr.lit true)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:BExpr.lit a✝ = BExpr.lit truex✝:a✝².disj a✝¹ = BExpr.lit true → False⊢ Reduced (BExpr.lit true)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:a✝².disj a✝¹ = BExpr.lit falsex✝:BExpr.lit a✝ = BExpr.lit true → False⊢ Reduced (BExpr.lit a✝)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝³:BExprx✝²:BExprheq✝:BExpr.lit a✝ = BExpr.lit falsex✝¹:a✝².disj a✝¹ = BExpr.lit true → Falsex✝:a✝².disj a✝¹ = BExpr.lit false → False⊢ Reduced (a✝².disj a✝¹)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝⁵:BExprx✝⁴:BExprx✝³:a✝².disj a✝¹ = BExpr.lit true → Falsex✝²:a✝².disj a✝¹ = BExpr.lit false → Falsex✝¹:BExpr.lit a✝ = BExpr.lit true → Falsex✝:BExpr.lit a✝ = BExpr.lit false → False⊢ Reduced ((a✝².disj a✝¹).disj (BExpr.lit a✝)) a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝¹:BExprx✝:BExprheq✝:a✝².disj a✝¹ = BExpr.lit true⊢ Reduced (BExpr.lit true)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:BExpr.lit a✝ = BExpr.lit truex✝:a✝².disj a✝¹ = BExpr.lit true → False⊢ Reduced (BExpr.lit true)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝²:BExprx✝¹:BExprheq✝:a✝².disj a✝¹ = BExpr.lit falsex✝:BExpr.lit a✝ = BExpr.lit true → False⊢ Reduced (BExpr.lit a✝)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝³:BExprx✝²:BExprheq✝:BExpr.lit a✝ = BExpr.lit falsex✝¹:a✝².disj a✝¹ = BExpr.lit true → Falsex✝:a✝².disj a✝¹ = BExpr.lit false → False⊢ Reduced (a✝².disj a✝¹)a✝²:BExpra✝¹:BExpra✝:Boolha:Reduced a✝² ∧ Reduced a✝¹ ∧ (match a✝² with | BExpr.lit a => False | x => True) ∧ match a✝¹ with | BExpr.lit a => False | x => Truex✝⁵:BExprx✝⁴:BExprx✝³:a✝².disj a✝¹ = BExpr.lit true → Falsex✝²:a✝².disj a✝¹ = BExpr.lit false → Falsex✝¹:BExpr.lit a✝ = BExpr.lit true → Falsex✝:BExpr.lit a✝ = BExpr.lit false → False⊢ Reduced ((a✝².disj a✝¹).disj (BExpr.lit a✝)) All goals completed! 🐙 theorem simplify_reduced (e : BExpr) : Reduced (simplify e) := e:BExpr⊢ Reduced (simplify e) induction e with b:Bool⊢ Reduced (simplify (BExpr.lit b)) All goals completed! 🐙 n:Nat⊢ Reduced (simplify (BExpr.var n)) All goals completed! 🐙 e:BExprih:Reduced (simplify e)⊢ Reduced (simplify e.neg) All goals completed! 🐙 a:BExprb:BExpriha:Reduced (simplify a)ihb:Reduced (simplify b)⊢ Reduced (simplify (a.conj b)) All goals completed! 🐙 a:BExprb:BExpriha:Reduced (simplify a)ihb:Reduced (simplify b)⊢ Reduced (simplify (a.disj b)) All goals completed! 🐙

이제 Reduced인 식을 다시 단순화한다. 하위 식은 각각의 귀납 가정으로 원래대로 돌아온다. 보조 함수가 연산 노드를 그대로 만드는 세 정리를 적용하면 전체 식도 그대로다. e를 한 번 단순화하면 이미 Reduced이므로, 그 결과에 이 고정점 성질을 적용한다.

theorem mkNeg_of_canKeepNeg (e : BExpr) (h : CanKeepNeg e) : mkNeg e = .neg e := e:BExprh:CanKeepNeg e⊢ mkNeg e = e.neg a✝:Boolh:CanKeepNeg (BExpr.lit a✝)⊢ mkNeg (BExpr.lit a✝) = (BExpr.lit a✝).nega✝:Nath:CanKeepNeg (BExpr.var a✝)⊢ mkNeg (BExpr.var a✝) = (BExpr.var a✝).nega✝:BExprh:CanKeepNeg a✝.neg⊢ mkNeg a✝.neg = a✝.neg.nega✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.conj a✝)⊢ mkNeg (a✝¹.conj a✝) = (a✝¹.conj a✝).nega✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.disj a✝)⊢ mkNeg (a✝¹.disj a✝) = (a✝¹.disj a✝).neg a✝:Boolh:CanKeepNeg (BExpr.lit a✝)⊢ mkNeg (BExpr.lit a✝) = (BExpr.lit a✝).nega✝:Nath:CanKeepNeg (BExpr.var a✝)⊢ mkNeg (BExpr.var a✝) = (BExpr.var a✝).nega✝:BExprh:CanKeepNeg a✝.neg⊢ mkNeg a✝.neg = a✝.neg.nega✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.conj a✝)⊢ mkNeg (a✝¹.conj a✝) = (a✝¹.conj a✝).nega✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.disj a✝)⊢ mkNeg (a✝¹.disj a✝) = (a✝¹.disj a✝).neg All goals completed! 🐙 theorem mkConj_of_notLit (a b : BExpr) (ha : NotLit a) (hb : NotLit b) : mkConj a b = .conj a b := a:BExprb:BExprha:NotLit ahb:NotLit b⊢ mkConj a b = a.conj b b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.lit a✝) b = (BExpr.lit a✝).conj bb:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.var a✝) b = (BExpr.var a✝).conj bb:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkConj a✝.neg b = a✝.neg.conj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝¹.conj a✝) b = (a✝¹.conj a✝).conj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝¹.disj a✝) b = (a✝¹.disj a✝).conj b b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.lit a✝) b = (BExpr.lit a✝).conj bb:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.var a✝) b = (BExpr.var a✝).conj bb:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkConj a✝.neg b = a✝.neg.conj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝¹.conj a✝) b = (a✝¹.conj a✝).conj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝¹.disj a✝) b = (a✝¹.disj a✝).conj b a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj (a✝².disj a✝¹) (BExpr.lit a✝) = (a✝².disj a✝¹).conj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj (a✝².disj a✝¹) (BExpr.var a✝) = (a✝².disj a✝¹).conj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).conj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝³.disj a✝²) (a✝¹.conj a✝) = (a✝³.disj a✝²).conj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝³.disj a✝²) (a✝¹.disj a✝) = (a✝³.disj a✝²).conj (a✝¹.disj a✝) a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.lit a✝¹) (BExpr.lit a✝) = (BExpr.lit a✝¹).conj (BExpr.lit a✝)a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.lit a✝¹) (BExpr.var a✝) = (BExpr.lit a✝¹).conj (BExpr.var a✝)a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (BExpr.lit a✝¹) a✝.neg = (BExpr.lit a✝¹).conj a✝.nega✝²:Boolha:NotLit (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj (BExpr.lit a✝²) (a✝¹.conj a✝) = (BExpr.lit a✝²).conj (a✝¹.conj a✝)a✝²:Boolha:NotLit (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj (BExpr.lit a✝²) (a✝¹.disj a✝) = (BExpr.lit a✝²).conj (a✝¹.disj a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.var a✝¹) (BExpr.lit a✝) = (BExpr.var a✝¹).conj (BExpr.lit a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.var a✝¹) (BExpr.var a✝) = (BExpr.var a✝¹).conj (BExpr.var a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (BExpr.var a✝¹) a✝.neg = (BExpr.var a✝¹).conj a✝.nega✝²:Natha:NotLit (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj (BExpr.var a✝²) (a✝¹.conj a✝) = (BExpr.var a✝²).conj (a✝¹.conj a✝)a✝²:Natha:NotLit (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj (BExpr.var a✝²) (a✝¹.disj a✝) = (BExpr.var a✝²).conj (a✝¹.disj a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj a✝¹.neg (BExpr.lit a✝) = a✝¹.neg.conj (BExpr.lit a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj a✝¹.neg (BExpr.var a✝) = a✝¹.neg.conj (BExpr.var a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:BExprhb:NotLit a✝.neg⊢ mkConj a✝¹.neg a✝.neg = a✝¹.neg.conj a✝.nega✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj a✝².neg (a✝¹.conj a✝) = a✝².neg.conj (a✝¹.conj a✝)a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj a✝².neg (a✝¹.disj a✝) = a✝².neg.conj (a✝¹.disj a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj (a✝².conj a✝¹) (BExpr.lit a✝) = (a✝².conj a✝¹).conj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj (a✝².conj a✝¹) (BExpr.var a✝) = (a✝².conj a✝¹).conj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².conj a✝¹) a✝.neg = (a✝².conj a✝¹).conj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝³.conj a✝²) (a✝¹.conj a✝) = (a✝³.conj a✝²).conj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝³.conj a✝²) (a✝¹.disj a✝) = (a✝³.conj a✝²).conj (a✝¹.disj a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj (a✝².disj a✝¹) (BExpr.lit a✝) = (a✝².disj a✝¹).conj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj (a✝².disj a✝¹) (BExpr.var a✝) = (a✝².disj a✝¹).conj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).conj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝³.disj a✝²) (a✝¹.conj a✝) = (a✝³.disj a✝²).conj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝³.disj a✝²) (a✝¹.disj a✝) = (a✝³.disj a✝²).conj (a✝¹.disj a✝) All goals completed! 🐙 theorem mkDisj_of_notLit (a b : BExpr) (ha : NotLit a) (hb : NotLit b) : mkDisj a b = .disj a b := a:BExprb:BExprha:NotLit ahb:NotLit b⊢ mkDisj a b = a.disj b b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.lit a✝) b = (BExpr.lit a✝).disj bb:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.var a✝) b = (BExpr.var a✝).disj bb:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkDisj a✝.neg b = a✝.neg.disj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝¹.conj a✝) b = (a✝¹.conj a✝).disj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝¹.disj a✝) b = (a✝¹.disj a✝).disj b b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.lit a✝) b = (BExpr.lit a✝).disj bb:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.var a✝) b = (BExpr.var a✝).disj bb:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkDisj a✝.neg b = a✝.neg.disj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝¹.conj a✝) b = (a✝¹.conj a✝).disj bb:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝¹.disj a✝) b = (a✝¹.disj a✝).disj b a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj (a✝².disj a✝¹) (BExpr.lit a✝) = (a✝².disj a✝¹).disj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj (a✝².disj a✝¹) (BExpr.var a✝) = (a✝².disj a✝¹).disj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).disj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝) = (a✝³.disj a✝²).disj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝) = (a✝³.disj a✝²).disj (a✝¹.disj a✝) a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.lit a✝¹) (BExpr.lit a✝) = (BExpr.lit a✝¹).disj (BExpr.lit a✝)a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.lit a✝¹) (BExpr.var a✝) = (BExpr.lit a✝¹).disj (BExpr.var a✝)a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (BExpr.lit a✝¹) a✝.neg = (BExpr.lit a✝¹).disj a✝.nega✝²:Boolha:NotLit (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj (BExpr.lit a✝²) (a✝¹.conj a✝) = (BExpr.lit a✝²).disj (a✝¹.conj a✝)a✝²:Boolha:NotLit (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj (BExpr.lit a✝²) (a✝¹.disj a✝) = (BExpr.lit a✝²).disj (a✝¹.disj a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.var a✝¹) (BExpr.lit a✝) = (BExpr.var a✝¹).disj (BExpr.lit a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.var a✝¹) (BExpr.var a✝) = (BExpr.var a✝¹).disj (BExpr.var a✝)a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (BExpr.var a✝¹) a✝.neg = (BExpr.var a✝¹).disj a✝.nega✝²:Natha:NotLit (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj (BExpr.var a✝²) (a✝¹.conj a✝) = (BExpr.var a✝²).disj (a✝¹.conj a✝)a✝²:Natha:NotLit (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj (BExpr.var a✝²) (a✝¹.disj a✝) = (BExpr.var a✝²).disj (a✝¹.disj a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj a✝¹.neg (BExpr.lit a✝) = a✝¹.neg.disj (BExpr.lit a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj a✝¹.neg (BExpr.var a✝) = a✝¹.neg.disj (BExpr.var a✝)a✝¹:BExprha:NotLit a✝¹.nega✝:BExprhb:NotLit a✝.neg⊢ mkDisj a✝¹.neg a✝.neg = a✝¹.neg.disj a✝.nega✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj a✝².neg (a✝¹.conj a✝) = a✝².neg.disj (a✝¹.conj a✝)a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj a✝².neg (a✝¹.disj a✝) = a✝².neg.disj (a✝¹.disj a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj (a✝².conj a✝¹) (BExpr.lit a✝) = (a✝².conj a✝¹).disj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj (a✝².conj a✝¹) (BExpr.var a✝) = (a✝².conj a✝¹).disj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².conj a✝¹) a✝.neg = (a✝².conj a✝¹).disj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝³.conj a✝²) (a✝¹.conj a✝) = (a✝³.conj a✝²).disj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝³.conj a✝²) (a✝¹.disj a✝) = (a✝³.conj a✝²).disj (a✝¹.disj a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj (a✝².disj a✝¹) (BExpr.lit a✝) = (a✝².disj a✝¹).disj (BExpr.lit a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj (a✝².disj a✝¹) (BExpr.var a✝) = (a✝².disj a✝¹).disj (BExpr.var a✝)a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).disj a✝.nega✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝) = (a✝³.disj a✝²).disj (a✝¹.conj a✝)a✝³:BExpra✝²:BExprha:NotLit (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝) = (a✝³.disj a✝²).disj (a✝¹.disj a✝) All goals completed! 🐙 theorem simplify_eq_self_of_reduced (e : BExpr) (h : Reduced e) : simplify e = e := e:BExprh:Reduced e⊢ simplify e = e induction e with b:Boolh:Reduced (BExpr.lit b)⊢ simplify (BExpr.lit b) = BExpr.lit b All goals completed! 🐙 n:Nath:Reduced (BExpr.var n)⊢ simplify (BExpr.var n) = BExpr.var n All goals completed! 🐙 e:BExprih:Reduced e → simplify e = eh:Reduced e.neg⊢ simplify e.neg = e.neg e:BExprih:Reduced e → simplify e = ehe:Reduced ehn:CanKeepNeg e⊢ simplify e.neg = e.neg All goals completed! 🐙 a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bh:Reduced (a.conj b)⊢ simplify (a.conj b) = a.conj b a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ simplify (a.conj b) = a.conj b All goals completed! 🐙 a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bh:Reduced (a.disj b)⊢ simplify (a.disj b) = a.disj b a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ simplify (a.disj b) = a.disj b All goals completed! 🐙 theorem simplify_idempotent (e : BExpr) : simplify (simplify e) = simplify e := simplify_eq_self_of_reduced (simplify e) (simplify_reduced e)
연습문제 8.7.8 산술식 단순화의 구현과 검증

AExpr.eval은 불리언 값 대신 자연수를 계산한다. 두 보조 함수에서는 리터럴이 두 개 있는 분기를 먼저 두어 바로 더하거나 곱한다. 그다음에 0과 1에 관한 규칙을 적용한다.

namespace AExpr def eval (env : Nat → Nat) : AExpr → Nat | .lit n => n | .var i => env i | .add a b => eval env a + eval env b | .mul a b => eval env a * eval env b def mkAdd : AExpr → AExpr → AExpr | .lit a, .lit b => .lit (a + b) | .lit 0, e => e | e, .lit 0 => e | a, b => .add a b def mkMul : AExpr → AExpr → AExpr | .lit a, .lit b => .lit (a * b) | .lit 0, _ => .lit 0 | _, .lit 0 => .lit 0 | .lit 1, e => e | e, .lit 1 => e | a, b => .mul a b def simplify : AExpr → AExpr | .lit n => .lit n | .var i => .var i | .add a b => mkAdd (simplify a) (simplify b) | .mul a b => mkMul (simplify a) (simplify b) end AExpr

mkAdd와 mkMul의 각 분기에서 평가값을 계산하면 자연수 연산의 항등식이 남는다. 예를 들어 .lit 1과 어떤 식을 곱하는 분기에서는 1 * eval env b = eval env b를 simp로 쓴다. 전체 정리는 하위 식을 먼저 단순화한 다음 두 귀납 가정으로 평가값을 원래대로 바꾼다.

namespace AExpr theorem eval_mkAdd (env : Nat → Nat) (a b : AExpr) : eval env (mkAdd a b) = eval env a + eval env b := env:Nat → Nata:AExprb:AExpr⊢ eval env (a.mkAdd b) = eval env a + eval env b env:Nat → Nata:AExprb:AExpr⊢ eval env (match a, b with | lit a, lit b => lit (a + b) | lit 0, e => e | e, lit 0 => e | a, b => a.add b) = eval env a + eval env b env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ + b✝)) = eval env (lit a✝) + eval env (lit b✝)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 0) + eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a + eval env (lit 0)env:Nat → Nata:AExprb:AExprx✝⁴:AExprx✝³:AExprx✝²:a = lit 0 → Falsex✝¹:b = lit 0 → Falsex✝:∀ (a_1 b_1 : Nat), a = lit a_1 → b = lit b_1 → False⊢ eval env (a.add b) = eval env a + eval env b env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ + b✝)) = eval env (lit a✝) + eval env (lit b✝)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 0) + eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a + eval env (lit 0)env:Nat → Nata:AExprb:AExprx✝⁴:AExprx✝³:AExprx✝²:a = lit 0 → Falsex✝¹:b = lit 0 → Falsex✝:∀ (a_1 b_1 : Nat), a = lit a_1 → b = lit b_1 → False⊢ eval env (a.add b) = eval env a + eval env b All goals completed! 🐙 theorem eval_mkMul (env : Nat → Nat) (a b : AExpr) : eval env (mkMul a b) = eval env a * eval env b := env:Nat → Nata:AExprb:AExpr⊢ eval env (a.mkMul b) = eval env a * eval env b env:Nat → Nata:AExprb:AExpr⊢ eval env (match a, b with | lit a, lit b => lit (a * b) | lit 0, x => lit 0 | x, lit 0 => lit 0 | lit 1, e => e | e, lit 1 => e | a, b => a.mul b) = eval env a * eval env b env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ * b✝)) = eval env (lit a✝) * eval env (lit b✝)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env (lit 0) = eval env (lit 0) * eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env (lit 0) = eval env a * eval env (lit 0)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 1) * eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a * eval env (lit 1)env:Nat → Nata:AExprb:AExprx✝⁶:AExprx✝⁵:AExprx✝⁴:a = lit 0 → Falsex✝³:a = lit 1 → Falsex✝²:b = lit 0 → Falsex✝¹:b = lit 1 → Falsex✝:∀ (a_1 b_1 : Nat), a = lit a_1 → b = lit b_1 → False⊢ eval env (a.mul b) = eval env a * eval env b env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ * b✝)) = eval env (lit a✝) * eval env (lit b✝)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env (lit 0) = eval env (lit 0) * eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env (lit 0) = eval env a * eval env (lit 0)env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 1) * eval env benv:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a * eval env (lit 1)env:Nat → Nata:AExprb:AExprx✝⁶:AExprx✝⁵:AExprx✝⁴:a = lit 0 → Falsex✝³:a = lit 1 → Falsex✝²:b = lit 0 → Falsex✝¹:b = lit 1 → Falsex✝:∀ (a_1 b_1 : Nat), a = lit a_1 → b = lit b_1 → False⊢ eval env (a.mul b) = eval env a * eval env b All goals completed! 🐙 theorem eval_simplify (env : Nat → Nat) (e : AExpr) : eval env (simplify e) = eval env e := env:Nat → Nate:AExpr⊢ eval env e.simplify = eval env e induction e with env:Nat → Natn:Nat⊢ eval env (lit n).simplify = eval env (lit n) All goals completed! 🐙 env:Nat → Nati:Nat⊢ eval env (var i).simplify = eval env (var i) All goals completed! 🐙 env:Nat → Nata:AExprb:AExpriha:eval env a.simplify = eval env aihb:eval env b.simplify = eval env b⊢ eval env (a.add b).simplify = eval env (a.add b) All goals completed! 🐙 env:Nat → Nata:AExprb:AExpriha:eval env a.simplify = eval env aihb:eval env b.simplify = eval env b⊢ eval env (a.mul b).simplify = eval env (a.mul b) All goals completed! 🐙 BooleanSimplification.AExpr.lit 5#eval simplify (.mul (.add (.lit 2) (.lit 3)) (.lit 1)) end AExpr