dafny - 在 dafny 中实现链表的追加操作,复杂度为 O(1)

标签 dafny formal-verification

在 dafny 中,我们可以使用 set<T>作为验证链表终止的动态框架:

class Node {
  // the sequence of data values stored in a node and its successors.
  ghost var list: seq<Data>;
  // `footprint` is a set consisting of the node and its successors.
  // it is actually the dynamic frame mentioned in the paper.
  ghost var footprint: set<Node>;

  var data: Data;
  var next: Node?;

  // A "Valid" linked list is one without ring. 
  function Valid(): bool
    reads this, footprint
  {
    this in footprint &&
    (next == null ==> list  == [data]) &&
    (next != null ==> next in footprint &&
                      next.footprint <= footprint &&
                      !(this in next.footprint) &&
                      list == [data] + next.list &&
                      next.Valid())
  }
  ...
}

(这是 Specification and Verification of Object-Oriented Software 中的实现。)

但是,footprint使得实现 append 变得困难手术。因为当向链表追加新节点时,我们需要更新footprint所有先前的节点。我想知道是否可以实现append dafny 中的复杂度为 O(1)(除了 Ghost 方法),否则我们需要删除 Valid并使用decreases *

谢谢:)

最佳答案

footprint 是一个 ghost 变量,因此仅由验证者使用(并且 functionghost定义也是如此)。它不会被编译和执行,因此您不必在(运行时)复杂性分析中考虑它。

关于dafny - 在 dafny 中实现链表的追加操作,复杂度为 O(1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67303068/

相关文章:

formal-verification - 如何提示 Dafny 对序列进行归纳?

verification - 我可以说状态空间是某些系统行为的正式规范吗?

language-agnostic - 程序验证的正式方法在工业中占有一席之地吗?

dafny - 在 Dafny 链表实现中使用空引用终止循环

methods - 在 Dafny 中将两个整数与不变性相乘的简单方法

static-analysis - 静态分析真的是形式验证吗?

time - 时序要求的形式验证

coq - 如何在 Coq 中重新排列新定义的关联术语?

z3 - Dafny,数组中没有重复项