首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在dafny中实现O(1)复杂度链表追加操作

在dafny中实现O(1)复杂度链表追加操作
EN

Stack Overflow用户
提问于 2021-04-28 23:20:21
回答 1查看 78关注 0票数 1

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

代码语言:javascript
复制
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。我想知道是否有可能在dafny中实现复杂度为O(1)的append (除了ghost方法),或者我们需要删除Valid并使用decreases *

谢谢您:)

EN

回答 1

Stack Overflow用户

发布于 2021-04-30 19:28:13

footprint是一个ghost变量,因此仅供验证器使用(根据定义,function也是ghost )。它不会被编译和执行,所以你在(运行时)复杂性分析中不必考虑它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67303068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档