我想计算 Dafny 中设置小于阈值的元素,但我无法弄清楚“确保”。
method CountLessThan(numbers: set<int>, threshold: int) returns (count: int)
ensures count == |{i in numbers | i < threshold}|
{
count := 0;
var ss := numbers;
while ss != {}
decreases |ss|
{
var i: int :| i in ss;
ss := ss - {i};
if i < threshold {
count := count + 1;
}
}
}
method Main()
{
var s: set<int> := {1, 2, 3, 4, 5};
var c: int := CountLessThan(s, 4);
print c;
// assert c == 3;
}
错误是:this operator chain cannot continue with an ascending operator
您有语法错误。该
ensures
条款应该是:由于
|
该语法中有多种不同的用法,因此很难阅读。您可能想将 分解set
成一个函数只是为了给它一个名称:然后你可以
ensures
写成