Dafny-从Main调用类方法后违反声明

时间:2019-11-26 00:03:28

标签: class assert dafny post-conditions

我正在为将棒球奔跑次数设置为0的类编写一些简单的代码。它的唯一方法应该将奔跑次数作为其参数,并根据输入是否大于输入值返回布尔值。 class变量和一个int,两个中的较高者运行如下:

    class Baseball_Runs
    {
        var runs : int;
        constructor()
        ensures runs == 0;
        {
            runs := 0;
        }
        method moreRuns (val: int) returns (hasChanged: bool, newRuns: int)
        requires val >= 0;
        ensures (val > runs) ==> (hasChanged && newRuns == runs);
        ensures (!hasChanged && newRuns == val) ==> (val <= runs);
        //   ensures if (val > runs) then (hasChanged && newRuns == val) else (!hasChanged && newRuns == runs); 
        modifies this;
        {
            if (val > runs)
            {
                hasChanged := true;
                runs := val;
                newRuns := val;
            } else {
                hasChanged := false;
                newRuns := runs;
            }
        }
    }

    method Main()
    {
        var r := new Baseball_Runs();
        var new_run: int := 0;
        var hasChanged: bool := false;
        var score: int;
        score := 2;
        hasChanged, new_run := r.moreRuns(score);
        assert (hasChanged && new_run == 2);          // I get an assertion error here
    }

我注释掉了第三个suresure块,因为这是我在后置条件下的第一个刺,但它返回一个错误,表明后置条件(其他块)不成立,因此我选择了前2个确保(不确定是否是正确,但整个课程都得到了验证,没有错误)。

无论如何,当我从main调用moreRuns()时,就会遇到问题。我对bool和int返回的主张似乎不成立。有人知道我哪里出问题了吗?是我的后置条件,还是在调用moreRuns()之前忘记添加一些断言,还是我不满足val ==运行的选项?

任何提示将不胜感激!

1 个答案:

答案 0 :(得分:0)

您需要注意,后置条件要与哪个值runs进行比较。修改runs时,您想与old(runs)进行比较。

以下moreRuns版本适用:

        method moreRuns (val: int) returns (hasChanged: bool, newRuns: int)
          modifies this
          requires val >= 0
          ensures 
            if val > old(runs)
            then newRuns == val && hasChanged
            else newRuns == old(runs) && !hasChanged
        {
            if (val > runs) {
                hasChanged := true;
                runs := val;
                newRuns := val;
            } else {
                hasChanged := false;
                newRuns := runs;
            }
        }

您无需在modifies / requires / ensures子句中以分号结尾。