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

9.6.4. 계산 비용의 상한 증명🔗

길이가 n+1인 리스트를 정렬할 때, 꼬리를 먼저 정렬하는 데 든 비용에는 길이 n의 상한 B(n)을 적용한다. 남은 삽입은 길이가 n인 정렬 결과에 새 원소를 넣으므로 비교 비용이 n 이하이다. 이를 더하면 B(n+1)=B(n)+n이다. 그림 9.19에서 길이가 6이면 각 재귀 단계에 더하는 상한은 0부터 5까지다.

꼬리 정렬에 필요한 비용의 상한 B(n)에 현재 원소를 넣는 비용의 상한 n을 더한다. 길이 6에서 꼬리 길이별 비용 상한은 0, 1, 2, 3, 4, 5이며 합은 15다.
그림 9.19 꼬리 정렬과 삽입의 비용을 합쳐 얻는 상한

insertCost_le_length를 적용하면 삽입 비용은 (insertionSort xs).length 이하다. 여기서 연습문제 8.6.6에서 보았던 길이 보존이 필요하다. 아래 insertionSort_length는 8장의 순열 정리에서 .length_eq를 바로 얻는 방식이다. 삽입의 상한을 xs.length로 다시 쓴 뒤, Nat.add_le_add로 꼬리 정렬의 귀납 가정과 합친다.

def insertionSortBound : Nat → Nat | 0 => 0 | n + 1 => insertionSortBound n + n theorem insertionSort_length (xs : List Nat) : (insertionSort xs).length = xs.length := (insertionSort_perm xs).length_eq theorem insertionSortCost_le_bound (xs : List Nat) : insertionSortCost xs ≤ insertionSortBound xs.length := xs:List Nat⊢ insertionSortCost xs ≤ insertionSortBound xs.length induction xs with ⊢ insertionSortCost [] ≤ insertionSortBound [].length All goals completed! 🐙 x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ (insertionSort xs).length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost (x :: xs) ≤ insertionSortBound (x :: xs).length x:Natxs:List Natih:insertionSortCost xs ≤ insertionSortBound xs.lengthh:insertCost x (insertionSort xs) ≤ xs.length⊢ insertionSortCost xs + insertCost x (insertionSort xs) ≤ insertionSortBound xs.length + xs.length All goals completed! 🐙

이 절의 상한은 n + 1에 도달할 때 n을 더한다. 9.3.1절의 reverseCostBySize는 같은 지점에서 n + 1을 더했다. 따라서 B(n+1)은 뒤집기의 T(n)과 같다. 이를 insertionSortBound_succ로 증명한 뒤 9.3절의 삼각수 공식에 연결한다. 길이가 0일 때는 양변이 0이고, n + 1일 때는 (n + 1) - 1이 n으로 줄어든다.

theorem insertionSortBound_succ (n : Nat) : insertionSortBound (n + 1) = reverseCostBySize n := n:Nat⊢ insertionSortBound (n + 1) = reverseCostBySize n induction n with ⊢ insertionSortBound (0 + 1) = reverseCostBySize 0 All goals completed! 🐙 n:Natih:insertionSortBound (n + 1) = reverseCostBySize n⊢ insertionSortBound (n + 1 + 1) = reverseCostBySize (n + 1) n:Natih:insertionSortBound (n + 1) = reverseCostBySize n⊢ insertionSortBound (n + 1) + (n + 1) = reverseCostBySize n + (n + 1) All goals completed! 🐙 theorem insertionSortBound_formula (n : Nat) : 2 * insertionSortBound n = n * (n - 1) := n:Nat⊢ 2 * insertionSortBound n = n * (n - 1) cases n with ⊢ 2 * insertionSortBound 0 = 0 * (0 - 1) All goals completed! 🐙 n:Nat⊢ 2 * insertionSortBound (n + 1) = (n + 1) * (n + 1 - 1) n:Nat⊢ 2 * reverseCostBySize n = (n + 1) * (n + 1 - 1) All goals completed! 🐙 theorem insertionSortBound_eq_div (n : Nat) : insertionSortBound n = n * (n - 1) / 2 := n:Nat⊢ insertionSortBound n = n * (n - 1) / 2 n:Nat⊢ insertionSortBound n = 2 * insertionSortBound n / 2 All goals completed! 🐙 theorem insertionSortCost_le_formula (xs : List Nat) : insertionSortCost xs ≤ xs.length * (xs.length - 1) / 2 := xs:List Nat⊢ insertionSortCost xs ≤ xs.length * (xs.length - 1) / 2 xs:List Nat⊢ insertionSortCost xs ≤ insertionSortBound xs.length All goals completed! 🐙

따라서 길이가 n인 모든 입력에서 원소 비교 횟수는 n(n-1)/2 이하이다. 이제 각 길이에서 그 횟수를 모두 쓰게 만드는 입력을 찾는다.