我正在尝试指定和验证汇总数组的简单函数。 WP接受此整数数组规范:
/*@ axiomatic Sum_Int {
logic int sum_int(int *values, integer begin, integer end)
reads values[begin .. (end - 1)];
axiom empty_int:
\forall int *p, integer begin, end; begin >= end
==> sum_int(p, begin, end) == 0;
axiom range_int:
\forall int *p, integer begin, end; begin < end
==> sum_int(p, begin, end) == sum_int(p, begin, end - 1) + p[end - 1];
}
*/
/*@ requires \valid_read(values + (0 .. size - 1));
assigns \nothing;
ensures \result == sum_int(values, 0, size);
*/
int sum_int_array(const int *values, size_t size) {
int sum = 0;
/*@ loop invariant 0 <= index <= size;
loop invariant sum == sum_int(values, 0, index);
loop assigns sum, index;
loop variant size - index;
*/
for (size_t index = 0; index < size; index++) {
sum += values[index];
}
return sum;
}
但是WP无法证明浮点数组的相同规范:
/*@ axiomatic Sum_Real {
logic float sum_real(float *values, integer begin, integer end)
reads values[begin .. (end - 1)];
axiom empty_real:
\forall float *p, integer begin, end; begin >= end
==> sum_real(p, begin, end) == 0;
axiom range_real:
\forall float *p, integer begin, end; begin < end
==> sum_real(p, begin, end) == sum_real(p, begin, end - 1) + p[end - 1];
}
*/
/*@ requires \valid_read(values + (0 .. size - 1));
requires \forall integer i; 0 <= i < size ==> \is_finite(values[i]);
assigns \nothing;
ensures \result == sum_real(values, 0, size);
*/
float sum_float_array(const float *values, size_t size) {
float sum = 0;
/*@ loop invariant 0 <= index <= size;
loop invariant sum == sum_real(values, 0, index);
loop assigns sum, index;
loop variant size - index;
*/
for (size_t index = 0; index < size; index++) {
sum += values[index];
}
return sum;
}
公理的定义是相同的(如果正确的话,我认为应该是相同的)。唯一的区别是为float数组添加了前提条件:
requires \forall integer i; 0 <= i < size ==> \is_finite(values[i]);
我不确定是否有必要。 WP缺少哪些信息才能验证浮动版本?