根据Automating Induction with a SMT Solver,以下内容应适用于 Dafny:
ghost method AdjacentImpliesTransitive(s: seq<int>)
requires ∀ i • 1 ≤ i < |s| ==> s[i-1] ≤ s[i];
ensures ∀ i,j {:induction j} • 0 ≤ i < j < |s| ==> s[i] ≤ s[j];
{ }
但 Dafny 拒绝它(至少在我的桌面和 Dafny 在线)。也许有些东西改变了。
问题:
Q1。为什么?
Q2。真的需要对 j 或 i 和 j 进行归纳吗?我认为对 seq 长度的归纳应该足够了。
无论如何,我对以下内容更感兴趣:我想通过手动归纳来证明这一点,以供学习。在纸上,我认为对 seq 长度的归纳就足够了。现在我试图在 Dafny 上这样做:
lemma {:induction false} AdjacentImpliesTransitive(s: seq<int>)
ensures forall i :: 0 <= i <= |s|-2 ==> s[i] <= s[i+1] ==> forall l, r :: 0 <= l < r <= |s|-1 ==> s[l] <= s[r]
decreases s
{
if |s| == 0
{
//explicit calc proof, not neccesary
calc {
(forall i :: 0 <= i <= 0-2 ==> s[i] <= s[i+1]) ==> (forall l, r :: 0 <= l < r <= 0-1 ==> s[l] <= s[r]);
==
(forall i :: false ==> s[i] <= s[i+1]) ==> (forall l, r :: false ==> s[l] <= s[r]);
==
true ==> true;
==
true;
}
}
else if |s| == 1
{
//trivial for dafny
}
else {
AdjacentImpliesTransitive(s[..|s|-1]);
assert (forall i :: 0 <= i <= |s|-3 ==> s[i] <= s[i+1]) ==> (forall l, r :: 0 <= l < r <= |s|-2 ==> s[l] <= s[r]);
//What??
}
}
我坚持最后一个案例。我不知道如何将计算证明风格(如基本案例中的风格)与归纳炒作结合起来。
也许是棘手的暗示。在纸上(“非正式”证明),当我们需要p(n) ==> q(n)
通过归纳证明一个蕴涵时,我们有类似的东西:
炒作: p(k-1) ==> q(k-1)
特西斯:p(k) ==> q(k)
但这证明:
(p(k-1) ==> q(k-1) && p(k)) ==> q(k)
Q3。我的方法有意义吗?我们如何在 dafny 中进行这种归纳?