逐步主鏈
S01 · definition
操作前
S=\{123,132,213,231,312,321\},\ \mathrm{Init}=\{321\},\ \mathrm{Halt}=\{123\}操作後
\mathrm{Init},\mathrm{Halt}\subseteq S,\ \mathrm{Step}\subseteq S\times S,\ |S|=3!=6條件:無
規則:def_intro · 依賴:G1
葉節點依據:def_intro + subset_def + cartesian_product_def + finite_counting_basics
確認 M 是 MKO 意義下的有限模型。321 與 123 都在 S 中。交換一個排列的兩項,所得仍是 1、2、3 的排列,所以每條邊的兩端都在 S 中,Step⊆S×S。S 恰有 3!=6 個元素。123 沒有相鄰逆序對,所以本來就沒有外出邊,與「Halt 為終點」一致。
S02 · definition
操作前
M,\ Q
操作後
\mathrm{PC}:\ \text{every halt reached satisfies}\ Q;\quad \mathrm{T}:\ \text{every maximal execution is finite and ends in Halt};\quad \mathrm{TC}=\mathrm{PC}\wedge\mathrm{T}條件:無
規則:def_unfold · 依賴:G2, G4, mko_algorithm_correctness_def
葉節點依據:def_unfold + mko_algorithm_correctness_def
依 MKO 的部分正確/總正確區分寫出責任,並把終止性精確量化於極大執行。PC:從 Init 出發、到達 Halt 的每條執行,其末狀態滿足 Q。T:從 Init 出發的每條極大執行都有限且末狀態屬於 Halt,等價於沒有無窮執行,也沒有停在非 Halt 狀態的有限極大執行。有限非極大前綴不要求已到 Halt。TC 是 PC 與 T 的合取,所有非確定選擇都必須涵蓋(G4)。
S03 · theorem_application
操作前
s\in S\setminus\mathrm{Halt}操作後
x_1>x_2\ \vee\ x_2>x_3,\ \text{so}\ \mathrm{Post}(s)\ne\varnothing條件:無
規則:L_succ · 依賴:G1
葉節點依據:contrapositive + order_basics
套用引理 L_succ(在 S03.1、S03.2 證明):不在 Halt 的狀態一定有相鄰逆序對,因此有後繼,不會卡住。這是終止性論證中排除「卡住」的部分。
S04 · definition
操作前
s=(x_1,x_2,x_3)
操作後
\mathrm{inv}(s)=[x_1>x_2]+[x_1>x_3]+[x_2>x_3]\ge 0;\ \mathrm{inv}(321)=3,\ \mathrm{inv}(231)=\mathrm{inv}(312)=2,\ \mathrm{inv}(213)=\mathrm{inv}(132)=1,\ \mathrm{inv}(123)=0條件:無
規則:def_intro · 依賴:G1
葉節點依據:def_intro + numeric_evaluation
定義排名函數 inv(s):位置對 (i,j)、i<j 中 x_i>x_j 的個數。它是三個 0 或 1 的和,所以是 0 到 3 的自然數。逐一計算六個狀態的值(N2 到 N7 可重算)。
S05 · theorem_application
操作前
(s,s')\in\mathrm{Step}操作後
\mathrm{inv}(s')=\mathrm{inv}(s)-1條件:無
規則:L_rank · 依賴:S04, G1
葉節點依據:def_unfold + add_comm + numeric_evaluation
套用引理 L_rank(在 S05.1、S05.2 對兩種交換分別證明):每一步都使逆序數恰好減 1。這是對每一種邊的證明,不是只在幾個樣本狀態上觀察。
S06 · induction_base
操作前
(t_k)\ \text{an execution of}\ M,\ t_0\in\mathrm{Init}操作後
\mathrm{inv}(t_0)=\mathrm{inv}(321)=3條件:無
規則:nat_induction · 依賴:S04, G1
葉節點依據:nat_induction + numeric_evaluation
考慮任意一條執行 (t_k)。t0∈Init={321},所以 inv(t0)=3。這是不變量 inv(t_k)=3−k 的歸納基底(k=0)。
S07 · induction_step
操作前
\mathrm{inv}(t_k)=3-k,\ (t_k,t_{k+1})\in\mathrm{Step}操作後
\mathrm{inv}(t_{k+1})=(3-k)-1=3-(k+1)條件:無
規則:nat_induction · 依賴:S06, S05
葉節點依據:nat_induction + eq_replace + add_assoc
設 inv(t_k)=3−k,且執行還有下一步 t_{k+1}。依 L_rank,inv(t_{k+1})=inv(t_k)−1=(3−k)−1=3−(k+1)。所以對執行中的每個索引 k,都有 inv(t_k)=3−k。不變量對每一步都證明了保持,不論在非確定的分岔中選了哪一條邊。
S08 · implies
操作前
\mathrm{inv}(t_k)=3-k\ge 0操作後
k\le 3:\ \text{every execution has at most 3 steps; no infinite execution}條件:無
規則:order_basics · 依賴:S07, S04
葉節點依據:order_basics + nat_induction
inv 的值不小於 0,所以執行中任何索引 k 都滿足 3−k≥0,即 k≤3。因此每條執行至多有 4 個狀態、3 步,不存在無窮執行。這是排名函數論證:一個非負整數值每步嚴格下降,不能無限下降。
S09 · implies
操作前
\text{every execution is finite}操作後
\text{every maximal execution ends in Halt:}\ \mathrm{T}條件:無
規則:contrapositive · 依賴:S08, S03
葉節點依據:contrapositive + mko_computational_model_def
由 S08,每條執行有限;只要末狀態還有後繼就可以延長,延長至多到 3 步,所以每條執行都可延伸成極大執行,其末狀態沒有後繼。依 L_succ 的逆否(S03),沒有後繼的狀態必在 Halt。所以每條從 Init 出發的極大執行都有限並以 Halt 結束;每條有限前綴亦可延伸到這樣的停止執行。依 S02,終止性 T 成立。
S10 · implies
操作前
t_{\mathrm{last}}\in\mathrm{Halt}=\{123\}操作後
Q(123):\ 1<2<3\ \wedge\ \{1,2,3\}=\{1,2,3\}\ \Rightarrow\ \mathrm{PC}條件:無
規則:mko_algorithm_correctness_def · 依賴:S01, G1, G2
葉節點依據:mko_algorithm_correctness_def + order_basics
任何到達 Halt 的執行,末狀態都是 123。123 遞增,而且其項目是輸入 321 的項目的重排(S01:每個狀態都是 1、2、3 的排列,交換不改變項目)。所以 Q 在每個可到達的停止狀態成立,部分正確性 PC 成立。
S11 · implies
操作前
\mathrm{PC},\ \mathrm{T}操作後
\mathrm{TC}=\mathrm{PC}\wedge\mathrm{T}\ \text{holds for}\ M條件:無
規則:and_intro_elim · 依賴:S09, S10, S02
葉節點依據:and_intro_elim + mko_algorithm_correctness_def
合取 S10 的 PC 與 S09 的 T,依 S02 的定義得總正確性:從 321 出發的每條極大執行都終止,且終止時輸出遞增的重排 123。
S12 · implies
操作前
\text{reachable graph from}\ 321操作後
321\to 231\to 213\to 123,\quad 321\to 312\to 132\to 123
條件:無
規則:mko_computational_model_def · 依賴:G1, S11
葉節點依據:mko_computational_model_def + case_analysis
有限模型可用完整可達圖交叉核對。321 有兩個後繼:交換前兩項得 231,交換後兩項得 312。231 只有後兩項逆序,得 213;312 只有前兩項逆序,得 132;213 得 123;132 得 123。所以恰有兩條極大執行,各 3 步,等於 inv(321),都停在 123。這與 S07 到 S11 一致;但一般性證明來自 S05 到 S10,不依賴這張圖。
S13 · implies
操作前
M':\ 321\to 231\to 213\to 123\ \text{exists}操作後
t_{2k}=321,\ t_{2k+1}=231\ \text{is infinite}\ \Rightarrow\ \neg\mathrm{T},\ \neg\mathrm{TC}\ \text{for}\ M'條件:無
規則:quantifier_negation · 依賴:G3, S02
葉節點依據:quantifier_negation + case_analysis + mko_algorithm_correctness_def
在 M′ 中,成功路徑 321→231→213→123 仍存在,而且 Halt 仍是 {123},所以 PC 照樣成立。但 M′ 允許交換非逆序對:321 交換前兩項得 231,231 交換前兩項又回到 321,兩者都不在 Halt。序列 t_{2k}=321、t_{2k+1}=231 是合法的無窮執行,T 不成立,TC 不成立。一條成功路徑與 PC 都不足以得到總正確(MKO 誤解)。
S14 · implies
操作前
M'':\ \mathrm{Halt}''=\{s\mid x_1<x_2\}=\{123,132,231\}操作後
321\to 231\in\mathrm{Halt}'',\ \neg Q(231)\ \Rightarrow\ \neg\mathrm{PC},\ \neg\mathrm{TC}\ \text{for}\ M''條件:無
規則:quantifier_negation · 依賴:G3, G2, S02
葉節點依據:quantifier_negation + mko_algorithm_correctness_def
在 M″ 中,從 321 出發的極大執行只有兩條:321→231(231 的 2<3,屬於 Halt″,停止)與 321→312→132(132 屬於 Halt″,停止)。兩條都停止,所以 T 成立;但停止狀態 231 不遞增(3>1),Q(231) 為假,PC 不成立,TC 也不成立。終止本身不保證正確。
局部子步與引理
S03.1 · implies
操作前
\neg(x_1>x_2)\ \wedge\ \neg(x_2>x_3)
操作後
x_1<x_2<x_3
條件:x_1,x_2,x_3\ \text{distinct}
規則:order_basics · 依賴:order_basics, contrapositive
葉節點依據:order_basics + contrapositive
改證逆否命題:若沒有相鄰逆序對,則 s∈Halt。排列的三項互不相同,依三分律「不是 x1>x2」且 x1≠x2 給出 x1<x2;同理 x2<x3。所以 x1<x2<x3。
S03.2 · implies
操作前
x_1<x_2<x_3,\ \{x_1,x_2,x_3\}=\{1,2,3\}操作後
(x_1,x_2,x_3)=(1,2,3)\in\mathrm{Halt}條件:無
規則:order_basics · 依賴:S03.1, order_basics
葉節點依據:order_basics + contrapositive
x1 小於另外兩項(x1<x2,且依傳遞 x1<x3),所以 x1 是 {1,2,3} 的最小元 1;x3 大於另外兩項,所以 x3=3;剩下 x2=2。因此 s=123∈Halt。S03.1 與 S03.2 合起來,以逆否方式證得 L_succ。
S05.1 · implies
操作前
x_1>x_2,\ s'=(x_2,x_1,x_3)
操作後
\mathrm{inv}(s')=0+[x_2>x_3]+[x_1>x_3]=\mathrm{inv}(s)-1條件:x_1>x_2
規則:def_unfold · 依賴:S04, G1
葉節點依據:def_unfold + add_comm + order_basics
交換前兩項:inv(s′)=[x2>x1]+[x2>x3]+[x1>x3]。因 x1>x2,[x2>x1]=0。而 inv(s)=[x1>x2]+[x1>x3]+[x2>x3]=1+[x1>x3]+[x2>x3]。其餘兩項只是交換了位置(加法交換律),所以 inv(s′)=inv(s)−1。
S05.2 · implies
操作前
x_2>x_3,\ s'=(x_1,x_3,x_2)
操作後
\mathrm{inv}(s')=[x_1>x_3]+[x_1>x_2]+0=\mathrm{inv}(s)-1條件:x_2>x_3
規則:def_unfold · 依賴:S04, G1
葉節點依據:def_unfold + add_comm + order_basics + case_analysis
交換後兩項:inv(s′)=[x1>x3]+[x1>x2]+[x3>x2]。因 x2>x3,[x3>x2]=0,而 inv(s)=[x1>x2]+[x1>x3]+1。所以 inv(s′)=inv(s)−1。S05.1 與 S05.2 涵蓋 M 的兩種邊,證得 L_rank。