【发布时间】:2020-09-21 02:34:58
【问题描述】:
Dafny 使用减少子句来验证递归函数是否终止。当验证失败时,可以给 Dafny 一个提示,在这种情况下是以引理的形式。在检查减少子句是否实际减少时,如何告诉 Dafny 使用引理?
datatype List<T> = Nil | Cons(head:T, tail:List<T>)
datatype Twee = Node(value : int, left : Twee, right : Twee) | Leaf
function rotateLeft(t:Twee) :Twee
{
match t
case Leaf => t
case Node(v,Node(vl,ll,rl),r) => Node(vl,ll,Node(v,rl,r))
case Node(v,Leaf,r) => Node(v,Leaf,r)
}
function Leftheight(t:Twee) :(h:nat) decreases t
{
match t
case Leaf => 0
case Node(_,l,_) => 1+Leftheight(l)
}
lemma {:induction ll, rl, r} decreasesHint(v:int, vl:int, ll:Twee,rl:Twee,r:Twee)
ensures Leftheight(Node(v,Node(vl,ll,rl),r)) ==
1+ Leftheight(Node(vl,ll, Node(v,rl,r))) {}
function rotateAllLeft(t:Twee) :Twee
decreases t, Leftheight(t)
{
match t
case Leaf => Leaf
case Node(v,Leaf,r) => Node(v,Leaf,rotateAllLeft(r)) //decrases t
case Node(v,Node(vl,ll,rl),r) => //{ decreasesHint(v,vl,ll,rl,r);}
rotateAllLeft(rotateLeft(Node(v,Node(vl,ll,rl),r)))
}
对不起,这么长的例子,但这是我第一次想给出检查终止的提示。错误failure to decrease termination measure出现在最后一行rotateAllLeft(rotateLeft(Node(v,Node(vl,ll,rl),r)。
【问题讨论】: