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

8.5.4. 완전성 증명🔗

완전성에서는 대상이 리스트에 있다고 가정하고 linearSearch target xs = true를 보인다. 빈 리스트에는 원소가 없으므로 기초 단계는 simp로 끝난다. cons 분기에서 머리가 대상과 같으면 비교식이 참이 되어 바로 true를 반환한다.

머리가 대상과 다르면 List.mem_cons.mp h로 포함 가정을 논리합으로 펼친다. target = x인 경우는 heq : x ≠ target과 모순이므로, target ∈ xs를 얻는다. 그림 8.21처럼 그 증명을 귀납 가정에 넘기면 꼬리 탐색의 성공을 얻는다. 현재 분기에서 전체 리스트의 탐색은 꼬리의 탐색으로 계산된다.

포함을 가정한 cons 분기에서 머리가 대상과 같으면 즉시 true를 반환한다. 머리가 다르면 포함 가정에서 꼬리에 대상이 있음을 얻고, 귀납 가정으로 꼬리의 탐색이 성공함을 보인다.
그림 8.21 원소의 포함 관계로부터 탐색의 성공을 얻는 두 경우
theorem linearSearch_complete (target : Nat) (xs : List Nat) : target ∈ xs → linearSearch target xs = true := target:Natxs:List Nat⊢ target ∈ xs → linearSearch target xs = true induction xs with target:Nat⊢ target ∈ [] → linearSearch target [] = true All goals completed! 🐙 target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = true⊢ target ∈ x :: xs → linearSearch target (x :: xs) = true target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xs⊢ linearSearch target (x :: xs) = true target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xsheq:x = target⊢ linearSearch target (x :: xs) = truetarget:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xsheq:¬x = target⊢ linearSearch target (x :: xs) = true target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xsheq:x = target⊢ linearSearch target (x :: xs) = true All goals completed! 🐙 target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xsheq:¬x = target⊢ linearSearch target (x :: xs) = true target:Natx:Natxs:List Natih:target ∈ xs → linearSearch target xs = trueh:target ∈ x :: xsheq:¬x = targethm:target ∈ xs⊢ linearSearch target (x :: xs) = true All goals completed! 🐙

건전성 증명에서는 꼬리의 탐색 성공 ht를 귀납 가정에 넘겼다. 완전성에서는 꼬리에 대상이 포함된다는 증명 hm을 넘긴다. 6.3.3절에서처럼 동치의 양쪽 방향을 constructor로 나누면 이미 증명한 두 정리를 하나씩 쓸 수 있다. 그림 8.22의 아래 동치가 linearSearch_correct의 결론이다.

theorem linearSearch_correct (target : Nat) (xs : List Nat) : SearchSpec target xs (linearSearch target xs) := target:Natxs:List Nat⊢ SearchSpec target xs (linearSearch target xs) target:Natxs:List Nat⊢ linearSearch target xs = true ↔ target ∈ xs target:Natxs:List Nat⊢ linearSearch target xs = true → target ∈ xstarget:Natxs:List Nat⊢ target ∈ xs → linearSearch target xs = true target:Natxs:List Nat⊢ linearSearch target xs = true → target ∈ xs All goals completed! 🐙 target:Natxs:List Nat⊢ target ∈ xs → linearSearch target xs = true All goals completed! 🐙
건전성은 탐색의 성공에서 포함을, 완전성은 포함에서 탐색의 성공을 얻는다. 두 증명을 동치의 각각의 방향에 적용하면 선형 탐색의 정확성이 성립한다.
그림 8.22 건전성과 완전성을 결합한 선형 탐색의 정확성 증명

동치 전체에 한 번 귀납법을 적용할 수도 있다. 아래 코드의 마지막 줄에서는 Ne.symm h로 target = x인 경우를 제외한다. 그러면 남은 동치가 꼬리의 귀납 가정 ih와 같다.

example (target : Nat) (xs : List Nat) : linearSearch target xs = true ↔ target ∈ xs := target:Natxs:List Nat⊢ linearSearch target xs = true ↔ target ∈ xs induction xs with target:Nat⊢ linearSearch target [] = true ↔ target ∈ [] All goals completed! 🐙 target:Natx:Natxs:List Natih:linearSearch target xs = true ↔ target ∈ xs⊢ linearSearch target (x :: xs) = true ↔ target ∈ x :: xs target:Natx:Natxs:List Natih:linearSearch target xs = true ↔ target ∈ xsh:x = target⊢ linearSearch target (x :: xs) = true ↔ target ∈ x :: xstarget:Natx:Natxs:List Natih:linearSearch target xs = true ↔ target ∈ xsh:¬x = target⊢ linearSearch target (x :: xs) = true ↔ target ∈ x :: xs target:Natx:Natxs:List Natih:linearSearch target xs = true ↔ target ∈ xsh:x = target⊢ linearSearch target (x :: xs) = true ↔ target ∈ x :: xs All goals completed! 🐙 target:Natx:Natxs:List Natih:linearSearch target xs = true ↔ target ∈ xsh:¬x = target⊢ linearSearch target (x :: xs) = true ↔ target ∈ x :: xs All goals completed! 🐙

두 방향을 나누는 방법은 다른 판정에도 적용할 수 있다. 소수 판별이라면 소수라고 반환한 수가 실제로 소수인지, 모든 소수에서 그 결과를 반환하는지를 각각 증명한다. 8.6절에서는 정렬 결과가 순서대로 나열되는지와 입력 원소의 출현 횟수가 보존되는지를 별도로 증명한 뒤 두 성질을 논리곱으로 결합한다.

그림 8.23에서 비교 횟수는 처음 발견한 위치에 따라 달라진다. 길이가 8인 리스트에서 첫 번째 원소가 대상이면 한 번, 마지막 원소가 대상이거나 대상이 없으면 여덟 번 비교한다. 빈 리스트에서는 원소 비교가 없다. 9장에서는 입력 길이와 비교 횟수의 관계를 분석한다.

길이가 8인 리스트에서 첫째 원소를 찾으면 한 번, 넷째 원소를 찾으면 네 번 비교한다. 마지막 원소를 찾거나 대상이 없으면 여덟 번 비교한다.
그림 8.23 발견 위치와 탐색 성공 여부에 따른 원소 비교 횟수
연습문제 8.5.4 탐색 실패의 동치

linearSearch target xs = false ↔ target ∉ xs를 증명한다. linearSearch_correct가 주는 동치에서 양쪽을 부정하고, 불리언 값이 true가 아닌 경우를 false와의 등식으로 표현한다.

연습문제 8.5.5 짝수 탐색의 정확성

hasEven xs = true와 ∃ x, x ∈ xs ∧ x % 2 = 0가 동치임을 증명한다. 첫 원소가 짝수인 분기에서는 그 원소를 존재 명제의 증인으로 삼을 수 있다.

def hasEven : List Nat → Bool | [] => false | x :: xs => if x % 2 == 0 then true else hasEven xs
연습문제 8.5.6 모든 원소의 짝수 여부 검사

allEven xs = true와 ∀ x, x ∈ xs → x % 2 = 0이 동치임을 증명한다. 빈 리스트에서 hasEven과 allEven의 결과가 다른 이유를 각 명세의 한정사로 설명한다.

def allEven : List Nat → Bool | [] => true | x :: xs => if x % 2 == 0 then allEven xs else false
연습문제 8.5.7 위치를 반환하는 선형 탐색

선택 실습이다. findIndex가 some i를 반환하면 xs[i]? = some target이 성립하고, none을 반환하면 target ∉ xs가 성립함을 명세로 적고 증명한다. 이 명세가 대상이 포함된 입력에서 some의 반환을 어떻게 보장하는지 설명한다. 처음 발견한 위치까지 명세하려면 i보다 작은 인덱스에 어떤 조건을 추가해야 하는지도 적는다.

def findIndex (target : Nat) : List Nat → Option Nat | [] => none | x :: xs => if x == target then some 0 else match findIndex target xs with | none => none | some i => some (i + 1)