我怎样代表Dafny中的一对(两个元组)?

时间:2016-04-15 05:00:11

标签: dafny

如何编写一个dafny function,它接受​​一系列int并返回一对Pairs?例如,input = [1,2],output = [Pair(1,1),Pair(1,2)]

我从

开始
function foo (l : seq<int>) : seq<Pair> 
{
  if |l| == 0 then [] 
  else new Pair() .... 
}

似乎不起作用。

1 个答案:

答案 0 :(得分:1)

你不能在函数中使用new,因为在Dafny中函数是纯粹的,他们不能修改堆。使用inductive datatypes

datatype Pair = Pair(fst:int, snd:int)

function foo (l : seq<int>) : seq<Pair> 
{
  if |l| <= 1 then [] 
  else [Pair(l[0],l[1])] + foo(l[2..]) 
}

或使用tuples

function foo (l : seq<int>) : seq<(int,int)> 
{
  if |l| <= 1 then [] 
  else [(l[0],l[1])] + foo(l[2..]) 
}
相关问题