我想证明Frama-C中这种欧几里得除法的循环实现:
/*@
requires a >= 0 && 0 < b;
ensures \result == a / b;
*/
int euclid_div(const int a, const int b)
{
int q = 0;
int r = a;
/*@
loop invariant a == b*q+r && r>=0;
loop assigns q,r;
loop variant r;
*/
while (b <= r)
{
q++;
r -= b;
}
return q;
}
但是发布条件无法自动证明(循环不变证明很好):
Goal Post-condition:
Let x = r + (b * euclid_div_0).
Assume {
(* Pre-condition *)
Have: (0 < b) /\ (0 <= x).
(* Invariant *)
Have: 0 <= r.
(* Else *)
Have: r < b.
}
Prove: (x / b) = euclid_div_0.
--------------------------------------------------------------------------------
Prover Alt-Ergo: Unknown (250ms).
它确实具有欧几里得分裂的所有假设,有人知道为什么它不能得出结论吗?
答案 0 :(得分:3)
因为它是非线性算法,所以有时对于自动(SMT)求解器来说很难。
我以SMT2格式重新编写了目标,但Alt-Ergo 2.2,CVC4 1.5和Z3 4.6.0都无法证明这一点:
(set-logic QF_NIA)
(declare-const i Int)
(declare-const i_1 Int)
(declare-const i_2 Int)
(assert (>= i_1 0))
(assert (> i_2 0))
(assert (>= i 0))
(assert (< i i_2))
; proved by alt-ergo 2.2 and z3 4.6.0 if these two asserts are uncommented
;(assert (<= i_1 10))
;(assert (<= i_2 10))
(assert
(not
(= i_1
(div
(+ i (* i_1 i_2))
i_2 )
)
)
)
(check-sat)
如果您这样更改后置条件,则Alt-Ergo会证明
ensures \exists int r ;
a == b * \result + r && 0 <= r && r < b;
答案 1 :(得分:3)
如Mohamed Iguernlala's answer所示,自动证明不适用于非线性算术。可以直接在GUI中使用WP进行交互式证明(有关更多信息,请参见section 2.3 of WP Manual),也可以使用coq(双击GUI的WP Goals选项卡的适当单元格以在Windows上启动coqide)进行交互性证明。相应的目标)。
通常最好在ACSL引理上使用coq,因为您可以专注于要手动证明的确切公式,而不必为要证明的代码的逻辑模型所困扰。使用这种策略,我可以通过以下中间引理证明您的后置条件:
/*@
// WP's interactive prover select lemmas based on the predicate and
// function names which appear in it, but does not take arithmetic operators
// into account . Hence the DIV definition.
logic integer DIV(integer a,integer b) = a / b ;
lemma div_rem:
\forall integer a,b,q,r; a >=0 ==> 0 < b ==> 0 <= r < b ==>
a == b*q+r ==> q == DIV(a, b);
*/
/*@
requires a >= 0 && 0 < b;
ensures \result == DIV(a, b);
*/
int euclid_div(const int a, const int b)
{
int q = 0;
int r = a;
/*@
loop invariant a == b*q+r;
loop invariant r>=0;
loop assigns q,r;
loop variant r;
*/
while (b <= r)
{
q++;
r -= b;
}
/*@ assert 0<=r<b; */
/*@ assert a == b*q+r; */
return q;
}
更准确地说,引理本身由以下Coq脚本证明:
intros a b q prod Hb Ha Hle Hge.
unfold L_DIV.
generalize (Cdiv_cases a b).
intros Hcdiv; destruct Hcdiv.
clear H0.
rewrite H; auto with zarith.
clear H.
symmetry; apply (Zdiv_unique a b q (a-prod)); auto with zarith.
unfold prod; simpl.
assert (b*q = q*b); auto with zarith.
尽管后置条件仅需要使用适当的参数实例化引理。