id
stringlengths
3
3
name
stringlengths
3
3
docstring
stringlengths
18
18
code
stringlengths
129
20.6k
ground_truth
stringlengths
148
21.8k
test_file
stringlengths
15
119
255
255
Dafny program: 255
method Mult(x:nat, y:nat) returns (r: nat) ensures r == x * y { var m := x; var n := y; r:=0; while m > 0 { r := r + n; m := m - 1; } return r; }
method Mult(x:nat, y:nat) returns (r: nat) ensures r == x * y { var m := x; var n := y; r:=0; while m > 0 invariant m >= 0 invariant m*n+r == x*y { r := r + n; m := m - 1; } return r; }
Metodos_Formais_tmp_tmpbez22nnn_Aula_2_ex1.dfy
256
256
Dafny program: 256
function Potencia(x: nat, y: nat): nat { if y == 0 then 1 else x * Potencia(x, y-1) } method Pot(x: nat, y: nat) returns (r: nat) ensures r == Potencia(x,y) { var b := x; var e := y; r := 1; while e > 0 { r := b * r; e := e - 1; } return r; }
function Potencia(x: nat, y: nat): nat { if y == 0 then 1 else x * Potencia(x, y-1) } method Pot(x: nat, y: nat) returns (r: nat) ensures r == Potencia(x,y) { var b := x; var e := y; r := 1; while e > 0 invariant Potencia(b, e) * r == Potencia(x,y) { r := b * r; e := e - 1; } return r; }
Metodos_Formais_tmp_tmpbez22nnn_Aula_2_ex2.dfy
257
257
Dafny program: 257
predicate Par(n:int) { n % 2 == 0 } method FazAlgo (a:int, b:int) returns (x:int, y:int) requires a >= b && Par (a-b) { x := a; y := b; while x != y { x := x - 1; y := y + 1; } }
predicate Par(n:int) { n % 2 == 0 } method FazAlgo (a:int, b:int) returns (x:int, y:int) requires a >= b && Par (a-b) { x := a; y := b; while x != y invariant x >= y invariant Par(x-y) decreases x-y { x := x - 1; y := y + 1; } }
Metodos_Formais_tmp_tmpbez22nnn_Aula_4_ex1.dfy
258
258
Dafny program: 258
function Fib(n:nat):nat { if n < 2 then n else Fib(n-2) + Fib(n-1) } method ComputeFib(n:nat) returns (x:nat) ensures x == Fib(n) { var i := 0; x := 0; var y := 1; while i < n { x, y := y, x + y; i := i + 1; } } method Teste() { var n := 3; var f := ComputeFib(n); }
function Fib(n:nat):nat { if n < 2 then n else Fib(n-2) + Fib(n-1) } method ComputeFib(n:nat) returns (x:nat) ensures x == Fib(n) { var i := 0; x := 0; var y := 1; while i < n decreases n - i invariant 0 <= i <= n invariant x == Fib(i) invariant y == Fib(i+1) { x, y := y, x + y; i := i + 1; } } method Teste() { var n := 3; var f := ComputeFib(n); assert f == 2; }
Metodos_Formais_tmp_tmpbez22nnn_Aula_4_ex3.dfy
259
259
Dafny program: 259
// Array<T> = visualização de um array // Uma busca ordenada em um array // Buscar: Array<Z>xZ -> Z (Z é inteiro) // Pré: True (pré-condição é sempre verdadeira) // Pos: R < 0 => Para todo i pertencente aos naturais(0 <= i < A.length => A[i] != X) e // 0 <= R < A.length => A[R] = x // // método em qualquer linguagem: // R = 0 // Enquanto(R < |A|) { // Se (A[R] == X) retorne E // R = R + 1 // } // retorne -1 // // X | R | |A| // 10 | 0 | 5 // 10 | 1 | 5 // 10 | 2 | // invariante detectada: 0 <= R <= |A| e Para todo i pertencente aos naturais(0 <= i < R => A[i] != X) // no dafy // forall = é o para todo logico // :: é igual ao tal que lógico // ==> é o então lógico // forall i :: 0 <= i < a.Length ==> a[i] != x (para todo i tal que i e maior ou igual a zero e menor que o tamanho do array, então a posição i do array a é diferente de x) method buscar(a:array<int>, x:int) returns (r:int) ensures r < 0 ==> forall i :: 0 <= i < a.Length ==> a[i] != x ensures 0 <= r < a.Length ==> a[r] == x { r := 0; while r < a.Length { if a[r] == x { return r; } r := r + 1; } return -1; }
// Array<T> = visualização de um array // Uma busca ordenada em um array // Buscar: Array<Z>xZ -> Z (Z é inteiro) // Pré: True (pré-condição é sempre verdadeira) // Pos: R < 0 => Para todo i pertencente aos naturais(0 <= i < A.length => A[i] != X) e // 0 <= R < A.length => A[R] = x // // método em qualquer linguagem: // R = 0 // Enquanto(R < |A|) { // Se (A[R] == X) retorne E // R = R + 1 // } // retorne -1 // // X | R | |A| // 10 | 0 | 5 // 10 | 1 | 5 // 10 | 2 | // invariante detectada: 0 <= R <= |A| e Para todo i pertencente aos naturais(0 <= i < R => A[i] != X) // no dafy // forall = é o para todo logico // :: é igual ao tal que lógico // ==> é o então lógico // forall i :: 0 <= i < a.Length ==> a[i] != x (para todo i tal que i e maior ou igual a zero e menor que o tamanho do array, então a posição i do array a é diferente de x) method buscar(a:array<int>, x:int) returns (r:int) ensures r < 0 ==> forall i :: 0 <= i < a.Length ==> a[i] != x ensures 0 <= r < a.Length ==> a[r] == x { r := 0; while r < a.Length decreases a.Length - r //variante, decrescendo a cada passo com o r invariant 0 <= r <= a.Length //a invariante é quando nao é encontado o x depois de rodado todo o projeto invariant forall i :: 0 <= i < r ==> a[i] != x { if a[r] == x { return r; } r := r + 1; } return -1; }
Metodos_Formais_tmp_tmpql2hwcsh_Arrays_explicacao.dfy
260
260
Dafny program: 260
// Deve ser criado uma função explicando o que é um somatório // Somatorio: Array<N> -> N // Pre: True // Pos: Somatorio(A) = somatório de i = 0 até |A|-1 os valores das posições do array pelo i // // function é uma fórmula matemática, ele não possui variaveis globais // Soma: Array<N>xN -> N // { Soma(A,0) = A[0] // { Soma(A,i) = A[i] + soma(A, i-1) , se i > 0 // Teste // |A| = 4 // Soma(A, |A|-1) = Soma(A,3) // A[3] + Soma(A,2) // A[3] + A[2] + Soma(A,1) // A[3] + A[2] + A[1] + Soma(A,0) // A[3] + A[2] + A[1] + A[0] function soma(a:array<nat>, i:nat):nat requires i <= a.Length //Tem que dizer que o i só vai até um valor antes do tamanho do array reads a //serve para dizer que está sendo lido da memoria o array a (áreas de memória) { if i == 0 then 0 else a[i-1] + soma(a,i-1) } method somatorio(a:array<nat>) returns (s:nat) ensures s == soma(a, a.Length) { s := 0; for i := 0 to a.Length { s := s + a[i]; } }
// Deve ser criado uma função explicando o que é um somatório // Somatorio: Array<N> -> N // Pre: True // Pos: Somatorio(A) = somatório de i = 0 até |A|-1 os valores das posições do array pelo i // // function é uma fórmula matemática, ele não possui variaveis globais // Soma: Array<N>xN -> N // { Soma(A,0) = A[0] // { Soma(A,i) = A[i] + soma(A, i-1) , se i > 0 // Teste // |A| = 4 // Soma(A, |A|-1) = Soma(A,3) // A[3] + Soma(A,2) // A[3] + A[2] + Soma(A,1) // A[3] + A[2] + A[1] + Soma(A,0) // A[3] + A[2] + A[1] + A[0] function soma(a:array<nat>, i:nat):nat requires i <= a.Length //Tem que dizer que o i só vai até um valor antes do tamanho do array reads a //serve para dizer que está sendo lido da memoria o array a (áreas de memória) { if i == 0 then 0 else a[i-1] + soma(a,i-1) } method somatorio(a:array<nat>) returns (s:nat) ensures s == soma(a, a.Length) { s := 0; for i := 0 to a.Length invariant s == soma(a,i) { s := s + a[i]; } }
Metodos_Formais_tmp_tmpql2hwcsh_Arrays_somatorioArray.dfy
261
261
Dafny program: 261
function Fat(n:nat):nat { if n == 0 then 1 else n*Fat(n-1) } method Fatorial(n:nat) returns (f:nat) ensures f == Fat(n) { f := 1; var i := 1; while i <= n { f := f * i; i := i + 1; } return f; } // i | n | variante // 1 | 3 | 2 // 2 | 3 | 1 // 3 | 3 | 0 // 4 | 3 | -1 // variante = n - i // então é usado o decreases n-1
function Fat(n:nat):nat { if n == 0 then 1 else n*Fat(n-1) } method Fatorial(n:nat) returns (f:nat) ensures f == Fat(n) { f := 1; var i := 1; while i <= n decreases n-i //variante invariant 1 <= i <= n+1 //invariante invariant f == Fat(i-1) //invariante { f := f * i; i := i + 1; } return f; } // i | n | variante // 1 | 3 | 2 // 2 | 3 | 1 // 3 | 3 | 0 // 4 | 3 | -1 // variante = n - i // então é usado o decreases n-1
Metodos_Formais_tmp_tmpql2hwcsh_Invariantes_fatorial2.dfy
262
262
Dafny program: 262
// Provando fibonacci function Fib(n:nat):nat { if n < 2 then n else Fib(n-2) + Fib(n-1) } method ComputeFib(n:nat) returns (x:nat) ensures x == Fib(n) { var i := 0; x := 0; var y := 1; while i < n { x, y := y, x + y; //multiplas atribuições i := i + 1; } } // Fibonnaci // n | Fib // 0 | 0 // 1 | 1 // 2 | 1 // 3 | 2 // 4 | 3 // 5 | 5 // Teste da computação do Fibonnaci // i | n | x | y | n-1 // 0 | 3 | 0 | 1 | 3 // 1 | 3 | 1 | 1 | 2 // 2 | 3 | 1 | 2 | 1 // 3 | 3 | 2 | 3 | 0 // Variante: n - 1 // Invariante: x = Fib(i) = x sempre é o resultado do fibonnaci do valor de i // Invariante: 0 <= i <= n = i deve ter um valor entre 0 e o valor de n // Invariante: y = Fib(i+1) = o valor de y sempre vai ser o valor de fibonnaci mais um
// Provando fibonacci function Fib(n:nat):nat { if n < 2 then n else Fib(n-2) + Fib(n-1) } method ComputeFib(n:nat) returns (x:nat) ensures x == Fib(n) { var i := 0; x := 0; var y := 1; while i < n decreases n-i invariant 0 <= i <= n invariant x == Fib(i) invariant y == Fib(i+1) { x, y := y, x + y; //multiplas atribuições i := i + 1; } } // Fibonnaci // n | Fib // 0 | 0 // 1 | 1 // 2 | 1 // 3 | 2 // 4 | 3 // 5 | 5 // Teste da computação do Fibonnaci // i | n | x | y | n-1 // 0 | 3 | 0 | 1 | 3 // 1 | 3 | 1 | 1 | 2 // 2 | 3 | 1 | 2 | 1 // 3 | 3 | 2 | 3 | 0 // Variante: n - 1 // Invariante: x = Fib(i) = x sempre é o resultado do fibonnaci do valor de i // Invariante: 0 <= i <= n = i deve ter um valor entre 0 e o valor de n // Invariante: y = Fib(i+1) = o valor de y sempre vai ser o valor de fibonnaci mais um
Metodos_Formais_tmp_tmpql2hwcsh_Invariantes_fibonacci.dfy
263
263
Dafny program: 263
// Exemplo de invariantes // Invariante significa que o valor não muda desde a pré-condição até a pós-condição method Mult(x:nat, y:nat) returns (r:nat) ensures r == x * y { // parâmetros de entrada são imutáveis, por isso // é preciso a atribuir a variáveis locais para usar em blocos de códigos para mudar var m := x; var n := y; r := 0; while m > 0 { r := r + n; m := m -1; } return r; } // Teste do método para encontrar a invariante // x | y | m | n | r // 5 | 3 | 5 | 3 | 0 // 5 | 3 | 4 | 3 | 3 // 5 | 3 | 3 | 3 | 6 // 5 | 3 | 2 | 3 | 9 // 5 | 3 | 1 | 3 | 12 // 5 | 3 | 0 | 3 | 15 // vimos o seguinte: // m * n + r = x * y // 5 * 3 + 0 (15) = 5 * 3 (15) // portanto a fórmula m*n+r == x*y é uma invariante // mas só isso não serve, o m ele é maior ou igual a zero quando acaba o while // por isso, também é a invariante que necessita // com isso dizemos para o programa as alterações do m de maior ou igual a zero // e mostramos a função encontrada que alterava o valor de m e n das variaveis criadas // SE OS ALGORITMOS TIVEREM REPETIÇÃO OU RECURSÃO, DEVEM SER MOSTRADOS QUAIS SÃO AS INVARIANTES // OU SEJA, OS VALORES QUE NÃO ESTÃO SENDO MUDADOS E COLOCAR A FÓRMULA DELE COMO ACIMA
// Exemplo de invariantes // Invariante significa que o valor não muda desde a pré-condição até a pós-condição method Mult(x:nat, y:nat) returns (r:nat) ensures r == x * y { // parâmetros de entrada são imutáveis, por isso // é preciso a atribuir a variáveis locais para usar em blocos de códigos para mudar var m := x; var n := y; r := 0; while m > 0 invariant m >= 0 invariant m*n+r == x*y { r := r + n; m := m -1; } return r; } // Teste do método para encontrar a invariante // x | y | m | n | r // 5 | 3 | 5 | 3 | 0 // 5 | 3 | 4 | 3 | 3 // 5 | 3 | 3 | 3 | 6 // 5 | 3 | 2 | 3 | 9 // 5 | 3 | 1 | 3 | 12 // 5 | 3 | 0 | 3 | 15 // vimos o seguinte: // m * n + r = x * y // 5 * 3 + 0 (15) = 5 * 3 (15) // portanto a fórmula m*n+r == x*y é uma invariante // mas só isso não serve, o m ele é maior ou igual a zero quando acaba o while // por isso, também é a invariante que necessita // com isso dizemos para o programa as alterações do m de maior ou igual a zero // e mostramos a função encontrada que alterava o valor de m e n das variaveis criadas // SE OS ALGORITMOS TIVEREM REPETIÇÃO OU RECURSÃO, DEVEM SER MOSTRADOS QUAIS SÃO AS INVARIANTES // OU SEJA, OS VALORES QUE NÃO ESTÃO SENDO MUDADOS E COLOCAR A FÓRMULA DELE COMO ACIMA
Metodos_Formais_tmp_tmpql2hwcsh_Invariantes_multiplicador.dfy
264
264
Dafny program: 264
// Potência // deve ser especificado a potência, porque ele não existe n dafny // Função recursiva da potência function Potencia(x:nat, y:nat):nat { if y == 0 then 1 else x * Potencia(x,y-1) } // Quero agora implementar como uma função não recursiva method Pot(x:nat, y:nat) returns (r:nat) ensures r == Potencia(x,y) { r := 1; //sempre r começa com 1 var b := x; //base var e := y; //expoente while e > 0 { r := r * b; e := e - 1; } return r; } // Devemos sempre construir uma tabela para vermos passo a passo o processo // POT(2,3) // x | y | b | e | r | // 2 | 3 | 2 | 3 | 1 | // 2 | 3 | 2 | 2 | 1x2 | // 2 | 3 | 2 | 1 | 1x2x2 | // 2 | 3 | 2 | 0 | 1x2x2x2 | // temos que na invariante queremos a fórmula x^y // INV ... = x^y // vendo pelo que foi processado fica dando o seguinte // x | y | b | e | r | // 2 | 3 | 2 | 3 | 1 (2^0) | 2^3 x 2^0 = 2^3 // 2 | 3 | 2 | 2 | 1x2 (2^1) | 2^2 x 2^1 = 2^3 // 2 | 3 | 2 | 1 | 1x2x2 (2^2) | 2^1 x 2^2 = 2^3 // 2 | 3 | 2 | 0 | 1x2x2x2 (2^3)| 2^0 x 2^3 = 2^3 // portanto a base está sendo feito a potencia de e (usando o potencia) e multiplicado pelo valor de r // b^e * r // assim temos a fórmula: b^e * r = x^y // dai utilizamos a function potencia para construir a fórmula // Potencia(b,e)*r == Potencia(x,y)
// Potência // deve ser especificado a potência, porque ele não existe n dafny // Função recursiva da potência function Potencia(x:nat, y:nat):nat { if y == 0 then 1 else x * Potencia(x,y-1) } // Quero agora implementar como uma função não recursiva method Pot(x:nat, y:nat) returns (r:nat) ensures r == Potencia(x,y) { r := 1; //sempre r começa com 1 var b := x; //base var e := y; //expoente while e > 0 invariant Potencia(b,e)*r == Potencia(x,y) { r := r * b; e := e - 1; } return r; } // Devemos sempre construir uma tabela para vermos passo a passo o processo // POT(2,3) // x | y | b | e | r | // 2 | 3 | 2 | 3 | 1 | // 2 | 3 | 2 | 2 | 1x2 | // 2 | 3 | 2 | 1 | 1x2x2 | // 2 | 3 | 2 | 0 | 1x2x2x2 | // temos que na invariante queremos a fórmula x^y // INV ... = x^y // vendo pelo que foi processado fica dando o seguinte // x | y | b | e | r | // 2 | 3 | 2 | 3 | 1 (2^0) | 2^3 x 2^0 = 2^3 // 2 | 3 | 2 | 2 | 1x2 (2^1) | 2^2 x 2^1 = 2^3 // 2 | 3 | 2 | 1 | 1x2x2 (2^2) | 2^1 x 2^2 = 2^3 // 2 | 3 | 2 | 0 | 1x2x2x2 (2^3)| 2^0 x 2^3 = 2^3 // portanto a base está sendo feito a potencia de e (usando o potencia) e multiplicado pelo valor de r // b^e * r // assim temos a fórmula: b^e * r = x^y // dai utilizamos a function potencia para construir a fórmula // Potencia(b,e)*r == Potencia(x,y)
Metodos_Formais_tmp_tmpql2hwcsh_Invariantes_potencia.dfy
265
265
Dafny program: 265
ghost function f(n: nat): nat { if n == 0 then 1 else if n%2 == 0 then 1 + 2*f(n/2) else 2*f(n/2) } method mod(n:nat) returns (a:nat) ensures a == f(n) { var x:nat := 0; var y:nat := 1; var k:nat := n; while k > 0 { if (k%2 == 0) { x := x + y; } else { } y := 2*y; k := k/2; } a := x+y; }
ghost function f(n: nat): nat { if n == 0 then 1 else if n%2 == 0 then 1 + 2*f(n/2) else 2*f(n/2) } method mod(n:nat) returns (a:nat) ensures a == f(n) { var x:nat := 0; var y:nat := 1; var k:nat := n; while k > 0 invariant f(n) == x + y*f(k) invariant 0 <= k <= n decreases k { assert f(n) == x + y*f(k); if (k%2 == 0) { assert f(n) == x + y*f(k); assert f(n) == x + y*(1+2*f(k/2)); assert f(n) == x + y + 2*y*f(k/2); x := x + y; assert f(n) == x + 2*y*f(k/2); } else { assert f(n) == x + y*(2*f(k/2)); assert f(n) == x + 2*y*f(k/2); } y := 2*y; assert f(n) == x + y*f(k/2); k := k/2; assert f(n) == x + y*f(k); } assert k == 0; assert f(n) == x+y*f(0); assert f(n) == x+y; a := x+y; }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_extra_mod.dfy
266
266
Dafny program: 266
ghost function f2(n: nat): nat { if n == 0 then 0 else 5*f2(n/3) + n%4 } method mod2(n:nat) returns (a:nat) ensures a == f2(n) { var x:nat := 1; var y:nat := 0; var k:nat := n; while k > 0 { y := x*(k%4) + y; x := 5*x; k := k/3; } a := y; }
ghost function f2(n: nat): nat { if n == 0 then 0 else 5*f2(n/3) + n%4 } method mod2(n:nat) returns (a:nat) ensures a == f2(n) { var x:nat := 1; var y:nat := 0; var k:nat := n; while k > 0 invariant f2(n) == x*f2(k) + y invariant 0 <= k <= n decreases k { assert f2(n) == x*f2(k) + y; assert f2(n) == x*(5*f2(k/3) + k%4) + y; assert f2(n) == 5*x*f2(k/3) + x*(k%4) + y; y := x*(k%4) + y; assert f2(n) == 5*x*f2(k/3) + y; x := 5*x; assert f2(n) == x*f2(k/3) + y; k := k/3; assert f2(n) == x*f2(k) + y; } assert k == 0; assert f2(n) == x*f2(0) + y; assert f2(n) == x*0 + y; assert f2(n) == y; a := y; }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_extra_mod2.dfy
267
267
Dafny program: 267
ghost function pow(a: int, e: nat): int { if e == 0 then 1 else a*pow(a, e-1) } method Pow(a: nat, n: nat) returns (y: nat) ensures y == pow(a, n) { var x:nat := 1; var k:nat := 0; while k < n { x := a*x; k := k + 1; } y := x; }
ghost function pow(a: int, e: nat): int { if e == 0 then 1 else a*pow(a, e-1) } method Pow(a: nat, n: nat) returns (y: nat) ensures y == pow(a, n) { var x:nat := 1; var k:nat := 0; while k < n invariant x == pow(a, k) invariant 0 <= k <= n decreases n-k { assert x == pow(a, k); x := a*x; assert x == a*pow(a, k); assert x == pow(a, k+1); k := k + 1; assert x == pow(a, k); } assert k == n; y := x; assert y == pow(a, n); }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_extra_pow.dfy
268
268
Dafny program: 268
ghost function sum(n: nat): int { if n == 0 then 0 else n + sum(n - 1) } method Sum(n: nat) returns (s: int) ensures s == sum(n) { var x:nat := 0; var y:nat := 1; var k:nat := n; while k > 0 { x := x + y*k; k := k-1; } s := x; }
ghost function sum(n: nat): int { if n == 0 then 0 else n + sum(n - 1) } method Sum(n: nat) returns (s: int) ensures s == sum(n) { var x:nat := 0; var y:nat := 1; var k:nat := n; while k > 0 invariant sum(n) == x + y*sum(k) invariant 0 <= k <= n decreases k { assert sum(n) == x + y*sum(k); assert sum(n) == x + y*(k+sum(k-1)); assert sum(n) == x + y*k + y*sum(k-1); x := x + y*k; assert sum(n) == x + y*sum(k-1); assert sum(n) == x + y*sum(k-1); k := k-1; assert sum(n) == x + y*sum(k); } assert k == 0; assert sum(n) == x + y*sum(0); assert sum(n) == x + y*0; s := x; assert sum(n) == s; }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_extra_sum.dfy
271
271
Dafny program: 271
// problem 5: // name: Gabriele Berardi // s-number: s4878728 // table: XXXX ghost function f(n: int): int { if n < 0 then 0 else 3*f(n-5) + n } method problem5(n:nat) returns (x: int) ensures x == f(n) { var a := 1; var b := 0; var k := n; while k >= 0 { b := a*k + b; a := 3*a; k := k - 5; } x := b; }
// problem 5: // name: Gabriele Berardi // s-number: s4878728 // table: XXXX ghost function f(n: int): int { if n < 0 then 0 else 3*f(n-5) + n } method problem5(n:nat) returns (x: int) ensures x == f(n) { var a := 1; var b := 0; var k := n; while k >= 0 invariant f(n) == a*f(k) + b invariant -5 <= k <= n decreases k { assert f(n) == a*f(k) + b; assert f(n) == a*(3*f(k-5)+k) + b; assert f(n) == 3*a*f(k-5) + a*k + b; b := a*k + b; assert f(n) == 3*a*f(k-5) + b; a := 3*a; assert f(n) == a*f(k-5) + b; k := k - 5; assert f(n) == a*f(k) + b; } assert k < 0; assert f(n) == a*f(k) + b; assert f(n) == a*0 + b; x := b; assert x== f(n); }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_mockExam2_p5.dfy
272
272
Dafny program: 272
// problem 6: // name: Gabriele Berardi // s-number: s4878728 // table: XXXXX ghost function f(n: int): int { if n <= 0 then 1 else n + f(n-1)*f(n-2) } ghost function fSum(n: nat): int { // give the body of this function // it should return Sum(i: 0<=i < n: f(i)) if n <= 0 then 0 else f(n-1) + fSum(n-1) } method problem6(n:nat) returns (a: int) ensures a == fSum(n) { a := 0; var k := 0; var x := 1; var y := 2; while k < n { k := k + 1; a := a + x; x, y := y, k+1 + x*y; } }
// problem 6: // name: Gabriele Berardi // s-number: s4878728 // table: XXXXX ghost function f(n: int): int { if n <= 0 then 1 else n + f(n-1)*f(n-2) } ghost function fSum(n: nat): int { // give the body of this function // it should return Sum(i: 0<=i < n: f(i)) if n <= 0 then 0 else f(n-1) + fSum(n-1) } method problem6(n:nat) returns (a: int) ensures a == fSum(n) { a := 0; var k := 0; var x := 1; var y := 2; while k < n invariant 0 <= k <= n && x == f(k) && y == f(k+1) && a == fSum(k) decreases n-k { assert x == f(k) && y == f(k+1) && a == fSum(k); k := k + 1; assert x == f(k-1) && y == f(k) && a == fSum(k-1); assert x == f(k-1) && y == f(k) && a == fSum(k) - f(k-1); a := a + x; assert x == f(k-1) && y == f(k) && a == fSum(k) - f(k-1) + f(k-1); assert x == f(k-1) && y == f(k) && a == fSum(k); x, y := y, k+1 + x*y; assert x == f(k) && y == k+1+f(k-1)*f(k) && a == fSum(k); assert x == f(k) && y == k+1+f(k+1-2)*f(k+1-1) && a == fSum(k); assert x == f(k) && y == f(k+1) && a == fSum(k); } assert a == fSum(k); }
Prog-Fun-Solutions_tmp_tmp7_gmnz5f_mockExam2_p6.dfy
273
273
Dafny program: 273
// RUN: /print:log.bpl method ArrayMap<A>(f: int -> A, a: array<A>) requires a != null requires forall j :: 0 <= j < a.Length ==> f.requires(j) requires forall j :: 0 <= j < a.Length ==> a !in f.reads(j) modifies a ensures forall j :: 0 <= j < a.Length ==> a[j] == f(j) { var i := 0; while i < a.Length { a[i] := f(i); i := i + 1; } } /*method GenericSort<A>(cmp: (A, A) -> bool, a: array<A>) requires a != null requires forall x, y :: a !in cmp.reads(x, y) requires forall x, y :: cmp.requires(x, y) modifies a ensures forall x, y :: cmp.requires(x, y) ensures forall x, y :: 0 <= x < y < a.Length ==> cmp(a[x], a[y]) { var i := 0; while i < a.Length modifies a { var j := i - 1; while j >= 0 && !cmp(a[j], a[i]) modifies a { a[i], a[j] := a[j], a[i]; j := j - 1; } i := i + 1; } }*/
// RUN: /print:log.bpl method ArrayMap<A>(f: int -> A, a: array<A>) requires a != null requires forall j :: 0 <= j < a.Length ==> f.requires(j) requires forall j :: 0 <= j < a.Length ==> a !in f.reads(j) modifies a ensures forall j :: 0 <= j < a.Length ==> a[j] == f(j) { var i := 0; while i < a.Length invariant 0 <= i <= a.Length; invariant forall j :: 0 <= j < i ==> a[j] == f(j) { a[i] := f(i); i := i + 1; } } /*method GenericSort<A>(cmp: (A, A) -> bool, a: array<A>) requires a != null requires forall x, y :: a !in cmp.reads(x, y) requires forall x, y :: cmp.requires(x, y) modifies a ensures forall x, y :: cmp.requires(x, y) ensures forall x, y :: 0 <= x < y < a.Length ==> cmp(a[x], a[y]) { var i := 0; while i < a.Length modifies a { var j := i - 1; while j >= 0 && !cmp(a[j], a[i]) modifies a { a[i], a[j] := a[j], a[i]; j := j - 1; } i := i + 1; } }*/
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_ArrayMap.dfy
275
275
Dafny program: 275
method GenericMax<A>(cmp: (A, A) -> bool, a: array<A>) returns (max: A) requires a != null && a.Length > 0 requires forall x, y :: cmp.requires(x, y) requires forall x, y :: cmp(x, y) || cmp(y, x); requires forall x, y, z :: cmp(x, y) && cmp(y, z) ==> cmp(x, z); ensures forall x :: 0 <= x < a.Length ==> // uncommenting the following line causes the program to verify // assume cmp.requires(a[x], max); cmp(a[x], max) { max := a[0]; var i := 0; while i < a.Length { if !cmp(a[i], max) { max := a[i]; } i := i + 1; } }
method GenericMax<A>(cmp: (A, A) -> bool, a: array<A>) returns (max: A) requires a != null && a.Length > 0 requires forall x, y :: cmp.requires(x, y) requires forall x, y :: cmp(x, y) || cmp(y, x); requires forall x, y, z :: cmp(x, y) && cmp(y, z) ==> cmp(x, z); ensures forall x :: 0 <= x < a.Length ==> // uncommenting the following line causes the program to verify // assume cmp.requires(a[x], max); cmp(a[x], max) { max := a[0]; var i := 0; while i < a.Length invariant 0 <= i <= a.Length invariant forall x :: 0 <= x < i ==> cmp(a[x], max) { if !cmp(a[i], max) { max := a[i]; } i := i + 1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_GenericMax.dfy
276
276
Dafny program: 276
predicate sorted (a:array<int>, start:int, end:int) // all "before" end are sorted requires a!=null requires 0<=start<=end<=a.Length reads a { forall j,k:: start<=j<k<end ==> a[j]<=a[k] } method InsertionSort (a:array<int>) requires a!=null && a.Length>1 ensures sorted(a, 0, a.Length) modifies a { var up := 1; while (up < a.Length) // outer loop { var down := up-1; var temp := a[up]; while down >= 0 && a[down+1] < a[down] // inner loop { a[down],a[down+1] := a[down+1],a[down]; down := down-1; } up := up+1; } } method Main(){ var a := new int[5]; a[0],a[1],a[2],a[3],a[4] := 3,2,5,1,8; InsertionSort(a); print a[..]; }
predicate sorted (a:array<int>, start:int, end:int) // all "before" end are sorted requires a!=null requires 0<=start<=end<=a.Length reads a { forall j,k:: start<=j<k<end ==> a[j]<=a[k] } method InsertionSort (a:array<int>) requires a!=null && a.Length>1 ensures sorted(a, 0, a.Length) modifies a { var up := 1; while (up < a.Length) // outer loop invariant 1 <= up <= a.Length invariant sorted(a,0,up) { var down := up-1; var temp := a[up]; while down >= 0 && a[down+1] < a[down] // inner loop invariant forall j,k | 0 <= j < k < up+1 && k != down+1 :: a[j]<=a[k] { a[down],a[down+1] := a[down+1],a[down]; down := down-1; } up := up+1; } } method Main(){ var a := new int[5]; a[0],a[1],a[2],a[3],a[4] := 3,2,5,1,8; InsertionSort(a); print a[..]; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_InsertionSort.dfy
277
277
Dafny program: 277
function RowColumnProduct(m1: array2<int>, m2: array2<int>, row: nat, column: nat): int reads m1 reads m2 requires m1 != null && m2 != null && m1.Length1 == m2.Length0 requires row < m1.Length0 && column < m2.Length1 { RowColumnProductFrom(m1, m2, row, column, 0) } function RowColumnProductFrom(m1: array2<int>, m2: array2<int>, row: nat, column: nat, k: nat): int reads m1 reads m2 requires m1 != null && m2 != null && k <= m1.Length1 == m2.Length0 requires row < m1.Length0 && column < m2.Length1 { if k == m1.Length1 then 0 else m1[row,k]*m2[k,column] + RowColumnProductFrom(m1, m2, row, column, k+1) } method multiply(m1: array2<int>, m2: array2<int>) returns (m3: array2<int>) requires m1 != null && m2 != null requires m1.Length1 == m2.Length0 ensures m3 != null && m3.Length0 == m1.Length0 && m3.Length1 == m2.Length1 ensures forall i, j | 0 <= i < m3.Length0 && 0 <= j < m3.Length1 :: m3[i, j] == RowColumnProduct(m1, m2, i, j) { m3 := new int[m1.Length0, m2.Length1]; var i := 0; while i < m1.Length0 m3[i',j'] == RowColumnProduct(m1, m2, i', j') { var j := 0; while j < m2.Length1 m3[i',j'] == RowColumnProduct(m1, m2, i', j') m3[i,j'] == RowColumnProduct(m1, m2, i, j') { var k :=0; m3[i, j] := 0; while k < m1.Length1 m3[i',j'] == RowColumnProduct(m1, m2, i', j') m3[i,j'] == RowColumnProduct(m1, m2, i, j') m3[i,j] + RowColumnProductFrom(m1, m2, i, j, k) { m3[i,j] := m3[i,j] + m1[i,k] * m2[k,j]; k := k+1; } j := j+1; } i := i+1; } }
function RowColumnProduct(m1: array2<int>, m2: array2<int>, row: nat, column: nat): int reads m1 reads m2 requires m1 != null && m2 != null && m1.Length1 == m2.Length0 requires row < m1.Length0 && column < m2.Length1 { RowColumnProductFrom(m1, m2, row, column, 0) } function RowColumnProductFrom(m1: array2<int>, m2: array2<int>, row: nat, column: nat, k: nat): int reads m1 reads m2 requires m1 != null && m2 != null && k <= m1.Length1 == m2.Length0 requires row < m1.Length0 && column < m2.Length1 decreases m1.Length1 - k { if k == m1.Length1 then 0 else m1[row,k]*m2[k,column] + RowColumnProductFrom(m1, m2, row, column, k+1) } method multiply(m1: array2<int>, m2: array2<int>) returns (m3: array2<int>) requires m1 != null && m2 != null requires m1.Length1 == m2.Length0 ensures m3 != null && m3.Length0 == m1.Length0 && m3.Length1 == m2.Length1 ensures forall i, j | 0 <= i < m3.Length0 && 0 <= j < m3.Length1 :: m3[i, j] == RowColumnProduct(m1, m2, i, j) { m3 := new int[m1.Length0, m2.Length1]; var i := 0; while i < m1.Length0 invariant 0 <= i <= m1.Length0 invariant forall i', j' | 0 <= i' < i && 0 <= j' < m2.Length1 :: m3[i',j'] == RowColumnProduct(m1, m2, i', j') { var j := 0; while j < m2.Length1 invariant 0 <= j <= m2.Length1 invariant forall i', j' | 0 <= i' < i && 0 <= j' < m2.Length1 :: m3[i',j'] == RowColumnProduct(m1, m2, i', j') invariant forall j' | 0 <= j' < j :: m3[i,j'] == RowColumnProduct(m1, m2, i, j') { var k :=0; m3[i, j] := 0; while k < m1.Length1 invariant 0 <= k <= m1.Length1 invariant forall i', j' | 0 <= i' < i && 0 <= j' < m2.Length1 :: m3[i',j'] == RowColumnProduct(m1, m2, i', j') invariant forall j' | 0 <= j' < j :: m3[i,j'] == RowColumnProduct(m1, m2, i, j') invariant RowColumnProduct(m1, m2, i, j) == m3[i,j] + RowColumnProductFrom(m1, m2, i, j, k) { m3[i,j] := m3[i,j] + m1[i,k] * m2[k,j]; k := k+1; } j := j+1; } i := i+1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_MatrixMultiplication.dfy
279
279
Dafny program: 279
// RUN: /compile:0 /nologo method CardinalitySubsetLt<T>(A: set<T>, B: set<T>) requires A < B ensures |A| < |B| { var b :| b in B && b !in A; var B' := B - {b}; if A < B' { CardinalitySubsetLt(A, B'); } else { } } method strategy<T>(P: set<T>, Special: T) returns (count: int) requires |P| > 1 && Special in P ensures count == |P| - 1 { count := 0; var I := {}; var S := {}; var switch := false; while count < |P| - 1 { var p :| p in P; I := I + {p}; if p == Special { if switch { switch := false; count := count + 1; } } else { if p !in S && !switch { S := S + {p}; switch := true; } } } CardinalitySubsetLt(S, I); if I < P { CardinalitySubsetLt(I, P); } }
// RUN: /compile:0 /nologo method CardinalitySubsetLt<T>(A: set<T>, B: set<T>) requires A < B ensures |A| < |B| decreases B { var b :| b in B && b !in A; var B' := B - {b}; assert |B| == |B'| + 1; if A < B' { CardinalitySubsetLt(A, B'); } else { assert A == B'; } } method strategy<T>(P: set<T>, Special: T) returns (count: int) requires |P| > 1 && Special in P ensures count == |P| - 1 decreases * { count := 0; var I := {}; var S := {}; var switch := false; while count < |P| - 1 invariant count <= |P| - 1 invariant count > 0 ==> Special in I invariant Special !in S && S < P && S <= I <= P invariant if switch then |S| == count + 1 else |S| == count decreases * { var p :| p in P; I := I + {p}; if p == Special { if switch { switch := false; count := count + 1; } } else { if p !in S && !switch { S := S + {p}; switch := true; } } } CardinalitySubsetLt(S, I); if I < P { CardinalitySubsetLt(I, P); } assert P <= I; assert I == P; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_OneHundredPrisonersAndALightbulb.dfy
280
280
Dafny program: 280
// Sum of elements of A from indices 0 to end. // end is inclusive! (not James's normal way of thinking!!) function SumUpto(A: array<real>, end: int): real requires -1 <= end < A.Length reads A { if end == -1 then 0.0 else A[end] + SumUpto(A, end-1) } function Sum(A: array<real>): real reads A { SumUpto(A, A.Length-1) } method Percentile(p: real, A: array<real>, total: real) returns (i: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires 0.0 <= p <= 100.0 requires total == Sum(A) requires total > 0.0 ensures -1 <= i < A.Length ensures SumUpto(A, i) <= (p/100.0) * total ensures i+1 < A.Length ==> SumUpto(A, i+1) > (p/100.0) * total { i := -1; var s: real := 0.0; while i+1 != A.Length && s + A[i+1] <= (p/100.0) * total { i := i + 1; s := s + A[i]; } } // example showing that, with the original postcondition, the answer is non-unique! method PercentileNonUniqueAnswer() returns (p: real, A: array<real>, total: real, i1: int, i2: int) ensures forall i | 0 <= i < A.Length :: A[i] > 0.0 ensures 0.0 <= p <= 100.0 ensures total == Sum(A) ensures total > 0.0 ensures -1 <= i1 < A.Length ensures SumUpto(A, i1) <= (p/100.0) * total ensures i1+1 < A.Length ==> SumUpto(A, i1+1) >= (p/100.0) * total ensures -1 <= i2 < A.Length ensures SumUpto(A, i2) <= (p/100.0) * total ensures i2+1 < A.Length ==> SumUpto(A, i2+1) >= (p/100.0) * total ensures i1 != i2 { p := 100.0; A := new real[1]; A[0] := 1.0; total := 1.0; i1 := -1; i2 := 0; } // proof that, with the corrected postcondition, the answer is unique lemma PercentileUniqueAnswer(p: real, A: array<real>, total: real, i1: int, i2: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires 0.0 <= p <= 100.0 requires total == Sum(A) requires total > 0.0 requires -1 <= i1 < A.Length requires SumUpto(A, i1) <= (p/100.0) * total requires i1+1 < A.Length ==> SumUpto(A, i1+1) > (p/100.0) * total requires -1 <= i2 < A.Length requires SumUpto(A, i2) <= (p/100.0) * total requires i2+1 < A.Length ==> SumUpto(A, i2+1) > (p/100.0) * total ensures i1 == i2 { if i1+1< i2 { SumUpto_increase(A, i1+1, i2); } } // lemma for previous proof: when an array has strictly positive elements, the // sums strictly increase left to right lemma SumUpto_increase(A: array<real>, end1: int, end2: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires -1 <= end1 < A.Length requires -1 <= end2 < A.Length requires end1 < end2 ensures SumUpto(A, end1) < SumUpto(A, end2) {}
// Sum of elements of A from indices 0 to end. // end is inclusive! (not James's normal way of thinking!!) function SumUpto(A: array<real>, end: int): real requires -1 <= end < A.Length reads A { if end == -1 then 0.0 else A[end] + SumUpto(A, end-1) } function Sum(A: array<real>): real reads A { SumUpto(A, A.Length-1) } method Percentile(p: real, A: array<real>, total: real) returns (i: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires 0.0 <= p <= 100.0 requires total == Sum(A) requires total > 0.0 ensures -1 <= i < A.Length ensures SumUpto(A, i) <= (p/100.0) * total ensures i+1 < A.Length ==> SumUpto(A, i+1) > (p/100.0) * total { i := -1; var s: real := 0.0; while i+1 != A.Length && s + A[i+1] <= (p/100.0) * total invariant -1 <= i < A.Length invariant s == SumUpto(A, i) invariant s <= (p/100.0) * total { i := i + 1; s := s + A[i]; } } // example showing that, with the original postcondition, the answer is non-unique! method PercentileNonUniqueAnswer() returns (p: real, A: array<real>, total: real, i1: int, i2: int) ensures forall i | 0 <= i < A.Length :: A[i] > 0.0 ensures 0.0 <= p <= 100.0 ensures total == Sum(A) ensures total > 0.0 ensures -1 <= i1 < A.Length ensures SumUpto(A, i1) <= (p/100.0) * total ensures i1+1 < A.Length ==> SumUpto(A, i1+1) >= (p/100.0) * total ensures -1 <= i2 < A.Length ensures SumUpto(A, i2) <= (p/100.0) * total ensures i2+1 < A.Length ==> SumUpto(A, i2+1) >= (p/100.0) * total ensures i1 != i2 { p := 100.0; A := new real[1]; A[0] := 1.0; total := 1.0; assert SumUpto(A, 0) == 1.0; i1 := -1; i2 := 0; } // proof that, with the corrected postcondition, the answer is unique lemma PercentileUniqueAnswer(p: real, A: array<real>, total: real, i1: int, i2: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires 0.0 <= p <= 100.0 requires total == Sum(A) requires total > 0.0 requires -1 <= i1 < A.Length requires SumUpto(A, i1) <= (p/100.0) * total requires i1+1 < A.Length ==> SumUpto(A, i1+1) > (p/100.0) * total requires -1 <= i2 < A.Length requires SumUpto(A, i2) <= (p/100.0) * total requires i2+1 < A.Length ==> SumUpto(A, i2+1) > (p/100.0) * total decreases if i2 < i1 then 1 else 0 // wlog i1 <= i2 ensures i1 == i2 { if i1+1< i2 { SumUpto_increase(A, i1+1, i2); } } // lemma for previous proof: when an array has strictly positive elements, the // sums strictly increase left to right lemma SumUpto_increase(A: array<real>, end1: int, end2: int) requires forall i | 0 <= i < A.Length :: A[i] > 0.0 requires -1 <= end1 < A.Length requires -1 <= end2 < A.Length requires end1 < end2 ensures SumUpto(A, end1) < SumUpto(A, end2) {}
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_Percentile.dfy
282
282
Dafny program: 282
// RUN: /compile:0 predicate reversed (arr : array<char>, outarr: array<char>) requires arr != null && outarr != null //requires 0<=k<=arr.Length-1 requires arr.Length == outarr.Length reads arr, outarr { forall k :: 0<=k<=arr.Length-1 ==> outarr[k] == arr[(arr.Length-1-k)] } method yarra(arr : array<char>) returns (outarr : array<char>) requires arr != null && arr.Length > 0 ensures outarr != null && arr.Length == outarr.Length && reversed(arr,outarr) { var i:= 0; var j:= arr.Length-1; outarr := new char[arr.Length]; outarr[0] := arr[j]; i := i+1; j := j-1; while i<arr.Length && 0<=j<arr.Length { outarr[i] := arr[j]; i:=i+1; j:=j-1; } //return outarr; } method Main() { var s := ['a','b','a','b','a','b','a','b','a','b','a','b']; var a,b,c,d := new char[5], new char[5], new char[5], new char[5]; a[0], a[1], a[2], a[3], a[4] := 'y', 'a', 'r', 'r', 'a'; d[0], d[1], d[2], d[3], d[4] := 'y', 'a', 'r', 'r', 'a'; b := yarra(a); c := yarra(b); //assert c.Length > -2; //assert d[0] == a[0]; //print c; print a; }
// RUN: /compile:0 predicate reversed (arr : array<char>, outarr: array<char>) requires arr != null && outarr != null //requires 0<=k<=arr.Length-1 requires arr.Length == outarr.Length reads arr, outarr { forall k :: 0<=k<=arr.Length-1 ==> outarr[k] == arr[(arr.Length-1-k)] } method yarra(arr : array<char>) returns (outarr : array<char>) requires arr != null && arr.Length > 0 ensures outarr != null && arr.Length == outarr.Length && reversed(arr,outarr) { var i:= 0; var j:= arr.Length-1; outarr := new char[arr.Length]; outarr[0] := arr[j]; i := i+1; j := j-1; while i<arr.Length && 0<=j<arr.Length invariant 0<=i<=arr.Length invariant j == arr.Length - 1 - i invariant forall k | 0 <= k < i :: outarr[k] == arr[arr.Length-1-k] decreases arr.Length-i, j { outarr[i] := arr[j]; i:=i+1; j:=j-1; } //return outarr; } method Main() { var s := ['a','b','a','b','a','b','a','b','a','b','a','b']; var a,b,c,d := new char[5], new char[5], new char[5], new char[5]; a[0], a[1], a[2], a[3], a[4] := 'y', 'a', 'r', 'r', 'a'; d[0], d[1], d[2], d[3], d[4] := 'y', 'a', 'r', 'r', 'a'; b := yarra(a); c := yarra(b); assert c[..] == a[..]; //assert c.Length > -2; //assert d[0] == a[0]; //print c; print a; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_ReverseString.dfy
283
283
Dafny program: 283
method Partition(a: array<int>) returns (lo: int, hi: int) modifies a ensures 0 <= lo <= hi <= a.Length ensures forall x | 0 <= x < lo :: a[x] < 0 ensures forall x | lo <= x < hi :: a[x] == 0 ensures forall x | hi <= x < a.Length :: a[x] > 0 { var i := 0; var j := a.Length; var k := a.Length; while i < j { if a[i] < 0 { i := i + 1; } else if a[i] == 0 { var current := a[i]; a[i] := a[j-1]; a[j-1] := current; j := j - 1; } else { var current := a[i]; a[i] := a[j-1]; a[j-1] := a[k-1]; a[k-1] := current; j := j - 1; k := k - 1; } } return i, k; }
method Partition(a: array<int>) returns (lo: int, hi: int) modifies a ensures 0 <= lo <= hi <= a.Length ensures forall x | 0 <= x < lo :: a[x] < 0 ensures forall x | lo <= x < hi :: a[x] == 0 ensures forall x | hi <= x < a.Length :: a[x] > 0 { var i := 0; var j := a.Length; var k := a.Length; while i < j invariant 0 <= i <= j <= k <= a.Length invariant forall x | 0 <= x < i :: a[x] < 0 invariant forall x | j <= x < k :: a[x] == 0 invariant forall x | k <= x < a.Length :: a[x] > 0 { if a[i] < 0 { i := i + 1; } else if a[i] == 0 { var current := a[i]; a[i] := a[j-1]; a[j-1] := current; j := j - 1; } else { assert a[i] > 0; var current := a[i]; a[i] := a[j-1]; a[j-1] := a[k-1]; a[k-1] := current; j := j - 1; k := k - 1; } } return i, k; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_advanced examples_demo.dfy
284
284
Dafny program: 284
predicate SplitPoint(a: array<int>, n: int) reads a requires 0 <= n <= n { forall i,j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] } method SelectionSort(a: array<int>) modifies a ensures forall i,j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { var n := 0; while n != a.Length // invariant forall i,j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] { var mindex, m := n, n; while m != a.Length { if a[m] < a[mindex] { mindex := m; } m := m + 1; } a[n], a[mindex] := a[mindex], a[n]; n := n + 1; } } method QuickSort(a: array<int>) modifies a ensures forall i,j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { QuickSortAux(a, 0, a.Length); } twostate predicate SwapFrame(a: array<int>, lo: int, hi: int) requires 0 <= lo <= hi <= a.Length reads a { (forall i :: 0 <= i < lo || hi <= i < a.Length ==> a[i] == old(a[i])) && multiset(a[..]) == old(multiset(a[..])) } method QuickSortAux(a: array<int>, lo: int, hi: int) requires 0 <= lo <= hi <= a.Length requires SplitPoint(a, lo) && SplitPoint(a, hi) modifies a ensures forall i,j :: lo <= i < j < hi ==> a[i] <= a[j] ensures SwapFrame(a, lo, hi) ensures SplitPoint(a, lo) && SplitPoint(a, hi) { if 2 <= hi - lo { var p := Partition(a, lo, hi); QuickSortAux(a, lo, p); QuickSortAux(a, p + 1, hi); } } method Partition(a: array<int>, lo: int, hi: int) returns (p: int) requires 0 <= lo < hi <= a.Length requires SplitPoint(a, lo) && SplitPoint(a, hi) modifies a ensures lo <= p < hi ensures forall i :: lo <= i < p ==> a[i] < a[p] ensures forall i :: p <= i < hi ==> a[p] <= a[i] ensures SplitPoint(a, lo) && SplitPoint(a, hi) ensures SwapFrame(a, lo, hi) { var pivot := a[lo]; var m, n := lo + 1, hi; while m < n { if a[m] < pivot { m := m + 1; } else { a[m], a[n-1] := a[n-1], a[m]; n := n - 1; } } a[lo], a[m - 1] := a[m - 1], a[lo]; return m - 1; }
predicate SplitPoint(a: array<int>, n: int) reads a requires 0 <= n <= n { forall i,j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] } method SelectionSort(a: array<int>) modifies a ensures forall i,j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { var n := 0; while n != a.Length invariant 0 <= n <= a.Length invariant forall i,j :: 0 <= i < j < n ==> a[i] <= a[j] // invariant forall i,j :: 0 <= i < n <= j < a.Length ==> a[i] <= a[j] invariant SplitPoint(a, n) invariant multiset(a[..]) == old(multiset(a[..])) { var mindex, m := n, n; while m != a.Length invariant n <= m <= a.Length && n <= mindex < a.Length invariant forall i :: n <= i < m ==> a[mindex] <= a[i] { if a[m] < a[mindex] { mindex := m; } m := m + 1; } a[n], a[mindex] := a[mindex], a[n]; assert forall i,j :: 0 <= i < j < n ==> a[i] <= a[j]; n := n + 1; } } method QuickSort(a: array<int>) modifies a ensures forall i,j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures multiset(a[..]) == old(multiset(a[..])) { QuickSortAux(a, 0, a.Length); } twostate predicate SwapFrame(a: array<int>, lo: int, hi: int) requires 0 <= lo <= hi <= a.Length reads a { (forall i :: 0 <= i < lo || hi <= i < a.Length ==> a[i] == old(a[i])) && multiset(a[..]) == old(multiset(a[..])) } method QuickSortAux(a: array<int>, lo: int, hi: int) requires 0 <= lo <= hi <= a.Length requires SplitPoint(a, lo) && SplitPoint(a, hi) modifies a ensures forall i,j :: lo <= i < j < hi ==> a[i] <= a[j] ensures SwapFrame(a, lo, hi) ensures SplitPoint(a, lo) && SplitPoint(a, hi) decreases hi - lo { if 2 <= hi - lo { var p := Partition(a, lo, hi); QuickSortAux(a, lo, p); QuickSortAux(a, p + 1, hi); } } method Partition(a: array<int>, lo: int, hi: int) returns (p: int) requires 0 <= lo < hi <= a.Length requires SplitPoint(a, lo) && SplitPoint(a, hi) modifies a ensures lo <= p < hi ensures forall i :: lo <= i < p ==> a[i] < a[p] ensures forall i :: p <= i < hi ==> a[p] <= a[i] ensures SplitPoint(a, lo) && SplitPoint(a, hi) ensures SwapFrame(a, lo, hi) { var pivot := a[lo]; var m, n := lo + 1, hi; while m < n invariant lo + 1 <= m <= n <= hi invariant a[lo] == pivot invariant forall i :: lo + 1 <= i < m ==> a[i] < pivot invariant forall i :: n <= i < hi ==> pivot <= a[i] invariant SplitPoint(a, lo) && SplitPoint(a, hi) invariant SwapFrame(a, lo, hi) { if a[m] < pivot { m := m + 1; } else { a[m], a[n-1] := a[n-1], a[m]; n := n - 1; } } a[lo], a[m - 1] := a[m - 1], a[lo]; return m - 1; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_ProgramProofs_ch15.dfy
285
285
Dafny program: 285
//https://stackoverflow.com/questions/69364687/how-to-prove-time-complexity-of-bubble-sort-using-dafny function NChoose2(n: int): int { n * (n - 1) / 2 } // sum of all integers in the range [lo, hi) // (inclusive of lo, exclusive of hi) function SumRange(lo: int, hi: int): int { if lo >= hi then 0 else SumRange(lo, hi - 1) + hi - 1 } // dafny proves this automatically by induction lemma SumRangeNChoose2(n: nat) ensures SumRange(0, n) == NChoose2(n) {} // dafny proves this automatically by induction // (given the correct decreases clause) lemma SumRangeUnrollLeft(lo: int, hi: int) ensures SumRange(lo, hi) == if lo >= hi then 0 else lo + SumRange(lo + 1, hi) {} method BubbleSort(a: array<int>) returns (n: nat) modifies a ensures n <= NChoose2(a.Length) { // it simplifies the remaining invariants to handle the empty array here if a.Length == 0 { return 0; } var i := a.Length - 1; n := 0; while i > 0 { var j := 0; while j < i { if a[j] > a[j+1] { a[j], a[j+1] := a[j+1], a[j]; n := n + 1; } j := j + 1; } SumRangeUnrollLeft(i, a.Length); // see lemma below } i := i - 1; } calc <= { n; // less or equal to next line by the loop invariant SumRange(1, a.Length); { SumRangeUnrollLeft(0, a.Length); } SumRange(0, a.Length); { SumRangeNChoose2(a.Length); } // see lemma below NChoose2(a.Length); } }
//https://stackoverflow.com/questions/69364687/how-to-prove-time-complexity-of-bubble-sort-using-dafny function NChoose2(n: int): int { n * (n - 1) / 2 } // sum of all integers in the range [lo, hi) // (inclusive of lo, exclusive of hi) function SumRange(lo: int, hi: int): int decreases hi - lo { if lo >= hi then 0 else SumRange(lo, hi - 1) + hi - 1 } // dafny proves this automatically by induction lemma SumRangeNChoose2(n: nat) ensures SumRange(0, n) == NChoose2(n) {} // dafny proves this automatically by induction // (given the correct decreases clause) lemma SumRangeUnrollLeft(lo: int, hi: int) decreases hi - lo ensures SumRange(lo, hi) == if lo >= hi then 0 else lo + SumRange(lo + 1, hi) {} method BubbleSort(a: array<int>) returns (n: nat) modifies a ensures n <= NChoose2(a.Length) { // it simplifies the remaining invariants to handle the empty array here if a.Length == 0 { return 0; } var i := a.Length - 1; n := 0; while i > 0 invariant 0 <= i < a.Length invariant n <= SumRange(i+1, a.Length) { var j := 0; while j < i invariant n <= SumRange(i+1, a.Length) + j invariant j <= i { if a[j] > a[j+1] { a[j], a[j+1] := a[j+1], a[j]; n := n + 1; } j := j + 1; } assert n <= SumRange(i, a.Length) by { SumRangeUnrollLeft(i, a.Length); // see lemma below } i := i - 1; } calc <= { n; // less or equal to next line by the loop invariant SumRange(1, a.Length); { SumRangeUnrollLeft(0, a.Length); } SumRange(0, a.Length); { SumRangeNChoose2(a.Length); } // see lemma below NChoose2(a.Length); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_examples_bubblesort.dfy
286
286
Dafny program: 286
predicate IsEven (n: int) { n % 2 == 0 } method FindEvenNumbers (arr: array<int>) returns (evenNumbers: array<int>) ensures forall x :: x in arr[..] && IsEven(x) ==> x in evenNumbers[..]; ensures forall x :: x !in arr[..] ==> x !in evenNumbers[..] ensures forall k, l :: 0 <= k < l < evenNumbers.Length ==> exists n, m :: 0 <= n < m < arr.Length && evenNumbers[k] == arr[n] && evenNumbers[l] == arr[m] { var evenList: seq<int> := []; ghost var indices: seq<int> := []; for i := 0 to arr.Length exists n, m :: 0 <= n < m < i && evenList[k] == arr[n] && evenList[l] == arr[m] { if IsEven(arr[i]) { evenList := evenList + [arr[i]]; indices := indices + [i]; } } evenNumbers := new int[|evenList|](i requires 0 <= i < |evenList| => evenList[i]); }
predicate IsEven (n: int) { n % 2 == 0 } method FindEvenNumbers (arr: array<int>) returns (evenNumbers: array<int>) ensures forall x :: x in arr[..] && IsEven(x) ==> x in evenNumbers[..]; ensures forall x :: x !in arr[..] ==> x !in evenNumbers[..] ensures forall k, l :: 0 <= k < l < evenNumbers.Length ==> exists n, m :: 0 <= n < m < arr.Length && evenNumbers[k] == arr[n] && evenNumbers[l] == arr[m] { var evenList: seq<int> := []; ghost var indices: seq<int> := []; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant 0 <= |evenList| <= i invariant forall x :: x in arr[..i] && IsEven(x) ==> x in evenList[..] invariant forall x :: x !in arr[..i] ==> x !in evenList invariant |evenList| == |indices| invariant forall k :: 0 <= k < |indices| ==> indices[k] < i invariant forall k, l :: 0 <= k < l < |indices| ==> indices[k] < indices[l] invariant forall k :: 0 <= k < |evenList| ==> 0 <= indices[k] < i <= arr.Length && arr[indices[k]] == evenList[k] invariant forall k, l :: 0 <= k < l < |evenList| ==> exists n, m :: 0 <= n < m < i && evenList[k] == arr[n] && evenList[l] == arr[m] { if IsEven(arr[i]) { evenList := evenList + [arr[i]]; indices := indices + [i]; } } evenNumbers := new int[|evenList|](i requires 0 <= i < |evenList| => evenList[i]); assert evenList == evenNumbers[..]; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_examples_relativeOrder.dfy
287
287
Dafny program: 287
method Foo(y: int, x: int) returns (z: int) requires 0 <= y ensures z == x*y { var a: int := 0; z := 0; while a != y { z := z + x; a := a + 1; } return z; } function stringToSet(s: string): (r: set<char>) ensures forall x :: 0 <= x < |s| ==> s[x] in r { set x | 0 <= x < |s| :: s[x] } //ensures forall a, b :: 0 <= a < b < |s| ==> forall k, j :: a <=k < j <=b ==> k !=j ==> s[k] != s[j] ==> b-a <= longest // lemma stringSet(s: string) // // { // if |s| != 0 { // } // } method Main() { var sample: string := "test"; var foof := Foo(3,4); var test: set<char> := stringToSet(sample); // var test := set x | 0 <= x < |sample| :: sample[x]; print foof; }
method Foo(y: int, x: int) returns (z: int) requires 0 <= y ensures z == x*y { var a: int := 0; z := 0; while a != y invariant 0 <= a <= y invariant z == a*x decreases y-a { z := z + x; a := a + 1; } return z; } function stringToSet(s: string): (r: set<char>) ensures forall x :: 0 <= x < |s| ==> s[x] in r { set x | 0 <= x < |s| :: s[x] } //ensures forall a, b :: 0 <= a < b < |s| ==> forall k, j :: a <=k < j <=b ==> k !=j ==> s[k] != s[j] ==> b-a <= longest // lemma stringSet(s: string) // // { // if |s| != 0 { // } // } method Main() { var sample: string := "test"; var foof := Foo(3,4); var test: set<char> := stringToSet(sample); // var test := set x | 0 <= x < |sample| :: sample[x]; assert test == stringToSet(sample); assert forall x :: 0 <= x < |sample| ==> sample[x] in test; assert 't' in sample; assert 't' in test; print foof; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_examples_simpleMultiplication.dfy
289
289
Dafny program: 289
/* function numRescueBoats(people: number[], limit: number): number { //people.sort((a,b) => a-b); binsort(people, limit); let boats = 0; let lower = 0, upper = people.length-1; while(lower <= upper) { if(people[upper] == limit || people[upper]+people[lower] > limit) { boats++ upper--; }else if(people[upper]+people[lower] <= limit) { upper--; lower++; boats++; } } return boats; }; nums[k++] = i+1; function binsort(nums: number[], limit: number) { let result = new Array(limit); result.fill(0); for(let i = 0; i < nums.length; i++) { result[nums[i]-1]++; } var k = 0; for(let i=0; i < result.length; i++) { for(let j = 0; j < result[i]; j++) { nums[k++] = i+1; } } } */ function sumBoat(s: seq<nat>): nat requires 1 <= |s| <= 2 { if |s| == 1 then s[0] else s[0] + s[1] } predicate isSafeBoat(boat: seq<nat>, limit: nat) { 1 <= |boat| <= 2 && sumBoat(boat) <= limit } function multisetAdd(ss: seq<seq<nat>>): multiset<nat> { if ss == [] then multiset{} else multiset(ss[0]) + multisetAdd(ss[1..]) } predicate multisetEqual(ss: seq<seq<nat>>, xs: seq<nat>) { multiset(xs) == multisetAdd(ss) } predicate allSafe(boats: seq<seq<nat>>, limit: nat) { forall boat :: boat in boats ==> isSafeBoat(boat, limit) } predicate sorted(list: seq<int>) { forall i,j :: 0 <= i < j < |list| ==> list[i] <= list[j] } method numRescueBoats(people: seq<nat>, limit: nat) returns (boats: nat) requires |people| >= 1 requires sorted(people) requires forall i: nat :: i < |people| ==> 1 <= people[i] <= limit ensures exists boatConfig: seq<seq<nat>> :: multisetEqual(boatConfig, people) && allSafe(boatConfig, limit) && boats == |boatConfig|// && forall boatConfigs :: multisetEqual(boatConfigs, people) && allSafe(boatConfigs, limit) ==> boats <= |boatConfigs| { boats := 0; var lower: nat := 0; var upper: int := |people| - 1; ghost var visitedUpper: multiset<nat> := multiset{}; ghost var visitedLower: multiset<nat> := multiset{}; ghost var remaining: multiset<nat> := multiset(people); ghost var safeBoats: seq<seq<nat>> := []; while lower <= upper { if people[upper] == limit || people[upper] + people[lower] > limit { boats := boats + 1; safeBoats := [[people[upper]]] + safeBoats; ghost var gu := people[upper+1..]; visitedUpper := visitedUpper + multiset{people[upper]}; upper := upper - 1; }else{ ghost var gl := people[..lower]; boats := boats + 1; if lower == upper { visitedLower := visitedLower + multiset{people[lower]}; safeBoats := [[people[lower]]] + safeBoats; }else{ ghost var gu := people[upper+1..]; visitedUpper := visitedUpper + multiset{people[upper]}; visitedLower := visitedLower + multiset{people[lower]}; safeBoats := [[people[upper], people[lower]]] + safeBoats; upper := upper - 1; } lower := lower + 1; } } } /* limit 3 [3,2,2,1] lower = 0 upper = 3 upper = 2 lower= 0 lower = 1 upper = 1 lower = 2 [..2] upper = 1 [2..] */
/* function numRescueBoats(people: number[], limit: number): number { //people.sort((a,b) => a-b); binsort(people, limit); let boats = 0; let lower = 0, upper = people.length-1; while(lower <= upper) { if(people[upper] == limit || people[upper]+people[lower] > limit) { boats++ upper--; }else if(people[upper]+people[lower] <= limit) { upper--; lower++; boats++; } } return boats; }; nums[k++] = i+1; function binsort(nums: number[], limit: number) { let result = new Array(limit); result.fill(0); for(let i = 0; i < nums.length; i++) { result[nums[i]-1]++; } var k = 0; for(let i=0; i < result.length; i++) { for(let j = 0; j < result[i]; j++) { nums[k++] = i+1; } } } */ function sumBoat(s: seq<nat>): nat requires 1 <= |s| <= 2 { if |s| == 1 then s[0] else s[0] + s[1] } predicate isSafeBoat(boat: seq<nat>, limit: nat) { 1 <= |boat| <= 2 && sumBoat(boat) <= limit } function multisetAdd(ss: seq<seq<nat>>): multiset<nat> { if ss == [] then multiset{} else multiset(ss[0]) + multisetAdd(ss[1..]) } predicate multisetEqual(ss: seq<seq<nat>>, xs: seq<nat>) { multiset(xs) == multisetAdd(ss) } predicate allSafe(boats: seq<seq<nat>>, limit: nat) { forall boat :: boat in boats ==> isSafeBoat(boat, limit) } predicate sorted(list: seq<int>) { forall i,j :: 0 <= i < j < |list| ==> list[i] <= list[j] } method numRescueBoats(people: seq<nat>, limit: nat) returns (boats: nat) requires |people| >= 1 requires sorted(people) requires forall i: nat :: i < |people| ==> 1 <= people[i] <= limit ensures exists boatConfig: seq<seq<nat>> :: multisetEqual(boatConfig, people) && allSafe(boatConfig, limit) && boats == |boatConfig|// && forall boatConfigs :: multisetEqual(boatConfigs, people) && allSafe(boatConfigs, limit) ==> boats <= |boatConfigs| { boats := 0; var lower: nat := 0; var upper: int := |people| - 1; ghost var visitedUpper: multiset<nat> := multiset{}; ghost var visitedLower: multiset<nat> := multiset{}; ghost var remaining: multiset<nat> := multiset(people); ghost var safeBoats: seq<seq<nat>> := []; while lower <= upper invariant 0 <= lower <= |people| invariant lower-1 <= upper < |people| invariant visitedUpper == multiset(people[upper+1..]) invariant visitedLower == multiset(people[..lower]) invariant allSafe(safeBoats, limit) invariant multisetAdd(safeBoats) == visitedLower + visitedUpper; invariant |safeBoats| == boats { if people[upper] == limit || people[upper] + people[lower] > limit { boats := boats + 1; assert isSafeBoat([people[upper]], limit); safeBoats := [[people[upper]]] + safeBoats; assert visitedUpper == multiset(people[upper+1..]); ghost var gu := people[upper+1..]; assert multiset(gu) == visitedUpper; assert people[upper..] == [people[upper]] + gu; visitedUpper := visitedUpper + multiset{people[upper]}; upper := upper - 1; assert people[(upper+1)..] == [people[upper+1]] + gu; }else{ ghost var gl := people[..lower]; boats := boats + 1; if lower == upper { visitedLower := visitedLower + multiset{people[lower]}; assert isSafeBoat([people[lower]], limit); safeBoats := [[people[lower]]] + safeBoats; }else{ ghost var gu := people[upper+1..]; assert multiset(gu) == visitedUpper; visitedUpper := visitedUpper + multiset{people[upper]}; visitedLower := visitedLower + multiset{people[lower]}; assert isSafeBoat([people[upper], people[lower]], limit); safeBoats := [[people[upper], people[lower]]] + safeBoats; upper := upper - 1; assert people[(upper+1)..] == [people[upper+1]] + gu; } lower := lower + 1; assert people[..lower] == gl + [people[lower-1]]; } } assert visitedLower == multiset(people[..lower]); assert visitedUpper == multiset(people[upper+1..]); assert upper+1 == lower; assert people == people[..lower] + people[upper+1..]; assert visitedLower + visitedUpper == multiset(people); } /* limit 3 [3,2,2,1] lower = 0 upper = 3 upper = 2 lower= 0 lower = 1 upper = 1 lower = 2 [..2] upper = 1 [2..] */
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_BoatsToSavePeople.dfy
290
290
Dafny program: 290
/* https://leetcode.com/problems/find-pivot-index/description/ Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array. Return the leftmost pivot index. If no such index exists, return -1. Example 1: Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11 Example 2: Input: nums = [1,2,3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement. Example 3: Input: nums = [2,1,-1] Output: 0 Explanation: The pivot index is 0. Left sum = 0 (no elements to the left of index 0) Right sum = nums[1] + nums[2] = 1 + -1 = 0 ```TypeScript function pivotIndex(nums: number[]): number { const n = nums.length; let leftsums = [0], rightsums = [0]; for(let i=1; i < n+1; i++) { leftsums.push(nums[i-1]+leftsums[i-1]); rightsums.push(nums[n-i]+rightsums[i-1]); } for(let i=0; i <= n; i++) { if(leftsums[i] == rightsums[n-(i+1)]) return i; } return -1; }; ``` */ function sum(nums: seq<int>): int { // if |nums| == 0 then 0 else nums[0]+sum(nums[1..]) if |nums| == 0 then 0 else sum(nums[0..(|nums|-1)])+nums[|nums|-1] } function sumUp(nums: seq<int>): int { if |nums| == 0 then 0 else nums[0]+sumUp(nums[1..]) } // By Divyanshu Ranjan lemma sumUpLemma(a: seq<int>, b: seq<int>) ensures sumUp(a + b) == sumUp(a) + sumUp(b) { if a == [] { } else { sumUpLemma(a[1..], b); calc { sumUp(a + b); { } a[0] + sumUp(a[1..] + b); a[0] + sumUp(a[1..]) + sumUp(b); } } } // By Divyanshu Ranjan lemma sumsEqual(nums: seq<int>) ensures sum(nums) == sumUp(nums) { if nums == [] {} else { var ln := |nums|-1; calc { sumUp(nums[..]); { sumUpLemma(nums[0..ln], [nums[ln]]); } sumUp(nums[0..ln]) + sumUp([nums[ln]]); { } sumUp(nums[0..ln]) + nums[ln]; { sumsEqual(nums[0..ln]); } sum(nums[0..ln]) + nums[ln]; } } } method FindPivotIndex(nums: seq<int>) returns (index: int) requires |nums| > 0 ensures index == -1 ==> forall k: nat :: k < |nums| ==> sum(nums[0..k]) != sum(nums[(k+1)..]) ensures 0 <= index < |nums| ==> sum(nums[0..index]) == sum(nums[(index+1)..]) { var leftsums: seq<int> := [0]; var rightsums: seq<int> := [0]; var i := 1; while i < |nums|+1 { leftsums := leftsums + [leftsums[i-1]+nums[i-1]]; rightsums := rightsums + [nums[|nums|-i]+rightsums[i-1]]; i := i + 1; } forall k: nat | 0 <= k < i && k <= |nums| ensures sumUp(nums[(|nums|-k)..]) == sum(nums[(|nums|-k)..]) ensures rightsums[k] == sumUp(nums[(|nums|-k)..]) { sumsEqual(nums[(|nums|-k)..]); } } i :=0; while i < |nums| { var x := |nums|-(i+1); if leftsums[i] == rightsums[x] { // assert rightsums[i+1] == sum(nums[(|nums|-(i+1))..]); return i; } i := i + 1; } return -1; }
/* https://leetcode.com/problems/find-pivot-index/description/ Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to the left of the index is equal to the sum of all the numbers strictly to the index's right. If the index is on the left edge of the array, then the left sum is 0 because there are no elements to the left. This also applies to the right edge of the array. Return the leftmost pivot index. If no such index exists, return -1. Example 1: Input: nums = [1,7,3,6,5,6] Output: 3 Explanation: The pivot index is 3. Left sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 Right sum = nums[4] + nums[5] = 5 + 6 = 11 Example 2: Input: nums = [1,2,3] Output: -1 Explanation: There is no index that satisfies the conditions in the problem statement. Example 3: Input: nums = [2,1,-1] Output: 0 Explanation: The pivot index is 0. Left sum = 0 (no elements to the left of index 0) Right sum = nums[1] + nums[2] = 1 + -1 = 0 ```TypeScript function pivotIndex(nums: number[]): number { const n = nums.length; let leftsums = [0], rightsums = [0]; for(let i=1; i < n+1; i++) { leftsums.push(nums[i-1]+leftsums[i-1]); rightsums.push(nums[n-i]+rightsums[i-1]); } for(let i=0; i <= n; i++) { if(leftsums[i] == rightsums[n-(i+1)]) return i; } return -1; }; ``` */ function sum(nums: seq<int>): int { // if |nums| == 0 then 0 else nums[0]+sum(nums[1..]) if |nums| == 0 then 0 else sum(nums[0..(|nums|-1)])+nums[|nums|-1] } function sumUp(nums: seq<int>): int { if |nums| == 0 then 0 else nums[0]+sumUp(nums[1..]) } // By Divyanshu Ranjan lemma sumUpLemma(a: seq<int>, b: seq<int>) ensures sumUp(a + b) == sumUp(a) + sumUp(b) { if a == [] { assert a + b == b; } else { sumUpLemma(a[1..], b); calc { sumUp(a + b); { assert (a + b)[0] == a[0]; assert (a + b)[1..] == a[1..] + b; } a[0] + sumUp(a[1..] + b); a[0] + sumUp(a[1..]) + sumUp(b); } } } // By Divyanshu Ranjan lemma sumsEqual(nums: seq<int>) decreases |nums| ensures sum(nums) == sumUp(nums) { if nums == [] {} else { var ln := |nums|-1; calc { sumUp(nums[..]); { assert nums[..] == nums[0..ln] + [nums[ln]]; sumUpLemma(nums[0..ln], [nums[ln]]); } sumUp(nums[0..ln]) + sumUp([nums[ln]]); { assert forall a:: sumUp([a]) == a; } sumUp(nums[0..ln]) + nums[ln]; { sumsEqual(nums[0..ln]); } sum(nums[0..ln]) + nums[ln]; } } } method FindPivotIndex(nums: seq<int>) returns (index: int) requires |nums| > 0 ensures index == -1 ==> forall k: nat :: k < |nums| ==> sum(nums[0..k]) != sum(nums[(k+1)..]) ensures 0 <= index < |nums| ==> sum(nums[0..index]) == sum(nums[(index+1)..]) { var leftsums: seq<int> := [0]; var rightsums: seq<int> := [0]; var i := 1; assert leftsums[0] == sum(nums[0..0]); assert rightsums[0] == sumUp(nums[|nums|..]); while i < |nums|+1 invariant 1 <= i <= |nums|+1 invariant |leftsums| == i invariant |rightsums| == i invariant forall k: nat :: 0 <= k < i && k <= |nums| ==> leftsums[k] == sum(nums[0..k]) invariant forall k: nat :: 0 <= k < i && k <= |nums| ==> rightsums[k] == sumUp(nums[(|nums|-k)..]) { leftsums := leftsums + [leftsums[i-1]+nums[i-1]]; assert nums[0..i] == nums[0..i-1]+[nums[i-1]]; rightsums := rightsums + [nums[|nums|-i]+rightsums[i-1]]; i := i + 1; } assert forall k: nat :: 0 <= k < i && k <= |nums| ==> rightsums[k] == sum(nums[(|nums|-k)..]) by { forall k: nat | 0 <= k < i && k <= |nums| ensures sumUp(nums[(|nums|-k)..]) == sum(nums[(|nums|-k)..]) ensures rightsums[k] == sumUp(nums[(|nums|-k)..]) { sumsEqual(nums[(|nums|-k)..]); } } i :=0; while i < |nums| invariant 0 <= i <= |nums| invariant forall k: nat :: k < i ==> sum(nums[0..k]) != sum(nums[(k+1)..]) { var x := |nums|-(i+1); if leftsums[i] == rightsums[x] { assert sum(nums[0..i]) == sum(nums[(i+1)..]); // assert rightsums[i+1] == sum(nums[(|nums|-(i+1))..]); return i; } assert sum(nums[0..i]) != sum(nums[(i+1)..]); i := i + 1; } return -1; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_FindPivotIndex.dfy
291
291
Dafny program: 291
/* https://leetcode.com/problems/reverse-linked-list/description/ * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } function reverseList(head: ListNode | null): ListNode | null { if (head == null) return null; let curr = head; let prev = null; while(curr !== null && curr.next !== null) { let next = curr.next; curr.next = prev; prev = curr; curr = next; } curr.next = prev; return curr; }; */ datatype ListNode = Null | Node(val: nat, next: ListNode) function reverse<A>(x: seq<A>): seq<A> { if x == [] then [] else reverse(x[1..])+[x[0]] } function nodeConcat(xs: ListNode, end: ListNode): ListNode { if xs == Null then end else Node(xs.val, nodeConcat(xs.next, end)) } function reverseList(xs: ListNode): ListNode { if xs == Null then Null else nodeConcat(reverseList(xs.next), Node(xs.val, Null)) } lemma ConcatNullIsRightIdentity(xs: ListNode) ensures xs == nodeConcat(xs, Null) { } lemma ConcatNullIsLeftIdentity(xs: ListNode) ensures xs == nodeConcat(Null, xs) { } lemma ConcatExtensionality(xs: ListNode) requires xs != Null ensures nodeConcat(Node(xs.val, Null), xs.next) == xs; { } lemma ConcatAssociative(xs: ListNode, ys: ListNode, zs: ListNode) ensures nodeConcat(nodeConcat(xs, ys), zs) == nodeConcat(xs, nodeConcat(ys, zs)) { } lemma reverseSingleList(xs: ListNode) requires xs != Null; requires xs.next == Null; ensures reverseList(xs) == xs; { } lemma {:verify true} ConcatReverseList(xs:ListNode, ys: ListNode) ensures reverseList(nodeConcat(xs,ys)) == nodeConcat(reverseList(ys), reverseList(xs)) { if xs == Null { calc { reverseList(nodeConcat(xs,ys)); == {ConcatNullIsLeftIdentity(ys);} reverseList(ys); == {ConcatNullIsRightIdentity(reverseList(ys));} nodeConcat(reverseList(ys), Null); nodeConcat(reverseList(ys), xs); nodeConcat(reverseList(ys), reverseList(xs)); } }else{ var x := Node(xs.val, Null); calc { reverseList(nodeConcat(xs, ys)); reverseList(nodeConcat(nodeConcat(x, xs.next), ys)); == {ConcatAssociative(x, xs.next, ys);} reverseList(nodeConcat(x, nodeConcat(xs.next, ys))); nodeConcat(reverseList(nodeConcat(xs.next, ys)), x); == {ConcatReverseList(xs.next, ys);} nodeConcat(nodeConcat(reverseList(ys) , reverseList(xs.next)), x); == {ConcatAssociative(reverseList(ys), reverseList(xs.next), x);} nodeConcat(reverseList(ys) , nodeConcat(reverseList(xs.next), x)); nodeConcat(reverseList(ys) , reverseList(xs)); } } } lemma reverseReverseListIsIdempotent(xs: ListNode) ensures reverseList(reverseList(xs)) == xs { if xs == Null { }else{ var x := Node(xs.val, Null); calc { reverseList(reverseList(xs)); reverseList(reverseList(nodeConcat(x, xs.next))); == {ConcatReverseList(x, xs.next);} reverseList(nodeConcat(reverseList(xs.next), reverseList(x))); reverseList(nodeConcat(reverseList(xs.next), x)); == {ConcatReverseList(reverseList(xs.next),x);} nodeConcat(reverseList(x), reverseList(reverseList(xs.next))); //dafny can figure out the rest from here nodeConcat(x, reverseList(reverseList(xs.next))); nodeConcat(x, xs.next); xs; } } } lemma {:induction false} reversePreservesMultiset<A>(xs: seq<A>) ensures multiset(xs) == multiset(reverse(xs)) { if xs == [] { }else { var x := xs[0]; reversePreservesMultiset(xs[1..]); } } lemma reversePreservesLength<A>(xs: seq<A>) ensures |xs| == |reverse(xs)| { } lemma lastReverseIsFirst<A>(xs: seq<A>) requires |xs| > 0 ensures xs[0] == reverse(xs)[|reverse(xs)|-1] { reversePreservesLength(xs); } lemma firstReverseIsLast<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs)[0] == xs[|xs|-1] { } lemma ReverseConcat<T>(xs: seq<T>, ys: seq<T>) ensures reverse(xs + ys) == reverse(ys) + reverse(xs) { // reveal Reverse(); if |xs| == 0 { } else { } } lemma reverseRest<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs) == [xs[ |xs| -1 ] ] + reverse(xs[0..|xs|-1]) { firstReverseIsLast(xs); calc { reverse(xs); reverse(xs[0..|xs|-1] + [xs[|xs|-1]]); == {ReverseConcat(xs[0..|xs|-1], [xs[ |xs|-1 ]]);} reverse([xs[ |xs|-1 ]]) + reverse(xs[0..|xs|-1]); } } lemma SeqEq<T>(xs: seq<T>, ys: seq<T>) requires |xs| == |ys| requires forall i :: 0 <= i < |xs| ==> xs[i] == ys[i] ensures xs == ys { } lemma ReverseIndexAll<T>(xs: seq<T>) ensures |reverse(xs)| == |xs| ensures forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1] { // reveal Reverse(); } lemma ReverseIndex<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[i] == xs[|xs| - i - 1] { ReverseIndexAll(xs); } lemma ReverseSingle<A>(xs: seq<A>) requires |xs| == 1 ensures reverse(xs) == xs { } lemma reverseReverseIdempotent<A>(xs: seq<A>) ensures reverse(reverse(xs)) == xs { if xs == [] { }else{ calc { reverse(reverse(xs)); reverse(reverse([xs[0]] + xs[1..])); == {ReverseConcat([xs[0]] , xs[1..]);} reverse(reverse(xs[1..]) + reverse([xs[0]])); == {ReverseSingle([xs[0]]);} reverse(reverse(xs[1..]) + [xs[0]]); == {ReverseConcat(reverse(xs[1..]), [xs[0]]);} reverse([xs[0]]) + reverse(reverse(xs[1..])); [xs[0]] + reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs; } } /* Alternatively */ // ReverseIndexAll(reverse(xs)); // ReverseIndexAll(xs); // SeqEq(reverse(reverse(xs)), xs); } // var x := xs[0]; // assert xs == [x] + xs[1..]; // reversePreservesLength(xs); // assert |xs| == |reverse(xs)|; // calc { // x; // reverse(xs)[|xs|-1]; // == {firstReverseIsLast(reverse(xs));} // reverse(reverse(xs))[0]; // } // var y := xs[|xs|-1]; // calc{ // y; // == {firstReverseIsLast(xs);} // reverse(xs)[0]; // } // assert xs == xs[0..|xs|-1] + [y]; // lastReverseIsFirst(xs); // lastReverseIsFirst(reverse(xs)); // assert reverse(reverse(xs))[0] == x; /* /** https://leetcode.com/problems/linked-list-cycle/description/ * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ function hasCycle(head: ListNode | null): boolean { let leader = head; let follower = head; while(leader !== null) { leader = leader.next; if(follower && follower.next) { follower = follower.next.next; }else if(follower && follower.next == null){ follower=follower.next; } if(follower == leader && follower != null) return true; } return false; }; */ method test() { var cycle := Node(1, Null); var next := Node(2, cycle); // cycle.next := next; }
/* https://leetcode.com/problems/reverse-linked-list/description/ * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } function reverseList(head: ListNode | null): ListNode | null { if (head == null) return null; let curr = head; let prev = null; while(curr !== null && curr.next !== null) { let next = curr.next; curr.next = prev; prev = curr; curr = next; } curr.next = prev; return curr; }; */ datatype ListNode = Null | Node(val: nat, next: ListNode) function reverse<A>(x: seq<A>): seq<A> { if x == [] then [] else reverse(x[1..])+[x[0]] } function nodeConcat(xs: ListNode, end: ListNode): ListNode { if xs == Null then end else Node(xs.val, nodeConcat(xs.next, end)) } function reverseList(xs: ListNode): ListNode { if xs == Null then Null else nodeConcat(reverseList(xs.next), Node(xs.val, Null)) } lemma ConcatNullIsRightIdentity(xs: ListNode) ensures xs == nodeConcat(xs, Null) { } lemma ConcatNullIsLeftIdentity(xs: ListNode) ensures xs == nodeConcat(Null, xs) { } lemma ConcatExtensionality(xs: ListNode) requires xs != Null ensures nodeConcat(Node(xs.val, Null), xs.next) == xs; { } lemma ConcatAssociative(xs: ListNode, ys: ListNode, zs: ListNode) ensures nodeConcat(nodeConcat(xs, ys), zs) == nodeConcat(xs, nodeConcat(ys, zs)) { } lemma reverseSingleList(xs: ListNode) requires xs != Null; requires xs.next == Null; ensures reverseList(xs) == xs; { } lemma {:verify true} ConcatReverseList(xs:ListNode, ys: ListNode) ensures reverseList(nodeConcat(xs,ys)) == nodeConcat(reverseList(ys), reverseList(xs)) decreases xs; { if xs == Null { calc { reverseList(nodeConcat(xs,ys)); == {ConcatNullIsLeftIdentity(ys);} reverseList(ys); == {ConcatNullIsRightIdentity(reverseList(ys));} nodeConcat(reverseList(ys), Null); nodeConcat(reverseList(ys), xs); nodeConcat(reverseList(ys), reverseList(xs)); } }else{ var x := Node(xs.val, Null); calc { reverseList(nodeConcat(xs, ys)); reverseList(nodeConcat(nodeConcat(x, xs.next), ys)); == {ConcatAssociative(x, xs.next, ys);} reverseList(nodeConcat(x, nodeConcat(xs.next, ys))); nodeConcat(reverseList(nodeConcat(xs.next, ys)), x); == {ConcatReverseList(xs.next, ys);} nodeConcat(nodeConcat(reverseList(ys) , reverseList(xs.next)), x); == {ConcatAssociative(reverseList(ys), reverseList(xs.next), x);} nodeConcat(reverseList(ys) , nodeConcat(reverseList(xs.next), x)); nodeConcat(reverseList(ys) , reverseList(xs)); } } } lemma reverseReverseListIsIdempotent(xs: ListNode) ensures reverseList(reverseList(xs)) == xs { if xs == Null { }else{ var x := Node(xs.val, Null); calc { reverseList(reverseList(xs)); reverseList(reverseList(nodeConcat(x, xs.next))); == {ConcatReverseList(x, xs.next);} reverseList(nodeConcat(reverseList(xs.next), reverseList(x))); reverseList(nodeConcat(reverseList(xs.next), x)); == {ConcatReverseList(reverseList(xs.next),x);} nodeConcat(reverseList(x), reverseList(reverseList(xs.next))); //dafny can figure out the rest from here nodeConcat(x, reverseList(reverseList(xs.next))); nodeConcat(x, xs.next); xs; } } } lemma {:induction false} reversePreservesMultiset<A>(xs: seq<A>) ensures multiset(xs) == multiset(reverse(xs)) { if xs == [] { }else { var x := xs[0]; assert xs == [x] + xs[1..]; assert multiset(xs) == multiset([x]) + multiset(xs[1..]); assert reverse(xs) == reverse(xs[1..])+[x]; reversePreservesMultiset(xs[1..]); assert multiset(xs[1..]) == multiset(reverse(xs[1..])); } } lemma reversePreservesLength<A>(xs: seq<A>) ensures |xs| == |reverse(xs)| { } lemma lastReverseIsFirst<A>(xs: seq<A>) requires |xs| > 0 ensures xs[0] == reverse(xs)[|reverse(xs)|-1] { reversePreservesLength(xs); assert |xs| == |reverse(xs)|; } lemma firstReverseIsLast<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs)[0] == xs[|xs|-1] { } lemma ReverseConcat<T>(xs: seq<T>, ys: seq<T>) ensures reverse(xs + ys) == reverse(ys) + reverse(xs) { // reveal Reverse(); if |xs| == 0 { assert xs + ys == ys; } else { assert xs + ys == [xs[0]] + (xs[1..] + ys); } } lemma reverseRest<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs) == [xs[ |xs| -1 ] ] + reverse(xs[0..|xs|-1]) { firstReverseIsLast(xs); assert xs == xs[0..|xs|-1] + [xs[|xs|-1]]; assert reverse(xs)[0] == xs[ |xs| -1]; assert reverse(xs) == [xs[ |xs| -1]] + reverse(xs)[1..]; calc { reverse(xs); reverse(xs[0..|xs|-1] + [xs[|xs|-1]]); == {ReverseConcat(xs[0..|xs|-1], [xs[ |xs|-1 ]]);} reverse([xs[ |xs|-1 ]]) + reverse(xs[0..|xs|-1]); } } lemma SeqEq<T>(xs: seq<T>, ys: seq<T>) requires |xs| == |ys| requires forall i :: 0 <= i < |xs| ==> xs[i] == ys[i] ensures xs == ys { } lemma ReverseIndexAll<T>(xs: seq<T>) ensures |reverse(xs)| == |xs| ensures forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1] { // reveal Reverse(); } lemma ReverseIndex<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[i] == xs[|xs| - i - 1] { ReverseIndexAll(xs); assert forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1]; } lemma ReverseSingle<A>(xs: seq<A>) requires |xs| == 1 ensures reverse(xs) == xs { } lemma reverseReverseIdempotent<A>(xs: seq<A>) ensures reverse(reverse(xs)) == xs { if xs == [] { }else{ calc { reverse(reverse(xs)); reverse(reverse([xs[0]] + xs[1..])); == {ReverseConcat([xs[0]] , xs[1..]);} reverse(reverse(xs[1..]) + reverse([xs[0]])); == {ReverseSingle([xs[0]]);} reverse(reverse(xs[1..]) + [xs[0]]); == {ReverseConcat(reverse(xs[1..]), [xs[0]]);} reverse([xs[0]]) + reverse(reverse(xs[1..])); [xs[0]] + reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs; } } /* Alternatively */ // ReverseIndexAll(reverse(xs)); // ReverseIndexAll(xs); // SeqEq(reverse(reverse(xs)), xs); } // var x := xs[0]; // assert xs == [x] + xs[1..]; // reversePreservesLength(xs); // assert |xs| == |reverse(xs)|; // calc { // x; // reverse(xs)[|xs|-1]; // == {firstReverseIsLast(reverse(xs));} // reverse(reverse(xs))[0]; // } // var y := xs[|xs|-1]; // calc{ // y; // == {firstReverseIsLast(xs);} // reverse(xs)[0]; // } // assert xs == xs[0..|xs|-1] + [y]; // lastReverseIsFirst(xs); // lastReverseIsFirst(reverse(xs)); // assert reverse(reverse(xs))[0] == x; /* /** https://leetcode.com/problems/linked-list-cycle/description/ * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ function hasCycle(head: ListNode | null): boolean { let leader = head; let follower = head; while(leader !== null) { leader = leader.next; if(follower && follower.next) { follower = follower.next.next; }else if(follower && follower.next == null){ follower=follower.next; } if(follower == leader && follower != null) return true; } return false; }; */ method test() { var cycle := Node(1, Null); var next := Node(2, cycle); // cycle.next := next; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_ReverseLinkedList.dfy
292
292
Dafny program: 292
//https://leetcode.com/problems/remove-element/ method removeElement(nums: array<int>, val: int) returns (i: int) ensures forall k :: 0 < k < i < nums.Length ==> nums[k] != val modifies nums { i := 0; var end := nums.Length - 1; while i <= end { if(nums[i] == val) { if(nums[end] == val) { end := end - 1; }else{ nums[i], nums[end] := nums[end], nums[i]; i := i + 1; end := end - 1; } }else{ i := i + 1; } } } ///compileTarget:js method Main() { var elems := new int[5][1,2,3,4,5]; var res := removeElement(elems, 5); print res, "\n", elems; }
//https://leetcode.com/problems/remove-element/ method removeElement(nums: array<int>, val: int) returns (i: int) ensures forall k :: 0 < k < i < nums.Length ==> nums[k] != val modifies nums { i := 0; var end := nums.Length - 1; while i <= end invariant 0 <= i <= nums.Length invariant end < nums.Length invariant forall k :: 0 <= k < i ==> nums[k] != val { if(nums[i] == val) { if(nums[end] == val) { end := end - 1; }else{ nums[i], nums[end] := nums[end], nums[i]; i := i + 1; end := end - 1; } }else{ i := i + 1; } } } ///compileTarget:js method Main() { var elems := new int[5][1,2,3,4,5]; var res := removeElement(elems, 5); print res, "\n", elems; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_lc-remove-element.dfy
293
293
Dafny program: 293
//https://leetcode.com/problems/path-sum /** function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if(root == null) { return false; } if(root.val-targetSum == 0 && root.left == null && root.right == null) { return true; } return hasPathSum(root.left, targetSum-root.val) || hasPathSum(root.right, targetSum-root.val); }; */ datatype TreeNode = Nil | Cons(val: nat, left: TreeNode, right: TreeNode) function TreeSeq(root: TreeNode): seq<TreeNode> { match root { case Nil => [Nil] case Cons(val, left, right) => [root]+TreeSeq(left)+TreeSeq(right) } } function TreeSet(root: TreeNode): set<TreeNode> { match root { case Nil => {Nil} case Cons(val, left, right) => TreeSet(left)+{root}+TreeSet(right) } } predicate isPath(paths: seq<TreeNode>, root: TreeNode) { if |paths| == 0 then false else match paths[0] { case Nil => false case Cons(val, left, right) => if |paths| == 1 then root == paths[0] else root == paths[0] && (isPath(paths[1..], left) || isPath(paths[1..], right)) } } function pathSum(paths: seq<TreeNode>): nat { if |paths| == 0 then 0 else match paths[0] { case Nil => 0 case Cons(val, left, right) => val + pathSum(paths[1..]) } } method hasPathSum(root: TreeNode, targetSum: int) returns (b: bool) ensures b ==> exists p: seq<TreeNode> :: isPath(p, root) && pathSum(p) == targetSum { if root == Nil { return false; } if(root.val - targetSum == 0 && root.left == Nil && root.right == Nil) { return true; } var leftPath := hasPathSum(root.left, targetSum-root.val); var rightPath := hasPathSum(root.right, targetSum-root.val); if leftPath { ghost var p: seq<TreeNode> :| isPath(p, root.left) && pathSum(p) == targetSum-root.val; } if rightPath { ghost var p: seq<TreeNode> :| isPath(p, root.right) && pathSum(p) == targetSum-root.val; } return leftPath || rightPath; } method Test() { var c := Cons(3, Nil, Nil); var b := Cons(2, c, Nil); var a := Cons(1, b, Nil); }
//https://leetcode.com/problems/path-sum /** function hasPathSum(root: TreeNode | null, targetSum: number): boolean { if(root == null) { return false; } if(root.val-targetSum == 0 && root.left == null && root.right == null) { return true; } return hasPathSum(root.left, targetSum-root.val) || hasPathSum(root.right, targetSum-root.val); }; */ datatype TreeNode = Nil | Cons(val: nat, left: TreeNode, right: TreeNode) function TreeSeq(root: TreeNode): seq<TreeNode> { match root { case Nil => [Nil] case Cons(val, left, right) => [root]+TreeSeq(left)+TreeSeq(right) } } function TreeSet(root: TreeNode): set<TreeNode> { match root { case Nil => {Nil} case Cons(val, left, right) => TreeSet(left)+{root}+TreeSet(right) } } predicate isPath(paths: seq<TreeNode>, root: TreeNode) { if |paths| == 0 then false else match paths[0] { case Nil => false case Cons(val, left, right) => if |paths| == 1 then root == paths[0] else root == paths[0] && (isPath(paths[1..], left) || isPath(paths[1..], right)) } } function pathSum(paths: seq<TreeNode>): nat { if |paths| == 0 then 0 else match paths[0] { case Nil => 0 case Cons(val, left, right) => val + pathSum(paths[1..]) } } method hasPathSum(root: TreeNode, targetSum: int) returns (b: bool) ensures b ==> exists p: seq<TreeNode> :: isPath(p, root) && pathSum(p) == targetSum { if root == Nil { return false; } if(root.val - targetSum == 0 && root.left == Nil && root.right == Nil) { assert isPath([root], root) && pathSum([root]) == targetSum; return true; } var leftPath := hasPathSum(root.left, targetSum-root.val); var rightPath := hasPathSum(root.right, targetSum-root.val); if leftPath { ghost var p: seq<TreeNode> :| isPath(p, root.left) && pathSum(p) == targetSum-root.val; assert isPath([root]+p, root) && pathSum([root]+p) == targetSum; } if rightPath { ghost var p: seq<TreeNode> :| isPath(p, root.right) && pathSum(p) == targetSum-root.val; assert isPath([root]+p, root) && pathSum([root]+p) == targetSum; } return leftPath || rightPath; } method Test() { var c := Cons(3, Nil, Nil); var b := Cons(2, c, Nil); var a := Cons(1, b, Nil); assert isPath([a], a); assert a.left == b; assert isPath([a,b], a); assert isPath([a,b,c], a); }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_pathSum.dfy
294
294
Dafny program: 294
/* You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? function climbStairs(n: number): number { let steps = new Array(n+1); steps[0] = 0; steps[1] = 1; steps[2] = 2; for(let i = 3; i <= n; i++) { steps[i] = steps[i-1] + steps[i-2]; } return steps[n]; }; */ datatype Steps = One | Two function stepSum(xs: seq<Steps>): nat { if xs == [] then 0 else (match xs[0] { case One => 1 case Two => 2 } + stepSum(xs[1..])) } ghost predicate stepEndsAt(xs: seq<Steps>, n: nat) { stepSum(xs) == n } ghost predicate allEndAtN(ss: set<seq<Steps> >, n: nat) { forall xs :: xs in ss ==> stepEndsAt(xs, n) } lemma stepBaseZero() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 0) && |ss| == 0 { } lemma stepBaseOne() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 1) && |ss| == 1 { } lemma stepBaseTwo() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 2) && |ss| == 2 { } ghost function plusOne(x: seq<Steps>): seq<Steps> { [One]+x } ghost function addOne(ss: set<seq<Steps>>): set<seq<Steps>> ensures forall x :: x in ss ==> plusOne(x) in addOne(ss) ensures addOne(ss) == set x | x in ss :: plusOne(x) { set x | x in ss :: plusOne(x) } lemma SeqsNotEqualImplication<T>(xs: seq<T>, ys: seq<T>, someT: T) requires xs != ys ensures (exists i: nat :: i < |xs| && i <|ys| && xs[i] != ys[i]) || |xs| < |ys| || |ys| < |xs| {} lemma UnequalSeqs<T>(xs: seq<T>, ys: seq<T>, someT: T) requires xs != ys ensures [someT]+xs != [someT]+ys { if |xs| < |ys| {} else if |ys| > |xs| {} else if i: nat :| i < |xs| && i <|ys| && xs[i] != ys[i] { } } lemma plusOneNotIn(ss: set<seq<Steps>>, x: seq<Steps>) requires x !in ss ensures plusOne(x) !in addOne(ss) { if x == [] { } if plusOne(x) in addOne(ss) { forall y | y in ss ensures y != x ensures plusOne(y) in addOne(ss) ensures plusOne(y) != plusOne(x) { UnequalSeqs(x, y, One); } } } lemma addOneSize(ss: set<seq<Steps>>) ensures |addOne(ss)| == |ss| { var size := |ss|; if x :| x in ss { addOneSize(ss - {x}); plusOneNotIn(ss-{x}, x); }else{ } } lemma addOneSum(ss: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) ensures allEndAtN(addOne(ss), sum+1) { } lemma endAtNPlus(ss: set<seq<Steps>>, sz: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) requires allEndAtN(sz, sum) ensures allEndAtN(ss+sz, sum) { } ghost function plusTwo(x: seq<Steps>): seq<Steps> { [Two]+x } ghost function addTwo(ss: set<seq<Steps>>): set<seq<Steps>> ensures forall x :: x in ss ==> plusTwo(x) in addTwo(ss) ensures addTwo(ss) == set x | x in ss :: plusTwo(x) { set x | x in ss :: plusTwo(x) } lemma plusTwoNotIn(ss: set<seq<Steps>>, x: seq<Steps>) requires x !in ss ensures plusTwo(x) !in addTwo(ss) { if x == [] { } if plusTwo(x) in addTwo(ss) { forall y | y in ss ensures y != x ensures plusTwo(y) in addTwo(ss) ensures plusTwo(y) != plusTwo(x) { UnequalSeqs(x, y, Two); } } } lemma addTwoSize(ss: set<seq<Steps>>) ensures |addTwo(ss)| == |ss| { var size := |ss|; if x :| x in ss { // assert |ss - {x}| == size - 1; addTwoSize(ss - {x}); // assert |addTwo(ss-{x})| == size - 1; // assert x !in ss-{x}; plusTwoNotIn(ss-{x}, x); // assert plusTwo(x) !in addTwo(ss-{x}); } } lemma addTwoSum(ss: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) ensures allEndAtN(addTwo(ss), sum+2) { } lemma setSizeAddition<T>(sx: set<T>, sy: set<T>, sz: set<T>) requires sx !! sy requires sz == sx + sy ensures |sx + sy| == |sx| + |sy| ensures |sz| == |sx| + |sy| { } lemma stepSetsAdd(i: nat, steps: array<nat>) requires i >= 2 requires steps.Length >= i+1 requires forall k: nat :: k < i ==> exists ss: set< seq<Steps> > :: steps[k] == |ss| && allEndAtN(ss, k) ensures exists sp : set< seq<Steps> > :: |sp| == steps[i-1] + steps[i-2] && allEndAtN(sp, i) { var oneStepBack :| steps[i-1] == |oneStepBack| && allEndAtN(oneStepBack, i-1); var twoStepBack :| steps[i-2] == |twoStepBack| && allEndAtN(twoStepBack, i-2); var stepForward := addOne(oneStepBack); var stepTwoForward := addTwo(twoStepBack); // assert stepForward !! stepTwoForward; addOneSize(oneStepBack); addTwoSize(twoStepBack); var sumSet := stepForward + stepTwoForward; // assert |sumSet| == steps[i-1]+steps[i-2]; } method climbStairs(n: nat) returns (count: nat) ensures exists ss: set< seq<Steps> > :: count == |ss| && allEndAtN(ss, n) { var steps := new nat[n+1]; steps[0] := 0; if n > 0 { steps[1] := 1; } if n > 1 { steps[2] := 2; } stepBaseZero(); stepBaseOne(); stepBaseTwo(); if n < 3 { return steps[n]; } var i := 3; while i <= n { steps[i] := steps[i-1] + steps[i-2]; stepSetsAdd(i, steps); i := i + 1; } return steps[n]; } method Test() { var foo := [One, One, Two]; }
/* You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? function climbStairs(n: number): number { let steps = new Array(n+1); steps[0] = 0; steps[1] = 1; steps[2] = 2; for(let i = 3; i <= n; i++) { steps[i] = steps[i-1] + steps[i-2]; } return steps[n]; }; */ datatype Steps = One | Two function stepSum(xs: seq<Steps>): nat { if xs == [] then 0 else (match xs[0] { case One => 1 case Two => 2 } + stepSum(xs[1..])) } ghost predicate stepEndsAt(xs: seq<Steps>, n: nat) { stepSum(xs) == n } ghost predicate allEndAtN(ss: set<seq<Steps> >, n: nat) { forall xs :: xs in ss ==> stepEndsAt(xs, n) } lemma stepBaseZero() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 0) && |ss| == 0 { assert allEndAtN({[]}, 0); } lemma stepBaseOne() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 1) && |ss| == 1 { assert allEndAtN({[One]}, 1); } lemma stepBaseTwo() ensures exists ss: set< seq<Steps> > :: allEndAtN(ss, 2) && |ss| == 2 { assert allEndAtN({[One,One], [Two]}, 2); } ghost function plusOne(x: seq<Steps>): seq<Steps> { [One]+x } ghost function addOne(ss: set<seq<Steps>>): set<seq<Steps>> ensures forall x :: x in ss ==> plusOne(x) in addOne(ss) ensures addOne(ss) == set x | x in ss :: plusOne(x) { set x | x in ss :: plusOne(x) } lemma SeqsNotEqualImplication<T>(xs: seq<T>, ys: seq<T>, someT: T) requires xs != ys ensures (exists i: nat :: i < |xs| && i <|ys| && xs[i] != ys[i]) || |xs| < |ys| || |ys| < |xs| {} lemma UnequalSeqs<T>(xs: seq<T>, ys: seq<T>, someT: T) requires xs != ys ensures [someT]+xs != [someT]+ys { if |xs| < |ys| {} else if |ys| > |xs| {} else if i: nat :| i < |xs| && i <|ys| && xs[i] != ys[i] { assert ([someT]+xs)[i+1] != ([someT]+ys)[i+1]; } } lemma plusOneNotIn(ss: set<seq<Steps>>, x: seq<Steps>) requires x !in ss ensures plusOne(x) !in addOne(ss) { if x == [] { assert [] !in ss; assert [One]+[] !in addOne(ss); } if plusOne(x) in addOne(ss) { forall y | y in ss ensures y != x ensures plusOne(y) in addOne(ss) ensures plusOne(y) != plusOne(x) { UnequalSeqs(x, y, One); assert plusOne(y) != [One]+x; } assert false; } } lemma addOneSize(ss: set<seq<Steps>>) ensures |addOne(ss)| == |ss| { var size := |ss|; if x :| x in ss { assert |ss - {x}| == size - 1; addOneSize(ss - {x}); assert |addOne(ss-{x})| == size - 1; assert addOne(ss) == addOne(ss-{x}) + {[One]+x}; assert x !in ss-{x}; plusOneNotIn(ss-{x}, x); assert plusOne(x) !in addOne(ss-{x}); assert |addOne(ss)| == |addOne(ss-{x})| + |{[One]+x}|; }else{ } } lemma addOneSum(ss: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) ensures allEndAtN(addOne(ss), sum+1) { } lemma endAtNPlus(ss: set<seq<Steps>>, sz: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) requires allEndAtN(sz, sum) ensures allEndAtN(ss+sz, sum) { } ghost function plusTwo(x: seq<Steps>): seq<Steps> { [Two]+x } ghost function addTwo(ss: set<seq<Steps>>): set<seq<Steps>> ensures forall x :: x in ss ==> plusTwo(x) in addTwo(ss) ensures addTwo(ss) == set x | x in ss :: plusTwo(x) { set x | x in ss :: plusTwo(x) } lemma plusTwoNotIn(ss: set<seq<Steps>>, x: seq<Steps>) requires x !in ss ensures plusTwo(x) !in addTwo(ss) { if x == [] { assert [] !in ss; assert [Two]+[] !in addTwo(ss); } if plusTwo(x) in addTwo(ss) { forall y | y in ss ensures y != x ensures plusTwo(y) in addTwo(ss) ensures plusTwo(y) != plusTwo(x) { UnequalSeqs(x, y, Two); assert plusTwo(y) != [Two]+x; } } } lemma addTwoSize(ss: set<seq<Steps>>) ensures |addTwo(ss)| == |ss| { var size := |ss|; if x :| x in ss { // assert |ss - {x}| == size - 1; addTwoSize(ss - {x}); // assert |addTwo(ss-{x})| == size - 1; assert addTwo(ss) == addTwo(ss-{x}) + {[Two]+x}; // assert x !in ss-{x}; plusTwoNotIn(ss-{x}, x); // assert plusTwo(x) !in addTwo(ss-{x}); assert |addTwo(ss)| == |addTwo(ss-{x})| + |{[Two]+x}|; } } lemma addTwoSum(ss: set<seq<Steps>>, sum: nat) requires allEndAtN(ss, sum) ensures allEndAtN(addTwo(ss), sum+2) { } lemma setSizeAddition<T>(sx: set<T>, sy: set<T>, sz: set<T>) requires sx !! sy requires sz == sx + sy ensures |sx + sy| == |sx| + |sy| ensures |sz| == |sx| + |sy| { } lemma stepSetsAdd(i: nat, steps: array<nat>) requires i >= 2 requires steps.Length >= i+1 requires forall k: nat :: k < i ==> exists ss: set< seq<Steps> > :: steps[k] == |ss| && allEndAtN(ss, k) ensures exists sp : set< seq<Steps> > :: |sp| == steps[i-1] + steps[i-2] && allEndAtN(sp, i) { var oneStepBack :| steps[i-1] == |oneStepBack| && allEndAtN(oneStepBack, i-1); var twoStepBack :| steps[i-2] == |twoStepBack| && allEndAtN(twoStepBack, i-2); var stepForward := addOne(oneStepBack); var stepTwoForward := addTwo(twoStepBack); assert forall x :: x in stepForward ==> x[0] == One; // assert stepForward !! stepTwoForward; addOneSize(oneStepBack); addTwoSize(twoStepBack); var sumSet := stepForward + stepTwoForward; // assert |sumSet| == steps[i-1]+steps[i-2]; } method climbStairs(n: nat) returns (count: nat) ensures exists ss: set< seq<Steps> > :: count == |ss| && allEndAtN(ss, n) { var steps := new nat[n+1]; steps[0] := 0; if n > 0 { steps[1] := 1; } if n > 1 { steps[2] := 2; } stepBaseZero(); stepBaseOne(); stepBaseTwo(); if n < 3 { return steps[n]; } assert steps[0] == 0; assert steps[1] == 1; assert steps[2] == 2; assert forall k: nat :: k < 3 ==> exists ss: set< seq<Steps> > :: steps[k] == |ss| && allEndAtN(ss, k); var i := 3; while i <= n invariant 3 <= i <= n+1 invariant forall k: nat :: k < i ==> exists ss: set< seq<Steps> > :: steps[k] == |ss| && allEndAtN(ss, k) { steps[i] := steps[i-1] + steps[i-2]; stepSetsAdd(i, steps); i := i + 1; } return steps[n]; } method Test() { var foo := [One, One, Two]; assert stepSum(foo) == 4; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_stairClimbing.dfy
295
295
Dafny program: 295
method toMultiset(s: string) returns (mset: multiset<char>) ensures multiset(s) == mset { mset := multiset{}; for i := 0 to |s| { // assert multiset(s) == multiset(s[0..i])+multiset{s[i]}+multiset(s[(i+1)..]); mset := mset + multiset{s[i]}; } // assert mset == multiset(s[0..|s|]); return mset; } method msetEqual(s: multiset<char>, t: multiset<char>) returns (equal: bool) ensures s == t <==> equal { ghost var sremoved: multiset<char> := multiset{}; var scopy := s; while scopy != multiset{} { var x :| x in scopy; if !(x in t && s[x] == t[x]) { return false; } var removed := multiset{}; // assert removed[x := s[x]] <= s; sremoved := sremoved + removed[x := s[x]]; scopy := scopy - removed[x := s[x]]; } // assert scopy == multiset{}; // assert s - sremoved == scopy; // assert sremoved == s; // assert forall x :: x in sremoved ==> x in s && x in t && t[x] == s[x]; ghost var tremoved: multiset<char> := multiset{}; var tcopy := t; while tcopy != multiset{} { var x :| x in tcopy; if !(x in t && s[x] == t[x]) { return false; } var removed := multiset{}; tremoved := tremoved + removed[x := s[x]]; tcopy := tcopy - removed[x := s[x]]; } // assert forall x :: x in tremoved ==> x in s && x in t && t[x] == s[x]; return true; } method isAnagram(s: string, t: string) returns (equal: bool) ensures (multiset(s) == multiset(t)) == equal { var smset := toMultiset(s); var tmset := toMultiset(t); equal := msetEqual(smset, tmset); }
method toMultiset(s: string) returns (mset: multiset<char>) ensures multiset(s) == mset { mset := multiset{}; for i := 0 to |s| invariant mset == multiset(s[0..i]) { assert s == s[0..i] + [s[i]] + s[(i+1)..]; // assert multiset(s) == multiset(s[0..i])+multiset{s[i]}+multiset(s[(i+1)..]); mset := mset + multiset{s[i]}; } assert s == s[0..|s|]; // assert mset == multiset(s[0..|s|]); return mset; } method msetEqual(s: multiset<char>, t: multiset<char>) returns (equal: bool) ensures s == t <==> equal { ghost var sremoved: multiset<char> := multiset{}; var scopy := s; while scopy != multiset{} invariant s - sremoved == scopy invariant sremoved !! scopy invariant sremoved <= s invariant forall x :: x in sremoved ==> x in s && x in t && t[x] == s[x] { var x :| x in scopy; if !(x in t && s[x] == t[x]) { return false; } var removed := multiset{}; // assert removed[x := s[x]] <= s; sremoved := sremoved + removed[x := s[x]]; scopy := scopy - removed[x := s[x]]; } // assert scopy == multiset{}; // assert s - sremoved == scopy; // assert sremoved == s; // assert forall x :: x in sremoved ==> x in s && x in t && t[x] == s[x]; ghost var tremoved: multiset<char> := multiset{}; var tcopy := t; while tcopy != multiset{} invariant t - tremoved == tcopy invariant tremoved !! tcopy invariant tremoved <= t invariant forall x :: x in tremoved ==> x in s && x in t && t[x] == s[x] { var x :| x in tcopy; if !(x in t && s[x] == t[x]) { return false; } var removed := multiset{}; tremoved := tremoved + removed[x := s[x]]; tcopy := tcopy - removed[x := s[x]]; } // assert forall x :: x in tremoved ==> x in s && x in t && t[x] == s[x]; return true; } method isAnagram(s: string, t: string) returns (equal: bool) ensures (multiset(s) == multiset(t)) == equal { var smset := toMultiset(s); var tmset := toMultiset(t); equal := msetEqual(smset, tmset); }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_leetcode_validAnagram.dfy
296
296
Dafny program: 296
module Seq { export reveals * function ToSet<A>(xs: seq<A>): set<A> ensures forall x :: x in ToSet(xs) ==> x in xs ensures forall x :: x !in ToSet(xs) ==> x !in xs { if xs == [] then {} else {xs[0]}+ToSet(xs[1..]) } predicate substring1<A(==)>(sub: seq<A>, super: seq<A>) { exists k :: 0 <= k < |super| && sub <= super[k..] } ghost predicate isSubstringAlt<A(!new)>(sub: seq<A>, super: seq<A>) { |sub| <= |super| && exists xs: seq<A> :: IsSuffix(xs, super) && sub <= xs } predicate isSubstring<A(==)>(sub: seq<A>, super: seq<A>) { |sub| <= |super| && exists k,j :: 0 <= k < j <= |super| && sub == super[k..j] } lemma SliceOfSliceIsSlice<A>(xs: seq<A>, k: int, j: int, s: int, t: int) requires 0 <= k <= j <= |xs| requires 0 <= s <= t <= j-k ensures xs[k..j][s..t] == xs[(k+s)..(k+s+(t-s))] { if j-k == 0 { }else if t-s == 0 { }else if t-s > 0 { SliceOfSliceIsSlice(xs, k, j, s,t-1); } } lemma AllSubstringsAreSubstrings<A>(subsub: seq<A>, sub: seq<A>, super: seq<A>) requires isSubstring(sub, super) requires isSubstring(subsub, sub) ensures isSubstring(subsub, super) { var k,j :| 0 <= k < j <= |super| && sub == super[k..j]; var s,t :| 0 <= s < t <= |sub| && subsub == sub[s..t]; //[3,4,5,6,7,8,9,10,11,12,13][2,7] //k,j //[5,6,7,8,9,10][1..3] //s,t //[5,7,8] // var example:= [3,4,5,6,7,8,9,10,11,12,13]; // assert example[2..7] == [5,6,7,8,9]; // assert example[2..7][1..3] == [6,7]; // assert example[3..5] == [6,7]; // assert k+s+(t-s) // assert 2+1+(3-1) == 5; /* */ if t < j { calc { subsub; super[k..j][s..t]; {SliceOfSliceIsSlice(super,k,j,s,t);} super[(k+s)..(k+s+(t-s))]; } } else if t <= j { } // var z,q :| 0 <= z <= q <= |super| && super[z..q] == super[k..j][s..t]; } predicate IsSuffix<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && xs == ys[|ys| - |xs|..] } predicate IsPrefix<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && xs == ys[..|xs|] } lemma PrefixRest<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(xs, ys) ensures exists yss: seq<T> :: ys == xs + yss && |yss| == |ys|-|xs|; { } lemma IsSuffixReversed<T>(xs: seq<T>, ys: seq<T>) requires IsSuffix(xs, ys) ensures IsPrefix(reverse(xs), reverse(ys)) { ReverseIndexAll(xs); ReverseIndexAll(ys); } lemma IsPrefixReversed<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(xs, ys) ensures IsSuffix(reverse(xs), reverse(ys)) { ReverseIndexAll(xs); ReverseIndexAll(ys); } lemma IsPrefixReversedAll<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(reverse(xs), reverse(ys)) ensures IsSuffix(reverse(reverse(xs)), reverse(reverse(ys))) { ReverseIndexAll(xs); ReverseIndexAll(ys); PrefixRest(reverse(xs), reverse(ys)); var yss :| reverse(ys) == reverse(xs) + yss && |yss| == |ys|-|xs|; reverseReverseIdempotent(ys); ReverseConcat(reverse(xs), yss); calc { reverse(reverse(ys)); ys; reverse(reverse(xs) + yss); reverse(yss)+reverse(reverse(xs)); == {reverseReverseIdempotent(xs);} reverse(yss)+xs; } } predicate IsSuffix2<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && exists K :: 0 <= K <= |ys|-|xs| && ys == ys[0..K] + xs + ys[(K+|xs|)..] } function reverse<A>(x: seq<A>): seq<A> { if x == [] then [] else reverse(x[1..])+[x[0]] } lemma {:induction false} reversePreservesMultiset<A>(xs: seq<A>) ensures multiset(xs) == multiset(reverse(xs)) { if xs == [] { }else { var x := xs[0]; reversePreservesMultiset(xs[1..]); } } lemma reversePreservesLength<A>(xs: seq<A>) ensures |xs| == |reverse(xs)| { } lemma lastReverseIsFirst<A>(xs: seq<A>) requires |xs| > 0 ensures xs[0] == reverse(xs)[|reverse(xs)|-1] { reversePreservesLength(xs); } lemma firstReverseIsLast<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs)[0] == xs[|xs|-1] { } lemma ReverseConcat<T>(xs: seq<T>, ys: seq<T>) ensures reverse(xs + ys) == reverse(ys) + reverse(xs) { // reveal Reverse(); if |xs| == 0 { } else { } } lemma reverseRest<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs) == [xs[ |xs| -1 ] ] + reverse(xs[0..|xs|-1]) { firstReverseIsLast(xs); calc { reverse(xs); reverse(xs[0..|xs|-1] + [xs[|xs|-1]]); == {ReverseConcat(xs[0..|xs|-1], [xs[ |xs|-1 ]]);} reverse([xs[ |xs|-1 ]]) + reverse(xs[0..|xs|-1]); } } lemma ReverseIndexAll<T>(xs: seq<T>) ensures |reverse(xs)| == |xs| ensures forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1] { // reveal Reverse(); } lemma ReverseIndex<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[i] == xs[|xs| - i - 1] { ReverseIndexAll(xs); } lemma ReverseIndexBack<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[|xs| - i - 1] == xs[i] { ReverseIndexAll(xs); } lemma ReverseSingle<A>(xs: seq<A>) requires |xs| == 1 ensures reverse(xs) == xs { } lemma SeqEq<T>(xs: seq<T>, ys: seq<T>) requires |xs| == |ys| requires forall i :: 0 <= i < |xs| ==> xs[i] == ys[i] ensures xs == ys { } lemma reverseReverseIdempotent<A>(xs: seq<A>) ensures reverse(reverse(xs)) == xs { if xs == [] { }else{ calc { reverse(reverse(xs)); reverse(reverse([xs[0]] + xs[1..])); == {ReverseConcat([xs[0]] , xs[1..]);} reverse(reverse(xs[1..]) + reverse([xs[0]])); == {ReverseSingle([xs[0]]);} reverse(reverse(xs[1..]) + [xs[0]]); == {ReverseConcat(reverse(xs[1..]), [xs[0]]);} reverse([xs[0]]) + reverse(reverse(xs[1..])); [xs[0]] + reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs; } } /* Alternatively */ // ReverseIndexAll(reverse(xs)); // ReverseIndexAll(xs); // SeqEq(reverse(reverse(xs)), xs); } lemma notInNotEqual<A>(xs: seq<A>, elem: A) requires elem !in xs ensures forall k :: 0 <= k < |xs| ==> xs[k] != elem { } predicate distinct<A(==)>(s: seq<A>) { forall x,y :: x != y && 0 <= x <= y < |s| ==> s[x] != s[y] } lemma distincts<A>(xs: seq<A>, ys: seq<A>) requires distinct(xs) requires distinct(ys) requires forall x :: x in xs ==> x !in ys requires forall y :: y in ys ==> y !in xs ensures distinct(xs+ys) { var len := |xs + ys|; forall x,y | x != y && 0 <= x <= y < |xs+ys| ensures (xs+ys)[x] != (xs+ys)[y] { if 0 <= x < |xs| && 0 <= y < |xs| { }else if |xs| <= x < |xs+ys| && |xs| <= y < |xs+ys| { }else if 0 <= x < |xs| && |xs| <= y < |xs+ys| { notInNotEqual(ys, xs[x]); } } } lemma reverseDistinct<A>(list: seq<A>) requires distinct(list) ensures distinct(reverse(list)) { ReverseIndexAll(list); } lemma distinctSplits<A>(list: seq<A>) requires distinct(list) ensures forall i :: 1 <= i < |list| ==> distinct(list[..i]) {} lemma multisetItems<A>(list: seq<A>, item: A) requires item in list requires multiset(list)[item] > 1 ensures exists i,j :: 0 <= i < j < |list| && list[i] == item && list[j] == item && i != j { var k :| 0 <= k < |list| && list[k] == item; var rest := list[..k]+list[k+1..]; } lemma distinctMultisetIs1<A>(list: seq<A>, item: A) requires distinct(list) requires item in list ensures multiset(list)[item] == 1 { if multiset(list)[item] == 0 { } if multiset(list)[item] > 1 { multisetItems(list, item); var i,j :| 0 <= i < j < |list| && list[i] == item && list[j] == item && i != j; } } lemma indistinctMultisetIsGreaterThan1<A>(list: seq<A>) requires !distinct(list) ensures exists item :: multiset(list)[item] > 1 { var x,y :| x != y && 0 <= x <= y < |list| && list[x] == list[y]; var item := list[x]; } lemma multisetIsGreaterThan1Indistinct<A>(list: seq<A>) requires exists item :: multiset(list)[item] > 1 ensures !distinct(list) { var item :| multiset(list)[item] > 1; var x :| 0 <= x < |list| && list[x] == item; var y :| x != y && 0 <= y < |list| && list[y] == item; } lemma indistinctPlusX<A>(items: seq<A>, x: A) requires !distinct(items) ensures forall i :: 0 <= i < |items| ==> !distinct(items[..i]+[x]+items[i..]) { forall i | 0 <= i < |items| ensures !distinct(items[..i]+[x]+items[i..]) { indistinctMultisetIsGreaterThan1(items); var item :| multiset(items)[item] > 1; var itemsPlus := items[..i]+[x]+items[i..]; calc { multiset(itemsPlus); multiset(items[..i])+multiset(items[i..])+multiset{x}; multiset(items)+multiset{x}; } multisetIsGreaterThan1Indistinct(itemsPlus); } } lemma pigeonHolesMultiset<A>(items: set<A>, list: seq<A>, n: nat) requires |items| == n requires forall x :: x in list ==> x in items requires |list| > n ensures exists item :: multiset(list)[item] > 1 { if x :| multiset(list)[x] > 1 { }else if x :| multiset(list)[x] == 1 { var i :| 0 <= i < |list| && list[i] == x; var rest := list[..i]+list[i+1..]; forall y | y in rest ensures y in items-{x} { } if n -1 == 0 { // assert |items| == 1; // assert x in items; }else{ pigeonHolesMultiset(items-{x}, rest, n-1); var item :| multiset(rest)[item] > 1; } }else if x :| multiset(list)[x] == 0 {} } lemma pigeonHoles<A>(items: set<A>, list: seq<A>, n: nat) requires |items| == n requires forall x :: x in list ==> x in items requires |list| > n ensures !distinct(list) { if x :| multiset(list)[x] > 1 { multisetItems(list, x); var i,j :| 0 <= i < j < |list| && list[i] == x && list[j] == x && i != j; }else if x :| multiset(list)[x] == 1 { var i :| 0 <= i < |list| && list[i] == x; var rest := list[..i]+list[i+1..]; forall y | y in rest ensures y in items-{x} { } pigeonHoles(items-{x}, rest, n-1); indistinctPlusX(rest, x); }else if x :| multiset(list)[x] == 0 {} } lemma reverseInitList<T>(xs: seq<T>) requires |xs| > 1 requires |reverse(xs)| == |xs| ensures reverse(reverse(xs)[..|xs|-1]) == xs[1..] { calc { reverse(xs); reverse(xs[1..])+reverse([xs[0]]); reverse(xs[1..])+[xs[0]]; } calc { reverse(reverse(xs)[..|xs|-1]); reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs[1..]; } } method SeqTest() { var t1 := [4,5,6,1,2,3]; // assert t1 == [4,5,6]+[1,2,3]; var s1 := [1,2,3]; // assert isSubstring(s1,t1); } }
module Seq { export reveals * function ToSet<A>(xs: seq<A>): set<A> ensures forall x :: x in ToSet(xs) ==> x in xs ensures forall x :: x !in ToSet(xs) ==> x !in xs { if xs == [] then {} else {xs[0]}+ToSet(xs[1..]) } predicate substring1<A(==)>(sub: seq<A>, super: seq<A>) { exists k :: 0 <= k < |super| && sub <= super[k..] } ghost predicate isSubstringAlt<A(!new)>(sub: seq<A>, super: seq<A>) { |sub| <= |super| && exists xs: seq<A> :: IsSuffix(xs, super) && sub <= xs } predicate isSubstring<A(==)>(sub: seq<A>, super: seq<A>) { |sub| <= |super| && exists k,j :: 0 <= k < j <= |super| && sub == super[k..j] } lemma SliceOfSliceIsSlice<A>(xs: seq<A>, k: int, j: int, s: int, t: int) requires 0 <= k <= j <= |xs| requires 0 <= s <= t <= j-k ensures xs[k..j][s..t] == xs[(k+s)..(k+s+(t-s))] { if j-k == 0 { }else if t-s == 0 { }else if t-s > 0 { SliceOfSliceIsSlice(xs, k, j, s,t-1); assert xs[k..j][s..t] == xs[k..j][s..(t-1)]+[xs[k..j][t-1]]; } } lemma AllSubstringsAreSubstrings<A>(subsub: seq<A>, sub: seq<A>, super: seq<A>) requires isSubstring(sub, super) requires isSubstring(subsub, sub) ensures isSubstring(subsub, super) { assert |sub| <= |super|; assert |subsub| <= |super|; var k,j :| 0 <= k < j <= |super| && sub == super[k..j]; var s,t :| 0 <= s < t <= |sub| && subsub == sub[s..t]; assert t <= (j-k) <= j; //[3,4,5,6,7,8,9,10,11,12,13][2,7] //k,j //[5,6,7,8,9,10][1..3] //s,t //[5,7,8] // var example:= [3,4,5,6,7,8,9,10,11,12,13]; // assert example[2..7] == [5,6,7,8,9]; // assert example[2..7][1..3] == [6,7]; // assert example[3..5] == [6,7]; // assert k+s+(t-s) // assert 2+1+(3-1) == 5; /* assert s[..idx] == [s[0]] + s[1..idx]; assert s[1..idx] == s[1..][..(idx-1)]; assert s[1..][(idx-1)..] == s[idx..]; */ assert super[..j][..t] == super[..(t)]; assert super[k..][s..] == super[(k+s)..]; if t < j { calc { subsub; super[k..j][s..t]; {SliceOfSliceIsSlice(super,k,j,s,t);} super[(k+s)..(k+s+(t-s))]; } } else if t <= j { } // var z,q :| 0 <= z <= q <= |super| && super[z..q] == super[k..j][s..t]; } predicate IsSuffix<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && xs == ys[|ys| - |xs|..] } predicate IsPrefix<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && xs == ys[..|xs|] } lemma PrefixRest<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(xs, ys) ensures exists yss: seq<T> :: ys == xs + yss && |yss| == |ys|-|xs|; { assert ys == ys[..|xs|] + ys[|xs|..]; } lemma IsSuffixReversed<T>(xs: seq<T>, ys: seq<T>) requires IsSuffix(xs, ys) ensures IsPrefix(reverse(xs), reverse(ys)) { ReverseIndexAll(xs); ReverseIndexAll(ys); } lemma IsPrefixReversed<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(xs, ys) ensures IsSuffix(reverse(xs), reverse(ys)) { ReverseIndexAll(xs); ReverseIndexAll(ys); } lemma IsPrefixReversedAll<T>(xs: seq<T>, ys: seq<T>) requires IsPrefix(reverse(xs), reverse(ys)) ensures IsSuffix(reverse(reverse(xs)), reverse(reverse(ys))) { ReverseIndexAll(xs); ReverseIndexAll(ys); assert |ys| == |reverse(ys)|; assert reverse(xs) == reverse(ys)[..|reverse(xs)|]; assert reverse(xs) == reverse(ys)[..|xs|]; PrefixRest(reverse(xs), reverse(ys)); var yss :| reverse(ys) == reverse(xs) + yss && |yss| == |ys|-|xs|; reverseReverseIdempotent(ys); ReverseConcat(reverse(xs), yss); calc { reverse(reverse(ys)); ys; reverse(reverse(xs) + yss); reverse(yss)+reverse(reverse(xs)); == {reverseReverseIdempotent(xs);} reverse(yss)+xs; } } predicate IsSuffix2<T(==)>(xs: seq<T>, ys: seq<T>) { |xs| <= |ys| && exists K :: 0 <= K <= |ys|-|xs| && ys == ys[0..K] + xs + ys[(K+|xs|)..] } function reverse<A>(x: seq<A>): seq<A> { if x == [] then [] else reverse(x[1..])+[x[0]] } lemma {:induction false} reversePreservesMultiset<A>(xs: seq<A>) ensures multiset(xs) == multiset(reverse(xs)) { if xs == [] { }else { var x := xs[0]; assert xs == [x] + xs[1..]; assert multiset(xs) == multiset([x]) + multiset(xs[1..]); assert reverse(xs) == reverse(xs[1..])+[x]; reversePreservesMultiset(xs[1..]); assert multiset(xs[1..]) == multiset(reverse(xs[1..])); } } lemma reversePreservesLength<A>(xs: seq<A>) ensures |xs| == |reverse(xs)| { } lemma lastReverseIsFirst<A>(xs: seq<A>) requires |xs| > 0 ensures xs[0] == reverse(xs)[|reverse(xs)|-1] { reversePreservesLength(xs); assert |xs| == |reverse(xs)|; } lemma firstReverseIsLast<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs)[0] == xs[|xs|-1] { } lemma ReverseConcat<T>(xs: seq<T>, ys: seq<T>) ensures reverse(xs + ys) == reverse(ys) + reverse(xs) { // reveal Reverse(); if |xs| == 0 { assert xs + ys == ys; } else { assert xs + ys == [xs[0]] + (xs[1..] + ys); } } lemma reverseRest<A>(xs: seq<A>) requires |xs| > 0 ensures reverse(xs) == [xs[ |xs| -1 ] ] + reverse(xs[0..|xs|-1]) { firstReverseIsLast(xs); assert xs == xs[0..|xs|-1] + [xs[|xs|-1]]; assert reverse(xs)[0] == xs[ |xs| -1]; assert reverse(xs) == [xs[ |xs| -1]] + reverse(xs)[1..]; calc { reverse(xs); reverse(xs[0..|xs|-1] + [xs[|xs|-1]]); == {ReverseConcat(xs[0..|xs|-1], [xs[ |xs|-1 ]]);} reverse([xs[ |xs|-1 ]]) + reverse(xs[0..|xs|-1]); } } lemma ReverseIndexAll<T>(xs: seq<T>) ensures |reverse(xs)| == |xs| ensures forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1] { // reveal Reverse(); } lemma ReverseIndex<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[i] == xs[|xs| - i - 1] { ReverseIndexAll(xs); assert forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1]; } lemma ReverseIndexBack<T>(xs: seq<T>, i: int) requires 0 <= i < |xs| ensures |reverse(xs)| == |xs| ensures reverse(xs)[|xs| - i - 1] == xs[i] { ReverseIndexAll(xs); assert forall i :: 0 <= i < |xs| ==> reverse(xs)[i] == xs[|xs| - i - 1]; } lemma ReverseSingle<A>(xs: seq<A>) requires |xs| == 1 ensures reverse(xs) == xs { } lemma SeqEq<T>(xs: seq<T>, ys: seq<T>) requires |xs| == |ys| requires forall i :: 0 <= i < |xs| ==> xs[i] == ys[i] ensures xs == ys { } lemma reverseReverseIdempotent<A>(xs: seq<A>) ensures reverse(reverse(xs)) == xs { if xs == [] { }else{ calc { reverse(reverse(xs)); reverse(reverse([xs[0]] + xs[1..])); == {ReverseConcat([xs[0]] , xs[1..]);} reverse(reverse(xs[1..]) + reverse([xs[0]])); == {ReverseSingle([xs[0]]);} reverse(reverse(xs[1..]) + [xs[0]]); == {ReverseConcat(reverse(xs[1..]), [xs[0]]);} reverse([xs[0]]) + reverse(reverse(xs[1..])); [xs[0]] + reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs; } } /* Alternatively */ // ReverseIndexAll(reverse(xs)); // ReverseIndexAll(xs); // SeqEq(reverse(reverse(xs)), xs); } lemma notInNotEqual<A>(xs: seq<A>, elem: A) requires elem !in xs ensures forall k :: 0 <= k < |xs| ==> xs[k] != elem { } predicate distinct<A(==)>(s: seq<A>) { forall x,y :: x != y && 0 <= x <= y < |s| ==> s[x] != s[y] } lemma distincts<A>(xs: seq<A>, ys: seq<A>) requires distinct(xs) requires distinct(ys) requires forall x :: x in xs ==> x !in ys requires forall y :: y in ys ==> y !in xs ensures distinct(xs+ys) { var len := |xs + ys|; forall x,y | x != y && 0 <= x <= y < |xs+ys| ensures (xs+ys)[x] != (xs+ys)[y] { if 0 <= x < |xs| && 0 <= y < |xs| { assert (xs+ys)[x] != (xs+ys)[y]; }else if |xs| <= x < |xs+ys| && |xs| <= y < |xs+ys| { assert (xs+ys)[x] != (xs+ys)[y]; }else if 0 <= x < |xs| && |xs| <= y < |xs+ys| { notInNotEqual(ys, xs[x]); assert (xs+ys)[x] != (xs+ys)[y]; } } } lemma reverseDistinct<A>(list: seq<A>) requires distinct(list) ensures distinct(reverse(list)) { ReverseIndexAll(list); } lemma distinctSplits<A>(list: seq<A>) requires distinct(list) ensures forall i :: 1 <= i < |list| ==> distinct(list[..i]) {} lemma multisetItems<A>(list: seq<A>, item: A) requires item in list requires multiset(list)[item] > 1 ensures exists i,j :: 0 <= i < j < |list| && list[i] == item && list[j] == item && i != j { var k :| 0 <= k < |list| && list[k] == item; var rest := list[..k]+list[k+1..]; assert list == list[..k]+[item]+list[k+1..]; assert multiset(list) == multiset(list[..k])+multiset(list[k+1..])+multiset{item}; } lemma distinctMultisetIs1<A>(list: seq<A>, item: A) requires distinct(list) requires item in list ensures multiset(list)[item] == 1 { if multiset(list)[item] == 0 { assert false; } if multiset(list)[item] > 1 { multisetItems(list, item); var i,j :| 0 <= i < j < |list| && list[i] == item && list[j] == item && i != j; } } lemma indistinctMultisetIsGreaterThan1<A>(list: seq<A>) requires !distinct(list) ensures exists item :: multiset(list)[item] > 1 { var x,y :| x != y && 0 <= x <= y < |list| && list[x] == list[y]; var item := list[x]; assert x < y; assert list == list[..x] + [item] + list[(x+1)..y] + [item] + list[y+1..]; assert multiset(list)[item] > 1; } lemma multisetIsGreaterThan1Indistinct<A>(list: seq<A>) requires exists item :: multiset(list)[item] > 1 ensures !distinct(list) { var item :| multiset(list)[item] > 1; var x :| 0 <= x < |list| && list[x] == item; assert list == list[..x] + [item] + list[x+1..]; var y :| x != y && 0 <= y < |list| && list[y] == item; } lemma indistinctPlusX<A>(items: seq<A>, x: A) requires !distinct(items) ensures forall i :: 0 <= i < |items| ==> !distinct(items[..i]+[x]+items[i..]) { forall i | 0 <= i < |items| ensures !distinct(items[..i]+[x]+items[i..]) { indistinctMultisetIsGreaterThan1(items); var item :| multiset(items)[item] > 1; var itemsPlus := items[..i]+[x]+items[i..]; assert items == items[..i]+items[i..]; calc { multiset(itemsPlus); multiset(items[..i])+multiset(items[i..])+multiset{x}; multiset(items)+multiset{x}; } assert multiset(itemsPlus)[item] > 1; multisetIsGreaterThan1Indistinct(itemsPlus); } } lemma pigeonHolesMultiset<A>(items: set<A>, list: seq<A>, n: nat) requires |items| == n requires forall x :: x in list ==> x in items requires |list| > n ensures exists item :: multiset(list)[item] > 1 { if x :| multiset(list)[x] > 1 { }else if x :| multiset(list)[x] == 1 { assert x in list; var i :| 0 <= i < |list| && list[i] == x; assert list == list[..i] + [x] + list[i+1..]; assert list == list[..i] + list[i..]; assert multiset(list) == multiset(list[..i])+multiset(list[i+1..])+multiset{x}; var rest := list[..i]+list[i+1..]; assert multiset(rest)[x] == 0; forall y | y in rest ensures y in items-{x} { assert y in items; assert y != x; } if n -1 == 0 { // assert |items| == 1; // assert x in items; assert |items-{x}| == 0; assert list[0] in list; assert list[0] == x; assert list[1] in list; assert list[1] == x; assert false; }else{ pigeonHolesMultiset(items-{x}, rest, n-1); var item :| multiset(rest)[item] > 1; assert multiset(list) == multiset(rest) + multiset{x}; assert multiset(list)[item] > 1; } }else if x :| multiset(list)[x] == 0 {} } lemma pigeonHoles<A>(items: set<A>, list: seq<A>, n: nat) requires |items| == n requires forall x :: x in list ==> x in items requires |list| > n ensures !distinct(list) { if x :| multiset(list)[x] > 1 { multisetItems(list, x); var i,j :| 0 <= i < j < |list| && list[i] == x && list[j] == x && i != j; }else if x :| multiset(list)[x] == 1 { assert x in list; var i :| 0 <= i < |list| && list[i] == x; assert list == list[..i] + [x] + list[i+1..]; assert list == list[..i] + list[i..]; assert multiset(list) == multiset(list[..i])+multiset(list[i+1..])+multiset{x}; assert multiset(list)[x] == 1; var rest := list[..i]+list[i+1..]; assert multiset(rest)[x] == 0; forall y | y in rest ensures y in items-{x} { assert y in items; assert y != x; } pigeonHoles(items-{x}, rest, n-1); assert !distinct(rest); indistinctPlusX(rest, x); assert !distinct(list); }else if x :| multiset(list)[x] == 0 {} } lemma reverseInitList<T>(xs: seq<T>) requires |xs| > 1 requires |reverse(xs)| == |xs| ensures reverse(reverse(xs)[..|xs|-1]) == xs[1..] { assert xs == [xs[0]] + xs[1..]; assert |reverse(xs)| == |xs|; calc { reverse(xs); reverse(xs[1..])+reverse([xs[0]]); reverse(xs[1..])+[xs[0]]; } assert reverse(xs)[..|xs|-1] == reverse(xs[1..]); calc { reverse(reverse(xs)[..|xs|-1]); reverse(reverse(xs[1..])); == {reverseReverseIdempotent(xs[1..]);} xs[1..]; } } method SeqTest() { var t1 := [4,5,6,1,2,3]; // assert t1 == [4,5,6]+[1,2,3]; var s1 := [1,2,3]; assert IsSuffix(s1,t1); // assert isSubstring(s1,t1); assert substring1(s1, t1); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_algorithms and leetcode_lib_seq.dfy
298
298
Dafny program: 298
predicate sorted(a:array<int>, from:int, to:int) requires a != null; reads a; requires 0 <= from <= to <= a.Length; { forall u, v :: from <= u < v < to ==> a[u] <= a[v] } predicate pivot(a:array<int>, to:int, pvt:int) requires a != null; reads a; requires 0 <= pvt < to <= a.Length; { forall u, v :: 0 <= u < pvt < v < to ==> a[u] <= a[v] } method bubbleSort (a: array<int>) requires a != null && a.Length > 0; modifies a; ensures sorted(a, 0, a.Length); ensures multiset(a[..]) == multiset(old(a[..])); { var i:nat := 1; while (i < a.Length) { var j:nat := i; while (j > 0) { if (a[j-1] > a[j]) { var temp:int := a[j-1]; a[j-1] := a[j]; a[j] := temp; } j := j - 1; } i := i+1; } }
predicate sorted(a:array<int>, from:int, to:int) requires a != null; reads a; requires 0 <= from <= to <= a.Length; { forall u, v :: from <= u < v < to ==> a[u] <= a[v] } predicate pivot(a:array<int>, to:int, pvt:int) requires a != null; reads a; requires 0 <= pvt < to <= a.Length; { forall u, v :: 0 <= u < pvt < v < to ==> a[u] <= a[v] } method bubbleSort (a: array<int>) requires a != null && a.Length > 0; modifies a; ensures sorted(a, 0, a.Length); ensures multiset(a[..]) == multiset(old(a[..])); { var i:nat := 1; while (i < a.Length) invariant i <= a.Length; invariant sorted(a, 0, i); invariant multiset(a[..]) == multiset(old(a[..])); { var j:nat := i; while (j > 0) invariant multiset(a[..]) == multiset(old(a[..])); invariant sorted(a, 0, j); invariant sorted(a, j, i+1); invariant pivot(a, i+1, j); { if (a[j-1] > a[j]) { var temp:int := a[j-1]; a[j-1] := a[j]; a[j] := temp; } j := j - 1; } i := i+1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_basic examples_BubbleSort.dfy
299
299
Dafny program: 299
predicate sorted_between (a:array<int>, from:nat, to:nat) reads a; requires a != null; requires from <= to; requires to <= a.Length; { forall i,j :: from <= i < j < to && 0 <= i < j < a.Length ==> a[i] <= a[j] } predicate sorted (a:array<int>) reads a; requires a!=null; { sorted_between (a, 0, a.Length) } method bubbleSort (a: array<int>) modifies a; requires a != null; requires a.Length > 0; ensures sorted(a); ensures multiset(old(a[..])) == multiset(a[..]); { var i:nat := 1; while (i < a.Length) { var j:nat := i; while (j > 0) { if (a[j-1] > a[j]) { var temp:int := a[j-1]; // Introduced bug for permutation a[j-1] := a[j]; //a[j-1] := a[j-1]; a[j] := temp; } j := j - 1; } i := i+1; } }
predicate sorted_between (a:array<int>, from:nat, to:nat) reads a; requires a != null; requires from <= to; requires to <= a.Length; { forall i,j :: from <= i < j < to && 0 <= i < j < a.Length ==> a[i] <= a[j] } predicate sorted (a:array<int>) reads a; requires a!=null; { sorted_between (a, 0, a.Length) } method bubbleSort (a: array<int>) modifies a; requires a != null; requires a.Length > 0; ensures sorted(a); ensures multiset(old(a[..])) == multiset(a[..]); { var i:nat := 1; while (i < a.Length) invariant i <= a.Length; invariant sorted_between (a, 0, i); invariant multiset(old (a[..])) == multiset(a[..]); { var j:nat := i; while (j > 0) invariant 0 <= j <= i; invariant sorted_between (a, 0, j); invariant forall u,v:: 0 <= u < j < v < i+1 ==> a[u] <= a[v]; invariant sorted_between (a, j, i+1); invariant multiset(old (a[..])) == multiset(a[..]); { if (a[j-1] > a[j]) { var temp:int := a[j-1]; // Introduced bug for permutation a[j-1] := a[j]; //a[j-1] := a[j-1]; a[j] := temp; } j := j - 1; } i := i+1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_basic examples_BubbleSort_sol.dfy
300
300
Dafny program: 300
method add_by_one (x:int, y:int) returns (r:int) requires y >= 0; ensures r == x + y; { var i:int := 0; r := x; while (i < y) { r := r + 1; i := i + 1; } return r; } /* * Illustrates de-sugaring of the while loop. */ method bar (x:int, y:int) returns (r:int) requires y >= 0; ensures r == x + y; { var i := 0; r := x; // the invariant is true before the loop // the ranking function is positive before the loop // havoc variables assigned by the loop i, r := *, *; // assume the invariant holds assume (i <= y && r == x + i); // assume the ranking function is positive assume (y-i >= 0); // store the value of ranking function to compare against later ghost var rank_before := y-i; // one body of the loop if (i < y) { r := r + 1; i := i + 1; // invariant is true at the end of the loop // ranking function is positive at the end of the loop // ranking function has decreased // if got to here, stop verification of this branch assume (false); } // at this point only know the invariant of the loop + negation of // the loop condition return r; }
method add_by_one (x:int, y:int) returns (r:int) requires y >= 0; ensures r == x + y; { var i:int := 0; r := x; while (i < y) invariant i <= y; invariant r == x + i; decreases y-i; { r := r + 1; i := i + 1; } return r; } /* * Illustrates de-sugaring of the while loop. */ method bar (x:int, y:int) returns (r:int) requires y >= 0; ensures r == x + y; { var i := 0; r := x; // the invariant is true before the loop assert (i <= y && r == x + i); // the ranking function is positive before the loop assert (y-i >= 0); // havoc variables assigned by the loop i, r := *, *; // assume the invariant holds assume (i <= y && r == x + i); // assume the ranking function is positive assume (y-i >= 0); // store the value of ranking function to compare against later ghost var rank_before := y-i; // one body of the loop if (i < y) { r := r + 1; i := i + 1; // invariant is true at the end of the loop assert (i <= y && r == x + i); // ranking function is positive at the end of the loop assert (y-i >= 0); // ranking function has decreased assert (rank_before - (y-i) > 0); // if got to here, stop verification of this branch assume (false); } // at this point only know the invariant of the loop + negation of // the loop condition return r; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_basic examples_add_by_one.dfy
302
302
Dafny program: 302
method FindMax(a: array<int>) returns (max: int) requires a != null && a.Length > 0; ensures 0 <= max < a.Length; ensures forall x :: 0 <= x < a.Length ==> a[max] >= a[x]; { var i := 0; max := 0; while (i < a.Length) { if (a[i] > a[max]) { max := i; } i := i + 1; } return max; }
method FindMax(a: array<int>) returns (max: int) requires a != null && a.Length > 0; ensures 0 <= max < a.Length; ensures forall x :: 0 <= x < a.Length ==> a[max] >= a[x]; { var i := 0; max := 0; while (i < a.Length) invariant i <= a.Length; invariant 0 <= max; invariant max == 0 || 0 < max < i; invariant forall k :: 0 <= k < i ==> a[max] >= a[k] { if (a[i] > a[max]) { max := i; } i := i + 1; } return max; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_basic examples_find_max.dfy
304
304
Dafny program: 304
function sum_up_to (n: nat): nat { if (n == 0) then 0 else sum_up_to (n-1) + 1 } method SumUpTo (n: nat) returns (r: nat) ensures r == sum_up_to (n); { var i := 0; r := 0; while (i < n) { r := r + 1; i := i + 1; } } function total (a: seq<nat>) : nat { if |a| == 0 then 0 else total (a[0..|a|-1]) + a[|a|-1] } lemma total_lemma (a: seq<nat>, i:nat) requires |a| > 0; requires 0 <= i < |a|; ensures total (a[0..i]) + a[i] == total (a[0..i+1]); { ghost var b := a[0..i+1]; calc { total (a[0..i+1]); total (b); total (b[0..|b|-1]) + b[|b|-1]; total (b[0..|b|-1]) + a[i]; {assert (b[0..|b|-1] == a[0..i]);} total (a[0..i]) + a[i]; } } method Total (a: seq<nat>) returns (r:nat) ensures r == total (a[0..|a|]); { var i := 0; r := 0; while i < |a| { total_lemma (a, i); r := r + a[i]; i := i + 1; } }
function sum_up_to (n: nat): nat { if (n == 0) then 0 else sum_up_to (n-1) + 1 } method SumUpTo (n: nat) returns (r: nat) ensures r == sum_up_to (n); { var i := 0; r := 0; while (i < n) invariant 0 <= i <= n; invariant r == sum_up_to (i); { r := r + 1; i := i + 1; } } function total (a: seq<nat>) : nat { if |a| == 0 then 0 else total (a[0..|a|-1]) + a[|a|-1] } lemma total_lemma (a: seq<nat>, i:nat) requires |a| > 0; requires 0 <= i < |a|; ensures total (a[0..i]) + a[i] == total (a[0..i+1]); { ghost var b := a[0..i+1]; calc { total (a[0..i+1]); total (b); total (b[0..|b|-1]) + b[|b|-1]; total (b[0..|b|-1]) + a[i]; {assert (b[0..|b|-1] == a[0..i]);} total (a[0..i]) + a[i]; } } method Total (a: seq<nat>) returns (r:nat) ensures r == total (a[0..|a|]); { var i := 0; r := 0; while i < |a| invariant 0 <= i <= |a|; invariant r == total (a[0..i]); { total_lemma (a, i); r := r + a[i]; i := i + 1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_basic examples_sumto_sol.dfy
307
307
Dafny program: 307
// RUN: %testDafnyForEachResolver "%s" module SimpleBDD { class BDDNode { static ghost predicate bitfunc(f: map<seq<bool>, bool>, n: nat) { forall i:seq<bool> :: i in f <==> |i| == n } ghost var Contents: map<seq<bool>, bool> ghost var Repr: set<object> ghost var n: nat var f: BDDNode?, t: BDDNode? var b: bool ghost predicate valid() reads this, Repr { bitfunc(Contents,n) && (0 == n ==> (b <==> Contents[[]])) && (0 < n ==> this in Repr && f != null && t != null && t in Repr && f in Repr && t.Repr <= Repr && f.Repr <= Repr && this !in f.Repr && this !in t.Repr && t.valid() && f.valid() && t.n == f.n == n-1 && (forall s | s in t.Contents :: Contents[[true] + s] <==> t.Contents[s]) && (forall s | s in f.Contents :: Contents[[false] + s] <==> f.Contents[s])) } } class BDD { var root: BDDNode ghost predicate valid() reads this, Repr { root in Repr && root.Repr <= Repr && root.valid() && n == root.n && Contents == root.Contents } constructor () { root := new BDDNode; } ghost var Contents: map<seq<bool>, bool> var n: nat ghost var Repr: set<object> method Eval(s: seq<bool>) returns(b: bool) requires valid() && |s| == n ensures b == Contents[s] { var node: BDDNode := root; var i := n; while i > 0 { node := if s[n-i] then node.t else node.f; i := i - 1; } b := node.b; } } }
// RUN: %testDafnyForEachResolver "%s" module SimpleBDD { class BDDNode { static ghost predicate bitfunc(f: map<seq<bool>, bool>, n: nat) { forall i:seq<bool> :: i in f <==> |i| == n } ghost var Contents: map<seq<bool>, bool> ghost var Repr: set<object> ghost var n: nat var f: BDDNode?, t: BDDNode? var b: bool ghost predicate valid() reads this, Repr { bitfunc(Contents,n) && (0 == n ==> (b <==> Contents[[]])) && (0 < n ==> this in Repr && f != null && t != null && t in Repr && f in Repr && t.Repr <= Repr && f.Repr <= Repr && this !in f.Repr && this !in t.Repr && t.valid() && f.valid() && t.n == f.n == n-1 && (forall s | s in t.Contents :: Contents[[true] + s] <==> t.Contents[s]) && (forall s | s in f.Contents :: Contents[[false] + s] <==> f.Contents[s])) } } class BDD { var root: BDDNode ghost predicate valid() reads this, Repr { root in Repr && root.Repr <= Repr && root.valid() && n == root.n && Contents == root.Contents } constructor () { root := new BDDNode; } ghost var Contents: map<seq<bool>, bool> var n: nat ghost var Repr: set<object> method Eval(s: seq<bool>) returns(b: bool) requires valid() && |s| == n ensures b == Contents[s] { var node: BDDNode := root; var i := n; assert s[n-i..] == s; while i > 0 invariant node.valid() invariant 0 <= i == node.n <= n invariant Contents[s] == node.Contents[s[n-i..]] { assert s[n-i..] == [s[n-i]] + s[n-i+1..]; node := if s[n-i] then node.t else node.f; i := i - 1; } assert s[n-i..] == []; b := node.b; } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny1_BDD.dfy
308
308
Dafny program: 308
// RUN: %testDafnyForEachResolver "%s" class Node<T> { ghost var List: seq<T> ghost var Repr: set<Node<T>> var data: T var next: Node?<T> ghost predicate Valid() reads this, Repr { this in Repr && (next == null ==> List == [data]) && (next != null ==> next in Repr && next.Repr <= Repr && this !in next.Repr && List == [data] + next.List && next.Valid()) } constructor (d: T) ensures Valid() && fresh(Repr) ensures List == [d] { data, next := d, null; List, Repr := [d], {this}; } constructor InitAsPredecessor(d: T, succ: Node<T>) requires succ.Valid() ensures Valid() && fresh(Repr - succ.Repr) ensures List == [d] + succ.List { data, next := d, succ; List := [d] + succ.List; Repr := {this} + succ.Repr; } method Prepend(d: T) returns (r: Node<T>) requires Valid() ensures r.Valid() && fresh(r.Repr - old(Repr)) ensures r.List == [d] + List { r := new Node.InitAsPredecessor(d, this); } method SkipHead() returns (r: Node?<T>) requires Valid() ensures r == null ==> |List| == 1 ensures r != null ==> r.Valid() && r.List == List[1..] && r.Repr <= Repr { r := next; } method ReverseInPlace() returns (reverse: Node<T>) requires Valid() modifies Repr ensures reverse.Valid() && reverse.Repr <= old(Repr) ensures |reverse.List| == |old(List)| ensures forall i :: 0 <= i < |reverse.List| ==> reverse.List[i] == old(List)[|old(List)|-1-i] { var current := next; reverse := this; reverse.next := null; reverse.Repr := {reverse}; reverse.List := [data]; while current != null current.Valid() && current in old(Repr) && current.Repr <= old(Repr) && current.Repr !! reverse.Repr |old(List)| == |reverse.List| + |current.List| && current.List == old(List)[|reverse.List|..] { var nx := current.next; // ..., reverse, current, nx, ... current.next := reverse; current.Repr := {current} + reverse.Repr; current.List := [current.data] + reverse.List; reverse := current; current := nx; } } }
// RUN: %testDafnyForEachResolver "%s" class Node<T> { ghost var List: seq<T> ghost var Repr: set<Node<T>> var data: T var next: Node?<T> ghost predicate Valid() reads this, Repr { this in Repr && (next == null ==> List == [data]) && (next != null ==> next in Repr && next.Repr <= Repr && this !in next.Repr && List == [data] + next.List && next.Valid()) } constructor (d: T) ensures Valid() && fresh(Repr) ensures List == [d] { data, next := d, null; List, Repr := [d], {this}; } constructor InitAsPredecessor(d: T, succ: Node<T>) requires succ.Valid() ensures Valid() && fresh(Repr - succ.Repr) ensures List == [d] + succ.List { data, next := d, succ; List := [d] + succ.List; Repr := {this} + succ.Repr; } method Prepend(d: T) returns (r: Node<T>) requires Valid() ensures r.Valid() && fresh(r.Repr - old(Repr)) ensures r.List == [d] + List { r := new Node.InitAsPredecessor(d, this); } method SkipHead() returns (r: Node?<T>) requires Valid() ensures r == null ==> |List| == 1 ensures r != null ==> r.Valid() && r.List == List[1..] && r.Repr <= Repr { r := next; } method ReverseInPlace() returns (reverse: Node<T>) requires Valid() modifies Repr ensures reverse.Valid() && reverse.Repr <= old(Repr) ensures |reverse.List| == |old(List)| ensures forall i :: 0 <= i < |reverse.List| ==> reverse.List[i] == old(List)[|old(List)|-1-i] { var current := next; reverse := this; reverse.next := null; reverse.Repr := {reverse}; reverse.List := [data]; while current != null invariant reverse.Valid() && reverse.Repr <= old(Repr) invariant current == null ==> |old(List)| == |reverse.List| invariant current != null ==> current.Valid() && current in old(Repr) && current.Repr <= old(Repr) && current.Repr !! reverse.Repr invariant current != null ==> |old(List)| == |reverse.List| + |current.List| && current.List == old(List)[|reverse.List|..] invariant forall i :: 0 <= i < |reverse.List| ==> reverse.List[i] == old(List)[|reverse.List|-1-i] decreases if current != null then |current.List| else -1 { var nx := current.next; // ..., reverse, current, nx, ... current.next := reverse; current.Repr := {current} + reverse.Repr; current.List := [current.data] + reverse.List; reverse := current; current := nx; } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny1_ListContents.dfy
310
310
Dafny program: 310
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false /* Rustan Leino, 5 Oct 2011 COST Verification Competition, Challenge 2: Maximum in a tree http://foveoos2011.cost-ic0701.org/verification-competition Given: A non-empty binary tree, where every node carries an integer. Implement and verify a program that computes the maximum of the values in the tree. Please base your program on the following data structure signature: public class Tree { int value; Tree left; Tree right; } You may represent empty trees as null references or as you consider appropriate. */ // Remarks: // The specification of this program uses the common dynamic-frames idiom in Dafny: the // ghost field 'Contents' stores the abstract value of an object, the ghost field 'Repr' // stores the set of (references to) objects that make up the representation of the object // (which in this case is the Tree itself plus the 'Repr' sets of the left and right // subtrees), and a function 'Valid()' that returns 'true' when an object is in a // consistent state (that is, when an object satisfies the "class invariant"). // The design I used was to represent an empty tree as a Tree object whose left and // right pointers point to the object iself. This is convenient, because it lets // clients of Tree and the implementation of Tree always use non-null pointers to // Tree objects. // What needs to be human-trusted about this program is that the 'requires' and // 'ensures' clauses (that is, the pre- and postconditions, respectively) of // 'ComputeMax' are correct. And, since the specification talks about the ghost // variable 'Contents', one also needs to trust that the 'Valid()' function // constrains 'Contents' in a way that a human thinks matches the intuitive // definition of what the contents of a tree is. // To give a taste of that the 'Valid()' function does not over-constrain the // object, I have included two instance constructors, 'Empty()' and 'Node(...)'. // To take this a step further, one could also write a 'Main' method that // builds somme tree and then calls 'ComputeMax', but I didn't do that here. // About Dafny: // As always (when it is successful), Dafny verifies that the program does not // cause any run-time errors (like array index bounds errors), that the program // terminates, that expressions and functions are well defined, and that all // specifications are satisfied. The language prevents type errors by being type // safe, prevents dangling pointers by not having an "address-of" or "deallocate" // operation (which is accommodated at run time by a garbage collector), and // prevents arithmetic overflow errors by using mathematical integers (which // is accommodated at run time by using BigNum's). By proving that programs // terminate, Dafny proves that a program's time usage is finite, which implies // that the program's space usage is finite too. However, executing the // program may fall short of your hopes if you don't have enough time or // space; that is, the program may run out of space or may fail to terminate in // your lifetime, because Dafny does not prove that the time or space needed by // the program matches your execution environment. The only input fed to // the Dafny verifier/compiler is the program text below; Dafny then automatically // verifies and compiles the program (for this program in less than 2.5 seconds) // without further human intervention. class Tree { // an empty tree is represented by a Tree object with left==this==right var value: int var left: Tree? var right: Tree? ghost var Contents: seq<int> ghost var Repr: set<object> ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr { this in Repr && left != null && right != null && ((left == this == right && Contents == []) || (left in Repr && left.Repr <= Repr && this !in left.Repr && right in Repr && right.Repr <= Repr && this !in right.Repr && left.Valid() && right.Valid() && Contents == left.Contents + [value] + right.Contents)) } function IsEmpty(): bool requires Valid(); reads this, Repr; ensures IsEmpty() <==> Contents == []; { left == this } constructor Empty() ensures Valid() && Contents == []; { left, right := this, this; Contents := []; Repr := {this}; } constructor Node(lft: Tree, val: int, rgt: Tree) requires lft.Valid() && rgt.Valid(); ensures Valid() && Contents == lft.Contents + [val] + rgt.Contents; { left, value, right := lft, val, rgt; Contents := lft.Contents + [val] + rgt.Contents; Repr := lft.Repr + {this} + rgt.Repr; } lemma exists_intro<T>(P: T ~> bool, x: T) requires P.requires(x) requires P(x) ensures exists y :: P.requires(y) && P(y) { } method ComputeMax() returns (mx: int) requires Valid() && !IsEmpty(); ensures forall x :: x in Contents ==> x <= mx; ensures exists x :: x in Contents && x == mx; { mx := value; if (!left.IsEmpty()) { var m := left.ComputeMax(); mx := if mx < m then m else mx; } if (!right.IsEmpty()) { var m := right.ComputeMax(); mx := if mx < m then m else mx; } exists_intro(x reads this => x in Contents && x == mx, mx); } }
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false /* Rustan Leino, 5 Oct 2011 COST Verification Competition, Challenge 2: Maximum in a tree http://foveoos2011.cost-ic0701.org/verification-competition Given: A non-empty binary tree, where every node carries an integer. Implement and verify a program that computes the maximum of the values in the tree. Please base your program on the following data structure signature: public class Tree { int value; Tree left; Tree right; } You may represent empty trees as null references or as you consider appropriate. */ // Remarks: // The specification of this program uses the common dynamic-frames idiom in Dafny: the // ghost field 'Contents' stores the abstract value of an object, the ghost field 'Repr' // stores the set of (references to) objects that make up the representation of the object // (which in this case is the Tree itself plus the 'Repr' sets of the left and right // subtrees), and a function 'Valid()' that returns 'true' when an object is in a // consistent state (that is, when an object satisfies the "class invariant"). // The design I used was to represent an empty tree as a Tree object whose left and // right pointers point to the object iself. This is convenient, because it lets // clients of Tree and the implementation of Tree always use non-null pointers to // Tree objects. // What needs to be human-trusted about this program is that the 'requires' and // 'ensures' clauses (that is, the pre- and postconditions, respectively) of // 'ComputeMax' are correct. And, since the specification talks about the ghost // variable 'Contents', one also needs to trust that the 'Valid()' function // constrains 'Contents' in a way that a human thinks matches the intuitive // definition of what the contents of a tree is. // To give a taste of that the 'Valid()' function does not over-constrain the // object, I have included two instance constructors, 'Empty()' and 'Node(...)'. // To take this a step further, one could also write a 'Main' method that // builds somme tree and then calls 'ComputeMax', but I didn't do that here. // About Dafny: // As always (when it is successful), Dafny verifies that the program does not // cause any run-time errors (like array index bounds errors), that the program // terminates, that expressions and functions are well defined, and that all // specifications are satisfied. The language prevents type errors by being type // safe, prevents dangling pointers by not having an "address-of" or "deallocate" // operation (which is accommodated at run time by a garbage collector), and // prevents arithmetic overflow errors by using mathematical integers (which // is accommodated at run time by using BigNum's). By proving that programs // terminate, Dafny proves that a program's time usage is finite, which implies // that the program's space usage is finite too. However, executing the // program may fall short of your hopes if you don't have enough time or // space; that is, the program may run out of space or may fail to terminate in // your lifetime, because Dafny does not prove that the time or space needed by // the program matches your execution environment. The only input fed to // the Dafny verifier/compiler is the program text below; Dafny then automatically // verifies and compiles the program (for this program in less than 2.5 seconds) // without further human intervention. class Tree { // an empty tree is represented by a Tree object with left==this==right var value: int var left: Tree? var right: Tree? ghost var Contents: seq<int> ghost var Repr: set<object> ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr { this in Repr && left != null && right != null && ((left == this == right && Contents == []) || (left in Repr && left.Repr <= Repr && this !in left.Repr && right in Repr && right.Repr <= Repr && this !in right.Repr && left.Valid() && right.Valid() && Contents == left.Contents + [value] + right.Contents)) } function IsEmpty(): bool requires Valid(); reads this, Repr; ensures IsEmpty() <==> Contents == []; { left == this } constructor Empty() ensures Valid() && Contents == []; { left, right := this, this; Contents := []; Repr := {this}; } constructor Node(lft: Tree, val: int, rgt: Tree) requires lft.Valid() && rgt.Valid(); ensures Valid() && Contents == lft.Contents + [val] + rgt.Contents; { left, value, right := lft, val, rgt; Contents := lft.Contents + [val] + rgt.Contents; Repr := lft.Repr + {this} + rgt.Repr; } lemma exists_intro<T>(P: T ~> bool, x: T) requires P.requires(x) requires P(x) ensures exists y :: P.requires(y) && P(y) { } method ComputeMax() returns (mx: int) requires Valid() && !IsEmpty(); ensures forall x :: x in Contents ==> x <= mx; ensures exists x :: x in Contents && x == mx; decreases Repr; { mx := value; if (!left.IsEmpty()) { var m := left.ComputeMax(); mx := if mx < m then m else mx; } if (!right.IsEmpty()) { var m := right.ComputeMax(); mx := if mx < m then m else mx; } exists_intro(x reads this => x in Contents && x == mx, mx); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny2_COST-verif-comp-2011-2-MaxTree-class.dfy
311
311
Dafny program: 311
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false /* Rustan Leino, 5 Oct 2011 COST Verification Competition, Challenge 3: Two equal elements http://foveoos2011.cost-ic0701.org/verification-competition Given: An integer array a of length n+2 with n>=2. It is known that at least two values stored in the array appear twice (i.e., there are at least two duplets). Implement and verify a program finding such two values. You may assume that the array contains values between 0 and n-1. */ // Remarks: // The implementation of method 'Search' takes one pass through the elements of // the given array. To keep track of what it has seen, it allocates an array as // temporary storage--I imagine that this is what the competition designers // had in mind, since the problem description says one can assume the values // of the given array to lie in the range 0..n. // To keep track of whether it already has found one duplicate, the method // sets the output variables p and q as follows: // p != q - no duplicates found yet // p == q - one duplicate found so far, namely the value stored in p and q // Note, the loop invariant does not need to say anything about the state // of two duplicates having been found, because when the second duplicate is // found, the method returns. // What needs to be human-trusted about this program is the specification of // 'Search'. The specification straightforwardly lists the assumptions stated // in the problem description, including the given fact that the array contains // (at least) two distinct elements that each occurs (at least) twice. To // trust the specification of 'Search', a human also needs to trust the definition // of 'IsDuplicate' and its auxiliary function 'IsPrefixDuplicate'. // About Dafny: // As always (when it is successful), Dafny verifies that the program does not // cause any run-time errors (like array index bounds errors), that the program // terminates, that expressions and functions are well defined, and that all // specifications are satisfied. The language prevents type errors by being type // safe, prevents dangling pointers by not having an "address-of" or "deallocate" // operation (which is accommodated at run time by a garbage collector), and // prevents arithmetic overflow errors by using mathematical integers (which // is accommodated at run time by using BigNum's). By proving that programs // terminate, Dafny proves that a program's time usage is finite, which implies // that the program's space usage is finite too. However, executing the // program may fall short of your hopes if you don't have enough time or // space; that is, the program may run out of space or may fail to terminate in // your lifetime, because Dafny does not prove that the time or space needed by // the program matches your execution environment. The only input fed to // the Dafny verifier/compiler is the program text below; Dafny then automatically // verifies and compiles the program (for this program in less than 11 seconds) // without further human intervention. ghost predicate IsDuplicate(a: array<int>, p: int) reads a { IsPrefixDuplicate(a, a.Length, p) } ghost predicate IsPrefixDuplicate(a: array<int>, k: int, p: int) requires 0 <= k <= a.Length; reads a; { exists i,j :: 0 <= i < j < k && a[i] == a[j] == p } method Search(a: array<int>) returns (p: int, q: int) requires 4 <= a.Length; requires exists p,q :: p != q && IsDuplicate(a, p) && IsDuplicate(a, q); // two distinct duplicates exist requires forall i :: 0 <= i < a.Length ==> 0 <= a[i] < a.Length - 2; // the elements of "a" in the range [0.. a.Length-2] ensures p != q && IsDuplicate(a, p) && IsDuplicate(a, q); { // allocate an array "d" and initialize its elements to -1. var d := new int[a.Length-2]; var i := 0; while (i < d.Length) { d[i], i := -1, i+1; } i, p, q := 0, 0, 1; while (true) (d[j] == -1 && forall k :: 0 <= k < i ==> a[k] != j) || (0 <= d[j] < i && a[d[j]] == j); { var k := d[a[i]]; if (k == -1) { // a[i] does not exist in a[..i] d[a[i]] := i; } else { // we have encountered a duplicate if (p != q) { // this is the first duplicate encountered p, q := a[i], a[i]; } else if (p == a[i]) { // this is another copy of the same duplicate we have seen before } else { // this is the second duplicate q := a[i]; return; } } i := i + 1; } }
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false /* Rustan Leino, 5 Oct 2011 COST Verification Competition, Challenge 3: Two equal elements http://foveoos2011.cost-ic0701.org/verification-competition Given: An integer array a of length n+2 with n>=2. It is known that at least two values stored in the array appear twice (i.e., there are at least two duplets). Implement and verify a program finding such two values. You may assume that the array contains values between 0 and n-1. */ // Remarks: // The implementation of method 'Search' takes one pass through the elements of // the given array. To keep track of what it has seen, it allocates an array as // temporary storage--I imagine that this is what the competition designers // had in mind, since the problem description says one can assume the values // of the given array to lie in the range 0..n. // To keep track of whether it already has found one duplicate, the method // sets the output variables p and q as follows: // p != q - no duplicates found yet // p == q - one duplicate found so far, namely the value stored in p and q // Note, the loop invariant does not need to say anything about the state // of two duplicates having been found, because when the second duplicate is // found, the method returns. // What needs to be human-trusted about this program is the specification of // 'Search'. The specification straightforwardly lists the assumptions stated // in the problem description, including the given fact that the array contains // (at least) two distinct elements that each occurs (at least) twice. To // trust the specification of 'Search', a human also needs to trust the definition // of 'IsDuplicate' and its auxiliary function 'IsPrefixDuplicate'. // About Dafny: // As always (when it is successful), Dafny verifies that the program does not // cause any run-time errors (like array index bounds errors), that the program // terminates, that expressions and functions are well defined, and that all // specifications are satisfied. The language prevents type errors by being type // safe, prevents dangling pointers by not having an "address-of" or "deallocate" // operation (which is accommodated at run time by a garbage collector), and // prevents arithmetic overflow errors by using mathematical integers (which // is accommodated at run time by using BigNum's). By proving that programs // terminate, Dafny proves that a program's time usage is finite, which implies // that the program's space usage is finite too. However, executing the // program may fall short of your hopes if you don't have enough time or // space; that is, the program may run out of space or may fail to terminate in // your lifetime, because Dafny does not prove that the time or space needed by // the program matches your execution environment. The only input fed to // the Dafny verifier/compiler is the program text below; Dafny then automatically // verifies and compiles the program (for this program in less than 11 seconds) // without further human intervention. ghost predicate IsDuplicate(a: array<int>, p: int) reads a { IsPrefixDuplicate(a, a.Length, p) } ghost predicate IsPrefixDuplicate(a: array<int>, k: int, p: int) requires 0 <= k <= a.Length; reads a; { exists i,j :: 0 <= i < j < k && a[i] == a[j] == p } method Search(a: array<int>) returns (p: int, q: int) requires 4 <= a.Length; requires exists p,q :: p != q && IsDuplicate(a, p) && IsDuplicate(a, q); // two distinct duplicates exist requires forall i :: 0 <= i < a.Length ==> 0 <= a[i] < a.Length - 2; // the elements of "a" in the range [0.. a.Length-2] ensures p != q && IsDuplicate(a, p) && IsDuplicate(a, q); { // allocate an array "d" and initialize its elements to -1. var d := new int[a.Length-2]; var i := 0; while (i < d.Length) invariant 0 <= i <= d.Length && forall j :: 0 <= j < i ==> d[j] == -1; { d[i], i := -1, i+1; } i, p, q := 0, 0, 1; while (true) invariant 0 <= i < a.Length; invariant forall j :: 0 <= j < d.Length ==> (d[j] == -1 && forall k :: 0 <= k < i ==> a[k] != j) || (0 <= d[j] < i && a[d[j]] == j); invariant p == q ==> IsDuplicate(a, p); //WISH remove the trigger on the next line invariant forall k {:trigger old(a[k])} :: 0 <= k < i && IsPrefixDuplicate(a, i, a[k]) ==> p == q == a[k]; decreases a.Length - i; { var k := d[a[i]]; assert k < i; // note, this assertion is really for human consumption; it is not needed by the verifier, and it does not change the performance of the verifier if (k == -1) { // a[i] does not exist in a[..i] d[a[i]] := i; } else { // we have encountered a duplicate assert a[i] == a[k] && IsDuplicate(a, a[i]); // note, this assertion is really for human consumption; it is not needed by the verifier, and it does not change the performance of the verifier if (p != q) { // this is the first duplicate encountered p, q := a[i], a[i]; } else if (p == a[i]) { // this is another copy of the same duplicate we have seen before } else { // this is the second duplicate q := a[i]; return; } } i := i + 1; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny2_COST-verif-comp-2011-3-TwoDuplicates.dfy
312
312
Dafny program: 312
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false // A version of Turing's additive factorial program [Dr. A. Turing, "Checking a large routine", // In "Report of a Conference of High Speed Automatic Calculating Machines", pp. 67-69, 1949]. ghost function Factorial(n: nat): nat { if n == 0 then 1 else n * Factorial(n-1) } method AdditiveFactorial(n: nat) returns (u: nat) ensures u == Factorial(n); { u := 1; var r := 0; while (r < n) { var v := u; var s := 1; while (s <= r) { u := u + v; s := s + 1; } r := r + 1; } } // Hoare's FIND program [C.A.R. Hoare, "Proof of a program: FIND", CACM 14(1): 39-45, 1971]. // The proof annotations here are not the same as in Hoare's article. // In Hoare's words: // This program operates on an array A[1:N], and a value of f (1 <= f <= N). // Its effect is to rearrange the elements of A in such a way that: // forall p,q (1 <= p <= f <= q <= N ==> A[p] <= A[f] <= A[q]). // // Here, we use 0-based indices, so we would say: // This method operates on an array A[0..N], and a value of f (0 <= f < N). // Its effect is to rearrange the elements of A in such a way that: // forall p,q :: 0 <= p <= f <= q < N ==> A[p] <= A[f] <= A[q]). method FIND(A: array<int>, N: int, f: int) requires A.Length == N; requires 0 <= f < N; modifies A; ensures forall p,q :: 0 <= p <= f <= q < N ==> A[p] <= A[q]; { var m, n := 0, N-1; while (m < n) { var r, i, j := A[f], m, n; while (i <= j) // the following two invariants capture (and follow from) the fact that the array is not modified outside the [m:n] range // the following invariant is used to prove progress of the outer loop { ghost var firstIteration := i==m && j==n; while (A[i] < r) { i := i + 1; } while (r < A[j]) { j := j - 1; } if (i <= j) { var w := A[i]; A[i] := A[j]; A[j] := w; // swap A[i] and A[j] (which may be referring to the same location) i, j := i + 1, j - 1; } } if (f <= j) { n := j; } else if (i <= f) { m := i; } else { break; // Hoare used a goto } } }
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false // A version of Turing's additive factorial program [Dr. A. Turing, "Checking a large routine", // In "Report of a Conference of High Speed Automatic Calculating Machines", pp. 67-69, 1949]. ghost function Factorial(n: nat): nat { if n == 0 then 1 else n * Factorial(n-1) } method AdditiveFactorial(n: nat) returns (u: nat) ensures u == Factorial(n); { u := 1; var r := 0; while (r < n) invariant 0 <= r <= n; invariant u == Factorial(r); { var v := u; var s := 1; while (s <= r) invariant 1 <= s <= r+1; invariant u == s * Factorial(r); { u := u + v; s := s + 1; } r := r + 1; } } // Hoare's FIND program [C.A.R. Hoare, "Proof of a program: FIND", CACM 14(1): 39-45, 1971]. // The proof annotations here are not the same as in Hoare's article. // In Hoare's words: // This program operates on an array A[1:N], and a value of f (1 <= f <= N). // Its effect is to rearrange the elements of A in such a way that: // forall p,q (1 <= p <= f <= q <= N ==> A[p] <= A[f] <= A[q]). // // Here, we use 0-based indices, so we would say: // This method operates on an array A[0..N], and a value of f (0 <= f < N). // Its effect is to rearrange the elements of A in such a way that: // forall p,q :: 0 <= p <= f <= q < N ==> A[p] <= A[f] <= A[q]). method FIND(A: array<int>, N: int, f: int) requires A.Length == N; requires 0 <= f < N; modifies A; ensures forall p,q :: 0 <= p <= f <= q < N ==> A[p] <= A[q]; { var m, n := 0, N-1; while (m < n) invariant 0 <= m <= f <= n < N; invariant forall p,q :: 0 <= p < m <= q < N ==> A[p] <= A[q]; invariant forall p,q :: 0 <= p <= n < q < N ==> A[p] <= A[q]; { var r, i, j := A[f], m, n; while (i <= j) invariant m <= i && j <= n; invariant -1 <= j && i <= N; invariant i <= j ==> exists g :: i <= g < N && r <= A[g]; invariant i <= j ==> exists g :: 0 <= g <= j && A[g] <= r; invariant forall p :: 0 <= p < i ==> A[p] <= r; invariant forall q :: j < q < N ==> r <= A[q]; // the following two invariants capture (and follow from) the fact that the array is not modified outside the [m:n] range invariant forall p,q :: 0 <= p < m <= q < N ==> A[p] <= A[q]; invariant forall p,q :: 0 <= p <= n < q < N ==> A[p] <= A[q]; // the following invariant is used to prove progress of the outer loop invariant (i==m && j==n && r==A[f]) || (m<i && j<n); { ghost var firstIteration := i==m && j==n; while (A[i] < r) invariant m <= i <= N && (firstIteration ==> i <= f); invariant exists g :: i <= g < N && r <= A[g]; invariant exists g :: 0 <= g <= j && A[g] <= r; invariant forall p :: 0 <= p < i ==> A[p] <= r; decreases j - i; { i := i + 1; } while (r < A[j]) invariant 0 <= j <= n && (firstIteration ==> f <= j); invariant exists g :: i <= g < N && r <= A[g]; invariant exists g :: 0 <= g <= j && A[g] <= r; invariant forall q :: j < q < N ==> r <= A[q]; decreases j; { j := j - 1; } assert A[j] <= r <= A[i]; if (i <= j) { var w := A[i]; A[i] := A[j]; A[j] := w; // swap A[i] and A[j] (which may be referring to the same location) assert A[i] <= r <= A[j]; i, j := i + 1, j - 1; } } if (f <= j) { n := j; } else if (i <= f) { m := i; } else { break; // Hoare used a goto } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny2_Classics.dfy
313
313
Dafny program: 313
// RUN: %testDafnyForEachResolver "%s" // Rustan Leino, June 2012. // This file verifies an algorithm, due to Boyer and Moore, that finds the majority choice // among a sequence of votes, see http://www.cs.utexas.edu/~moore/best-ideas/mjrty/. // Actually, this algorithm is a slight variation on theirs, but the general idea for why // it is correct is the same. In the Boyer and Moore algorithm, the loop counter is advanced // by exactly 1 each iteration, which means that there may or may not be a "current leader". // In my program below, I had instead written the loop invariant to say there is always a // "current leader", which requires the loop index sometimes to skip a value. // // This file has two versions of the algorithm. In the first version, the given sequence // of votes is assumed to have a (strict) majority choice, meaning that strictly more than // 50% of the votes are for one candidate. It is convenient to have a name for the majority // choice, in order to talk about it in specifications. The easiest way to do this in // Dafny is probably to introduce a ghost parameter with the given properties. That's what // the algorithm does, see parameter K. The postcondition is thus to output the value of // K, which is done in the non-ghost out-parameter k. // The proof of the algorithm requires two lemmas. These lemmas are proved automatically // by Dafny's induction tactic. // // In the second version of the program, the main method does not assume there is a majority // choice. Rather, it eseentially uses the first algorithm and then checks if what it // returns really is a majority choice. To do this, the specification of the first algorithm // needs to be changed slightly to accommodate the possibility that there is no majority // choice. That change in specification is also reflected in the loop invariant. Moreover, // the algorithm itself now needs to extra 'if' statements to see if the entire sequence // has been searched through. (This extra 'if' is essentially already handled by Boyer and // Moore's algorithm, because it increments the loop index by 1 each iteration and therefore // already has a special case for the case of running out of sequence elements without a // current leader.) // The calling harness, DetermineElection, somewhat existentially comes up with the majority // choice, if there is such a choice, and then passes in that choice as the ghost parameter K // to the main algorithm. Neat, huh? // Language comment: // The "(==)" that sits after some type parameters in this program says that the actual // type argument must support equality. // Advanced remark: // There is a subtle situation in the verification of DetermineElection. Suppose the type // parameter Candidate denotes some type whose instances depend on which object are // allocated. For example, if Candidate is some class type, then more candidates can come // into being by object allocations (using "new"). What does the quantification of // candidates "c" in the postcondition of DetermineElection now mean--all candidates that // existed in the pre-state or (the possibly larger set of) all candidates that exist in the // post-state? (It means the latter.) And if there does not exist a candidate in majority // in the pre-state, could there be a (newly created) candidate in majority in the post-state? // This will require some proof. The simplest argument seems to be that even if more candidates // are created during the course of DetermineElection, such candidates cannot possibly // be in majority in the sequence "a", since "a" can only contain candidates that were already // created in the pre-state. This property is easily specified by adding a postcondition // to the Count function. Alternatively, one could have added the antecedent "c in a" or // "old(allocated(c))" to the "forall c" quantification in the postcondition of DetermineElection. // About reading the proofs: // Dafny proves the FindWinner algorithm from the given loop invariants and the two lemmas // Lemma_Unique and Lemma_Split. In showing this proof to some colleagues, they found they // were not as quick as Dafny in constructing the proof from these ingredients. For a human // to understand the situation better, it helps to take smaller (and more) steps in the proof. // At the end of this file, Nadia Polikarpova has written two versions of FindWinner that does // that, using Dafny's support for calculational proofs. function Count<T(==)>(a: seq<T>, s: int, t: int, x: T): int requires 0 <= s <= t <= |a| { if s == t then 0 else Count(a, s, t-1, x) + if a[t-1] == x then 1 else 0 } ghost predicate HasMajority<T>(a: seq<T>, s: int, t: int, x: T) requires 0 <= s <= t <= |a| { 2 * Count(a, s, t, x) > t - s } // Here is the first version of the algorithm, the one that assumes there is a majority choice. method FindWinner<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; var n, c, s := 1, 1, 0; while n < |a| { if a[n] == k { n, c := n + 1, c + 1; } else if 2 * c > n + 1 - s { n := n + 1; } else { n := n + 1; // We have 2*Count(a, s, n, k) == n-s, and thus the following lemma // lets us conclude 2*Count(a, s, n, K) <= n-s. Lemma_Unique(a, s, n, K, k); // We also have 2*Count(a, s, |a|, K) > |a|-s, and the following lemma // tells us Count(a, s, |a|, K) == Count(a, s, n, K) + Count(a, n, |a|, K), // and thus we can conclude 2*Count(a, n, |a|, K) > |a|-n. Lemma_Split(a, s, n, |a|, K); k, n, c, s := a[n], n + 1, 1, n; } } Lemma_Unique(a, s, |a|, K, k); // both k and K have a majority, so K == k } // ------------------------------------------------------------------------------ // Here is the second version of the program, the one that also computes whether or not // there is a majority choice. datatype Result<Candidate> = NoWinner | Winner(cand: Candidate) method DetermineElection<Candidate(==,0,!new)>(a: seq<Candidate>) returns (result: Result<Candidate>) ensures result.Winner? ==> 2 * Count(a, 0, |a|, result.cand) > |a| ensures result.NoWinner? ==> forall c :: 2 * Count(a, 0, |a|, c) <= |a| { if |a| == 0 { return NoWinner; } ghost var b := exists c :: 2 * Count(a, 0, |a|, c) > |a|; ghost var w :| b ==> 2 * Count(a, 0, |a|, w) > |a|; var cand := SearchForWinner(a, b, w); return if 2 * Count(a, 0, |a|, cand) > |a| then Winner(cand) else NoWinner; } // The difference between SearchForWinner for FindWinner above are the occurrences of the // antecedent "hasWinner ==>" and the two checks for no-more-votes that may result in a "return" // statement. method SearchForWinner<Candidate(==)>(a: seq<Candidate>, ghost hasWinner: bool, ghost K: Candidate) returns (k: Candidate) requires |a| != 0 requires hasWinner ==> 2 * Count(a, 0, |a|, K) > |a| // K has a (strict) majority of the votes ensures hasWinner ==> k == K // find K { k := a[0]; var n, c, s := 1, 1, 0; while n < |a| { if a[n] == k { n, c := n + 1, c + 1; } else if 2 * c > n + 1 - s { n := n + 1; } else { n := n + 1; // We have 2*Count(a, s, n, k) == n-s, and thus the following lemma // lets us conclude 2*Count(a, s, n, K) <= n-s. Lemma_Unique(a, s, n, K, k); // We also have 2*Count(a, s, |a|, K) > |a|-s, and the following lemma // tells us Count(a, s, |a|, K) == Count(a, s, n, K) + Count(a, n, |a|, K), // and thus we can conclude 2*Count(a, n, |a|, K) > |a|-n. Lemma_Split(a, s, n, |a|, K); if |a| == n { return; } k, n, c, s := a[n], n + 1, 1, n; } } Lemma_Unique(a, s, |a|, K, k); // both k and K have a majority, so K == k } // ------------------------------------------------------------------------------ // Here are two lemmas about Count that are used in the methods above. lemma Lemma_Split<T>(a: seq<T>, s: int, t: int, u: int, x: T) requires 0 <= s <= t <= u <= |a| ensures Count(a, s, t, x) + Count(a, t, u, x) == Count(a, s, u, x) { /* The postcondition of this method is proved automatically via Dafny's induction tactic. But if a manual proof had to be provided, it would look like this: if s != t { Lemma_Split(a, s, t-1, u, x); } */ } lemma Lemma_Unique<T>(a: seq<T>, s: int, t: int, x: T, y: T) requires 0 <= s <= t <= |a| ensures x != y ==> Count(a, s, t, x) + Count(a, s, t, y) <= t - s { /* The postcondition of this method is proved automatically via Dafny's induction tactic. But if a manual proof had to be provided, it would look like this: if s != t { Lemma_Unique(a, s, t-1, x, y); } */ } // ------------------------------------------------------------------------------ // This version uses more calculations with integer formulas method FindWinner'<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; // Current candidate: the first element var lo, up, c := 0, 1, 1; // Window: [0..1], number of occurrences of k in the window: 1 while up < |a| { if a[up] == k { // One more occurrence of k up, c := up + 1, c + 1; } else if 2 * c > up + 1 - lo { // An occurrence of another value, but k still has the majority up := up + 1; } else { // An occurrence of another value and k just lost the majority. // Prove that k has exactly 50% in the future window a[lo..up + 1]: calc /* k has 50% among a[lo..up + 1] */ { true; == // negation of the previous branch condition; 2 * c <= up + 1 - lo; == // loop invariant (3) 2 * Count(a, lo, up, k) <= up + 1 - lo; == calc { true; == // loop invariant (2) HasMajority(a, lo, up, k); == // def. HasMajority 2 * Count(a, lo, up, k) > up - lo; == 2 * Count(a, lo, up, k) >= up + 1 - lo; } 2 * Count(a, lo, up, k) == up + 1 - lo; } up := up + 1; // We are going to start a new window a[up..up + 1] and choose a new candidate, // so invariants (2) and (3) will be easy to re-establish. // To re-establish (1) we have to prove that K has majority among a[up..], as up will become the new lo. // The main idea is that we had enough K's in a[lo..], and there cannot be too many in a[lo..up]. calc /* K has majority among a[up..] */ { 2 * Count(a, up, |a|, K); == { Lemma_Split(a, lo, up, |a|, K); } 2 * Count(a, lo, |a|, K) - 2 * Count(a, lo, up, K); > { assert HasMajority(a, lo, |a|, K); } // loop invariant (1) |a| - lo - 2 * Count(a, lo, up, K); >= { if k == K { calc { 2 * Count(a, lo, up, K); == 2 * Count(a, lo, up, k); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } else { calc { 2 * Count(a, lo, up, K); <= { Lemma_Unique(a, lo, up, k, K); } 2 * ((up - lo) - Count(a, lo, up, k)); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } } |a| - lo - (up - lo); == |a| - up; } k, lo, up, c := a[up], up, up + 1, 1; } } Lemma_Unique(a, lo, |a|, K, k); // both k and K have a majority among a[lo..], so K == k } // This version uses more calculations with boolean formulas method FindWinner''<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; // Current candidate: the first element var lo, up, c := 0, 1, 1; // Window: [0..1], number of occurrences of k in the window: 1 while up < |a| { if a[up] == k { // One more occurrence of k up, c := up + 1, c + 1; } else if 2 * c > up + 1 - lo { // An occurrence of another value, but k still has the majority up := up + 1; } else { // An occurrence of another value and k just lost the majority. // Prove that k has exactly 50% in the future window a[lo..up + 1]: calc /* k has 50% among a[lo..up + 1] */ { true; == // negation of the previous branch condition 2 * c <= up + 1 - lo; == // loop invariant (3) 2 * Count(a, lo, up, k) <= up + 1 - lo; == calc { true; == // loop invariant (2) HasMajority(a, lo, up, k); == // def. HasMajority 2 * Count(a, lo, up, k) > up - lo; == 2 * Count(a, lo, up, k) >= up + 1 - lo; } 2 * Count(a, lo, up, k) == up + 1 - lo; } up := up + 1; // We are going to start a new window a[up..up + 1] and choose a new candidate, // so invariants (2) and (3) will be easy to re-establish. // To re-establish (1) we have to prove that K has majority among a[up..], as up will become the new lo. // The main idea is that we had enough K's in a[lo..], and there cannot be too many in a[lo..up]. calc /* K has majority among a[up..] */ { true; == // loop invariant (1) HasMajority(a, lo, |a|, K); == 2 * Count(a, lo, |a|, K) > |a| - lo; == { Lemma_Split(a, lo, up, |a|, K); } 2 * Count(a, lo, up, K) + 2 * Count(a, up, |a|, K) > |a| - lo; ==> { if k == K { calc { 2 * Count(a, lo, up, K); == 2 * Count(a, lo, up, k); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } else { calc { true; == { Lemma_Unique(a, lo, up, k, K); } Count(a, lo, up, K) + Count(a, lo, up, k) <= up - lo; == 2 * Count(a, lo, up, K) + 2 * Count(a, lo, up, k) <= 2 * (up - lo); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] 2 * Count(a, lo, up, K) <= up - lo; } } } // subtract off Count(a, lo, up, K) from the LHS and subtract off the larger amount up - lo from the RHS 2 * Count(a, up, |a|, K) > (|a| - lo) - (up - lo); == 2 * Count(a, up, |a|, K) > |a| - up; == HasMajority(a, up, |a|, K); } k, lo, up, c := a[up], up, up + 1, 1; } } Lemma_Unique(a, lo, |a|, K, k); // both k and K have a majority among a[lo..], so K == k }
// RUN: %testDafnyForEachResolver "%s" // Rustan Leino, June 2012. // This file verifies an algorithm, due to Boyer and Moore, that finds the majority choice // among a sequence of votes, see http://www.cs.utexas.edu/~moore/best-ideas/mjrty/. // Actually, this algorithm is a slight variation on theirs, but the general idea for why // it is correct is the same. In the Boyer and Moore algorithm, the loop counter is advanced // by exactly 1 each iteration, which means that there may or may not be a "current leader". // In my program below, I had instead written the loop invariant to say there is always a // "current leader", which requires the loop index sometimes to skip a value. // // This file has two versions of the algorithm. In the first version, the given sequence // of votes is assumed to have a (strict) majority choice, meaning that strictly more than // 50% of the votes are for one candidate. It is convenient to have a name for the majority // choice, in order to talk about it in specifications. The easiest way to do this in // Dafny is probably to introduce a ghost parameter with the given properties. That's what // the algorithm does, see parameter K. The postcondition is thus to output the value of // K, which is done in the non-ghost out-parameter k. // The proof of the algorithm requires two lemmas. These lemmas are proved automatically // by Dafny's induction tactic. // // In the second version of the program, the main method does not assume there is a majority // choice. Rather, it eseentially uses the first algorithm and then checks if what it // returns really is a majority choice. To do this, the specification of the first algorithm // needs to be changed slightly to accommodate the possibility that there is no majority // choice. That change in specification is also reflected in the loop invariant. Moreover, // the algorithm itself now needs to extra 'if' statements to see if the entire sequence // has been searched through. (This extra 'if' is essentially already handled by Boyer and // Moore's algorithm, because it increments the loop index by 1 each iteration and therefore // already has a special case for the case of running out of sequence elements without a // current leader.) // The calling harness, DetermineElection, somewhat existentially comes up with the majority // choice, if there is such a choice, and then passes in that choice as the ghost parameter K // to the main algorithm. Neat, huh? // Language comment: // The "(==)" that sits after some type parameters in this program says that the actual // type argument must support equality. // Advanced remark: // There is a subtle situation in the verification of DetermineElection. Suppose the type // parameter Candidate denotes some type whose instances depend on which object are // allocated. For example, if Candidate is some class type, then more candidates can come // into being by object allocations (using "new"). What does the quantification of // candidates "c" in the postcondition of DetermineElection now mean--all candidates that // existed in the pre-state or (the possibly larger set of) all candidates that exist in the // post-state? (It means the latter.) And if there does not exist a candidate in majority // in the pre-state, could there be a (newly created) candidate in majority in the post-state? // This will require some proof. The simplest argument seems to be that even if more candidates // are created during the course of DetermineElection, such candidates cannot possibly // be in majority in the sequence "a", since "a" can only contain candidates that were already // created in the pre-state. This property is easily specified by adding a postcondition // to the Count function. Alternatively, one could have added the antecedent "c in a" or // "old(allocated(c))" to the "forall c" quantification in the postcondition of DetermineElection. // About reading the proofs: // Dafny proves the FindWinner algorithm from the given loop invariants and the two lemmas // Lemma_Unique and Lemma_Split. In showing this proof to some colleagues, they found they // were not as quick as Dafny in constructing the proof from these ingredients. For a human // to understand the situation better, it helps to take smaller (and more) steps in the proof. // At the end of this file, Nadia Polikarpova has written two versions of FindWinner that does // that, using Dafny's support for calculational proofs. function Count<T(==)>(a: seq<T>, s: int, t: int, x: T): int requires 0 <= s <= t <= |a| { if s == t then 0 else Count(a, s, t-1, x) + if a[t-1] == x then 1 else 0 } ghost predicate HasMajority<T>(a: seq<T>, s: int, t: int, x: T) requires 0 <= s <= t <= |a| { 2 * Count(a, s, t, x) > t - s } // Here is the first version of the algorithm, the one that assumes there is a majority choice. method FindWinner<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; var n, c, s := 1, 1, 0; while n < |a| invariant 0 <= s <= n <= |a| invariant 2 * Count(a, s, |a|, K) > |a| - s // K has majority among a[s..] invariant 2 * Count(a, s, n, k) > n - s // k has majority among a[s..n] invariant c == Count(a, s, n, k) { if a[n] == k { n, c := n + 1, c + 1; } else if 2 * c > n + 1 - s { n := n + 1; } else { n := n + 1; // We have 2*Count(a, s, n, k) == n-s, and thus the following lemma // lets us conclude 2*Count(a, s, n, K) <= n-s. Lemma_Unique(a, s, n, K, k); // We also have 2*Count(a, s, |a|, K) > |a|-s, and the following lemma // tells us Count(a, s, |a|, K) == Count(a, s, n, K) + Count(a, n, |a|, K), // and thus we can conclude 2*Count(a, n, |a|, K) > |a|-n. Lemma_Split(a, s, n, |a|, K); k, n, c, s := a[n], n + 1, 1, n; } } Lemma_Unique(a, s, |a|, K, k); // both k and K have a majority, so K == k } // ------------------------------------------------------------------------------ // Here is the second version of the program, the one that also computes whether or not // there is a majority choice. datatype Result<Candidate> = NoWinner | Winner(cand: Candidate) method DetermineElection<Candidate(==,0,!new)>(a: seq<Candidate>) returns (result: Result<Candidate>) ensures result.Winner? ==> 2 * Count(a, 0, |a|, result.cand) > |a| ensures result.NoWinner? ==> forall c :: 2 * Count(a, 0, |a|, c) <= |a| { if |a| == 0 { return NoWinner; } ghost var b := exists c :: 2 * Count(a, 0, |a|, c) > |a|; ghost var w :| b ==> 2 * Count(a, 0, |a|, w) > |a|; var cand := SearchForWinner(a, b, w); return if 2 * Count(a, 0, |a|, cand) > |a| then Winner(cand) else NoWinner; } // The difference between SearchForWinner for FindWinner above are the occurrences of the // antecedent "hasWinner ==>" and the two checks for no-more-votes that may result in a "return" // statement. method SearchForWinner<Candidate(==)>(a: seq<Candidate>, ghost hasWinner: bool, ghost K: Candidate) returns (k: Candidate) requires |a| != 0 requires hasWinner ==> 2 * Count(a, 0, |a|, K) > |a| // K has a (strict) majority of the votes ensures hasWinner ==> k == K // find K { k := a[0]; var n, c, s := 1, 1, 0; while n < |a| invariant 0 <= s <= n <= |a| invariant hasWinner ==> 2 * Count(a, s, |a|, K) > |a| - s // K has majority among a[s..] invariant 2 * Count(a, s, n, k) > n - s // k has majority among a[s..n] invariant c == Count(a, s, n, k) { if a[n] == k { n, c := n + 1, c + 1; } else if 2 * c > n + 1 - s { n := n + 1; } else { n := n + 1; // We have 2*Count(a, s, n, k) == n-s, and thus the following lemma // lets us conclude 2*Count(a, s, n, K) <= n-s. Lemma_Unique(a, s, n, K, k); // We also have 2*Count(a, s, |a|, K) > |a|-s, and the following lemma // tells us Count(a, s, |a|, K) == Count(a, s, n, K) + Count(a, n, |a|, K), // and thus we can conclude 2*Count(a, n, |a|, K) > |a|-n. Lemma_Split(a, s, n, |a|, K); if |a| == n { return; } k, n, c, s := a[n], n + 1, 1, n; } } Lemma_Unique(a, s, |a|, K, k); // both k and K have a majority, so K == k } // ------------------------------------------------------------------------------ // Here are two lemmas about Count that are used in the methods above. lemma Lemma_Split<T>(a: seq<T>, s: int, t: int, u: int, x: T) requires 0 <= s <= t <= u <= |a| ensures Count(a, s, t, x) + Count(a, t, u, x) == Count(a, s, u, x) { /* The postcondition of this method is proved automatically via Dafny's induction tactic. But if a manual proof had to be provided, it would look like this: if s != t { Lemma_Split(a, s, t-1, u, x); } */ } lemma Lemma_Unique<T>(a: seq<T>, s: int, t: int, x: T, y: T) requires 0 <= s <= t <= |a| ensures x != y ==> Count(a, s, t, x) + Count(a, s, t, y) <= t - s { /* The postcondition of this method is proved automatically via Dafny's induction tactic. But if a manual proof had to be provided, it would look like this: if s != t { Lemma_Unique(a, s, t-1, x, y); } */ } // ------------------------------------------------------------------------------ // This version uses more calculations with integer formulas method FindWinner'<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; // Current candidate: the first element var lo, up, c := 0, 1, 1; // Window: [0..1], number of occurrences of k in the window: 1 while up < |a| invariant 0 <= lo < up <= |a| // (0) invariant HasMajority(a, lo, |a|, K) // (1) K has majority among a[lo..] invariant HasMajority(a, lo, up, k) // (2) k has majority among a[lo..up] (in the current window) invariant c == Count(a, lo, up, k) // (3) { if a[up] == k { // One more occurrence of k up, c := up + 1, c + 1; } else if 2 * c > up + 1 - lo { // An occurrence of another value, but k still has the majority up := up + 1; } else { // An occurrence of another value and k just lost the majority. // Prove that k has exactly 50% in the future window a[lo..up + 1]: calc /* k has 50% among a[lo..up + 1] */ { true; == // negation of the previous branch condition; 2 * c <= up + 1 - lo; == // loop invariant (3) 2 * Count(a, lo, up, k) <= up + 1 - lo; == calc { true; == // loop invariant (2) HasMajority(a, lo, up, k); == // def. HasMajority 2 * Count(a, lo, up, k) > up - lo; == 2 * Count(a, lo, up, k) >= up + 1 - lo; } 2 * Count(a, lo, up, k) == up + 1 - lo; } up := up + 1; assert 2 * Count(a, lo, up, k) == up - lo; // k has exactly 50% in the current window a[lo..up] // We are going to start a new window a[up..up + 1] and choose a new candidate, // so invariants (2) and (3) will be easy to re-establish. // To re-establish (1) we have to prove that K has majority among a[up..], as up will become the new lo. // The main idea is that we had enough K's in a[lo..], and there cannot be too many in a[lo..up]. calc /* K has majority among a[up..] */ { 2 * Count(a, up, |a|, K); == { Lemma_Split(a, lo, up, |a|, K); } 2 * Count(a, lo, |a|, K) - 2 * Count(a, lo, up, K); > { assert HasMajority(a, lo, |a|, K); } // loop invariant (1) |a| - lo - 2 * Count(a, lo, up, K); >= { if k == K { calc { 2 * Count(a, lo, up, K); == 2 * Count(a, lo, up, k); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } else { calc { 2 * Count(a, lo, up, K); <= { Lemma_Unique(a, lo, up, k, K); } 2 * ((up - lo) - Count(a, lo, up, k)); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } assert 2 * Count(a, lo, up, K) <= up - lo; } |a| - lo - (up - lo); == |a| - up; } assert HasMajority(a, up, |a|, K); k, lo, up, c := a[up], up, up + 1, 1; assert HasMajority(a, lo, |a|, K); } } Lemma_Unique(a, lo, |a|, K, k); // both k and K have a majority among a[lo..], so K == k } // This version uses more calculations with boolean formulas method FindWinner''<Candidate(==)>(a: seq<Candidate>, ghost K: Candidate) returns (k: Candidate) requires HasMajority(a, 0, |a|, K) // K has a (strict) majority of the votes ensures k == K // find K { k := a[0]; // Current candidate: the first element var lo, up, c := 0, 1, 1; // Window: [0..1], number of occurrences of k in the window: 1 while up < |a| invariant 0 <= lo < up <= |a| // (0) invariant HasMajority(a, lo, |a|, K) // (1) K has majority among a[lo..] invariant HasMajority(a, lo, up, k) // (2) k has majority among a[lo..up] (in the current window) invariant c == Count(a, lo, up, k) // (3) { if a[up] == k { // One more occurrence of k up, c := up + 1, c + 1; } else if 2 * c > up + 1 - lo { // An occurrence of another value, but k still has the majority up := up + 1; } else { // An occurrence of another value and k just lost the majority. // Prove that k has exactly 50% in the future window a[lo..up + 1]: calc /* k has 50% among a[lo..up + 1] */ { true; == // negation of the previous branch condition 2 * c <= up + 1 - lo; == // loop invariant (3) 2 * Count(a, lo, up, k) <= up + 1 - lo; == calc { true; == // loop invariant (2) HasMajority(a, lo, up, k); == // def. HasMajority 2 * Count(a, lo, up, k) > up - lo; == 2 * Count(a, lo, up, k) >= up + 1 - lo; } 2 * Count(a, lo, up, k) == up + 1 - lo; } up := up + 1; assert 2 * Count(a, lo, up, k) == up - lo; // k has exactly 50% in the current window a[lo..up] // We are going to start a new window a[up..up + 1] and choose a new candidate, // so invariants (2) and (3) will be easy to re-establish. // To re-establish (1) we have to prove that K has majority among a[up..], as up will become the new lo. // The main idea is that we had enough K's in a[lo..], and there cannot be too many in a[lo..up]. calc /* K has majority among a[up..] */ { true; == // loop invariant (1) HasMajority(a, lo, |a|, K); == 2 * Count(a, lo, |a|, K) > |a| - lo; == { Lemma_Split(a, lo, up, |a|, K); } 2 * Count(a, lo, up, K) + 2 * Count(a, up, |a|, K) > |a| - lo; ==> { if k == K { calc { 2 * Count(a, lo, up, K); == 2 * Count(a, lo, up, k); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] up - lo; } } else { calc { true; == { Lemma_Unique(a, lo, up, k, K); } Count(a, lo, up, K) + Count(a, lo, up, k) <= up - lo; == 2 * Count(a, lo, up, K) + 2 * Count(a, lo, up, k) <= 2 * (up - lo); == { assert 2 * Count(a, lo, up, k) == up - lo; } // k has 50% among a[lo..up] 2 * Count(a, lo, up, K) <= up - lo; } } assert 2 * Count(a, lo, up, K) <= up - lo; } // subtract off Count(a, lo, up, K) from the LHS and subtract off the larger amount up - lo from the RHS 2 * Count(a, up, |a|, K) > (|a| - lo) - (up - lo); == 2 * Count(a, up, |a|, K) > |a| - up; == HasMajority(a, up, |a|, K); } k, lo, up, c := a[up], up, up + 1, 1; assert HasMajority(a, lo, |a|, K); } } Lemma_Unique(a, lo, |a|, K, k); // both k and K have a majority among a[lo..], so K == k }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny2_MajorityVote.dfy
314
314
Dafny program: 314
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment // This file shows an example program that uses both refinement and :autocontracts // specify a class that stores a set of things that can be retrieved using a query. // // (For another example that uses these features, see Test/dafny3/CachedContainer.dfy.) abstract module AbstractInterface { class {:autocontracts} StoreAndRetrieve<Thing(==)> { ghost var Contents: set<Thing> ghost predicate Valid() { Valid'() } ghost predicate {:autocontracts false} Valid'() reads this, Repr constructor Init() ensures Contents == {} method Store(t: Thing) ensures Contents == old(Contents) + {t} method Retrieve(matchCriterion: Thing -> bool) returns (thing: Thing) requires exists t :: t in Contents && matchCriterion(t) ensures Contents == old(Contents) ensures thing in Contents && matchCriterion(thing) } } abstract module A refines AbstractInterface { class StoreAndRetrieve<Thing(==)> ... { constructor Init... { Contents := {}; Repr := {this}; new; assume Valid'(); // to be checked in module B } method Store... { Contents := Contents + {t}; assume Valid'(); // to be checked in module B } method Retrieve... { var k :| assume k in Contents && matchCriterion(k); thing := k; } } } abstract module B refines A { class StoreAndRetrieve<Thing(==)> ... { var arr: seq<Thing> ghost predicate Valid'... { Contents == set x | x in arr } constructor Init... { arr := []; new; } method Store... { arr := arr + [t]; ...; } method Retrieve... { var i := 0; while (i < |arr|) { if matchCriterion(arr[i]) { break; } i := i + 1; } var k := arr[i]; ...; var a: seq<Thing> :| assume Contents == set x | x in a; arr := a; } } } module abC refines B { // TODO module C causes Go to fail class StoreAndRetrieve<Thing(==)> ... { method Retrieve... { ...; var a := [thing] + arr[..i] + arr[i+1..]; // LRU behavior } } } abstract module AbstractClient { import S : AbstractInterface method Test() { var s := new S.StoreAndRetrieve<real>.Init(); s.Store(20.3); var fn := r => true; var r := s.Retrieve(fn); print r, "\n"; // 20.3 } } module Client refines AbstractClient { import S = abC method Main() { Test(); } }
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment // This file shows an example program that uses both refinement and :autocontracts // specify a class that stores a set of things that can be retrieved using a query. // // (For another example that uses these features, see Test/dafny3/CachedContainer.dfy.) abstract module AbstractInterface { class {:autocontracts} StoreAndRetrieve<Thing(==)> { ghost var Contents: set<Thing> ghost predicate Valid() { Valid'() } ghost predicate {:autocontracts false} Valid'() reads this, Repr constructor Init() ensures Contents == {} method Store(t: Thing) ensures Contents == old(Contents) + {t} method Retrieve(matchCriterion: Thing -> bool) returns (thing: Thing) requires exists t :: t in Contents && matchCriterion(t) ensures Contents == old(Contents) ensures thing in Contents && matchCriterion(thing) } } abstract module A refines AbstractInterface { class StoreAndRetrieve<Thing(==)> ... { constructor Init... { Contents := {}; Repr := {this}; new; assume Valid'(); // to be checked in module B } method Store... { Contents := Contents + {t}; assume Valid'(); // to be checked in module B } method Retrieve... { var k :| assume k in Contents && matchCriterion(k); thing := k; } } } abstract module B refines A { class StoreAndRetrieve<Thing(==)> ... { var arr: seq<Thing> ghost predicate Valid'... { Contents == set x | x in arr } constructor Init... { arr := []; new; assert ...; } method Store... { arr := arr + [t]; ...; assert ...; } method Retrieve... { var i := 0; while (i < |arr|) invariant i < |arr| invariant forall j :: 0 <= j < i ==> !matchCriterion(arr[j]) { if matchCriterion(arr[i]) { break; } i := i + 1; } var k := arr[i]; ...; var a: seq<Thing> :| assume Contents == set x | x in a; arr := a; } } } module abC refines B { // TODO module C causes Go to fail class StoreAndRetrieve<Thing(==)> ... { method Retrieve... { ...; var a := [thing] + arr[..i] + arr[i+1..]; // LRU behavior } } } abstract module AbstractClient { import S : AbstractInterface method Test() { var s := new S.StoreAndRetrieve<real>.Init(); s.Store(20.3); var fn := r => true; var r := s.Retrieve(fn); print r, "\n"; // 20.3 } } module Client refines AbstractClient { import S = abC method Main() { Test(); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny2_StoreAndRetrieve.dfy
315
315
Dafny program: 315
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment // This file contains an example chain of module refinements, starting from a // simple interface M0 to an implementation M3. Module Client.Test() is // verified against the original M0 module. Module CachedClient instantiates // the abstract import of M0 with the concrete module M3, and then gets to // reuse the proof done in Client. // // At a sufficiently abstract level, the concepts used are all standard. // However, it can be tricky to set these things up in Dafny, if you want // the final program to be a composition of smaller refinement steps. // // Textually, refinement modules in Dafny are written with "...", rather // than by repeating the program text from the module being refined. // This can be difficult to both author and read, so this file can be // used as a guide for what to aim for. Undoubtedly, use of the /rprint:- // option on the command line will be useful, since it lets you see what // all the ...'s expand to. // // As a convenience, this program also uses a second experimental feature, // namely the preprocessing requested by :autocontracts, which supplies // much of the boilerplate specifications that one uses with the // dynamic-frames idiom in Dafny. This feature was designed to reduce clutter // in the program text, but can increase the mystery behind what's really // going on. Here, too, using the /rprint:- option will be useful, since // it shows the automatically generated specifications and code. // // (For another example that uses these features, see Test/dafny2/StoreAndRetrieve.dfy.) // give the method signatures and specs abstract module M0 { class {:autocontracts} Container<T(==)> { ghost var Contents: set<T> ghost predicate Valid() { Valid'() } ghost predicate {:autocontracts false} Valid'() reads this, Repr constructor () ensures Contents == {} method Add(t: T) ensures Contents == old(Contents) + {t} method Remove(t: T) ensures Contents == old(Contents) - {t} method Contains(t: T) returns (b: bool) ensures Contents == old(Contents) ensures b <==> t in Contents } } // provide bodies for the methods abstract module M1 refines M0 { class Container<T(==)> ... { constructor... { Contents := {}; Repr := {this}; new; label CheckPost: assume Valid'(); // to be checked in further refinements } method Add... { Contents := Contents + {t}; label CheckPost: assume Valid'(); // to be checked in further refinements } method Remove... { Contents := Contents - {t}; label CheckPost: assume Valid'(); // to be checked in further refinements } method Contains... { // b := t in Contents; b :| assume b <==> t in Contents; } } } // implement the set in terms of a sequence abstract module M2 refines M1 { class Container<T(==)> ... { var elems: seq<T> ghost predicate Valid'... { Contents == (set x | x in elems) && (forall i,j :: 0 <= i < j < |elems| ==> elems[i] != elems[j]) && Valid''() } ghost predicate {:autocontracts false} Valid''() reads this, Repr method FindIndex(t: T) returns (j: nat) ensures j <= |elems| ensures if j < |elems| then elems[j] == t else t !in elems { j := 0; while (j < |elems|) { if (elems[j] == t) { return; } j := j + 1; } } constructor... { elems := []; new; label CheckPost: assume Valid''(); // to be checked in further refinements } method Add... { var j := FindIndex(t); if j == |elems| { elems := elems + [t]; } ...; label CheckPost: assume Valid''(); // to be checked in further refinements } method Remove... { var j := FindIndex(t); if j < |elems| { elems := elems[..j] + elems[j+1..]; } ...; label CheckPost: assume Valid''(); // to be checked in further refinements } method Contains... { var j := FindIndex(t); b := j < |elems|; } } } // implement a cache module M3 refines M2 { datatype Cache<T> = None | Some(index: nat, value: T) class Container<T(==)> ... { var cache: Cache<T> ghost predicate Valid''... { cache.Some? ==> cache.index < |elems| && elems[cache.index] == cache.value } constructor... { cache := None; new; ...; } method FindIndex... { if cache.Some? && cache.value == t { return cache.index; } } method Add... { ...; } method Remove... { ...; if ... { if cache.Some? { if cache.index == j { // clear the cache cache := None; } else if j < cache.index { // adjust for the shifting down cache := cache.(index := cache.index - 1); } } } ...; } } } // here a client of the Container abstract module Client { import M : M0 method Test() { var c := new M.Container(); c.Add(56); c.Add(12); var b := c.Contains(17); print b, " "; // false (does not contain 17) b := c.Contains(12); print b, " "; // true (contains 12) c.Remove(12); b := c.Contains(12); print b, " "; // false (no longer contains 12) b := c.Contains(56); print b, "\n"; // true (still contains 56) } } module CachedClient refines Client { import M = M3 method Main() { Test(); } }
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment // This file contains an example chain of module refinements, starting from a // simple interface M0 to an implementation M3. Module Client.Test() is // verified against the original M0 module. Module CachedClient instantiates // the abstract import of M0 with the concrete module M3, and then gets to // reuse the proof done in Client. // // At a sufficiently abstract level, the concepts used are all standard. // However, it can be tricky to set these things up in Dafny, if you want // the final program to be a composition of smaller refinement steps. // // Textually, refinement modules in Dafny are written with "...", rather // than by repeating the program text from the module being refined. // This can be difficult to both author and read, so this file can be // used as a guide for what to aim for. Undoubtedly, use of the /rprint:- // option on the command line will be useful, since it lets you see what // all the ...'s expand to. // // As a convenience, this program also uses a second experimental feature, // namely the preprocessing requested by :autocontracts, which supplies // much of the boilerplate specifications that one uses with the // dynamic-frames idiom in Dafny. This feature was designed to reduce clutter // in the program text, but can increase the mystery behind what's really // going on. Here, too, using the /rprint:- option will be useful, since // it shows the automatically generated specifications and code. // // (For another example that uses these features, see Test/dafny2/StoreAndRetrieve.dfy.) // give the method signatures and specs abstract module M0 { class {:autocontracts} Container<T(==)> { ghost var Contents: set<T> ghost predicate Valid() { Valid'() } ghost predicate {:autocontracts false} Valid'() reads this, Repr constructor () ensures Contents == {} method Add(t: T) ensures Contents == old(Contents) + {t} method Remove(t: T) ensures Contents == old(Contents) - {t} method Contains(t: T) returns (b: bool) ensures Contents == old(Contents) ensures b <==> t in Contents } } // provide bodies for the methods abstract module M1 refines M0 { class Container<T(==)> ... { constructor... { Contents := {}; Repr := {this}; new; label CheckPost: assume Valid'(); // to be checked in further refinements } method Add... { Contents := Contents + {t}; label CheckPost: assume Valid'(); // to be checked in further refinements } method Remove... { Contents := Contents - {t}; label CheckPost: assume Valid'(); // to be checked in further refinements } method Contains... { // b := t in Contents; b :| assume b <==> t in Contents; } } } // implement the set in terms of a sequence abstract module M2 refines M1 { class Container<T(==)> ... { var elems: seq<T> ghost predicate Valid'... { Contents == (set x | x in elems) && (forall i,j :: 0 <= i < j < |elems| ==> elems[i] != elems[j]) && Valid''() } ghost predicate {:autocontracts false} Valid''() reads this, Repr method FindIndex(t: T) returns (j: nat) ensures j <= |elems| ensures if j < |elems| then elems[j] == t else t !in elems { j := 0; while (j < |elems|) invariant j <= |elems| invariant forall i :: 0 <= i < j ==> elems[i] != t { if (elems[j] == t) { return; } j := j + 1; } } constructor... { elems := []; new; label CheckPost: assume Valid''(); // to be checked in further refinements assert ...; } method Add... { var j := FindIndex(t); if j == |elems| { elems := elems + [t]; } ...; label CheckPost: assume Valid''(); // to be checked in further refinements assert ...; } method Remove... { var j := FindIndex(t); if j < |elems| { elems := elems[..j] + elems[j+1..]; } ...; label CheckPost: assume Valid''(); // to be checked in further refinements assert ...; } method Contains... { var j := FindIndex(t); b := j < |elems|; } } } // implement a cache module M3 refines M2 { datatype Cache<T> = None | Some(index: nat, value: T) class Container<T(==)> ... { var cache: Cache<T> ghost predicate Valid''... { cache.Some? ==> cache.index < |elems| && elems[cache.index] == cache.value } constructor... { cache := None; new; ...; assert ...; } method FindIndex... { if cache.Some? && cache.value == t { return cache.index; } } method Add... { ...; assert ...; } method Remove... { ...; if ... { if cache.Some? { if cache.index == j { // clear the cache cache := None; } else if j < cache.index { // adjust for the shifting down cache := cache.(index := cache.index - 1); } } } ...; assert ...; } } } // here a client of the Container abstract module Client { import M : M0 method Test() { var c := new M.Container(); c.Add(56); c.Add(12); var b := c.Contains(17); assert !b; print b, " "; // false (does not contain 17) b := c.Contains(12); assert b; print b, " "; // true (contains 12) c.Remove(12); b := c.Contains(12); assert !b; print b, " "; // false (no longer contains 12) assert c.Contents == {56}; b := c.Contains(56); assert b; print b, "\n"; // true (still contains 56) } } module CachedClient refines Client { import M = M3 method Main() { Test(); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny3_CachedContainer.dfy
316
316
Dafny program: 316
// RUN: %testDafnyForEachResolver "%s" // Here is a function "f" and three axioms (that is, unproved lemmas) about "f": ghost function f(x: int, y: int): int lemma Associativity(x: int, y: int, z: int) ensures f(x, f(y, z)) == f(f(x, y), z) lemma Monotonicity(y: int, z: int) requires y <= z ensures forall x :: f(x, y) <= f(x, z) lemma DiagonalIdentity(x: int) ensures f(x, x) == x // From these axioms, we can prove a lemma about "f": method CalculationalStyleProof(a: int, b: int, c: int, x: int) requires c <= x == f(a, b) ensures f(a, f(b, c)) <= x { calc { f(a, f(b, c)); == { Associativity(a, b, c); } f(f(a, b), c); == { assert f(a, b) == x; } f(x, c); <= { assert c <= x; Monotonicity(c, x); } f(x, x); == { DiagonalIdentity(x); } x; } } // Here's the same lemma, but with a proof written in a different style. // (An explanation of the constructs in this lemma is found below.) method DifferentStyleProof(a: int, b: int, c: int, x: int) requires A: c <= x requires B: x == f(a, b) ensures f(a, f(b, c)) <= x { Associativity(a, b, c); } reveal B; } Monotonicity(c, x); } DiagonalIdentity(x); } reveal 0, 1; } reveal 2, 3; } reveal 4, 5; } } // To understand the lemma above, here's what you need to know (and then some): // // * An ordinary "assert P;" statement instructs the verifier to verify // the boolean condition "P" and then to assume "P" from here on (that // is, in the control flow that continues from here). // // * An assert with a proof is written "assert P by { S }" where "S" is // a list of statements (typically other assertions and lemma calls). // This statement instructs the verifier to do "S" and then prove "P". // Once this is done, the verifier assumes "P" from here on, but it // "forgets" anything it learnt or was able to assume on account of // doing "S". In other words, an assertion like this is like a local // lemma--the proof "S" is used only to establish "P" and is then // forgotten, and after the statement, only "P" remains. Note, the // body of the "by" clause does "S" and then stops; that is, there are // no control paths out of the body of the "by" clause. // // * An assertion (either an ordinary assertion or an assertion with a // proof) can start with a label, as in: // // assert L: P; // // or: // // assert L: P by { S } // // This instructs the verifier to prove the assertion as described in the // previous two bullets, but then to forget about "P". In other words, the // difference between a labeled assertion and and an unlabeled assertion // is that an unlabeled assertion ends by assuming "P" whereas the labeled // assertion does not assume anything. // // * Syntactically, the label "L" in a labeled assertion is the same as in // a statement prefix "label L:", namely, "L" is either an identifier or // a (decimal) numeric literal. // // * The condition "P" proved by a labeled assertion can later be recalled // using a "reveal" statement. The "reveal" statement takes a list of // arguments, each of which can be a label occurring in a previous // assertion. // // * A precondition (or think of it as an antecedent of a lemma) is given by // a "requires" clause. Ordinarily, the precondition is assumed on entry // to the body of a method or lemma. Like an assert statement, a precondition // can also be labeled. Such a precondition is not automatically assumed on // entry to the body, but can be recalled by a "reveal" statement. // // * Fine points: Some exclusions apply. For example, labeled preconditions are // not supported for functions and cannot be used to hide/reveal conditions // while checking the well-formedness of a specification. Labeled assertions are // not supported in expression contexts. The "reveal" described is the "reveal" // statement. A labeled assertion can be revealed only at those program points // that are dominated by the assertion, that is, in places that are reached // only after definitely first having reached the assertion. // // * Fine point: The label "L" introduced by an assertion can also be used in // "old@L(E)" expressions, where "E" is an expression. However, note that // "old@L(E)" differs from "E" only in how the heap is dereferenced. That is, // "old@L" has no effect on local variables. In contrast, a labeled assertion // speaks about the values of the heap and locals at the time the assertion is // mentioned. So, even if the heap or locals mentioned in a labeled assertion // change after the assertion is mentioned, recalling the assertion condition // with a "reveal" statement always recall the condition with the heap and locals // as they were when the assert was stated. For example, suppose "P" is an // expression that mentions a local variable "x". Then, the second assertion in // // assert L: P by { ... } // x := x + 1; // ...make changes to the heap... // reveal L; // assert old@L(P); // // does not necessarily hold. The first assertion uses the initial value of the // heap and the initial value of "x". Consequently, "reveal L;" recalls the // asserted condition, with that initial heap and that initial value of "x", // despite the fact that the code changes both "x" and the heap between the // assert and the reveal. The expression "old@L(P)" essentially rolls // back to the initial heap, but it uses the current value of "x".
// RUN: %testDafnyForEachResolver "%s" // Here is a function "f" and three axioms (that is, unproved lemmas) about "f": ghost function f(x: int, y: int): int lemma Associativity(x: int, y: int, z: int) ensures f(x, f(y, z)) == f(f(x, y), z) lemma Monotonicity(y: int, z: int) requires y <= z ensures forall x :: f(x, y) <= f(x, z) lemma DiagonalIdentity(x: int) ensures f(x, x) == x // From these axioms, we can prove a lemma about "f": method CalculationalStyleProof(a: int, b: int, c: int, x: int) requires c <= x == f(a, b) ensures f(a, f(b, c)) <= x { calc { f(a, f(b, c)); == { Associativity(a, b, c); } f(f(a, b), c); == { assert f(a, b) == x; } f(x, c); <= { assert c <= x; Monotonicity(c, x); } f(x, x); == { DiagonalIdentity(x); } x; } } // Here's the same lemma, but with a proof written in a different style. // (An explanation of the constructs in this lemma is found below.) method DifferentStyleProof(a: int, b: int, c: int, x: int) requires A: c <= x requires B: x == f(a, b) ensures f(a, f(b, c)) <= x { assert 0: f(a, f(b, c)) == f(f(a, b), c) by { Associativity(a, b, c); } assert 1: f(f(a, b), c) == f(x, c) by { reveal B; } assert 2: f(x, c) <= f(x, x) by { assert c <= x by { reveal A; } Monotonicity(c, x); } assert 3: f(x, x) == x by { DiagonalIdentity(x); } assert 4: f(a, f(b, c)) == f(x, c) by { reveal 0, 1; } assert 5: f(x, c) <= x by { reveal 2, 3; } assert f(a, f(b, c)) <= x by { reveal 4, 5; } } // To understand the lemma above, here's what you need to know (and then some): // // * An ordinary "assert P;" statement instructs the verifier to verify // the boolean condition "P" and then to assume "P" from here on (that // is, in the control flow that continues from here). // // * An assert with a proof is written "assert P by { S }" where "S" is // a list of statements (typically other assertions and lemma calls). // This statement instructs the verifier to do "S" and then prove "P". // Once this is done, the verifier assumes "P" from here on, but it // "forgets" anything it learnt or was able to assume on account of // doing "S". In other words, an assertion like this is like a local // lemma--the proof "S" is used only to establish "P" and is then // forgotten, and after the statement, only "P" remains. Note, the // body of the "by" clause does "S" and then stops; that is, there are // no control paths out of the body of the "by" clause. // // * An assertion (either an ordinary assertion or an assertion with a // proof) can start with a label, as in: // // assert L: P; // // or: // // assert L: P by { S } // // This instructs the verifier to prove the assertion as described in the // previous two bullets, but then to forget about "P". In other words, the // difference between a labeled assertion and and an unlabeled assertion // is that an unlabeled assertion ends by assuming "P" whereas the labeled // assertion does not assume anything. // // * Syntactically, the label "L" in a labeled assertion is the same as in // a statement prefix "label L:", namely, "L" is either an identifier or // a (decimal) numeric literal. // // * The condition "P" proved by a labeled assertion can later be recalled // using a "reveal" statement. The "reveal" statement takes a list of // arguments, each of which can be a label occurring in a previous // assertion. // // * A precondition (or think of it as an antecedent of a lemma) is given by // a "requires" clause. Ordinarily, the precondition is assumed on entry // to the body of a method or lemma. Like an assert statement, a precondition // can also be labeled. Such a precondition is not automatically assumed on // entry to the body, but can be recalled by a "reveal" statement. // // * Fine points: Some exclusions apply. For example, labeled preconditions are // not supported for functions and cannot be used to hide/reveal conditions // while checking the well-formedness of a specification. Labeled assertions are // not supported in expression contexts. The "reveal" described is the "reveal" // statement. A labeled assertion can be revealed only at those program points // that are dominated by the assertion, that is, in places that are reached // only after definitely first having reached the assertion. // // * Fine point: The label "L" introduced by an assertion can also be used in // "old@L(E)" expressions, where "E" is an expression. However, note that // "old@L(E)" differs from "E" only in how the heap is dereferenced. That is, // "old@L" has no effect on local variables. In contrast, a labeled assertion // speaks about the values of the heap and locals at the time the assertion is // mentioned. So, even if the heap or locals mentioned in a labeled assertion // change after the assertion is mentioned, recalling the assertion condition // with a "reveal" statement always recall the condition with the heap and locals // as they were when the assert was stated. For example, suppose "P" is an // expression that mentions a local variable "x". Then, the second assertion in // // assert L: P by { ... } // x := x + 1; // ...make changes to the heap... // reveal L; // assert old@L(P); // // does not necessarily hold. The first assertion uses the initial value of the // heap and the initial value of "x". Consequently, "reveal L;" recalls the // asserted condition, with that initial heap and that initial value of "x", // despite the fact that the code changes both "x" and the heap between the // assert and the reveal. The expression "old@L(P)" essentially rolls // back to the initial heap, but it uses the current value of "x".
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny3_CalcExample.dfy
317
317
Dafny program: 317
// RUN: %dafny /compile:0 /deprecation:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" // Here is the usual definition of possibly infinite lists, along with a function Tail(s, n), which drops // n heads from s, and two lemmas that prove properties of Tail. codatatype Stream<T> = Nil | Cons(head: T, tail: Stream) ghost function Tail(s: Stream, n: nat): Stream { if n == 0 then s else var t := Tail(s, n-1); if t == Nil then t else t.tail } lemma Tail_Lemma0(s: Stream, n: nat) requires s.Cons? && Tail(s, n).Cons?; ensures Tail(s, n).tail == Tail(s.tail, n); { } lemma Tail_Lemma1(s: Stream, k: nat, n: nat) requires k <= n; ensures Tail(s, n).Cons? ==> Tail(s, k).Cons?; // Note, the contrapositive of this lemma says: Tail(s, k) == Nil ==> Tail(s, n) == Nil { if k < n && Tail(s, n).Cons? { } } lemma Tail_Lemma2(s: Stream, n: nat) requires s.Cons? && Tail(s.tail, n).Cons?; ensures Tail(s, n).Cons?; { if n != 0 { Tail_Lemma0(s, n-1); } } // Co-predicate IsNeverEndingStream(s) answers whether or not s ever contains Nil. greatest predicate IsNeverEndingStream<S>(s: Stream<S>) { match s case Nil => false case Cons(_, tail) => IsNeverEndingStream(tail) } // Here is an example of an infinite stream. ghost function AnInfiniteStream(): Stream<int> { Cons(0, AnInfiniteStream()) } greatest lemma Proposition0() ensures IsNeverEndingStream(AnInfiniteStream()); { } // Now, consider a Tree definition, where each node can have a possibly infinite number of children. datatype Tree = Node(children: Stream<Tree>) // Such a tree might have not just infinite width but also infinite height. The following predicate // holds if there is, for every path down from the root, a common bound on the height of each such path. // Note that the definition needs a co-predicate in order to say something about all of a node's children. ghost predicate HasBoundedHeight(t: Tree) { exists n :: 0 <= n && LowerThan(t.children, n) } greatest predicate LowerThan(s: Stream<Tree>, n: nat) { match s case Nil => true case Cons(t, tail) => 1 <= n && LowerThan(t.children, n-1) && LowerThan(tail, n) } // Co-predicate LowerThan(s, n) recurses on LowerThan(s.tail, n). Thus, a property of LowerThan is that // LowerThan(s, h) implies LowerThan(s', h) for any suffix s' of s. lemma LowerThan_Lemma(s: Stream<Tree>, n: nat, h: nat) ensures LowerThan(s, h) ==> LowerThan(Tail(s, n), h); { Tail_Lemma1(s, 0, n); if n == 0 || Tail(s, n) == Nil { } else { match s { case Cons(t, tail) => LowerThan_Lemma(tail, n-1, h); Tail_Lemma0(s, n-1); } } } // A tree t where every node has an infinite number of children satisfies InfiniteEverywhere(t.children). // Otherwise, IsFiniteSomewhere(t) holds. That is, IsFiniteSomewhere says that the tree has some node // with less than infinite width. Such a tree may or may not be of finite height, as we'll see in an // example below. ghost predicate IsFiniteSomewhere(t: Tree) { !InfiniteEverywhere(t.children) } greatest predicate InfiniteEverywhere(s: Stream<Tree>) { match s case Nil => false case Cons(t, tail) => InfiniteEverywhere(t.children) && InfiniteEverywhere(tail) } // Here is a tree where every node has exactly 1 child. Such a tree is finite in width (which implies // it is finite somewhere) and infinite in height (which implies there is no bound on its height). ghost function SkinnyTree(): Tree { Node(Cons(SkinnyTree(), Nil)) } lemma Proposition1() ensures IsFiniteSomewhere(SkinnyTree()) && !HasBoundedHeight(SkinnyTree()); { } // Any tree where all paths have bounded height are finite somewhere. lemma Theorem0(t: Tree) requires HasBoundedHeight(t); ensures IsFiniteSomewhere(t); { var n :| 0 <= n && LowerThan(t.children, n); /* */ var k := FindNil(t.children, n); } lemma FindNil(s: Stream<Tree>, n: nat) returns (k: nat) requires LowerThan(s, n); ensures !InfiniteEverywhere#[k as ORDINAL](s); { match s { case Nil => k := 1; case Cons(t, _) => k := FindNil(t.children, n-1); k := k + 1; } } // We defined an InfiniteEverywhere property above and negated it to get an IsFiniteSomewhere predicate. // If we had an InfiniteHeightSomewhere property, then we could negate it to obtain a predicate // HasFiniteHeightEverywhere. Consider the following definitions: ghost predicate HasFiniteHeightEverywhere_Bad(t: Tree) { !InfiniteHeightSomewhere_Bad(t.children) } greatest predicate InfiniteHeightSomewhere_Bad(s: Stream<Tree>) { match s case Nil => false case Cons(t, tail) => InfiniteHeightSomewhere_Bad(t.children) || InfiniteHeightSomewhere_Bad(tail) } // In some ways, this definition may look reasonable--a list of trees is infinite somewhere // if it is nonempty, and either the list of children of the first node satisfies the property // or the tail of the list does. However, because co-predicates are defined by greatest // fix-points, there is nothing in this definition that "forces" the list to ever get to a // node whose list of children satisfy the property. The following example shows that a // shallow, infinitely wide tree satisfies the negation of HasFiniteHeightEverywhere_Bad. ghost function ATree(): Tree { Node(ATreeChildren()) } ghost function ATreeChildren(): Stream<Tree> { Cons(Node(Nil), ATreeChildren()) } lemma Proposition2() ensures !HasFiniteHeightEverywhere_Bad(ATree()); { Proposition2_Lemma0(); Proposition2_Lemma1(ATreeChildren()); } greatest lemma Proposition2_Lemma0() ensures IsNeverEndingStream(ATreeChildren()); { } greatest lemma Proposition2_Lemma1(s: Stream<Tree>) requires IsNeverEndingStream(s); ensures InfiniteHeightSomewhere_Bad(s); { calc { InfiniteHeightSomewhere_Bad#[_k](s); InfiniteHeightSomewhere_Bad#[_k-1](s.head.children) || InfiniteHeightSomewhere_Bad#[_k-1](s.tail); <== InfiniteHeightSomewhere_Bad#[_k-1](s.tail); // induction hypothesis } } // What was missing from the InfiniteHeightSomewhere_Bad definition was the existence of a child // node that satisfies the property recursively. To address that problem, we may consider // a definition like the following: /* ghost predicate HasFiniteHeightEverywhere_Attempt(t: Tree) { !InfiniteHeightSomewhere_Attempt(t.children) } greatest predicate InfiniteHeightSomewhere_Attempt(s: Stream<Tree>) { exists n :: 0 <= n && var ch := Tail(s, n); ch.Cons? && InfiniteHeightSomewhere_Attempt(ch.head.children) } */ // However, Dafny does not allow this definition: the recursive call to InfiniteHeightSomewhere_Attempt // sits inside an unbounded existential quantifier, which means the co-predicate's connection with its prefix // predicate is not guaranteed to hold, so Dafny disallows this co-predicate definition. // We will use a different way to express the HasFiniteHeightEverywhere property. Instead of // using an existential quantifier inside the recursively defined co-predicate, we can place a "larger" // existential quantifier outside the call to the co-predicate. This existential quantifier is going to be // over the possible paths down the tree (it is "larger" in the sense that it selects a child tree at each // level down the path, not just at one level). // A path is a possibly infinite list of indices, each selecting the next child tree to navigate to. A path // is valid when it uses valid indices and does not stop at a node with children. greatest predicate ValidPath(t: Tree, p: Stream<int>) { match p case Nil => t == Node(Nil) case Cons(index, tail) => 0 <= index && var ch := Tail(t.children, index); ch.Cons? && ValidPath(ch.head, tail) } lemma ValidPath_Lemma(p: Stream<int>) ensures ValidPath(Node(Nil), p) ==> p == Nil; { if ValidPath(Node(Nil), p) { match p { case Nil => case Cons(index, tail) => // proof by contradiction var nil : Stream<Tree> := Nil; Tail_Lemma1(nil, 0, index); } } } // A tree has finite height (everywhere) if it has no valid infinite paths. ghost predicate HasFiniteHeight(t: Tree) { forall p :: ValidPath(t, p) ==> !IsNeverEndingStream(p) } // From this definition, we can prove that any tree of bounded height is also of finite height. lemma Theorem1(t: Tree) requires HasBoundedHeight(t); ensures HasFiniteHeight(t); { var n :| 0 <= n && LowerThan(t.children, n); forall p | ValidPath(t, p) { Theorem1_Lemma(t, n, p); } } lemma Theorem1_Lemma(t: Tree, n: nat, p: Stream<int>) requires LowerThan(t.children, n) && ValidPath(t, p); ensures !IsNeverEndingStream(p); { match p { case Nil => case Cons(index, tail) => var ch := Tail(t.children, index); calc { LowerThan(t.children, n); ==> { LowerThan_Lemma(t.children, index, n); } LowerThan(ch, n); ==> // def. LowerThan LowerThan(ch.head.children, n-1); ==> //{ Theorem1_Lemma(ch.head, n-1, tail); } !IsNeverEndingStream(tail); ==> // def. IsNeverEndingStream !IsNeverEndingStream(p); } } } // In fact, HasBoundedHeight is strictly strong than HasFiniteHeight, as we'll show with an example. // Define SkinnyFiniteTree(n) to be a skinny (that is, of width 1) tree of height n. ghost function SkinnyFiniteTree(n: nat): Tree ensures forall k: nat :: LowerThan(SkinnyFiniteTree(n).children, k) <==> n <= k; { if n == 0 then Node(Nil) else Node(Cons(SkinnyFiniteTree(n-1), Nil)) } // Next, we define a tree whose root has an infinite number of children, child i of which // is a SkinnyFiniteTree(i). ghost function FiniteUnboundedTree(): Tree { Node(EverLongerSkinnyTrees(0)) } ghost function EverLongerSkinnyTrees(n: nat): Stream<Tree> { Cons(SkinnyFiniteTree(n), EverLongerSkinnyTrees(n+1)) } lemma EverLongerSkinnyTrees_Lemma(k: nat, n: nat) ensures Tail(EverLongerSkinnyTrees(k), n).Cons?; ensures Tail(EverLongerSkinnyTrees(k), n).head == SkinnyFiniteTree(k+n); { if n == 0 { } else { calc { Tail(EverLongerSkinnyTrees(k), n); { EverLongerSkinnyTrees_Lemma(k, n-1); } // this ensures that .tail on the next line is well-defined Tail(EverLongerSkinnyTrees(k), n-1).tail; { Tail_Lemma0(EverLongerSkinnyTrees(k), n-1); } Tail(EverLongerSkinnyTrees(k).tail, n-1); Tail(EverLongerSkinnyTrees(k+1), n-1); } EverLongerSkinnyTrees_Lemma(k+1, n-1); } } lemma Proposition3() ensures !HasBoundedHeight(FiniteUnboundedTree()) && HasFiniteHeight(FiniteUnboundedTree()); { Proposition3a(); Proposition3b(); } lemma Proposition3a() ensures !HasBoundedHeight(FiniteUnboundedTree()); { var ch := FiniteUnboundedTree().children; forall n | 0 <= n ensures !LowerThan(ch, n); { var cn := Tail(ch, n+1); EverLongerSkinnyTrees_Lemma(0, n+1); LowerThan_Lemma(ch, n+1, n); } } lemma Proposition3b() ensures HasFiniteHeight(FiniteUnboundedTree()); { var t := FiniteUnboundedTree(); forall p | ValidPath(t, p) ensures !IsNeverEndingStream(p); { var index := p.head; var ch := Tail(t.children, index); EverLongerSkinnyTrees_Lemma(0, index); var si := SkinnyFiniteTree(index); Proposition3b_Lemma(si, index, p.tail); } } lemma Proposition3b_Lemma(t: Tree, h: nat, p: Stream<int>) requires LowerThan(t.children, h) && ValidPath(t, p) ensures !IsNeverEndingStream(p) { match p { case Nil => case Cons(index, tail) => // From the definition of ValidPath(t, p), we get the following: var ch := Tail(t.children, index); // assert ch.Cons? && ValidPath(ch.head, tail); // From the definition of LowerThan(t.children, h), we get the following: match t.children { case Nil => ValidPath_Lemma(p); case Cons(_, _) => // assert 1 <= h; LowerThan_Lemma(t.children, index, h); // assert LowerThan(ch, h); } // Putting these together, by ch.Cons? and the definition of LowerThan(ch, h), we get: // And now we can invoke the induction hypothesis: // Proposition3b_Lemma(ch.head, h-1, tail); } } // Using a stream of integers to denote a path is convenient, because it allows us to // use Tail to quickly select the next child tree. But we can also define paths in a // way that more directly follows the navigation steps required to get to the next child, // using Peano numbers instead of the built-in integers. This means that each Succ // constructor among the Peano numbers corresponds to moving "right" among the children // of a tree node. A path is valid only if it always selects a child from a list // of children; this implies we must avoid infinite "right" moves. The appropriate type // Numbers (which is really just a stream of natural numbers) is defined as a combination // two mutually recursive datatypes, one inductive and the other co-inductive. codatatype CoOption<T> = None | Some(get: T) datatype Number = Succ(Number) | Zero(CoOption<Number>) // Note that the use of an inductive datatype for Number guarantees that sequences of successive // "right" moves are finite (analogously, each Peano number is finite). Yet the use of a co-inductive // CoOption in between allows paths to go on forever. In contrast, a definition like: codatatype InfPath = Right(InfPath) | Down(InfPath) | Stop // does not guarantee the absence of infinitely long sequences of "right" moves. In other words, // InfPath also gives rise to indecisive paths--those that never select a child node. Also, // compare the definition of Number with: codatatype FinPath = Right(FinPath) | Down(FinPath) | Stop // where the type can only represent finite paths. As a final alternative to consider, had we // wanted only infinite, decisive paths, we would just drop the None constructor, forcing each // CoOption to be some Number. As it is, we want to allow both finite and infinite paths, but we // want to be able to distinguish them, so we define a co-predicate that does so: greatest predicate InfinitePath(r: CoOption<Number>) { match r case None => false case Some(num) => InfinitePath'(num) } greatest predicate InfinitePath'(num: Number) { match num case Succ(next) => InfinitePath'(next) case Zero(r) => InfinitePath(r) } // As before, a path is valid for a tree when it navigates to existing nodes and does not stop // in a node with more children. greatest predicate ValidPath_Alt(t: Tree, r: CoOption<Number>) { match r case None => t == Node(Nil) case Some(num) => ValidPath_Alt'(t.children, num) } greatest predicate ValidPath_Alt'(s: Stream<Tree>, num: Number) { match num case Succ(next) => s.Cons? && ValidPath_Alt'(s.tail, next) case Zero(r) => s.Cons? && ValidPath_Alt(s.head, r) } // Here is the alternative definition of a tree that has finite height everywhere, using the // new paths. ghost predicate HasFiniteHeight_Alt(t: Tree) { forall r :: ValidPath_Alt(t, r) ==> !InfinitePath(r) } // We will prove that this new definition is equivalent to the previous. To do that, we // first definite functions S2N and N2S to map between the path representations // Stream<int> and CoOption<Number>, and then prove some lemmas about this correspondence. ghost function S2N(p: Stream<int>): CoOption<Number> { match p case Nil => None case Cons(n, tail) => Some(S2N'(if n < 0 then 0 else n, tail)) } ghost function S2N'(n: nat, tail: Stream<int>): Number { if n <= 0 then Zero(S2N(tail)) else Succ(S2N'(n-1, tail)) } ghost function N2S(r: CoOption<Number>): Stream<int> { match r case None => Nil case Some(num) => N2S'(0, num) } ghost function N2S'(n: nat, num: Number): Stream<int> { match num case Zero(r) => Cons(n, N2S(r)) case Succ(next) => N2S'(n + 1, next) } lemma Path_Lemma0(t: Tree, p: Stream<int>) requires ValidPath(t, p); ensures ValidPath_Alt(t, S2N(p)); { if ValidPath(t, p) { Path_Lemma0'(t, p); } } greatest lemma Path_Lemma0'(t: Tree, p: Stream<int>) requires ValidPath(t, p); ensures ValidPath_Alt(t, S2N(p)); { match p { case Nil => case Cons(index, tail) => var ch := Tail(t.children, index); calc { ValidPath_Alt#[_k](t, S2N(p)); { assert S2N(p) == Some(S2N'(index, tail)); } ValidPath_Alt#[_k](t, Some(S2N'(index, tail))); // def. ValidPath_Alt# ValidPath_Alt'#[_k-1](t.children, S2N'(index, tail)); { Path_Lemma0''(t.children, index, tail); } true; } } } greatest lemma Path_Lemma0''(tChildren: Stream<Tree>, n: nat, tail: Stream<int>) requires var ch := Tail(tChildren, n); ch.Cons? && ValidPath(ch.head, tail); ensures ValidPath_Alt'(tChildren, S2N'(n, tail)); { Tail_Lemma1(tChildren, 0, n); match S2N'(n, tail) { case Succ(next) => calc { Tail(tChildren, n); { Tail_Lemma1(tChildren, n-1, n); } Tail(tChildren, n-1).tail; { Tail_Lemma0(tChildren, n-1); } Tail(tChildren.tail, n-1); } Path_Lemma0''(tChildren.tail, n-1, tail); case Zero(r) => Path_Lemma0'(tChildren.head, tail); } } lemma Path_Lemma1(t: Tree, r: CoOption<Number>) requires ValidPath_Alt(t, r); ensures ValidPath(t, N2S(r)); { if ValidPath_Alt(t, r) { Path_Lemma1'(t, r); } } greatest lemma Path_Lemma1'(t: Tree, r: CoOption<Number>) requires ValidPath_Alt(t, r); ensures ValidPath(t, N2S(r)); { match r { case None => case Some(num) => // assert N2S'(0, num).Cons?; // Path_Lemma1''(t.children, 0, num); var p := N2S'(0, num); calc { ValidPath#[_k](t, N2S(r)); ValidPath#[_k](t, N2S(Some(num))); ValidPath#[_k](t, N2S'(0, num)); { Path_Lemma1''#[_k](t.children, 0, num); } true; } } } greatest lemma Path_Lemma1''(s: Stream<Tree>, n: nat, num: Number) requires ValidPath_Alt'(Tail(s, n), num); ensures ValidPath(Node(s), N2S'(n, num)); { match num { case Succ(next) => Path_Lemma1''#[_k](s, n+1, next); case Zero(r) => calc { ValidPath#[_k](Node(s), N2S'(n, num)); ValidPath#[_k](Node(s), Cons(n, N2S(r))); Tail(s, n).Cons? && ValidPath#[_k-1](Tail(s, n).head, N2S(r)); { assert Tail(s, n).Cons?; } ValidPath#[_k-1](Tail(s, n).head, N2S(r)); { Path_Lemma1'(Tail(s, n).head, r); } true; } } } lemma Path_Lemma2(p: Stream<int>) ensures IsNeverEndingStream(p) ==> InfinitePath(S2N(p)); { if IsNeverEndingStream(p) { Path_Lemma2'(p); } } greatest lemma Path_Lemma2'(p: Stream<int>) requires IsNeverEndingStream(p); ensures InfinitePath(S2N(p)); { match p { case Cons(n, tail) => calc { InfinitePath#[_k](S2N(p)); // def. S2N InfinitePath#[_k](Some(S2N'(if n < 0 then 0 else n, tail))); // def. InfinitePath InfinitePath'#[_k-1](S2N'(if n < 0 then 0 else n, tail)); <== { Path_Lemma2''(p, if n < 0 then 0 else n, tail); } InfinitePath#[_k-1](S2N(tail)); { Path_Lemma2'(tail); } true; } } } greatest lemma Path_Lemma2''(p: Stream<int>, n: nat, tail: Stream<int>) requires IsNeverEndingStream(p) && p.tail == tail ensures InfinitePath'(S2N'(n, tail)) { Path_Lemma2'(tail); } lemma Path_Lemma3(r: CoOption<Number>) ensures InfinitePath(r) ==> IsNeverEndingStream(N2S(r)); { if InfinitePath(r) { match r { case Some(num) => Path_Lemma3'(0, num); } } } greatest lemma Path_Lemma3'(n: nat, num: Number) requires InfinitePath'(num); ensures IsNeverEndingStream(N2S'(n, num)); { match num { case Zero(r) => calc { IsNeverEndingStream#[_k](N2S'(n, num)); // def. N2S' IsNeverEndingStream#[_k](Cons(n, N2S(r))); // def. IsNeverEndingStream IsNeverEndingStream#[_k-1](N2S(r)); { Path_Lemma3'(0, r.get); } true; } case Succ(next) => Path_Lemma3'#[_k](n + 1, next); } } lemma Theorem2(t: Tree) ensures HasFiniteHeight(t) <==> HasFiniteHeight_Alt(t); { if HasFiniteHeight_Alt(t) { forall p { calc ==> { ValidPath(t, p); { Path_Lemma0(t, p); } ValidPath_Alt(t, S2N(p)); // assumption HasFiniteHeight(t) !InfinitePath(S2N(p)); { Path_Lemma2(p); } !IsNeverEndingStream(p); } } } if HasFiniteHeight(t) { forall r { calc ==> { ValidPath_Alt(t, r); { Path_Lemma1(t, r); } ValidPath(t, N2S(r)); // assumption HasFiniteHeight_Alt(t) !IsNeverEndingStream(N2S(r)); { Path_Lemma3(r); } !InfinitePath(r); } } } }
// RUN: %dafny /compile:0 /deprecation:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" // Here is the usual definition of possibly infinite lists, along with a function Tail(s, n), which drops // n heads from s, and two lemmas that prove properties of Tail. codatatype Stream<T> = Nil | Cons(head: T, tail: Stream) ghost function Tail(s: Stream, n: nat): Stream { if n == 0 then s else var t := Tail(s, n-1); if t == Nil then t else t.tail } lemma Tail_Lemma0(s: Stream, n: nat) requires s.Cons? && Tail(s, n).Cons?; ensures Tail(s, n).tail == Tail(s.tail, n); { } lemma Tail_Lemma1(s: Stream, k: nat, n: nat) requires k <= n; ensures Tail(s, n).Cons? ==> Tail(s, k).Cons?; // Note, the contrapositive of this lemma says: Tail(s, k) == Nil ==> Tail(s, n) == Nil { if k < n && Tail(s, n).Cons? { assert Tail(s, n) == Tail(s, n-1).tail; } } lemma Tail_Lemma2(s: Stream, n: nat) requires s.Cons? && Tail(s.tail, n).Cons?; ensures Tail(s, n).Cons?; { if n != 0 { Tail_Lemma0(s, n-1); } } // Co-predicate IsNeverEndingStream(s) answers whether or not s ever contains Nil. greatest predicate IsNeverEndingStream<S>(s: Stream<S>) { match s case Nil => false case Cons(_, tail) => IsNeverEndingStream(tail) } // Here is an example of an infinite stream. ghost function AnInfiniteStream(): Stream<int> { Cons(0, AnInfiniteStream()) } greatest lemma Proposition0() ensures IsNeverEndingStream(AnInfiniteStream()); { } // Now, consider a Tree definition, where each node can have a possibly infinite number of children. datatype Tree = Node(children: Stream<Tree>) // Such a tree might have not just infinite width but also infinite height. The following predicate // holds if there is, for every path down from the root, a common bound on the height of each such path. // Note that the definition needs a co-predicate in order to say something about all of a node's children. ghost predicate HasBoundedHeight(t: Tree) { exists n :: 0 <= n && LowerThan(t.children, n) } greatest predicate LowerThan(s: Stream<Tree>, n: nat) { match s case Nil => true case Cons(t, tail) => 1 <= n && LowerThan(t.children, n-1) && LowerThan(tail, n) } // Co-predicate LowerThan(s, n) recurses on LowerThan(s.tail, n). Thus, a property of LowerThan is that // LowerThan(s, h) implies LowerThan(s', h) for any suffix s' of s. lemma LowerThan_Lemma(s: Stream<Tree>, n: nat, h: nat) ensures LowerThan(s, h) ==> LowerThan(Tail(s, n), h); { Tail_Lemma1(s, 0, n); if n == 0 || Tail(s, n) == Nil { } else { match s { case Cons(t, tail) => LowerThan_Lemma(tail, n-1, h); Tail_Lemma0(s, n-1); } } } // A tree t where every node has an infinite number of children satisfies InfiniteEverywhere(t.children). // Otherwise, IsFiniteSomewhere(t) holds. That is, IsFiniteSomewhere says that the tree has some node // with less than infinite width. Such a tree may or may not be of finite height, as we'll see in an // example below. ghost predicate IsFiniteSomewhere(t: Tree) { !InfiniteEverywhere(t.children) } greatest predicate InfiniteEverywhere(s: Stream<Tree>) { match s case Nil => false case Cons(t, tail) => InfiniteEverywhere(t.children) && InfiniteEverywhere(tail) } // Here is a tree where every node has exactly 1 child. Such a tree is finite in width (which implies // it is finite somewhere) and infinite in height (which implies there is no bound on its height). ghost function SkinnyTree(): Tree { Node(Cons(SkinnyTree(), Nil)) } lemma Proposition1() ensures IsFiniteSomewhere(SkinnyTree()) && !HasBoundedHeight(SkinnyTree()); { assert forall n {:induction} :: 0 <= n ==> !LowerThan(SkinnyTree().children, n); } // Any tree where all paths have bounded height are finite somewhere. lemma Theorem0(t: Tree) requires HasBoundedHeight(t); ensures IsFiniteSomewhere(t); { var n :| 0 <= n && LowerThan(t.children, n); /* assert (forall k :: 0 <= k ==> InfiniteEverywhere#[k](t.children)) ==> InfiniteEverywhere(t.children); assert InfiniteEverywhere(t.children) ==> (forall k :: 0 <= k ==> InfiniteEverywhere#[k](t.children)); assert InfiniteEverywhere(t.children) <==> (forall k :: 0 <= k ==> InfiniteEverywhere#[k](t.children)); // TODO: why does this not follow from the previous two? */ var k := FindNil(t.children, n); } lemma FindNil(s: Stream<Tree>, n: nat) returns (k: nat) requires LowerThan(s, n); ensures !InfiniteEverywhere#[k as ORDINAL](s); { match s { case Nil => k := 1; case Cons(t, _) => k := FindNil(t.children, n-1); k := k + 1; } } // We defined an InfiniteEverywhere property above and negated it to get an IsFiniteSomewhere predicate. // If we had an InfiniteHeightSomewhere property, then we could negate it to obtain a predicate // HasFiniteHeightEverywhere. Consider the following definitions: ghost predicate HasFiniteHeightEverywhere_Bad(t: Tree) { !InfiniteHeightSomewhere_Bad(t.children) } greatest predicate InfiniteHeightSomewhere_Bad(s: Stream<Tree>) { match s case Nil => false case Cons(t, tail) => InfiniteHeightSomewhere_Bad(t.children) || InfiniteHeightSomewhere_Bad(tail) } // In some ways, this definition may look reasonable--a list of trees is infinite somewhere // if it is nonempty, and either the list of children of the first node satisfies the property // or the tail of the list does. However, because co-predicates are defined by greatest // fix-points, there is nothing in this definition that "forces" the list to ever get to a // node whose list of children satisfy the property. The following example shows that a // shallow, infinitely wide tree satisfies the negation of HasFiniteHeightEverywhere_Bad. ghost function ATree(): Tree { Node(ATreeChildren()) } ghost function ATreeChildren(): Stream<Tree> { Cons(Node(Nil), ATreeChildren()) } lemma Proposition2() ensures !HasFiniteHeightEverywhere_Bad(ATree()); { Proposition2_Lemma0(); Proposition2_Lemma1(ATreeChildren()); } greatest lemma Proposition2_Lemma0() ensures IsNeverEndingStream(ATreeChildren()); { } greatest lemma Proposition2_Lemma1(s: Stream<Tree>) requires IsNeverEndingStream(s); ensures InfiniteHeightSomewhere_Bad(s); { calc { InfiniteHeightSomewhere_Bad#[_k](s); InfiniteHeightSomewhere_Bad#[_k-1](s.head.children) || InfiniteHeightSomewhere_Bad#[_k-1](s.tail); <== InfiniteHeightSomewhere_Bad#[_k-1](s.tail); // induction hypothesis } } // What was missing from the InfiniteHeightSomewhere_Bad definition was the existence of a child // node that satisfies the property recursively. To address that problem, we may consider // a definition like the following: /* ghost predicate HasFiniteHeightEverywhere_Attempt(t: Tree) { !InfiniteHeightSomewhere_Attempt(t.children) } greatest predicate InfiniteHeightSomewhere_Attempt(s: Stream<Tree>) { exists n :: 0 <= n && var ch := Tail(s, n); ch.Cons? && InfiniteHeightSomewhere_Attempt(ch.head.children) } */ // However, Dafny does not allow this definition: the recursive call to InfiniteHeightSomewhere_Attempt // sits inside an unbounded existential quantifier, which means the co-predicate's connection with its prefix // predicate is not guaranteed to hold, so Dafny disallows this co-predicate definition. // We will use a different way to express the HasFiniteHeightEverywhere property. Instead of // using an existential quantifier inside the recursively defined co-predicate, we can place a "larger" // existential quantifier outside the call to the co-predicate. This existential quantifier is going to be // over the possible paths down the tree (it is "larger" in the sense that it selects a child tree at each // level down the path, not just at one level). // A path is a possibly infinite list of indices, each selecting the next child tree to navigate to. A path // is valid when it uses valid indices and does not stop at a node with children. greatest predicate ValidPath(t: Tree, p: Stream<int>) { match p case Nil => t == Node(Nil) case Cons(index, tail) => 0 <= index && var ch := Tail(t.children, index); ch.Cons? && ValidPath(ch.head, tail) } lemma ValidPath_Lemma(p: Stream<int>) ensures ValidPath(Node(Nil), p) ==> p == Nil; { if ValidPath(Node(Nil), p) { match p { case Nil => case Cons(index, tail) => // proof by contradiction var nil : Stream<Tree> := Nil; Tail_Lemma1(nil, 0, index); } } } // A tree has finite height (everywhere) if it has no valid infinite paths. ghost predicate HasFiniteHeight(t: Tree) { forall p :: ValidPath(t, p) ==> !IsNeverEndingStream(p) } // From this definition, we can prove that any tree of bounded height is also of finite height. lemma Theorem1(t: Tree) requires HasBoundedHeight(t); ensures HasFiniteHeight(t); { var n :| 0 <= n && LowerThan(t.children, n); forall p | ValidPath(t, p) { Theorem1_Lemma(t, n, p); } } lemma Theorem1_Lemma(t: Tree, n: nat, p: Stream<int>) requires LowerThan(t.children, n) && ValidPath(t, p); ensures !IsNeverEndingStream(p); decreases n; { match p { case Nil => case Cons(index, tail) => var ch := Tail(t.children, index); calc { LowerThan(t.children, n); ==> { LowerThan_Lemma(t.children, index, n); } LowerThan(ch, n); ==> // def. LowerThan LowerThan(ch.head.children, n-1); ==> //{ Theorem1_Lemma(ch.head, n-1, tail); } !IsNeverEndingStream(tail); ==> // def. IsNeverEndingStream !IsNeverEndingStream(p); } } } // In fact, HasBoundedHeight is strictly strong than HasFiniteHeight, as we'll show with an example. // Define SkinnyFiniteTree(n) to be a skinny (that is, of width 1) tree of height n. ghost function SkinnyFiniteTree(n: nat): Tree ensures forall k: nat :: LowerThan(SkinnyFiniteTree(n).children, k) <==> n <= k; { if n == 0 then Node(Nil) else Node(Cons(SkinnyFiniteTree(n-1), Nil)) } // Next, we define a tree whose root has an infinite number of children, child i of which // is a SkinnyFiniteTree(i). ghost function FiniteUnboundedTree(): Tree { Node(EverLongerSkinnyTrees(0)) } ghost function EverLongerSkinnyTrees(n: nat): Stream<Tree> { Cons(SkinnyFiniteTree(n), EverLongerSkinnyTrees(n+1)) } lemma EverLongerSkinnyTrees_Lemma(k: nat, n: nat) ensures Tail(EverLongerSkinnyTrees(k), n).Cons?; ensures Tail(EverLongerSkinnyTrees(k), n).head == SkinnyFiniteTree(k+n); decreases n; { if n == 0 { } else { calc { Tail(EverLongerSkinnyTrees(k), n); { EverLongerSkinnyTrees_Lemma(k, n-1); } // this ensures that .tail on the next line is well-defined Tail(EverLongerSkinnyTrees(k), n-1).tail; { Tail_Lemma0(EverLongerSkinnyTrees(k), n-1); } Tail(EverLongerSkinnyTrees(k).tail, n-1); Tail(EverLongerSkinnyTrees(k+1), n-1); } EverLongerSkinnyTrees_Lemma(k+1, n-1); } } lemma Proposition3() ensures !HasBoundedHeight(FiniteUnboundedTree()) && HasFiniteHeight(FiniteUnboundedTree()); { Proposition3a(); Proposition3b(); } lemma Proposition3a() ensures !HasBoundedHeight(FiniteUnboundedTree()); { var ch := FiniteUnboundedTree().children; forall n | 0 <= n ensures !LowerThan(ch, n); { var cn := Tail(ch, n+1); EverLongerSkinnyTrees_Lemma(0, n+1); assert cn.head == SkinnyFiniteTree(n+1); assert !LowerThan(cn.head.children, n); LowerThan_Lemma(ch, n+1, n); } } lemma Proposition3b() ensures HasFiniteHeight(FiniteUnboundedTree()); { var t := FiniteUnboundedTree(); forall p | ValidPath(t, p) ensures !IsNeverEndingStream(p); { assert p.Cons?; var index := p.head; assert 0 <= index; var ch := Tail(t.children, index); assert ch.Cons? && ValidPath(ch.head, p.tail); EverLongerSkinnyTrees_Lemma(0, index); assert ch.head == SkinnyFiniteTree(index); var si := SkinnyFiniteTree(index); assert LowerThan(si.children, index); Proposition3b_Lemma(si, index, p.tail); } } lemma Proposition3b_Lemma(t: Tree, h: nat, p: Stream<int>) requires LowerThan(t.children, h) && ValidPath(t, p) ensures !IsNeverEndingStream(p) decreases h { match p { case Nil => case Cons(index, tail) => // From the definition of ValidPath(t, p), we get the following: var ch := Tail(t.children, index); // assert ch.Cons? && ValidPath(ch.head, tail); // From the definition of LowerThan(t.children, h), we get the following: match t.children { case Nil => ValidPath_Lemma(p); assert false; // absurd case case Cons(_, _) => // assert 1 <= h; LowerThan_Lemma(t.children, index, h); // assert LowerThan(ch, h); } // Putting these together, by ch.Cons? and the definition of LowerThan(ch, h), we get: assert LowerThan(ch.head.children, h-1); // And now we can invoke the induction hypothesis: // Proposition3b_Lemma(ch.head, h-1, tail); } } // Using a stream of integers to denote a path is convenient, because it allows us to // use Tail to quickly select the next child tree. But we can also define paths in a // way that more directly follows the navigation steps required to get to the next child, // using Peano numbers instead of the built-in integers. This means that each Succ // constructor among the Peano numbers corresponds to moving "right" among the children // of a tree node. A path is valid only if it always selects a child from a list // of children; this implies we must avoid infinite "right" moves. The appropriate type // Numbers (which is really just a stream of natural numbers) is defined as a combination // two mutually recursive datatypes, one inductive and the other co-inductive. codatatype CoOption<T> = None | Some(get: T) datatype Number = Succ(Number) | Zero(CoOption<Number>) // Note that the use of an inductive datatype for Number guarantees that sequences of successive // "right" moves are finite (analogously, each Peano number is finite). Yet the use of a co-inductive // CoOption in between allows paths to go on forever. In contrast, a definition like: codatatype InfPath = Right(InfPath) | Down(InfPath) | Stop // does not guarantee the absence of infinitely long sequences of "right" moves. In other words, // InfPath also gives rise to indecisive paths--those that never select a child node. Also, // compare the definition of Number with: codatatype FinPath = Right(FinPath) | Down(FinPath) | Stop // where the type can only represent finite paths. As a final alternative to consider, had we // wanted only infinite, decisive paths, we would just drop the None constructor, forcing each // CoOption to be some Number. As it is, we want to allow both finite and infinite paths, but we // want to be able to distinguish them, so we define a co-predicate that does so: greatest predicate InfinitePath(r: CoOption<Number>) { match r case None => false case Some(num) => InfinitePath'(num) } greatest predicate InfinitePath'(num: Number) { match num case Succ(next) => InfinitePath'(next) case Zero(r) => InfinitePath(r) } // As before, a path is valid for a tree when it navigates to existing nodes and does not stop // in a node with more children. greatest predicate ValidPath_Alt(t: Tree, r: CoOption<Number>) { match r case None => t == Node(Nil) case Some(num) => ValidPath_Alt'(t.children, num) } greatest predicate ValidPath_Alt'(s: Stream<Tree>, num: Number) { match num case Succ(next) => s.Cons? && ValidPath_Alt'(s.tail, next) case Zero(r) => s.Cons? && ValidPath_Alt(s.head, r) } // Here is the alternative definition of a tree that has finite height everywhere, using the // new paths. ghost predicate HasFiniteHeight_Alt(t: Tree) { forall r :: ValidPath_Alt(t, r) ==> !InfinitePath(r) } // We will prove that this new definition is equivalent to the previous. To do that, we // first definite functions S2N and N2S to map between the path representations // Stream<int> and CoOption<Number>, and then prove some lemmas about this correspondence. ghost function S2N(p: Stream<int>): CoOption<Number> decreases 0; { match p case Nil => None case Cons(n, tail) => Some(S2N'(if n < 0 then 0 else n, tail)) } ghost function S2N'(n: nat, tail: Stream<int>): Number decreases n + 1; { if n <= 0 then Zero(S2N(tail)) else Succ(S2N'(n-1, tail)) } ghost function N2S(r: CoOption<Number>): Stream<int> { match r case None => Nil case Some(num) => N2S'(0, num) } ghost function N2S'(n: nat, num: Number): Stream<int> decreases num; { match num case Zero(r) => Cons(n, N2S(r)) case Succ(next) => N2S'(n + 1, next) } lemma Path_Lemma0(t: Tree, p: Stream<int>) requires ValidPath(t, p); ensures ValidPath_Alt(t, S2N(p)); { if ValidPath(t, p) { Path_Lemma0'(t, p); } } greatest lemma Path_Lemma0'(t: Tree, p: Stream<int>) requires ValidPath(t, p); ensures ValidPath_Alt(t, S2N(p)); { match p { case Nil => assert t == Node(Nil); case Cons(index, tail) => assert 0 <= index; var ch := Tail(t.children, index); assert ch.Cons? && ValidPath(ch.head, tail); calc { ValidPath_Alt#[_k](t, S2N(p)); { assert S2N(p) == Some(S2N'(index, tail)); } ValidPath_Alt#[_k](t, Some(S2N'(index, tail))); // def. ValidPath_Alt# ValidPath_Alt'#[_k-1](t.children, S2N'(index, tail)); { Path_Lemma0''(t.children, index, tail); } true; } } } greatest lemma Path_Lemma0''(tChildren: Stream<Tree>, n: nat, tail: Stream<int>) requires var ch := Tail(tChildren, n); ch.Cons? && ValidPath(ch.head, tail); ensures ValidPath_Alt'(tChildren, S2N'(n, tail)); { Tail_Lemma1(tChildren, 0, n); match S2N'(n, tail) { case Succ(next) => calc { Tail(tChildren, n); { Tail_Lemma1(tChildren, n-1, n); } Tail(tChildren, n-1).tail; { Tail_Lemma0(tChildren, n-1); } Tail(tChildren.tail, n-1); } Path_Lemma0''(tChildren.tail, n-1, tail); case Zero(r) => Path_Lemma0'(tChildren.head, tail); } } lemma Path_Lemma1(t: Tree, r: CoOption<Number>) requires ValidPath_Alt(t, r); ensures ValidPath(t, N2S(r)); { if ValidPath_Alt(t, r) { Path_Lemma1'(t, r); } } greatest lemma Path_Lemma1'(t: Tree, r: CoOption<Number>) requires ValidPath_Alt(t, r); ensures ValidPath(t, N2S(r)); decreases 1; { match r { case None => assert t == Node(Nil); assert N2S(r) == Nil; case Some(num) => assert ValidPath_Alt'(t.children, num); // assert N2S'(0, num).Cons?; // Path_Lemma1''(t.children, 0, num); var p := N2S'(0, num); calc { ValidPath#[_k](t, N2S(r)); ValidPath#[_k](t, N2S(Some(num))); ValidPath#[_k](t, N2S'(0, num)); { Path_Lemma1''#[_k](t.children, 0, num); } true; } } } greatest lemma Path_Lemma1''(s: Stream<Tree>, n: nat, num: Number) requires ValidPath_Alt'(Tail(s, n), num); ensures ValidPath(Node(s), N2S'(n, num)); decreases 0, num; { match num { case Succ(next) => Path_Lemma1''#[_k](s, n+1, next); case Zero(r) => calc { ValidPath#[_k](Node(s), N2S'(n, num)); ValidPath#[_k](Node(s), Cons(n, N2S(r))); Tail(s, n).Cons? && ValidPath#[_k-1](Tail(s, n).head, N2S(r)); { assert Tail(s, n).Cons?; } ValidPath#[_k-1](Tail(s, n).head, N2S(r)); { Path_Lemma1'(Tail(s, n).head, r); } true; } } } lemma Path_Lemma2(p: Stream<int>) ensures IsNeverEndingStream(p) ==> InfinitePath(S2N(p)); { if IsNeverEndingStream(p) { Path_Lemma2'(p); } } greatest lemma Path_Lemma2'(p: Stream<int>) requires IsNeverEndingStream(p); ensures InfinitePath(S2N(p)); { match p { case Cons(n, tail) => calc { InfinitePath#[_k](S2N(p)); // def. S2N InfinitePath#[_k](Some(S2N'(if n < 0 then 0 else n, tail))); // def. InfinitePath InfinitePath'#[_k-1](S2N'(if n < 0 then 0 else n, tail)); <== { Path_Lemma2''(p, if n < 0 then 0 else n, tail); } InfinitePath#[_k-1](S2N(tail)); { Path_Lemma2'(tail); } true; } } } greatest lemma Path_Lemma2''(p: Stream<int>, n: nat, tail: Stream<int>) requires IsNeverEndingStream(p) && p.tail == tail ensures InfinitePath'(S2N'(n, tail)) { Path_Lemma2'(tail); } lemma Path_Lemma3(r: CoOption<Number>) ensures InfinitePath(r) ==> IsNeverEndingStream(N2S(r)); { if InfinitePath(r) { match r { case Some(num) => Path_Lemma3'(0, num); } } } greatest lemma Path_Lemma3'(n: nat, num: Number) requires InfinitePath'(num); ensures IsNeverEndingStream(N2S'(n, num)); decreases num; { match num { case Zero(r) => calc { IsNeverEndingStream#[_k](N2S'(n, num)); // def. N2S' IsNeverEndingStream#[_k](Cons(n, N2S(r))); // def. IsNeverEndingStream IsNeverEndingStream#[_k-1](N2S(r)); { Path_Lemma3'(0, r.get); } true; } case Succ(next) => Path_Lemma3'#[_k](n + 1, next); } } lemma Theorem2(t: Tree) ensures HasFiniteHeight(t) <==> HasFiniteHeight_Alt(t); { if HasFiniteHeight_Alt(t) { forall p { calc ==> { ValidPath(t, p); { Path_Lemma0(t, p); } ValidPath_Alt(t, S2N(p)); // assumption HasFiniteHeight(t) !InfinitePath(S2N(p)); { Path_Lemma2(p); } !IsNeverEndingStream(p); } } } if HasFiniteHeight(t) { forall r { calc ==> { ValidPath_Alt(t, r); { Path_Lemma1(t, r); } ValidPath(t, N2S(r)); // assumption HasFiniteHeight_Alt(t) !IsNeverEndingStream(N2S(r)); { Path_Lemma3(r); } !InfinitePath(r); } } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny3_InfiniteTrees.dfy
318
318
Dafny program: 318
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment class List<T> { ghost var Contents: seq<T> ghost var Repr: set<object> var a: array<T> var n: nat ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr { this in Repr && a in Repr && n <= a.Length && Contents == a[..n] } constructor Init() ensures Valid() && fresh(Repr) ensures Contents == [] { Contents, n := [], 0; a := new T[0]; Repr := {this, a}; } method Add(t: T) requires Valid() modifies Repr ensures Valid() && fresh(Repr - old(Repr)) ensures Contents == old(Contents) + [t] { if (n == a.Length) { var b := new T[2 * a.Length + 1](i requires 0 <= i reads this, a => if i < a.Length then a[i] else t); a, Repr := b, Repr + {b}; } a[n], n, Contents := t, n + 1, Contents + [t]; } } class Cell { var data: int } iterator M<T(0)>(l: List<T>, c: Cell) yields (x: T) requires l.Valid() reads l.Repr modifies c yield requires true yield ensures xs <= l.Contents // this is needed in order for the next line to be well-formed yield ensures x == l.Contents[|xs|-1] ensures xs == l.Contents { var i := 0; while i < l.n { if (*) { assert l.Valid(); } // this property is maintained, due to the reads clause if (*) { x := l.a[i]; yield; // or, equivalently, 'yield l.a[i]' i := i + 1; } else { x, i := l.a[i], i + 1; yield; } } } method Client<T(==,0)>(l: List, stop: T) returns (s: seq<T>) requires l.Valid() { var c := new Cell; var iter := new M(l, c); s := []; while true { var more := iter.MoveNext(); if (!more) { break; } s := s + [iter.x]; if (iter.x == stop) { return; } // if we ever see 'stop', then just end } } method PrintSequence<T>(s: seq<T>) { var i := 0; while i < |s| { print s[i], " "; i := i + 1; } print "\n"; } method Main() { var myList := new List.Init(); var i := 0; while i < 100 { myList.Add(i); i := i + 2; } var s := Client(myList, 89); PrintSequence(s); s := Client(myList, 14); PrintSequence(s); }
// RUN: %testDafnyForEachCompiler --refresh-exit-code=0 "%s" -- --relax-definite-assignment class List<T> { ghost var Contents: seq<T> ghost var Repr: set<object> var a: array<T> var n: nat ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr { this in Repr && a in Repr && n <= a.Length && Contents == a[..n] } constructor Init() ensures Valid() && fresh(Repr) ensures Contents == [] { Contents, n := [], 0; a := new T[0]; Repr := {this, a}; } method Add(t: T) requires Valid() modifies Repr ensures Valid() && fresh(Repr - old(Repr)) ensures Contents == old(Contents) + [t] { if (n == a.Length) { var b := new T[2 * a.Length + 1](i requires 0 <= i reads this, a => if i < a.Length then a[i] else t); assert b[..n] == a[..n] == Contents; a, Repr := b, Repr + {b}; assert b[..n] == Contents; } a[n], n, Contents := t, n + 1, Contents + [t]; } } class Cell { var data: int } iterator M<T(0)>(l: List<T>, c: Cell) yields (x: T) requires l.Valid() reads l.Repr modifies c yield requires true yield ensures xs <= l.Contents // this is needed in order for the next line to be well-formed yield ensures x == l.Contents[|xs|-1] ensures xs == l.Contents { var i := 0; while i < l.n invariant i <= l.n && i == |xs| && xs <= l.Contents { if (*) { assert l.Valid(); } // this property is maintained, due to the reads clause if (*) { x := l.a[i]; yield; // or, equivalently, 'yield l.a[i]' i := i + 1; } else { x, i := l.a[i], i + 1; yield; } } } method Client<T(==,0)>(l: List, stop: T) returns (s: seq<T>) requires l.Valid() { var c := new Cell; var iter := new M(l, c); s := []; while true invariant iter.Valid() && fresh(iter._new) invariant iter.xs <= l.Contents decreases |l.Contents| - |iter.xs| { var more := iter.MoveNext(); if (!more) { break; } s := s + [iter.x]; if (iter.x == stop) { return; } // if we ever see 'stop', then just end } } method PrintSequence<T>(s: seq<T>) { var i := 0; while i < |s| { print s[i], " "; i := i + 1; } print "\n"; } method Main() { var myList := new List.Init(); var i := 0; while i < 100 invariant myList.Valid() && fresh(myList.Repr) { myList.Add(i); i := i + 2; } var s := Client(myList, 89); PrintSequence(s); s := Client(myList, 14); PrintSequence(s); }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny3_Iter.dfy
319
319
Dafny program: 319
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false // ----- Stream codatatype Stream<T> = Nil | Cons(head: T, tail: Stream) ghost function append(M: Stream, N: Stream): Stream { match M case Nil => N case Cons(t, M') => Cons(t, append(M', N)) } // ----- f, g, and maps type X ghost function f(x: X): X ghost function g(x: X): X ghost function map_f(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(f(x), map_f(N)) } ghost function map_g(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(g(x), map_g(N)) } ghost function map_fg(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(f(g(x)), map_fg(N)) } // ----- Theorems // map (f * g) M = map f (map g M) greatest lemma Theorem0(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { match (M) { case Nil => case Cons(x, N) => Theorem0(N); } } greatest lemma Theorem0_Alt(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { if (M.Cons?) { Theorem0_Alt(M.tail); } } lemma Theorem0_Par(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { forall k: nat { Theorem0_Ind(k, M); } } lemma Theorem0_Ind(k: nat, M: Stream<X>) ensures map_fg(M) ==#[k] map_f(map_g(M)); { if (k != 0) { match (M) { case Nil => case Cons(x, N) => Theorem0_Ind(k-1, N); } } } lemma Theorem0_AutoInd(k: nat, M: Stream<X>) ensures map_fg(M) ==#[k] map_f(map_g(M)); { } // map f (append M N) = append (map f M) (map f N) greatest lemma Theorem1(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { match (M) { case Nil => case Cons(x, M') => Theorem1(M', N); } } greatest lemma Theorem1_Alt(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { if (M.Cons?) { Theorem1_Alt(M.tail, N); } } lemma Theorem1_Par(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { forall k: nat { Theorem1_Ind(k, M, N); } } lemma Theorem1_Ind(k: nat, M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); { // this time, try doing the 'if' inside the 'match' (instead of the other way around) match (M) { case Nil => case Cons(x, M') => if (k != 0) { Theorem1_Ind(k-1, M', N); } } } lemma Theorem1_AutoInd(k: nat, M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); { } lemma Theorem1_AutoForall() { // assert forall k: nat, M, N :: map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); // TODO: this is not working yet, apparently } // append NIL M = M lemma Theorem2(M: Stream<X>) ensures append(Nil, M) == M; { // trivial } // append M NIL = M greatest lemma Theorem3(M: Stream<X>) ensures append(M, Nil) == M; { match (M) { case Nil => case Cons(x, N) => Theorem3(N); } } greatest lemma Theorem3_Alt(M: Stream<X>) ensures append(M, Nil) == M; { if (M.Cons?) { Theorem3_Alt(M.tail); } } // append M (append N P) = append (append M N) P greatest lemma Theorem4(M: Stream<X>, N: Stream<X>, P: Stream<X>) ensures append(M, append(N, P)) == append(append(M, N), P); { match (M) { case Nil => case Cons(x, M') => Theorem4(M', N, P); } } greatest lemma Theorem4_Alt(M: Stream<X>, N: Stream<X>, P: Stream<X>) ensures append(M, append(N, P)) == append(append(M, N), P); { if (M.Cons?) { Theorem4_Alt(M.tail, N, P); } } // ----- Flatten // Flatten can't be written as just: // // function SimpleFlatten(M: Stream<Stream>): Stream // { // match M // case Nil => Nil // case Cons(s, N) => append(s, SimpleFlatten(N)) // } // // because this function fails to be productive given an infinite stream of Nil's. // Instead, here are two variations of SimpleFlatten. The first variation (FlattenStartMarker) // prepends a "startMarker" to each of the streams in "M". The other (FlattenNonEmpties) // insists that "M" contain no empty streams. One can prove a theorem that relates these // two versions. // This first variation of Flatten returns a stream of the streams in M, each preceded with // "startMarker". ghost function FlattenStartMarker<T>(M: Stream<Stream>, startMarker: T): Stream { PrependThenFlattenStartMarker(Nil, M, startMarker) } ghost function PrependThenFlattenStartMarker<T>(prefix: Stream, M: Stream<Stream>, startMarker: T): Stream { match prefix case Cons(hd, tl) => Cons(hd, PrependThenFlattenStartMarker(tl, M, startMarker)) case Nil => match M case Nil => Nil case Cons(s, N) => Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) } // The next variation of Flatten requires M to contain no empty streams. greatest predicate StreamOfNonEmpties(M: Stream<Stream>) { match M case Nil => true case Cons(s, N) => s.Cons? && StreamOfNonEmpties(N) } ghost function FlattenNonEmpties(M: Stream<Stream>): Stream requires StreamOfNonEmpties(M); { PrependThenFlattenNonEmpties(Nil, M) } ghost function PrependThenFlattenNonEmpties(prefix: Stream, M: Stream<Stream>): Stream requires StreamOfNonEmpties(M); { match prefix case Cons(hd, tl) => Cons(hd, PrependThenFlattenNonEmpties(tl, M)) case Nil => match M case Nil => Nil case Cons(s, N) => Cons(s.head, PrependThenFlattenNonEmpties(s.tail, N)) } // We can prove a theorem that links the previous two variations of flatten. To // do that, we first define a function that prepends an element to each stream // of a given stream of streams. ghost function Prepend<T>(x: T, M: Stream<Stream>): Stream<Stream> { match M case Nil => Nil case Cons(s, N) => Cons(Cons(x, s), Prepend(x, N)) } greatest lemma Prepend_Lemma<T>(x: T, M: Stream<Stream>) ensures StreamOfNonEmpties(Prepend(x, M)); { match M { case Nil => case Cons(s, N) => Prepend_Lemma(x, N); } } lemma Theorem_Flatten<T>(M: Stream<Stream>, startMarker: T) ensures StreamOfNonEmpties(Prepend(startMarker, M)) ==> // always holds, on account of Prepend_Lemma; // but until (co-)method can be called from functions, // this condition is used as an antecedent here FlattenStartMarker(M, startMarker) == FlattenNonEmpties(Prepend(startMarker, M)); { Prepend_Lemma(startMarker, M); Lemma_Flatten(Nil, M, startMarker); } greatest lemma Lemma_Flatten<T>(prefix: Stream, M: Stream<Stream>, startMarker: T) ensures StreamOfNonEmpties(Prepend(startMarker, M)) ==> // always holds, on account of Prepend_Lemma; // but until (co-)method can be called from functions, // this condition is used as an antecedent here PrependThenFlattenStartMarker(prefix, M, startMarker) == PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { Prepend_Lemma(startMarker, M); match (prefix) { case Cons(hd, tl) => Lemma_Flatten(tl, M, startMarker); case Nil => match (M) { case Nil => case Cons(s, N) => if (*) { // This is all that's needed for the proof Lemma_Flatten(s, N, startMarker); } else { // ...but here are some calculations that try to show more of what's going on // (It would be nice to have ==#[...] available as an operator in calculations.) // massage the LHS: calc { PrependThenFlattenStartMarker(prefix, M, startMarker); == // def. PrependThenFlattenStartMarker Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)); } // massage the RHS: calc { PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); == // M == Cons(s, N) PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, Cons(s, N))); == // def. Prepend PrependThenFlattenNonEmpties(prefix, Cons(Cons(startMarker, s), Prepend(startMarker, N))); == // def. PrependThenFlattenNonEmpties Cons(Cons(startMarker, s).head, PrependThenFlattenNonEmpties(Cons(startMarker, s).tail, Prepend(startMarker, N))); == // Cons, head, tail Cons(startMarker, PrependThenFlattenNonEmpties(s, Prepend(startMarker, N))); } // all together now: calc { PrependThenFlattenStartMarker(prefix, M, startMarker) ==#[_k] PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { // by the calculation above, we have: Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) ==#[_k] PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { // and by the other calculation above, we have: Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) ==#[_k] Cons(startMarker, PrependThenFlattenNonEmpties(s, Prepend(startMarker, N))); == // def. of ==#[_k] for _k != 0 startMarker == startMarker && PrependThenFlattenStartMarker(s, N, startMarker) ==#[_k-1] PrependThenFlattenNonEmpties(s, Prepend(startMarker, N)); { Lemma_Flatten(s, N, startMarker); // the postcondition of the call we just made (which invokes the co-induction hypothesis) is: } true; } } } } } greatest lemma Lemma_FlattenAppend0<T>(s: Stream, M: Stream<Stream>, startMarker: T) ensures PrependThenFlattenStartMarker(s, M, startMarker) == append(s, PrependThenFlattenStartMarker(Nil, M, startMarker)); { match (s) { case Nil => case Cons(hd, tl) => Lemma_FlattenAppend0(tl, M, startMarker); } } greatest lemma Lemma_FlattenAppend1<T>(s: Stream, M: Stream<Stream>) requires StreamOfNonEmpties(M); ensures PrependThenFlattenNonEmpties(s, M) == append(s, PrependThenFlattenNonEmpties(Nil, M)); { match (s) { case Nil => case Cons(hd, tl) => Lemma_FlattenAppend1(tl, M); } }
// RUN: %testDafnyForEachResolver "%s" -- --warn-deprecation:false // ----- Stream codatatype Stream<T> = Nil | Cons(head: T, tail: Stream) ghost function append(M: Stream, N: Stream): Stream { match M case Nil => N case Cons(t, M') => Cons(t, append(M', N)) } // ----- f, g, and maps type X ghost function f(x: X): X ghost function g(x: X): X ghost function map_f(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(f(x), map_f(N)) } ghost function map_g(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(g(x), map_g(N)) } ghost function map_fg(M: Stream<X>): Stream<X> { match M case Nil => Nil case Cons(x, N) => Cons(f(g(x)), map_fg(N)) } // ----- Theorems // map (f * g) M = map f (map g M) greatest lemma Theorem0(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { match (M) { case Nil => case Cons(x, N) => Theorem0(N); } } greatest lemma Theorem0_Alt(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { if (M.Cons?) { Theorem0_Alt(M.tail); } } lemma Theorem0_Par(M: Stream<X>) ensures map_fg(M) == map_f(map_g(M)); { forall k: nat { Theorem0_Ind(k, M); } } lemma Theorem0_Ind(k: nat, M: Stream<X>) ensures map_fg(M) ==#[k] map_f(map_g(M)); { if (k != 0) { match (M) { case Nil => case Cons(x, N) => Theorem0_Ind(k-1, N); } } } lemma Theorem0_AutoInd(k: nat, M: Stream<X>) ensures map_fg(M) ==#[k] map_f(map_g(M)); { } // map f (append M N) = append (map f M) (map f N) greatest lemma Theorem1(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { match (M) { case Nil => case Cons(x, M') => Theorem1(M', N); } } greatest lemma Theorem1_Alt(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { if (M.Cons?) { Theorem1_Alt(M.tail, N); } } lemma Theorem1_Par(M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) == append(map_f(M), map_f(N)); { forall k: nat { Theorem1_Ind(k, M, N); } } lemma Theorem1_Ind(k: nat, M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); { // this time, try doing the 'if' inside the 'match' (instead of the other way around) match (M) { case Nil => case Cons(x, M') => if (k != 0) { Theorem1_Ind(k-1, M', N); } } } lemma Theorem1_AutoInd(k: nat, M: Stream<X>, N: Stream<X>) ensures map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); { } lemma Theorem1_AutoForall() { // assert forall k: nat, M, N :: map_f(append(M, N)) ==#[k] append(map_f(M), map_f(N)); // TODO: this is not working yet, apparently } // append NIL M = M lemma Theorem2(M: Stream<X>) ensures append(Nil, M) == M; { // trivial } // append M NIL = M greatest lemma Theorem3(M: Stream<X>) ensures append(M, Nil) == M; { match (M) { case Nil => case Cons(x, N) => Theorem3(N); } } greatest lemma Theorem3_Alt(M: Stream<X>) ensures append(M, Nil) == M; { if (M.Cons?) { Theorem3_Alt(M.tail); } } // append M (append N P) = append (append M N) P greatest lemma Theorem4(M: Stream<X>, N: Stream<X>, P: Stream<X>) ensures append(M, append(N, P)) == append(append(M, N), P); { match (M) { case Nil => case Cons(x, M') => Theorem4(M', N, P); } } greatest lemma Theorem4_Alt(M: Stream<X>, N: Stream<X>, P: Stream<X>) ensures append(M, append(N, P)) == append(append(M, N), P); { if (M.Cons?) { Theorem4_Alt(M.tail, N, P); } } // ----- Flatten // Flatten can't be written as just: // // function SimpleFlatten(M: Stream<Stream>): Stream // { // match M // case Nil => Nil // case Cons(s, N) => append(s, SimpleFlatten(N)) // } // // because this function fails to be productive given an infinite stream of Nil's. // Instead, here are two variations of SimpleFlatten. The first variation (FlattenStartMarker) // prepends a "startMarker" to each of the streams in "M". The other (FlattenNonEmpties) // insists that "M" contain no empty streams. One can prove a theorem that relates these // two versions. // This first variation of Flatten returns a stream of the streams in M, each preceded with // "startMarker". ghost function FlattenStartMarker<T>(M: Stream<Stream>, startMarker: T): Stream { PrependThenFlattenStartMarker(Nil, M, startMarker) } ghost function PrependThenFlattenStartMarker<T>(prefix: Stream, M: Stream<Stream>, startMarker: T): Stream { match prefix case Cons(hd, tl) => Cons(hd, PrependThenFlattenStartMarker(tl, M, startMarker)) case Nil => match M case Nil => Nil case Cons(s, N) => Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) } // The next variation of Flatten requires M to contain no empty streams. greatest predicate StreamOfNonEmpties(M: Stream<Stream>) { match M case Nil => true case Cons(s, N) => s.Cons? && StreamOfNonEmpties(N) } ghost function FlattenNonEmpties(M: Stream<Stream>): Stream requires StreamOfNonEmpties(M); { PrependThenFlattenNonEmpties(Nil, M) } ghost function PrependThenFlattenNonEmpties(prefix: Stream, M: Stream<Stream>): Stream requires StreamOfNonEmpties(M); { match prefix case Cons(hd, tl) => Cons(hd, PrependThenFlattenNonEmpties(tl, M)) case Nil => match M case Nil => Nil case Cons(s, N) => Cons(s.head, PrependThenFlattenNonEmpties(s.tail, N)) } // We can prove a theorem that links the previous two variations of flatten. To // do that, we first define a function that prepends an element to each stream // of a given stream of streams. ghost function Prepend<T>(x: T, M: Stream<Stream>): Stream<Stream> { match M case Nil => Nil case Cons(s, N) => Cons(Cons(x, s), Prepend(x, N)) } greatest lemma Prepend_Lemma<T>(x: T, M: Stream<Stream>) ensures StreamOfNonEmpties(Prepend(x, M)); { match M { case Nil => case Cons(s, N) => Prepend_Lemma(x, N); } } lemma Theorem_Flatten<T>(M: Stream<Stream>, startMarker: T) ensures StreamOfNonEmpties(Prepend(startMarker, M)) ==> // always holds, on account of Prepend_Lemma; // but until (co-)method can be called from functions, // this condition is used as an antecedent here FlattenStartMarker(M, startMarker) == FlattenNonEmpties(Prepend(startMarker, M)); { Prepend_Lemma(startMarker, M); Lemma_Flatten(Nil, M, startMarker); } greatest lemma Lemma_Flatten<T>(prefix: Stream, M: Stream<Stream>, startMarker: T) ensures StreamOfNonEmpties(Prepend(startMarker, M)) ==> // always holds, on account of Prepend_Lemma; // but until (co-)method can be called from functions, // this condition is used as an antecedent here PrependThenFlattenStartMarker(prefix, M, startMarker) == PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { Prepend_Lemma(startMarker, M); match (prefix) { case Cons(hd, tl) => Lemma_Flatten(tl, M, startMarker); case Nil => match (M) { case Nil => case Cons(s, N) => if (*) { // This is all that's needed for the proof Lemma_Flatten(s, N, startMarker); } else { // ...but here are some calculations that try to show more of what's going on // (It would be nice to have ==#[...] available as an operator in calculations.) // massage the LHS: calc { PrependThenFlattenStartMarker(prefix, M, startMarker); == // def. PrependThenFlattenStartMarker Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)); } // massage the RHS: calc { PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); == // M == Cons(s, N) PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, Cons(s, N))); == // def. Prepend PrependThenFlattenNonEmpties(prefix, Cons(Cons(startMarker, s), Prepend(startMarker, N))); == // def. PrependThenFlattenNonEmpties Cons(Cons(startMarker, s).head, PrependThenFlattenNonEmpties(Cons(startMarker, s).tail, Prepend(startMarker, N))); == // Cons, head, tail Cons(startMarker, PrependThenFlattenNonEmpties(s, Prepend(startMarker, N))); } // all together now: calc { PrependThenFlattenStartMarker(prefix, M, startMarker) ==#[_k] PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { // by the calculation above, we have: assert PrependThenFlattenStartMarker(prefix, M, startMarker) == Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)); } Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) ==#[_k] PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)); { // and by the other calculation above, we have: assert PrependThenFlattenNonEmpties(prefix, Prepend(startMarker, M)) == Cons(startMarker, PrependThenFlattenNonEmpties(s, Prepend(startMarker, N))); } Cons(startMarker, PrependThenFlattenStartMarker(s, N, startMarker)) ==#[_k] Cons(startMarker, PrependThenFlattenNonEmpties(s, Prepend(startMarker, N))); == // def. of ==#[_k] for _k != 0 startMarker == startMarker && PrependThenFlattenStartMarker(s, N, startMarker) ==#[_k-1] PrependThenFlattenNonEmpties(s, Prepend(startMarker, N)); { Lemma_Flatten(s, N, startMarker); // the postcondition of the call we just made (which invokes the co-induction hypothesis) is: assert PrependThenFlattenStartMarker(s, N, startMarker) ==#[_k-1] PrependThenFlattenNonEmpties(s, Prepend(startMarker, N)); } true; } } } } } greatest lemma Lemma_FlattenAppend0<T>(s: Stream, M: Stream<Stream>, startMarker: T) ensures PrependThenFlattenStartMarker(s, M, startMarker) == append(s, PrependThenFlattenStartMarker(Nil, M, startMarker)); { match (s) { case Nil => case Cons(hd, tl) => Lemma_FlattenAppend0(tl, M, startMarker); } } greatest lemma Lemma_FlattenAppend1<T>(s: Stream, M: Stream<Stream>) requires StreamOfNonEmpties(M); ensures PrependThenFlattenNonEmpties(s, M) == append(s, PrependThenFlattenNonEmpties(Nil, M)); { match (s) { case Nil => case Cons(hd, tl) => Lemma_FlattenAppend1(tl, M); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny3_Streams.dfy
320
320
Dafny program: 320
// RUN: %dafny /compile:0 /deprecation:0 /proverOpt:O:smt.qi.eager_threshold=30 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" // This is the Extractor Problem from section 11.8 of the ACL2 book, // "Computer-Aided Reasoning: An Approach" by Kaufmann, Manolios, and // Moore (2011 edition). datatype List<T> = Nil | Cons(head: T, tail: List) ghost function length(xs: List): nat { match xs case Nil => 0 case Cons(_, rest) => 1 + length(rest) } // If "0 <= n < length(xs)", then return the element of "xs" that is preceded by // "n" elements; otherwise, return an arbitrary value. ghost opaque function nth<T(00)>(n: int, xs: List<T>): T { if 0 <= n < length(xs) then nthWorker(n, xs) else var t :| true; t } ghost function nthWorker<T>(n: int, xs: List<T>): T requires 0 <= n < length(xs); { if n == 0 then xs.head else nthWorker(n-1, xs.tail) } ghost function append(xs: List, ys: List): List { match xs case Nil => ys case Cons(x, rest) => Cons(x, append(rest, ys)) } ghost function rev(xs: List): List { match xs case Nil => Nil case Cons(x, rest) => append(rev(rest), Cons(x, Nil)) } ghost function nats(n: nat): List<int> { if n == 0 then Nil else Cons(n-1, nats(n-1)) } ghost function xtr<T(00)>(mp: List<int>, lst: List): List { match mp case Nil => Nil case Cons(n, rest) => Cons(nth(n, lst), xtr(rest, lst)) } lemma ExtractorTheorem<T(00)>(xs: List) ensures xtr(nats(length(xs)), xs) == rev(xs); { var a, b := xtr(nats(length(xs)), xs), rev(xs); calc { length(a); { XtrLength(nats(length(xs)), xs); } length(nats(length(xs))); { NatsLength(length(xs)); } length(xs); } calc { length(xs); { RevLength(xs); } length(b); } forall i | 0 <= i < length(xs) ensures nth(i, a) == nth(i, b); { reveal nth(); ExtractorLemma(i, xs); } EqualElementsMakeEqualLists(a, b); } // auxiliary lemmas and proofs follow // lemmas about length lemma XtrLength(mp: List<int>, lst: List) ensures length(xtr(mp, lst)) == length(mp); { } lemma NatsLength(n: nat) ensures length(nats(n)) == n; { } lemma AppendLength(xs: List, ys: List) ensures length(append(xs, ys)) == length(xs) + length(ys); { } lemma RevLength(xs: List) ensures length(rev(xs)) == length(xs); { match xs { case Nil => case Cons(x, rest) => calc { length(append(rev(rest), Cons(x, Nil))); { AppendLength(rev(rest), Cons(x, Nil)); } length(rev(rest)) + length(Cons(x, Nil)); length(rev(rest)) + 1; } } } // you can prove two lists equal by proving their elements equal lemma EqualElementsMakeEqualLists<T(00)>(xs: List, ys: List) requires length(xs) == length(ys) requires forall i :: 0 <= i < length(xs) ==> nth(i, xs) == nth(i, ys) ensures xs == ys { reveal nth(); match xs { case Nil => case Cons(x, rest) => forall i | 0 <= i < length(xs.tail) { calc { nth(i, xs.tail) == nth(i, ys.tail); nth(i+1, Cons(xs.head, xs.tail)) == nth(i+1, Cons(ys.head, ys.tail)); nth(i+1, xs) == nth(i+1, ys); } } // EqualElementsMakeEqualLists(xs.tail, ys.tail); } } // here is the theorem, but applied to the ith element lemma {:vcs_split_on_every_assert} ExtractorLemma<T(00)>(i: int, xs: List) requires 0 <= i < length(xs); ensures nth(i, xtr(nats(length(xs)), xs)) == nth(i, rev(xs)); { calc { nth(i, xtr(nats(length(xs)), xs)); { NatsLength(length(xs)); NthXtr(i, nats(length(xs)), xs); } nth(nth(i, nats(length(xs))), xs); { NthNats(i, length(xs)); } nth(length(xs) - 1 - i, xs); { reveal nth(); RevLength(xs); NthRev(i, xs); } nth(i, rev(xs)); } } // lemmas about what nth gives on certain lists lemma NthXtr<T(00)>(i: int, mp: List<int>, lst: List<T>) requires 0 <= i < length(mp); ensures nth(i, xtr(mp, lst)) == nth(nth(i, mp), lst); { reveal nth(); XtrLength(mp, lst); if i == 0 { } else { calc { nth(i-1, xtr(mp, lst).tail); // def. xtr nth(i-1, xtr(mp.tail, lst)); { NthXtr(i-1, mp.tail, lst); } nth(nth(i-1, mp.tail), lst); } } } lemma NthNats(i: int, n: nat) requires 0 <= i < n; ensures nth(i, nats(n)) == n - 1 - i; { reveal nth(); NatsLength(n); NthNatsWorker(i, n); } lemma NthNatsWorker(i: int, n: nat) requires 0 <= i < n && length(nats(n)) == n; ensures nthWorker(i, nats(n)) == n - 1 - i; { } lemma NthRev<T(00)>(i: int, xs: List) requires 0 <= i < length(xs) == length(rev(xs)); ensures nthWorker(i, rev(xs)) == nthWorker(length(xs) - 1 - i, xs); { reveal nth(); RevLength(xs.tail); if i < length(rev(xs.tail)) { calc { nth(i, rev(xs)); nthWorker(i, rev(xs)); // def. rev nthWorker(i, append(rev(xs.tail), Cons(xs.head, Nil))); { NthAppendA(i, rev(xs.tail), Cons(xs.head, Nil)); } nthWorker(i, rev(xs.tail)); { NthRev(i, xs.tail); } // induction hypothesis nthWorker(length(xs.tail) - 1 - i, xs.tail); // def. nthWorker nthWorker(length(xs.tail) - 1 - i + 1, xs); nthWorker(length(xs) - 1 - i, xs); } } else { calc { nth(i, rev(xs)); nthWorker(i, rev(xs)); // def. rev nthWorker(i, append(rev(xs.tail), Cons(xs.head, Nil))); { NthAppendB(i, rev(xs.tail), Cons(xs.head, Nil)); } nthWorker(i - length(rev(xs.tail)), Cons(xs.head, Nil)); nthWorker(0, Cons(xs.head, Nil)); nthWorker(0, xs); nthWorker(length(xs) - 1 - length(xs.tail), xs); { RevLength(xs.tail); } nthWorker(length(xs) - 1 - length(rev(xs.tail)), xs); nth(length(xs) - 1 - length(rev(xs.tail)), xs); nth(length(xs) - 1 - i, xs); } } } lemma NthAppendA<T(00)>(i: int, xs: List, ys: List) requires 0 <= i < length(xs); ensures nth(i, append(xs, ys)) == nth(i, xs); { reveal nth(); if i == 0 { calc { nth(0, append(xs, ys)); nth(0, Cons(xs.head, append(xs.tail, ys))); xs.head; } } else { calc { nth(i, append(xs, ys)); nth(i, Cons(xs.head, append(xs.tail, ys))); nth(i-1, append(xs.tail, ys)); { NthAppendA(i-1, xs.tail, ys); } nth(i-1, xs.tail); } } } lemma NthAppendB<T(00)>(i: int, xs: List, ys: List) requires length(xs) <= i < length(xs) + length(ys); ensures nth(i, append(xs, ys)) == nth(i - length(xs), ys); { reveal nth(); AppendLength(xs, ys); match xs { case Nil => case Cons(x, rest) => calc { nth(i, append(xs, ys)); nth(i, append(Cons(x, rest), ys)); // def. append nth(i, Cons(x, append(rest, ys))); nth(i-1, append(rest, ys)); { NthAppendB(i-1, rest, ys); } nth(i-1 - length(rest), ys); } } }
// RUN: %dafny /compile:0 /deprecation:0 /proverOpt:O:smt.qi.eager_threshold=30 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" // This is the Extractor Problem from section 11.8 of the ACL2 book, // "Computer-Aided Reasoning: An Approach" by Kaufmann, Manolios, and // Moore (2011 edition). datatype List<T> = Nil | Cons(head: T, tail: List) ghost function length(xs: List): nat { match xs case Nil => 0 case Cons(_, rest) => 1 + length(rest) } // If "0 <= n < length(xs)", then return the element of "xs" that is preceded by // "n" elements; otherwise, return an arbitrary value. ghost opaque function nth<T(00)>(n: int, xs: List<T>): T { if 0 <= n < length(xs) then nthWorker(n, xs) else var t :| true; t } ghost function nthWorker<T>(n: int, xs: List<T>): T requires 0 <= n < length(xs); { if n == 0 then xs.head else nthWorker(n-1, xs.tail) } ghost function append(xs: List, ys: List): List { match xs case Nil => ys case Cons(x, rest) => Cons(x, append(rest, ys)) } ghost function rev(xs: List): List { match xs case Nil => Nil case Cons(x, rest) => append(rev(rest), Cons(x, Nil)) } ghost function nats(n: nat): List<int> { if n == 0 then Nil else Cons(n-1, nats(n-1)) } ghost function xtr<T(00)>(mp: List<int>, lst: List): List { match mp case Nil => Nil case Cons(n, rest) => Cons(nth(n, lst), xtr(rest, lst)) } lemma ExtractorTheorem<T(00)>(xs: List) ensures xtr(nats(length(xs)), xs) == rev(xs); { var a, b := xtr(nats(length(xs)), xs), rev(xs); calc { length(a); { XtrLength(nats(length(xs)), xs); } length(nats(length(xs))); { NatsLength(length(xs)); } length(xs); } calc { length(xs); { RevLength(xs); } length(b); } forall i | 0 <= i < length(xs) ensures nth(i, a) == nth(i, b); { reveal nth(); ExtractorLemma(i, xs); } EqualElementsMakeEqualLists(a, b); } // auxiliary lemmas and proofs follow // lemmas about length lemma XtrLength(mp: List<int>, lst: List) ensures length(xtr(mp, lst)) == length(mp); { } lemma NatsLength(n: nat) ensures length(nats(n)) == n; { } lemma AppendLength(xs: List, ys: List) ensures length(append(xs, ys)) == length(xs) + length(ys); { } lemma RevLength(xs: List) ensures length(rev(xs)) == length(xs); { match xs { case Nil => case Cons(x, rest) => calc { length(append(rev(rest), Cons(x, Nil))); { AppendLength(rev(rest), Cons(x, Nil)); } length(rev(rest)) + length(Cons(x, Nil)); length(rev(rest)) + 1; } } } // you can prove two lists equal by proving their elements equal lemma EqualElementsMakeEqualLists<T(00)>(xs: List, ys: List) requires length(xs) == length(ys) requires forall i :: 0 <= i < length(xs) ==> nth(i, xs) == nth(i, ys) ensures xs == ys { reveal nth(); match xs { case Nil => case Cons(x, rest) => assert nth(0, xs) == nth(0, ys); forall i | 0 <= i < length(xs.tail) { calc { nth(i, xs.tail) == nth(i, ys.tail); nth(i+1, Cons(xs.head, xs.tail)) == nth(i+1, Cons(ys.head, ys.tail)); nth(i+1, xs) == nth(i+1, ys); } } // EqualElementsMakeEqualLists(xs.tail, ys.tail); } } // here is the theorem, but applied to the ith element lemma {:vcs_split_on_every_assert} ExtractorLemma<T(00)>(i: int, xs: List) requires 0 <= i < length(xs); ensures nth(i, xtr(nats(length(xs)), xs)) == nth(i, rev(xs)); { calc { nth(i, xtr(nats(length(xs)), xs)); { NatsLength(length(xs)); NthXtr(i, nats(length(xs)), xs); } nth(nth(i, nats(length(xs))), xs); { NthNats(i, length(xs)); } nth(length(xs) - 1 - i, xs); { reveal nth(); RevLength(xs); NthRev(i, xs); } nth(i, rev(xs)); } } // lemmas about what nth gives on certain lists lemma NthXtr<T(00)>(i: int, mp: List<int>, lst: List<T>) requires 0 <= i < length(mp); ensures nth(i, xtr(mp, lst)) == nth(nth(i, mp), lst); { reveal nth(); XtrLength(mp, lst); assert nth(i, xtr(mp, lst)) == nthWorker(i, xtr(mp, lst)); if i == 0 { } else { calc { nth(i-1, xtr(mp, lst).tail); // def. xtr nth(i-1, xtr(mp.tail, lst)); { NthXtr(i-1, mp.tail, lst); } nth(nth(i-1, mp.tail), lst); } } } lemma NthNats(i: int, n: nat) requires 0 <= i < n; ensures nth(i, nats(n)) == n - 1 - i; { reveal nth(); NatsLength(n); NthNatsWorker(i, n); } lemma NthNatsWorker(i: int, n: nat) requires 0 <= i < n && length(nats(n)) == n; ensures nthWorker(i, nats(n)) == n - 1 - i; { } lemma NthRev<T(00)>(i: int, xs: List) requires 0 <= i < length(xs) == length(rev(xs)); ensures nthWorker(i, rev(xs)) == nthWorker(length(xs) - 1 - i, xs); { reveal nth(); assert xs.Cons?; assert 1 <= length(rev(xs)) && rev(xs).Cons?; RevLength(xs.tail); if i < length(rev(xs.tail)) { calc { nth(i, rev(xs)); nthWorker(i, rev(xs)); // def. rev nthWorker(i, append(rev(xs.tail), Cons(xs.head, Nil))); { NthAppendA(i, rev(xs.tail), Cons(xs.head, Nil)); } nthWorker(i, rev(xs.tail)); { NthRev(i, xs.tail); } // induction hypothesis nthWorker(length(xs.tail) - 1 - i, xs.tail); // def. nthWorker nthWorker(length(xs.tail) - 1 - i + 1, xs); nthWorker(length(xs) - 1 - i, xs); } } else { assert i == length(rev(xs.tail)); calc { nth(i, rev(xs)); nthWorker(i, rev(xs)); // def. rev nthWorker(i, append(rev(xs.tail), Cons(xs.head, Nil))); { NthAppendB(i, rev(xs.tail), Cons(xs.head, Nil)); } nthWorker(i - length(rev(xs.tail)), Cons(xs.head, Nil)); nthWorker(0, Cons(xs.head, Nil)); nthWorker(0, xs); nthWorker(length(xs) - 1 - length(xs.tail), xs); { RevLength(xs.tail); } nthWorker(length(xs) - 1 - length(rev(xs.tail)), xs); nth(length(xs) - 1 - length(rev(xs.tail)), xs); nth(length(xs) - 1 - i, xs); } } } lemma NthAppendA<T(00)>(i: int, xs: List, ys: List) requires 0 <= i < length(xs); ensures nth(i, append(xs, ys)) == nth(i, xs); { reveal nth(); if i == 0 { calc { nth(0, append(xs, ys)); nth(0, Cons(xs.head, append(xs.tail, ys))); xs.head; } } else { calc { nth(i, append(xs, ys)); nth(i, Cons(xs.head, append(xs.tail, ys))); nth(i-1, append(xs.tail, ys)); { NthAppendA(i-1, xs.tail, ys); } nth(i-1, xs.tail); } } } lemma NthAppendB<T(00)>(i: int, xs: List, ys: List) requires length(xs) <= i < length(xs) + length(ys); ensures nth(i, append(xs, ys)) == nth(i - length(xs), ys); { reveal nth(); AppendLength(xs, ys); match xs { case Nil => assert nth(i, append(xs, ys)) == nth(i, ys); case Cons(x, rest) => calc { nth(i, append(xs, ys)); nth(i, append(Cons(x, rest), ys)); // def. append nth(i, Cons(x, append(rest, ys))); nth(i-1, append(rest, ys)); { NthAppendB(i-1, rest, ys); } nth(i-1 - length(rest), ys); } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny4_ACL2-extractor.dfy
324
324
Dafny program: 324
// RUN: %testDafnyForEachResolver "%s" ghost predicate IsPrime(n: int) { 2 <= n && forall m :: 2 <= m < n ==> n % m != 0 // WISH It would be great to think about the status of modulo as a trigger } // The following theorem shows that there is an infinite number of primes lemma AlwaysMorePrimes(k: int) ensures exists p :: k <= p && IsPrime(p) { var j, s := 0, {}; while true { var p := GetLargerPrime(s, j); if k <= p { return; } j, s := p, set x | 2 <= x <= p && IsPrime(x); } } // Here is an alternative formulation of the theorem lemma NoFiniteSetContainsAllPrimes(s: set<int>) ensures exists p :: IsPrime(p) && p !in s { AlwaysMorePrimes(if s == {} then 0 else PickLargest(s) + 1); } // ------------------------- lemmas and auxiliary definitions ghost predicate AllPrimes(s: set<int>, bound: int) { // s contains only primes (forall x :: x in s ==> IsPrime(x)) && // every prime up to "bound" is included in s (forall p :: IsPrime(p) && p <= bound ==> p in s) } lemma GetLargerPrime(s: set<int>, bound: int) returns (p: int) requires AllPrimes(s, bound) ensures bound < p && IsPrime(p) { var q := product(s); if exists p :: bound < p <= q && IsPrime(p) { p :| bound < p <= q && IsPrime(p); } else { ProductPlusOneIsPrime(s, q); p := q+1; if p <= bound { // by contradction, establish bound < p product_property(s); } } } ghost function product(s: set<int>): int { if s == {} then 1 else var a := PickLargest(s); a * product(s - {a}) } lemma product_property(s: set<int>) requires forall x :: x in s ==> 1 <= x ensures 1 <= product(s) && forall x :: x in s ==> x <= product(s) { if s != {} { var a := PickLargest(s); var s' := s - {a}; product_property(s'); MulPos(a, product(s')); } } lemma ProductPlusOneIsPrime(s: set<int>, q: int) requires AllPrimes(s, q) && q == product(s) ensures IsPrime(q+1) { var p := q+1; calc { true; { product_property(s); } 2 <= p; } forall m | 2 <= m <= q && IsPrime(m) ensures p % m != 0 { RemoveFactor(m, s); var l := product(s-{m}); MulDivMod(m, l, q, 1); } AltPrimeDefinition(q+1); } // The following lemma is essentially just associativity and commutativity of multiplication. // To get this proof through, it is necessary to know that if x!=y and y==Pick...(s), then // also y==Pick...(s - {x}). It is for this reason that we use PickLargest, instead of // picking an arbitrary element from s. lemma RemoveFactor(x: int, s: set<int>) requires x in s ensures product(s) == x * product(s - {x}) { var y := PickLargest(s); if x != y { calc { product(s); y * product(s - {y}); { RemoveFactor(x, s - {y}); } y * x * product(s - {y} - {x}); x * y * product(s - {y} - {x}); { assert s - {y} - {x} == s - {x} - {y}; } x * y * product(s - {x} - {y}); /* FIXME: This annotation wasn't needed before the introduction * of auto-triggers. It's not needed if one adds {:no_trigger} * to the forall y :: y in s ==> y <= x part of PickLargest, but that * boils down to z3 picking $Box(...) as good trigger */ // FIXME: the parens shouldn't be needed around (s - {x}) { assert y in (s - {x}); } { assert y == PickLargest(s - {x}); } x * product(s - {x}); } } } // This definition is like IsPrime above, except that the quantification is only over primes. ghost predicate IsPrime_Alt(n: int) { 2 <= n && forall m :: 2 <= m < n && IsPrime(m) ==> n % m != 0 } // To show that n is prime, it suffices to prove that it satisfies the alternate definition lemma AltPrimeDefinition(n: int) requires IsPrime_Alt(n) ensures IsPrime(n) { forall m | 2 <= m < n ensures n % m != 0 { if !IsPrime(m) { var a, b := Composite(m); if n % m == 0 { // proof by contradiction var k := n / m; calc { true; k == n / m; m * k == n; a * b * k == n; ==> { MulDivMod(a, b*k, n, 0); } n % a == 0; ==> // IsPrime_Alt !(2 <= a < n && IsPrime(a)); { assert 2 <= a < m < n; } !IsPrime(a); false; } } } } } lemma Composite(c: int) returns (a: int, b: int) requires 2 <= c && !IsPrime(c) ensures 2 <= a < c && 2 <= b && a * b == c ensures IsPrime(a) { calc { true; !IsPrime(c); !(2 <= c && forall m :: 2 <= m < c ==> c % m != 0); exists m :: 2 <= m < c && c % m == 0; } a :| 2 <= a < c && c % a == 0; b := c / a; if !IsPrime(a) { var x, y := Composite(a); a, b := x, y*b; } } ghost function PickLargest(s: set<int>): int requires s != {} { LargestElementExists(s); var x :| x in s && forall y :: y in s ==> y <= x; x } lemma LargestElementExists(s: set<int>) requires s != {} ensures exists x :: x in s && forall y :: y in s ==> y <= x { var s' := s; while true { var x :| x in s'; // pick something if forall y :: y in s' ==> y <= x { // good pick return; } else { // constrain the pick further var y :| y in s' && x < y; s' := set z | z in s && x < z; } } } lemma MulPos(a: int, b: int) requires 1 <= a && 1 <= b ensures a <= a * b { if b == 1 { } else { MulPos(a, b - 1); } } // This axiom about % is needed. Unfortunately, Z3 seems incapable of proving it. lemma MulDivMod(a: nat, b: nat, c: nat, j: nat) requires a * b == c && j < a ensures (c+j) % a == j
// RUN: %testDafnyForEachResolver "%s" ghost predicate IsPrime(n: int) { 2 <= n && forall m :: 2 <= m < n ==> n % m != 0 // WISH It would be great to think about the status of modulo as a trigger } // The following theorem shows that there is an infinite number of primes lemma AlwaysMorePrimes(k: int) ensures exists p :: k <= p && IsPrime(p) { var j, s := 0, {}; while true invariant AllPrimes(s, j) decreases k - j { var p := GetLargerPrime(s, j); if k <= p { return; } j, s := p, set x | 2 <= x <= p && IsPrime(x); } } // Here is an alternative formulation of the theorem lemma NoFiniteSetContainsAllPrimes(s: set<int>) ensures exists p :: IsPrime(p) && p !in s { AlwaysMorePrimes(if s == {} then 0 else PickLargest(s) + 1); } // ------------------------- lemmas and auxiliary definitions ghost predicate AllPrimes(s: set<int>, bound: int) { // s contains only primes (forall x :: x in s ==> IsPrime(x)) && // every prime up to "bound" is included in s (forall p :: IsPrime(p) && p <= bound ==> p in s) } lemma GetLargerPrime(s: set<int>, bound: int) returns (p: int) requires AllPrimes(s, bound) ensures bound < p && IsPrime(p) { var q := product(s); if exists p :: bound < p <= q && IsPrime(p) { p :| bound < p <= q && IsPrime(p); } else { ProductPlusOneIsPrime(s, q); p := q+1; if p <= bound { // by contradction, establish bound < p assert p in s; product_property(s); assert false; } } } ghost function product(s: set<int>): int { if s == {} then 1 else var a := PickLargest(s); a * product(s - {a}) } lemma product_property(s: set<int>) requires forall x :: x in s ==> 1 <= x ensures 1 <= product(s) && forall x :: x in s ==> x <= product(s) { if s != {} { var a := PickLargest(s); var s' := s - {a}; assert s == s' + {a}; product_property(s'); MulPos(a, product(s')); } } lemma ProductPlusOneIsPrime(s: set<int>, q: int) requires AllPrimes(s, q) && q == product(s) ensures IsPrime(q+1) { var p := q+1; calc { true; { product_property(s); } 2 <= p; } forall m | 2 <= m <= q && IsPrime(m) ensures p % m != 0 { assert m in s; // because AllPrimes(s, q) && m <= q && IsPrime(m) RemoveFactor(m, s); var l := product(s-{m}); assert m*l == q; MulDivMod(m, l, q, 1); } assert IsPrime_Alt(q+1); AltPrimeDefinition(q+1); } // The following lemma is essentially just associativity and commutativity of multiplication. // To get this proof through, it is necessary to know that if x!=y and y==Pick...(s), then // also y==Pick...(s - {x}). It is for this reason that we use PickLargest, instead of // picking an arbitrary element from s. lemma RemoveFactor(x: int, s: set<int>) requires x in s ensures product(s) == x * product(s - {x}) { var y := PickLargest(s); if x != y { calc { product(s); y * product(s - {y}); { RemoveFactor(x, s - {y}); } y * x * product(s - {y} - {x}); x * y * product(s - {y} - {x}); { assert s - {y} - {x} == s - {x} - {y}; } x * y * product(s - {x} - {y}); /* FIXME: This annotation wasn't needed before the introduction * of auto-triggers. It's not needed if one adds {:no_trigger} * to the forall y :: y in s ==> y <= x part of PickLargest, but that * boils down to z3 picking $Box(...) as good trigger */ // FIXME: the parens shouldn't be needed around (s - {x}) { assert y in (s - {x}); } { assert y == PickLargest(s - {x}); } x * product(s - {x}); } } } // This definition is like IsPrime above, except that the quantification is only over primes. ghost predicate IsPrime_Alt(n: int) { 2 <= n && forall m :: 2 <= m < n && IsPrime(m) ==> n % m != 0 } // To show that n is prime, it suffices to prove that it satisfies the alternate definition lemma AltPrimeDefinition(n: int) requires IsPrime_Alt(n) ensures IsPrime(n) { forall m | 2 <= m < n ensures n % m != 0 { if !IsPrime(m) { var a, b := Composite(m); if n % m == 0 { // proof by contradiction var k := n / m; calc { true; k == n / m; m * k == n; a * b * k == n; ==> { MulDivMod(a, b*k, n, 0); } n % a == 0; ==> // IsPrime_Alt !(2 <= a < n && IsPrime(a)); { assert 2 <= a < m < n; } !IsPrime(a); false; } } } } } lemma Composite(c: int) returns (a: int, b: int) requires 2 <= c && !IsPrime(c) ensures 2 <= a < c && 2 <= b && a * b == c ensures IsPrime(a) { calc { true; !IsPrime(c); !(2 <= c && forall m :: 2 <= m < c ==> c % m != 0); exists m :: 2 <= m < c && c % m == 0; } a :| 2 <= a < c && c % a == 0; b := c / a; assert 2 <= a < c && 2 <= b && a * b == c; if !IsPrime(a) { var x, y := Composite(a); a, b := x, y*b; } } ghost function PickLargest(s: set<int>): int requires s != {} { LargestElementExists(s); var x :| x in s && forall y :: y in s ==> y <= x; x } lemma LargestElementExists(s: set<int>) requires s != {} ensures exists x :: x in s && forall y :: y in s ==> y <= x { var s' := s; while true invariant s' != {} && s' <= s invariant forall x,y :: x in s' && y in s && y !in s' ==> y <= x decreases s' { var x :| x in s'; // pick something if forall y :: y in s' ==> y <= x { // good pick return; } else { // constrain the pick further var y :| y in s' && x < y; s' := set z | z in s && x < z; assert y in s'; } } } lemma MulPos(a: int, b: int) requires 1 <= a && 1 <= b ensures a <= a * b { if b == 1 { assert a * b == a; } else { assert a * b == a * (b - 1) + a; MulPos(a, b - 1); } } // This axiom about % is needed. Unfortunately, Z3 seems incapable of proving it. lemma MulDivMod(a: nat, b: nat, c: nat, j: nat) requires a * b == c && j < a ensures (c+j) % a == j
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_from dafny main repo_dafny4_Primes.dfy
327
327
Dafny program: 327
class CrashableMem<T> { var mem_ : array<T>; method read(off : int) returns (r : T) requires 0 <= off < mem_.Length; { return mem_[off]; } method write(off : int, val : T) requires 0 <= off < mem_.Length; modifies mem_; { mem_[off] := val; } } datatype GhostState = GS( num_entry : int, log : seq<int>, mem_len : int, mem : seq<int>, old_mem : seq<int>, ideal_mem : seq<int>, countdown : int, first_log_pos : map<int, int> ) datatype GhostOp = WriteMem(off : int, val : int) | WriteLog(off : int, val : int) predicate ghost_state_inv(s : GhostState) { 0 <= s.num_entry * 2 < |s.log| && |s.log| > 0 && |s.mem| == s.mem_len && |s.ideal_mem| == s.mem_len && |s.old_mem| == s.mem_len && s.countdown >= 0 } function init_ghost_state(log : seq<int>, mem : seq<int>, countdown : int) : GhostState requires |log| > 0; requires countdown >= 0; ensures ghost_state_inv(init_ghost_state(log, mem, countdown)); { GS(0, log[..], |mem|, mem[..], mem[..], mem[..], countdown, map[]) } function mem_write(s : GhostState, off: int, val: int) : GhostState requires ghost_state_inv(s); requires 0 <= off < s.mem_len; ensures ghost_state_inv(mem_write(s, off, val)); { var new_mem := s.mem[off := val]; var new_ideal_mem := s.ideal_mem[off := val]; s.(mem := new_mem, ideal_mem := new_ideal_mem) } function log_write(s : GhostState, off : int, val: int) : GhostState requires ghost_state_inv(s); requires 0 <= off < |s.log|; ensures ghost_state_inv(log_write(s, off, val)); { s.(log := s.log[off := val]) } predicate valid_op(s : GhostState, op : GhostOp) { match op case WriteMem(off, val) => 0 <= off < |s.mem| case WriteLog(off, val) => 0 <= off < |s.log| } function countdown (s : GhostState) : GhostState { if s.countdown > 0 then s.(countdown := s.countdown - 1) else s } function normal_step (s : GhostState, op : GhostOp) : GhostState requires valid_op(s, op); requires ghost_state_inv(s); ensures ghost_state_inv(normal_step(s, op)); { match op case WriteMem(off, val) => mem_write(s, off, val) case WriteLog(off, val) => log_write(s, off, val) } function ghost_step (s : GhostState, op : GhostOp) : (GhostState, bool) requires valid_op(s, op); requires ghost_state_inv(s); ensures ghost_state_inv(normal_step(s, op)); { if s.countdown > 0 then var s' := normal_step(s, op); (s'.(countdown := s.countdown - 1), true) else (s, false) } function mem_write_step (s : GhostState, off : int, val : int) : (GhostState, bool) requires 0 <= off < s.mem_len; requires ghost_state_inv(s); { ghost_step(s, WriteMem(off, val)) } function log_write_step (s : GhostState, off : int, val : int) : (GhostState, bool) requires 0 <= off < |s.log|; requires ghost_state_inv(s); { ghost_step(s, WriteLog(off, val)) } function set_num_entry (s : GhostState, n : int) : (GhostState, bool) requires 0 <= n * 2 < |s.log|; { if s.countdown > 0 then (s.(num_entry := n, countdown := s.countdown - 1), true) else (s, false) } predicate crashed (s : GhostState) { s.countdown <= 0 } predicate old_mem_equiv (s : GhostState) requires ghost_state_inv(s); { (forall o :: !(o in s.first_log_pos) && 0 <= o < |s.mem| ==> s.mem[o] == s.old_mem[o]) } predicate ghost_tx_inv (s : GhostState) { ghost_state_inv(s) && (forall o :: o in s.first_log_pos ==> 0 <= o < s.mem_len) && (forall o :: o in s.first_log_pos ==> 0 <= s.first_log_pos[o] < s.num_entry) && (forall o :: o in s.first_log_pos ==> 0 <= s.first_log_pos[o] * 2 + 1 < |s.log|) && (forall o :: o in s.first_log_pos ==> s.log[s.first_log_pos[o] * 2] == o) && (forall o :: o in s.first_log_pos ==> s.log[s.first_log_pos[o] * 2 + 1] == s.old_mem[o]) && (forall o :: o in s.first_log_pos ==> forall i :: 0 <= i < s.first_log_pos[o] ==> s.log[i * 2] != o) && (forall i :: 0 <= i < s.num_entry ==> s.log[i * 2] in s.first_log_pos) } function ghost_begin_tx (s : GhostState) : GhostState requires ghost_state_inv(s); requires s.num_entry == 0; ensures ghost_state_inv(ghost_begin_tx(s)); ensures ghost_tx_inv(ghost_begin_tx(s)); ensures old_mem_equiv(ghost_begin_tx(s)); { var (s', f) := set_num_entry(s, 0); var s' := s'.(first_log_pos := map[], old_mem := s.mem[..]); s' } function ghost_commit_tx (s : GhostState) : (GhostState, bool) requires ghost_tx_inv(s); requires old_mem_equiv(s); ensures ghost_state_inv(ghost_commit_tx(s).0); ensures ghost_tx_inv(ghost_commit_tx(s).0); ensures !ghost_commit_tx(s).1 ==> old_mem_equiv(ghost_commit_tx(s).0); ensures ghost_commit_tx(s).1 ==> ghost_commit_tx(s).0.num_entry == 0; { var s' := s; var (s', f) := set_num_entry(s', 0); var s' := if f then s'.(first_log_pos := map[]) else s'; (s', f) } function ghost_tx_write (s0 : GhostState, off : int, val : int) : GhostState requires ghost_tx_inv(s0); requires old_mem_equiv(s0); requires 0 <= off < s0.mem_len; requires 0 <= s0.num_entry * 2 + 2 < |s0.log|; ensures ghost_tx_inv(ghost_tx_write(s0, off, val)); ensures old_mem_equiv(ghost_tx_write(s0, off, val)); ensures |ghost_tx_write(s0, off, val).mem| == s0.mem_len; ensures !crashed(ghost_tx_write(s0, off, val)) ==> ghost_tx_write(s0, off, val).mem[off] == val; { var s := s0; var log_idx := s.num_entry; var log_off := log_idx * 2; var old_val := s.mem[off]; var (s, f) := log_write_step(s, log_off, off); var (s, f) := log_write_step(s, log_off + 1, old_val); var (s, f) := set_num_entry(s, log_idx + 1); var s := if f && !(off in s.first_log_pos) then s.(first_log_pos := s.first_log_pos[off := log_idx]) else s; var (s, f) := mem_write_step(s, off, val); s } function reverse_recovery (s0 : GhostState, idx : int) : GhostState requires ghost_tx_inv(s0); requires old_mem_equiv(s0); requires 0 <= idx <= s0.num_entry; ensures ghost_tx_inv(reverse_recovery(s0, idx)); ensures old_mem_equiv(reverse_recovery(s0, idx)); ensures s0.old_mem == reverse_recovery(s0, idx).old_mem; ensures s0.first_log_pos == reverse_recovery(s0, idx).first_log_pos; ensures forall o :: o in s0.first_log_pos && s0.first_log_pos[o] >= idx ==> reverse_recovery(s0, idx).mem[o] == s0.mem[o]; ensures forall o :: o in s0.first_log_pos && 0 <= s0.first_log_pos[o] < idx ==> reverse_recovery(s0, idx).mem[o] == s0.old_mem[o]; { if idx == 0 then s0 else var s := s0; var i := idx - 1; var off := s.log[i * 2]; var val := s.log[i * 2 + 1]; var s := s.(mem := s.mem[off := val]); var s := reverse_recovery(s, idx - 1); s.mem[o] == s.old_mem[o]; o == off && val == s.old_mem[o]; s.mem[o] == val; s } function ghost_recover (s0 : GhostState) : GhostState requires ghost_tx_inv(s0); requires old_mem_equiv(s0); ensures ghost_recover(s0).mem == s0.old_mem; ensures ghost_recover(s0).num_entry == 0; { var s := reverse_recovery(s0, s0.num_entry); s.(num_entry := 0) } class UndoLog { var log_ : array<int>; var mem_ : array<int>; var impl_countdown : int; ghost var gs : GhostState; constructor () {} predicate ghost_state_equiv(gs : GhostState) reads this; reads mem_; reads log_; { log_.Length > 0 && mem_[..] == gs.mem && log_[1..] == gs.log && log_[0] == gs.num_entry && impl_countdown == gs.countdown } predicate state_inv() reads this; reads log_; { log_.Length > 1 && 0 <= log_[0] && (log_[0] * 2) < log_.Length && log_.Length < 0xffffffff && mem_ != log_ && forall i : int :: 0 <= i < log_[0] ==> 0 <= log_[i * 2 + 1] < mem_.Length && impl_countdown >= 0 } method init(log_size : int, mem_size : int, countdown : int) requires log_size > 1; requires mem_size > 0; requires log_size < 0xffffffff; modifies this; ensures fresh(log_); ensures fresh(mem_); ensures state_inv(); ensures ghost_state_equiv(gs); { log_ := new int[log_size]; mem_ := new int[mem_size]; log_[0] := 0; impl_countdown := countdown; gs := GS(0, log_[1..], mem_size, mem_[..], mem_[..], mem_[..], countdown, map[]); } method impl_countdown_dec() modifies this; requires impl_countdown > 0; requires mem_ != log_; ensures mem_ != log_; ensures impl_countdown == old(impl_countdown) - 1; ensures impl_countdown >= 0; ensures gs == old(gs); ensures log_[..] == old(log_)[..]; ensures mem_[..] == old(mem_)[..]; { impl_countdown := impl_countdown - 1; } method write_mem(off : int, val : int) modifies this; modifies mem_; requires 0 <= off < mem_.Length; requires mem_ != log_; requires ghost_state_inv(gs); requires ghost_state_equiv(gs); requires 0 <= off < gs.mem_len; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures gs == old(gs); ensures ghost_state_equiv(mem_write_step(gs, off, val).0); { if (impl_countdown > 0) { mem_[off] := val; impl_countdown := impl_countdown - 1; } } method write_log(off : int, val : int) modifies this; modifies log_; requires 0 <= off <= |gs.log|; requires mem_ != log_; requires ghost_state_inv(gs); requires ghost_state_equiv(gs); requires off == 0 ==> 0 <= val * 2 < |gs.log|; ensures mem_ != log_; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures log_.Length == old(log_).Length; ensures mem_[..] == old(mem_)[..]; ensures log_[off] == val || log_[off] == old(log_)[off]; ensures forall i :: 0 <= i < log_.Length && i != off ==> log_[i] == old(log_)[i]; ensures gs == old(gs); ensures off > 0 ==> ghost_state_equiv(log_write_step(gs, off - 1, val).0); ensures off == 0 ==> ghost_state_equiv(set_num_entry(gs, val).0); { if (impl_countdown > 0) { log_[off] := val; impl_countdown := impl_countdown - 1; } } method begin_tx() modifies log_; modifies this; requires state_inv(); requires ghost_state_equiv(gs); requires ghost_state_inv(gs); requires log_[0] == 0; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures state_inv(); ensures ghost_state_equiv(gs); ensures ghost_tx_inv(gs); { write_log(0, 0); gs := ghost_begin_tx(gs); } method commit_tx() modifies log_; modifies this; requires state_inv(); requires ghost_state_equiv(gs); requires ghost_state_inv(gs); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); ensures mem_ == old(mem_); ensures log_ == old(log_); ensures ghost_state_equiv(gs); ensures state_inv(); { write_log(0, 0); gs := ghost_commit_tx(gs).0; } method tx_write(offset: int, val : int) modifies this; modifies log_; modifies mem_; requires state_inv(); requires mem_ != log_; requires 0 <= offset < mem_.Length; requires ghost_state_equiv(gs); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); requires 0 <= log_[0] * 2 + 3 < log_.Length; ensures ghost_state_equiv(gs); ensures ghost_tx_inv(gs); ensures old_mem_equiv(gs); { var log_idx := log_[0]; var log_off := log_idx * 2; ghost var old_gs := gs; write_log(log_off + 1, offset); gs := log_write_step(gs, log_off, offset).0; var old_val := mem_[offset]; write_log(log_off + 2, old_val); gs := log_write_step(gs, log_off + 1, old_val).0; write_log(0, log_idx + 1); ghost var (s, f) := set_num_entry(gs, log_idx + 1); s := if f && !(offset in s.first_log_pos) then s.(first_log_pos := s.first_log_pos[offset := log_idx]) else s; gs := s; write_mem(offset, val); gs := mem_write_step(gs, offset, val).0; } // we assume that recover won't crash (though this code works when recover can fail) method recover() modifies log_; modifies mem_; modifies this; requires state_inv(); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); requires ghost_state_equiv(gs); ensures gs == ghost_recover(old(gs)); ensures ghost_state_equiv(gs); { var log_len := log_[0]; if (log_len > 0) { var i := log_len - 1; ghost var gs0 := gs; while i >= 0 modifies mem_; modifies this; { var o := i * 2 + 1; var off := log_[o]; var val := log_[o + 1]; mem_[off] := val; gs := gs.(mem := gs.mem[off := val]); i := i - 1; } } else { } log_[0] := 0; gs := ghost_recover(old(gs)); } } lemma crash_safe_single_tx(init_log : seq<int>, init_mem : seq<int>, countdown : int, writes : seq<(int, int)>) requires |init_log| > 0; requires countdown >= 0; requires forall i :: 0 <= i < |writes| ==> 0 <= writes[i].0 < |init_mem|; requires 0 < |writes| * 2 < |init_log|; { var s := init_ghost_state(init_log, init_mem, countdown); var end_mem := init_mem; s := ghost_begin_tx(s); var i := 0; while i < |writes| { s := ghost_tx_write(s, writes[i].0, writes[i].1); end_mem := end_mem[writes[i].0 := writes[i].1]; i := i + 1; } var (s', c) := ghost_commit_tx(s); if (c) { } else { var recovered := ghost_recover(s'); } }
class CrashableMem<T> { var mem_ : array<T>; method read(off : int) returns (r : T) requires 0 <= off < mem_.Length; { return mem_[off]; } method write(off : int, val : T) requires 0 <= off < mem_.Length; modifies mem_; { mem_[off] := val; } } datatype GhostState = GS( num_entry : int, log : seq<int>, mem_len : int, mem : seq<int>, old_mem : seq<int>, ideal_mem : seq<int>, countdown : int, first_log_pos : map<int, int> ) datatype GhostOp = WriteMem(off : int, val : int) | WriteLog(off : int, val : int) predicate ghost_state_inv(s : GhostState) { 0 <= s.num_entry * 2 < |s.log| && |s.log| > 0 && |s.mem| == s.mem_len && |s.ideal_mem| == s.mem_len && |s.old_mem| == s.mem_len && s.countdown >= 0 } function init_ghost_state(log : seq<int>, mem : seq<int>, countdown : int) : GhostState requires |log| > 0; requires countdown >= 0; ensures ghost_state_inv(init_ghost_state(log, mem, countdown)); { GS(0, log[..], |mem|, mem[..], mem[..], mem[..], countdown, map[]) } function mem_write(s : GhostState, off: int, val: int) : GhostState requires ghost_state_inv(s); requires 0 <= off < s.mem_len; ensures ghost_state_inv(mem_write(s, off, val)); { var new_mem := s.mem[off := val]; var new_ideal_mem := s.ideal_mem[off := val]; s.(mem := new_mem, ideal_mem := new_ideal_mem) } function log_write(s : GhostState, off : int, val: int) : GhostState requires ghost_state_inv(s); requires 0 <= off < |s.log|; ensures ghost_state_inv(log_write(s, off, val)); { s.(log := s.log[off := val]) } predicate valid_op(s : GhostState, op : GhostOp) { match op case WriteMem(off, val) => 0 <= off < |s.mem| case WriteLog(off, val) => 0 <= off < |s.log| } function countdown (s : GhostState) : GhostState { if s.countdown > 0 then s.(countdown := s.countdown - 1) else s } function normal_step (s : GhostState, op : GhostOp) : GhostState requires valid_op(s, op); requires ghost_state_inv(s); ensures ghost_state_inv(normal_step(s, op)); { match op case WriteMem(off, val) => mem_write(s, off, val) case WriteLog(off, val) => log_write(s, off, val) } function ghost_step (s : GhostState, op : GhostOp) : (GhostState, bool) requires valid_op(s, op); requires ghost_state_inv(s); ensures ghost_state_inv(normal_step(s, op)); { if s.countdown > 0 then var s' := normal_step(s, op); (s'.(countdown := s.countdown - 1), true) else (s, false) } function mem_write_step (s : GhostState, off : int, val : int) : (GhostState, bool) requires 0 <= off < s.mem_len; requires ghost_state_inv(s); { ghost_step(s, WriteMem(off, val)) } function log_write_step (s : GhostState, off : int, val : int) : (GhostState, bool) requires 0 <= off < |s.log|; requires ghost_state_inv(s); { ghost_step(s, WriteLog(off, val)) } function set_num_entry (s : GhostState, n : int) : (GhostState, bool) requires 0 <= n * 2 < |s.log|; { if s.countdown > 0 then (s.(num_entry := n, countdown := s.countdown - 1), true) else (s, false) } predicate crashed (s : GhostState) { s.countdown <= 0 } predicate old_mem_equiv (s : GhostState) requires ghost_state_inv(s); { (forall o :: !(o in s.first_log_pos) && 0 <= o < |s.mem| ==> s.mem[o] == s.old_mem[o]) } predicate ghost_tx_inv (s : GhostState) { ghost_state_inv(s) && (forall o :: o in s.first_log_pos ==> 0 <= o < s.mem_len) && (forall o :: o in s.first_log_pos ==> 0 <= s.first_log_pos[o] < s.num_entry) && (forall o :: o in s.first_log_pos ==> 0 <= s.first_log_pos[o] * 2 + 1 < |s.log|) && (forall o :: o in s.first_log_pos ==> s.log[s.first_log_pos[o] * 2] == o) && (forall o :: o in s.first_log_pos ==> s.log[s.first_log_pos[o] * 2 + 1] == s.old_mem[o]) && (forall o :: o in s.first_log_pos ==> forall i :: 0 <= i < s.first_log_pos[o] ==> s.log[i * 2] != o) && (forall i :: 0 <= i < s.num_entry ==> s.log[i * 2] in s.first_log_pos) } function ghost_begin_tx (s : GhostState) : GhostState requires ghost_state_inv(s); requires s.num_entry == 0; ensures ghost_state_inv(ghost_begin_tx(s)); ensures ghost_tx_inv(ghost_begin_tx(s)); ensures old_mem_equiv(ghost_begin_tx(s)); { var (s', f) := set_num_entry(s, 0); var s' := s'.(first_log_pos := map[], old_mem := s.mem[..]); s' } function ghost_commit_tx (s : GhostState) : (GhostState, bool) requires ghost_tx_inv(s); requires old_mem_equiv(s); ensures ghost_state_inv(ghost_commit_tx(s).0); ensures ghost_tx_inv(ghost_commit_tx(s).0); ensures !ghost_commit_tx(s).1 ==> old_mem_equiv(ghost_commit_tx(s).0); ensures ghost_commit_tx(s).1 ==> ghost_commit_tx(s).0.num_entry == 0; { var s' := s; var (s', f) := set_num_entry(s', 0); var s' := if f then s'.(first_log_pos := map[]) else s'; (s', f) } function ghost_tx_write (s0 : GhostState, off : int, val : int) : GhostState requires ghost_tx_inv(s0); requires old_mem_equiv(s0); requires 0 <= off < s0.mem_len; requires 0 <= s0.num_entry * 2 + 2 < |s0.log|; ensures ghost_tx_inv(ghost_tx_write(s0, off, val)); ensures old_mem_equiv(ghost_tx_write(s0, off, val)); ensures |ghost_tx_write(s0, off, val).mem| == s0.mem_len; ensures !crashed(ghost_tx_write(s0, off, val)) ==> ghost_tx_write(s0, off, val).mem[off] == val; { var s := s0; var log_idx := s.num_entry; var log_off := log_idx * 2; var old_val := s.mem[off]; var (s, f) := log_write_step(s, log_off, off); var (s, f) := log_write_step(s, log_off + 1, old_val); var (s, f) := set_num_entry(s, log_idx + 1); var s := if f && !(off in s.first_log_pos) then s.(first_log_pos := s.first_log_pos[off := log_idx]) else s; var (s, f) := mem_write_step(s, off, val); s } function reverse_recovery (s0 : GhostState, idx : int) : GhostState decreases idx; requires ghost_tx_inv(s0); requires old_mem_equiv(s0); requires 0 <= idx <= s0.num_entry; ensures ghost_tx_inv(reverse_recovery(s0, idx)); ensures old_mem_equiv(reverse_recovery(s0, idx)); ensures s0.old_mem == reverse_recovery(s0, idx).old_mem; ensures s0.first_log_pos == reverse_recovery(s0, idx).first_log_pos; ensures forall o :: o in s0.first_log_pos && s0.first_log_pos[o] >= idx ==> reverse_recovery(s0, idx).mem[o] == s0.mem[o]; ensures forall o :: o in s0.first_log_pos && 0 <= s0.first_log_pos[o] < idx ==> reverse_recovery(s0, idx).mem[o] == s0.old_mem[o]; { if idx == 0 then assert old_mem_equiv(s0); s0 else var s := s0; var i := idx - 1; var off := s.log[i * 2]; var val := s.log[i * 2 + 1]; var s := s.(mem := s.mem[off := val]); assert off in s.first_log_pos; var s := reverse_recovery(s, idx - 1); assert i == idx - 1; assert forall o :: o in s.first_log_pos && 0 <= s.first_log_pos[o] < i ==> s.mem[o] == s.old_mem[o]; assert forall o :: o in s.first_log_pos && s.first_log_pos[o] == i ==> o == off && val == s.old_mem[o]; assert forall o :: o in s.first_log_pos && s.first_log_pos[o] == i ==> s.mem[o] == val; assert old_mem_equiv(s); s } function ghost_recover (s0 : GhostState) : GhostState requires ghost_tx_inv(s0); requires old_mem_equiv(s0); ensures ghost_recover(s0).mem == s0.old_mem; ensures ghost_recover(s0).num_entry == 0; { var s := reverse_recovery(s0, s0.num_entry); assert (old_mem_equiv(s)); assert (forall o :: o in s.first_log_pos ==> s.mem[o] == s0.old_mem[o]); assert forall i :: 0 <= i < |s.mem| ==> s.mem[i] == s0.old_mem[i]; s.(num_entry := 0) } class UndoLog { var log_ : array<int>; var mem_ : array<int>; var impl_countdown : int; ghost var gs : GhostState; constructor () {} predicate ghost_state_equiv(gs : GhostState) reads this; reads mem_; reads log_; { log_.Length > 0 && mem_[..] == gs.mem && log_[1..] == gs.log && log_[0] == gs.num_entry && impl_countdown == gs.countdown } predicate state_inv() reads this; reads log_; { log_.Length > 1 && 0 <= log_[0] && (log_[0] * 2) < log_.Length && log_.Length < 0xffffffff && mem_ != log_ && forall i : int :: 0 <= i < log_[0] ==> 0 <= log_[i * 2 + 1] < mem_.Length && impl_countdown >= 0 } method init(log_size : int, mem_size : int, countdown : int) requires log_size > 1; requires mem_size > 0; requires log_size < 0xffffffff; modifies this; ensures fresh(log_); ensures fresh(mem_); ensures state_inv(); ensures ghost_state_equiv(gs); { log_ := new int[log_size]; mem_ := new int[mem_size]; log_[0] := 0; impl_countdown := countdown; gs := GS(0, log_[1..], mem_size, mem_[..], mem_[..], mem_[..], countdown, map[]); } method impl_countdown_dec() modifies this; requires impl_countdown > 0; requires mem_ != log_; ensures mem_ != log_; ensures impl_countdown == old(impl_countdown) - 1; ensures impl_countdown >= 0; ensures gs == old(gs); ensures log_[..] == old(log_)[..]; ensures mem_[..] == old(mem_)[..]; { impl_countdown := impl_countdown - 1; } method write_mem(off : int, val : int) modifies this; modifies mem_; requires 0 <= off < mem_.Length; requires mem_ != log_; requires ghost_state_inv(gs); requires ghost_state_equiv(gs); requires 0 <= off < gs.mem_len; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures gs == old(gs); ensures ghost_state_equiv(mem_write_step(gs, off, val).0); { if (impl_countdown > 0) { mem_[off] := val; impl_countdown := impl_countdown - 1; } } method write_log(off : int, val : int) modifies this; modifies log_; requires 0 <= off <= |gs.log|; requires mem_ != log_; requires ghost_state_inv(gs); requires ghost_state_equiv(gs); requires off == 0 ==> 0 <= val * 2 < |gs.log|; ensures mem_ != log_; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures log_.Length == old(log_).Length; ensures mem_[..] == old(mem_)[..]; ensures log_[off] == val || log_[off] == old(log_)[off]; ensures forall i :: 0 <= i < log_.Length && i != off ==> log_[i] == old(log_)[i]; ensures gs == old(gs); ensures off > 0 ==> ghost_state_equiv(log_write_step(gs, off - 1, val).0); ensures off == 0 ==> ghost_state_equiv(set_num_entry(gs, val).0); { if (impl_countdown > 0) { log_[off] := val; impl_countdown := impl_countdown - 1; } } method begin_tx() modifies log_; modifies this; requires state_inv(); requires ghost_state_equiv(gs); requires ghost_state_inv(gs); requires log_[0] == 0; ensures mem_ == old(mem_); ensures log_ == old(log_); ensures state_inv(); ensures ghost_state_equiv(gs); ensures ghost_tx_inv(gs); { write_log(0, 0); gs := ghost_begin_tx(gs); assert state_inv(); } method commit_tx() modifies log_; modifies this; requires state_inv(); requires ghost_state_equiv(gs); requires ghost_state_inv(gs); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); ensures mem_ == old(mem_); ensures log_ == old(log_); ensures ghost_state_equiv(gs); ensures state_inv(); { write_log(0, 0); gs := ghost_commit_tx(gs).0; } method tx_write(offset: int, val : int) modifies this; modifies log_; modifies mem_; requires state_inv(); requires mem_ != log_; requires 0 <= offset < mem_.Length; requires ghost_state_equiv(gs); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); requires 0 <= log_[0] * 2 + 3 < log_.Length; ensures ghost_state_equiv(gs); ensures ghost_tx_inv(gs); ensures old_mem_equiv(gs); { var log_idx := log_[0]; var log_off := log_idx * 2; ghost var old_gs := gs; write_log(log_off + 1, offset); gs := log_write_step(gs, log_off, offset).0; assert log_off + 1 > 0; assert ghost_state_equiv(gs); assert mem_ != log_; var old_val := mem_[offset]; assert old_val == gs.mem[offset]; write_log(log_off + 2, old_val); gs := log_write_step(gs, log_off + 1, old_val).0; assert ghost_tx_inv(gs); assert log_[0] == gs.num_entry; assert log_.Length == |gs.log| + 1; assert 0 <= gs.num_entry * 2 < |gs.log|; write_log(0, log_idx + 1); ghost var (s, f) := set_num_entry(gs, log_idx + 1); s := if f && !(offset in s.first_log_pos) then s.(first_log_pos := s.first_log_pos[offset := log_idx]) else s; gs := s; write_mem(offset, val); gs := mem_write_step(gs, offset, val).0; assert gs == ghost_tx_write(old_gs, offset, val); } // we assume that recover won't crash (though this code works when recover can fail) method recover() modifies log_; modifies mem_; modifies this; requires state_inv(); requires ghost_tx_inv(gs); requires old_mem_equiv(gs); requires ghost_state_equiv(gs); ensures gs == ghost_recover(old(gs)); ensures ghost_state_equiv(gs); { var log_len := log_[0]; assert log_len == gs.num_entry; if (log_len > 0) { var i := log_len - 1; ghost var gs0 := gs; while i >= 0 modifies mem_; modifies this; invariant log_ == old(log_); invariant mem_ == old(mem_); invariant unchanged(log_); invariant -1 <= i < log_len; invariant |gs.log| == |gs0.log|; invariant ghost_state_equiv(gs); invariant ghost_tx_inv(gs); invariant old_mem_equiv(gs); invariant reverse_recovery(gs0, log_len) == reverse_recovery(gs, i + 1); decreases i; { assert ghost_state_equiv(gs); assert 0 <= i < log_[0]; var o := i * 2 + 1; var off := log_[o]; var val := log_[o + 1]; mem_[off] := val; assert 0 <= off < mem_.Length; assert gs.log[i * 2] == off; assert gs.log[i * 2 + 1] == val; gs := gs.(mem := gs.mem[off := val]); i := i - 1; } assert ghost_state_equiv(gs); } else { assert ghost_state_equiv(gs); } log_[0] := 0; gs := ghost_recover(old(gs)); assert ghost_state_equiv(gs); } } lemma crash_safe_single_tx(init_log : seq<int>, init_mem : seq<int>, countdown : int, writes : seq<(int, int)>) requires |init_log| > 0; requires countdown >= 0; requires forall i :: 0 <= i < |writes| ==> 0 <= writes[i].0 < |init_mem|; requires 0 < |writes| * 2 < |init_log|; { var s := init_ghost_state(init_log, init_mem, countdown); var end_mem := init_mem; s := ghost_begin_tx(s); assert s.num_entry == 0; assert init_mem == s.old_mem; var i := 0; while i < |writes| decreases |writes| - i; invariant 0 <= i <= |writes|; invariant s.mem_len == |init_mem|; invariant s.mem_len == |end_mem|; invariant 0 <= s.num_entry <= i; invariant |init_log| == |s.log|; invariant i * 2 < |s.log|; invariant 0 <= s.num_entry * 2 < |s.log|; invariant ghost_tx_inv(s); invariant old_mem_equiv(s); invariant init_mem == s.old_mem; invariant !crashed(s) ==> forall i :: 0 <= i < |s.mem| ==> s.mem[i] == end_mem[i]; { assert 0 <= i < |writes|; assert 0 <= writes[i].0 < s.mem_len; assert 0 <= s.num_entry * 2 + 2 < |s.log|; s := ghost_tx_write(s, writes[i].0, writes[i].1); end_mem := end_mem[writes[i].0 := writes[i].1]; assert !crashed(s) ==> s.mem[writes[i].0] == writes[i].1; i := i + 1; } assert ghost_tx_inv(s); assert old_mem_equiv(s); var (s', c) := ghost_commit_tx(s); assert c ==> !crashed(s); if (c) { assert !crashed(s); assert s.mem == end_mem; } else { var recovered := ghost_recover(s'); assert recovered.mem == init_mem; } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_lightening_verifier.dfy
328
328
Dafny program: 328
function exp(b: nat, n: nat): nat { if n == 0 then 1 else b * exp(b, n-1) } lemma exp_sum(b: nat, n1: nat, n2: nat) ensures exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { if n1 == 0 { return; } else { exp_sum(b, n1-1, n2); } } lemma exp_sum_auto(b: nat) ensures forall n1: nat, n2: nat :: exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { forall n1: nat, n2: nat ensures exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { exp_sum(b, n1, n2); } } function bits(n: nat): seq<bool> { if n == 0 then [] else [if (n % 2 == 0) then false else true] + bits(n/2) } function from_bits(s: seq<bool>): nat { if s == [] then 0 else (if s[0] then 1 else 0) + 2 * from_bits(s[1..]) } lemma bits_from_bits(n: nat) ensures from_bits(bits(n)) == n { } lemma bits_trim_front(n: nat) requires n > 0 ensures from_bits(bits(n)[1..]) == n/2 {} lemma from_bits_append(s: seq<bool>, b: bool) ensures from_bits(s + [b]) == from_bits(s) + exp(2, |s|) * (if b then 1 else 0) { if s == [] { return; } from_bits_append(s[1..], b); // from recursive call exp_sum(2, |s|-1, 1); } lemma from_bits_sum(s1: seq<bool>, s2: seq<bool>) ensures from_bits(s1 + s2) == from_bits(s1) + exp(2, |s1|) * from_bits(s2) { if s2 == [] { return; } from_bits_sum(s1 + [s2[0]], s2[1..]); from_bits_append(s1, s2[0]); assume false; // TODO } method fast_exp(b: nat, n: nat) returns (r: nat) ensures r == exp(b, n) { var a := 1; var c := b; ghost var n0 := n; var n := n; ghost var i: nat := 0; bits_from_bits(n); while n > 0 { ghost var n_loop_top := n; if n % 2 == 1 { // a accumulates sum(i => b^(2^n_i), i) where n_i are the 1 bits of n // TODO: n0-n is sum(i => 2^n_i, i), right? a := a * c; exp_sum(b, n0-n, i); // (n-1)/2 == n/2 in this case, but we want to be extra clear that we're // "dropping" a 1 bit here and so something interesting is happening n := (n-1) / 2; exp_sum_auto(b); } assume false; } else { n := n / 2; assume false; } c := c * c; exp_sum(b, exp(2, i), exp(2, i)); // assert bits(n0)[i+1..] == bits(n0)[i..][1..]; i := i + 1; } return a; }
function exp(b: nat, n: nat): nat { if n == 0 then 1 else b * exp(b, n-1) } lemma exp_sum(b: nat, n1: nat, n2: nat) ensures exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { if n1 == 0 { return; } else { exp_sum(b, n1-1, n2); } } lemma exp_sum_auto(b: nat) ensures forall n1: nat, n2: nat :: exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { forall n1: nat, n2: nat ensures exp(b, n1 + n2) == exp(b, n1) * exp(b, n2) { exp_sum(b, n1, n2); } } function bits(n: nat): seq<bool> decreases n { if n == 0 then [] else [if (n % 2 == 0) then false else true] + bits(n/2) } function from_bits(s: seq<bool>): nat { if s == [] then 0 else (if s[0] then 1 else 0) + 2 * from_bits(s[1..]) } lemma bits_from_bits(n: nat) ensures from_bits(bits(n)) == n { } lemma bits_trim_front(n: nat) requires n > 0 ensures from_bits(bits(n)[1..]) == n/2 {} lemma from_bits_append(s: seq<bool>, b: bool) ensures from_bits(s + [b]) == from_bits(s) + exp(2, |s|) * (if b then 1 else 0) { if s == [] { return; } assert s == [s[0]] + s[1..]; from_bits_append(s[1..], b); // from recursive call assert from_bits(s[1..] + [b]) == from_bits(s[1..]) + exp(2, |s|-1) * (if b then 1 else 0); exp_sum(2, |s|-1, 1); assert (s + [b])[1..] == s[1..] + [b]; // observe assert from_bits(s + [b]) == (if s[0] then 1 else 0) + 2 * from_bits(s[1..] + [b]); } lemma from_bits_sum(s1: seq<bool>, s2: seq<bool>) decreases s2 ensures from_bits(s1 + s2) == from_bits(s1) + exp(2, |s1|) * from_bits(s2) { if s2 == [] { assert s1 + s2 == s1; return; } from_bits_sum(s1 + [s2[0]], s2[1..]); assert s1 + [s2[0]] + s2[1..] == s1 + s2; from_bits_append(s1, s2[0]); assume false; // TODO } method fast_exp(b: nat, n: nat) returns (r: nat) ensures r == exp(b, n) { var a := 1; var c := b; ghost var n0 := n; var n := n; ghost var i: nat := 0; bits_from_bits(n); while n > 0 decreases n invariant c == exp(b, exp(2, i)) invariant n <= n0 invariant i <= |bits(n0)| invariant bits(n) == bits(n0)[i..] invariant n == from_bits(bits(n0)[i..]) invariant a == exp(b, from_bits(bits(n0)[..i])) { ghost var n_loop_top := n; if n % 2 == 1 { assert bits(n)[0] == true; // a accumulates sum(i => b^(2^n_i), i) where n_i are the 1 bits of n // TODO: n0-n is sum(i => 2^n_i, i), right? a := a * c; exp_sum(b, n0-n, i); // (n-1)/2 == n/2 in this case, but we want to be extra clear that we're // "dropping" a 1 bit here and so something interesting is happening n := (n-1) / 2; assert 2 * exp(2, i) == exp(2, i+1); assert a == exp(b, from_bits(bits(n0)[..i]) + exp(2, i)) by { exp_sum_auto(b); } assume false; assert a == exp(b, from_bits(bits(n0)[..i+1])); } else { assert bits(n)[0] == false; n := n / 2; assume false; assert a == exp(b, from_bits(bits(n0)[..i+1])); } assert n == n_loop_top/2; c := c * c; exp_sum(b, exp(2, i), exp(2, i)); // assert bits(n0)[i+1..] == bits(n0)[i..][1..]; i := i + 1; } assert bits(n0)[..i] == bits(n0); return a; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_mathematical objects verification_examples_fast_exp.dfy
329
329
Dafny program: 329
/* Here's a small but realistic setting where you could use Dafny. The setting is that we're implementing an interval library that manages a data structure with a low and a high value. It implements some computations on intervals, and we want to make sure those are right. */ // Interval is the Dafny model of the data structure itself. We're using `real` // here for the numbers; the specifics don't really matter, as long as we can // compare them with <. datatype Interval = Interval(lo: real, hi: real) // Contains is one of the core operations on intervals, both because we support // it in the API and because in some ways it defines what the interval means. predicate contains(i: Interval, r: real) { i.lo <= r <= i.hi } // We also provide a way to check if an interval is empty. predicate empty(i: Interval) { i.lo > i.hi } /* Now we can already do our first proof! Empty is a way to check if an interval * doesn't contain any numbers - let's prove that empty and contains agree with * each other. */ lemma empty_ok(i: Interval) // this is the sort of property that's easy to express logically but hard to test for ensures empty(i) <==> !exists r :: contains(i, r) { if empty(i) { } else { } } // min and max are just helper functions for the implementation function min(r1: real, r2: real): real { if r1 < r2 then r1 else r2 } function max(r1: real, r2: real): real { if r1 > r2 then r1 else r2 } /* The first complicated operation we expose is a function to intersect two * intervals. It's not so easy to think about whether this is correct - for * example, does it handle empty intervals correctly? Maybe two empty intervals * could intersect to a non-empty one? */ function intersect(i1: Interval, i2: Interval): Interval { Interval(max(i1.lo, i2.lo), min(i1.hi, i2.hi)) } // This theorem proves that intersect does exactly what we wanted it to, using // `contains` as the specification. lemma intersect_ok(i1: Interval, i2: Interval) ensures forall r :: contains(intersect(i1, i2), r) <==> contains(i1, r) && contains(i2, r) { } /* Next we'll define the union of intervals. This is more complicated because if * the intervals have no overlap, a single interval can't capture their union * exactly. */ // Intersect gives us an easy way to define overlap, and we already know it // handles empty intervals correctly. predicate overlap(i1: Interval, i2: Interval) { !empty(intersect(i1, i2)) } lemma overlap_ok(i1: Interval, i2: Interval) ensures overlap(i1, i2) <==> exists r :: contains(i1, r) && contains(i2, r) { if overlap(i1, i2) { if i1.lo >= i2.lo { } else { } } } // We'll give this function a precondition so that it always does the right thing. function union(i1: Interval, i2: Interval): Interval requires overlap(i1, i2) { Interval(min(i1.lo, i2.lo), max(i1.hi, i2.hi)) } // We can prove union correct in much the same way as intersect, with a similar // specification, although notice that now we require that the intervals // overlap. lemma union_ok(i1: Interval, i2: Interval) requires overlap(i1, i2) ensures forall r :: contains(union(i1, i2), r) <==> contains(i1, r) || contains(i2, r) { } // Though not used elsewhere here, if two intervals overlap its possible to show // that there's a common real contained in both of them. We also show off new // syntax: this lemma returns a value which is used in the postcondition, and // which the calling lemma can make use of. lemma overlap_witness(i1: Interval, i2: Interval) returns (r: real) requires overlap(i1, i2) ensures contains(i1, r) && contains(i2, r) { if i1.lo >= i2.lo { r := i1.lo; } else { r := i2.lo; } } /* One extension you might try is adding is an operation to check if an interval * is contained in another and proving that correct. Or, try implementing a * similar library for 2D rectangles. */
/* Here's a small but realistic setting where you could use Dafny. The setting is that we're implementing an interval library that manages a data structure with a low and a high value. It implements some computations on intervals, and we want to make sure those are right. */ // Interval is the Dafny model of the data structure itself. We're using `real` // here for the numbers; the specifics don't really matter, as long as we can // compare them with <. datatype Interval = Interval(lo: real, hi: real) // Contains is one of the core operations on intervals, both because we support // it in the API and because in some ways it defines what the interval means. predicate contains(i: Interval, r: real) { i.lo <= r <= i.hi } // We also provide a way to check if an interval is empty. predicate empty(i: Interval) { i.lo > i.hi } /* Now we can already do our first proof! Empty is a way to check if an interval * doesn't contain any numbers - let's prove that empty and contains agree with * each other. */ lemma empty_ok(i: Interval) // this is the sort of property that's easy to express logically but hard to test for ensures empty(i) <==> !exists r :: contains(i, r) { if empty(i) { } else { assert contains(i, i.lo); } } // min and max are just helper functions for the implementation function min(r1: real, r2: real): real { if r1 < r2 then r1 else r2 } function max(r1: real, r2: real): real { if r1 > r2 then r1 else r2 } /* The first complicated operation we expose is a function to intersect two * intervals. It's not so easy to think about whether this is correct - for * example, does it handle empty intervals correctly? Maybe two empty intervals * could intersect to a non-empty one? */ function intersect(i1: Interval, i2: Interval): Interval { Interval(max(i1.lo, i2.lo), min(i1.hi, i2.hi)) } // This theorem proves that intersect does exactly what we wanted it to, using // `contains` as the specification. lemma intersect_ok(i1: Interval, i2: Interval) ensures forall r :: contains(intersect(i1, i2), r) <==> contains(i1, r) && contains(i2, r) { } /* Next we'll define the union of intervals. This is more complicated because if * the intervals have no overlap, a single interval can't capture their union * exactly. */ // Intersect gives us an easy way to define overlap, and we already know it // handles empty intervals correctly. predicate overlap(i1: Interval, i2: Interval) { !empty(intersect(i1, i2)) } lemma overlap_ok(i1: Interval, i2: Interval) ensures overlap(i1, i2) <==> exists r :: contains(i1, r) && contains(i2, r) { if overlap(i1, i2) { if i1.lo >= i2.lo { assert contains(i2, i1.lo); } else { assert contains(i1, i2.lo); } } } // We'll give this function a precondition so that it always does the right thing. function union(i1: Interval, i2: Interval): Interval requires overlap(i1, i2) { Interval(min(i1.lo, i2.lo), max(i1.hi, i2.hi)) } // We can prove union correct in much the same way as intersect, with a similar // specification, although notice that now we require that the intervals // overlap. lemma union_ok(i1: Interval, i2: Interval) requires overlap(i1, i2) ensures forall r :: contains(union(i1, i2), r) <==> contains(i1, r) || contains(i2, r) { } // Though not used elsewhere here, if two intervals overlap its possible to show // that there's a common real contained in both of them. We also show off new // syntax: this lemma returns a value which is used in the postcondition, and // which the calling lemma can make use of. lemma overlap_witness(i1: Interval, i2: Interval) returns (r: real) requires overlap(i1, i2) ensures contains(i1, r) && contains(i2, r) { if i1.lo >= i2.lo { r := i1.lo; } else { r := i2.lo; } } /* One extension you might try is adding is an operation to check if an interval * is contained in another and proving that correct. Or, try implementing a * similar library for 2D rectangles. */
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_mathematical objects verification_examples_interval_example.dfy
332
332
Dafny program: 332
module Permutation { /** * Given n >= 0, generate a permuation of {0,...,n-1} nondeterministically. */ method Generate(n: int) returns (perm: array<int>) requires n >= 0 ensures perm != null ensures perm.Length == n ensures fresh(perm) ensures isValid(perm, n) { var all := set x | 0 <= x < n; var used := {}; perm := new int[n]; CardinalityLemma(n, all); while used < all { CardinalityOrderingLemma(used, all); var dst :| dst in all && dst !in used; perm[|used|] := dst; used := used + {dst}; } print perm; } predicate isValid(a: array<int>, n: nat) requires a != null && a.Length == n reads a { assume forall i | 0 <= i < n :: i in a[..]; distinct(a) && (forall i | 0 <= i < a.Length :: 0 <= a[i] < n) && (forall i | 0 <= i < n :: i in a[..]) } predicate distinct(a: array<int>) requires a != null reads a { distinct'(a, a.Length) } predicate distinct'(a: array<int>, n: int) requires a != null requires a.Length >= n reads a { forall i,j | 0 <= i < n && 0 <= j < n && i != j :: a[i] != a[j] } lemma CardinalityLemma (size: int, s: set<int>) requires size >= 0 requires s == set x | 0 <= x < size ensures size == |s| { if(size == 0) { } else { CardinalityLemma(size - 1, s - {size - 1}); } } lemma CardinalityOrderingLemma<T> (s1: set<T>, s2: set<T>) requires s1 < s2 ensures |s1| < |s2| { var e :| e in s2 - s1; if s1 != s2 - {e} { CardinalityOrderingLemma(s1, s2 - {e}); } } lemma SetDiffLemma<T> (s1: set<T>, s2: set<T>) requires s1 < s2 ensures s2 - s1 != {} { var e :| e in s2 - s1; if s2 - s1 != {e} {} // What does Dafny prove here??? } }
module Permutation { /** * Given n >= 0, generate a permuation of {0,...,n-1} nondeterministically. */ method Generate(n: int) returns (perm: array<int>) requires n >= 0 ensures perm != null ensures perm.Length == n ensures fresh(perm) ensures isValid(perm, n) { var all := set x | 0 <= x < n; var used := {}; perm := new int[n]; CardinalityLemma(n, all); while used < all invariant used <= all invariant |used| <= |all| invariant forall i | 0 <= i < |used| :: perm[i] in used invariant distinct'(perm, |used|) decreases |all| - |used| { CardinalityOrderingLemma(used, all); var dst :| dst in all && dst !in used; perm[|used|] := dst; used := used + {dst}; } assert used == all; print perm; } predicate isValid(a: array<int>, n: nat) requires a != null && a.Length == n reads a { assume forall i | 0 <= i < n :: i in a[..]; distinct(a) && (forall i | 0 <= i < a.Length :: 0 <= a[i] < n) && (forall i | 0 <= i < n :: i in a[..]) } predicate distinct(a: array<int>) requires a != null reads a { distinct'(a, a.Length) } predicate distinct'(a: array<int>, n: int) requires a != null requires a.Length >= n reads a { forall i,j | 0 <= i < n && 0 <= j < n && i != j :: a[i] != a[j] } lemma CardinalityLemma (size: int, s: set<int>) requires size >= 0 requires s == set x | 0 <= x < size ensures size == |s| { if(size == 0) { assert size == |(set x | 0 <= x < size)|; } else { CardinalityLemma(size - 1, s - {size - 1}); } } lemma CardinalityOrderingLemma<T> (s1: set<T>, s2: set<T>) requires s1 < s2 ensures |s1| < |s2| { var e :| e in s2 - s1; if s1 != s2 - {e} { CardinalityOrderingLemma(s1, s2 - {e}); } } lemma SetDiffLemma<T> (s1: set<T>, s2: set<T>) requires s1 < s2 ensures s2 - s1 != {} { var e :| e in s2 - s1; if s2 - s1 != {e} {} // What does Dafny prove here??? } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_pregel algorithms_skeleton_nondet-permutation.dfy
333
333
Dafny program: 333
// Assuming Array is Object Blood // Blood Array<int> // index method Find(blood: array<int>, key: int) returns (index: int) requires blood != null ensures 0 <= index ==> index < blood.Length && blood[index] == key ensures index < 0 ==> forall k :: 0 <= k < blood.Length ==> blood[k] != key { index := 0; while index < blood.Length { if blood[index] == key { return; } index := index + 1; } index := -1; }
// Assuming Array is Object Blood // Blood Array<int> // index method Find(blood: array<int>, key: int) returns (index: int) requires blood != null ensures 0 <= index ==> index < blood.Length && blood[index] == key ensures index < 0 ==> forall k :: 0 <= k < blood.Length ==> blood[k] != key { index := 0; while index < blood.Length invariant 0 <= index <= blood.Length invariant forall k :: 0 <= k < index ==> blood[k] != key { if blood[index] == key { return; } index := index + 1; } index := -1; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_vampire project_original_Searching.dfy
334
334
Dafny program: 334
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" method NinetyOne(x: int, ghost proveFunctionalPostcondition: bool) returns (z: int) ensures proveFunctionalPostcondition ==> z == if x > 101 then x-10 else 91; { var y1 := x; var y2 := 1; while (true) // the following two invariants are needed only to prove the postcondition // the following two lines justify termination, as in the paper by Katz and Manna { if (y1 > 100) { if (y2 == 1) { break; } else { y1 := y1 - 10; y2 := y2 - 1; } } else { y1 := y1 + 11; y2 := y2 + 1; } } z := y1 - 10; } method Gcd(x1: int, x2: int) requires 1 <= x1 && 1 <= x2; { var y1 := x1; var y2 := x2; while (y1 != y2) { while (y1 > y2) { y1 := y1 - y2; } while (y2 > y1) { y2 := y2 - y1; } } } method Determinant(X: array2<int>, M: int) returns (z: int) requires 1 <= M; requires X != null && M == X.Length0 && M == X.Length1; modifies X; { var y := X[1-1,1-1]; var a := 1; while (a != M) { var b := a + 1; while (b != M+1) { var c := M; while (c != a) { assume X[a-1,a-1] != 0; X[b-1, c-1] := X[b-1,c-1] - X[b-1,a-1] / X[a-1,a-1] * X[a-1,c-1]; c := c - 1; } b := b + 1; } a := a + 1; y := y * X[a-1,a-1]; } z := y; }
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" method NinetyOne(x: int, ghost proveFunctionalPostcondition: bool) returns (z: int) ensures proveFunctionalPostcondition ==> z == if x > 101 then x-10 else 91; { var y1 := x; var y2 := 1; while (true) // the following two invariants are needed only to prove the postcondition invariant proveFunctionalPostcondition ==> 100 < x ==> y1 == x; invariant proveFunctionalPostcondition ==> x <= 100 < y1 && y2 == 1 ==> y1 == 101; // the following two lines justify termination, as in the paper by Katz and Manna invariant (y1 <= 111 && y2 >= 1) || (y1 == x && y2 == 1); decreases -2*y1 + 21*y2 + 2*(if x < 111 then 111 else x); { if (y1 > 100) { if (y2 == 1) { break; } else { y1 := y1 - 10; y2 := y2 - 1; } } else { y1 := y1 + 11; y2 := y2 + 1; } } z := y1 - 10; } method Gcd(x1: int, x2: int) requires 1 <= x1 && 1 <= x2; { var y1 := x1; var y2 := x2; while (y1 != y2) invariant 1 <= y1 && 1 <= y2; decreases y1 + y2; { while (y1 > y2) invariant 1 <= y1 && 1 <= y2; { y1 := y1 - y2; } while (y2 > y1) invariant 1 <= y1 && 1 <= y2; { y2 := y2 - y1; } } } method Determinant(X: array2<int>, M: int) returns (z: int) requires 1 <= M; requires X != null && M == X.Length0 && M == X.Length1; modifies X; { var y := X[1-1,1-1]; var a := 1; while (a != M) invariant 1 <= a <= M; { var b := a + 1; while (b != M+1) invariant a+1 <= b <= M+1; { var c := M; while (c != a) invariant a <= c <= M; { assume X[a-1,a-1] != 0; X[b-1, c-1] := X[b-1,c-1] - X[b-1,a-1] / X[a-1,a-1] * X[a-1,c-1]; c := c - 1; } b := b + 1; } a := a + 1; y := y * X[a-1,a-1]; } z := y; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_variant examples_KatzManna.dfy
335
335
Dafny program: 335
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" class SumOfCubes { static function SumEmUp(n: int, m: int): int requires 0 <= n && n <= m; { if m == n then 0 else n*n*n + SumEmUp(n+1, m) } static method Socu(n: int, m: int) returns (r: int) requires 0 <= n && n <= m; ensures r == SumEmUp(n, m); { var a := SocuFromZero(m); var b := SocuFromZero(n); r := a - b; Lemma0(n, m); } static method SocuFromZero(k: int) returns (r: int) requires 0 <= k; ensures r == SumEmUp(0, k); { var g := Gauss(k); r := g * g; Lemma1(k); } ghost static method Lemma0(n: int, m: int) requires 0 <= n && n <= m; ensures SumEmUp(n, m) == SumEmUp(0, m) - SumEmUp(0, n); { var k := n; while (k < m) { k := k + 1; } Lemma3(0, n); Lemma3(n, k); Lemma3(0, k); } static function GSum(k: int): int requires 0 <= k; { if k == 0 then 0 else GSum(k-1) + k-1 } static method Gauss(k: int) returns (r: int) requires 0 <= k; ensures r == GSum(k); { r := k * (k - 1) / 2; Lemma2(k); } ghost static method Lemma1(k: int) requires 0 <= k; ensures SumEmUp(0, k) == GSum(k) * GSum(k); { var i := 0; while (i < k) { Lemma2(i); i := i + 1; } Lemma3(0, k); } ghost static method Lemma2(k: int) requires 0 <= k; ensures 2 * GSum(k) == k * (k - 1); { var i := 0; while (i < k) { i := i + 1; } } static function SumEmDown(n: int, m: int): int requires 0 <= n && n <= m; { if m == n then 0 else SumEmDown(n, m-1) + (m-1)*(m-1)*(m-1) } ghost static method Lemma3(n: int, m: int) requires 0 <= n && n <= m; ensures SumEmUp(n, m) == SumEmDown(n, m); { var k := n; while (k < m) { k := k + 1; } } }
// RUN: %dafny /compile:0 /dprint:"%t.dprint" "%s" > "%t" // RUN: %diff "%s.expect" "%t" class SumOfCubes { static function SumEmUp(n: int, m: int): int requires 0 <= n && n <= m; decreases m - n; { if m == n then 0 else n*n*n + SumEmUp(n+1, m) } static method Socu(n: int, m: int) returns (r: int) requires 0 <= n && n <= m; ensures r == SumEmUp(n, m); { var a := SocuFromZero(m); var b := SocuFromZero(n); r := a - b; Lemma0(n, m); } static method SocuFromZero(k: int) returns (r: int) requires 0 <= k; ensures r == SumEmUp(0, k); { var g := Gauss(k); r := g * g; Lemma1(k); } ghost static method Lemma0(n: int, m: int) requires 0 <= n && n <= m; ensures SumEmUp(n, m) == SumEmUp(0, m) - SumEmUp(0, n); { var k := n; while (k < m) invariant n <= k && k <= m; invariant SumEmDown(0, n) + SumEmDown(n, k) == SumEmDown(0, k); { k := k + 1; } Lemma3(0, n); Lemma3(n, k); Lemma3(0, k); } static function GSum(k: int): int requires 0 <= k; { if k == 0 then 0 else GSum(k-1) + k-1 } static method Gauss(k: int) returns (r: int) requires 0 <= k; ensures r == GSum(k); { r := k * (k - 1) / 2; Lemma2(k); } ghost static method Lemma1(k: int) requires 0 <= k; ensures SumEmUp(0, k) == GSum(k) * GSum(k); { var i := 0; while (i < k) invariant i <= k; invariant SumEmDown(0, i) == GSum(i) * GSum(i); { Lemma2(i); i := i + 1; } Lemma3(0, k); } ghost static method Lemma2(k: int) requires 0 <= k; ensures 2 * GSum(k) == k * (k - 1); { var i := 0; while (i < k) invariant i <= k; invariant 2 * GSum(i) == i * (i - 1); { i := i + 1; } } static function SumEmDown(n: int, m: int): int requires 0 <= n && n <= m; { if m == n then 0 else SumEmDown(n, m-1) + (m-1)*(m-1)*(m-1) } ghost static method Lemma3(n: int, m: int) requires 0 <= n && n <= m; ensures SumEmUp(n, m) == SumEmDown(n, m); { var k := n; while (k < m) invariant n <= k && k <= m; invariant SumEmUp(n, m) == SumEmDown(n, k) + SumEmUp(k, m); { k := k + 1; } } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_variant examples_SumOfCubes.dfy
336
336
Dafny program: 336
// This file demonstrates how to "close" a critical "gap" between definitions // between Dafny and Coq. // In general, most commonly-used "building blocks" in Coq can be mapped to Dafny: // [Coq] [Dafny] // -------------------------------------------------------------------- // Inductive (Set) datatype // Definition function/predicate // Fixpoint function/predicate (with `decreases`) // Theorem & Proof lemma // Type (Set, e.g. `list nat`) still a type (e.g. `seq<nat>`) // Type (Prop, e.g. `1+1==2`) encode in `requires` or `ensures` // N/A (at least NOT built-in) method (imperative programming) // // Inductive (Prop) ??? (discussed in this file) // Dafny's way to define Coq's `Fixpoint` predicate: ghost predicate even(n: nat) { match n { case 0 => true case 1 => false case _ => even(n - 2) } } // all below are automatically proved: lemma a0() ensures even(4) {} lemma a1() ensures !even(3) {} lemma a2(n: nat) requires even(n) ensures even(n + 2) {} lemma a3(n: nat) requires even(n + 2) ensures even(n) {} // Dafny lacks syntax to define `Inductive` Prop like in Coq. // We'll show two workarounds for this. // Workaround 1: simulate with "rules" datatype EvenRule = | ev_0 | ev_SS(r: EvenRule) { ghost function apply(): nat { match this { case ev_0 => 0 case ev_SS(r) => r.apply() + 2 } } } ghost predicate Even(n: nat) { exists r: EvenRule :: r.apply() == n } // then we can prove by "constructing" or "destructing" just like in Coq: lemma b0() ensures Even(4) { } lemma b1() ensures !Even(3) { if r: EvenRule :| r.apply() == 3 { } } lemma b2(n: nat) requires Even(n) ensures Even(n + 2) { var r: EvenRule :| r.apply() == n; } lemma b3(n: nat) requires Even(n + 2) ensures Even(n) { var r: EvenRule :| r.apply() == n + 2; } // Workaround 2: using "higher-order" predicates type P = nat -> bool ghost predicate Ev(ev: P) { && ev(0) && (forall n: nat | ev(n) :: ev(n + 2)) } // we explicitly say that `ev` is the "strictest" `P` that satisfies `Ev`: ghost predicate Minimal(Ev: P -> bool, ev: P) { && Ev(ev) && (forall ev': P, n: nat | Ev(ev') :: ev(n) ==> ev'(n)) } // In this approach, some lemmas are a bit tricky to prove... lemma c0(ev: P) requires Minimal(Ev, ev) ensures ev(4) { } lemma c1(ev: P) requires Minimal(Ev, ev) ensures !ev(3) { var cex := (n: nat) => ( // `cex` stands for "counterexample" n != 1 && n != 3 ); } lemma c2(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n) ensures ev(n + 2) {} lemma c3(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n + 2) ensures ev(n) { if !ev(n) { var cex := (m: nat) => ( m != n + 2 && ev(m) ); } } // Finally, we "circularly" prove the equivalence among these three: lemma a_implies_b(n: nat) requires even(n) ensures Even(n) { if n == 0 { } else { a_implies_b(n - 2); var r: EvenRule :| r.apply() == n - 2; } } lemma b_implies_c(ev: P, n: nat) requires Minimal(Ev, ev) && Even(n) ensures ev(n) { var r: EvenRule :| r.apply() == n; if r.ev_SS? { b_implies_c(ev, n - 2); } } lemma c_implies_a(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n) ensures even(n) { if n == 1 { var cex := (m: nat) => ( m != 1 ); } else if n >= 2 { c3(ev, n - 2); c_implies_a(ev, n - 2); } }
// This file demonstrates how to "close" a critical "gap" between definitions // between Dafny and Coq. // In general, most commonly-used "building blocks" in Coq can be mapped to Dafny: // [Coq] [Dafny] // -------------------------------------------------------------------- // Inductive (Set) datatype // Definition function/predicate // Fixpoint function/predicate (with `decreases`) // Theorem & Proof lemma // Type (Set, e.g. `list nat`) still a type (e.g. `seq<nat>`) // Type (Prop, e.g. `1+1==2`) encode in `requires` or `ensures` // N/A (at least NOT built-in) method (imperative programming) // // Inductive (Prop) ??? (discussed in this file) // Dafny's way to define Coq's `Fixpoint` predicate: ghost predicate even(n: nat) { match n { case 0 => true case 1 => false case _ => even(n - 2) } } // all below are automatically proved: lemma a0() ensures even(4) {} lemma a1() ensures !even(3) {} lemma a2(n: nat) requires even(n) ensures even(n + 2) {} lemma a3(n: nat) requires even(n + 2) ensures even(n) {} // Dafny lacks syntax to define `Inductive` Prop like in Coq. // We'll show two workarounds for this. // Workaround 1: simulate with "rules" datatype EvenRule = | ev_0 | ev_SS(r: EvenRule) { ghost function apply(): nat { match this { case ev_0 => 0 case ev_SS(r) => r.apply() + 2 } } } ghost predicate Even(n: nat) { exists r: EvenRule :: r.apply() == n } // then we can prove by "constructing" or "destructing" just like in Coq: lemma b0() ensures Even(4) { assert ev_SS(ev_SS(ev_0)).apply() == 4; } lemma b1() ensures !Even(3) { if r: EvenRule :| r.apply() == 3 { assert r.ev_SS? && r.r.apply() == 1; } } lemma b2(n: nat) requires Even(n) ensures Even(n + 2) { var r: EvenRule :| r.apply() == n; assert ev_SS(r).apply() == n + 2; } lemma b3(n: nat) requires Even(n + 2) ensures Even(n) { var r: EvenRule :| r.apply() == n + 2; assert r.ev_SS? && r.r.apply() == n; } // Workaround 2: using "higher-order" predicates type P = nat -> bool ghost predicate Ev(ev: P) { && ev(0) && (forall n: nat | ev(n) :: ev(n + 2)) } // we explicitly say that `ev` is the "strictest" `P` that satisfies `Ev`: ghost predicate Minimal(Ev: P -> bool, ev: P) { && Ev(ev) && (forall ev': P, n: nat | Ev(ev') :: ev(n) ==> ev'(n)) } // In this approach, some lemmas are a bit tricky to prove... lemma c0(ev: P) requires Minimal(Ev, ev) ensures ev(4) { assert ev(2); } lemma c1(ev: P) requires Minimal(Ev, ev) ensures !ev(3) { var cex := (n: nat) => ( // `cex` stands for "counterexample" n != 1 && n != 3 ); assert Ev(cex); } lemma c2(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n) ensures ev(n + 2) {} lemma c3(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n + 2) ensures ev(n) { if !ev(n) { var cex := (m: nat) => ( m != n + 2 && ev(m) ); assert Ev(cex); } } // Finally, we "circularly" prove the equivalence among these three: lemma a_implies_b(n: nat) requires even(n) ensures Even(n) { if n == 0 { assert ev_0.apply() == 0; } else { a_implies_b(n - 2); var r: EvenRule :| r.apply() == n - 2; assert ev_SS(r).apply() == n; } } lemma b_implies_c(ev: P, n: nat) requires Minimal(Ev, ev) && Even(n) ensures ev(n) { var r: EvenRule :| r.apply() == n; if r.ev_SS? { assert r.r.apply() == n - 2; b_implies_c(ev, n - 2); } } lemma c_implies_a(ev: P, n: nat) requires Minimal(Ev, ev) && ev(n) ensures even(n) { if n == 1 { var cex := (m: nat) => ( m != 1 ); assert Ev(cex); } else if n >= 2 { c3(ev, n - 2); c_implies_a(ev, n - 2); } }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_verified algorithms_inductive_props.dfy
337
337
Dafny program: 337
// By `lol sort` here, I refer to a seemingly-broken sorting algorithm, // which actually somehow manages to work perfectly: // // for i in 0..n // for j in 0..n // if i < j // swap a[i], a[j] // // It is perhaps the simpliest sorting algorithm to "memorize", // even "symmetrically beautiful" as if `i` and `j` just played highly // similar roles. And technically it's still O(n^2) time lol... // // Proving its correctness is tricky (interesting) though. // Successfully verified with [Dafny 3.3.0.31104] in about 5 seconds. // We define "valid permutation" using multiset: predicate valid_permut(a: seq<int>, b: seq<int>) requires |a| == |b| { multiset(a) == multiset(b) } // This is a swap-based sorting algorithm, so permutedness is trivial: // note that: if i == j, the spec just says a[..] remains the same. method swap(a: array<int>, i: int, j: int) requires 0 <= i < a.Length && 0 <= j < a.Length modifies a ensures a[..] == old(a[..]) [i := old(a[j])] [j := old(a[i])] ensures valid_permut(a[..], old(a[..])) { a[i], a[j] := a[j], a[i]; } // We then define "sorted" (by increasing order): predicate sorted(a: seq<int>) { forall i, j | 0 <= i <= j < |a| :: a[i] <= a[j] } // Now, the lol sort algorithm: // (Some invariants were tricky to find, but Dafny was smart enough otherwise) method lol_sort(a: array<int>) modifies a ensures valid_permut(a[..], old(a[..])) ensures sorted(a[..]) { for i := 0 to a.Length { for j := 0 to a.Length { if a[i] < a[j] { swap(a, i, j); } } } } method Main() { var a := new int[] [3,1,4,1,5,9,2,6]; lol_sort(a); print a[..]; // `expect` is a run-time assert, more suitable than `assert` on complicated testcases: expect a[..] == [1,1,2,3,4,5,6,9]; var empty := new int[] []; lol_sort(empty); }
// By `lol sort` here, I refer to a seemingly-broken sorting algorithm, // which actually somehow manages to work perfectly: // // for i in 0..n // for j in 0..n // if i < j // swap a[i], a[j] // // It is perhaps the simpliest sorting algorithm to "memorize", // even "symmetrically beautiful" as if `i` and `j` just played highly // similar roles. And technically it's still O(n^2) time lol... // // Proving its correctness is tricky (interesting) though. // Successfully verified with [Dafny 3.3.0.31104] in about 5 seconds. // We define "valid permutation" using multiset: predicate valid_permut(a: seq<int>, b: seq<int>) requires |a| == |b| { multiset(a) == multiset(b) } // This is a swap-based sorting algorithm, so permutedness is trivial: // note that: if i == j, the spec just says a[..] remains the same. method swap(a: array<int>, i: int, j: int) requires 0 <= i < a.Length && 0 <= j < a.Length modifies a ensures a[..] == old(a[..]) [i := old(a[j])] [j := old(a[i])] ensures valid_permut(a[..], old(a[..])) { a[i], a[j] := a[j], a[i]; } // We then define "sorted" (by increasing order): predicate sorted(a: seq<int>) { forall i, j | 0 <= i <= j < |a| :: a[i] <= a[j] } // Now, the lol sort algorithm: // (Some invariants were tricky to find, but Dafny was smart enough otherwise) method lol_sort(a: array<int>) modifies a ensures valid_permut(a[..], old(a[..])) ensures sorted(a[..]) { for i := 0 to a.Length invariant valid_permut(a[..], old(a[..])) invariant sorted(a[..i]) { for j := 0 to a.Length invariant valid_permut(a[..], old(a[..])) invariant j < i ==> forall k | 0 <= k < j :: a[k] <= a[i] invariant j < i ==> sorted(a[..i]) invariant j >= i ==> sorted(a[..i+1]) { if a[i] < a[j] { swap(a, i, j); } } } } method Main() { var a := new int[] [3,1,4,1,5,9,2,6]; lol_sort(a); print a[..]; // `expect` is a run-time assert, more suitable than `assert` on complicated testcases: expect a[..] == [1,1,2,3,4,5,6,9]; var empty := new int[] []; lol_sort(empty); assert empty[..] == []; }
Program-Verification-Dataset_tmp_tmpgbdrlnu__Dafny_verified algorithms_lol_sort.dfy
338
338
Dafny program: 338
//Problem 01 method sumOdds(n: nat) returns (sum: nat) requires n > 0; ensures sum == n * n; { sum := 1; var i := 0; while i < n-1 { i := i + 1; sum := sum + 2 * i + 1; } } //problem02 //a) method intDiv(n:int, d:int) returns (q:int, r:int) requires n >= d && n >= 0 && d > 0 ; ensures (d*q)+r == n && 0 <= q <= n/2 && 0 <= r < d; //b)c) method intDivImpl(n:int, d:int) returns (q:int, r:int) requires n >= d && n >= 0 && d > 0; ensures (d*q)+r == n && 0 <= q <= n/2 && 0 <= r < d; { q := 0; r := n; while r >= d r := r-1; { r := r - d; q := q + 1; } }
//Problem 01 method sumOdds(n: nat) returns (sum: nat) requires n > 0; ensures sum == n * n; { sum := 1; var i := 0; while i < n-1 invariant 0 <= i < n; invariant sum == (i + 1) * (i + 1); { i := i + 1; sum := sum + 2 * i + 1; } assert sum == n * n; } //problem02 //a) method intDiv(n:int, d:int) returns (q:int, r:int) requires n >= d && n >= 0 && d > 0 ; ensures (d*q)+r == n && 0 <= q <= n/2 && 0 <= r < d; //b)c) method intDivImpl(n:int, d:int) returns (q:int, r:int) requires n >= d && n >= 0 && d > 0; ensures (d*q)+r == n && 0 <= q <= n/2 && 0 <= r < d; { q := 0; r := n; while r >= d invariant r == n - q * d; invariant d <= r; r := r-1; { r := r - d; q := q + 1; } assert n == (d * q) + r; }
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_04_Hoangkim_ex_04_Hoangkim.dfy
339
339
Dafny program: 339
//Problem01 function fib(n: nat):nat { if n < 2 then n else fib(n-2)+fib(n-1) } method fibIter(n:nat) returns (a:nat) requires n > 0 ensures a == fib(n) { a := 0; var b,x := 1,0; while x < n { a,b := b,a+b; //why a,b := b,a+b is okay //but when I write a := b; //# Because this // b := a+b; //# is not the same !! //is error? //# {a = 1 , b = 2 } a := b ; b := a+b { b = 4 }, but x := x+1; //# {a = 1 , b = 2 } a, b := b,a+b { b = 3 } } } //# 2 pts //Problem02 function fact(n:nat):nat {if n==0 then 1 else n*fact(n-1)} method factIter(n:nat) returns (a:nat) requires n >= 0; ensures a == fact(n) { a := 1; var i := 1; while i <= n { a := a * i; i := i + 1; } } //# 3 pts //Problem03 function gcd(m: nat, n: nat): nat requires m > 0 && n > 0 { if m == n then m else if m > n then gcd(m - n, n) else gcd(m, n - m) } method gcdI(m: int, n: int) returns (g: int) requires m > 0 && n > 0 ensures g == gcd(m, n); { var x: int; g := m; x := n; while (g != x) { if (g > x) { g := g - x; } else { x := x - g; } } } //# 3 pts // # sum: 9 pts
//Problem01 function fib(n: nat):nat { if n < 2 then n else fib(n-2)+fib(n-1) } method fibIter(n:nat) returns (a:nat) requires n > 0 ensures a == fib(n) { a := 0; var b,x := 1,0; while x < n invariant 0 <= x <= n invariant a == fib(x) invariant b == fib(x+1) { a,b := b,a+b; //why a,b := b,a+b is okay //but when I write a := b; //# Because this // b := a+b; //# is not the same !! //is error? //# {a = 1 , b = 2 } a := b ; b := a+b { b = 4 }, but x := x+1; //# {a = 1 , b = 2 } a, b := b,a+b { b = 3 } } assert a == fib(n); } //# 2 pts //Problem02 function fact(n:nat):nat {if n==0 then 1 else n*fact(n-1)} method factIter(n:nat) returns (a:nat) requires n >= 0; ensures a == fact(n) { a := 1; var i := 1; while i <= n invariant 1 <= i <= n+1 invariant a == fact(i-1) { a := a * i; i := i + 1; } assert a == fact(n); } //# 3 pts //Problem03 function gcd(m: nat, n: nat): nat requires m > 0 && n > 0 { if m == n then m else if m > n then gcd(m - n, n) else gcd(m, n - m) } method gcdI(m: int, n: int) returns (g: int) requires m > 0 && n > 0 ensures g == gcd(m, n); { var x: int; g := m; x := n; while (g != x) invariant x > 0; invariant g > 0; invariant gcd(g, x) == gcd(m, n); decreases x+g; { if (g > x) { g := g - x; } else { x := x - g; } } } //# 3 pts // # sum: 9 pts
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_05_Hoangkim_ex_05_Hoangkim.dfy
340
340
Dafny program: 340
ghost function gcd(x:int,y:int):int requires x > 0 && y > 0 { if x==y then x else if x > y then gcd(x-y,y) else gcd(x,y-x) } method gcdI(m:int, n:int) returns (d:int) requires m > 0 && n > 0 ensures d == gcd(m,n) { var x,y := m,n; d := 1; while x != y { if x > y { x := x-y; } else { y := y-x; } } d := x; } ghost function gcd'(x:int,y:int):int requires x > 0 && y > 0 { if x==y then x else if x > y then gcd'(x-y,y) else gcd'(y,x) }
ghost function gcd(x:int,y:int):int requires x > 0 && y > 0 { if x==y then x else if x > y then gcd(x-y,y) else gcd(x,y-x) } method gcdI(m:int, n:int) returns (d:int) requires m > 0 && n > 0 ensures d == gcd(m,n) { var x,y := m,n; d := 1; while x != y decreases x+y invariant x > 0 && y > 0 invariant gcd(x,y) == gcd(m,n) { if x > y { x := x-y; } else { y := y-x; } } d := x; } ghost function gcd'(x:int,y:int):int requires x > 0 && y > 0 decreases x+y,y // x+y decreases or x+y remains unchanged while y decreases { if x==y then x else if x > y then gcd'(x-y,y) else gcd'(y,x) }
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_06_Hoangkim_ex06-solution.dfy
341
341
Dafny program: 341
//Problem01 //a) ghost function gcd(x: int, y: int): int requires x > 0 && y > 0 { if x == y then x else if x > y then gcd(x - y, y) else gcd(x, y - x) } method gcdI(m: int, n: int) returns (d: int) requires m > 0 && n > 0 ensures d == gcd(m, n); { var x: int; d := m; x := n; while (d != x) { if (d > x) { d := d - x; } else { x := x - d; } } } //b) ghost function gcd'(x: int, y: int): int requires x > 0 && y > 0 { if x == y then x else if x > y then gcd'(x - y, y) else gcd(y, x) }
//Problem01 //a) ghost function gcd(x: int, y: int): int requires x > 0 && y > 0 { if x == y then x else if x > y then gcd(x - y, y) else gcd(x, y - x) } method gcdI(m: int, n: int) returns (d: int) requires m > 0 && n > 0 ensures d == gcd(m, n); { var x: int; d := m; x := n; while (d != x) invariant x > 0; invariant d > 0; invariant gcd(d, x) == gcd(m, n); decreases x+d; { if (d > x) { d := d - x; } else { x := x - d; } } } //b) ghost function gcd'(x: int, y: int): int requires x > 0 && y > 0 decreases if x > y then x else y { if x == y then x else if x > y then gcd'(x - y, y) else gcd(y, x) }
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_06_Hoangkim_ex_06_hoangkim.dfy
342
342
Dafny program: 342
//Problem01 //a) method swap(a: array<int>, i: nat, j: nat) modifies a requires a != null && a.Length > 0 && i < a.Length && j < a.Length ensures a[i] == old(a[j]) ensures a[j] == old(a[i]) { a[i], a[j] := a[j], a[i]; } //b) //Problem04 method FindMin(a: array<int>, lo: nat) returns (minIdx: nat) requires a != null && a.Length > 0 && lo < a.Length ensures lo <= minIdx < a.Length ensures forall x :: lo <= x < a.Length ==> a[minIdx] <= a[x] { var j := lo; minIdx := lo; while j < a.Length { if(a[j] < a[minIdx]) { minIdx := j; } j := j + 1; } } //Problem02 ghost predicate sorted(a:seq<int>) { forall i | 0 < i < |a| :: a[i-1] <= a[i] } method selectionSort(a: array<int>) modifies a //ensures multiset(a[..]) == multiset(old(a[..])) //ensures sorted(a[..]) { var i := 0; while(i < a.Length) { var mx := FindMin(a, i); //swap(a,i,mx); a[i], a[mx] := a[mx], a[i]; i := i + 1; } } //Problem03
//Problem01 //a) method swap(a: array<int>, i: nat, j: nat) modifies a requires a != null && a.Length > 0 && i < a.Length && j < a.Length ensures a[i] == old(a[j]) ensures a[j] == old(a[i]) { a[i], a[j] := a[j], a[i]; } //b) //Problem04 method FindMin(a: array<int>, lo: nat) returns (minIdx: nat) requires a != null && a.Length > 0 && lo < a.Length ensures lo <= minIdx < a.Length ensures forall x :: lo <= x < a.Length ==> a[minIdx] <= a[x] { var j := lo; minIdx := lo; while j < a.Length invariant lo <= j <= a.Length invariant lo <= minIdx < a.Length invariant forall k :: lo <= k < j ==> a[k] >= a[minIdx] decreases a.Length - j { if(a[j] < a[minIdx]) { minIdx := j; } j := j + 1; } } //Problem02 ghost predicate sorted(a:seq<int>) { forall i | 0 < i < |a| :: a[i-1] <= a[i] } method selectionSort(a: array<int>) modifies a //ensures multiset(a[..]) == multiset(old(a[..])) //ensures sorted(a[..]) { var i := 0; while(i < a.Length) invariant 0 <= i <= a.Length invariant forall k, l :: 0 <= k < i <= l < a.Length ==> a[k] <= a[l] invariant sorted(a[..i]) { var mx := FindMin(a, i); //swap(a,i,mx); a[i], a[mx] := a[mx], a[i]; i := i + 1; } } //Problem03
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_07_Hoangkim_ex07_Hoangkim.dfy
343
343
Dafny program: 343
//Problem01 method square0(n:nat) returns (sqn : nat) ensures sqn == n*n { sqn := 0; var i:= 0; var x; while i < n { x := 2*i+1; sqn := sqn+x; i := i+1; } } /* 3 Verification conditions 1. VC1: Precondiotion implies the loop variant n ∈ ℕ => sqn = 0*0 ∧ i = 0 ∧ x=? ∧ i≤n n >= 0 => 0 = 0*0 ∧ i = 0 ∧ i≤n n >= 0 => 0 = 0*0 ∧ 0 ≤ n 2. VC2: Loop invariant and loop guard preserve the loop invariant. VC2: i < n ∧ i+1 ≤ n ∧ sqn = i * i ⇒ sqn = sqn + x ∧ i = i + 1 ∧ x = 2 * i + 1 3.VC3: Loop terminates, and the loop invariant implies the postcondition. VC3: ¬(i < n) ∧ i ≤ n ∧ sqn = i * i ⇒ sqn = n * n Simplified VC for square0 1. true, since 0 = 0 and n >= 0 => 0 ≤ n 2. true, i < n => i + 1 <= n 3. true, ¬(i < n) ∧ i ≤ n ∧ sqn = i * i ⇒ sqn = n * n since ¬(i < n) ∧ i ≤ n imply i = n */ method square1(n:nat) returns (sqn : nat) ensures sqn == n*n { sqn := 0; var i:= 0; while i < n { var x := 2*i+1; sqn := sqn+x; i := i+1; } } //Problem02 //As you can see below, Dafny claims that after executing the following method //strange() we will have that 1=2; method q(x:nat, y:nat) returns (z:nat) requires y - x > 2 ensures x < z*z < y method strange() ensures 1==2 { var x := 4; var c:nat := q(x,2*x); } /*(a). Do you have an explanation for this behaviour? Answer: the method strange() doesn't have any input or output. This method initializes variable x with value 4. Then it calculates variable c as a result of calling method 'q' with x as first var and 2*x as second var.the strange method does not specify any postcondition. Therefore, we cannot make any assumptions about the behavior or the value of c after calling q. We can change ensures in strange() to false and it's still verified */ /*(b) {true}var x:nat := 4; var c := q(x,2*x); {1 = 2 } precond in strange(): difference between 'y' and 'x' muss be greater than 2, square from 'z' will be a value between 'x' and 'y' apply the Hoare rules step by step: 1. {true} as a precondition 2. we assign 4 to 'x' and having {4=4} 3. assign value q(x, 2 * x) to c, substitute the postcondition of 'q' in place of 'c' post cond of q will be x < z*z < 2*x. Replacing c we having {x < z * z < 2 * x} 4. we having the statement {x < z*z < 2*x} => {1 = 2} as postcondtion as we know the statment {1 = 2} is always false. true => false is always false */ //Problem 3 //Use what you know about the weakest preconditions/strongest postconditions to ex- //plain why the following code verifies: method test0(){ var x:int := *; assume x*x < 100; } /* WP: is a condition that, if satisfied before the execution of a program, guarantees the satisfaction of a specified postcondition SP: is a condition that must hold after the execution of a program, assuming a specified precondition The strongest postcondition for assert is x<=9 Analyze the code: The strongest postcondition for the assert statement assert x <= 9; is x <= 9. This postcondition asserts that the value of x should be less than or equal to 9 after the execution of the program. To ensure this postcondition, we need to find a weakest precondition (WP) that guarantees x <= 9 after executing the code. The "assume" statement introduces a precondition. It assumes that the square of x is less than 100. In other words, it assumes that x is within the range (0, 10) since the largest possible square less than 100 is 9 * 9 = 81. */
//Problem01 method square0(n:nat) returns (sqn : nat) ensures sqn == n*n { sqn := 0; var i:= 0; var x; while i < n invariant i <= n && sqn == i*i { x := 2*i+1; sqn := sqn+x; i := i+1; } } /* 3 Verification conditions 1. VC1: Precondiotion implies the loop variant n ∈ ℕ => sqn = 0*0 ∧ i = 0 ∧ x=? ∧ i≤n n >= 0 => 0 = 0*0 ∧ i = 0 ∧ i≤n n >= 0 => 0 = 0*0 ∧ 0 ≤ n 2. VC2: Loop invariant and loop guard preserve the loop invariant. VC2: i < n ∧ i+1 ≤ n ∧ sqn = i * i ⇒ sqn = sqn + x ∧ i = i + 1 ∧ x = 2 * i + 1 3.VC3: Loop terminates, and the loop invariant implies the postcondition. VC3: ¬(i < n) ∧ i ≤ n ∧ sqn = i * i ⇒ sqn = n * n Simplified VC for square0 1. true, since 0 = 0 and n >= 0 => 0 ≤ n 2. true, i < n => i + 1 <= n 3. true, ¬(i < n) ∧ i ≤ n ∧ sqn = i * i ⇒ sqn = n * n since ¬(i < n) ∧ i ≤ n imply i = n */ method square1(n:nat) returns (sqn : nat) ensures sqn == n*n { sqn := 0; var i:= 0; while i < n invariant i <= n && sqn == i*i { var x := 2*i+1; sqn := sqn+x; i := i+1; } } //Problem02 //As you can see below, Dafny claims that after executing the following method //strange() we will have that 1=2; method q(x:nat, y:nat) returns (z:nat) requires y - x > 2 ensures x < z*z < y method strange() ensures 1==2 { var x := 4; var c:nat := q(x,2*x); } /*(a). Do you have an explanation for this behaviour? Answer: the method strange() doesn't have any input or output. This method initializes variable x with value 4. Then it calculates variable c as a result of calling method 'q' with x as first var and 2*x as second var.the strange method does not specify any postcondition. Therefore, we cannot make any assumptions about the behavior or the value of c after calling q. We can change ensures in strange() to false and it's still verified */ /*(b) {true}var x:nat := 4; var c := q(x,2*x); {1 = 2 } precond in strange(): difference between 'y' and 'x' muss be greater than 2, square from 'z' will be a value between 'x' and 'y' apply the Hoare rules step by step: 1. {true} as a precondition 2. we assign 4 to 'x' and having {4=4} 3. assign value q(x, 2 * x) to c, substitute the postcondition of 'q' in place of 'c' post cond of q will be x < z*z < 2*x. Replacing c we having {x < z * z < 2 * x} 4. we having the statement {x < z*z < 2*x} => {1 = 2} as postcondtion as we know the statment {1 = 2} is always false. true => false is always false */ //Problem 3 //Use what you know about the weakest preconditions/strongest postconditions to ex- //plain why the following code verifies: method test0(){ var x:int := *; assume x*x < 100; assert x<= 9; } /* WP: is a condition that, if satisfied before the execution of a program, guarantees the satisfaction of a specified postcondition SP: is a condition that must hold after the execution of a program, assuming a specified precondition The strongest postcondition for assert is x<=9 Analyze the code: The strongest postcondition for the assert statement assert x <= 9; is x <= 9. This postcondition asserts that the value of x should be less than or equal to 9 after the execution of the program. To ensure this postcondition, we need to find a weakest precondition (WP) that guarantees x <= 9 after executing the code. The "assume" statement introduces a precondition. It assumes that the square of x is less than 100. In other words, it assumes that x is within the range (0, 10) since the largest possible square less than 100 is 9 * 9 = 81. */
Programmverifikation-und-synthese_tmp_tmppurk6ime_PVS_Assignment_ex_10_Hoangkim_ex10_hoangkim.dfy
344
344
Dafny program: 344
// **************************************************************************************** // DafnyIntro.dfy // **************************************************************************************** // We write a program to sum all numbers from 1 to n // // Gauss' formula states that 1 + 2 + 3 + ... + (n-1) + n == n*(n+1)/2 // // We take this a specification, thus in effect we use Dafny to prove Gauss' formula: // In essence Dafny does an inductive proof. It needs help with a loop "invariant". // This is a condition which is // - true at the beginning of the loop // - maintained with each passage through the loop body. // These requirements correspond to an inductive proof // - the invariant is the inductive hypothesis H(i) // - it must be true for i=0 // - it must remain true when stepping from i to i+1, // Here we use two invariants I1 and I2, which amounts to the same as using I1 && I2: method Gauss(n:int) returns (sum:int) requires n >= 0 ensures sum == n*(n+1)/2 // { sum := 0; var i := 0; while i < n { i := i+1; sum := sum + i; } } // As a second example, we add the first n odd numbers // This yields n*n, i.e. // // 1 + 3 + 5 + 7 + 9 + 11 + ... 2n+1 == n*n // // Here is the proof using Dafny: method sumOdds(n:nat) returns (sum:nat) ensures sum == n*n; { sum := 0; var i := 0; while i < n { sum := sum + 2*i+1; i := i+1; // the step from i to i+1 } } // This verifies, so the proof is complete !!
// **************************************************************************************** // DafnyIntro.dfy // **************************************************************************************** // We write a program to sum all numbers from 1 to n // // Gauss' formula states that 1 + 2 + 3 + ... + (n-1) + n == n*(n+1)/2 // // We take this a specification, thus in effect we use Dafny to prove Gauss' formula: // In essence Dafny does an inductive proof. It needs help with a loop "invariant". // This is a condition which is // - true at the beginning of the loop // - maintained with each passage through the loop body. // These requirements correspond to an inductive proof // - the invariant is the inductive hypothesis H(i) // - it must be true for i=0 // - it must remain true when stepping from i to i+1, // Here we use two invariants I1 and I2, which amounts to the same as using I1 && I2: method Gauss(n:int) returns (sum:int) requires n >= 0 ensures sum == n*(n+1)/2 // { sum := 0; var i := 0; while i < n invariant sum == i*(i+1)/2 invariant i <= n { i := i+1; sum := sum + i; } } // As a second example, we add the first n odd numbers // This yields n*n, i.e. // // 1 + 3 + 5 + 7 + 9 + 11 + ... 2n+1 == n*n // // Here is the proof using Dafny: method sumOdds(n:nat) returns (sum:nat) ensures sum == n*n; { sum := 0; var i := 0; while i < n invariant sum == i*i // the inductive hypothesis invariant i <= n { sum := sum + 2*i+1; i := i+1; // the step from i to i+1 } } // This verifies, so the proof is complete !!
Programmverifikation-und-synthese_tmp_tmppurk6ime_example_DafnyIntro_01_Simple_Loops.dfy
345
345
Dafny program: 345
/** CVS 2021-22 Handout 1 Authors Gonçalo Martins Lourenço nº55780 Joana Soares Faria nº55754 */ // First Exercise lemma peasantMultLemma(a:int, b:int) requires b >= 0 ensures b % 2 == 0 ==> (a * b == 2 * a * b / 2) ensures b % 2 == 1 ==> (a * b == a + 2 * a * (b - 1) / 2) { if (b % 2 == 0 && b > 0) { peasantMultLemma(a, b - 2); } if (b % 2 == 1 && b > 1) { peasantMultLemma(a, b - 2); } } method peasantMult(a: int, b: int) returns (r: int) requires b > 0 ensures r == a * b { r := 0; var aa := a; var bb := b; while(bb > 0) { // Use of lemma was not necessary for a successful verification // peasantMultLemma(aa, bb); if (bb % 2 == 0) { aa := 2 * aa; bb := bb / 2; } else if (bb % 2 == 1) { r := r + aa; aa := 2 * aa; bb := (bb-1) / 2; } } } //Second Exercise method euclidianDiv(a: int,b : int) returns (q: int,r: int) requires a >= 0 requires b > 0 ensures a == b * q + r { r := a; q := 0; while(r - b >= 0) // invariant a == b * q + r { r := r - b; q := q + 1; } }
/** CVS 2021-22 Handout 1 Authors Gonçalo Martins Lourenço nº55780 Joana Soares Faria nº55754 */ // First Exercise lemma peasantMultLemma(a:int, b:int) requires b >= 0 ensures b % 2 == 0 ==> (a * b == 2 * a * b / 2) ensures b % 2 == 1 ==> (a * b == a + 2 * a * (b - 1) / 2) { if (b % 2 == 0 && b > 0) { peasantMultLemma(a, b - 2); } if (b % 2 == 1 && b > 1) { peasantMultLemma(a, b - 2); } } method peasantMult(a: int, b: int) returns (r: int) requires b > 0 ensures r == a * b { r := 0; var aa := a; var bb := b; while(bb > 0) decreases bb invariant 0 <= bb <= b invariant r + aa * bb == a * b { // Use of lemma was not necessary for a successful verification // peasantMultLemma(aa, bb); if (bb % 2 == 0) { aa := 2 * aa; bb := bb / 2; } else if (bb % 2 == 1) { r := r + aa; aa := 2 * aa; bb := (bb-1) / 2; } } } //Second Exercise method euclidianDiv(a: int,b : int) returns (q: int,r: int) requires a >= 0 requires b > 0 ensures a == b * q + r { r := a; q := 0; while(r - b >= 0) decreases r - b invariant 0 <= r <= a // invariant a == b * q + r invariant r == a - b * q { r := r - b; q := q + 1; } }
ProjectosCVS_tmp_tmp_02_gmcw_Handout 1_CVS_handout1_55754_55780.dfy
346
346
Dafny program: 346
function sorted(s : seq<int>) : bool { forall k1, k2 :: 0 <= k1 <= k2 < |s| ==> s[k1] <= s[k2] } // Ex1 method copyArr(a : array<int>, l : int, r : int) returns (ret : array<int>) requires 0 <= l < r <= a.Length ensures ret[..] == a[l..r] { var size := r - l; ret := new int[size]; var i := 0; while(i < size) { ret[i] := a[i + l]; i := i + 1; } return; } // Ex2 method mergeArr(a : array<int>, l : int, m : int, r : int) requires 0 <= l < m < r <= a.Length requires sorted(a[l..m]) && sorted(a[m..r]) ensures sorted(a[l..r]) ensures a[..l] == old(a[..l]) ensures a[r..] == old(a[r..]) modifies a { var left := copyArr(a, l, m); var right := copyArr(a, m, r); var i := 0; var j := 0; var cur := l; ghost var old_arr := a[..]; while(cur < r) { if((i == left.Length && j < right.Length) || (j != right.Length && left[i] > right[j])) { a[cur] := right[j]; j := j + 1; } else if((j == right.Length && i < left.Length) || (i != left.Length && left[i] <= right[j])) { a[cur] := left[i]; i := i + 1; } cur := cur + 1; } return; } // Ex3 method sort(a : array<int>) ensures sorted(a[..]) modifies a { if(a.Length == 0) { return; } else { sortAux(a, 0, a.Length); } } method sortAux(a : array<int>, l : int, r : int) ensures sorted(a[l..r]) ensures a[..l] == old(a[..l]) ensures a[r..] == old(a[r..]) requires 0 <= l < r <= a.Length modifies a { if(l >= (r - 1)) {return;} else { var m := l + (r - l) / 2; sortAux(a, l, m); sortAux(a, m, r); mergeArr(a, l, m, r); return; } }
function sorted(s : seq<int>) : bool { forall k1, k2 :: 0 <= k1 <= k2 < |s| ==> s[k1] <= s[k2] } // Ex1 method copyArr(a : array<int>, l : int, r : int) returns (ret : array<int>) requires 0 <= l < r <= a.Length ensures ret[..] == a[l..r] { var size := r - l; ret := new int[size]; var i := 0; while(i < size) invariant a[..] == old(a[..]) invariant 0 <= i <= size invariant ret[..i] == a[l..(l + i)] decreases size - i { ret[i] := a[i + l]; i := i + 1; } return; } // Ex2 method mergeArr(a : array<int>, l : int, m : int, r : int) requires 0 <= l < m < r <= a.Length requires sorted(a[l..m]) && sorted(a[m..r]) ensures sorted(a[l..r]) ensures a[..l] == old(a[..l]) ensures a[r..] == old(a[r..]) modifies a { var left := copyArr(a, l, m); var right := copyArr(a, m, r); var i := 0; var j := 0; var cur := l; ghost var old_arr := a[..]; while(cur < r) decreases a.Length - cur invariant 0 <= i <= left.Length invariant 0 <= j <= right.Length invariant l <= cur <= r invariant cur == i + j + l invariant a[..l] == old_arr[..l] invariant a[r..] == old_arr[r..] invariant sorted(a[l..cur]) invariant sorted(left[..]) invariant sorted(right[..]) invariant i < left.Length && cur > l ==> a[cur - 1] <= left[i] invariant j < right.Length && cur > l ==> a[cur - 1] <= right[j] { if((i == left.Length && j < right.Length) || (j != right.Length && left[i] > right[j])) { a[cur] := right[j]; j := j + 1; } else if((j == right.Length && i < left.Length) || (i != left.Length && left[i] <= right[j])) { a[cur] := left[i]; i := i + 1; } cur := cur + 1; } return; } // Ex3 method sort(a : array<int>) ensures sorted(a[..]) modifies a { if(a.Length == 0) { return; } else { sortAux(a, 0, a.Length); } } method sortAux(a : array<int>, l : int, r : int) ensures sorted(a[l..r]) ensures a[..l] == old(a[..l]) ensures a[r..] == old(a[r..]) requires 0 <= l < r <= a.Length modifies a decreases r - l { if(l >= (r - 1)) {return;} else { var m := l + (r - l) / 2; sortAux(a, l, m); sortAux(a, m, r); mergeArr(a, l, m, r); return; } }
QS_BoilerPlate1_tmp_tmpa29vtz9__Ex2.dfy
348
348
Dafny program: 348
// successfully verifies method GetEven(a: array<nat>) requires true; ensures forall i:int :: 0<=i<a.Length ==> a[i] % 2 == 0 modifies a { var i := 0; while i < a.Length { if a[i] % 2 != 0 { a[i] := a[i] + 1; } i := i + 1; } }
// successfully verifies method GetEven(a: array<nat>) requires true; ensures forall i:int :: 0<=i<a.Length ==> a[i] % 2 == 0 modifies a { var i := 0; while i < a.Length invariant 0 <= i <= a.Length && forall j:int :: 0<=j<i ==> a[j] % 2 == 0 decreases a.Length - i { if a[i] % 2 != 0 { a[i] := a[i] + 1; } i := i + 1; } }
SENG2011_tmp_tmpgk5jq85q_ass1_ex8.dfy
351
351
Dafny program: 351
// verifies // all bs are before all as which are before all ds predicate sortedbad(s:string) { // all b's are before all a's and d's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'b' && (s[j] == 'a' || s[j] == 'd') ==> i < j && // all a's are after all b's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'a' && s[j] == 'b' ==> i > j && // all a's are before all d's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'a' && s[j] == 'd' ==> i < j && // all d's are after a;; b's and a's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'd' && (s[j] == 'a' || s[j] == 'b') ==> i > j } method BadSort(a: string) returns (b: string) requires forall k :: 0 <= k < |a| ==> a[k] == 'b' || a[k] == 'a' || a[k] == 'd'; ensures sortedbad(b); ensures multiset(a[..]) == multiset(b[..]); ensures |a| == |b|; { b := a; var next := 0; var white := 0; var blue := |b|; // colours between next and blue unsorted while (next != blue) // if next==blue, no colours left to sort // ensure next, white, blue are correct // all b's are before all a's and d's // all a's are after all b's // all a's are before all d's // all d's are after a;; b's and a's { if b[next] == 'b' { var tmp := b[next]; b := b[next := b[white]]; b := b[white := tmp]; next := next + 1; white := white + 1; } else if b[next] == 'a' { next := next + 1; } else if b[next] == 'd'{ blue := blue - 1; var tmp := b[next]; b := b[next := b[blue]]; b := b[blue := tmp]; } } } method check() { var f:string := "dabdabdab"; var g:string := BadSort(f); /* f := "dba"; // testcase1 g := BadSort(f); f := "aaaaaaaa"; // testcase 2 g := BadSort(f); */ /* var a:string := "dabdabdab"; var b:string := BadSort(a); // apparently not possible ot verify this */ }
// verifies // all bs are before all as which are before all ds predicate sortedbad(s:string) { // all b's are before all a's and d's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'b' && (s[j] == 'a' || s[j] == 'd') ==> i < j && // all a's are after all b's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'a' && s[j] == 'b' ==> i > j && // all a's are before all d's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'a' && s[j] == 'd' ==> i < j && // all d's are after a;; b's and a's forall i,j :: 0 <= i < |s| && 0 <= j < |s| && s[i] == 'd' && (s[j] == 'a' || s[j] == 'b') ==> i > j } method BadSort(a: string) returns (b: string) requires forall k :: 0 <= k < |a| ==> a[k] == 'b' || a[k] == 'a' || a[k] == 'd'; ensures sortedbad(b); ensures multiset(a[..]) == multiset(b[..]); ensures |a| == |b|; { b := a; var next := 0; var white := 0; var blue := |b|; // colours between next and blue unsorted while (next != blue) // if next==blue, no colours left to sort invariant forall k :: 0 <= k < |b| ==> b[k] == 'b' || b[k] == 'a' || b[k] == 'd'; invariant 0 <= white <= next <= blue <= |b|; // ensure next, white, blue are correct invariant forall i :: 0 <= i < white ==> b[i] == 'b'; invariant forall i :: white <= i < next ==> b[i] == 'a'; invariant forall i :: blue <= i < |b| ==> b[i] == 'd'; // all b's are before all a's and d's invariant forall i,j :: 0 <= i < next && 0 <= j < next && b[i] == 'b' && (b[j] == 'a' || b[j] == 'd') ==> i < j // all a's are after all b's invariant forall i,j :: 0 <= i < next && 0 <= j < next && b[i] == 'a' && b[j] == 'b' ==> i > j // all a's are before all d's invariant forall i,j :: 0 <= i < next && 0 <= j < next && b[i] == 'a' && b[j] == 'd' ==> i < j // all d's are after a;; b's and a's invariant forall i,j :: 0 <= i < next && 0 <= j < next && b[i] == 'd' && (b[j] == 'a' || b[j] == 'b') ==> i > j invariant multiset(b[..]) == multiset(a[..]); invariant |a| == |b|; { if b[next] == 'b' { var tmp := b[next]; b := b[next := b[white]]; b := b[white := tmp]; next := next + 1; white := white + 1; } else if b[next] == 'a' { next := next + 1; } else if b[next] == 'd'{ blue := blue - 1; var tmp := b[next]; b := b[next := b[blue]]; b := b[blue := tmp]; } } } method check() { var f:string := "dabdabdab"; var g:string := BadSort(f); assert multiset(f)==multiset(g); assert sortedbad(g); /* f := "dba"; // testcase1 g := BadSort(f); assert g=="bad"; f := "aaaaaaaa"; // testcase 2 g := BadSort(f); assert g=="aaaaaaaa"; */ /* var a:string := "dabdabdab"; var b:string := BadSort(a); assert multiset(a) == multiset(b); assert b == "bbbaaaddd"; // apparently not possible ot verify this */ }
SENG2011_tmp_tmpgk5jq85q_ass2_ex3.dfy
352
352
Dafny program: 352
// verifies function expo(x:int, n:nat): int requires n >= 0; { if (n == 0) then 1 else x * expo(x, n - 1) } lemma {:induction false} Expon23(n: nat) requires n >= 0; ensures ((expo(2, 3 * n) - expo(3, n))) % 5 == 0; { if (n == 0) { } else if (n == 1) { } else { var i:nat := n; var j:nat := n; // assume true for n // prove for n - 1 Expon23(n - 1); //assert expo(2, 2 + 3) == expo(2, 2) * expo(2, 3); //assert expo(2, i - 2) == expo(2, i) / expo(2, 2); //assert expo(2, i - 3) == expo(2, i) / expo(2, 3); // training } } method check() { }
// verifies function expo(x:int, n:nat): int requires n >= 0; { if (n == 0) then 1 else x * expo(x, n - 1) } lemma {:induction false} Expon23(n: nat) requires n >= 0; ensures ((expo(2, 3 * n) - expo(3, n))) % 5 == 0; { if (n == 0) { assert (expo(2, 3 * 0) - expo(3, 0)) % 5 == 0; } else if (n == 1) { assert (expo(2, 3 * 1) - expo(3, 1)) % 5 == 0; } else { var i:nat := n; var j:nat := n; // assume true for n // prove for n - 1 Expon23(n - 1); assert (expo(2, 3 * (i - 1)) - expo(3, i - 1)) % 5 == 0; assert (expo(2, (3 * i) - 3) - expo(3, (n - 1))) % 5 == 0; //assert expo(2, 2 + 3) == expo(2, 2) * expo(2, 3); assert expo(2, i - 0) == expo(2, i); assert expo(2, i - 1) == expo(2, i) / expo(2, 1); //assert expo(2, i - 2) == expo(2, i) / expo(2, 2); //assert expo(2, i - 3) == expo(2, i) / expo(2, 3); // training assert expo(2, (1 * i) - 0) == expo(2, (1 * i)); assert expo(2, (2 * i) - 1) == expo(2, (2 * i)) / expo(2, 1); assert expo(2, (3 * 1) - 3) == expo(2, (3 * 1)) / expo(2, 3); assert expo(2, (3 * i) - 0) == expo(2, (3 * i)); assert expo(2, (3 * i) - 1) == expo(2, (3 * i)) / expo(2, 1); assert expo(2, (3 * i) - 2) == expo(2, (3 * i)) / expo(2, 2); assert expo(2, (3 * i) - 3) == expo(2, (3 * i)) / expo(2, 3); assert expo(3, (i - 1)) == expo(3, i) / expo (3, 1); assert expo(2, (3 * i) - 3) - expo(3, (i - 1)) == expo(2, (3 * i)) / expo(2,3) - expo(3, i) / expo (3, 1); assert expo(2, 3) % 5 == expo(3, 1); assert (expo(2, (3 * i)) * 6) % 5 == expo(2, (3 * i)) % 5; assert (expo(2, (3 * i)) * expo(2, 3)) % 5 == (expo(2, (3 * i)) * expo(3, 1)) % 5; assert (expo(2, (3 * i)) * expo(2,3) - expo(3, i) * expo (3, 1)) % 5 == (expo(2, (3 * i)) * expo(3, 1) - expo(3, i) * expo (3, 1)) % 5; assert (expo(2, (3 * i)) * expo(3, 1) - expo(3, i) * expo (3, 1)) % 5 == (expo(3, 1) * (expo(2, (3 * i)) - expo(3, i))) % 5; assert (expo(2, (3 * i)) - expo(3, i)) % 5 == 0; } } method check() { assert expo(2, 3) == 8; assert expo(-2, 3) == -8; assert expo(3, 0) == 1; assert expo(0, 0) == 1; assert expo(10, 2) == 100; }
SENG2011_tmp_tmpgk5jq85q_ass2_ex5.dfy
353
353
Dafny program: 353
method Getmini(a:array<int>) returns(mini:nat) requires a.Length > 0 ensures 0 <= mini < a.Length // mini is an index of a ensures forall x :: 0 <= x < a.Length ==> a[mini] <= a[x] // a[mini] is the minimum value ensures forall x :: 0 <= x < mini ==> a[mini] < a[x] // a[mini] is the first min { // find mini var min:int := a[0]; var i:int := 0; while i < a.Length { if a[i] < min { min := a[i]; } i := i + 1; } //assert min in a[..]; // min is in a -> it will be found by this loop // find first occurance var k:int := 0; while k < a.Length { if a[k] == min { return k; } k := k + 1; } } /* method check() { var data := new int[][9,5,42,5,5]; // minimum 5 first at index 1 var mini := Getmini(data); //print mini; } */
method Getmini(a:array<int>) returns(mini:nat) requires a.Length > 0 ensures 0 <= mini < a.Length // mini is an index of a ensures forall x :: 0 <= x < a.Length ==> a[mini] <= a[x] // a[mini] is the minimum value ensures forall x :: 0 <= x < mini ==> a[mini] < a[x] // a[mini] is the first min { // find mini var min:int := a[0]; var i:int := 0; while i < a.Length invariant 0 <= i <= a.Length invariant forall x :: 0 <= x < i ==> min <= a[x] // min is the smallest so far invariant min in a[..] // min is always in a { if a[i] < min { min := a[i]; } i := i + 1; } //assert min in a[..]; // min is in a -> it will be found by this loop // find first occurance var k:int := 0; while k < a.Length invariant 0 <= k <= a.Length invariant forall x :: 0 <= x < k ==> min < a[x] { if a[k] == min { return k; } k := k + 1; } } /* method check() { var data := new int[][9,5,42,5,5]; // minimum 5 first at index 1 var mini := Getmini(data); //print mini; assert mini==1; } */
SENG2011_tmp_tmpgk5jq85q_exam_ex2.dfy
354
354
Dafny program: 354
method Symmetric(a: array<int>) returns (flag: bool) ensures flag == true ==> forall x :: 0 <= x < a.Length ==> a[x] == a[a.Length - x - 1] ensures flag == false ==> exists x :: 0 <= x < a.Length && a[x] != a[a.Length - x - 1] { // empty == symmetrical if a.Length == 0 { return true; } var i:int := 0; while i < a.Length { if a[i] != a[a.Length - i - 1] { return false; } i := i + 1; } return true; } /* method Main() { var data1 := new int[][1,2,3,2,1]; var f1 := Symmetric(data1); var data2 := new int[][1,2]; var f2 := Symmetric(data2); //print f2; } */
method Symmetric(a: array<int>) returns (flag: bool) ensures flag == true ==> forall x :: 0 <= x < a.Length ==> a[x] == a[a.Length - x - 1] ensures flag == false ==> exists x :: 0 <= x < a.Length && a[x] != a[a.Length - x - 1] { // empty == symmetrical if a.Length == 0 { return true; } var i:int := 0; while i < a.Length invariant 0 <= i <= a.Length // probably only need to check to halfway but this works as well invariant forall x :: 0 <= x < i ==> a[x] == a[a.Length - x - 1] { if a[i] != a[a.Length - i - 1] { return false; } i := i + 1; } return true; } /* method Main() { var data1 := new int[][1,2,3,2,1]; var f1 := Symmetric(data1); assert f1; var data2 := new int[][1,2]; var f2 := Symmetric(data2); assert !f2; //print f2; } */
SENG2011_tmp_tmpgk5jq85q_exam_ex3.dfy
355
355
Dafny program: 355
lemma {:induction false} Divby2(n: nat) ensures (n*(n-1))%2 == 0 { if n == 0 { } else { Divby2(n - 1); // proved in case n - 1 } }
lemma {:induction false} Divby2(n: nat) ensures (n*(n-1))%2 == 0 { if n == 0 { assert (1*(1-1))%2 == 0; // base case } else { Divby2(n - 1); // proved in case n - 1 assert (n-1)*(n-2) == n*n -3*n + 2; // expanded case n - 1 } }
SENG2011_tmp_tmpgk5jq85q_exam_ex4.dfy
356
356
Dafny program: 356
// sums from index 0 -> i - 1 function sumcheck(s: array<int>, i: int): int requires 0 <= i <= s.Length reads s { if i == 0 then 0 else s[i - 1] + sumcheck(s, i - 1) } // returns sum of array method sum(s: array<int>) returns (a:int) requires s.Length > 0 ensures sumcheck(s, s.Length) == a { a := 0; var i:int := 0; while i < s.Length { a := a + s[i]; i := i + 1; } } method Main() { var a: array<int> := new int[4]; a[0] := 1; a[1] := 3; a[2] := 3; a[3] := 2; var s:= sum(a); print "\nThe sum of all elements in [1,3,3,2] is "; print s; }
// sums from index 0 -> i - 1 function sumcheck(s: array<int>, i: int): int requires 0 <= i <= s.Length reads s { if i == 0 then 0 else s[i - 1] + sumcheck(s, i - 1) } // returns sum of array method sum(s: array<int>) returns (a:int) requires s.Length > 0 ensures sumcheck(s, s.Length) == a { a := 0; var i:int := 0; while i < s.Length invariant 0 <= i <= s.Length && a == sumcheck(s, i) { a := a + s[i]; i := i + 1; } } method Main() { var a: array<int> := new int[4]; a[0] := 1; a[1] := 3; a[2] := 3; a[3] := 2; assert a[..] == [1,3,3,2]; var s:= sum(a); assert a[0] == 1 && a[1] == 3 && a[2] == 3 && a[3] == 2; assert s == sumcheck(a, a.Length); print "\nThe sum of all elements in [1,3,3,2] is "; print s; }
SENG2011_tmp_tmpgk5jq85q_flex_ex1.dfy
357
357
Dafny program: 357
function maxcheck(s: array<nat>, i: int, max: int): int requires 0 <= i <= s.Length reads s { if i == 0 then max else if s[i - 1] > max then maxcheck(s, i - 1, s[i - 1]) else maxcheck(s, i - 1, max) } method max(s: array<nat>) returns (a:int) requires s.Length > 0 ensures forall x :: 0 <= x < s.Length ==> a >= s[x] ensures a in s[..] { a := s[0]; var i:int := 0; while i < s.Length { if (s[i] > a) { a := s[i]; } i := i + 1; } } method Checker() { var a := new nat[][1,2,3,50,5,51]; // ghost var a := [1,2,3]; var n := max(a); // assert a[..] == [1,2,3]; // assert MAXIMUM(1,2) == 2; // assert ret_max(a,a.Length-1) == 12; // assert ret_max(a,a.Length-1) == x+3; }
function maxcheck(s: array<nat>, i: int, max: int): int requires 0 <= i <= s.Length reads s { if i == 0 then max else if s[i - 1] > max then maxcheck(s, i - 1, s[i - 1]) else maxcheck(s, i - 1, max) } method max(s: array<nat>) returns (a:int) requires s.Length > 0 ensures forall x :: 0 <= x < s.Length ==> a >= s[x] ensures a in s[..] { a := s[0]; var i:int := 0; while i < s.Length invariant 0 <= i <= s.Length invariant forall x :: 0 <= x < i ==> a >= s[x] invariant a in s[..] { if (s[i] > a) { a := s[i]; } i := i + 1; } } method Checker() { var a := new nat[][1,2,3,50,5,51]; // ghost var a := [1,2,3]; var n := max(a); // assert a[..] == [1,2,3]; assert n == 51; // assert MAXIMUM(1,2) == 2; // assert ret_max(a,a.Length-1) == 12; // assert ret_max(a,a.Length-1) == x+3; }
SENG2011_tmp_tmpgk5jq85q_flex_ex2.dfy
358
358
Dafny program: 358
method firste(a: array<char>) returns (c:int) ensures -1 <= c < a.Length ensures 0 <= c < a.Length ==> a[c] == 'e' && forall x :: 0 <= x < c ==> a[x] != 'e' ensures c == -1 ==> forall x :: 0 <= x < a.Length ==> a[x] != 'e' { var i:int := 0; while i < a.Length { if a[i] == 'e' { return i; } i := i + 1; } return -1; } method Main(){ var a := new char[6]['c','h','e','e','s','e']; var p := firste(a); print p; //assert p == 2; }
method firste(a: array<char>) returns (c:int) ensures -1 <= c < a.Length ensures 0 <= c < a.Length ==> a[c] == 'e' && forall x :: 0 <= x < c ==> a[x] != 'e' ensures c == -1 ==> forall x :: 0 <= x < a.Length ==> a[x] != 'e' { var i:int := 0; while i < a.Length invariant 0 <= i <= a.Length invariant forall x :: 0 <= x < i ==> a[x] != 'e' { if a[i] == 'e' { return i; } i := i + 1; } return -1; } method Main(){ var a := new char[6]['c','h','e','e','s','e']; var p := firste(a); print p; //assert p == 2; }
SENG2011_tmp_tmpgk5jq85q_flex_ex5.dfy
359
359
Dafny program: 359
method Reverse(a: array<char>) returns (b: array<char>) requires a.Length > 0 ensures a.Length == b.Length ensures forall x :: 0 <= x < a.Length ==> b[x] == a[a.Length - x - 1] { // copy array a to new array b b := new char[a.Length]; var k := 0; while (k < a.Length) { b[k] := a[a.Length - 1 - k]; k := k + 1; } /* var i:int := 0; while i < a.Length //invariant multiset(a[..]) == multiset(b[..]) { b[i] := a[a.Length - 1 - i]; i := i + 1; } */ } method Main() { var a := new char[8]; a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] := 'd', 'e', 's', 'r', 'e', 'v', 'e', 'r'; var b := Reverse(a); print b[..]; a := new char[1]; a[0] := '!'; b := Reverse(a); print b[..], '\n'; }
method Reverse(a: array<char>) returns (b: array<char>) requires a.Length > 0 ensures a.Length == b.Length ensures forall x :: 0 <= x < a.Length ==> b[x] == a[a.Length - x - 1] { // copy array a to new array b b := new char[a.Length]; var k := 0; while (k < a.Length) invariant 0 <= k <= a.Length; invariant forall x :: 0 <= x < k ==> b[x] == a[a.Length - x - 1] decreases a.Length - k { b[k] := a[a.Length - 1 - k]; k := k + 1; } /* var i:int := 0; while i < a.Length invariant a.Length == b.Length invariant 0 <= i <= a.Length invariant 0 <= i <= b.Length //invariant multiset(a[..]) == multiset(b[..]) invariant forall x :: 0 <= x < i ==> b[x] == a[a.Length - x - 1] decreases a.Length - i { b[i] := a[a.Length - 1 - i]; i := i + 1; } */ } method Main() { var a := new char[8]; a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7] := 'd', 'e', 's', 'r', 'e', 'v', 'e', 'r'; var b := Reverse(a); assert b[..] == [ 'r', 'e', 'v', 'e', 'r', 's', 'e', 'd' ]; print b[..]; a := new char[1]; a[0] := '!'; b := Reverse(a); assert b[..] == [ '!' ]; print b[..], '\n'; }
SENG2011_tmp_tmpgk5jq85q_p1.dfy
360
360
Dafny program: 360
method AbsIt(s: array<int>) modifies s; //requires ensures forall x :: 0 <= x < s.Length ==> old(s[x]) < 0 ==> s[x] == -old(s[x]) ensures forall x :: 0 <= x < s.Length ==> old(s[x]) >= 0 ==> s[x] == old(s[x]) { var i:int := 0; while i < s.Length //invariant forall x :: 0 <= x < i ==> s[x] >= 0 //invariant forall x :: 0 <= x < i ==> old(s[x]) < 0 ==> s[x] == -old(s[x]) //invariant forall x :: 0 <= x < i ==> old(s[x]) >= 0 ==> s[x] == old(s[x]) { if (s[i] < 0) { s[i] := -s[i]; } i := i + 1; } }
method AbsIt(s: array<int>) modifies s; //requires ensures forall x :: 0 <= x < s.Length ==> old(s[x]) < 0 ==> s[x] == -old(s[x]) ensures forall x :: 0 <= x < s.Length ==> old(s[x]) >= 0 ==> s[x] == old(s[x]) { var i:int := 0; while i < s.Length invariant 0 <= i <= s.Length //invariant forall x :: 0 <= x < i ==> s[x] >= 0 //invariant forall x :: 0 <= x < i ==> old(s[x]) < 0 ==> s[x] == -old(s[x]) //invariant forall x :: 0 <= x < i ==> old(s[x]) >= 0 ==> s[x] == old(s[x]) invariant forall k :: 0 <= k < i ==> old(s[k]) < 0 ==> s[k] == -old(s[k])// negatives are abs'ed invariant forall k :: 0 <= k < i ==> old(s[k]) >= 0 ==> s[k] == old(s[k]) // positives left alone invariant forall k:: i <= k < s.Length ==> old(s[k]) == s[k] // not yet touched { if (s[i] < 0) { s[i] := -s[i]; } i := i + 1; } }
SENG2011_tmp_tmpgk5jq85q_p2.dfy
361
361
Dafny program: 361
module Utils { lemma AllBelowBoundSize(bound: nat) ensures var below := set n : nat | n < bound :: n; |below| == bound { if bound == 0 { } else { AllBelowBoundSize(bound-1); var belowminus := set n : nat | n < bound-1 :: n; var below := set n : nat | n < bound :: n; } } lemma SizeOfContainedSet(a: set<nat>, b: set<nat>) requires forall n: nat :: n in a ==> n in b ensures |a| <= |b| { if |a| == 0 { } else { var y :| y in a; var new_a := a - {y}; var new_b := b - {y}; SizeOfContainedSet(new_a, new_b); } } lemma BoundedSetSize(bound: nat, values: set<nat>) requires forall n :: n in values ==> n < bound ensures |values| <= bound { var all_below_bound := set n : nat | n < bound :: n; AllBelowBoundSize(bound); SizeOfContainedSet(values, all_below_bound); } lemma MappedSetSize<T, U>(s: set<T>, f: T->U, t: set<U>) requires forall n: T, m: T :: m != n ==> f(n) != f(m) requires t == set n | n in s :: f(n) ensures |s| == |t| { var t := set n | n in s :: f(n); if |s| == 0 { } else { var y :| y in s; var new_s := s - {y}; var new_t := t - {f(y)}; MappedSetSize(new_s, f, new_t); } } lemma SetSizes<T>(a: set<T>, b: set<T>, c: set<T>) requires c == a + b requires forall t: T :: t in a ==> t !in b requires forall t: T :: t in b ==> t !in a ensures |c| == |a| + |b| { } }
module Utils { lemma AllBelowBoundSize(bound: nat) ensures var below := set n : nat | n < bound :: n; |below| == bound decreases bound { if bound == 0 { } else { AllBelowBoundSize(bound-1); var belowminus := set n : nat | n < bound-1 :: n; var below := set n : nat | n < bound :: n; assert below == belowminus + {bound-1}; } } lemma SizeOfContainedSet(a: set<nat>, b: set<nat>) requires forall n: nat :: n in a ==> n in b ensures |a| <= |b| decreases |a| { if |a| == 0 { } else { var y :| y in a; var new_a := a - {y}; var new_b := b - {y}; SizeOfContainedSet(new_a, new_b); } } lemma BoundedSetSize(bound: nat, values: set<nat>) requires forall n :: n in values ==> n < bound ensures |values| <= bound { var all_below_bound := set n : nat | n < bound :: n; AllBelowBoundSize(bound); assert |all_below_bound| == bound; assert forall n :: n in values ==> n in all_below_bound; SizeOfContainedSet(values, all_below_bound); } lemma MappedSetSize<T, U>(s: set<T>, f: T->U, t: set<U>) requires forall n: T, m: T :: m != n ==> f(n) != f(m) requires t == set n | n in s :: f(n) ensures |s| == |t| decreases |s| { var t := set n | n in s :: f(n); if |s| == 0 { } else { var y :| y in s; var new_s := s - {y}; var new_t := t - {f(y)}; assert new_t == set n | n in new_s :: f(n); MappedSetSize(new_s, f, new_t); } } lemma SetSizes<T>(a: set<T>, b: set<T>, c: set<T>) requires c == a + b requires forall t: T :: t in a ==> t !in b requires forall t: T :: t in b ==> t !in a ensures |c| == |a| + |b| { } }
SiLemma_tmp_tmpfxtryv2w_utils.dfy
363
363
Dafny program: 363
method best_time_to_buy_and_sell_stock(prices: array<int>) returns (max_profit: int) requires 1 <= prices.Length <= 100000 requires forall i :: 0 <= i < prices.Length ==> 0 <= prices[i] <= 10000 ensures forall i, j :: 0 <= i < j < prices.Length ==> max_profit >= prices[j] - prices[i] { var min_price := 10001; max_profit := 0; var i := 0; while (i < prices.Length) { var price := prices[i]; if (price < min_price) { min_price := price; } if (price - min_price > max_profit) { max_profit := price - min_price; } i := i + 1; } }
method best_time_to_buy_and_sell_stock(prices: array<int>) returns (max_profit: int) requires 1 <= prices.Length <= 100000 requires forall i :: 0 <= i < prices.Length ==> 0 <= prices[i] <= 10000 ensures forall i, j :: 0 <= i < j < prices.Length ==> max_profit >= prices[j] - prices[i] { var min_price := 10001; max_profit := 0; var i := 0; while (i < prices.Length) invariant 0 <= i <= prices.Length invariant forall j :: 0 <= j < i ==> min_price <= prices[j] invariant forall j, k :: 0 <= j < k < i ==> max_profit >= prices[k] - prices[j] { var price := prices[i]; if (price < min_price) { min_price := price; } if (price - min_price > max_profit) { max_profit := price - min_price; } i := i + 1; } }
Software-Verification_tmp_tmpv4ueky2d_Best Time to Buy and Sell Stock_best_time_to_buy_and_sell_stock.dfy
364
364
Dafny program: 364
method contains_duplicate(nums: seq<int>) returns (result: bool) requires 1 <= |nums| <= 100000 requires forall i :: 0 <= i < |nums| ==> -1000000000 <= nums[i] <= 1000000000 ensures result <==> distinct(nums) { var i := 0; var s: set<int> := {}; while (i < |nums|) { var num := nums[i]; if (num in s) { return false; } s := s + {num}; i := i + 1; } return true; } predicate distinct(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] != nums[j] }
method contains_duplicate(nums: seq<int>) returns (result: bool) requires 1 <= |nums| <= 100000 requires forall i :: 0 <= i < |nums| ==> -1000000000 <= nums[i] <= 1000000000 ensures result <==> distinct(nums) { var i := 0; var s: set<int> := {}; while (i < |nums|) invariant i <= |nums| invariant forall j :: j in nums[..i] <==> j in s invariant distinct(nums[..i]) { var num := nums[i]; if (num in s) { return false; } s := s + {num}; i := i + 1; } return true; } predicate distinct(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] != nums[j] }
Software-Verification_tmp_tmpv4ueky2d_Contains Duplicate_contains_duplicate.dfy
365
365
Dafny program: 365
method counting_bits(n: int) returns (result: array<int>) requires 0 <= n <= 100000 ensures result.Length == n + 1 ensures forall i :: 1 <= i < n + 1 ==> result[i] == result[i / 2] + i % 2 { result := new int[n + 1](i => 0); var i := 1; while (i < n + 1) { result[i] := result[i / 2] + i % 2; i := i + 1; } }
method counting_bits(n: int) returns (result: array<int>) requires 0 <= n <= 100000 ensures result.Length == n + 1 ensures forall i :: 1 <= i < n + 1 ==> result[i] == result[i / 2] + i % 2 { result := new int[n + 1](i => 0); var i := 1; while (i < n + 1) invariant 1 <= i <= n + 1 invariant forall j :: 1 <= j < i ==> result[j] == result[j / 2] + j % 2 { result[i] := result[i / 2] + i % 2; i := i + 1; } }
Software-Verification_tmp_tmpv4ueky2d_Counting Bits_counting_bits.dfy
366
366
Dafny program: 366
method longest_increasing_subsequence(nums: array<int>) returns (max: int) requires 1 <= nums.Length <= 2500 requires forall i :: 0 <= i < nums.Length ==> -10000 <= nums[i] <= 10000 // TODO: modify the ensures clause so that max is indeed equal to the longest increasing subsequence ensures max >= 1 { var length := nums.Length; if (length == 1) { return 1; } max := 1; var dp := new int[length](_ => 1); var i := 1; while (i < length) modifies dp { var j := 0; while (j < i) { if (nums[j] < nums[i]) { dp[i] := find_max(dp[i], dp[j] + 1); } j := j + 1; } max := find_max(max, dp[i]); i := i + 1; } } // Function function find_max(x: int, y: int): int { if x > y then x else y }
method longest_increasing_subsequence(nums: array<int>) returns (max: int) requires 1 <= nums.Length <= 2500 requires forall i :: 0 <= i < nums.Length ==> -10000 <= nums[i] <= 10000 // TODO: modify the ensures clause so that max is indeed equal to the longest increasing subsequence ensures max >= 1 { var length := nums.Length; if (length == 1) { return 1; } max := 1; var dp := new int[length](_ => 1); var i := 1; while (i < length) modifies dp invariant 1 <= i <= length invariant max >= 1 { var j := 0; while (j < i) invariant 0 <= j <= i { if (nums[j] < nums[i]) { dp[i] := find_max(dp[i], dp[j] + 1); } j := j + 1; } max := find_max(max, dp[i]); i := i + 1; } } // Function function find_max(x: int, y: int): int { if x > y then x else y }
Software-Verification_tmp_tmpv4ueky2d_Longest Increasing Subsequence_longest_increasing_subsequence.dfy
367
367
Dafny program: 367
method non_overlapping_intervals(intervals: array2<int>) returns (count: int) modifies intervals requires 1 <= intervals.Length0 <= 100000 requires intervals.Length1 == 2 requires forall i :: 0 <= i < intervals.Length0 ==> -50000 <= intervals[i, 0] <= 50000 requires forall i :: 0 <= i < intervals.Length0 ==> -50000 <= intervals[i, 1] <= 50000 // TODO: modify the ensures clause so that count is indeed equal to the minimum number of intervals we need to remove to make the rest of the intervals non-overlapping. ensures count >= 0 { var row := intervals.Length0; if (row == 0) { return 0; } bubble_sort(intervals); var i := 1; count := 1; var end := intervals[0, 1]; while (i < row) { if (intervals[i, 0] >= end) { count := count + 1; end := intervals[i, 1]; } i := i + 1; } return row - count; } // Bubble Sort method bubble_sort(a: array2<int>) modifies a requires a.Length1 == 2 ensures sorted(a, 0, a.Length0 - 1) { var i := a.Length0 - 1; while (i > 0) { var j := 0; while (j < i) { if (a[j, 1] > a[j + 1, 1]) { a[j, 1], a[j + 1, 1] := a[j + 1, 1], a[j, 1]; } j := j + 1; } i := i -1; } } // Predicates for Bubble Sort predicate sorted(a: array2<int>, l: int, u: int) reads a requires a.Length1 == 2 { forall i, j :: 0 <= l <= i <= j <= u < a.Length0 ==> a[i, 1] <= a[j, 1] } predicate partitioned(a: array2<int>, i: int) reads a requires a.Length1 == 2 { forall k, k' :: 0 <= k <= i < k' < a.Length0 ==> a[k, 1] <= a[k', 1] }
method non_overlapping_intervals(intervals: array2<int>) returns (count: int) modifies intervals requires 1 <= intervals.Length0 <= 100000 requires intervals.Length1 == 2 requires forall i :: 0 <= i < intervals.Length0 ==> -50000 <= intervals[i, 0] <= 50000 requires forall i :: 0 <= i < intervals.Length0 ==> -50000 <= intervals[i, 1] <= 50000 // TODO: modify the ensures clause so that count is indeed equal to the minimum number of intervals we need to remove to make the rest of the intervals non-overlapping. ensures count >= 0 { var row := intervals.Length0; if (row == 0) { return 0; } bubble_sort(intervals); var i := 1; count := 1; var end := intervals[0, 1]; while (i < row) invariant 1 <= i <= row invariant 1 <= count <= i invariant intervals[0, 1] <= end <= intervals[i - 1, 1] { if (intervals[i, 0] >= end) { count := count + 1; end := intervals[i, 1]; } i := i + 1; } return row - count; } // Bubble Sort method bubble_sort(a: array2<int>) modifies a requires a.Length1 == 2 ensures sorted(a, 0, a.Length0 - 1) { var i := a.Length0 - 1; while (i > 0) invariant i < 0 ==> a.Length0 == 0 invariant sorted(a, i, a.Length0 - 1) invariant partitioned(a, i) { var j := 0; while (j < i) invariant 0 < i < a.Length0 && 0 <= j <= i invariant sorted(a, i, a.Length0 - 1) invariant partitioned(a, i) invariant forall k :: 0 <= k <= j ==> a[k, 1] <= a[j, 1] { if (a[j, 1] > a[j + 1, 1]) { a[j, 1], a[j + 1, 1] := a[j + 1, 1], a[j, 1]; } j := j + 1; } i := i -1; } } // Predicates for Bubble Sort predicate sorted(a: array2<int>, l: int, u: int) reads a requires a.Length1 == 2 { forall i, j :: 0 <= l <= i <= j <= u < a.Length0 ==> a[i, 1] <= a[j, 1] } predicate partitioned(a: array2<int>, i: int) reads a requires a.Length1 == 2 { forall k, k' :: 0 <= k <= i < k' < a.Length0 ==> a[k, 1] <= a[k', 1] }
Software-Verification_tmp_tmpv4ueky2d_Non-overlapping Intervals_non_overlapping_intervals.dfy
368
368
Dafny program: 368
method remove_duplicates_from_sorted_array(nums: seq<int>) returns (result: seq<int>) requires is_sorted(nums) requires 1 <= |nums| <= 30000 requires forall i :: 0 <= i < |nums| ==> -100 <= nums[i] <= 100 ensures is_sorted_and_distinct(result) ensures forall i :: i in nums <==> i in result { var previous := nums[0]; result := [nums[0]]; var i := 1; while (i < |nums|) { if (previous != nums[i]) { result := result + [nums[i]]; previous := nums[i]; } i := i + 1; } } // Helper predicate predicate is_sorted(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] <= nums[j] } predicate is_sorted_and_distinct(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] < nums[j] }
method remove_duplicates_from_sorted_array(nums: seq<int>) returns (result: seq<int>) requires is_sorted(nums) requires 1 <= |nums| <= 30000 requires forall i :: 0 <= i < |nums| ==> -100 <= nums[i] <= 100 ensures is_sorted_and_distinct(result) ensures forall i :: i in nums <==> i in result { var previous := nums[0]; result := [nums[0]]; var i := 1; while (i < |nums|) invariant 0 <= i <= |nums| invariant |result| >= 1; invariant previous in nums[0..i]; invariant previous == result[|result| - 1]; invariant is_sorted_and_distinct(result) invariant forall j :: j in nums[0..i] <==> j in result { if (previous != nums[i]) { result := result + [nums[i]]; previous := nums[i]; } i := i + 1; } } // Helper predicate predicate is_sorted(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] <= nums[j] } predicate is_sorted_and_distinct(nums: seq<int>) { forall i, j :: 0 <= i < j < |nums| ==> nums[i] < nums[j] }
Software-Verification_tmp_tmpv4ueky2d_Remove Duplicates from Sorted Array_remove_duplicates_from_sorted_array.dfy
369
369
Dafny program: 369
method remove_element(nums: array<int>, val: int) returns (i: int) modifies nums requires 0 <= nums.Length <= 100 requires forall i :: 0 <= i < nums.Length ==> 0 <= nums[i] <= 50 requires 0 <= val <= 100 ensures forall j :: 0 < j < i < nums.Length ==> nums[j] != val { i := 0; var end := nums.Length - 1; while i <= end { if (nums[i] == val) { if (nums[end] == val) { end := end - 1; } else { nums[i] := nums[end]; i := i + 1; end := end - 1; } } else { i := i + 1; } } }
method remove_element(nums: array<int>, val: int) returns (i: int) modifies nums requires 0 <= nums.Length <= 100 requires forall i :: 0 <= i < nums.Length ==> 0 <= nums[i] <= 50 requires 0 <= val <= 100 ensures forall j :: 0 < j < i < nums.Length ==> nums[j] != val { i := 0; var end := nums.Length - 1; while i <= end invariant 0 <= i <= nums.Length invariant end < nums.Length invariant forall k :: 0 <= k < i ==> nums[k] != val { if (nums[i] == val) { if (nums[end] == val) { end := end - 1; } else { nums[i] := nums[end]; i := i + 1; end := end - 1; } } else { i := i + 1; } } }
Software-Verification_tmp_tmpv4ueky2d_Remove Element_remove_element.dfy
370
370
Dafny program: 370
method is_anagram(s: string, t: string) returns (result: bool) requires |s| == |t| ensures (multiset(s) == multiset(t)) == result { result := is_equal(multiset(s), multiset(t)); } method is_equal(s: multiset<char>, t: multiset<char>) returns (result: bool) ensures (s == t) <==> result { var s_removed := multiset{}; var s_remaining := s; while (|s_remaining| > 0) removed in t && s[removed] == t[removed]) { var remaining :| remaining in s_remaining; if !(remaining in s && remaining in t && s[remaining] == t[remaining]) { return false; } var temp := multiset{}; s_removed := s_removed + temp[remaining := s[remaining]]; s_remaining := s_remaining - temp[remaining := s[remaining]]; } var t_removed := multiset{}; var t_remaining := t; while (|t_remaining| > 0) removed in t && s[removed] == t[removed]) { var remaining :| remaining in t_remaining; if !(remaining in s && remaining in t && s[remaining] == t[remaining]) { return false; } var temp := multiset{}; t_removed := t_removed + temp[remaining := t[remaining]]; t_remaining := t_remaining - temp[remaining := t[remaining]]; } return true; }
method is_anagram(s: string, t: string) returns (result: bool) requires |s| == |t| ensures (multiset(s) == multiset(t)) == result { result := is_equal(multiset(s), multiset(t)); } method is_equal(s: multiset<char>, t: multiset<char>) returns (result: bool) ensures (s == t) <==> result { var s_removed := multiset{}; var s_remaining := s; while (|s_remaining| > 0) invariant s_remaining == s - s_removed invariant forall removed :: removed in s_removed ==> (removed in s && removed in t && s[removed] == t[removed]) { var remaining :| remaining in s_remaining; if !(remaining in s && remaining in t && s[remaining] == t[remaining]) { return false; } var temp := multiset{}; s_removed := s_removed + temp[remaining := s[remaining]]; s_remaining := s_remaining - temp[remaining := s[remaining]]; } var t_removed := multiset{}; var t_remaining := t; while (|t_remaining| > 0) invariant t_remaining == t - t_removed invariant forall removed :: removed in t_removed ==> (removed in s && removed in t && s[removed] == t[removed]) { var remaining :| remaining in t_remaining; if !(remaining in s && remaining in t && s[remaining] == t[remaining]) { return false; } var temp := multiset{}; t_removed := t_removed + temp[remaining := t[remaining]]; t_remaining := t_remaining - temp[remaining := t[remaining]]; } return true; }
Software-Verification_tmp_tmpv4ueky2d_Valid Anagram_valid_anagram.dfy
371
371
Dafny program: 371
method isPalindrome(s: array<char>) returns (result: bool) requires 1<= s.Length <= 200000 ensures result <==> (forall i:: 0 <= i < s.Length / 2 ==> s[i] == s[s.Length - 1 - i]) { var length := s.Length; var i := 0; while i < length / 2 { if s[i] != s[length - 1 - i] { return false; } i := i + 1; } return true; }
method isPalindrome(s: array<char>) returns (result: bool) requires 1<= s.Length <= 200000 ensures result <==> (forall i:: 0 <= i < s.Length / 2 ==> s[i] == s[s.Length - 1 - i]) { var length := s.Length; var i := 0; while i < length / 2 invariant 0 <= i <= length invariant forall j:: 0 <= j < i ==> s[j] == s[length - 1 - j] { if s[i] != s[length - 1 - i] { return false; } i := i + 1; } return true; }
Software-Verification_tmp_tmpv4ueky2d_Valid Palindrome_valid_panlindrome.dfy
372
372
Dafny program: 372
method factImp(n: int) returns (r: int) { r := 1; var m := n; while (m > 0) { r := r*m; m := m-1; } } function power(n: int, m: nat) : int { if m==0 then 1 else n*power(n,m-1) } function pow(n: int, m: nat,r: int) : int { if m==0 then r else pow(n,m-1,r*n) } function powerAlt(n: int,m: nat) : int { pow(n,m,1) } // 3 function equivalentes(n: int,m: nat,r: int) : int ensures power(n,m) == pow(n,m,r) lemma l1(n: int,m: nat, r: int) ensures equivalentes(n,m, r) == powerAlt(n,m) // 4. function fact(n: nat) : nat { if n==0 then 1 else n*fact(n-1) } function factAcc(n: nat,a: int) : int { if (n == 0) then a else factAcc(n-1,n*a) } function factAlt(n: nat) : int { factAcc(n,1) } lemma factAcc_correct(n: nat,a: int) ensures factAcc(n,a) == fact(n)*a lemma equiv(n: nat) ensures fact(n) == factAlt(n) { factAcc_correct(n, 1); } // 5. a) function mystery1(n: nat,m: nat) : nat ensures mystery1(n,m) == n+m { if n==0 then m else mystery1(n-1,m+1) } // 5. b) function mystery2(n: nat,m: nat) : nat ensures mystery2(n,m) == n+m { if m==0 then n else mystery2(n+1,m-1) } // 5. c) function mystery3(n: nat,m: nat) : nat ensures mystery3(n,m) == n*m { if n==0 then 0 else mystery1(m,mystery3(n-1,m)) } // 5. d) function mystery4(n: nat,m: nat) : nat ensures mystery4(n,m) == power(n,m) { if m==0 then 1 else mystery3(n,mystery4(n,m-1)) } // 6 // 8 // 9 // 10 // 11
method factImp(n: int) returns (r: int) { r := 1; var m := n; while (m > 0) { r := r*m; m := m-1; } } function power(n: int, m: nat) : int { if m==0 then 1 else n*power(n,m-1) } function pow(n: int, m: nat,r: int) : int { if m==0 then r else pow(n,m-1,r*n) } function powerAlt(n: int,m: nat) : int { pow(n,m,1) } // 3 function equivalentes(n: int,m: nat,r: int) : int ensures power(n,m) == pow(n,m,r) lemma l1(n: int,m: nat, r: int) ensures equivalentes(n,m, r) == powerAlt(n,m) // 4. function fact(n: nat) : nat { if n==0 then 1 else n*fact(n-1) } function factAcc(n: nat,a: int) : int decreases n { if (n == 0) then a else factAcc(n-1,n*a) } function factAlt(n: nat) : int { factAcc(n,1) } lemma factAcc_correct(n: nat,a: int) ensures factAcc(n,a) == fact(n)*a lemma equiv(n: nat) ensures fact(n) == factAlt(n) { factAcc_correct(n, 1); assert factAcc(n, 1) == fact(n)*1; assert factAlt(n) == factAcc(n, 1); assert fact(n) == fact(n)*1; } // 5. a) function mystery1(n: nat,m: nat) : nat decreases n, m; ensures mystery1(n,m) == n+m { if n==0 then m else mystery1(n-1,m+1) } // 5. b) function mystery2(n: nat,m: nat) : nat decreases m ensures mystery2(n,m) == n+m { if m==0 then n else mystery2(n+1,m-1) } // 5. c) function mystery3(n: nat,m: nat) : nat ensures mystery3(n,m) == n*m { if n==0 then 0 else mystery1(m,mystery3(n-1,m)) } // 5. d) function mystery4(n: nat,m: nat) : nat ensures mystery4(n,m) == power(n,m) { if m==0 then 1 else mystery3(n,mystery4(n,m-1)) } // 6 // 8 // 9 // 10 // 11
Software-building-and-verification-Projects_tmp_tmp5tm1srrn_CVS-projeto_aula1.dfy
374
374
Dafny program: 374
function fib(n : nat) : nat { if (n==0) then 1 else if (n==1) then 1 else fib(n-1)+fib(n-2) } method Fib(n : nat) returns (r:nat) ensures r == fib(n) { if (n == 0) { return 1; } var next:= 2; r:=1; var i := 1; while (i < n) { var tmp := next; next := next + r; r := tmp; i := i + 1; } return r; } // 2. datatype List<T> = Nil | Cons(head: T, tail: List<T>) function add(l : List<int>) : int { match l case Nil => 0 case Cons(x,xs) => x + add(xs) } method addImp(l : List<int>) returns (r: int) ensures r == add(l) { r := 0; var ll := l; while (ll != Nil) { r := r + ll.head; ll := ll.tail; } } // 3. method maxArray(arr : array<int>) returns (max: int) requires arr.Length > 0 ensures forall i: int :: 0 <= i < arr.Length ==> arr[i] <= max ensures exists x::0 <= x < arr.Length && arr[x] == max { max := arr[0]; var index := 1; while(index < arr.Length) { if (arr[index] > max) { max := arr[index]; } index := index + 1; } } // 5. method maxArrayReverse(arr : array<int>) returns (max: int) requires arr.Length > 0 ensures forall i: int :: 0 <= i < arr.Length ==> arr[i] <= max ensures exists x::0 <= x < arr.Length && arr[x] == max { var ind := arr.Length - 1; max := arr[ind]; while ind > 0 { if (arr[ind - 1] > max) { max := arr[ind - 1]; } ind := ind - 1; } } // 6 function sum(n: nat) : nat { if (n == 0) then 0 else n + sum(n-1) } method sumBackwards(n: nat) returns (r: nat) ensures r == sum(n) { var i := n; r := 0; while i > 0 { r := r + i; i := i - 1; } }
function fib(n : nat) : nat { if (n==0) then 1 else if (n==1) then 1 else fib(n-1)+fib(n-2) } method Fib(n : nat) returns (r:nat) ensures r == fib(n) { if (n == 0) { return 1; } var next:= 2; r:=1; var i := 1; while (i < n) invariant next == fib(i+1) invariant r == fib(i) invariant 1 <= i <= n { var tmp := next; next := next + r; r := tmp; i := i + 1; } assert r == fib(n); return r; } // 2. datatype List<T> = Nil | Cons(head: T, tail: List<T>) function add(l : List<int>) : int { match l case Nil => 0 case Cons(x,xs) => x + add(xs) } method addImp(l : List<int>) returns (r: int) ensures r == add(l) { r := 0; var ll := l; while (ll != Nil) decreases ll invariant r==add(l) - add(ll) { r := r + ll.head; ll := ll.tail; } assert r == add(l); } // 3. method maxArray(arr : array<int>) returns (max: int) requires arr.Length > 0 ensures forall i: int :: 0 <= i < arr.Length ==> arr[i] <= max ensures exists x::0 <= x < arr.Length && arr[x] == max { max := arr[0]; var index := 1; while(index < arr.Length) invariant 0 <= index <= arr.Length invariant forall i: int :: 0 <= i < index ==> arr[i] <= max invariant exists x::0 <= x < arr.Length && arr[x] == max { if (arr[index] > max) { max := arr[index]; } index := index + 1; } } // 5. method maxArrayReverse(arr : array<int>) returns (max: int) requires arr.Length > 0 ensures forall i: int :: 0 <= i < arr.Length ==> arr[i] <= max ensures exists x::0 <= x < arr.Length && arr[x] == max { var ind := arr.Length - 1; max := arr[ind]; while ind > 0 invariant 0 <= ind <= arr.Length invariant forall i: int :: ind <= i < arr.Length ==> arr[i] <= max invariant exists x::0 <= x < arr.Length && arr[x] == max { if (arr[ind - 1] > max) { max := arr[ind - 1]; } ind := ind - 1; } } // 6 function sum(n: nat) : nat { if (n == 0) then 0 else n + sum(n-1) } method sumBackwards(n: nat) returns (r: nat) ensures r == sum(n) { var i := n; r := 0; while i > 0 invariant 0 <= i <= n invariant r == sum(n) - sum(i) { r := r + i; i := i - 1; } }
Software-building-and-verification-Projects_tmp_tmp5tm1srrn_CVS-projeto_aula3.dfy
375
375
Dafny program: 375
/*Ex1 Given the leaky specification of class Set found in Appendix ??, use the techniques from class (the use of ghost state and dynamic frames) so that the specification no longer leaks the internal representation. Produce client code that correctly connects to your revised Set class. */ class Set { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() requires size() < maxSize() ensures RepInv() modifies this,Repr ensures fresh(Repr - old(Repr)) { var f:int := find(v); if (f < 0) { store[nelems] := v; elems := elems + {v}; nelems := nelems + 1; } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new Set(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } } /*2. Using the corrected version of Set as a baseline, implement a PositiveSet class that enforces the invariant that all numbers in the set are strictly positive. */ class PositiveSet { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) && (forall x :: x in elems ==> x > 0) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() requires size() < maxSize() ensures RepInv() modifies this,Repr ensures fresh(Repr - old(Repr)) { if(v > 0) { var f:int := find(v); if (f < 0) { store[nelems] := v; elems := elems + {v}; nelems := nelems + 1; } } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new PositiveSet(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } } /* * Implement a savings account. * A savings account is actually made up of two balances. * * One is the checking balance, here account owner can deposit and withdraw * money at will. There is only one restriction on withdrawing. In a regular * bank account, the account owner can make withdrawals as long as he has the * balance for it, i.e., the user cannot withdraw more money than the user has. * In a savings account, the checking balance can go negative as long as it does * not surpass half of what is saved in the savings balance. Consider the * following example: * * Savings = 10 * Checking = 0 * Operation 1: withdraw 10 This operation is not valid. Given that the * the user only has $$10, his checking account * can only decrease down to $$-5 (10/2). * * Operation 2: withdraw 2 Despite the fact that the checking balance of * the user is zero, * money in his savings account, therefore, this * operation is valid, and the result would be * something like: * Savings = 10; * Checking = -2 * * Regarding depositing money in the savings balance (save), this operation has * one small restrictions. It is only possible to save money to the savings * balance when the user is not in debt; i.e. to save money into savings, the * checking must be non-negative. * * Given the states: * STATE 1 STATE 2 * Savings = 10 Savings = 10 * Checking = -5 Checking = 0 * * and the operation save($$60000000000), the operation is valid when executed * in STATE 2 but not in STATE 1. * * Finally, when withdrawing from the savings balance, an operation we will * call rescue, the amount the user can withdraw depends on the negativity of * the user’s checking account. For instance: * * Savings: 12 * Checking: -5 * * In the case, the user could withdraw at most two double dollars ($$). If the * user withdrew more than that, the balance of the checking account would * go beyond the -50% of the savings account; big no no. * */ class SavingsAccount { var cbalance: int; var sbalance: int; ghost var Repr:set<object>; ghost predicate RepInv() reads this,Repr { this in Repr && cbalance >= -sbalance/2 } ghost predicate PositiveChecking() reads this,Repr { cbalance >= 0 } constructor() ensures fresh(Repr-{this}) ensures RepInv() { Repr := {this}; cbalance := 0; sbalance := 0; } method deposit(amount:int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { cbalance := cbalance + amount; } method withdraw(amount:int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { if(cbalance-amount >= -sbalance/2) { cbalance := cbalance - amount; } } method save(amount: int) requires amount > 0 requires PositiveChecking() requires RepInv() ensures RepInv() modifies Repr { if(cbalance >= 0) { sbalance := sbalance + amount; } } method rescue(amount: int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { if(cbalance >= -(sbalance-amount)/2) { sbalance := sbalance - amount; } } } /*Ex 4 Change your specification and implementation of the ASet ADT to include a growing array of integer values. */ class GrowingSet { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() ensures RepInv() modifies Repr ensures fresh(Repr - old(Repr)) { var f:int := find(v); if (f < 0) { if(nelems == store.Length) { var tmp := new int[store.Length * 2]; var i:= 0; while i < store.Length modifies tmp { tmp[i] := store[i]; i := i + 1; } Repr := Repr - {store} + {tmp}; store := tmp; } store[nelems] := v; elems := elems + {v}; nelems := nelems + 1; } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new GrowingSet(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } }
/*Ex1 Given the leaky specification of class Set found in Appendix ??, use the techniques from class (the use of ghost state and dynamic frames) so that the specification no longer leaks the internal representation. Produce client code that correctly connects to your revised Set class. */ class Set { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() requires size() < maxSize() ensures RepInv() modifies this,Repr ensures fresh(Repr - old(Repr)) { var f:int := find(v); if (f < 0) { store[nelems] := v; elems := elems + {v}; assert forall i:: 0 <= i < nelems ==> old(store[i]) == store[i]; nelems := nelems + 1; } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) decreases nelems-i invariant 0 <= i <= nelems; invariant forall j::(0<=j< i) ==> x != store[j]; { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new Set(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } } /*2. Using the corrected version of Set as a baseline, implement a PositiveSet class that enforces the invariant that all numbers in the set are strictly positive. */ class PositiveSet { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) && (forall x :: x in elems ==> x > 0) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() requires size() < maxSize() ensures RepInv() modifies this,Repr ensures fresh(Repr - old(Repr)) { if(v > 0) { var f:int := find(v); if (f < 0) { store[nelems] := v; elems := elems + {v}; assert forall i:: 0 <= i < nelems ==> old(store[i]) == store[i]; nelems := nelems + 1; } } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) decreases nelems-i invariant 0 <= i <= nelems; invariant forall j::(0<=j< i) ==> x != store[j]; { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new PositiveSet(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } } /* * Implement a savings account. * A savings account is actually made up of two balances. * * One is the checking balance, here account owner can deposit and withdraw * money at will. There is only one restriction on withdrawing. In a regular * bank account, the account owner can make withdrawals as long as he has the * balance for it, i.e., the user cannot withdraw more money than the user has. * In a savings account, the checking balance can go negative as long as it does * not surpass half of what is saved in the savings balance. Consider the * following example: * * Savings = 10 * Checking = 0 * Operation 1: withdraw 10 This operation is not valid. Given that the * the user only has $$10, his checking account * can only decrease down to $$-5 (10/2). * * Operation 2: withdraw 2 Despite the fact that the checking balance of * the user is zero, * money in his savings account, therefore, this * operation is valid, and the result would be * something like: * Savings = 10; * Checking = -2 * * Regarding depositing money in the savings balance (save), this operation has * one small restrictions. It is only possible to save money to the savings * balance when the user is not in debt; i.e. to save money into savings, the * checking must be non-negative. * * Given the states: * STATE 1 STATE 2 * Savings = 10 Savings = 10 * Checking = -5 Checking = 0 * * and the operation save($$60000000000), the operation is valid when executed * in STATE 2 but not in STATE 1. * * Finally, when withdrawing from the savings balance, an operation we will * call rescue, the amount the user can withdraw depends on the negativity of * the user’s checking account. For instance: * * Savings: 12 * Checking: -5 * * In the case, the user could withdraw at most two double dollars ($$). If the * user withdrew more than that, the balance of the checking account would * go beyond the -50% of the savings account; big no no. * */ class SavingsAccount { var cbalance: int; var sbalance: int; ghost var Repr:set<object>; ghost predicate RepInv() reads this,Repr { this in Repr && cbalance >= -sbalance/2 } ghost predicate PositiveChecking() reads this,Repr { cbalance >= 0 } constructor() ensures fresh(Repr-{this}) ensures RepInv() { Repr := {this}; cbalance := 0; sbalance := 0; } method deposit(amount:int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { cbalance := cbalance + amount; } method withdraw(amount:int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { if(cbalance-amount >= -sbalance/2) { cbalance := cbalance - amount; } } method save(amount: int) requires amount > 0 requires PositiveChecking() requires RepInv() ensures RepInv() modifies Repr { if(cbalance >= 0) { sbalance := sbalance + amount; } } method rescue(amount: int) requires amount > 0 requires RepInv() ensures RepInv() modifies Repr { if(cbalance >= -(sbalance-amount)/2) { sbalance := sbalance - amount; } } } /*Ex 4 Change your specification and implementation of the ASet ADT to include a growing array of integer values. */ class GrowingSet { var store:array<int>; var nelems: int; ghost var Repr : set<object> ghost var elems : set<int> ghost predicate RepInv() reads this, Repr { this in Repr && store in Repr && 0 < store.Length && 0 <= nelems <= store.Length && (forall i :: 0 <= i < nelems ==> store[i] in elems) && (forall x :: x in elems ==> exists i :: 0 <= i < nelems && store[i] == x) } // the construction operation constructor(n: int) requires 0 < n ensures RepInv() ensures fresh(Repr-{this}) { store := new int[n]; Repr := {this,store}; elems := {}; nelems := 0; } // returns the number of elements in the set function size():int requires RepInv() ensures RepInv() reads Repr { nelems } // returns the maximum number of elements in the set function maxSize():int requires RepInv() ensures RepInv() reads Repr { store.Length } // checks if the element given is in the set method contains(v:int) returns (b:bool) requires RepInv() ensures RepInv() ensures b <==> v in elems { var i := find(v); return i >= 0; } // adds a new element to the set if space available method add(v:int) requires RepInv() ensures RepInv() modifies Repr ensures fresh(Repr - old(Repr)) { var f:int := find(v); assert forall i:: 0 <= i < nelems ==> old(store[i]) == store[i]; if (f < 0) { if(nelems == store.Length) { var tmp := new int[store.Length * 2]; var i:= 0; while i < store.Length invariant 0 <= i <= store.Length < tmp.Length invariant forall j :: 0 <= j < i ==> old(store[j]) == tmp[j] modifies tmp { tmp[i] := store[i]; i := i + 1; } Repr := Repr - {store} + {tmp}; store := tmp; } store[nelems] := v; elems := elems + {v}; assert forall i:: 0 <= i < nelems ==> old(store[i]) == store[i]; nelems := nelems + 1; } } // private method that should not be in the method find(x:int) returns (r:int) requires RepInv() ensures RepInv() ensures r < 0 ==> x !in elems ensures r >=0 ==> x in elems; { var i:int := 0; while (i<nelems) decreases nelems-i invariant 0 <= i <= nelems; invariant forall j::(0<=j< i) ==> x != store[j]; { if (store[i]==x) { return i; } i := i + 1; } return -1; } method Main() { var s := new GrowingSet(10); if (s.size() < s.maxSize()) { s.add(2); var b := s.contains(2); if (s.size() < s.maxSize()) { s.add(3); } } } }
Software-building-and-verification-Projects_tmp_tmp5tm1srrn_CVS-projeto_aula5.dfy
376
376
Dafny program: 376
// 1 a) // [ai, aj[ function sum(a: array<int>, i: int, j: int) : int requires 0 <= i <= j <= a.Length reads a { if i == j then 0 else a[j-1] + sum(a, i, j-1) } // 1 b) method query(a: array<int>, i: int, j: int) returns (res : int) requires 0 <= i <= j <= a.Length ensures res == sum(a, i, j) { res := 0; var ind := j-1; while ind >= i { res := res + a[ind]; ind := ind - 1; } } // 1 c) // a -> [1, 10, 3, −4, 5] // c -> [0, 1, 11, 14, 10, 15] method queryFast(a: array<int>, c: array<int>, i: int, j: int) returns (r: int) requires 0 <= i <= j <= a.Length requires is_prefix_sum_for(a,c) ensures r == sum(a, i, j) { var k := i; proof(a, 0, j, k); r := c[j] - c[i]; } predicate is_prefix_sum_for (a: array<int>, c: array<int>) reads c, a { a.Length + 1 == c.Length && forall i: int :: 0 <= i <= a.Length ==> c[i] == sum(a, 0, i) } lemma proof(a: array<int>, i: int, j: int, k:int) requires 0 <= i <= k <= j <= a.Length ensures sum(a, i, k) + sum(a, k, j) == sum(a, i, j) // 2 datatype List<T> = Nil | Cons(head: T, tail: List<T>) method from_array<T>(a: array<T>) returns (l: List<T>) ensures forall i: int :: 0 <= i < a.Length ==> mem(a[i], l) ensures forall x: T :: mem(x, l) ==> exists y: int :: 0 <= y < a.Length && a[y] == x { l := Nil; var i := a.Length - 1; while i >= 0 { l := Cons(a[i], l); i := i - 1; } } function mem<T(==)> (x: T, l: List<T>) : bool { match l case Nil => false case Cons(h, t) => h == x || mem(x, t) }
// 1 a) // [ai, aj[ function sum(a: array<int>, i: int, j: int) : int requires 0 <= i <= j <= a.Length reads a decreases j { if i == j then 0 else a[j-1] + sum(a, i, j-1) } // 1 b) method query(a: array<int>, i: int, j: int) returns (res : int) requires 0 <= i <= j <= a.Length ensures res == sum(a, i, j) { res := 0; var ind := j-1; while ind >= i invariant i-1 <= ind < j invariant res == sum(a, i, j) - sum(a, i, ind+1) decreases ind { res := res + a[ind]; ind := ind - 1; } } // 1 c) // a -> [1, 10, 3, −4, 5] // c -> [0, 1, 11, 14, 10, 15] method queryFast(a: array<int>, c: array<int>, i: int, j: int) returns (r: int) requires 0 <= i <= j <= a.Length requires is_prefix_sum_for(a,c) ensures r == sum(a, i, j) { var k := i; proof(a, 0, j, k); r := c[j] - c[i]; } predicate is_prefix_sum_for (a: array<int>, c: array<int>) reads c, a { a.Length + 1 == c.Length && forall i: int :: 0 <= i <= a.Length ==> c[i] == sum(a, 0, i) } lemma proof(a: array<int>, i: int, j: int, k:int) requires 0 <= i <= k <= j <= a.Length ensures sum(a, i, k) + sum(a, k, j) == sum(a, i, j) // 2 datatype List<T> = Nil | Cons(head: T, tail: List<T>) method from_array<T>(a: array<T>) returns (l: List<T>) ensures forall i: int :: 0 <= i < a.Length ==> mem(a[i], l) ensures forall x: T :: mem(x, l) ==> exists y: int :: 0 <= y < a.Length && a[y] == x { l := Nil; var i := a.Length - 1; while i >= 0 invariant 0 <= i+1 <= a.Length invariant forall j: int :: i < j < a.Length ==> mem(a[j], l) invariant forall x: T :: mem(x, l) ==> exists y: int :: i+1 <= y < a.Length && a[y] == x decreases i { l := Cons(a[i], l); i := i - 1; } } function mem<T(==)> (x: T, l: List<T>) : bool { match l case Nil => false case Cons(h, t) => h == x || mem(x, t) }
Software-building-and-verification-Projects_tmp_tmp5tm1srrn_CVS-projeto_handout1.dfy
377
377
Dafny program: 377
datatype List<T> = Nil | Cons(head:T,tail:List<T>) datatype Option<T> = None | Some(elem:T) ghost function mem<T>(x:T,l:List<T>) : bool { match l { case Nil => false case Cons(y,xs) => x==y || mem(x,xs) } } ghost function length<T>(l:List<T>) : int { match l { case Nil => 0 case Cons(_,xs) => 1 + length(xs) } } function list_find<K(==),V(!new)>(k:K,l:List<(K,V)>) : Option<V> ensures match list_find(k,l) { case None => forall v :: !mem((k,v),l) case Some(v) => mem((k,v),l) } { match l { case Nil => None case Cons((k',v),xs) => if k==k' then Some(v) else list_find(k,xs) } } function list_remove<K(==,!new),V(!new)>(k:K, l:List<(K,V)>) : List<(K,V)> ensures forall k',v :: mem((k',v),list_remove(k,l)) <==> (mem((k',v),l) && k != k') { match l { case Nil => Nil case Cons((k',v),xs) => if k==k' then list_remove(k,xs) else Cons((k',v),list_remove(k,xs)) } } class Hashtable<K(==,!new),V(!new)> { var size : int var data : array<List<(K,V)>> ghost var Repr : set<object> ghost var elems : map<K,Option<V>> ghost predicate RepInv() reads this, Repr { this in Repr && data in Repr && data.Length > 0 && (forall i :: 0 <= i < data.Length ==> valid_hash(data, i)) && (forall k,v :: valid_data(k,v,elems,data)) } ghost predicate valid_hash(data: array<List<(K,V)>>, i: int) requires 0 <= i < data.Length reads data { forall k,v :: mem((k,v), data[i]) ==> (bucket(k,data.Length) == i) } ghost predicate valid_data(k: K,v: V,elems: map<K, Option<V>>, data: array<List<(K,V)>>) reads this, Repr, data requires data.Length > 0 { (k in elems && elems[k] == Some(v)) <==> mem((k,v), data[bucket(k, data.Length)]) } function hash(key:K) : int ensures hash(key) >= 0 function bucket(k: K, n: int) : int requires n > 0 ensures 0 <= bucket(k, n) < n { hash(k) % n } constructor(n:int) requires n > 0 ensures RepInv() ensures fresh(Repr-{this}) ensures elems == map[] ensures size == 0 { size := 0; data := new List<(K,V)>[n](i => Nil); Repr := {this, data}; elems := map[]; } method clear() requires RepInv() ensures RepInv() ensures elems == map[] ensures fresh(Repr - old(Repr)) modifies Repr { var i := 0; while i < data.Length modifies data { data[i] := Nil; i := i + 1; } size := 0; elems := map[]; } method resize() requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures forall key :: key in old(elems) ==> key in elems ensures forall k,v :: k in old(elems) && old(elems)[k] == Some(v) ==> k in elems && elems[k] == Some(v) modifies Repr { var newData := new List<(K,V)>[data.Length * 2](i => Nil); var i := 0; var oldSize := data.Length; var newSize := newData.Length; while i < data.Length modifies newData if 0<= bucket(k, oldSize) < i then valid_data(k,v,elems,newData) else !mem((k,v), newData[bucket(k, newSize)])) { if 0 <= bucket(k, oldSize) < i then valid_data(k,v,elems,data) else if bucket(k, oldSize) == i then ((k in elems && elems[k] == Some(v)) <==> mem((k,v), data[bucket(k,data.Length)]) || mem((k,v), newData[bucket(k, newSize)])) else !mem((k,v), newData[bucket(k, newSize)])); rehash(data[i],newData,i,oldSize,newSize); i := i + 1; } Repr := Repr - {data} + {newData}; data := newData; } method rehash(l: List<(K,V)>, newData: array<List<(K,V)>>,i: int, oldSize: int, newSize: int) requires newData != data requires 0 < oldSize == data.Length requires newData.Length == 2 * oldSize == newSize requires forall k,v :: mem((k,v), l) ==> bucket(k, oldSize) == i requires forall j :: 0 <= j < newSize ==> valid_hash(newData, j) requires forall k,v :: ( if 0 <= bucket(k, oldSize) < i then valid_data(k,v,elems,newData) else if bucket(k, oldSize) == i then ((k in elems && elems[k] == Some(v)) <==> mem((k,v), l) || mem((k,v),newData[bucket(k, newSize)])) else !mem((k,v),newData[bucket(k, newSize)])) ensures forall j :: 0 <= j < newSize ==> valid_hash(newData, j) ensures forall k,v :: (if 0 <= bucket(k, oldSize) <= i then valid_data(k,v,elems,newData) else !mem((k,v),newData[bucket(k, newSize)])) modifies newData { match l { case Nil => return; case Cons((k,v), r) => { var b := bucket(k, newSize); newData[b] := Cons((k,v), newData[b]); rehash(r, newData, i, oldSize, newSize); } } } method find(k: K) returns (r: Option<V>) requires RepInv() ensures RepInv() ensures match r case None => (k !in elems || (k in elems && elems[k] == None)) case Some(v) => (k in elems && elems[k] == Some(v)) { var idx := bucket(k, data.Length); r := list_find(k, data[idx]); case None => forall v :: idx == bucket(k,data.Length) && !mem((k,v),data[idx]) case Some(v) => mem((k,v),data[bucket(k,data.Length)]); } method remove(k: K) requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures k !in elems || elems[k] == None ensures forall key :: key != k && key in old(elems) ==> key in elems && elems[key] == old(elems[key]) modifies Repr { var idx := bucket(k, data.Length); var opt := list_find(k, data[idx]); match opt case None => case Some(v) => var idx := bucket(k, data.Length); data[idx] := list_remove(k, data[idx]); elems := elems[k := None]; size := size - 1; } method add(k:K,v:V) requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures k in elems && elems[k] == Some(v) ensures forall key :: key != k && key in old(elems) ==> key in elems modifies Repr { if(size >= data.Length * 3/4) { resize(); } remove(k); var ind := bucket(k,data.Length); data[ind] := Cons((k,v), data[ind]); elems := elems[k := Some(v)]; size := size + 1; } }
datatype List<T> = Nil | Cons(head:T,tail:List<T>) datatype Option<T> = None | Some(elem:T) ghost function mem<T>(x:T,l:List<T>) : bool { match l { case Nil => false case Cons(y,xs) => x==y || mem(x,xs) } } ghost function length<T>(l:List<T>) : int { match l { case Nil => 0 case Cons(_,xs) => 1 + length(xs) } } function list_find<K(==),V(!new)>(k:K,l:List<(K,V)>) : Option<V> ensures match list_find(k,l) { case None => forall v :: !mem((k,v),l) case Some(v) => mem((k,v),l) } decreases l { match l { case Nil => None case Cons((k',v),xs) => if k==k' then Some(v) else list_find(k,xs) } } function list_remove<K(==,!new),V(!new)>(k:K, l:List<(K,V)>) : List<(K,V)> decreases l ensures forall k',v :: mem((k',v),list_remove(k,l)) <==> (mem((k',v),l) && k != k') { match l { case Nil => Nil case Cons((k',v),xs) => if k==k' then list_remove(k,xs) else Cons((k',v),list_remove(k,xs)) } } class Hashtable<K(==,!new),V(!new)> { var size : int var data : array<List<(K,V)>> ghost var Repr : set<object> ghost var elems : map<K,Option<V>> ghost predicate RepInv() reads this, Repr { this in Repr && data in Repr && data.Length > 0 && (forall i :: 0 <= i < data.Length ==> valid_hash(data, i)) && (forall k,v :: valid_data(k,v,elems,data)) } ghost predicate valid_hash(data: array<List<(K,V)>>, i: int) requires 0 <= i < data.Length reads data { forall k,v :: mem((k,v), data[i]) ==> (bucket(k,data.Length) == i) } ghost predicate valid_data(k: K,v: V,elems: map<K, Option<V>>, data: array<List<(K,V)>>) reads this, Repr, data requires data.Length > 0 { (k in elems && elems[k] == Some(v)) <==> mem((k,v), data[bucket(k, data.Length)]) } function hash(key:K) : int ensures hash(key) >= 0 function bucket(k: K, n: int) : int requires n > 0 ensures 0 <= bucket(k, n) < n { hash(k) % n } constructor(n:int) requires n > 0 ensures RepInv() ensures fresh(Repr-{this}) ensures elems == map[] ensures size == 0 { size := 0; data := new List<(K,V)>[n](i => Nil); Repr := {this, data}; elems := map[]; } method clear() requires RepInv() ensures RepInv() ensures elems == map[] ensures fresh(Repr - old(Repr)) modifies Repr { var i := 0; while i < data.Length invariant 0 <= i <= data.Length invariant forall j :: 0 <= j < i ==> data[j] == Nil modifies data { data[i] := Nil; i := i + 1; } size := 0; elems := map[]; } method resize() requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures forall key :: key in old(elems) ==> key in elems ensures forall k,v :: k in old(elems) && old(elems)[k] == Some(v) ==> k in elems && elems[k] == Some(v) modifies Repr { var newData := new List<(K,V)>[data.Length * 2](i => Nil); var i := 0; var oldSize := data.Length; var newSize := newData.Length; assert forall i :: 0 <= i < data.Length ==> valid_hash(data,i); while i < data.Length modifies newData invariant RepInv() invariant 0 <= i <= data.Length invariant newData != data invariant old(data) == data invariant old(size) == size invariant Repr == old(Repr) invariant 0 < oldSize == data.Length invariant data.Length*2 == newData.Length == newSize invariant forall j :: 0 <= j < newSize ==> valid_hash(newData, j) invariant forall k,v :: ( if 0<= bucket(k, oldSize) < i then valid_data(k,v,elems,newData) else !mem((k,v), newData[bucket(k, newSize)])) { assert valid_hash(data,i); assert forall k,v :: ( if 0 <= bucket(k, oldSize) < i then valid_data(k,v,elems,data) else if bucket(k, oldSize) == i then ((k in elems && elems[k] == Some(v)) <==> mem((k,v), data[bucket(k,data.Length)]) || mem((k,v), newData[bucket(k, newSize)])) else !mem((k,v), newData[bucket(k, newSize)])); rehash(data[i],newData,i,oldSize,newSize); i := i + 1; } Repr := Repr - {data} + {newData}; data := newData; } method rehash(l: List<(K,V)>, newData: array<List<(K,V)>>,i: int, oldSize: int, newSize: int) requires newData != data requires 0 < oldSize == data.Length requires newData.Length == 2 * oldSize == newSize requires forall k,v :: mem((k,v), l) ==> bucket(k, oldSize) == i requires forall j :: 0 <= j < newSize ==> valid_hash(newData, j) requires forall k,v :: ( if 0 <= bucket(k, oldSize) < i then valid_data(k,v,elems,newData) else if bucket(k, oldSize) == i then ((k in elems && elems[k] == Some(v)) <==> mem((k,v), l) || mem((k,v),newData[bucket(k, newSize)])) else !mem((k,v),newData[bucket(k, newSize)])) ensures forall j :: 0 <= j < newSize ==> valid_hash(newData, j) ensures forall k,v :: (if 0 <= bucket(k, oldSize) <= i then valid_data(k,v,elems,newData) else !mem((k,v),newData[bucket(k, newSize)])) modifies newData decreases l { match l { case Nil => return; case Cons((k,v), r) => { var b := bucket(k, newSize); newData[b] := Cons((k,v), newData[b]); rehash(r, newData, i, oldSize, newSize); } } } method find(k: K) returns (r: Option<V>) requires RepInv() ensures RepInv() ensures match r case None => (k !in elems || (k in elems && elems[k] == None)) case Some(v) => (k in elems && elems[k] == Some(v)) { assert forall k, v :: valid_data(k,v,elems,data) && ((k in elems && elems[k] == Some(v)) <==> (mem((k,v),data[bucket(k,data.Length)]))); var idx := bucket(k, data.Length); r := list_find(k, data[idx]); assert match list_find(k,data[bucket(k, data.Length)]) case None => forall v :: idx == bucket(k,data.Length) && !mem((k,v),data[idx]) case Some(v) => mem((k,v),data[bucket(k,data.Length)]); } method remove(k: K) requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures k !in elems || elems[k] == None ensures forall key :: key != k && key in old(elems) ==> key in elems && elems[key] == old(elems[key]) modifies Repr { assert forall i :: 0 <= i < data.Length ==> valid_hash(data, i); assert forall k,v :: valid_data(k,v,elems,data); var idx := bucket(k, data.Length); var opt := list_find(k, data[idx]); assert forall i :: 0 <= i < data.Length ==> valid_hash(data,i) && (forall k,v:: mem((k,v), data[i]) ==> (bucket(k,data.Length) == i)); match opt case None => assert forall k,v :: valid_data(k,v,elems, data) && ((k in elems && elems[k] == Some(v)) <==> (mem((k,v), data[bucket(k, data.Length)]))); assert forall i :: 0 <= i < data.Length ==> valid_hash(data,i); assert forall v :: !mem((k,v),data[bucket(k,data.Length)]); case Some(v) => assert forall k,v :: valid_data(k,v,elems,data) && ((k in elems && elems[k] == Some(v)) <==> (mem((k,v),data[bucket(k,data.Length)]))); var idx := bucket(k, data.Length); data[idx] := list_remove(k, data[idx]); elems := elems[k := None]; size := size - 1; } method add(k:K,v:V) requires RepInv() ensures RepInv() ensures fresh(Repr - old(Repr)) ensures k in elems && elems[k] == Some(v) ensures forall key :: key != k && key in old(elems) ==> key in elems modifies Repr { if(size >= data.Length * 3/4) { resize(); } remove(k); assert forall i :: 0 <= i < data.Length ==> valid_hash(data, i); var ind := bucket(k,data.Length); assert forall i :: 0 <= i < data.Length ==> valid_hash(data, i) && (forall k,v:: mem((k,v), data[i]) ==> (bucket(k,data.Length) == i)); assert forall k,v :: valid_data(k,v,elems, data) && ((k in elems && elems[k] == Some(v)) <==> (mem((k,v), data[bucket(k, data.Length)]))); assert forall k,v :: mem((k,v), data[ind]) ==> (bucket(k,data.Length) == ind); data[ind] := Cons((k,v), data[ind]); elems := elems[k := Some(v)]; assert bucket(k,data.Length) == ind; assert mem((k,v), data[bucket(k,data.Length)]); size := size + 1; assert k in elems && elems[k] == Some(v); } }
Software-building-and-verification-Projects_tmp_tmp5tm1srrn_CVS-projeto_handout2.dfy
378
378
Dafny program: 378
method div_ent_it(a: int, b: int) returns (c: int, r: int) // Algoritmo iterativo de la división de enteros // que calcula su cociente y resto requires a >= 0 && b > 0 ensures a == b*c + r && 0 <= r < b { c := 0; r := a ; while (r >= b) { c := c + 1 ; r := r - b ; } } method Main() { var c, r := div_ent_it(6 , 2) ; print "Cociente: ", c, ", Resto: ", r ; }
method div_ent_it(a: int, b: int) returns (c: int, r: int) // Algoritmo iterativo de la división de enteros // que calcula su cociente y resto requires a >= 0 && b > 0 ensures a == b*c + r && 0 <= r < b { c := 0; r := a ; while (r >= b) invariant a == b * c + r && r >= 0 && b > 0 decreases r { c := c + 1 ; r := r - b ; } } method Main() { var c, r := div_ent_it(6 , 2) ; print "Cociente: ", c, ", Resto: ", r ; }
TFG_tmp_tmpbvsao41w_Algoritmos Dafny_div_ent_it.dfy