dafny - 愚蠢的前置条件失败
问题描述
class Graph
{
var adjList : seq<seq<int>>;
constructor (adjListInput : seq<seq<int>>)
{
adjList := adjListInput;
}
}
function ValidGraph(G : Graph) : bool
reads G
{
(forall u :: 0 <= u < |G.adjList| ==> forall v :: 0 <= v < |G.adjList[u]| ==> 0 <= G.adjList[u][v] < |G.adjList|) &&
(forall u :: 0 <= u < |G.adjList| ==> forall v,w :: 0 <= v < w < |G.adjList[u]| ==> G.adjList[u][v] != G.adjList[u][w])
}
method main()
{
var G : Graph := new Graph([[1,2],[0,2],[0,1]]);
assert (ValidGraph(G));
}
- 达芙妮的回应是
Error: assertion violation
解决方案
您只需要添加ensures adjList == adjListInput
到构造函数。因为 Dafny 基本上把构造函数当作方法来对待,并且因为 Dafny 对每个方法进行单独分析,所以 Dafny 在分析main
时只使用构造函数的规范,而不是构造函数的主体。所以断言失败的原因是因为从 的角度来看main
,构造函数将字段设置adjList
为一个不一定对应于其参数的任意值。
推荐阅读
- javascript - React.js 状态仅正确更新一次,然后失败
- react-native - React Native 中连接和订阅 MQTT 服务器的问题
- postgresql - 任何可能的Postgres UUID数组索引?
- python - for 循环后绘制的条形图不正确
- authentication - Nginx -> Apache 2 认证 -> 返回 Nginix
- jquery - jQuery数据表:排序后用行索引重写每一行的标题
- sql - CREATE TABLE...LIKE 不保留 Redshift 中的主键
- flutter - Flutter:AndroidManifest 找不到@xml 资源
- javascript - 避免循环依赖:替换事件的导入?
- javascript - 箭头函数在大括号后用括号编译