dafny - 对于 Dafny 中的数组,old(a[0]) 和 old(a)[0] 有什么区别
问题描述
old(a[0])
对于 Dafny 中的数组,和有什么区别old(a)[0]
?
一种方法通过将 1 添加到第一个元素来修改数组“a”。在方法结束时,old(a[0])
和的值是old(a)[0]
多少?
解决方案
好问题!是的,它们是不同的。
当一个表达式在 a 中求值时old
,它的所有堆解引用都指向方法开始处的堆。任何不是堆取消引用的东西都不会受到old
.
在您的示例a[0]
中是堆取消引用,因此获取旧堆中old(a[0])
“指向”的数组的第 0 个值。a
然而,a
它本身并不是堆解引用,它只是局部函数的一个参数,其值永远不会改变。所以old(a) == a
。考虑这一点的一种方法是a
存储数组的“地址”,并且该地址在方法的生命周期内不会改变。
现在我们知道old(a) == a
了,它遵循old(a)[0] == a[0]
。换句话说,old
在你的第二个例子中没有效果。
这是一个小例子来演示:
method M(a: array<int>)
requires a.Length >= 2
requires a[0] == 1
modifies a
ensures a[1] == old(a[0]) // so far so good
ensures old(a) == a // perhaps surprising: Dafny proves this!
ensures a[1] == old(a)[0] // but this is not necessarily true,
// because old(a)[0] actually means the same thing as a[0]
{
a[1] := 1;
}
推荐阅读
- ansible - 如何注册_items并根据每个项目的条件检查结果采取行动
- postgresql - 如何从 ids 数组中获取 PostgreSQL 中的临时/虚拟表
- content-security-policy - 为什么我会出现“严格动态”?
- javascript - mocha chai 测试用例不会自动终止
- r - 在R中创建一个函数以使用位置从字符串中提取字符?根据模式条件确定字符的位置
- c# - 使用 openpop 连接到 Outlook 时提供的主机名应该是什么
- excel - 使用 range.find 方法时出现类型不匹配错误
- python - 我安装了 CUDA10,但 Anaconda 安装了 CUDA9。我可以删除前者吗?
- markdown - Markdown 列表缩进有 3 个空格还是 4 个空格?标准是什么?
- html - 如何使用 angular renderer2 将 mat-icon 动态添加到 div?