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! 🐙 #eval maxCandidate 2 4 #eval maxOfTwo 2 4- 연습문제 8.1.3 잘못된 프로그램의 반례 찾기
(1, 0)에서maxOfTwoWrong은0을 반환한다. 최댓값은1이다. 자연수 입력에서b = 0이고a > 0이면 오류가 나는 분기에 들어간다. 앞의 세 테스트에는 둘째 입력이0인 쌍이 없었다. 두 입력이 모두0인 경우에는 첫 분기가 실행되어 올바른 결과인0을 반환한다.#eval maxOfTwoWrong 1 0 #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)split isTrue a:Natb:Nath✝:a ≤ b⊢ a ≤ a ∧ a ≤ b ∧ (a = a ∨ a = b)isFalse a:Natb:Nath✝:¬a ≤ b⊢ b ≤ a ∧ b ≤ b ∧ (b = a ∨ b = b)· isTrue a:Natb:Nath✝:a ≤ b⊢ a ≤ a ∧ a ≤ b ∧ (a = a ∨ a = b)omega All goals completed! 🐙· isFalse a:Natb:Nath✝:¬a ≤ b⊢ b ≤ a ∧ b ≤ b ∧ (b = a ∨ b = b)omega All goals completed! 🐙- 연습문제 8.2.2 자연수 제곱합의 함수적 명세
공식을 그대로 기준 함수로 적고 전칭 한정사를 사용하여 모든 입력의 반환값이 그 함수의 값과 같다는 명제를 쓴다.
#check는 이 명제의 타입을 확인한다. 세#guard는 두 함수의 값이 차례로0,1,30인지 검사한다.def sumSquaresSpec (n : Nat) : Nat := n * (n + 1) * (2 * n + 1) / 6 #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 :=by ⊢ AbsDiffSpec 3 7 4unfold AbsDiffSpec ⊢ 3 + 4 = 7 ∨ 7 + 4 = 3decide All goals completed! 🐙 example : AbsDiffSpec 7 3 4 :=by ⊢ AbsDiffSpec 7 3 4unfold AbsDiffSpec ⊢ 7 + 4 = 3 ∨ 3 + 4 = 7decide 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) :=by ⊢ ¬MinSpec 3 5 (minCandidate 3 5)unfold MinSpec minCandidate ⊢ ¬(0 ≤ 3 ∧ 0 ≤ 5 ∧ (0 = 3 ∨ 0 = 5))decide 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다. 하나의 자연수 쌍에서 등식이 성립하지 않으므로 전칭 명제의 반례가 된다.- 연습문제 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) :=by n:Nath:HalfPre n⊢ HalfPost n (half n)unfold HalfPre at h n:Nath:n % 2 = 0⊢ HalfPost n (half n)unfold HalfPost half n:Nath:n % 2 = 0⊢ 2 * (n / 2) = nhave hdiv := Nat.div_add_mod n 2 n:Nath:n % 2 = 0hdiv:2 * (n / 2) + n % 2 = n⊢ 2 * (n / 2) = nrw [ n:Nath:n % 2 = 0hdiv:2 * (n / 2) = n⊢ 2 * (n / 2) = nh, n:Nath:n % 2 = 0hdiv:2 * (n / 2) + 0 = n⊢ 2 * (n / 2) = nNat.add_zero n:Nath:n % 2 = 0hdiv:2 * (n / 2) = n⊢ 2 * (n / 2) = n] at hdivexact hdiv 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) :=by xs:List Nath:HeadPre xs⊢ HeadPost xs (firstOrZero xs) cases xs with| nil => nil h:HeadPre []⊢ HeadPost [] (firstOrZero [])exact False.elim (h rfl) All goals completed! 🐙| cons x rest => cons x:Natrest:List Nath:HeadPre (x :: rest)⊢ HeadPost (x :: rest) (firstOrZero (x :: rest))exact ⟨rest, rfl⟩ 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) :=by ⊢ Correct divModPair (fun p => DivPre p.fst p.snd) fun p qr => DivPost p.fst p.snd qr.fst qr.sndintro p h p:Nat × Nath:DivPre p.fst p.snd⊢ DivPost p.fst p.snd (divModPair p).fst (divModPair p).sndexact divMod_correct p.1 p.2 h 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 :=by a:Natb:Nat⊢ addByRecursion a b = a + b induction b with| zero => zero a:Nat⊢ addByRecursion a 0 = a + 0rfl All goals completed! 🐙| succ b ih => succ a:Natb:Natih:addByRecursion a b = a + b⊢ addByRecursion a (b + 1) = a + (b + 1)rw [ All goals completed! 🐙addByRecursion, succ a:Natb:Natih:addByRecursion a b = a + b⊢ (addByRecursion a b).succ = a + (b + 1)Nat.add_succ, succ a:Natb:Natih:addByRecursion a b = a + b⊢ (addByRecursion a b).succ = (a + b).succih succ a:Natb:Natih:addByRecursion a b = a + b⊢ (a + b).succ = (a + b).succ]- 연습문제 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 :=by a:Natb:Nat⊢ maxFromRight a b = maxOfTwo a bunfold maxFromRight maxOfTwo a:Natb:Nat⊢ (if b ≤ a then a else b) = if a ≤ b then b else asplit isTrue a:Natb:Nath✝:b ≤ a⊢ a = if a ≤ b then b else aisFalse a:Natb:Nath✝:¬b ≤ a⊢ b = if a ≤ b then b else a<;> isTrue a:Natb:Nath✝:b ≤ a⊢ a = if a ≤ b then b else aisFalse a:Natb:Nath✝:¬b ≤ a⊢ b = if a ≤ b then b else asplit isFalse.isTrue a:Natb:Nath✝¹:¬b ≤ ah✝:a ≤ b⊢ b = bisFalse.isFalse a:Natb:Nath✝¹:¬b ≤ ah✝:¬a ≤ b⊢ b = a<;> isTrue.isTrue a:Natb:Nath✝¹:b ≤ ah✝:a ≤ b⊢ a = bisTrue.isFalse a:Natb:Nath✝¹:b ≤ ah✝:¬a ≤ b⊢ a = aisFalse.isTrue a:Natb:Nath✝¹:¬b ≤ ah✝:a ≤ b⊢ b = bisFalse.isFalse a:Natb:Nath✝¹:¬b ≤ ah✝:¬a ≤ b⊢ b = aomega 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 :=by α:Typexs:List αacc:Nat⊢ lengthAcc xs acc = acc + xs.length induction xs generalizing acc with| nil => nil α:Typeacc:Nat⊢ lengthAcc [] acc = acc + [].lengthrfl All goals completed! 🐙| cons x xs ih => cons α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ lengthAcc (x :: xs) acc = acc + (x :: xs).lengthsimp only [lengthAcc, ih, List.length_cons] cons α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ acc + 1 + xs.length = acc + (xs.length + 1)rw [ All goals completed! 🐙 theorem lengthAcc_eq_append_length {α : Type} (xs ys : List α) : lengthAcc xs ys.length = (ys ++ xs).length :=Nat.add_assoc, cons α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ acc + (1 + xs.length) = acc + (xs.length + 1)Nat.add_comm 1 cons α:Typex:αxs:List αih:∀ (acc : Nat), lengthAcc xs acc = acc + xs.lengthacc:Nat⊢ acc + (xs.length + 1) = acc + (xs.length + 1)]by α:Typexs:List αys:List α⊢ lengthAcc xs ys.length = (ys ++ xs).lengthrw [ All goals completed! 🐙lengthAcc_matches_length, α:Typexs:List αys:List α⊢ ys.length + xs.length = (ys ++ xs).lengthList.length_append α:Typexs:List αys:List α⊢ ys.length + xs.length = ys.length + xs.length]- 연습문제 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 :=by xs:List Natacc:Nat⊢ sumAux xs acc = acc + sumSlow xs induction xs generalizing acc with| nil => nil acc:Nat⊢ sumAux [] acc = acc + sumSlow []rfl All goals completed! 🐙| cons x xs ih => cons x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ sumAux (x :: xs) acc = acc + sumSlow (x :: xs)rw [ All goals completed! 🐙 theorem sum_correct (xs : List Nat) : sumFast xs = sumSlow xs :=sumAux, cons x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ sumAux xs (acc + x) = acc + sumSlow (x :: xs)sumSlow, cons x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ sumAux xs (acc + x) = acc + (x + sumSlow xs)ih (acc + x), cons x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ acc + x + sumSlow xs = acc + (x + sumSlow xs)Nat.add_assoc cons x:Natxs:List Natih:∀ (acc : Nat), sumAux xs acc = acc + sumSlow xsacc:Nat⊢ acc + (x + sumSlow xs) = acc + (x + sumSlow xs)]by xs:List Nat⊢ sumFast xs = sumSlow xsunfold sumFast xs:List Nat⊢ sumAux xs 0 = sumSlow xsrw [ All goals completed! 🐙sumAux_correct, xs:List Nat⊢ 0 + sumSlow xs = sumSlow xsNat.zero_add xs:List Nat⊢ sumSlow xs = sumSlow xs]- 연습문제 8.4.5 리스트 연결 함수의 동등성
두 연결은 첫째 리스트의 생성자에 따라 계산하므로
xs에 대해 귀납한다. 빈 리스트를 연결하면 양변이ys다.cons분기에서는 양쪽에 먼저x를 붙이고 각각의 방법으로 꼬리와ys를 연결한다. 꼬리에서 두 결과가 같다는 증명이ih다.theorem appendRec_correct {α : Type} (xs ys : List α) : appendRec xs ys = xs ++ ys :=by α:Typexs:List αys:List α⊢ appendRec xs ys = xs ++ ys induction xs with| nil => nil α:Typeys:List α⊢ appendRec [] ys = [] ++ ysrfl All goals completed! 🐙| cons x xs ih => cons α:Typeys:List αx:αxs:List αih:appendRec xs ys = xs ++ ys⊢ appendRec (x :: xs) ys = x :: xs ++ ysrw [ All goals completed! 🐙appendRec, cons α:Typeys:List αx:αxs:List αih:appendRec xs ys = xs ++ ys⊢ x :: appendRec xs ys = x :: xs ++ ysList.cons_append, cons α:Typeys:List αx:αxs:List αih:appendRec xs ys = xs ++ ys⊢ x :: appendRec xs ys = x :: (xs ++ ys)ih cons α:Typeys:List αx:αxs:List αih:appendRec xs ys = xs ++ ys⊢ x :: (xs ++ ys) = x :: (xs ++ ys)]- 연습문제 8.4.6 리스트를 두 번 뒤집는 함수의 정확성
두 번의
reverseFast를List.reverse로 바꾸면 목표는xs.reverse.reverse = xs가 된다.List.reverse_reverse가 이 등식의 증명이다.theorem reverseFast_twice {α : Type} (xs : List α) : reverseFast (reverseFast xs) = xs :=by α:Typexs:List α⊢ reverseFast (reverseFast xs) = xssimp only [reverseFast_eq_reverse, List.reverse_reverse] 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) :=by α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.lengthintro xs _ α:Typexs:List αa✝:True⊢ (reverseSlow xs).length = xs.lengthrw [ All goals completed! 🐙 theorem reverseFast_length {α : Type} : Correct (@reverseFast α) (fun _ => True) (fun xs ys => ys.length = xs.length) :=reverseSlow_eq_reverse, α:Typexs:List αa✝:True⊢ xs.reverse.length = xs.lengthList.length_reverse α:Typexs:List αa✝:True⊢ xs.length = xs.length]by α:Type⊢ Correct reverseFast (fun x => True) fun xs ys => ys.length = xs.lengthapply correct_of_equiv reverseSlow reverseFast hf α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.lengthheq α:Type⊢ ∀ (x : List α), True → reverseFast x = reverseSlow x· hf α:Type⊢ Correct reverseSlow (fun x => True) fun xs ys => ys.length = xs.lengthexact reverseSlow_length All goals completed! 🐙· heq α:Type⊢ ∀ (x : List α), True → reverseFast x = reverseSlow xintro xs _ heq α:Typexs:List αa✝:True⊢ reverseFast xs = reverseSlow xsexact reverse_correct 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 :=by target:Natxs:List Nat⊢ target ∈ xs → searchAlwaysTrue target xs = trueintro _ target:Natxs:List Nata✝:target ∈ xs⊢ searchAlwaysTrue target xs = truerfl All goals completed! 🐙 example : ¬ SearchSpec 6 [] (searchAlwaysTrue 6 []) :=by ⊢ ¬SearchSpec 6 [] (searchAlwaysTrue 6 [])simp [SearchSpec, searchAlwaysTrue] 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 :=by target:Natxs:List Nat⊢ searchAlwaysFalse target xs = true → target ∈ xssimp [searchAlwaysFalse] All goals completed! 🐙 example : ¬ SearchSpec 7 [7] (searchAlwaysFalse 7 [7]) :=by ⊢ ¬SearchSpec 7 [7] (searchAlwaysFalse 7 [7])simp [SearchSpec, searchAlwaysFalse] 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 :=by target:Natxs:List Nat⊢ linearSearch target xs = false ↔ ¬target ∈ xshave hs := linearSearch_correct target xs target:Natxs:List Naths:SearchSpec target xs (linearSearch target xs)⊢ linearSearch target xs = false ↔ ¬target ∈ xsunfold SearchSpec at hs target:Natxs:List Naths:linearSearch target xs = true ↔ target ∈ xs⊢ linearSearch target xs = false ↔ ¬target ∈ xssimpa only [Bool.not_eq_true] using (not_congr hs) All goals completed! 🐙- 연습문제 8.5.5 짝수 탐색의 정확성
머리가 짝수면 그 머리가 존재 명제의 증인이다. 머리가 홀수인 분기에서는 짝수인 증인이 있다면 꼬리에 있어야 한다.
simp는cons의 존재 명제를 머리에 대한 경우와 꼬리에서의 존재로 나누므로, 꼬리의 존재와 탐색 결과는ih로 연결할 수 있다.theorem hasEven_correct (xs : List Nat) : hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0 :=by xs:List Nat⊢ hasEven xs = true ↔ ∃ x, x ∈ xs ∧ x % 2 = 0 induction xs with| nil => nil ⊢ hasEven [] = true ↔ ∃ x, x ∈ [] ∧ x % 2 = 0simp [hasEven] All goals completed! 🐙| cons x xs ih => cons 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 = 0by_cases hx : x % 2 = 0 pos 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 = 0neg 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· pos 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 = 0simp [hasEven, hx] All goals completed! 🐙· neg 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 = 0simp [hasEven, hx, ih] All goals completed! 🐙- 연습문제 8.5.6 모든 원소의 짝수 여부 검사
모든 원소가 짝수라는 조건은 머리가 짝수이고 꼬리의 모든 원소도 짝수라는 조건으로 나뉜다. 머리가 짝수인 분기에서는 귀납 가정으로 꼬리의 계산 결과와 전칭 명제를 연결한다. 머리가 홀수이면 전체 조건에 반례가 있다. 빈 리스트에서는 짝수를 찾는 존재 명제가 거짓이고, 모두 짝수라는 전칭 명제는 반례가 없어 참이다.
theorem allEven_correct (xs : List Nat) : allEven xs = true ↔ ∀ x, x ∈ xs → x % 2 = 0 :=by xs:List Nat⊢ allEven xs = true ↔ ∀ (x : Nat), x ∈ xs → x % 2 = 0 induction xs with| nil => nil ⊢ allEven [] = true ↔ ∀ (x : Nat), x ∈ [] → x % 2 = 0simp [allEven] All goals completed! 🐙| cons x xs ih => cons 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 = 0by_cases hx : x % 2 = 0 pos 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 = 0neg 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· pos 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 = 0simp [allEven, hx, ih] All goals completed! 🐙· neg 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 = 0simp [allEven, hx] 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) :=by target:Natxs:List Nat⊢ IndexSearchSpec target xs (findIndex target xs) induction xs with| nil => nil target:Nat⊢ IndexSearchSpec target [] (findIndex target [])simp [findIndex, IndexSearchSpec] All goals completed! 🐙| cons x xs ih => cons target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs))by_cases hx : x = target pos target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs))neg target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:¬x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs))· pos target:Natx:Natxs:List Natih:IndexSearchSpec target xs (findIndex target xs)hx:x = target⊢ IndexSearchSpec target (x :: xs) (findIndex target (x :: xs))simp [findIndex, IndexSearchSpec, hx] All goals completed! 🐙· neg 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| none => neg.none 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))simpa [findIndex, IndexSearchSpec, hx, Ne.symm hx, h] using ih All goals completed! 🐙| some i => neg.some 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))simpa [findIndex, IndexSearchSpec, hx, h] using ih 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를 놓는다.#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] :=by ⊢ Sorted [3]simp [Sorted] All goals completed! 🐙 example : Sorted [1, 2, 2, 4] :=by ⊢ Sorted [1, 2, 2, 4]unfold Sorted ⊢ List.Pairwise (fun x1 x2 => x1 ≤ x2) [1, 2, 2, 4]decide All goals completed! 🐙 example : ¬ Sorted [1, 3, 2] :=by ⊢ ¬Sorted [1, 3, 2]unfold Sorted ⊢ ¬List.Pairwise (fun x1 x2 => x1 ≤ x2) [1, 3, 2]decide 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] :=by ⊢ [1, 2, 3].Perm [2, 3, 1]have first : ([1, 2, 3] : List Nat).Perm [2, 1, 3] := List.Perm.swap 2 1 [3] first:[1, 2, 3].Perm [2, 1, 3]⊢ [1, 2, 3].Perm [2, 3, 1]have second : ([2, 1, 3] : List Nat).Perm [2, 3, 1] := List.Perm.cons 2 (List.Perm.swap 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]exact first.trans second 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 :=by x:Natxs:List Nat⊢ (insertNat x xs).length = xs.length + 1 induction xs with| nil => nil x:Nat⊢ (insertNat x []).length = [].length + 1rfl All goals completed! 🐙| cons y ys ih => cons x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1by_cases h : x ≤ y pos x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1neg x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1· pos x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1rw [ insertNat, pos x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (if x ≤ y then x :: y :: ys else y :: insertNat x ys).length = (y :: ys).length + 1ite_eq_left h pos x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (x :: y :: ys).length = (y :: ys).length + 1]pos x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:x ≤ y⊢ (x :: y :: ys).length = (y :: ys).length + 1rfl All goals completed! 🐙· neg x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (insertNat x (y :: ys)).length = (y :: ys).length + 1rw [ insertNat, neg x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (if x ≤ y then x :: y :: ys else y :: insertNat x ys).length = (y :: ys).length + 1ite_eq_right h neg x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (y :: insertNat x ys).length = (y :: ys).length + 1]neg x:Naty:Natys:List Natih:(insertNat x ys).length = ys.length + 1h:¬x ≤ y⊢ (y :: insertNat x ys).length = (y :: ys).length + 1simp only [List.length_cons, ih] All goals completed! 🐙 example (x : Nat) (xs : List Nat) : (insertNat x xs).length = xs.length + 1 :=by x:Natxs:List Nat⊢ (insertNat x xs).length = xs.length + 1simpa only [List.length_cons] using (insert_perm x xs).length_eq All goals completed! 🐙- 연습문제 8.6.6 정렬 전후의 길이 보존
빈 리스트에서는 길이가
0이다. 귀납 단계에서는insertNat_length를 적용한 뒤 귀납 가정으로 정렬한 꼬리의 길이를 원래 꼬리의 길이로 바꾼다. 순열 정리를 사용하면 길이 보존을 바로 얻는다.theorem insertionSort_length (xs : List Nat) : (insertionSort xs).length = xs.length :=by xs:List Nat⊢ (insertionSort xs).length = xs.length induction xs with| nil => nil ⊢ (insertionSort []).length = [].lengthrfl All goals completed! 🐙| cons x xs ih => cons x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ (insertionSort (x :: xs)).length = (x :: xs).lengthrw [ All goals completed! 🐙 example (xs : List Nat) : (insertionSort xs).length = xs.length := (insertionSort_perm xs).length_eqinsertionSort, cons x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ (insertNat x (insertionSort xs)).length = (x :: xs).lengthinsertNat_length, cons x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ (insertionSort xs).length + 1 = (x :: xs).lengthih, cons x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ xs.length + 1 = (x :: xs).lengthList.length_cons cons x:Natxs:List Natih:(insertionSort xs).length = xs.length⊢ xs.length + 1 = xs.length + 1]- 연습문제 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 :=by xs:List Nath:Sorted xs⊢ insertionSort xs = xs induction xs with| nil => nil h:Sorted []⊢ insertionSort [] = []rfl All goals completed! 🐙| cons x xs ih => cons x:Natxs:List Natih:Sorted xs → insertionSort xs = xsh:Sorted (x :: xs)⊢ insertionSort (x :: xs) = x :: xs cases xs with| nil => cons.nil x:Natih:Sorted [] → insertionSort [] = []h:Sorted [x]⊢ insertionSort [x] = [x]rfl All goals completed! 🐙| cons y ys => cons.cons x:Naty:Natys:List Natih:Sorted (y :: ys) → insertionSort (y :: ys) = y :: ysh:Sorted (x :: y :: ys)⊢ insertionSort (x :: y :: ys) = x :: y :: ysrcases List.pairwise_cons.mp h with ⟨hhead, htail⟩ cons.cons 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 :: yshave hxy : x ≤ y := hhead y List.mem_cons_self cons.cons 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 :: ysrw [ All goals completed! 🐙 theorem insertionSort_idempotent (xs : List Nat) : insertionSort (insertionSort xs) = insertionSort xs := insertionSort_eq_self (insertionSort xs) (insertionSort_sorted xs)insertionSort, cons.cons 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⊢ insertNat x (insertionSort (y :: ys)) = x :: y :: ysih htail, cons.cons 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⊢ insertNat x (y :: ys) = x :: y :: ysinsertNat, cons.cons 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⊢ (if x ≤ y then x :: y :: ys else y :: insertNat x ys) = x :: y :: ysite_eq_left hxy cons.cons 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⊢ x :: y :: ys = x :: y :: ys]- 연습문제 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) :=by x:Natxs:List Nat⊢ (insertNatDescending x xs).Perm (x :: xs) induction xs with| nil => nil x:Nat⊢ (insertNatDescending x []).Perm [x]exact List.Perm.refl _ All goals completed! 🐙| cons y ys ih => cons x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)by_cases h : y ≤ x pos x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)neg x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)· pos x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)rw [ All goals completed! 🐙insertNatDescending, pos x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (if y ≤ x then x :: y :: ys else y :: insertNatDescending x ys).Perm (x :: y :: ys)ite_eq_left h pos x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:y ≤ x⊢ (x :: y :: ys).Perm (x :: y :: ys)]· neg x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (insertNatDescending x (y :: ys)).Perm (x :: y :: ys)rw [ insertNatDescending, neg x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (if y ≤ x then x :: y :: ys else y :: insertNatDescending x ys).Perm (x :: y :: ys)ite_eq_right h neg x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (y :: insertNatDescending x ys).Perm (x :: y :: ys)]neg x:Naty:Natys:List Natih:(insertNatDescending x ys).Perm (x :: ys)h:¬y ≤ x⊢ (y :: insertNatDescending x ys).Perm (x :: y :: ys)exact (List.Perm.cons y ih).trans (List.Perm.swap 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 :=by a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ a⊢ ∀ (y : Nat), y ∈ insertNatDescending x xs → y ≤ aintro y hy a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xs⊢ y ≤ ahave hm : y ∈ x :: xs := (insertDescending_perm x xs).mem_iff.mp hy a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xs⊢ y ≤ arcases List.mem_cons.mp hm with hyx | hmem inl a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ y ≤ ainr 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· inl a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ y ≤ arw [ hyx inl 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]inl a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshyx:y = x⊢ x ≤ aexact hx All goals completed! 🐙· inr a:Natx:Natxs:List Nathx:x ≤ ahxs:∀ (y : Nat), y ∈ xs → y ≤ ay:Nathy:y ∈ insertNatDescending x xshm:y ∈ x :: xshmem:y ∈ xs⊢ y ≤ aexact hxs y hmem All goals completed! 🐙 theorem insertDescending_sorted (x : Nat) (xs : List Nat) (h : SortedDescending xs) : SortedDescending (insertNatDescending x xs) :=by x:Natxs:List Nath:SortedDescending xs⊢ SortedDescending (insertNatDescending x xs) induction xs with| nil => nil x:Nath:SortedDescending []⊢ SortedDescending (insertNatDescending x [])simp [insertNatDescending, SortedDescending] All goals completed! 🐙| cons y ys ih => cons x:Naty:Natys:List Natih:SortedDescending ys → SortedDescending (insertNatDescending x ys)h:SortedDescending (y :: ys)⊢ SortedDescending (insertNatDescending x (y :: ys))rcases List.pairwise_cons.mp h with ⟨hhead, htail⟩ cons 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))by_cases hyx : y ≤ x pos 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))neg 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))· pos 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))rw [ insertNatDescending, pos 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 (if y ≤ x then x :: y :: ys else y :: insertNatDescending x ys)ite_eq_left hyx pos 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)]pos 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)apply List.Pairwise.cons _ h 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'intro z hz 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 ≥ zrcases List.mem_cons.mp hz with heq | hmem inl 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 ≥ zinr 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· inl 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 ≥ zrw [ heq inl 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]inl 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 ≥ yexact hyx All goals completed! 🐙· inr 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 ≥ zexact Nat.le_trans (hhead z hmem) hyx All goals completed! 🐙· neg 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))rw [ insertNatDescending, neg 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 (if y ≤ x then x :: y :: ys else y :: insertNatDescending x ys)ite_eq_right hyx neg 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)]neg 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)exact List.Pairwise.cons (insertDescending_preserves_upperBound y x ys (Nat.le_of_lt (Nat.lt_of_not_ge hyx)) hhead) (ih htail) All goals completed! 🐙전체 정렬의 귀납 단계에서는 정렬한 꼬리에 삽입의 보조 정리를 적용한다. 마지막 정리에서 정렬과 순열의 증명을 묶는다.
theorem insertionSortDescending_sorted (xs : List Nat) : SortedDescending (insertionSortDescending xs) :=by xs:List Nat⊢ SortedDescending (insertionSortDescending xs) induction xs with| nil => nil ⊢ SortedDescending (insertionSortDescending [])exact List.Pairwise.nil All goals completed! 🐙| cons x xs ih => cons x:Natxs:List Natih:SortedDescending (insertionSortDescending xs)⊢ SortedDescending (insertionSortDescending (x :: xs))rw [ insertionSortDescending cons x:Natxs:List Natih:SortedDescending (insertionSortDescending xs)⊢ SortedDescending (insertNatDescending x (insertionSortDescending xs))]cons x:Natxs:List Natih:SortedDescending (insertionSortDescending xs)⊢ SortedDescending (insertNatDescending x (insertionSortDescending xs))exact insertDescending_sorted x (insertionSortDescending xs) ih All goals completed! 🐙 theorem insertionSortDescending_perm (xs : List Nat) : (insertionSortDescending xs).Perm xs :=by xs:List Nat⊢ (insertionSortDescending xs).Perm xs induction xs with| nil => nil ⊢ (insertionSortDescending []).Perm []exact List.Perm.nil All goals completed! 🐙| cons x xs ih => cons x:Natxs:List Natih:(insertionSortDescending xs).Perm xs⊢ (insertionSortDescending (x :: xs)).Perm (x :: xs)rw [ insertionSortDescending cons x:Natxs:List Natih:(insertionSortDescending xs).Perm xs⊢ (insertNatDescending x (insertionSortDescending xs)).Perm (x :: xs)]cons x:Natxs:List Natih:(insertionSortDescending xs).Perm xs⊢ (insertNatDescending x (insertionSortDescending xs)).Perm (x :: xs)have hi := insertDescending_perm x (insertionSortDescending xs) cons x:Natxs:List Natih:(insertionSortDescending xs).Perm xshi:(insertNatDescending x (insertionSortDescending xs)).Perm (x :: insertionSortDescending xs)⊢ (insertNatDescending x (insertionSortDescending xs)).Perm (x :: xs)exact hi.trans (List.Perm.cons x ih) All goals completed! 🐙 theorem insertionSortDescending_correct (xs : List Nat) : SortedDescending (insertionSortDescending xs) ∧ xs.Perm (insertionSortDescending xs) :=by xs:List Nat⊢ SortedDescending (insertionSortDescending xs) ∧ xs.Perm (insertionSortDescending xs)constructor left xs:List Nat⊢ SortedDescending (insertionSortDescending xs)right xs:List Nat⊢ xs.Perm (insertionSortDescending xs)· left xs:List Nat⊢ SortedDescending (insertionSortDescending xs)exact insertionSortDescending_sorted xs All goals completed! 🐙· right xs:List Nat⊢ xs.Perm (insertionSortDescending xs)exact (insertionSortDescending_perm xs).symm All goals completed! 🐙
- 연습문제 8.7.1 불리언 식을 구문 트리로 표현하기
전체 식의 뿌리는 논리곱이다. 왼쪽은
p에 부정을 적용한 식이고, 오른쪽은q와false의 논리합이다.- 연습문제 8.7.2 변수 할당을 바꾸어 식 계산하기
q만 참이면 왼쪽의¬p와 오른쪽의q ∨ false가 둘 다 참이다.p만 참이면 두 하위 식이 모두 거짓이므로 논리곱의 값도 거짓이다.#eval eval (fun n => n == 1) practiceExpr #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 :=by ⊢ SemEquiv deMorganLeft deMorganRightintro env env:Env⊢ eval env deMorganLeft = eval env deMorganRightcases h₀ : env 0 false env:Envh₀:env 0 = false⊢ eval env deMorganLeft = eval env deMorganRighttrue env:Envh₀:env 0 = true⊢ eval env deMorganLeft = eval env deMorganRight<;> false env:Envh₀:env 0 = false⊢ eval env deMorganLeft = eval env deMorganRighttrue env:Envh₀:env 0 = true⊢ eval env deMorganLeft = eval env deMorganRightcases h₁ : env 1 true.false env:Envh₀:env 0 = trueh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRighttrue.true env:Envh₀:env 0 = trueh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRight<;> false.false env:Envh₀:env 0 = falseh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRightfalse.true env:Envh₀:env 0 = falseh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRighttrue.false env:Envh₀:env 0 = trueh₁:env 1 = false⊢ eval env deMorganLeft = eval env deMorganRighttrue.true env:Envh₀:env 0 = trueh₁:env 1 = true⊢ eval env deMorganLeft = eval env deMorganRightsimp [deMorganLeft, deMorganRight, eval, h₀, h₁] 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) :=by env:Enva:BExprb:BExpr⊢ eval env (mkDisj a b) = (eval env a || eval env b)unfold mkDisj 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)split h_1 env:Envb:BExprx✝¹:BExprx✝:BExpr⊢ eval env (BExpr.lit true) = (eval env (BExpr.lit true) || eval env b)h_2 env:Enva:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ eval env (BExpr.lit true) = (eval env a || eval env (BExpr.lit true))h_3 env:Envb:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ eval env b = (eval env (BExpr.lit false) || eval env b)h_4 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))h_5 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)<;> h_1 env:Envb:BExprx✝¹:BExprx✝:BExpr⊢ eval env (BExpr.lit true) = (eval env (BExpr.lit true) || eval env b)h_2 env:Enva:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ eval env (BExpr.lit true) = (eval env a || eval env (BExpr.lit true))h_3 env:Envb:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ eval env b = (eval env (BExpr.lit false) || eval env b)h_4 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))h_5 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)simp [eval] 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) :=by env:Enve:BExpr⊢ eval env (mkNeg e) = !eval env ecases e lit env:Enva✝:Bool⊢ eval env (mkNeg (BExpr.lit a✝)) = !eval env (BExpr.lit a✝)var env:Enva✝:Nat⊢ eval env (mkNeg (BExpr.var a✝)) = !eval env (BExpr.var a✝)neg env:Enva✝:BExpr⊢ eval env (mkNeg a✝.neg) = !eval env a✝.negconj env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.conj a✝)) = !eval env (a✝¹.conj a✝)disj env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.disj a✝)) = !eval env (a✝¹.disj a✝)<;> lit env:Enva✝:Bool⊢ eval env (mkNeg (BExpr.lit a✝)) = !eval env (BExpr.lit a✝)var env:Enva✝:Nat⊢ eval env (mkNeg (BExpr.var a✝)) = !eval env (BExpr.var a✝)neg env:Enva✝:BExpr⊢ eval env (mkNeg a✝.neg) = !eval env a✝.negconj env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.conj a✝)) = !eval env (a✝¹.conj a✝)disj env:Enva✝¹:BExpra✝:BExpr⊢ eval env (mkNeg (a✝¹.disj a✝)) = !eval env (a✝¹.disj a✝)simp [mkNeg, eval] 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 :=by e:BExpr⊢ exprSize (mkNeg e) ≤ 1 + exprSize eunfold mkNeg e:BExpr⊢ exprSize (match e with | BExpr.lit b => BExpr.lit !b | e.neg => e | e => e.neg) ≤ 1 + exprSize esplit h_1 x✝:BExprb✝:Bool⊢ exprSize (BExpr.lit !b✝) ≤ 1 + exprSize (BExpr.lit b✝)h_2 x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + exprSize e✝.negh_3 e:BExprx✝²:BExprx✝¹:∀ (b : Bool), e = BExpr.lit b → Falsex✝:∀ (e_1 : BExpr), e = e_1.neg → False⊢ exprSize e.neg ≤ 1 + exprSize e<;> h_1 x✝:BExprb✝:Bool⊢ exprSize (BExpr.lit !b✝) ≤ 1 + exprSize (BExpr.lit b✝)h_2 x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + exprSize e✝.negh_3 e:BExprx✝²:BExprx✝¹:∀ (b : Bool), e = BExpr.lit b → Falsex✝:∀ (e_1 : BExpr), e = e_1.neg → False⊢ exprSize e.neg ≤ 1 + exprSize esimp [exprSize] All goals completed! 🐙<;> h_2 x✝:BExpre✝:BExpr⊢ exprSize e✝ ≤ 1 + (1 + exprSize e✝)omega All goals completed! 🐙 theorem mkConj_size_le (a b : BExpr) : exprSize (mkConj a b) ≤ 1 + exprSize a + exprSize b :=by a:BExprb:BExpr⊢ exprSize (mkConj a b) ≤ 1 + exprSize a + exprSize bunfold mkConj 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 bsplit h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize (BExpr.lit false) + exprSize bh_2 a:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit false → False⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize a + exprSize (BExpr.lit false)h_3 b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit false → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit true) + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit true)h_5 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<;> h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize (BExpr.lit false) + exprSize bh_2 a:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit false → False⊢ exprSize (BExpr.lit false) ≤ 1 + exprSize a + exprSize (BExpr.lit false)h_3 b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit false → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit true) + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit true)h_5 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 bsimp [exprSize] All goals completed! 🐙<;> h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ 1 ≤ 2 + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit false → Falsex✝:a = BExpr.lit true → False⊢ exprSize a ≤ 1 + exprSize a + 1omega All goals completed! 🐙 theorem mkDisj_size_le (a b : BExpr) : exprSize (mkDisj a b) ≤ 1 + exprSize a + exprSize b :=by a:BExprb:BExpr⊢ exprSize (mkDisj a b) ≤ 1 + exprSize a + exprSize bunfold mkDisj 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 bsplit h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize (BExpr.lit true) + exprSize bh_2 a:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize a + exprSize (BExpr.lit true)h_3 b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit false) + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit false)h_5 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<;> h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize (BExpr.lit true) + exprSize bh_2 a:BExprx✝²:BExprx✝¹:BExprx✝:a = BExpr.lit true → False⊢ exprSize (BExpr.lit true) ≤ 1 + exprSize a + exprSize (BExpr.lit true)h_3 b:BExprx✝²:BExprx✝¹:BExprx✝:b = BExpr.lit true → False⊢ exprSize b ≤ 1 + exprSize (BExpr.lit false) + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + exprSize (BExpr.lit false)h_5 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 bsimp [exprSize] All goals completed! 🐙<;> h_1 b:BExprx✝¹:BExprx✝:BExpr⊢ 1 ≤ 2 + exprSize bh_4 a:BExprx✝³:BExprx✝²:BExprx✝¹:a = BExpr.lit true → Falsex✝:a = BExpr.lit false → False⊢ exprSize a ≤ 1 + exprSize a + 1omega All goals completed! 🐙전체 식의 귀납 단계에서는 먼저 단순화한 하위 식에 보조 정리를 적용한다. 예를 들어 논리곱에서 그 정리는 결과의 크기가
1 + exprSize (simplify a) + exprSize (simplify b)이하라고 보장한다. 귀납 가정은 각 항이 원래 하위 식의 크기 이하임을 주므로, 두 종류의 부등식을 합치면 된다.theorem simplify_size_le (e : BExpr) : exprSize (simplify e) ≤ exprSize e :=by e:BExpr⊢ exprSize (simplify e) ≤ exprSize e induction e with| lit b => lit b:Bool⊢ exprSize (simplify (BExpr.lit b)) ≤ exprSize (BExpr.lit b)exact Nat.le_refl _ All goals completed! 🐙| var n => var n:Nat⊢ exprSize (simplify (BExpr.var n)) ≤ exprSize (BExpr.var n)exact Nat.le_refl _ All goals completed! 🐙| neg e ih => neg e:BExprih:exprSize (simplify e) ≤ exprSize e⊢ exprSize (simplify e.neg) ≤ exprSize e.neghave h := mkNeg_size_le (simplify e) neg e:BExprih:exprSize (simplify e) ≤ exprSize eh:exprSize (mkNeg (simplify e)) ≤ 1 + exprSize (simplify e)⊢ exprSize (simplify e.neg) ≤ exprSize e.negsimp only [simplify, exprSize] neg e:BExprih:exprSize (simplify e) ≤ exprSize eh:exprSize (mkNeg (simplify e)) ≤ 1 + exprSize (simplify e)⊢ exprSize (mkNeg (simplify e)) ≤ 1 + exprSize eomega All goals completed! 🐙| conj a b iha ihb => conj a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize b⊢ exprSize (simplify (a.conj b)) ≤ exprSize (a.conj b)have h := mkConj_size_le (simplify a) (simplify b) conj 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)simp only [simplify, exprSize] conj 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 bomega All goals completed! 🐙| disj a b iha ihb => disj a:BExprb:BExpriha:exprSize (simplify a) ≤ exprSize aihb:exprSize (simplify b) ≤ exprSize b⊢ exprSize (simplify (a.disj b)) ≤ exprSize (a.disj b)have h := mkDisj_size_le (simplify a) (simplify b) disj 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)simp only [simplify, exprSize] disj 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 bomega All goals completed! 🐙- 연습문제 8.7.7 단순화 프로그램의 멱등성
NotLit는 리터럴이 아닌 노드에서 성립하고,CanKeepNeg는 부정을 그대로 붙일 수 있는 노드에서 성립한다.Reduced는 현재 노드와 모든 하위 식에서 단순화 규칙을 더 적용할 수 없는 경우를 표시한다. 논리곱과 논리합은 두 하위 식이 모두 이 조건을 만족하고 두 식 모두 리터럴이 아닐 때 그대로 남는다.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) :=by e:BExprh:Reduced e⊢ Reduced (mkNeg e)cases e lit a✝:Boolh:Reduced (BExpr.lit a✝)⊢ Reduced (mkNeg (BExpr.lit a✝))var a✝:Nath:Reduced (BExpr.var a✝)⊢ Reduced (mkNeg (BExpr.var a✝))neg a✝:BExprh:Reduced a✝.neg⊢ Reduced (mkNeg a✝.neg)conj a✝¹:BExpra✝:BExprh:Reduced (a✝¹.conj a✝)⊢ Reduced (mkNeg (a✝¹.conj a✝))disj a✝¹:BExpra✝:BExprh:Reduced (a✝¹.disj a✝)⊢ Reduced (mkNeg (a✝¹.disj a✝)) case neg e=> e:BExprh:Reduced e.neg⊢ Reduced (mkNeg e.neg)change Reduced e ∧ CanKeepNeg e at h e:BExprh:Reduced e ∧ CanKeepNeg e⊢ Reduced (mkNeg e.neg)change Reduced e e:BExprh:Reduced e ∧ CanKeepNeg e⊢ Reduced eexact h.1 All goals completed! 🐙 all_goalssimp_all [mkNeg, Reduced, CanKeepNeg] All goals completed! 🐙 theorem reduced_mkConj (a b : BExpr) (ha : Reduced a) (hb : Reduced b) : Reduced (mkConj a b) :=by a:BExprb:BExprha:Reduced ahb:Reduced b⊢ Reduced (mkConj a b)cases a lit b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝) b)var b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝) b)neg b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkConj a✝.neg b)conj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝¹.conj a✝) b)disj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝¹.disj a✝) b)<;> lit b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝) b)var b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝) b)neg b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkConj a✝.neg b)conj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝¹.conj a✝) b)disj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝¹.disj a✝) b)cases b disj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.lit a✝))disj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.var a✝))disj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².disj a✝¹) a✝.neg)disj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.conj a✝))disj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.disj a✝))<;> lit.lit a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.lit a✝¹) (BExpr.lit a✝))lit.var a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.lit a✝¹) (BExpr.var a✝))lit.neg a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (BExpr.lit a✝¹) a✝.neg)lit.conj a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (BExpr.lit a✝²) (a✝¹.conj a✝))lit.disj a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (BExpr.lit a✝²) (a✝¹.disj a✝))var.lit a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (BExpr.var a✝¹) (BExpr.lit a✝))var.var a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (BExpr.var a✝¹) (BExpr.var a✝))var.neg a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (BExpr.var a✝¹) a✝.neg)var.conj a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (BExpr.var a✝²) (a✝¹.conj a✝))var.disj a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (BExpr.var a✝²) (a✝¹.disj a✝))neg.lit a✝¹:BExprha:Reduced a✝¹.nega✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj a✝¹.neg (BExpr.lit a✝))neg.var a✝¹:BExprha:Reduced a✝¹.nega✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj a✝¹.neg (BExpr.var a✝))neg.neg a✝¹:BExprha:Reduced a✝¹.nega✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj a✝¹.neg a✝.neg)neg.conj a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj a✝².neg (a✝¹.conj a✝))neg.disj a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj a✝².neg (a✝¹.disj a✝))conj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².conj a✝¹) (BExpr.lit a✝))conj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².conj a✝¹) (BExpr.var a✝))conj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².conj a✝¹) a✝.neg)conj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.conj a✝²) (a✝¹.conj a✝))conj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.conj a✝²) (a✝¹.disj a✝))disj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.lit a✝))disj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkConj (a✝².disj a✝¹) (BExpr.var a✝))disj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkConj (a✝².disj a✝¹) a✝.neg)disj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.conj a✝))disj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkConj (a✝³.disj a✝²) (a✝¹.disj a✝))simp_all [mkConj, Reduced, NotLit] All goals completed! 🐙 all_goalssplit h_1 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)h_2 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)h_3 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✝)h_4 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✝¹)h_5 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✝))<;> h_1 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)h_2 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)h_3 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✝)h_4 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✝¹)h_5 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✝))simp_all [Reduced, NotLit] All goals completed! 🐙 theorem reduced_mkDisj (a b : BExpr) (ha : Reduced a) (hb : Reduced b) : Reduced (mkDisj a b) :=by a:BExprb:BExprha:Reduced ahb:Reduced b⊢ Reduced (mkDisj a b)cases a lit b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝) b)var b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝) b)neg b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkDisj a✝.neg b)conj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝¹.conj a✝) b)disj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝¹.disj a✝) b)<;> lit b:BExprhb:Reduced ba✝:Boolha:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝) b)var b:BExprhb:Reduced ba✝:Natha:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝) b)neg b:BExprhb:Reduced ba✝:BExprha:Reduced a✝.neg⊢ Reduced (mkDisj a✝.neg b)conj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝¹.conj a✝) b)disj b:BExprhb:Reduced ba✝¹:BExpra✝:BExprha:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝¹.disj a✝) b)cases b disj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.lit a✝))disj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.var a✝))disj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².disj a✝¹) a✝.neg)disj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝))disj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝))<;> lit.lit a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.lit a✝¹) (BExpr.lit a✝))lit.var a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.lit a✝¹) (BExpr.var a✝))lit.neg a✝¹:Boolha:Reduced (BExpr.lit a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (BExpr.lit a✝¹) a✝.neg)lit.conj a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (BExpr.lit a✝²) (a✝¹.conj a✝))lit.disj a✝²:Boolha:Reduced (BExpr.lit a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (BExpr.lit a✝²) (a✝¹.disj a✝))var.lit a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (BExpr.var a✝¹) (BExpr.lit a✝))var.var a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (BExpr.var a✝¹) (BExpr.var a✝))var.neg a✝¹:Natha:Reduced (BExpr.var a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (BExpr.var a✝¹) a✝.neg)var.conj a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (BExpr.var a✝²) (a✝¹.conj a✝))var.disj a✝²:Natha:Reduced (BExpr.var a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (BExpr.var a✝²) (a✝¹.disj a✝))neg.lit a✝¹:BExprha:Reduced a✝¹.nega✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj a✝¹.neg (BExpr.lit a✝))neg.var a✝¹:BExprha:Reduced a✝¹.nega✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj a✝¹.neg (BExpr.var a✝))neg.neg a✝¹:BExprha:Reduced a✝¹.nega✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj a✝¹.neg a✝.neg)neg.conj a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj a✝².neg (a✝¹.conj a✝))neg.disj a✝²:BExprha:Reduced a✝².nega✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj a✝².neg (a✝¹.disj a✝))conj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².conj a✝¹) (BExpr.lit a✝))conj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².conj a✝¹) (BExpr.var a✝))conj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².conj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².conj a✝¹) a✝.neg)conj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.conj a✝²) (a✝¹.conj a✝))conj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.conj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.conj a✝²) (a✝¹.disj a✝))disj.lit a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Boolhb:Reduced (BExpr.lit a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.lit a✝))disj.var a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:Nathb:Reduced (BExpr.var a✝)⊢ Reduced (mkDisj (a✝².disj a✝¹) (BExpr.var a✝))disj.neg a✝²:BExpra✝¹:BExprha:Reduced (a✝².disj a✝¹)a✝:BExprhb:Reduced a✝.neg⊢ Reduced (mkDisj (a✝².disj a✝¹) a✝.neg)disj.conj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.conj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.conj a✝))disj.disj a✝³:BExpra✝²:BExprha:Reduced (a✝³.disj a✝²)a✝¹:BExpra✝:BExprhb:Reduced (a✝¹.disj a✝)⊢ Reduced (mkDisj (a✝³.disj a✝²) (a✝¹.disj a✝))simp_all [mkDisj, Reduced, NotLit] All goals completed! 🐙 all_goalssplit h_1 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)h_2 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)h_3 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✝)h_4 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✝¹)h_5 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✝))<;> h_1 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)h_2 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)h_3 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✝)h_4 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✝¹)h_5 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✝))simp_all [Reduced, NotLit] All goals completed! 🐙 theorem simplify_reduced (e : BExpr) : Reduced (simplify e) :=by e:BExpr⊢ Reduced (simplify e) induction e with| lit b => lit b:Bool⊢ Reduced (simplify (BExpr.lit b))trivial All goals completed! 🐙| var n => var n:Nat⊢ Reduced (simplify (BExpr.var n))trivial All goals completed! 🐙| neg e ih => neg e:BExprih:Reduced (simplify e)⊢ Reduced (simplify e.neg)exact reduced_mkNeg _ ih All goals completed! 🐙| conj a b iha ihb => conj a:BExprb:BExpriha:Reduced (simplify a)ihb:Reduced (simplify b)⊢ Reduced (simplify (a.conj b))exact reduced_mkConj _ _ iha ihb All goals completed! 🐙| disj a b iha ihb => disj a:BExprb:BExpriha:Reduced (simplify a)ihb:Reduced (simplify b)⊢ Reduced (simplify (a.disj b))exact reduced_mkDisj _ _ iha ihb All goals completed! 🐙이제
Reduced인 식을 다시 단순화한다. 하위 식은 각각의 귀납 가정으로 원래대로 돌아온다. 보조 함수가 연산 노드를 그대로 만드는 세 정리를 적용하면 전체 식도 그대로다.e를 한 번 단순화하면 이미Reduced이므로, 그 결과에 이 고정점 성질을 적용한다.theorem mkNeg_of_canKeepNeg (e : BExpr) (h : CanKeepNeg e) : mkNeg e = .neg e :=by e:BExprh:CanKeepNeg e⊢ mkNeg e = e.negcases e lit a✝:Boolh:CanKeepNeg (BExpr.lit a✝)⊢ mkNeg (BExpr.lit a✝) = (BExpr.lit a✝).negvar a✝:Nath:CanKeepNeg (BExpr.var a✝)⊢ mkNeg (BExpr.var a✝) = (BExpr.var a✝).negneg a✝:BExprh:CanKeepNeg a✝.neg⊢ mkNeg a✝.neg = a✝.neg.negconj a✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.conj a✝)⊢ mkNeg (a✝¹.conj a✝) = (a✝¹.conj a✝).negdisj a✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.disj a✝)⊢ mkNeg (a✝¹.disj a✝) = (a✝¹.disj a✝).neg<;> lit a✝:Boolh:CanKeepNeg (BExpr.lit a✝)⊢ mkNeg (BExpr.lit a✝) = (BExpr.lit a✝).negvar a✝:Nath:CanKeepNeg (BExpr.var a✝)⊢ mkNeg (BExpr.var a✝) = (BExpr.var a✝).negneg a✝:BExprh:CanKeepNeg a✝.neg⊢ mkNeg a✝.neg = a✝.neg.negconj a✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.conj a✝)⊢ mkNeg (a✝¹.conj a✝) = (a✝¹.conj a✝).negdisj a✝¹:BExpra✝:BExprh:CanKeepNeg (a✝¹.disj a✝)⊢ mkNeg (a✝¹.disj a✝) = (a✝¹.disj a✝).negsimp_all [CanKeepNeg, mkNeg] All goals completed! 🐙 theorem mkConj_of_notLit (a b : BExpr) (ha : NotLit a) (hb : NotLit b) : mkConj a b = .conj a b :=by a:BExprb:BExprha:NotLit ahb:NotLit b⊢ mkConj a b = a.conj bcases a lit b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.lit a✝) b = (BExpr.lit a✝).conj bvar b:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.var a✝) b = (BExpr.var a✝).conj bneg b:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkConj a✝.neg b = a✝.neg.conj bconj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝¹.conj a✝) b = (a✝¹.conj a✝).conj bdisj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝¹.disj a✝) b = (a✝¹.disj a✝).conj b<;> lit b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkConj (BExpr.lit a✝) b = (BExpr.lit a✝).conj bvar b:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkConj (BExpr.var a✝) b = (BExpr.var a✝).conj bneg b:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkConj a✝.neg b = a✝.neg.conj bconj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkConj (a✝¹.conj a✝) b = (a✝¹.conj a✝).conj bdisj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkConj (a✝¹.disj a✝) b = (a✝¹.disj a✝).conj bcases b disj.lit 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✝)disj.var 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✝)disj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).conj a✝.negdisj.conj a✝³: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✝)disj.disj 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✝)<;> lit.lit 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✝)lit.var 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✝)lit.neg a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (BExpr.lit a✝¹) a✝.neg = (BExpr.lit a✝¹).conj a✝.neglit.conj a✝²: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✝)lit.disj 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✝)var.lit 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✝)var.var 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✝)var.neg a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (BExpr.var a✝¹) a✝.neg = (BExpr.var a✝¹).conj a✝.negvar.conj a✝²: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✝)var.disj 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✝)neg.lit a✝¹:BExprha:NotLit a✝¹.nega✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkConj a✝¹.neg (BExpr.lit a✝) = a✝¹.neg.conj (BExpr.lit a✝)neg.var a✝¹:BExprha:NotLit a✝¹.nega✝:Nathb:NotLit (BExpr.var a✝)⊢ mkConj a✝¹.neg (BExpr.var a✝) = a✝¹.neg.conj (BExpr.var a✝)neg.neg a✝¹:BExprha:NotLit a✝¹.nega✝:BExprhb:NotLit a✝.neg⊢ mkConj a✝¹.neg a✝.neg = a✝¹.neg.conj a✝.negneg.conj a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkConj a✝².neg (a✝¹.conj a✝) = a✝².neg.conj (a✝¹.conj a✝)neg.disj a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkConj a✝².neg (a✝¹.disj a✝) = a✝².neg.conj (a✝¹.disj a✝)conj.lit 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✝)conj.var 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✝)conj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².conj a✝¹) a✝.neg = (a✝².conj a✝¹).conj a✝.negconj.conj a✝³: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✝)conj.disj 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✝)disj.lit 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✝)disj.var 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✝)disj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkConj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).conj a✝.negdisj.conj a✝³: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✝)disj.disj 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✝)simp_all [NotLit, mkConj] All goals completed! 🐙 theorem mkDisj_of_notLit (a b : BExpr) (ha : NotLit a) (hb : NotLit b) : mkDisj a b = .disj a b :=by a:BExprb:BExprha:NotLit ahb:NotLit b⊢ mkDisj a b = a.disj bcases a lit b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.lit a✝) b = (BExpr.lit a✝).disj bvar b:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.var a✝) b = (BExpr.var a✝).disj bneg b:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkDisj a✝.neg b = a✝.neg.disj bconj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝¹.conj a✝) b = (a✝¹.conj a✝).disj bdisj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝¹.disj a✝) b = (a✝¹.disj a✝).disj b<;> lit b:BExprhb:NotLit ba✝:Boolha:NotLit (BExpr.lit a✝)⊢ mkDisj (BExpr.lit a✝) b = (BExpr.lit a✝).disj bvar b:BExprhb:NotLit ba✝:Natha:NotLit (BExpr.var a✝)⊢ mkDisj (BExpr.var a✝) b = (BExpr.var a✝).disj bneg b:BExprhb:NotLit ba✝:BExprha:NotLit a✝.neg⊢ mkDisj a✝.neg b = a✝.neg.disj bconj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.conj a✝)⊢ mkDisj (a✝¹.conj a✝) b = (a✝¹.conj a✝).disj bdisj b:BExprhb:NotLit ba✝¹:BExpra✝:BExprha:NotLit (a✝¹.disj a✝)⊢ mkDisj (a✝¹.disj a✝) b = (a✝¹.disj a✝).disj bcases b disj.lit 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✝)disj.var 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✝)disj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).disj a✝.negdisj.conj a✝³: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✝)disj.disj 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✝)<;> lit.lit 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✝)lit.var 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✝)lit.neg a✝¹:Boolha:NotLit (BExpr.lit a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (BExpr.lit a✝¹) a✝.neg = (BExpr.lit a✝¹).disj a✝.neglit.conj a✝²: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✝)lit.disj 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✝)var.lit 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✝)var.var 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✝)var.neg a✝¹:Natha:NotLit (BExpr.var a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (BExpr.var a✝¹) a✝.neg = (BExpr.var a✝¹).disj a✝.negvar.conj a✝²: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✝)var.disj 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✝)neg.lit a✝¹:BExprha:NotLit a✝¹.nega✝:Boolhb:NotLit (BExpr.lit a✝)⊢ mkDisj a✝¹.neg (BExpr.lit a✝) = a✝¹.neg.disj (BExpr.lit a✝)neg.var a✝¹:BExprha:NotLit a✝¹.nega✝:Nathb:NotLit (BExpr.var a✝)⊢ mkDisj a✝¹.neg (BExpr.var a✝) = a✝¹.neg.disj (BExpr.var a✝)neg.neg a✝¹:BExprha:NotLit a✝¹.nega✝:BExprhb:NotLit a✝.neg⊢ mkDisj a✝¹.neg a✝.neg = a✝¹.neg.disj a✝.negneg.conj a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.conj a✝)⊢ mkDisj a✝².neg (a✝¹.conj a✝) = a✝².neg.disj (a✝¹.conj a✝)neg.disj a✝²:BExprha:NotLit a✝².nega✝¹:BExpra✝:BExprhb:NotLit (a✝¹.disj a✝)⊢ mkDisj a✝².neg (a✝¹.disj a✝) = a✝².neg.disj (a✝¹.disj a✝)conj.lit 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✝)conj.var 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✝)conj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².conj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².conj a✝¹) a✝.neg = (a✝².conj a✝¹).disj a✝.negconj.conj a✝³: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✝)conj.disj 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✝)disj.lit 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✝)disj.var 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✝)disj.neg a✝²:BExpra✝¹:BExprha:NotLit (a✝².disj a✝¹)a✝:BExprhb:NotLit a✝.neg⊢ mkDisj (a✝².disj a✝¹) a✝.neg = (a✝².disj a✝¹).disj a✝.negdisj.conj a✝³: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✝)disj.disj 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✝)simp_all [NotLit, mkDisj] All goals completed! 🐙 theorem simplify_eq_self_of_reduced (e : BExpr) (h : Reduced e) : simplify e = e :=by e:BExprh:Reduced e⊢ simplify e = e induction e with| lit b => lit b:Boolh:Reduced (BExpr.lit b)⊢ simplify (BExpr.lit b) = BExpr.lit brfl All goals completed! 🐙| var n => var n:Nath:Reduced (BExpr.var n)⊢ simplify (BExpr.var n) = BExpr.var nrfl All goals completed! 🐙| neg e ih => neg e:BExprih:Reduced e → simplify e = eh:Reduced e.neg⊢ simplify e.neg = e.negrcases h with ⟨he, hn⟩ neg e:BExprih:Reduced e → simplify e = ehe:Reduced ehn:CanKeepNeg e⊢ simplify e.neg = e.negrw [ All goals completed! 🐙simplify, neg e:BExprih:Reduced e → simplify e = ehe:Reduced ehn:CanKeepNeg e⊢ mkNeg (simplify e) = e.negih he, neg e:BExprih:Reduced e → simplify e = ehe:Reduced ehn:CanKeepNeg e⊢ mkNeg e = e.negmkNeg_of_canKeepNeg e hn neg e:BExprih:Reduced e → simplify e = ehe:Reduced ehn:CanKeepNeg e⊢ e.neg = e.neg]| conj a b iha ihb => conj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bh:Reduced (a.conj b)⊢ simplify (a.conj b) = a.conj brcases h with ⟨ha, hb, hna, hnb⟩ conj 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 brw [ All goals completed! 🐙simplify, conj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkConj (simplify a) (simplify b) = a.conj biha ha, conj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkConj a (simplify b) = a.conj bihb hb, conj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkConj a b = a.conj bmkConj_of_notLit a b hna hnb conj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ a.conj b = a.conj b]| disj a b iha ihb => disj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bh:Reduced (a.disj b)⊢ simplify (a.disj b) = a.disj brcases h with ⟨ha, hb, hna, hnb⟩ disj 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 brw [ All goals completed! 🐙 theorem simplify_idempotent (e : BExpr) : simplify (simplify e) = simplify e := simplify_eq_self_of_reduced (simplify e) (simplify_reduced e)simplify, disj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkDisj (simplify a) (simplify b) = a.disj biha ha, disj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkDisj a (simplify b) = a.disj bihb hb, disj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ mkDisj a b = a.disj bmkDisj_of_notLit a b hna hnb disj a:BExprb:BExpriha:Reduced a → simplify a = aihb:Reduced b → simplify b = bha:Reduced ahb:Reduced bhna:NotLit ahnb:NotLit b⊢ a.disj b = a.disj b]- 연습문제 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 AExprmkAdd와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 :=by env:Nat → Nata:AExprb:AExpr⊢ eval env (a.mkAdd b) = eval env a + eval env bunfold mkAdd 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 bsplit h_1 env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ + b✝)) = eval env (lit a✝) + eval env (lit b✝)h_2 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 0) + eval env bh_3 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a + eval env (lit 0)h_4 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<;> h_1 env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ + b✝)) = eval env (lit a✝) + eval env (lit b✝)h_2 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 0) + eval env bh_3 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a + eval env (lit 0)h_4 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 bsimp [eval] All goals completed! 🐙 theorem eval_mkMul (env : Nat → Nat) (a b : AExpr) : eval env (mkMul a b) = eval env a * eval env b :=by env:Nat → Nata:AExprb:AExpr⊢ eval env (a.mkMul b) = eval env a * eval env bunfold mkMul 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 bsplit h_1 env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ * b✝)) = eval env (lit a✝) * eval env (lit b✝)h_2 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env (lit 0) = eval env (lit 0) * eval env bh_3 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env (lit 0) = eval env a * eval env (lit 0)h_4 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 1) * eval env bh_5 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a * eval env (lit 1)h_6 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<;> h_1 env:Nat → Natx✝¹:AExprx✝:AExpra✝:Natb✝:Nat⊢ eval env (lit (a✝ * b✝)) = eval env (lit a✝) * eval env (lit b✝)h_2 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env (lit 0) = eval env (lit 0) * eval env bh_3 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env (lit 0) = eval env a * eval env (lit 0)h_4 env:Nat → Natb:AExprx✝²:AExprx✝¹:AExprx✝:∀ (b_1 : Nat), b = lit b_1 → False⊢ eval env b = eval env (lit 1) * eval env bh_5 env:Nat → Nata:AExprx✝²:AExprx✝¹:AExprx✝:∀ (a_1 : Nat), a = lit a_1 → False⊢ eval env a = eval env a * eval env (lit 1)h_6 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 bsimp [eval] All goals completed! 🐙 theorem eval_simplify (env : Nat → Nat) (e : AExpr) : eval env (simplify e) = eval env e :=by env:Nat → Nate:AExpr⊢ eval env e.simplify = eval env e induction e with| lit n => lit env:Nat → Natn:Nat⊢ eval env (lit n).simplify = eval env (lit n)rfl All goals completed! 🐙| var i => var env:Nat → Nati:Nat⊢ eval env (var i).simplify = eval env (var i)rfl All goals completed! 🐙| add a b iha ihb => add 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)simp [simplify, eval, eval_mkAdd, iha, ihb] All goals completed! 🐙| mul a b iha ihb => mul 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)simp [simplify, eval, eval_mkMul, iha, ihb] All goals completed! 🐙 #eval simplify (.mul (.add (.lit 2) (.lit 3)) (.lit 1)) end AExpr