Dafny,按序列中的值替换索引

时间:2018-05-19 22:58:00

标签: z3 verification dafny

rise4fun的Dafny教程中,s[i := v]定义为按序列i中的v替换索引s。 但是使用它总是失败expected method call, found expression

例如在下面的代码中交换两个索引

var a:int :=input[j];
var b:int :=input[j-1];
input[j := b]; //expected method call, found expression
input[j-1 := a]; //expected method call, found expression

在交换两个索引的情况下使用s[i := v]的正确方法是什么。

1 个答案:

答案 0 :(得分:3)

你可以写

var a:int :=input[j];
var b:int :=input[j-1];
input := input[j:=b] ;
input := input[j-i : a];

更简洁,但也许更难阅读

input := input[ j := input[j-1] ][ j-1 := input[j] ] ;