test_ID
stringlengths
3
3
test_file
stringlengths
14
119
ground_truth
stringlengths
70
28.7k
hints_removed
stringlengths
58
28.7k
100
Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseBubbleSort.dfy
predicate sorted_seg(a:array<int>, i:int, j:int) //j excluded requires 0 <= i <= j <= a.Length reads a { forall l, k :: i <= l <= k < j ==> a[l] <= a[k] } method bubbleSorta(a:array<int>, c:int, f:int)//f excluded modifies a requires 0 <= c <= f <= a.Length //when c==f empty sequence ensures sorted_seg(a,c,f) en...
predicate sorted_seg(a:array<int>, i:int, j:int) //j excluded requires 0 <= i <= j <= a.Length reads a { forall l, k :: i <= l <= k < j ==> a[l] <= a[k] } method bubbleSorta(a:array<int>, c:int, f:int)//f excluded modifies a requires 0 <= c <= f <= a.Length //when c==f empty sequence ensures sorted_seg(a,c,f) en...
101
Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseReplace.dfy
method replace(v:array<int>, x:int, y:int) modifies v ensures forall k::0<=k<old(v.Length) && old(v[k])==x ==> v[k]==y ensures forall k::0<=k<old(v.Length) && old(v[k])!=x ==> v[k]==old(v[k]) { var i:=0; while(i<v.Length) decreases v.Length - i invariant 0<=i<=v.Length invariant forall k::0<=k<i ...
method replace(v:array<int>, x:int, y:int) modifies v ensures forall k::0<=k<old(v.Length) && old(v[k])==x ==> v[k]==y ensures forall k::0<=k<old(v.Length) && old(v[k])!=x ==> v[k]==old(v[k]) { var i:=0; while(i<v.Length) { if(v[i]==x){ v[i]:=y; } i:=i+1; } }
102
Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseSelSort.dfy
predicate sorted_seg(a:array<int>, i:int, j:int) //j not included requires 0 <= i <= j <= a.Length reads a { forall l, k :: i <= l <= k < j ==> a[l] <= a[k] } method selSort (a:array<int>, c:int, f:int)//f excluded modifies a requires 0 <= c <= f <= a.Length //when c==f empty sequence ensures sorted_seg(a,c,f) ...
predicate sorted_seg(a:array<int>, i:int, j:int) //j not included requires 0 <= i <= j <= a.Length reads a { forall l, k :: i <= l <= k < j ==> a[l] <= a[k] } method selSort (a:array<int>, c:int, f:int)//f excluded modifies a requires 0 <= c <= f <= a.Length //when c==f empty sequence ensures sorted_seg(a,c,f) ...
103
Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseSeparate.dfy
predicate strictNegative(v:array<int>,i:int,j:int) reads v requires 0<=i<=j<=v.Length {forall u | i<=u<j :: v[u]<0} predicate positive(s:seq<int>) {forall u::0<=u<|s| ==> s[u]>=0} predicate isPermutation(s:seq<int>, t:seq<int>) {multiset(s)==multiset(t)} /** returns an index st new array is a permutation of the old ...
predicate strictNegative(v:array<int>,i:int,j:int) reads v requires 0<=i<=j<=v.Length {forall u | i<=u<j :: v[u]<0} predicate positive(s:seq<int>) {forall u::0<=u<|s| ==> s[u]>=0} predicate isPermutation(s:seq<int>, t:seq<int>) {multiset(s)==multiset(t)} /** returns an index st new array is a permutation of the old ...
104
Dafny-Exercises_tmp_tmpjm75muf__Session8Exercises_ExerciseInsertionSort.dfy
predicate sorted_seg(a:array<int>, i:int, j:int) //i and j included requires 0 <= i <= j+1 <= a.Length reads a { forall l, k :: i <= l <= k <= j ==> a[l] <= a[k] } method InsertionSort(a: array<int>) modifies a; ensures sorted_seg(a,0,a.Length-1) ensures multiset(a[..]) == old(multiset(a[..])) //Add and pr...
predicate sorted_seg(a:array<int>, i:int, j:int) //i and j included requires 0 <= i <= j+1 <= a.Length reads a { forall l, k :: i <= l <= k <= j ==> a[l] <= a[k] } method InsertionSort(a: array<int>) modifies a; ensures sorted_seg(a,0,a.Length-1) ensures multiset(a[..]) == old(multiset(a[..])) //Add and pr...
105
Dafny-Exercises_tmp_tmpjm75muf__Session9Exercises_ExerciseSeqMaxSum.dfy
function Sum(v:array<int>,i:int,j:int):int reads v requires 0<=i<=j<=v.Length decreases j { if (i==j) then 0 else Sum(v,i,j-1)+v[j-1] } predicate SumMaxToRight(v:array<int>,i:int,s:int) reads v requires 0<=i<v.Length { forall l,ss {:induction l}::0<=l<=i && ss==i+1==> Sum(v,l,ss)<=s } method segMaxSum(v:arr...
function Sum(v:array<int>,i:int,j:int):int reads v requires 0<=i<=j<=v.Length { if (i==j) then 0 else Sum(v,i,j-1)+v[j-1] } predicate SumMaxToRight(v:array<int>,i:int,s:int) reads v requires 0<=i<v.Length { forall l,ss {:induction l}::0<=l<=i && ss==i+1==> Sum(v,l,ss)<=s } method segMaxSum(v:array<int>,i:in...
106
Dafny-Grind75_tmp_tmpsxfz3i4r_problems_twoSum.dfy
/* https://leetcode.com/problems/two-sum/ function twoSum(nums: number[], target: number): number[] { const n = nums.length; for(let i = 0; i < n; i++) { for(let k = i+1; k < n; k++) { if(nums[i] + nums[k] == target) return [i,k]; } } }; */ predicate summingPair(i: nat, j: nat, ...
/* https://leetcode.com/problems/two-sum/ function twoSum(nums: number[], target: number): number[] { const n = nums.length; for(let i = 0; i < n; i++) { for(let k = i+1; k < n; k++) { if(nums[i] + nums[k] == target) return [i,k]; } } }; */ predicate summingPair(i: nat, j: nat, ...
107
Dafny-Practice_tmp_tmphnmt4ovh_BST.dfy
datatype Tree = Empty | Node(int,Tree,Tree) method Main() { var tree := BuildBST([-2,8,3,9,2,-7,0]); PrintTreeNumbersInorder(tree); } method PrintTreeNumbersInorder(t: Tree) { match t { case Empty => case Node(n, l, r) => PrintTreeNumbersInorder(l); print n; print "\n"; PrintTreeNumbersInorder(r); ...
datatype Tree = Empty | Node(int,Tree,Tree) method Main() { var tree := BuildBST([-2,8,3,9,2,-7,0]); PrintTreeNumbersInorder(tree); } method PrintTreeNumbersInorder(t: Tree) { match t { case Empty => case Node(n, l, r) => PrintTreeNumbersInorder(l); print n; print "\n"; PrintTreeNumbersInorder(r); ...
108
Dafny-Practice_tmp_tmphnmt4ovh_Pattern Matching.dfy
method {:verify true} FindAllOccurrences(text: string, pattern: string) returns (offsets: set<nat>) ensures forall i :: i in offsets ==> i + |pattern| <= |text| ensures forall i :: 0 <= i <= |text| - |pattern| ==> (text[i..i+|pattern|] == pattern <==> i in offsets) { offsets := {}; var i:int := 0; // no pat...
method {:verify true} FindAllOccurrences(text: string, pattern: string) returns (offsets: set<nat>) ensures forall i :: i in offsets ==> i + |pattern| <= |text| ensures forall i :: 0 <= i <= |text| - |pattern| ==> (text[i..i+|pattern|] == pattern <==> i in offsets) { offsets := {}; var i:int := 0; // no pat...
109
Dafny-Projects_tmp_tmph399drhy_p2_arraySplit.dfy
method ArraySplit (a : array<int>) returns (b : array<int>, c : array<int>) ensures fresh(b) ensures fresh(c) ensures a[..] == b[..] + c[..] ensures a.Length == b.Length + c.Length ensures a.Length > 1 ==> a.Length > b.Length ensures a.Length > 1 ==> a.Length > c.Length { var splitPoint : int := a.Length ...
method ArraySplit (a : array<int>) returns (b : array<int>, c : array<int>) ensures fresh(b) ensures fresh(c) ensures a[..] == b[..] + c[..] ensures a.Length == b.Length + c.Length ensures a.Length > 1 ==> a.Length > b.Length ensures a.Length > 1 ==> a.Length > c.Length { var splitPoint : int := a.Length ...
110
Dafny-VMC_tmp_tmpzgqv0i1u_src_Math_Exponential.dfy
module Exponential { ghost function {:axiom} Exp(x: real): real lemma {:axiom} FunctionalEquation(x: real, y: real) ensures Exp(x + y) == Exp(x) * Exp(y) lemma {:axiom} Increasing(x: real, y: real) requires x < y ensures Exp(x) < Exp(y) lemma {:axiom} EvalOne() ensures 2.718281828 <= Exp(1.0)...
module Exponential { ghost function {:axiom} Exp(x: real): real lemma {:axiom} FunctionalEquation(x: real, y: real) ensures Exp(x + y) == Exp(x) * Exp(y) lemma {:axiom} Increasing(x: real, y: real) requires x < y ensures Exp(x) < Exp(y) lemma {:axiom} EvalOne() ensures 2.718281828 <= Exp(1.0)...
111
Dafny-VMC_tmp_tmpzgqv0i1u_src_Math_Helper.dfy
/******************************************************************************* * Copyright by the contributors to the Dafny Project * SPDX-License-Identifier: MIT *******************************************************************************/ module Helper { /************ Definitions ************/ fu...
/******************************************************************************* * Copyright by the contributors to the Dafny Project * SPDX-License-Identifier: MIT *******************************************************************************/ module Helper { /************ Definitions ************/ fu...
112
Dafny-demo_tmp_tmpkgr_dvdi_Dafny_BinarySearch.dfy
predicate sorted(a: array?<int>, l: int, u: int) reads a requires a != null { forall i, j :: 0 <= l <= i <= j <= u < a.Length ==> a[i] <= a[j] } method BinarySearch(a: array?<int>, key: int) returns (index: int) requires a != null && sorted(a,0,a.Length-1); ensures index >= 0 ==> index < a.Length && a[index] ...
predicate sorted(a: array?<int>, l: int, u: int) reads a requires a != null { forall i, j :: 0 <= l <= i <= j <= u < a.Length ==> a[i] <= a[j] } method BinarySearch(a: array?<int>, key: int) returns (index: int) requires a != null && sorted(a,0,a.Length-1); ensures index >= 0 ==> index < a.Length && a[index] ...
113
Dafny-experiences_tmp_tmp150sm9qy_dafny_started_tutorial_dafny_tutorial_array.dfy
method FindMax(a: array<int>) returns (i: int) // Annotate this method with pre- and postconditions // that ensure it behaves as described. requires a.Length > 0 ensures 0<= i < a.Length ensures forall k :: 0 <= k < a.Length ==> a[k] <= a[i] { // Fill in the body that calculates the INDEX of the maximum. ...
method FindMax(a: array<int>) returns (i: int) // Annotate this method with pre- and postconditions // that ensure it behaves as described. requires a.Length > 0 ensures 0<= i < a.Length ensures forall k :: 0 <= k < a.Length ==> a[k] <= a[i] { // Fill in the body that calculates the INDEX of the maximum. ...
114
Dafny-programs_tmp_tmpnso9eu7u_Algorithms + sorting_bubble-sort.dfy
/* Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. */ predicate sorted_between(A:array<int>, from:int, to:int) reads A { forall i, j :: 0 <= i <= j < A.Length && from <= i <= j <= to ==> A[i] <= A[j] } predicate sorted(A:array<...
/* Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order. */ predicate sorted_between(A:array<int>, from:int, to:int) reads A { forall i, j :: 0 <= i <= j < A.Length && from <= i <= j <= to ==> A[i] <= A[j] } predicate sorted(A:array<...
115
DafnyExercises_tmp_tmpd6qyevja_Part1_Q1.dfy
method addArrays(a : array<int>, b : array<int>) returns (c : array<int>) requires a.Length == b.Length ensures b.Length == c.Length ensures forall i:int :: 0 <= i <c.Length ==> c[i] == a[i] + b[i] { c := new int[a.Length]; var j := 0; while (j < a.Length) invariant 0 <= j <= c.Length in...
method addArrays(a : array<int>, b : array<int>) returns (c : array<int>) requires a.Length == b.Length ensures b.Length == c.Length ensures forall i:int :: 0 <= i <c.Length ==> c[i] == a[i] + b[i] { c := new int[a.Length]; var j := 0; while (j < a.Length) { c[j] := a[j] + b[j]; ...
116
DafnyExercises_tmp_tmpd6qyevja_QuickExercises_testing2.dfy
// predicate recSorted(s : string) decreases s // { // if (|s| <=1) then true else if(s[0] > s[1]) then false else recSorted(s[1..]) // } // predicate forallSorted (s : string) // { // forall x,y::0<x<y<|s|==>s[x]<s[y] // } // lemma forallEQrec(a:string) // ensures forallSorted(a) == recSorted(a) { ...
// predicate recSorted(s : string) decreases s // { // if (|s| <=1) then true else if(s[0] > s[1]) then false else recSorted(s[1..]) // } // predicate forallSorted (s : string) // { // forall x,y::0<x<y<|s|==>s[x]<s[y] // } // lemma forallEQrec(a:string) // ensures forallSorted(a) == recSorted(a) { ...
117
DafnyPrograms_tmp_tmp74_f9k_c_automaton.dfy
/** Consider cellular automata: a row of cells is repeatedly updated according to a rule. In this exercise I dabbled with, each cell has the value either false or true. Each cell's next state depends only on the immediate neighbours, in the case where the cell is at the edges of the row, the inexistent neighbours are ...
/** Consider cellular automata: a row of cells is repeatedly updated according to a rule. In this exercise I dabbled with, each cell has the value either false or true. Each cell's next state depends only on the immediate neighbours, in the case where the cell is at the edges of the row, the inexistent neighbours are ...
118
DafnyPrograms_tmp_tmp74_f9k_c_invertarray.dfy
/** Inverts an array of ints. */ method InvertArray(a: array<int>) modifies a ensures forall i | 0 <= i < a.Length :: a[i] == old(a[a.Length-1-i]) { var index := 0; while index < a.Length / 2 invariant 0 <= index <= a.Length / 2 // the elements i before position index are already switched with the o...
/** Inverts an array of ints. */ method InvertArray(a: array<int>) modifies a ensures forall i | 0 <= i < a.Length :: a[i] == old(a[a.Length-1-i]) { var index := 0; while index < a.Length / 2 // the elements i before position index are already switched with the old value of position a.Length - 1 - i ...
119
DafnyPrograms_tmp_tmp74_f9k_c_map-multiset-implementation.dfy
/** This ADT represents a multiset. */ trait MyMultiset { // internal invariant ghost predicate Valid() reads this // abstract variable ghost var theMultiset: multiset<int> method Add(elem: int) returns (didChange: bool) modifies this requires Valid() ensures Valid() ensures theMulti...
/** This ADT represents a multiset. */ trait MyMultiset { // internal invariant ghost predicate Valid() reads this // abstract variable ghost var theMultiset: multiset<int> method Add(elem: int) returns (didChange: bool) modifies this requires Valid() ensures Valid() ensures theMulti...
120
DafnyPrograms_tmp_tmp74_f9k_c_prime-database.dfy
//predicate for primeness ghost predicate prime(n: nat) { n > 1 && (forall nr | 1 < nr < n :: n % nr != 0) } datatype Answer = Yes | No | Unknown //the class containing a prime database, if a number is prime it returns Yes, if it is not No and if the number //is not in the database it returns Unknown class {:autocon...
//predicate for primeness ghost predicate prime(n: nat) { n > 1 && (forall nr | 1 < nr < n :: n % nr != 0) } datatype Answer = Yes | No | Unknown //the class containing a prime database, if a number is prime it returns Yes, if it is not No and if the number //is not in the database it returns Unknown class {:autocon...
121
DafnyProjects_tmp_tmp2acw_s4s_CombNK.dfy
/* * Formal specification and verification of a dynamic programming algorithm for calculating C(n, k). * FEUP, MIEIC, MFES, 2020/21. */ // Initial recursive definition of C(n, k), based on the Pascal equality. function comb(n: nat, k: nat): nat requires 0 <= k <= n { if k == 0 || k == n then 1 else comb(n-1, k)...
/* * Formal specification and verification of a dynamic programming algorithm for calculating C(n, k). * FEUP, MIEIC, MFES, 2020/21. */ // Initial recursive definition of C(n, k), based on the Pascal equality. function comb(n: nat, k: nat): nat requires 0 <= k <= n { if k == 0 || k == n then 1 else comb(n-1, k)...
122
DafnyProjects_tmp_tmp2acw_s4s_Graph.dfy
// Simple directed graph with vertices of any type T. class {:autocontracts} Graph<T(==)> { var V: set<T>; // vertex-set var E: set<(T, T)>; // edge-set // Class invariant. ghost predicate Valid() { // edges must refer to vertices that belong to the vertex-set // and self-loops (edges connec...
// Simple directed graph with vertices of any type T. class {:autocontracts} Graph<T(==)> { var V: set<T>; // vertex-set var E: set<(T, T)>; // edge-set // Class invariant. ghost predicate Valid() { // edges must refer to vertices that belong to the vertex-set // and self-loops (edges connec...
123
DafnyProjects_tmp_tmp2acw_s4s_Power.dfy
/* * Formal verification of an O(log n) algorithm to calculate the natural power of a real number (x^n), * illustrating the usage of lemmas and automatic induction in Dafny. * J. Pascoal Faria, FEUP, Jan/2022. */ // Recursive definition of x^n in functional style, with time and space complexity O(n). function power...
/* * Formal verification of an O(log n) algorithm to calculate the natural power of a real number (x^n), * illustrating the usage of lemmas and automatic induction in Dafny. * J. Pascoal Faria, FEUP, Jan/2022. */ // Recursive definition of x^n in functional style, with time and space complexity O(n). function power...
124
DafnyProjects_tmp_tmp2acw_s4s_RawSort.dfy
/** * Proves the correctness of a "raw" array sorting algorithm that swaps elements out of order, chosen randomly. * FEUP, MFES, 2020/21. */ // Type of each array element; can be any type supporting comparision operators. type T = int // Checks if array 'a' is sorted by non-descending order. ghost predicate sorte...
/** * Proves the correctness of a "raw" array sorting algorithm that swaps elements out of order, chosen randomly. * FEUP, MFES, 2020/21. */ // Type of each array element; can be any type supporting comparision operators. type T = int // Checks if array 'a' is sorted by non-descending order. ghost predicate sorte...
125
DafnyProjects_tmp_tmp2acw_s4s_findMax.dfy
/* * Formal verification of a simple algorithm to find the maximum value in an array. * FEUP, MIEIC, MFES, 2020/21. */ // Finds the maximum value in a non-empty array. method findMax(a: array<real>) returns (max: real) requires a.Length > 0 ensures exists k :: 0 <= k < a.Length && max == a[k] ensures forall k :...
/* * Formal verification of a simple algorithm to find the maximum value in an array. * FEUP, MIEIC, MFES, 2020/21. */ // Finds the maximum value in a non-empty array. method findMax(a: array<real>) returns (max: real) requires a.Length > 0 ensures exists k :: 0 <= k < a.Length && max == a[k] ensures forall k :...
126
DafnyProjects_tmp_tmp2acw_s4s_longestPrefix.dfy
// MFES, Exam 8/Sept/20201, Exercise 5 // Computes the length (i) of the longest common prefix (initial subarray) // of two arrays a and b. method longestPrefix(a: array<int>, b: array <int>) returns (i: nat) ensures i <= a.Length && i <= b.Length ensures a[..i] == b[..i] ensures i < a.Length && i < b.Length ==...
// MFES, Exam 8/Sept/20201, Exercise 5 // Computes the length (i) of the longest common prefix (initial subarray) // of two arrays a and b. method longestPrefix(a: array<int>, b: array <int>) returns (i: nat) ensures i <= a.Length && i <= b.Length ensures a[..i] == b[..i] ensures i < a.Length && i < b.Length ==...
127
DafnyProjects_tmp_tmp2acw_s4s_partitionOddEven.dfy
// Rearranges the elements in an array 'a' of natural numbers, // so that all odd numbers appear before all even numbers. method partitionOddEven(a: array<nat>) modifies a ensures multiset(a[..]) == multiset(old(a[..])) ensures ! exists i, j :: 0 <= i < j < a.Length && even(a[i]) && odd(a[j]) { var i := 0;...
// Rearranges the elements in an array 'a' of natural numbers, // so that all odd numbers appear before all even numbers. method partitionOddEven(a: array<nat>) modifies a ensures multiset(a[..]) == multiset(old(a[..])) ensures ! exists i, j :: 0 <= i < j < a.Length && even(a[i]) && odd(a[j]) { var i := 0;...
128
DafnyProjects_tmp_tmp2acw_s4s_sqrt.dfy
method sqrt(x: real) returns (r: real) requires x >= 0.0 ensures r * r == x && r >= 0.0 method testSqrt() { var r := sqrt(4.0); //if (2.0 < r) { monotonicSquare(2.0, r); } if (r < 2.0) { monotonicSquare(r, 2.0); } assert r == 2.0; } lemma monotonicMult(c: real, x: real, y: real) requires x < y && c > 0...
method sqrt(x: real) returns (r: real) requires x >= 0.0 ensures r * r == x && r >= 0.0 method testSqrt() { var r := sqrt(4.0); //if (2.0 < r) { monotonicSquare(2.0, r); } if (r < 2.0) { monotonicSquare(r, 2.0); } } lemma monotonicMult(c: real, x: real, y: real) requires x < y && c > 0.0 ensures c * x ...
129
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week1_7_A2_Q1_trimmed copy - 副本.dfy
ghost function Count(hi: nat, s:seq<int>): int requires 0 <= hi <= |s| decreases hi { if hi == 0 then 0 else if s[hi-1]%2 == 0 then 1 + Count(hi-1, s) else Count(hi-1, s) } method FooCount(CountIndex:nat, a:seq<int>,b:array<int>) returns (p:nat) requires CountIndex == 0 || (|a| == b.Length && 1 <=...
ghost function Count(hi: nat, s:seq<int>): int requires 0 <= hi <= |s| { if hi == 0 then 0 else if s[hi-1]%2 == 0 then 1 + Count(hi-1, s) else Count(hi-1, s) } method FooCount(CountIndex:nat, a:seq<int>,b:array<int>) returns (p:nat) requires CountIndex == 0 || (|a| == b.Length && 1 <= CountIndex <= |a...
130
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week1_7_MaxSum.dfy
method MaxSum(x:int, y:int) returns (s:int, m:int) ensures s == x+y ensures (m == x || m == y) && x <= m && y <= m { s := x+y; if x > y{ m := x; } else if y > x{ m := y; } else { m := x; } assert m >= y; } method Main() { var m, n := 4,5; var a,b := MaxSum(m,n); ...
method MaxSum(x:int, y:int) returns (s:int, m:int) ensures s == x+y ensures (m == x || m == y) && x <= m && y <= m { s := x+y; if x > y{ m := x; } else if y > x{ m := y; } else { m := x; } } method Main() { var m, n := 4,5; var a,b := MaxSum(m,n); print "Search retu...
131
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week1_7_Week4__LinearSearch.dfy
method LinearSeach0<T>(a: array<T>, P: T -> bool) returns (n: int) ensures 0 <= n <= a.Length ensures n == a.Length || P(a[n]) { n := 0; while n != a.Length invariant 0 <= n <= a.Length { if P(a[n]) {return;} n := n + 1; } } predicate P(n: int) { n % ...
method LinearSeach0<T>(a: array<T>, P: T -> bool) returns (n: int) ensures 0 <= n <= a.Length ensures n == a.Length || P(a[n]) { n := 0; while n != a.Length { if P(a[n]) {return;} n := n + 1; } } predicate P(n: int) { n % 2 == 0 } method TestLinearSearch() {...
132
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week1_7_week4_tute_ex4.dfy
method LinearSearch<T>(a: array<T>, P: T -> bool) returns (n: int) ensures -1 <= n < a.Length ensures n == -1 || P(a[n]) ensures n != -1 ==> forall i :: 0 <= i < n ==> ! P(a[i]) ensures n == -1 ==> forall i :: 0 <= i < a.Length ==> ! P(a[i]) { n := 0; while n != a.Length decreases a.Len...
method LinearSearch<T>(a: array<T>, P: T -> bool) returns (n: int) ensures -1 <= n < a.Length ensures n == -1 || P(a[n]) ensures n != -1 ==> forall i :: 0 <= i < n ==> ! P(a[i]) ensures n == -1 ==> forall i :: 0 <= i < a.Length ==> ! P(a[i]) { n := 0; while n != a.Length { if P(a[n]...
133
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week1_7_week5_ComputePower.dfy
function Power(n:nat):nat { if n == 0 then 1 else 2 * Power(n-1) } method CalcPower(n:nat) returns (p:nat) ensures p == 2*n; { p := 2*n; } method ComputePower(n:nat) returns (p:nat) ensures p == Power(n) { p:=1; var i:=0; while i!=n invariant 0 <= i <= n invariant p *Powe...
function Power(n:nat):nat { if n == 0 then 1 else 2 * Power(n-1) } method CalcPower(n:nat) returns (p:nat) ensures p == 2*n; { p := 2*n; } method ComputePower(n:nat) returns (p:nat) ensures p == Power(n) { p:=1; var i:=0; while i!=n { p:= CalcPower(p); i:=i+1; } }...
134
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_a3 copy 2.dfy
class TwoStacks<T(0)(==)> { //abstract state ghost var s1 :seq<T> ghost var s2 :seq<T> ghost const N :nat // maximum size of the stacks ghost var Repr : set<object> //concrete state var data: array<T> var n1: nat // number of elements in the stack 1 var n2: nat // number of elements...
class TwoStacks<T(0)(==)> { //abstract state ghost var s1 :seq<T> ghost var s2 :seq<T> ghost const N :nat // maximum size of the stacks ghost var Repr : set<object> //concrete state var data: array<T> var n1: nat // number of elements in the stack 1 var n2: nat // number of elements...
135
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_a3_search_findPositionOfIndex.dfy
method FindPositionOfElement(a:array<int>,Element:nat,n1:nat,s1:seq<int>) returns (Position:int,Count:nat) requires n1 == |s1| && 0 <= n1 <= a.Length requires forall i:: 0<= i < |s1| ==> a[i] == s1[i] ensures Position == -1 || Position >= 1 ensures |s1| != 0 && Position >= 1 ==> exists i...
method FindPositionOfElement(a:array<int>,Element:nat,n1:nat,s1:seq<int>) returns (Position:int,Count:nat) requires n1 == |s1| && 0 <= n1 <= a.Length requires forall i:: 0<= i < |s1| ==> a[i] == s1[i] ensures Position == -1 || Position >= 1 ensures |s1| != 0 && Position >= 1 ==> exists i...
136
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_week10_BoundedQueue_01.dfy
class BoundedQueue<T(0)> { // abstract state ghost var contents: seq<T> // the contents of the bounded queue ghost var N: nat // the (maximum) size of the bounded queue ghost var Repr: set<object> // concrete state var data: array<T> var wr: nat var rd: nat ghost predicate Valid() reads this, Repr ensures...
class BoundedQueue<T(0)> { // abstract state ghost var contents: seq<T> // the contents of the bounded queue ghost var N: nat // the (maximum) size of the bounded queue ghost var Repr: set<object> // concrete state var data: array<T> var wr: nat var rd: nat ghost predicate Valid() reads this, Repr ensures...
137
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_week10_ExtensibleArray.dfy
class ExtensibleArray<T(0)> { // abstract state ghost var Elements: seq<T> ghost var Repr: set<object> //concrete state var front: array?<T> var depot: ExtensibleArray?<array<T>> var length: int // number of elements var M: int // number of elements in depot ghost predicate Valid() decreases ...
class ExtensibleArray<T(0)> { // abstract state ghost var Elements: seq<T> ghost var Repr: set<object> //concrete state var front: array?<T> var depot: ExtensibleArray?<array<T>> var length: int // number of elements var M: int // number of elements in depot ghost predicate Valid() reads this...
138
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_week8_CheckSumCalculator.dfy
ghost function Hash(s:string):int { SumChars(s) % 137 } ghost function SumChars(s: string):int { if |s| == 0 then 0 else s[|s| - 1] as int + SumChars(s[..|s| -1]) } class CheckSumCalculator{ var data: string var cs:int ghost predicate Valid() reads this { cs == Hash(da...
ghost function Hash(s:string):int { SumChars(s) % 137 } ghost function SumChars(s: string):int { if |s| == 0 then 0 else s[|s| - 1] as int + SumChars(s[..|s| -1]) } class CheckSumCalculator{ var data: string var cs:int ghost predicate Valid() reads this { cs == Hash(da...
139
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_week8_CoffeeMaker2.dfy
class Grinder { ghost var hasBeans: bool ghost var Repr: set<object> ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr constructor() ensures Valid() && fresh(Repr) && !hasBeans function Ready(): bool requires Valid() reads Repr ensures Ready() == hasBeans ...
class Grinder { ghost var hasBeans: bool ghost var Repr: set<object> ghost predicate Valid() reads this, Repr ensures Valid() ==> this in Repr constructor() ensures Valid() && fresh(Repr) && !hasBeans function Ready(): bool requires Valid() reads Repr ensures Ready() == hasBeans ...
140
Dafny_Learning_Experience_tmp_tmpuxvcet_u_week8_12_week9_lemma.dfy
method AssignmentsToMark(students:int, tutors: int) returns (r:int) requires students > 0 && tutors > 1 ensures r < students { assert students > 0 && tutors > 1; assert students > 0 && tutors > 1 && true; assert students > 0 && tutors > 1 && students/tutors < students ==> students/tutors < students;...
method AssignmentsToMark(students:int, tutors: int) returns (r:int) requires students > 0 && tutors > 1 ensures r < students { DivisionLemma(students,tutors); r:= students/tutors; calc { //true; 1/tutors < 1; students/tutors < students; } } lemma DivisionLemma(n:int,d:i...
141
Dafny_ProgrammingLanguages_tmp_tmp82_e0kji_ExtraCredit.dfy
datatype Exp = Const(int) | Var(string) | Plus(Exp, Exp) | Mult(Exp, Exp) function eval(e:Exp, store:map<string, int>):int { match(e) case Const(n) => n case Var(s) => if(s in store) then store[s] else -1 case Plus(e1, e2) => eval(e1, store) + eval(e2, store) case Mult(e1, e2) => eval(e1, store) * eval(e2, s...
datatype Exp = Const(int) | Var(string) | Plus(Exp, Exp) | Mult(Exp, Exp) function eval(e:Exp, store:map<string, int>):int { match(e) case Const(n) => n case Var(s) => if(s in store) then store[s] else -1 case Plus(e1, e2) => eval(e1, store) + eval(e2, store) case Mult(e1, e2) => eval(e1, store) * eval(e2, s...
142
Dafny_Programs_tmp_tmp99966ew4_binary_search.dfy
predicate sorted(a: array<int>) requires a != null reads a { forall j, k :: 0 <= j < k < a.Length ==> a[j] <= a[k] } method BinarySearch(a: array<int>, value: int) returns (index: int) requires a != null && 0 <= a.Length && sorted(a) ensures 0 <= index ==> index < a.Length && a[index] == value ensures...
predicate sorted(a: array<int>) requires a != null reads a { forall j, k :: 0 <= j < k < a.Length ==> a[j] <= a[k] } method BinarySearch(a: array<int>, value: int) returns (index: int) requires a != null && 0 <= a.Length && sorted(a) ensures 0 <= index ==> index < a.Length && a[index] == value ensures...
143
Dafny_Programs_tmp_tmp99966ew4_lemma.dfy
lemma SkippingLemma(a : array<int>, j : int) requires a != null requires forall i :: 0 <= i < a.Length ==> 0 <= a[i] requires forall i :: 0 < i < a.Length ==> a[i-1]-1 <= a[i] requires 0 <= j < a.Length ensures forall k :: j <= k < j + a[j] && k < a.Length ==> a[k] != 0 { var i := j; while i < j + ...
lemma SkippingLemma(a : array<int>, j : int) requires a != null requires forall i :: 0 <= i < a.Length ==> 0 <= a[i] requires forall i :: 0 < i < a.Length ==> a[i-1]-1 <= a[i] requires 0 <= j < a.Length ensures forall k :: j <= k < j + a[j] && k < a.Length ==> a[k] != 0 { var i := j; while i < j + ...
144
Dafny_Programs_tmp_tmp99966ew4_mymax.dfy
method Max(a: int, b:int) returns (c: int) ensures c >= a && c>= b { if (a < b) { c := b; } else { c := a; } assert a <= c && b <= c; } method Testing() { var v := Max(2, 3); assert v >= 3; }
method Max(a: int, b:int) returns (c: int) ensures c >= a && c>= b { if (a < b) { c := b; } else { c := a; } } method Testing() { var v := Max(2, 3); }
145
Dafny_Programs_tmp_tmp99966ew4_trig.dfy
predicate P(x: int) predicate Q(x: int) method test() requires forall x {:trigger P(x)} :: P(x) && Q(x) ensures Q(0) { assert P(0); }
predicate P(x: int) predicate Q(x: int) method test() requires forall x {:trigger P(x)} :: P(x) && Q(x) ensures Q(0) { }
146
Dafny_Verify_tmp_tmphq7j0row_AI_agent_validation_examples.dfy
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(N: int) returns (y: nat) requires N >= 0 ensures y == Power(N) { y := 1; var x := 0; while x != N invariant 0 <= x <= N invariant y == Power(x) decreases N - x { x, y :=...
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(N: int) returns (y: nat) requires N >= 0 ensures y == Power(N) { y := 1; var x := 0; while x != N { x, y := x + 1, y + y; } } // Original davinci-003 completion: // method ComputePower1(N:...
147
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_ComputePower.dfy
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(N: int) returns (y: nat) requires N >= 0 ensures y == Power(N) { y := 1; var x := 0; while x != N invariant 0 <= x <= N invariant y == Power(x) decreases N - x { x, y :=...
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(N: int) returns (y: nat) requires N >= 0 ensures y == Power(N) { y := 1; var x := 0; while x != N { x, y := x + 1, y + y; } }
148
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_CopyMatrix.dfy
method CopyMatrix(src: array2, dst: array2) requires src.Length0 == dst.Length0 && src.Length1 == dst.Length1 modifies dst ensures forall i, j :: 0 <= i < src.Length0 && 0 <= j < src.Length1 ==> dst[i,j] == old(src[i,j]) { var m := 0; while m != src.Length0 invariant 0 <= m <= src.Length0 ...
method CopyMatrix(src: array2, dst: array2) requires src.Length0 == dst.Length0 && src.Length1 == dst.Length1 modifies dst ensures forall i, j :: 0 <= i < src.Length0 && 0 <= j < src.Length1 ==> dst[i,j] == old(src[i,j]) { var m := 0; while m != src.Length0 { var n := 0; while n...
149
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_Cube.dfy
method Cube(n: nat) returns (c: nat) ensures c == n * n * n { c := 0; var i := 0; var k := 1; var m := 6; while i != n invariant 0 <= i <= n invariant c == i * i * i invariant k == 3*i*i + 3*i + 1 invariant m == 6 * i + 6 { c, k, m := c + k, k + m, ...
method Cube(n: nat) returns (c: nat) ensures c == n * n * n { c := 0; var i := 0; var k := 1; var m := 6; while i != n { c, k, m := c + k, k + m, m + 6; i := i + 1; } }
150
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_DoubleArray.dfy
method DoubleArray(src: array<int>, dst: array<int>) requires src.Length == dst.Length modifies dst ensures forall i :: 0 <= i < src.Length ==> dst[i] == 2 * old(src[i]) { var n := 0; while n != src.Length invariant 0 <= n <= src.Length invariant forall i :: 0 <= i < n ==> dst[i] == 2 * old(...
method DoubleArray(src: array<int>, dst: array<int>) requires src.Length == dst.Length modifies dst ensures forall i :: 0 <= i < src.Length ==> dst[i] == 2 * old(src[i]) { var n := 0; while n != src.Length { dst[n] := 2 * src[n]; n := n + 1; } }
151
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_IncrementMatrix.dfy
method IncrementMatrix(a: array2<int>) modifies a ensures forall i, j :: 0 <= i < a.Length0 && 0 <= j < a.Length1 ==> a[i,j] == old(a[i,j]) + 1 { var m := 0; while m != a.Length0 invariant 0 <= m <= a.Length0 invariant forall i, j :: 0 <= i < m && 0 <= j < a.Length1 ==> a[i,j] == old(a[i...
method IncrementMatrix(a: array2<int>) modifies a ensures forall i, j :: 0 <= i < a.Length0 && 0 <= j < a.Length1 ==> a[i,j] == old(a[i,j]) + 1 { var m := 0; while m != a.Length0 { var n := 0; while n != a.Length1 { a[m,n] := a[m,n] + 1; n := n + 1; ...
152
Dafny_Verify_tmp_tmphq7j0row_AI_agent_verify_examples_RotateRight.dfy
method RotateRight(a: array) requires a.Length > 0 modifies a ensures forall i :: 1<= i < a.Length ==> a[i] == old(a[(i-1)]) ensures a[0] == old(a[a.Length-1]) { var n := 1; while n != a.Length invariant 1 <= n <= a.Length invariant forall i :: 1 <= i < n ==> a[i] == old(a[i-1])...
method RotateRight(a: array) requires a.Length > 0 modifies a ensures forall i :: 1<= i < a.Length ==> a[i] == old(a[(i-1)]) ensures a[0] == old(a[a.Length-1]) { var n := 1; while n != a.Length { a[0], a[n] := a[n], a[0]; n := n + 1; } }
153
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_28.dfy
method main(x: int, y: int) returns (x_out: int, y_out: int, n: int) requires x >= 0 requires y >= 0 requires x == y ensures y_out == n { x_out := x; y_out := y; n := 0; while (x_out != n) invariant x_out >= 0 invariant x_out == y_out { x_out := x_out - 1; y_out := y...
method main(x: int, y: int) returns (x_out: int, y_out: int, n: int) requires x >= 0 requires y >= 0 requires x == y ensures y_out == n { x_out := x; y_out := y; n := 0; while (x_out != n) { x_out := x_out - 1; y_out := y_out - 1; } }
154
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_37.dfy
method main(n: int) returns(x: int, m: int) requires n > 0 ensures (n <= 0) || (0 <= m && m < n) { x := 0; m := 0; while(x < n) invariant 0 <= x <= n invariant 0 <= m < n { if(*) { m := x; } else{} x := x + 1; } }
method main(n: int) returns(x: int, m: int) requires n > 0 ensures (n <= 0) || (0 <= m && m < n) { x := 0; m := 0; while(x < n) { if(*) { m := x; } else{} x := x + 1; } }
155
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_38.dfy
method main(n : int) returns (i: int, x: int, y:int) requires n >= 0 ensures (i % 2 != 0) || (x == 2 * y) { i := 0; x := 0; y := 0; while (i < n) invariant 0 <= i <= n invariant x == i invariant y == i / 2 { i := i + 1; x := x + 1; if (i % 2 == 0) ...
method main(n : int) returns (i: int, x: int, y:int) requires n >= 0 ensures (i % 2 != 0) || (x == 2 * y) { i := 0; x := 0; y := 0; while (i < n) { i := i + 1; x := x + 1; if (i % 2 == 0) { y := y + 1; } else {} } }
156
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_41.dfy
method main(n: int, k: int) returns (i :int, j: int) requires n >= 0 requires k == 1 || k >= 0 ensures k + i + j >= 2 * n { i := 0; j := 0; while(i < n) invariant 0 <= i <= n invariant j == i * (i + 1) / 2 { i := i + 1; j := j + i; } }
method main(n: int, k: int) returns (i :int, j: int) requires n >= 0 requires k == 1 || k >= 0 ensures k + i + j >= 2 * n { i := 0; j := 0; while(i < n) { i := i + 1; j := j + i; } }
157
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_BinarySearch.dfy
method BinarySearch(a: array<int>, key: int) returns (n: int) requires forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures 0 <= n <= a.Length ensures forall i :: 0 <= i < n ==> a[i] < key ensures forall i :: n <= i < a.Length ==> key <= a[i] { var lo, hi := 0, a.Length; while lo < hi ...
method BinarySearch(a: array<int>, key: int) returns (n: int) requires forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures 0 <= n <= a.Length ensures forall i :: 0 <= i < n ==> a[i] < key ensures forall i :: n <= i < a.Length ==> key <= a[i] { var lo, hi := 0, a.Length; while lo < hi ...
158
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_50_examples_SumArray.dfy
function Sum(arr: array<int>, len: int): int reads arr requires arr.Length > 0 && 0 <= len <= arr.Length { if len == 0 then 0 else arr[len-1] + Sum(arr, len-1) } method SumArray(arr: array<int>) returns (sum: int) requires arr.Length > 0 ensures sum == Sum(arr, arr.Length) { sum := 0; var i...
function Sum(arr: array<int>, len: int): int reads arr requires arr.Length > 0 && 0 <= len <= arr.Length { if len == 0 then 0 else arr[len-1] + Sum(arr, len-1) } method SumArray(arr: array<int>) returns (sum: int) requires arr.Length > 0 ensures sum == Sum(arr, arr.Length) { sum := 0; var i...
159
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_error_data_completion_06_n.dfy
method Main() returns (x: int, y: int) ensures x == y; { x := 0; y := 0; var w := 1; var z := 0; var turn := 0; while(x != y) invariant x == y ==> !(0 <= -z*2/2 && 1 <= -(w-1)*2/2) invariant !((x != y && x - y <= -1) || (x - y >= 1 && -z*2/2 <= 0 && (w-1)*2/2 <= 1)) invariant !(w*2/2 <= 0 && ((x != y ...
method Main() returns (x: int, y: int) ensures x == y; { x := 0; y := 0; var w := 1; var z := 0; var turn := 0; while(x != y) { if(turn == 0){ turn := 1; } if(turn == 1){ if(w % 2 == 1){ x := x + 1; } if(z % 2 == 0){ y := y + 1; } turn := 1; } else{ if(turn == 2){ ...
160
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_error_data_completion_07.dfy
method main(n: int) returns (a: int, b: int) requires n >= 0 ensures a + b == 3 * n { var i: int := 0; a := 0; b := 0; while(i < n) invariant 0 <= i <= n invariant a + b == 3 * i { if(*) { a := a + 1; b := b + 2; } else...
method main(n: int) returns (a: int, b: int) requires n >= 0 ensures a + b == 3 * n { var i: int := 0; a := 0; b := 0; while(i < n) { if(*) { a := a + 1; b := b + 2; } else { a := a + 2; b := b + 1; ...
161
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_error_data_completion_11.dfy
method main(x :int) returns (j :int, i :int) requires x > 0 ensures j == 2 * x { i := 0; j := 0; while i < x invariant 0 <= i <= x invariant j == 2 * i { j := j + 2; i := i + 1; } }
method main(x :int) returns (j :int, i :int) requires x > 0 ensures j == 2 * x { i := 0; j := 0; while i < x { j := j + 2; i := i + 1; } }
162
Dafny_Verify_tmp_tmphq7j0row_Fine_Tune_Examples_normal_data_completion_MaxPerdV2.dfy
function contains(v: int, a: array<int>, n: int): bool reads a requires n <= a.Length { exists j :: 0 <= j < n && a[j] == v } function upper_bound(v: int, a: array<int>, n: int): bool reads a requires n <= a.Length { forall j :: 0 <= j < n ==> a[j] <= v } function is_max(m: int, a: array<int>, n: int): bool r...
function contains(v: int, a: array<int>, n: int): bool reads a requires n <= a.Length { exists j :: 0 <= j < n && a[j] == v } function upper_bound(v: int, a: array<int>, n: int): bool reads a requires n <= a.Length { forall j :: 0 <= j < n ==> a[j] <= v } function is_max(m: int, a: array<int>, n: int): bool r...
163
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_15.dfy
method main(n: int, k: int) returns (k_out: int) requires n > 0; requires k > n; ensures k_out >= 0; { k_out := k; var j: int := 0; while(j < n) invariant 0 <= j <= n; invariant k_out == k - j; { j := j + 1; k_out := k_out - 1; } }
method main(n: int, k: int) returns (k_out: int) requires n > 0; requires k > n; ensures k_out >= 0; { k_out := k; var j: int := 0; while(j < n) { j := j + 1; k_out := k_out - 1; } }
164
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_ComputePower.dfy
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(n: nat) returns (p: nat) ensures p == Power(n) { p := 1; var i := 0; while i != n invariant 0 <= i <= n && p == Power(i) { i := i + 1; p := p * 2; } }
function Power(n: nat): nat { if n == 0 then 1 else 2 * Power(n - 1) } method ComputePower(n: nat) returns (p: nat) ensures p == Power(n) { p := 1; var i := 0; while i != n { i := i + 1; p := p * 2; } }
165
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_Count.dfy
function has_count(v: int, a: array<int>, n: int): int reads a // This allows the function to read from array 'a' requires n >= 0 && n <= a.Length { if n == 0 then 0 else (if a[n-1] == v then has_count(v, a, n-1) + 1 else has_count(v, a, n-1)) } method count (v: int, a: array<int>, n: int) returns (r...
function has_count(v: int, a: array<int>, n: int): int reads a // This allows the function to read from array 'a' requires n >= 0 && n <= a.Length { if n == 0 then 0 else (if a[n-1] == v then has_count(v, a, n-1) + 1 else has_count(v, a, n-1)) } method count (v: int, a: array<int>, n: int) returns (r...
166
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_LinearSearch.dfy
method LinearSearch<T>(a: array<T>, P: T -> bool) returns (n: int) ensures 0 <= n <= a.Length ensures n == a.Length || P(a[n]) ensures forall i :: 0 <= i < n ==> !P(a[i]) { n := 0; while n != a.Length invariant 0 <= n <= a.Length invariant forall i :: 0 <= i < n ==> !P(a[i]) { ...
method LinearSearch<T>(a: array<T>, P: T -> bool) returns (n: int) ensures 0 <= n <= a.Length ensures n == a.Length || P(a[n]) ensures forall i :: 0 <= i < n ==> !P(a[i]) { n := 0; while n != a.Length { if P(a[n]) { return; } n := n + 1; } }
167
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_Minimum.dfy
method Minimum(a: array<int>) returns (m: int) requires a.Length > 0 ensures exists i :: 0 <= i < a.Length && m == a[i] ensures forall i :: 0 <= i < a.Length ==> m <= a[i] { var n := 0; m := a[0]; while n != a.Length invariant 0 <= n <= a.Length invariant exists i :: 0 <= i < a.Length && m == a[i] invarian...
method Minimum(a: array<int>) returns (m: int) requires a.Length > 0 ensures exists i :: 0 <= i < a.Length && m == a[i] ensures forall i :: 0 <= i < a.Length ==> m <= a[i] { var n := 0; m := a[0]; while n != a.Length { if a[n] < m { m := a[n]; } n := n + 1; } }
168
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_Mult.dfy
method mult(a:int, b:int) returns (x:int) requires a >= 0 && b >= 0 ensures x == a * b { x := 0; var y := a; while y > 0 invariant x == (a - y) * b { x := x + b; y := y - 1; } }
method mult(a:int, b:int) returns (x:int) requires a >= 0 && b >= 0 ensures x == a * b { x := 0; var y := a; while y > 0 { x := x + b; y := y - 1; } }
169
Dafny_Verify_tmp_tmphq7j0row_Generated_Code_rand.dfy
method Main(xInit: int, y: int) returns (z: int) requires xInit >= 0 requires y >= 0 ensures z == 0 { var x := xInit; z := x * y; while x > 0 invariant x >= 0 invariant z == x * y decreases x { x := x - 1; z := z - y; } }
method Main(xInit: int, y: int) returns (z: int) requires xInit >= 0 requires y >= 0 ensures z == 0 { var x := xInit; z := x * y; while x > 0 { x := x - 1; z := z - y; } }
170
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_Function.dfy
function Average (a: int, b: int): int { (a + b) / 2 } method TripleConditions(x: int) returns (r: int) ensures r == 3 * x { r := 3 * x; assert r == 3 * x; } method Triple' (x: int) returns (r: int) ensures Average(r, 3 * x) == 3 * x ensures r == 3 * x { r:= 3 * x; } method ProveSpecif...
function Average (a: int, b: int): int { (a + b) / 2 } method TripleConditions(x: int) returns (r: int) ensures r == 3 * x { r := 3 * x; } method Triple' (x: int) returns (r: int) ensures Average(r, 3 * x) == 3 * x ensures r == 3 * x { r:= 3 * x; } method ProveSpecificationsEquivalent(x: i...
171
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_Ghost.dfy
function Average(a: int, b: int): int { (a + b) / 2 } ghost method Triple(x: int) returns (r: int) ensures r == 3 * x { r := Average(2 * x, 4 * x); } method Triple1(x: int) returns (r: int) ensures r == 3 * x { var y := 2 * x; r := x + y; ghost var a, b := DoubleQuadruple(x); assert ...
function Average(a: int, b: int): int { (a + b) / 2 } ghost method Triple(x: int) returns (r: int) ensures r == 3 * x { r := Average(2 * x, 4 * x); } method Triple1(x: int) returns (r: int) ensures r == 3 * x { var y := 2 * x; r := x + y; ghost var a, b := DoubleQuadruple(x); } ghost me...
172
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_Index.dfy
method Index(n: int) returns (i: int) requires 1 <= n ensures 0 <= i < n { i := n/2; } method Min(x: int, y: int) returns (m: int) ensures m <= x && m <= y ensures m == x || m == y { if (x >= y) { m := y; } else { m := x; } assert m <= x && m <= y; } method Max(x: int, y: int) re...
method Index(n: int) returns (i: int) requires 1 <= n ensures 0 <= i < n { i := n/2; } method Min(x: int, y: int) returns (m: int) ensures m <= x && m <= y ensures m == x || m == y { if (x >= y) { m := y; } else { m := x; } } method Max(x: int, y: int) returns (m: int) { if (x >=...
173
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_LoopInvariant.dfy
method UpWhileLess(N: int) returns (i: int) requires 0 <= N ensures i == N { i := 0; while i < N invariant 0 <= i <= N decreases N - i { i := i + 1; } } method UpWhileNotEqual(N: int) returns (i: int) requires 0 <= N ensures i == N { i := 0; while i != N invariant 0 <= i <...
method UpWhileLess(N: int) returns (i: int) requires 0 <= N ensures i == N { i := 0; while i < N { i := i + 1; } } method UpWhileNotEqual(N: int) returns (i: int) requires 0 <= N ensures i == N { i := 0; while i != N { i := i + 1; } } method DownWhileNotEqual(N: int)...
174
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_Triple.dfy
method Triple(x: int) returns (r: int) { var y := 2 * x; r := x + y; assert r == 3 * x; } method TripleIf(x: int) returns (r: int) { if (x == 0) { r := 0; } else { var y := 2 * x; r := x + y; } assert r == 3 * x; } method TripleOver(x: int) returns (r: int) { if...
method Triple(x: int) returns (r: int) { var y := 2 * x; r := x + y; } method TripleIf(x: int) returns (r: int) { if (x == 0) { r := 0; } else { var y := 2 * x; r := x + y; } } method TripleOver(x: int) returns (r: int) { if { case x < 18 => var a, b := ...
175
Dafny_Verify_tmp_tmphq7j0row_Test_Cases_solved_1_select.dfy
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 < a.Length && j < n ==> a[i] <= a[j] invaria...
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 { var mindex, m := n, n; while m != a.Length { if a[m] < a[mindex] { mindex := m; } ...
176
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_01.dfy
method main() returns (t1: int, t2: int, x: int, y: int) ensures y >= 1 { x := 1; y := 1; t1 := 0; t2 := 0; while(x <= 100000) invariant x == y; { t1 := x; t2 := y; x := t1 + t2; y := t1 + t2; } }
method main() returns (t1: int, t2: int, x: int, y: int) ensures y >= 1 { x := 1; y := 1; t1 := 0; t2 := 0; while(x <= 100000) { t1 := x; t2 := y; x := t1 + t2; y := t1 + t2; } }
177
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_06_n.dfy
// MODULE main // int x; // int y; // int w; // int z; // int turn; // assume(x == 0); // assume(y == 0); // assume(z == 0); // assume(w == 1); // assume(turn == 0); // while(*){ // if(turn == 0){ // if(*){ // turn = 1; // } // else{ // turn = 2; // } // } // else{ // skip; //...
// MODULE main // int x; // int y; // int w; // int z; // int turn; // assume(x == 0); // assume(y == 0); // assume(z == 0); // assume(w == 1); // assume(turn == 0); // while(*){ // if(turn == 0){ // if(*){ // turn = 1; // } // else{ // turn = 2; // } // } // else{ // skip; //...
178
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_07.dfy
// MODULE main // int i; // int n; // int a; // int b; // assume(i == 0); // assume(a == 0); // assume(b == 0); // assume(n >= 0); // while(i < n){ // if(*) { // a = a+1; // b = b+2; // } // else { // a = a+2; // b = b+1; // } // i = i+1; // } // assert(a + b ...
// MODULE main // int i; // int n; // int a; // int b; // assume(i == 0); // assume(a == 0); // assume(b == 0); // assume(n >= 0); // while(i < n){ // if(*) { // a = a+1; // b = b+2; // } // else { // a = a+2; // b = b+1; // } // i = i+1; // } // assert(a + b ...
179
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_11.dfy
method main(x :int) returns (j :int, i :int) requires x > 0 ensures j == 2 * x { i := 0; j := 0; while i < x invariant 0 <= i <= x invariant j == 2 * i { j := j + 2; i := i + 1; } } // MODULE main // int i; // int j; // int x; // assume(j == 0); // assume(x >...
method main(x :int) returns (j :int, i :int) requires x > 0 ensures j == 2 * x { i := 0; j := 0; while i < x { j := j + 2; i := i + 1; } } // MODULE main // int i; // int j; // int x; // assume(j == 0); // assume(x > 0); // assume(i == 0); // while(i < x){ // j = j + 2;...
180
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_15.dfy
method main(n: int, k: int) returns (k_out: int) requires n > 0; requires k > n; ensures k_out >= 0; { k_out := k; var j: int := 0; while(j < n) invariant 0 <= j <= n; invariant j + k_out == k; { j := j + 1; k_out := k_out - 1; } } // C code: // MODULE main // int i; // in...
method main(n: int, k: int) returns (k_out: int) requires n > 0; requires k > n; ensures k_out >= 0; { k_out := k; var j: int := 0; while(j < n) { j := j + 1; k_out := k_out - 1; } } // C code: // MODULE main // int i; // int n; // int j; // int k; // assume(n > 0); // as...
181
Dafny_Verify_tmp_tmphq7j0row_dataset_C_convert_examples_23_x.dfy
method main(n: int) returns (sum: int, i: int) requires n >= 0 { sum := 0; i := 0; while(i < n) invariant sum >= 0 invariant 0 <= i <= n { sum := sum + i; i := i + 1; } } // MODULE main // int i; // int sum; // int n; // assume(sum == 0); // assume(n >= 0); /...
method main(n: int) returns (sum: int, i: int) requires n >= 0 { sum := 0; i := 0; while(i < n) { sum := sum + i; i := i + 1; } } // MODULE main // int i; // int sum; // int n; // assume(sum == 0); // assume(n >= 0); // assume(i == 0); // while(i < n){ // sum = sum + i;...
182
Dafny_Verify_tmp_tmphq7j0row_dataset_bql_exampls_Min.dfy
method min(a: array<int>, n : int) returns (min : int) requires 0 < n <= a.Length; ensures (exists i : int :: 0 <= i && i < n && a[i] == min); ensures (forall i : int :: 0 <= i && i < n ==> a[i] >= min); { var i : int; min := a[0]; i := 1; while (i < n) invariant i <= n; invariant (exists j : int :: 0 <...
method min(a: array<int>, n : int) returns (min : int) requires 0 < n <= a.Length; ensures (exists i : int :: 0 <= i && i < n && a[i] == min); ensures (forall i : int :: 0 <= i && i < n ==> a[i] >= min); { var i : int; min := a[0]; i := 1; while (i < n) { if (a[i] < min) { min := a[i]; } i := i + ...
183
Dafny_Verify_tmp_tmphq7j0row_dataset_bql_exampls_SmallNum.dfy
method add_small_numbers (a: array<int>, n: int, max: int) returns (r: int) requires n > 0; requires n <= a.Length; requires (forall i: int :: 0 <= i && i < n ==> a[i] <= max); ensures r <= max * n; { var i: int; i := 0; r := 0; while (i < n) invariant i <= n; invariant r <= max * i; { r := r + a[i...
method add_small_numbers (a: array<int>, n: int, max: int) returns (r: int) requires n > 0; requires n <= a.Length; requires (forall i: int :: 0 <= i && i < n ==> a[i] <= max); ensures r <= max * n; { var i: int; i := 0; r := 0; while (i < n) { r := r + a[i]; i := i + 1; } }
184
Dafny_Verify_tmp_tmphq7j0row_dataset_bql_exampls_Square.dfy
method square (n: int) returns (r: int) requires 0 <= n; ensures r == n*n; { var x: int; var i: int; r := 0; i := 0; x := 1; while (i < n) invariant i <= n; invariant r == i*i; invariant x == 2*i + 1; { r := r + x; x := x + 2; i := i + 1; } }
method square (n: int) returns (r: int) requires 0 <= n; ensures r == n*n; { var x: int; var i: int; r := 0; i := 0; x := 1; while (i < n) { r := r + x; x := x + 2; i := i + 1; } }
185
Dafny_Verify_tmp_tmphq7j0row_dataset_detailed_examples_SelectionSort.dfy
// Works by dividing the input list into two parts: sorted and unsorted. At the beginning, // the sorted part is empty and the unsorted part contains all the elements. method SelectionSort(a: array<int>) modifies a // Ensures the final array is sorted in ascending order ensures forall i,j :: 0 <= i < j < a.Lengt...
// Works by dividing the input list into two parts: sorted and unsorted. At the beginning, // the sorted part is empty and the unsorted part contains all the elements. method SelectionSort(a: array<int>) modifies a // Ensures the final array is sorted in ascending order ensures forall i,j :: 0 <= i < j < a.Lengt...
186
Dafny_Verify_tmp_tmphq7j0row_dataset_error_data_real_error_IsEven_success_1.dfy
function even(n: int): bool requires n >= 0 { if n == 0 then true else !even(n-1) } method is_even(n: int) returns (r: bool) requires n >= 0; ensures r <==> even(n); { var i: int := 0; r := true; while i < n invariant 0 <= i <= n; invariant r <==> even(i); { r := !r; i := i + 1; } }
function even(n: int): bool requires n >= 0 { if n == 0 then true else !even(n-1) } method is_even(n: int) returns (r: bool) requires n >= 0; ensures r <==> even(n); { var i: int := 0; r := true; while i < n { r := !r; i := i + 1; } }
187
Dafny_tmp_tmp0wu8wmfr_Heimaverkefni 1_LinearSearch.dfy
// Author of question: Snorri Agnarsson // Permalink of question: https://rise4fun.com/Dafny/0HRr // Author of solution: Alexander Guðmundsson // Permalink of solution: https://rise4fun.com/Dafny/8pxWd // Use the command // dafny LinearSearch-skeleton.dfy // or // compile LinearSearch-skeleton.dfy // to compil...
// Author of question: Snorri Agnarsson // Permalink of question: https://rise4fun.com/Dafny/0HRr // Author of solution: Alexander Guðmundsson // Permalink of solution: https://rise4fun.com/Dafny/8pxWd // Use the command // dafny LinearSearch-skeleton.dfy // or // compile LinearSearch-skeleton.dfy // to compil...
188
Dafny_tmp_tmp0wu8wmfr_Heimaverkefni 2_BinarySearchDec.dfy
// Author of question: Snorri Agnarsson // Permalink of question: https://rise4fun.com/Dafny/CGB1z // Authors of solution: Alexander Guðmundsson // Permalink of solution: https://rise4fun.com/Dafny/VnB5 // Use the command // dafny H2-skeleton.dfy // or // compile H2-skeleton.dfy // to compile the file. // Or us...
// Author of question: Snorri Agnarsson // Permalink of question: https://rise4fun.com/Dafny/CGB1z // Authors of solution: Alexander Guðmundsson // Permalink of solution: https://rise4fun.com/Dafny/VnB5 // Use the command // dafny H2-skeleton.dfy // or // compile H2-skeleton.dfy // to compile the file. // Or us...
189
Dafny_tmp_tmp0wu8wmfr_Heimaverkefni 3_InsertionSortMultiset.dfy
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/G4sc3 // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://rise4fun.com/Dafny/nujsu // Insertion sort með hjálp helmingunarleitar. method Search( s: seq<int>, x: int ) returns ( k: i...
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/G4sc3 // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://rise4fun.com/Dafny/nujsu // Insertion sort með hjálp helmingunarleitar. method Search( s: seq<int>, x: int ) returns ( k: i...
190
Dafny_tmp_tmp0wu8wmfr_Heimaverkefni 3_SelectionSortMultiset.dfy
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/dtcnY // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://rise4fun.com/Dafny/ybUCz /////////////////////////////////////////////////////////////// // Hér byrjar óbreytanlegi hluti sk...
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/dtcnY // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://rise4fun.com/Dafny/ybUCz /////////////////////////////////////////////////////////////// // Hér byrjar óbreytanlegi hluti sk...
191
Dafny_tmp_tmp0wu8wmfr_Heimaverkefni 8_H8.dfy
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/GW7a // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://www.rise4fun.com/Dafny/JPGct // Klárið að forrita föllin tvö. method Partition( m: multiset<int> ) returns( pre: mul...
// Höfundur spurningar: Snorri Agnarsson, snorri@hi.is // Permalink spurningar: https://rise4fun.com/Dafny/GW7a // Höfundur lausnar: Alexander Guðmundsson // Permalink lausnar: https://www.rise4fun.com/Dafny/JPGct // Klárið að forrita föllin tvö. method Partition( m: multiset<int> ) returns( pre: mul...
192
Dafny_tmp_tmp0wu8wmfr_tests_F1a.dfy
method F() returns ( r: int) ensures r <= 0 { r := 0; } method Main() { var x := F(); assert x <= 0; x := x-1; assert x <= -1; print x; } method Mid( p: int, q: int) returns ( m: int ) // | ... | ??? | ... | // p m q requires p <= q; ensures p<= m <= q; ensur...
method F() returns ( r: int) ensures r <= 0 { r := 0; } method Main() { var x := F(); x := x-1; print x; } method Mid( p: int, q: int) returns ( m: int ) // | ... | ??? | ... | // p m q requires p <= q; ensures p<= m <= q; ensures m-p <= q-m; ensures 0 <= (q-m)-(...
193
Dafny_tmp_tmp0wu8wmfr_tests_InsertionSortSeq.dfy
// Insertion sort. // // Author: Snorri Agnarsson, snorri@hi.is predicate IsSorted( s: seq<int> ) { forall p,q | 0<=p<q<|s| :: s[p]<=s[q] } method InsertionSort( s: seq<int> ) returns ( r: seq<int> ) ensures multiset(r) == multiset(s); ensures IsSorted(r); { r := []; var rest := s; while rest ...
// Insertion sort. // // Author: Snorri Agnarsson, snorri@hi.is predicate IsSorted( s: seq<int> ) { forall p,q | 0<=p<q<|s| :: s[p]<=s[q] } method InsertionSort( s: seq<int> ) returns ( r: seq<int> ) ensures multiset(r) == multiset(s); ensures IsSorted(r); { r := []; var rest := s; while rest ...
194
Dafny_tmp_tmp0wu8wmfr_tests_Search1000.dfy
// Author: Snorri Agnarsson, snorri@hi.is // Search1000 is a Dafny version of a function shown // by Jon Bentley in his old Programming Pearls // column in CACM. Surprisingly Dafny needs no help // to verify the function. method Search1000( a: array<int>, x: int ) returns ( k: int ) requires a.Length >= 1000; ...
// Author: Snorri Agnarsson, snorri@hi.is // Search1000 is a Dafny version of a function shown // by Jon Bentley in his old Programming Pearls // column in CACM. Surprisingly Dafny needs no help // to verify the function. method Search1000( a: array<int>, x: int ) returns ( k: int ) requires a.Length >= 1000; ...
195
Dafny_tmp_tmp0wu8wmfr_tests_SumIntsLoop.dfy
function sumInts( n: int ): int requires n >= 0; { if n == 0 then 0 else sumInts(n-1)+n } method SumIntsLoop( n: int ) returns ( s: int ) requires n >= 0; ensures s == sumInts(n) ensures s == n*(n+1)/2; { s := 0; var k := 0; while k != n decreases n-k; ...
function sumInts( n: int ): int requires n >= 0; { if n == 0 then 0 else sumInts(n-1)+n } method SumIntsLoop( n: int ) returns ( s: int ) requires n >= 0; ensures s == sumInts(n) ensures s == n*(n+1)/2; { s := 0; var k := 0; while k != n { k := k+1; ...
196
Dafny_tmp_tmpj88zq5zt_2-Kontrakte_max.dfy
method max(a: array<int>, b: array<int>, i: int, j: int) returns (m: int) requires 0 <= i < a.Length requires 0 <= j < b.Length ensures a[i] > b[j] ==> m == a[i] ensures a[i] <= b[j] ==> m == b[j] { if a[i] > b[j] { m := a[i]; } else { m := b[j]; } } method testMax(a:array<int>, b:array<int>...
method max(a: array<int>, b: array<int>, i: int, j: int) returns (m: int) requires 0 <= i < a.Length requires 0 <= j < b.Length ensures a[i] > b[j] ==> m == a[i] ensures a[i] <= b[j] ==> m == b[j] { if a[i] > b[j] { m := a[i]; } else { m := b[j]; } } method testMax(a:array<int>, b:array<int>...
197
Dafny_tmp_tmpj88zq5zt_2-Kontrakte_reverse3.dfy
method swap3(a: array<int>, h: int, i: int, j: int) modifies a requires 0 <= h < a.Length requires 0 <= i < a.Length requires 0 <= j < a.Length requires i != j && j != h && h != i; ensures a[h] == old(a[i]); ensures a[j] == old(a[h]); ensures a[i] == old(a[j]); ensures forall k: int :: 0 <= k < a.Leng...
method swap3(a: array<int>, h: int, i: int, j: int) modifies a requires 0 <= h < a.Length requires 0 <= i < a.Length requires 0 <= j < a.Length requires i != j && j != h && h != i; ensures a[h] == old(a[i]); ensures a[j] == old(a[h]); ensures a[i] == old(a[j]); ensures forall k: int :: 0 <= k < a.Leng...
198
Dafny_tmp_tmpmvs2dmry_SlowMax.dfy
function max(x:nat, y:nat) : nat { if (x < y) then y else x } method slow_max(a: nat, b: nat) returns (z: nat) ensures z == max(a, b) { z := 0; var x := a; var y := b; while (z < x && z < y) invariant x >=0; invariant y >=0; invariant z == a - x && z == b - y; invariant a-x == b-y decr...
function max(x:nat, y:nat) : nat { if (x < y) then y else x } method slow_max(a: nat, b: nat) returns (z: nat) ensures z == max(a, b) { z := 0; var x := a; var y := b; while (z < x && z < y) { z := z + 1; x := x - 1; y := y - 1; } if (x <= y) { return b; } else { return a;} }
199
Dafny_tmp_tmpmvs2dmry_examples1.dfy
method Abs(x:int) returns (y:int) ensures y>=0; ensures x>=0 ==> x == y; ensures x<0 ==> -x == y; ensures y == abs(x); // use this instead of line 3,4 { if(x<0) { return -x; } else{ return x; } } function abs(x: int): int{ if x>0 then x else -x } method Testing(){ var v:= Ab...
method Abs(x:int) returns (y:int) ensures y>=0; ensures x>=0 ==> x == y; ensures x<0 ==> -x == y; ensures y == abs(x); // use this instead of line 3,4 { if(x<0) { return -x; } else{ return x; } } function abs(x: int): int{ if x>0 then x else -x } method Testing(){ var v:= Ab...