8.6.3. 삽입 함수의 정확성 증명
정렬 함수 전체를 증명하기 전에 insertNat x xs가 보장하는 성질을 정한다. 반환값은 x :: xs와 순열 관계이고, xs가 정렬되어 있으면 반환값도 정렬되어야 한다. 8.3절의 사전조건과 사후조건으로 보면, 삽입의 정렬 보존에는 Sorted xs라는 사전조건이 있다. 순열 조건은 모든 xs에 성립하며, 정렬 보존에 필요한 보조 정리에도 쓰인다.
삽입 결과의 순열 조건
xs에 대해 귀납법을 적용한다. 빈 리스트에서 insertNat x []와 x :: []는 둘 다 [x]다. 머리 y가 있고 x ≤ y이면 삽입이 맨 앞에서 끝나므로 두 리스트가 같다. Lean은 rw로 조건식을 정리한 뒤 이 분기를 닫는다.
x ≤ y가 성립하지 않으면 삽입 결과는 y :: insertNat x ys다. 그림 8.27에서처럼 귀납 가정에 머리 y를 붙여 y :: x :: ys까지 연결한다. List.Perm.swap x y ys로 맨 앞의 두 원소를 바꾸면 목표인 x :: y :: ys에 도달한다.
theorem insert_perm (x : Nat) (xs : List Nat) :
(insertNat x xs).Perm (x :: xs) := x:Natxs:List Nat⊢ (insertNat x xs).Perm (x :: xs)
induction xs with
x:Nat⊢ (insertNat x []).Perm [x] All goals completed! 🐙
x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)⊢ (insertNat x (y :: ys)).Perm (x :: y :: ys)
x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:x ≤ y⊢ (insertNat x (y :: ys)).Perm (x :: y :: ys)x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:¬x ≤ y⊢ (insertNat x (y :: ys)).Perm (x :: y :: ys)
x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:x ≤ y⊢ (insertNat x (y :: ys)).Perm (x :: y :: ys) All goals completed! 🐙
· neg x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:¬x ≤ y⊢ (insertNat x (y :: ys)).Perm (x :: y :: ys) rw [insertNat, neg x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:¬x ≤ y⊢ (if x ≤ y then x :: y :: ys else y :: insertNat x ys).Perm (x :: y :: ys) ite_eq_right h neg x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:¬x ≤ y⊢ (y :: insertNat x ys).Perm (x :: y :: ys)] neg x:Naty:Natys:List Natih:(insertNat x ys).Perm (x :: ys)h:¬x ≤ y⊢ (y :: insertNat x ys).Perm (x :: y :: ys)
exact (List.Perm.cons y ih).trans
(List.Perm.swap x y ys) All goals completed! 🐙
삽입에서 하한 보존하기
x ≤ y가 성립하지 않는 분기에서 y :: insertNat x ys가 정렬되어 있음을 보이려면 무엇이 더 필요할까? 꼬리에 대한 귀납 가정으로 insertNat x ys가 정렬되었다는 사실을 얻을 수 있다. 남은 목표는 y가 새 꼬리의 모든 원소 이하라는 것이다. 예를 들어 [2, 4]에 3을 삽입할 때 새 꼬리 [3, 4]가 정렬되었다는 사실과 함께, 두 원소가 모두 2 이상임을 보여야 한다.
리스트 원소들의 하한(lower bound)은 모든 원소 이하인 값이다. 삽입 전의 머리 y는 꼬리 ys의 하한이고 새 값 x도 y 이상이다. 이를 일반화해 a ≤ x이고 a가 xs의 하한이면 삽입한 리스트 insertNat x xs에서도 a가 하한이라는 명제를 세운다.
삽입 결과에 들어 있는 y를 잡고 insert_perm을 적용한다. 순열 관계 h에서 h.mem_iff는 두 리스트에 대한 포함 관계가 동치임을 준다. 따라서 y ∈ x :: xs를 얻고, List.mem_cons.mp로 y = x와 y ∈ xs의 두 경우를 나눈다. 각 경우에 처음 받은 하한 가정을 하나씩 쓴다.
theorem insert_preserves_lowerBound
(a x : Nat) (xs : List Nat)
(hx : a ≤ x) (hxs : ∀ y, y ∈ xs → a ≤ y) :
∀ y, y ∈ insertNat x xs → a ≤ y := by a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ y⊢ ∀ (y : Nat), y ∈ insertNat x xs → a ≤ y
intro y hy a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xs⊢ a ≤ y
have hm : y ∈ x :: xs := (insert_perm x xs).mem_iff.mp hy a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xs⊢ a ≤ y
rcases List.mem_cons.mp hm with hyx | hmem inl a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshyx:y = x⊢ a ≤ yinr a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshmem:y ∈ xs⊢ a ≤ y
· inl a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshyx:y = x⊢ a ≤ y rw [hyx inl a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshyx:y = x⊢ a ≤ x] inl a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshyx:y = x⊢ a ≤ x
exact hx All goals completed! 🐙
· inr a:Natx:Natxs:List Nathx:a ≤ xhxs:∀ (y : Nat), y ∈ xs → a ≤ yy:Nathy:y ∈ insertNat x xshm:y ∈ x :: xshmem:y ∈ xs⊢ a ≤ y exact hxs y hmem All goals completed! 🐙
삽입 결과의 정렬 조건
빈 리스트에 삽입한 결과는 원소가 하나이므로 정렬되어 있다. y :: ys가 정렬되어 있다는 가정에 List.pairwise_cons.mp를 쓰면, 머리 y가 꼬리의 모든 원소 이하라는 hhead와 꼬리도 정렬되어 있다는 htail을 얻는다. 역으로 List.Pairwise.cons는 머리의 하한 조건과 정렬된 꼬리의 증명을 받아 전체 리스트가 정렬되어 있음을 보인다.
#check List.pairwise_cons
#check List.Pairwise.cons
그림 8.28처럼 삽입이 끝나는 위치에 따라 새 머리를 정한다. x ≤ y이면 새 머리는 x다. z가 원래 리스트의 머리 y이면 x ≤ y를 쓰고, 꼬리 ys의 원소이면 x ≤ y ≤ z의 추이성을 쓴다. 꼬리인 y :: ys는 이미 가정 h에 따라 정렬되어 있다.
x ≤ y가 성립하지 않으면 머리 y가 그대로 남는다. 이때 y < x에서 y ≤ x를 얻는다. y는 x와 ys의 모든 원소 이하이므로 하한 보존 정리로 새 꼬리에서도 하한이다. 또 새 꼬리 insertNat x ys는 ih htail에 따라 정렬되어 있다.
theorem insert_sorted
(x : Nat) (xs : List Nat) (h : Sorted xs) :
Sorted (insertNat x xs) := by x:Natxs:List Nath:Sorted xs⊢ Sorted (insertNat x xs)
induction xs with
| nil => nil x:Nath:Sorted []⊢ Sorted (insertNat x []) simp [insertNat, Sorted] All goals completed! 🐙
| cons y ys ih => cons x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)⊢ Sorted (insertNat x (y :: ys))
rcases List.pairwise_cons.mp h with ⟨hhead, htail⟩ cons x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) ys⊢ Sorted (insertNat x (y :: ys))
by_cases hxy : x ≤ y pos x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ Sorted (insertNat x (y :: ys))neg x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:¬x ≤ y⊢ Sorted (insertNat x (y :: ys))
· pos x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ Sorted (insertNat x (y :: ys)) rw [insertNat, pos x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ Sorted (if x ≤ y then x :: y :: ys else y :: insertNat x ys) ite_eq_left hxy pos x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ Sorted (x :: y :: ys)] pos x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ Sorted (x :: y :: ys)
apply List.Pairwise.cons _ h x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ y⊢ ∀ (a' : Nat), a' ∈ y :: ys → x ≤ a'
intro z hz x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: ys⊢ x ≤ z
rcases List.mem_cons.mp hz with heq | hmem inl x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≤ zinr x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: yshmem:z ∈ ys⊢ x ≤ z
· inl x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≤ z rw [heq inl x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≤ y] inl x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: ysheq:z = y⊢ x ≤ y
exact hxy All goals completed! 🐙
· inr x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:x ≤ yz:Nathz:z ∈ y :: yshmem:z ∈ ys⊢ x ≤ z exact Nat.le_trans hxy (hhead z hmem) All goals completed! 🐙
· neg x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:¬x ≤ y⊢ Sorted (insertNat x (y :: ys)) rw [insertNat, neg x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:¬x ≤ y⊢ Sorted (if x ≤ y then x :: y :: ys else y :: insertNat x ys) ite_eq_right hxy neg x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:¬x ≤ y⊢ Sorted (y :: insertNat x ys)] neg x:Naty:Natys:List Natih:Sorted ys → Sorted (insertNat x ys)h:Sorted (y :: ys)hhead:∀ (a' : Nat), a' ∈ ys → y ≤ a'htail:List.Pairwise (fun x1 x2 => x1 ≤ x2) yshxy:¬x ≤ y⊢ Sorted (y :: insertNat x ys)
exact List.Pairwise.cons
(insert_preserves_lowerBound y x ys
(Nat.le_of_lt (Nat.lt_of_not_ge hxy)) hhead)
(ih htail) All goals completed! 🐙
여기서는 h : Sorted xs가 귀납 대상인 xs에 의존한다. Lean은 꼬리에 대한 귀납 가정에도 그 가정을 인수로 남긴다. 그래서 ih는 htail : Sorted ys를 받아 꼬리에 삽입한 결과가 정렬되었음을 보인다.
- 연습문제 8.6.5 삽입 후 리스트의 길이
(insertNat x xs).length = xs.length + 1을insertNat_length라는 이름으로 증명한다.xs에 귀납법을 적용한 뒤x ≤ y에 따라 분기를 나눈다. 이어insert_perm에서List.Perm.length_eq를 사용해 같은 결론을 얻을 수 있는지 확인한다.