Program Verification - 程序验证
本页目录
Hoare Logic
运行命令式程序(imperative program)可以描述为状态(state)的改变。
Requirements for a program (程序需求):自然语言的描述。
Specification for a program (程序规范):形式化的描述。
- Precondition(前置条件):程序运行前的状态。
- Postcondtion (后置条件):程序运行后的状态。
这些 Specification 是在 FOL 下,用 Hoare Triples(霍尔三元组)描述的。
Hoare Triple 形如:
(\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!)
其中 P,Q 为前置/后置条件,C 为要检验的代码。
例如,“将 y 设置为 \max(x,y)” 的 requirement 可写成这样的 specification:
(\!\lvert x=a\land y=b \rvert \!) \; C \; (\!\lvert y=\max(a,b) \rvert \!)
定义一个 Hoare Triple (\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!) 在 Partial Correctness 下 Satisfied,当且仅当:
- 对任意的满足
P的状态s_{1},如果执行C后终止在状态s_{2},则s_{2}满足Q。
记作
\vDash _{par} (\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!)
如果永远不会 Terminate ,也说是 Satisfied。(这也满足定义,不过空真)
我们称一个程序是 Partially Correct,如果它终止时总是给出了正确答案。
定义一个 Hoare Triple (\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!) 在 Total Correctness 下 Satisfied,当且仅当:
- 对任意的满足
P的状态s_{1},执行C后,会终止在状态s_{2},且s_{2}满足Q。
记作
\vDash _{\mathrm{tot}} (\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!)
Total correctness = Partial correctness + Termination
Program Variables / Logic Variables:因为程序中的变量会变,在 Hoare Triple 中,我们用 Fresh variable 来表示某些条件,例如:
(\!\lvert x=x_{0} \land x \geq 0 \rvert \!) \; C \; (\!\lvert y=x_{0}! \rvert \!)
Axioms and Rules
要验证一个程序,需要构造一个证明序列:
\begin{array}{ll}
(\!\lvert \phi_0\rvert\!) & \\
C_1; & \\
\quad (\!\lvert \phi_1\rvert\!) & \texttt{justification} \\
C_2; & \\
\quad\vdots & \\
\quad (\!\lvert \phi_{n-1}\rvert\!) & \texttt{justification} \\
C_n; & \\
\quad (\!\lvert \phi_n\rvert\!) & \texttt{justification}
\end{array}
(Axiom)
- Assignment:
\dfrac{}{(\!\lvert Q[E/x] \rvert \!) \; x=E \; (\!\lvert Q(x) \rvert \!)}
也就是,若要再执行赋值x=E后Q(x)成立,则原来把Q中的x替换为E一定成立。
例如,在图中\phi应该是x+1>0\land y>0。
(\!\lvert \phi\rvert\!)\ x=x+1\ (\!\lvert x>0\land y>0\rvert\!)
(Implied Rule)
- Precondition Strengthening
\dfrac{P\to P'\;\;(\!\lvert P' \rvert \!) \; C \; (\!\lvert Q \rvert \!)}{(\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!)}
也就是,可以把条件换成一个更强的条件。 - Postcondition Weakening
\dfrac{(\!\lvert P \rvert \!) \; C \; (\!\lvert Q' \rvert \!) \;\; Q'\to Q}{(\!\lvert P \rvert \!) \; C \; (\!\lvert Q \rvert \!)} - Composition
\dfrac{(\!\lvert P \rvert \!) \; C_{1} \; (\!\lvert Q \rvert \!) \;\; (\!\lvert Q \rvert \!) \; C_{2} \; (\!\lvert R \rvert \!)}{(\!\lvert P \rvert \!) \; C_{1},C_{2} \; (\!\lvert R \rvert \!)} - If statements
\frac{ (\!\lvert P \land B \rvert \!) \; C_1 \; (\!\lvert Q \rvert \!) \qquad (\!\lvert P \land \neg B \rvert \!) \; C_2 \; (\!\lvert Q \rvert \!) }{ (\!\lvert P \rvert \!) \text{ if } B \; \{C_1\} \text{ else } \{C_2\} \; (\!\lvert Q \rvert \!) }
\begin{array}{ll} (\!\lvert P\rvert\!) & \\ \texttt{if ( }B\texttt{ ) \{} & \\ \quad (\!\lvert (P\land B)\rvert\!) & \text{if-then-else} \\ \quad C_1 & \\ \quad (\!\lvert Q\rvert\!) & \textit{(justify depending on }C_1\textit{—a “subproof”)} \\ \texttt{\} else \{} & \\ \quad (\!\lvert (P\land(\neg B))\rvert\!) & \text{if-then-else} \\ \quad C_2 & \\ \quad (\!\lvert Q\rvert\!) & \textit{(justify depending on }C_2\textit{—a “subproof”)} \\ \texttt{\}} & \\ (\!\lvert Q\rvert\!) & \text{if-then-else }\textit{[justifies this }Q\textit{, given previous two]} \end{array}
Loop Invariant(循环不变式):在循环中始终成立。
- Partial-While:
\frac{ (\!\lvert I \land B \rvert \!) \; C \; (\!\lvert I \rvert \!) }{ (\!\lvert I \rvert \!) \text{ while } B \; \{C\} \; (\!\lvert I \land \neg B \rvert \!) }
这里I就是一个 Loop Invariant。需要合适地选取一个循环不变式,用来完成我们的证明。
\begin{array}{ll} (\!\lvert x\ge 0\rvert\!) & \\ (\!\lvert 1=0!\rvert\!) & \text{implied (a)} \\ \texttt{y = 1 ;} & \\ (\!\lvert y=0!\rvert\!) & \text{assignment} \\ \texttt{z = 0 ;} & \\ (\!\lvert y=z!\rvert\!) & \text{assignment} \\ \texttt{while (z != x) \{} & \\ \quad (\!\lvert (y=z!)\land\neg(z=x)\rvert\!) & \text{partial-while} \\ \quad (\!\lvert y(z+1)=(z+1)!\rvert\!) & \text{implied (b)} \\ \quad \texttt{z = z + 1 ;} & \\ \quad (\!\lvert yz=z!\rvert\!) & \text{assignment} \\ \quad \texttt{y = y * z ;} & \\ \quad (\!\lvert y=z!\rvert\!) & \text{assignment} \\ \texttt{\}} & \\ (\!\lvert y=z!\land(z=x)\rvert\!) & \text{partial-while} \\ (\!\lvert y=x!\rvert\!) & \text{implied (c)} \end{array}