不同的"排序"谓词在Dafny中应该是等价的

时间:2018-01-18 22:37:45

标签: theorem-proving induction dafny

根据Automating Induction with an 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??

   }
}

我坚持最后一个案子。我不知道如何将计算证明样式(如基本情况中的样式)与归纳法结合起来。

可能暗示是棘手的。在纸上(&#34;非正式&#34;证明),当我们需要通过归纳证明一个含义p(n) ==> q(n)时,我们有类似的东西:

Hyp:p(k-1) ==> q(k-1)

Tesis:p(k) ==> q(k)

然而这证明了:

(p(k-1) ==> q(k-1) && p(k)) ==> q(k)

Q3。我的方法有意义吗?我们怎样才能在dafny中进行这种归纳?

1 个答案:

答案 0 :(得分:0)

我不知道第一季度第二季度的答案。但是,如果在归纳案例中添加assert s[|s|-2] <= s[|s|-1],则归纳证明将通过(不需要其他断言)。这是完整的证明:

lemma {:induction false} AdjacentImpliesTransitive(s: seq<int>)
   requires forall i :: 0 <= i <= |s|-2 ==> s[i] <= s[i+1]
   ensures 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 s[|s|-2] <= s[|s|-1];

   }
}

由于某种原因,我不得不将您的ensures子句分为requiresensures子句。否则,Dafny抱怨undeclared identifier: _t#0#0。我不知道那是什么意思。

并且,如果有意思的话,这是一个简短的证明:

lemma AdjacentImpliesTransitive(s: seq<int>)
requires forall i | 1 <= i < |s| :: s[i-1] <= s[i]
ensures forall i,j | 0 <= i < j < |s| :: s[i] <= s[j]
decreases s
{
  if |s| < 2
  {
    assert forall i,j | 0 <= i < j < |s| :: s[i] <= s[j];
  } else {
    AdjacentImpliesTransitive(s[..|s|-1]);
    assert s[|s|-2] <= s[|s|-1];
  }
}