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 |
|---|---|---|---|---|---|
000 | 000 | Dafny program: 000 |
function sorted(a: array<int>) : bool
reads a
{
forall i,j : int :: 0 <= i < j < a.Length ==> a[i] <= a[j]
}
method BinarySearch(a: array<int>, x: int) returns (index: int)
requires sorted(a)
ensures 0 <= index < a.Length ==> a[index] == x
ensures index == -1 ==> forall i : int :: 0 <= i < a.Length... |
function sorted(a: array<int>) : bool
reads a
{
forall i,j : int :: 0 <= i < j < a.Length ==> a[i] <= a[j]
}
method BinarySearch(a: array<int>, x: int) returns (index: int)
requires sorted(a)
ensures 0 <= index < a.Length ==> a[index] == x
ensures index == -1 ==> forall i : int :: 0 <= i < a.Length... | 630-dafny_tmp_tmpz2kokaiq_Solution.dfy |
005 | 005 | Dafny program: 005 | // Noa Leron 207131871
// Tsuri Farhana 315016907
predicate Sorted(q: seq<int>) {
forall i,j :: 0 <= i <= j < |q| ==> q[i] <= q[j]
}
/*
Goal: Implement the well known merge sort algorithm in O(a.Length X log_2(a.Length)) time, recursively.
- Divide the contents of the original array into two local arrays
- After ... | // Noa Leron 207131871
// Tsuri Farhana 315016907
predicate Sorted(q: seq<int>) {
forall i,j :: 0 <= i <= j < |q| ==> q[i] <= q[j]
}
/*
Goal: Implement the well known merge sort algorithm in O(a.Length X log_2(a.Length)) time, recursively.
- Divide the contents of the original array into two local arrays
- After ... | AssertivePrograming_tmp_tmpwf43uz0e_MergeSort.dfy |
009 | 009 | Dafny program: 009 | //Bubblesort CS 494 submission
//References: https://stackoverflow.com/questions/69364687/how-to-prove-time-complexity-of-bubble-sort-using-dafny/69365785#69365785
// predicate checks if elements of a are in ascending order, two additional conditions are added to allow us to sort in specific range within array
predic... | //Bubblesort CS 494 submission
//References: https://stackoverflow.com/questions/69364687/how-to-prove-time-complexity-of-bubble-sort-using-dafny/69365785#69365785
// predicate checks if elements of a are in ascending order, two additional conditions are added to allow us to sort in specific range within array
predic... | CS494-final-project_tmp_tmp7nof55uq_bubblesort.dfy |
010 | 010 | Dafny program: 010 | class LFUCache {
var capacity : int;
var cacheMap : map<int, (int, int)>; //key -> {value, freq}
constructor(capacity: int)
requires capacity > 0;
ensures Valid();
{
this.capacity := capacity;
this.cacheMap := map[];
}
predicate Valid()
reads this;
// reads... | class LFUCache {
var capacity : int;
var cacheMap : map<int, (int, int)>; //key -> {value, freq}
constructor(capacity: int)
requires capacity > 0;
ensures Valid();
{
this.capacity := capacity;
this.cacheMap := map[];
}
predicate Valid()
reads this;
// reads... | CS5232_Project_tmp_tmpai_cfrng_LFUSimple.dfy |
011 | 011 | Dafny program: 011 | iterator Gen(start: int) yields (x: int)
yield ensures |xs| <= 10 && x == start + |xs| - 1
{
var i := 0;
while i < 10 invariant |xs| == i {
x := start + i;
yield;
i := i + 1;
}
}
method Main() {
var i := new Gen(30);
while true
{
var m := i.MoveNext();
if (!m) {break; }
print i.x;... | iterator Gen(start: int) yields (x: int)
yield ensures |xs| <= 10 && x == start + |xs| - 1
{
var i := 0;
while i < 10 invariant |xs| == i {
x := start + i;
yield;
i := i + 1;
}
}
method Main() {
var i := new Gen(30);
while true
invariant i.Valid() && fresh(i._new)
decreases 10 - |i.xs|
... | CS5232_Project_tmp_tmpai_cfrng_test.dfy |
014 | 014 | Dafny program: 014 | method Max (x: nat, y:nat) returns (r:nat)
ensures (r >= x && r >=y)
ensures (r == x || r == y)
{
if (x >= y) { r := x;}
else { r := y;}
}
method Test ()
{
var result := Max(42, 73);
}
method m1 (x: int, y: int) returns (z: int)
requires 0 < x < y
ensures z >= 0 && z <= y && z != x
{
//assume ... | method Max (x: nat, y:nat) returns (r:nat)
ensures (r >= x && r >=y)
ensures (r == x || r == y)
{
if (x >= y) { r := x;}
else { r := y;}
}
method Test ()
{
var result := Max(42, 73);
assert result == 73;
}
method m1 (x: int, y: int) returns (z: int)
requires 0 < x < y
ensures z >= 0 && z <= y ... | CVS-Projto1_tmp_tmpb1o0bu8z_Hoare.dfy |
016 | 016 | Dafny program: 016 | //Exercicio 1.a)
function sum (a:array<int>, i:int, j:int) :int
reads a
requires 0 <= i <= j <= a.Length
{
if i == j then
0
else
a[j-1] + sum(a, i, j-1)
}
//Exercicio 1.b)
method query (a:array<int>, i:int, j:int) returns (s:int)
requires 0 <= i <= j <= a.Length
ensures s == sum(a, i, j)
{
... | //Exercicio 1.a)
function sum (a:array<int>, i:int, j:int) :int
decreases j
reads a
requires 0 <= i <= j <= a.Length
{
if i == j then
0
else
a[j-1] + sum(a, i, j-1)
}
//Exercicio 1.b)
method query (a:array<int>, i:int, j:int) returns (s:int)
requires 0 <= i <= j <= a.Length
ensures s == sum(a, ... | CVS-Projto1_tmp_tmpb1o0bu8z_proj1_proj1.dfy |
018 | 018 | Dafny program: 018 | /* Cumulative Sums over Arrays */
/*
Daniel Cavalheiro 57869
Pedro Nunes 57854
*/
//(a)
function sum(a: array<int>, i: int, j: int): int
reads a
requires 0 <= i <= j <= a.Length
{
if (i == j) then 0
else a... | /* Cumulative Sums over Arrays */
/*
Daniel Cavalheiro 57869
Pedro Nunes 57854
*/
//(a)
function sum(a: array<int>, i: int, j: int): int
reads a
requires 0 <= i <= j <= a.Length
decreases j - i
{
if (i == ... | CVS-handout1_tmp_tmptm52no3k_1.dfy |
019 | 019 | Dafny program: 019 | /* Functional Lists and Imperative Arrays */
/*
Daniel Cavalheiro 57869
Pedro Nunes 57854
*/
datatype List<T> = Nil | Cons(head: T, tail: List<T>)
function length<T>(l: List<T>): nat
{
match l
case Nil => 0
case Cons(_... | /* Functional Lists and Imperative Arrays */
/*
Daniel Cavalheiro 57869
Pedro Nunes 57854
*/
datatype List<T> = Nil | Cons(head: T, tail: List<T>)
function length<T>(l: List<T>): nat
{
match l
case Nil => 0
case Cons(_... | CVS-handout1_tmp_tmptm52no3k_2.dfy |
021 | 021 | Dafny program: 021 | method allDigits(s: string) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |s| ==> s[i] in "0123456789")
{
result:=true ;
for i := 0 to |s|
{
if ! (s[i] in "0123456789"){
return false;
}
}
}
| method allDigits(s: string) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |s| ==> s[i] in "0123456789")
{
result:=true ;
for i := 0 to |s|
invariant result <==> (forall ii :: 0 <= ii < i ==> s[ii] in "0123456789")
{
if ! (s[i] in "0123456789"){
return false;
}
}
}
| Clover_all_digits.dfy |
022 | 022 | Dafny program: 022 | method append(a:array<int>, b:int) returns (c:array<int>)
ensures a[..] + [b] == c[..]
{
c := new int[a.Length+1];
var i:= 0;
while (i < a.Length)
{
c[i] := a[i];
i:=i+1;
}
c[a.Length]:=b;
}
| method append(a:array<int>, b:int) returns (c:array<int>)
ensures a[..] + [b] == c[..]
{
c := new int[a.Length+1];
var i:= 0;
while (i < a.Length)
invariant 0 <= i <= a.Length
invariant forall ii::0<=ii<i ==> c[ii]==a[ii]
{
c[i] := a[i];
i:=i+1;
}
c[a.Length]:=b;
}
| Clover_array_append.dfy |
023 | 023 | Dafny program: 023 | method concat(a:array<int>, b:array<int>) returns (c:array<int>)
ensures c.Length==b.Length+a.Length
ensures forall k :: 0 <= k < a.Length ==> c[k] == a[k]
ensures forall k :: 0 <= k < b.Length ==> c[k+a.Length] == b[k]
{
c := new int[a.Length+b.Length];
var i:= 0;
while (i < c.Length)
{
c[i] := if i<... | method concat(a:array<int>, b:array<int>) returns (c:array<int>)
ensures c.Length==b.Length+a.Length
ensures forall k :: 0 <= k < a.Length ==> c[k] == a[k]
ensures forall k :: 0 <= k < b.Length ==> c[k+a.Length] == b[k]
{
c := new int[a.Length+b.Length];
var i:= 0;
while (i < c.Length)
invariant 0 <= i ... | Clover_array_concat.dfy |
024 | 024 | Dafny program: 024 | method iter_copy<T(0)>(s: array<T>) returns (t: array<T>)
ensures s.Length==t.Length
ensures forall i::0<=i<s.Length ==> s[i]==t[i]
{
t := new T[s.Length];
var i:= 0;
while (i < s.Length)
{
t[i] := s[i];
i:=i+1;
}
}
| method iter_copy<T(0)>(s: array<T>) returns (t: array<T>)
ensures s.Length==t.Length
ensures forall i::0<=i<s.Length ==> s[i]==t[i]
{
t := new T[s.Length];
var i:= 0;
while (i < s.Length)
invariant 0 <= i <= s.Length
invariant forall x :: 0 <= x < i ==> s[x] == t[x]
{
t[i] := s[i];
i:=i+1;
... | Clover_array_copy.dfy |
025 | 025 | Dafny program: 025 | method arrayProduct(a: array<int>, b: array<int>) returns (c: array<int> )
requires a.Length==b.Length
ensures c.Length==a.Length
ensures forall i:: 0 <= i< a.Length==> a[i] * b[i]==c[i]
{
c:= new int[a.Length];
var i:=0;
while i<a.Length
{
c[i]:=a[i]*b[i];
i:=i+1;
}
}
| method arrayProduct(a: array<int>, b: array<int>) returns (c: array<int> )
requires a.Length==b.Length
ensures c.Length==a.Length
ensures forall i:: 0 <= i< a.Length==> a[i] * b[i]==c[i]
{
c:= new int[a.Length];
var i:=0;
while i<a.Length
invariant 0<=i<=a.Length
invariant forall j:: 0 <= j< i==> a[... | Clover_array_product.dfy |
026 | 026 | Dafny program: 026 | method arraySum(a: array<int>, b: array<int>) returns (c: array<int> )
requires a.Length==b.Length
ensures c.Length==a.Length
ensures forall i:: 0 <= i< a.Length==> a[i] + b[i]==c[i]
{
c:= new int[a.Length];
var i:=0;
while i<a.Length
{
c[i]:=a[i]+b[i];
i:=i+1;
}
}
| method arraySum(a: array<int>, b: array<int>) returns (c: array<int> )
requires a.Length==b.Length
ensures c.Length==a.Length
ensures forall i:: 0 <= i< a.Length==> a[i] + b[i]==c[i]
{
c:= new int[a.Length];
var i:=0;
while i<a.Length
invariant 0<=i<=a.Length
invariant forall j:: 0 <= j< i==> a[j] +... | Clover_array_sum.dfy |
028 | 028 | Dafny program: 028 | method below_zero(operations: seq<int>) returns (s:array<int>, result:bool)
ensures s.Length == |operations| + 1
ensures s[0]==0
ensures forall i :: 0 <= i < s.Length-1 ==> s[i+1]==s[i]+operations[i]
ensures result == true ==> (exists i :: 1 <= i <= |operations| && s[i] < 0)
ensures result == false ==> forall... | method below_zero(operations: seq<int>) returns (s:array<int>, result:bool)
ensures s.Length == |operations| + 1
ensures s[0]==0
ensures forall i :: 0 <= i < s.Length-1 ==> s[i+1]==s[i]+operations[i]
ensures result == true ==> (exists i :: 1 <= i <= |operations| && s[i] < 0)
ensures result == false ==> forall... | Clover_below_zero.dfy |
029 | 029 | Dafny program: 029 | 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 n == a.Length ==> forall i :: 0 <= i < a.Length ==> a[i] < key
ensures forall i :: n<= i < a.Length ==> a[i]>=key
{
... | 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 n == a.Length ==> forall i :: 0 <= i < a.Length ==> a[i] < key
ensures forall i :: n<= i < a.Length ==> a[i]>=key
{
... | Clover_binary_search.dfy |
030 | 030 | Dafny program: 030 | method BubbleSort(a: array<int>)
modifies a
ensures forall i,j::0<= i < j < a.Length ==> a[i] <= a[j]
ensures multiset(a[..])==multiset(old(a[..]))
{
var i := a.Length - 1;
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];
... | method BubbleSort(a: array<int>)
modifies a
ensures forall i,j::0<= i < j < a.Length ==> a[i] <= a[j]
ensures multiset(a[..])==multiset(old(a[..]))
{
var i := a.Length - 1;
while (i > 0)
invariant i < 0 ==> a.Length == 0
invariant -1 <= i < a.Length
invariant forall ii,jj::i <= ii< jj <a.Length ==... | Clover_bubble_sort.dfy |
031 | 031 | Dafny program: 031 | method CalDiv() returns (x:int, y:int)
ensures x==191/7
ensures y==191%7
{
x, y := 0, 191;
while 7 <= y
{
x := x+1;
y:=191-7*x;
}
}
| method CalDiv() returns (x:int, y:int)
ensures x==191/7
ensures y==191%7
{
x, y := 0, 191;
while 7 <= y
invariant 0 <= y && 7 * x + y == 191
{
x := x+1;
y:=191-7*x;
}
}
| Clover_cal_ans.dfy |
032 | 032 | Dafny program: 032 | method Sum(N:int) returns (s:int)
requires N >= 0
ensures s == N * (N + 1) / 2
{
var n := 0;
s := 0;
while n != N
{
n := n + 1;
s := s + n;
}
}
| method Sum(N:int) returns (s:int)
requires N >= 0
ensures s == N * (N + 1) / 2
{
var n := 0;
s := 0;
while n != N
invariant 0 <= n <= N
invariant s == n * (n + 1) / 2
{
n := n + 1;
s := s + n;
}
}
| Clover_cal_sum.dfy |
033 | 033 | Dafny program: 033 | method CanyonSearch(a: array<int>, b: array<int>) returns (d:nat)
requires a.Length !=0 && b.Length!=0
requires forall i,j :: 0<=i<j<a.Length ==> a[i]<=a[j]
requires forall i,j :: 0<=i<j<b.Length ==> b[i]<=b[j]
ensures exists i,j:: 0<=i<a.Length && 0<=j<b.Length && d==if a[i] < b[j] then (b[j]-a[i]) else (a[i]-... | method CanyonSearch(a: array<int>, b: array<int>) returns (d:nat)
requires a.Length !=0 && b.Length!=0
requires forall i,j :: 0<=i<j<a.Length ==> a[i]<=a[j]
requires forall i,j :: 0<=i<j<b.Length ==> b[i]<=b[j]
ensures exists i,j:: 0<=i<a.Length && 0<=j<b.Length && d==if a[i] < b[j] then (b[j]-a[i]) else (a[i]-... | Clover_canyon_search.dfy |
036 | 036 | Dafny program: 036 | method copy( src: array<int>, sStart: nat, dest: array<int>, dStart: nat, len: nat) returns (r: array<int>)
requires src.Length >= sStart + len
requires dest.Length >= dStart + len
ensures r.Length == dest.Length
ensures r[..dStart] == dest[..dStart]
ensures r[dStart + len..] == dest[dStart + len..]
ensures... | method copy( src: array<int>, sStart: nat, dest: array<int>, dStart: nat, len: nat) returns (r: array<int>)
requires src.Length >= sStart + len
requires dest.Length >= dStart + len
ensures r.Length == dest.Length
ensures r[..dStart] == dest[..dStart]
ensures r[dStart + len..] == dest[dStart + len..]
ensures... | Clover_copy_part.dfy |
037 | 037 | Dafny program: 037 | method CountLessThan(numbers: set<int>, threshold: int) returns (count: int)
ensures count == |set i | i in numbers && i < threshold|
{
count := 0;
var shrink := numbers;
var grow := {};
while |shrink | > 0
{
var i: int :| i in shrink;
shrink := shrink - {i};
var grow' := grow+{i};
grow := g... | method CountLessThan(numbers: set<int>, threshold: int) returns (count: int)
ensures count == |set i | i in numbers && i < threshold|
{
count := 0;
var shrink := numbers;
var grow := {};
while |shrink | > 0
decreases shrink
invariant shrink + grow == numbers
invariant grow !! shrink
invariant ... | Clover_count_lessthan.dfy |
038 | 038 | Dafny program: 038 | method double_array_elements(s: array<int>)
modifies s
ensures forall i :: 0 <= i < s.Length ==> s[i] == 2 * old(s[i])
{
var i:= 0;
while (i < s.Length)
{
s[i] := 2 * s[i];
i := i + 1;
}
} | method double_array_elements(s: array<int>)
modifies s
ensures forall i :: 0 <= i < s.Length ==> s[i] == 2 * old(s[i])
{
var i:= 0;
while (i < s.Length)
invariant 0 <= i <= s.Length
invariant forall x :: i <= x < s.Length ==> s[x] == old(s[x])
invariant forall x :: 0 <= x < i ==> s[x] == 2 * old(s[x... | Clover_double_array_elements.dfy |
040 | 040 | Dafny program: 040 | method FindEvenNumbers (arr: array<int>) returns (evenNumbers: array<int>)
ensures forall x {:trigger (x%2) }:: x in arr[..] && (x%2==0)==> x in evenNumbers[..]
ensures forall x :: x !in arr[..] ==> x !in evenNumbers[..]
ensures forall k :: 0 <= k < evenNumbers.Length ==> evenNumbers[k] % 2 == 0
ensures forall... | method FindEvenNumbers (arr: array<int>) returns (evenNumbers: array<int>)
ensures forall x {:trigger (x%2) }:: x in arr[..] && (x%2==0)==> x in evenNumbers[..]
ensures forall x :: x !in arr[..] ==> x !in evenNumbers[..]
ensures forall k :: 0 <= k < evenNumbers.Length ==> evenNumbers[k] % 2 == 0
ensures forall... | Clover_even_list.dfy |
041 | 041 | Dafny program: 041 | method Find(a: array<int>, key: int) returns (index: int)
ensures -1<=index<a.Length
ensures index!=-1 ==> a[index]==key && (forall i :: 0 <= i < index ==> a[i] != key)
ensures index == -1 ==> (forall i::0 <= i < a.Length ==> a[i] != key)
{
index := 0;
while index < a.Length
{
if a[index] == key { retur... | method Find(a: array<int>, key: int) returns (index: int)
ensures -1<=index<a.Length
ensures index!=-1 ==> a[index]==key && (forall i :: 0 <= i < index ==> a[i] != key)
ensures index == -1 ==> (forall i::0 <= i < a.Length ==> a[i] != key)
{
index := 0;
while index < a.Length
invariant 0<=index<=a.Length
... | Clover_find.dfy |
042 | 042 | Dafny program: 042 | method has_close_elements(numbers: seq<real>, threshold: real) returns (res: bool)
requires threshold >= 0.0
ensures res ==> exists i: int, j: int :: 0 <= i < |numbers| && 0 <= j < |numbers| && i != j && (if numbers[i] - numbers[j] < 0.0 then numbers[j] - numbers[i] else numbers[i] - numbers[j]) < threshold
ensur... | method has_close_elements(numbers: seq<real>, threshold: real) returns (res: bool)
requires threshold >= 0.0
ensures res ==> exists i: int, j: int :: 0 <= i < |numbers| && 0 <= j < |numbers| && i != j && (if numbers[i] - numbers[j] < 0.0 then numbers[j] - numbers[i] else numbers[i] - numbers[j]) < threshold
ensur... | Clover_has_close_elements.dfy |
043 | 043 | Dafny program: 043 | method insert(line:array<char>, l:int, nl:array<char>, p:int, at:int)
requires 0 <= l+p <= line.Length
requires 0 <= p <= nl.Length
requires 0 <= at <= l
modifies line
ensures forall i :: (0<=i<p) ==> line[at+i] == nl[i]
ensures forall i :: (0<=i<at) ==> line[i] == old(line[i])
ensures forall i :: (at+p<=... | method insert(line:array<char>, l:int, nl:array<char>, p:int, at:int)
requires 0 <= l+p <= line.Length
requires 0 <= p <= nl.Length
requires 0 <= at <= l
modifies line
ensures forall i :: (0<=i<p) ==> line[at+i] == nl[i]
ensures forall i :: (0<=i<at) ==> line[i] == old(line[i])
ensures forall i :: (at+p<=... | Clover_insert.dfy |
044 | 044 | Dafny program: 044 | method SquareRoot(N:nat) returns (r:nat)
ensures r*r <= N < (r+1)*(r+1)
{
r:=0;
while (r+1)*(r+1)<=N
{
r:=r+1;
}
}
| method SquareRoot(N:nat) returns (r:nat)
ensures r*r <= N < (r+1)*(r+1)
{
r:=0;
while (r+1)*(r+1)<=N
invariant r*r<=N
{
r:=r+1;
}
}
| Clover_integer_square_root.dfy |
046 | 046 | Dafny program: 046 | method IsPalindrome(x: seq<char>) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |x| ==> x[i] == x[|x| - i - 1])
{
if |x|==0 {
return true;
}
var i := 0;
var j := |x| - 1;
result := true;
while (i < j)
{
if x[i] != x[j] {
result := false;
return;
}
i := i + ... | method IsPalindrome(x: seq<char>) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |x| ==> x[i] == x[|x| - i - 1])
{
if |x|==0 {
return true;
}
var i := 0;
var j := |x| - 1;
result := true;
while (i < j)
invariant 0<=i<=j+1 && 0<=j < |x|
invariant i+j==|x|-1
invariant (fora... | Clover_is_palindrome.dfy |
047 | 047 | Dafny program: 047 | method LinearSearch(a: array<int>, e: int) returns (n:int)
ensures 0<=n<=a.Length
ensures n==a.Length || a[n]==e
ensures forall i::0<=i < n ==> e!=a[i]
{
n :=0;
while n!=a.Length
{
if e==a[n]{
return;
}
n:=n+1;
}
}
| method LinearSearch(a: array<int>, e: int) returns (n:int)
ensures 0<=n<=a.Length
ensures n==a.Length || a[n]==e
ensures forall i::0<=i < n ==> e!=a[i]
{
n :=0;
while n!=a.Length
invariant 0<=n<=a.Length
invariant forall i::0<=i<n ==> e!=a[i]
{
if e==a[n]{
return;
}
n:=n+1;
}
}
| Clover_linear_search1.dfy |
048 | 048 | Dafny program: 048 | method LinearSearch(a: array<int>, e: int) returns (n:int)
requires exists i::0<=i<a.Length && a[i]==e
ensures 0<=n<a.Length && a[n]==e
ensures forall k :: 0 <= k < n ==> a[k]!=e
{
n :=0;
while n!=a.Length
{
if e==a[n]{
return;
}
n:=n+1;
}
}
| method LinearSearch(a: array<int>, e: int) returns (n:int)
requires exists i::0<=i<a.Length && a[i]==e
ensures 0<=n<a.Length && a[n]==e
ensures forall k :: 0 <= k < n ==> a[k]!=e
{
n :=0;
while n!=a.Length
invariant 0<=n<=a.Length
invariant forall i::0<=i<n ==> e!=a[i]
{
if e==a[n]{
retur... | Clover_linear_search2.dfy |
049 | 049 | Dafny program: 049 | method LinearSearch3<T>(a: array<T>, P: T -> bool) returns (n: int)
requires exists i :: 0 <= i < a.Length && P(a[i])
ensures 0 <= n < a.Length && P(a[n])
ensures forall k :: 0 <= k < n ==> !P(a[k])
{
n := 0;
while true
{
if P(a[n]) {
return;
}
n := n + 1;
}
}
| method LinearSearch3<T>(a: array<T>, P: T -> bool) returns (n: int)
requires exists i :: 0 <= i < a.Length && P(a[i])
ensures 0 <= n < a.Length && P(a[n])
ensures forall k :: 0 <= k < n ==> !P(a[k])
{
n := 0;
while true
invariant 0 <= n < a.Length
invariant exists i :: n <= i < a.Length && P(a[i])
... | Clover_linear_search3.dfy |
050 | 050 | Dafny program: 050 | method LongestCommonPrefix(str1: seq<char>, str2: seq<char>) returns (prefix: seq<char>)
ensures |prefix| <= |str1| && prefix == str1[0..|prefix|]&& |prefix| <= |str2| && prefix == str2[0..|prefix|]
ensures |prefix|==|str1| || |prefix|==|str2| || (str1[|prefix|]!=str2[|prefix|])
{
prefix := [];
var minLength :=... | method LongestCommonPrefix(str1: seq<char>, str2: seq<char>) returns (prefix: seq<char>)
ensures |prefix| <= |str1| && prefix == str1[0..|prefix|]&& |prefix| <= |str2| && prefix == str2[0..|prefix|]
ensures |prefix|==|str1| || |prefix|==|str2| || (str1[|prefix|]!=str2[|prefix|])
{
prefix := [];
var minLength :=... | Clover_longest_prefix.dfy |
051 | 051 | Dafny program: 051 | method Match(s: string, p: string) returns (b: bool)
requires |s| == |p|
ensures b <==> forall n :: 0 <= n < |s| ==> s[n] == p[n] || p[n] == '?'
{
var i := 0;
while i < |s|
{
if s[i] != p[i] && p[i] != '?'
{
return false;
}
i := i + 1;
}
return true;
}
| method Match(s: string, p: string) returns (b: bool)
requires |s| == |p|
ensures b <==> forall n :: 0 <= n < |s| ==> s[n] == p[n] || p[n] == '?'
{
var i := 0;
while i < |s|
invariant 0 <= i <= |s|
invariant forall n :: 0 <= n < i ==> s[n] == p[n] || p[n] == '?'
{
if s[i] != p[i] && p[i] != '?'
... | Clover_match.dfy |
052 | 052 | Dafny program: 052 | method maxArray(a: array<int>) returns (m: int)
requires a.Length >= 1
ensures forall k :: 0 <= k < a.Length ==> m >= a[k]
ensures exists k :: 0 <= k < a.Length && m == a[k]
{
m := a[0];
var index := 1;
while (index < a.Length)
{
m := if m>a[index] then m else a[index];
index := index + 1;
}
}
| method maxArray(a: array<int>) returns (m: int)
requires a.Length >= 1
ensures forall k :: 0 <= k < a.Length ==> m >= a[k]
ensures exists k :: 0 <= k < a.Length && m == a[k]
{
m := a[0];
var index := 1;
while (index < a.Length)
invariant 0 <= index <= a.Length
invariant forall k :: 0 <= k < index ==... | Clover_max_array.dfy |
053 | 053 | Dafny program: 053 | method minArray(a: array<int>) returns (r:int)
requires a.Length > 0
ensures forall i :: 0 <= i < a.Length ==> r <= a[i]
ensures exists i :: 0 <= i < a.Length && r == a[i]
{
r:=a[0];
var i:=1;
while i<a.Length
{
if r>a[i]{
r:=a[i];
}
i:=i+1;
}
}
| method minArray(a: array<int>) returns (r:int)
requires a.Length > 0
ensures forall i :: 0 <= i < a.Length ==> r <= a[i]
ensures exists i :: 0 <= i < a.Length && r == a[i]
{
r:=a[0];
var i:=1;
while i<a.Length
invariant 0 <= i <= a.Length
invariant forall x :: 0 <= x < i ==> r <= a[x]
invariant ... | Clover_min_array.dfy |
057 | 057 | Dafny program: 057 | method onlineMax(a: array<int>, x: int) returns (ghost m:int, p:int)
requires 1<=x<a.Length
requires a.Length!=0
ensures x<=p<a.Length
ensures forall i::0<=i<x==> a[i]<=m
ensures exists i::0<=i<x && a[i]==m
ensures x<=p<a.Length-1 ==> (forall i::0<=i<p ==> a[i]<a[p])
ensures (forall i::x<=i<a.Length && a[... | method onlineMax(a: array<int>, x: int) returns (ghost m:int, p:int)
requires 1<=x<a.Length
requires a.Length!=0
ensures x<=p<a.Length
ensures forall i::0<=i<x==> a[i]<=m
ensures exists i::0<=i<x && a[i]==m
ensures x<=p<a.Length-1 ==> (forall i::0<=i<p ==> a[i]<a[p])
ensures (forall i::x<=i<a.Length && a[... | Clover_online_max.dfy |
058 | 058 | Dafny program: 058 | method only_once<T(==)>(a: array<T>, key: T) returns (b:bool)
ensures (multiset(a[..])[key] ==1 ) <==> b
{
var i := 0;
b := false;
var keyCount := 0;
while i < a.Length
{
if (a[i] == key)
{
keyCount := keyCount + 1;
}
if (keyCount == 1)
{ b := true; }
else
{ b := false; }
... | method only_once<T(==)>(a: array<T>, key: T) returns (b:bool)
ensures (multiset(a[..])[key] ==1 ) <==> b
{
var i := 0;
b := false;
var keyCount := 0;
while i < a.Length
invariant 0 <= i <= a.Length
invariant multiset(a[..i])[key] == keyCount
invariant b <==> (keyCount == 1)
{
if (a[i] == key... | Clover_only_once.dfy |
059 | 059 | Dafny program: 059 | method Quotient(x: nat, y:nat) returns (r:int, q:int)
requires y != 0
ensures q * y + r == x && 0 <= r < y && 0 <= q
{
r:=x;
q:=0;
while y<=r
{
r:=r-y;
q:=q+1;
}
}
| method Quotient(x: nat, y:nat) returns (r:int, q:int)
requires y != 0
ensures q * y + r == x && 0 <= r < y && 0 <= q
{
r:=x;
q:=0;
while y<=r
invariant q*y+r==x && r>=0
decreases r
{
r:=r-y;
q:=q+1;
}
}
| Clover_quotient.dfy |
060 | 060 | Dafny program: 060 | method remove_front(a:array<int>) returns (c:array<int>)
requires a.Length>0
ensures a[1..] == c[..]
{
c := new int[a.Length-1];
var i:= 1;
while (i < a.Length)
{
c[i-1] := a[i];
i:=i+1;
}
}
| method remove_front(a:array<int>) returns (c:array<int>)
requires a.Length>0
ensures a[1..] == c[..]
{
c := new int[a.Length-1];
var i:= 1;
while (i < a.Length)
invariant 1 <= i <= a.Length
invariant forall ii::1<=ii<i ==> c[ii-1]==a[ii]
{
c[i-1] := a[i];
i:=i+1;
}
}
| Clover_remove_front.dfy |
061 | 061 | Dafny program: 061 | method replace(arr: array<int>, k: int)
modifies arr
ensures forall i :: 0 <= i < arr.Length ==> old(arr[i]) > k ==> arr[i] == -1
ensures forall i :: 0 <= i < arr.Length ==> old(arr[i]) <= k ==> arr[i] == old(arr[i])
{
var i := 0;
while i < arr.Length
{
if arr[i] > k {
arr[i] := -1;
}
i :=... | method replace(arr: array<int>, k: int)
modifies arr
ensures forall i :: 0 <= i < arr.Length ==> old(arr[i]) > k ==> arr[i] == -1
ensures forall i :: 0 <= i < arr.Length ==> old(arr[i]) <= k ==> arr[i] == old(arr[i])
{
var i := 0;
while i < arr.Length
decreases arr.Length - i
invariant 0 <= i <= arr.L... | Clover_replace.dfy |
063 | 063 | Dafny program: 063 | method reverse(a: array<int>)
modifies a
ensures forall i :: 0 <= i < a.Length ==> a[i] == old(a[a.Length - 1 - i])
{
var i := 0;
while i <a.Length / 2
{
a[i], a[a.Length-1-i] := a[a.Length-1-i], a[i];
i := i + 1;
}
}
| method reverse(a: array<int>)
modifies a
ensures forall i :: 0 <= i < a.Length ==> a[i] == old(a[a.Length - 1 - i])
{
var i := 0;
while i <a.Length / 2
invariant 0 <= i <= a.Length/2
invariant forall k :: 0 <= k < i || a.Length-1-i < k <= a.Length-1 ==> a[k] == old(a[a.Length-1-k])
invariant forall ... | Clover_reverse.dfy |
064 | 064 | Dafny program: 064 | method rotate(a: array<int>, offset:int) returns (b: array<int> )
requires 0<=offset
ensures b.Length==a.Length
ensures forall i::0<=i<a.Length ==> b[i]==a[(i+offset)%a.Length]
{
b:= new int[a.Length];
var i:=0;
while i<a.Length
{
b[i]:=a[(i+offset)%a.Length];
i:=i+1;
}
} | method rotate(a: array<int>, offset:int) returns (b: array<int> )
requires 0<=offset
ensures b.Length==a.Length
ensures forall i::0<=i<a.Length ==> b[i]==a[(i+offset)%a.Length]
{
b:= new int[a.Length];
var i:=0;
while i<a.Length
invariant 0<=i<=a.Length
invariant forall j ::0<=j<i ==> b[j]==a[(j+o... | Clover_rotate.dfy |
065 | 065 | Dafny program: 065 | 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+1;
while m != a.Length
{
if a[m] < a[mindex] {
mindex := m;
}
... | 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 < n <= j < a.Length ==> a[i] <= a[j]
invariant f... | Clover_selectionsort.dfy |
067 | 067 | Dafny program: 067 | method SetToSeq<T>(s: set<T>) returns (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
xs := [];
var left: set<T> := s;
while left != {}
{
var x :| x in left;
left := left - {x};
xs := xs + [x];
}
}
| method SetToSeq<T>(s: set<T>) returns (xs: seq<T>)
ensures multiset(s) == multiset(xs)
{
xs := [];
var left: set<T> := s;
while left != {}
invariant multiset(left) + multiset(xs) == multiset(s)
{
var x :| x in left;
left := left - {x};
xs := xs + [x];
}
}
| Clover_set_to_seq.dfy |
068 | 068 | Dafny program: 068 | method SlopeSearch(a: array2<int>, key: int) returns (m:int, n:int)
requires forall i,j,j'::0<=i<a.Length0 && 0<=j<j'<a.Length1 ==> a[i,j]<=a[i,j']
requires forall i,i',j::0<=i<i'<a.Length0 && 0<=j<a.Length1 ==> a[i,j]<=a[i',j]
requires exists i,j :: 0<=i<a.Length0 && 0<=j<a.Length1 && a[i,j]==key
ensures 0<=m<... | method SlopeSearch(a: array2<int>, key: int) returns (m:int, n:int)
requires forall i,j,j'::0<=i<a.Length0 && 0<=j<j'<a.Length1 ==> a[i,j]<=a[i,j']
requires forall i,i',j::0<=i<i'<a.Length0 && 0<=j<a.Length1 ==> a[i,j]<=a[i',j]
requires exists i,j :: 0<=i<a.Length0 && 0<=j<a.Length1 && a[i,j]==key
ensures 0<=m<... | Clover_slope_search.dfy |
079 | 079 | Dafny program: 079 | method twoSum(nums: array<int>, target: int) returns (i: int, j: int)
requires nums.Length > 1
requires exists i,j::0 <= i < j < nums.Length && nums[i] + nums[j] == target
ensures 0 <= i < j < nums.Length && nums[i] + nums[j] == target
ensures forall ii,jj:: (0 <= ii < i && ii < jj < nums.Length) ==> nums[ii]... | method twoSum(nums: array<int>, target: int) returns (i: int, j: int)
requires nums.Length > 1
requires exists i,j::0 <= i < j < nums.Length && nums[i] + nums[j] == target
ensures 0 <= i < j < nums.Length && nums[i] + nums[j] == target
ensures forall ii,jj:: (0 <= ii < i && ii < jj < nums.Length) ==> nums[ii]... | Clover_two_sum.dfy |
085 | 085 | Dafny program: 085 |
//Method barrier below receives an array and an integer p
//and returns a boolean b which is true if and only if
//all the positions to the left of p and including also position p contain elements
//that are strictly smaller than all the elements contained in the positions to the right of p
//Examples:
// If v=[... |
//Method barrier below receives an array and an integer p
//and returns a boolean b which is true if and only if
//all the positions to the left of p and including also position p contain elements
//that are strictly smaller than all the elements contained in the positions to the right of p
//Examples:
// If v=[... | Dafny-Exercises_tmp_tmpjm75muf__Session10Exercises_ExerciseBarrier.dfy |
088 | 088 | Dafny program: 088 | predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
method mpositive(v:array<int>) returns (b:bool)
ensures b==positive(v[0..v.Length])
{
var i:=0;
//1. assert positive(v[..0])
while i<v.Length && v[i]>=0
{
//2. assert 0<=i<v.Length && positive(v[..i]);
i:=i+1;
//2.... | predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
method mpositive(v:array<int>) returns (b:bool)
ensures b==positive(v[0..v.Length])
{
var i:=0;
//1. assert positive(v[..0])
while i<v.Length && v[i]>=0
decreases v.Length - i
invariant 0<=i<=v.Length
invariant positi... | Dafny-Exercises_tmp_tmpjm75muf__Session2Exercises_ExercisePositive.dfy |
089 | 089 | Dafny program: 089 | method mroot1(n:int) returns (r:int) //Cost O(root n)
requires n>=0
ensures r>=0 && r*r <= n <(r+1)*(r+1)
{
r:=0;
while (r+1)*(r+1) <=n
{
r:=r+1;
}
}
method mroot2(n:int) returns (r:int) //Cost O(n)
requires n>=0
ensures r>=0 && r*r <= n <(r+1)*(r+1)
{
r:=n;
while n<r*r
{
r:=r-1;
}
}
method... | method mroot1(n:int) returns (r:int) //Cost O(root n)
requires n>=0
ensures r>=0 && r*r <= n <(r+1)*(r+1)
{
r:=0;
while (r+1)*(r+1) <=n
invariant r>=0 && r*r <=n
decreases n-r*r
{
r:=r+1;
}
}
method mroot2(n:int) returns (r:int) //Cost O(n)
requires n>=0
ensures r>=0 && r*r <= n <(r+1)*(r+1)
{
... | Dafny-Exercises_tmp_tmpjm75muf__Session2Exercises_ExerciseSquare_root.dfy |
090 | 090 | Dafny program: 090 | //Algorithm 1: From left to right return the first
method mmaximum1(v:array<int>) returns (i:int)
requires v.Length>0
ensures 0<=i<v.Length
ensures forall k:: 0<=k<v.Length ==> v[i]>=v[k]
{
var j:=1; i:=0;
while(j<v.Length)
{
if(v[j] > v[i]){i:=j;}
j:=j+1;
}
}
//Algorithm 2: From righ... | //Algorithm 1: From left to right return the first
method mmaximum1(v:array<int>) returns (i:int)
requires v.Length>0
ensures 0<=i<v.Length
ensures forall k:: 0<=k<v.Length ==> v[i]>=v[k]
{
var j:=1; i:=0;
while(j<v.Length)
decreases v.Length - j
invariant 0<=j<=v.Length
invariant 0<=i... | Dafny-Exercises_tmp_tmpjm75muf__Session3Exercises_ExerciseMaximum.dfy |
091 | 091 | Dafny program: 091 | predicate allEqual(s:seq<int>)
{forall i,j::0<=i<|s| && 0<=j<|s| ==> s[i]==s[j] }
//{forall i,j::0<=i<=j<|s| ==> s[i]==s[j] }
//{forall i::0<i<|s| ==> s[i-1]==s[i]}
//{forall i::0<=i<|s|-1 ==> s[i]==s[i+1]}
//Ordered indexes
lemma equivalenceNoOrder(s:seq<int>)
ensures allEqual(s) <==> forall i,j::0<=i<=j<|s| ==> s[... | predicate allEqual(s:seq<int>)
{forall i,j::0<=i<|s| && 0<=j<|s| ==> s[i]==s[j] }
//{forall i,j::0<=i<=j<|s| ==> s[i]==s[j] }
//{forall i::0<i<|s| ==> s[i-1]==s[i]}
//{forall i::0<=i<|s|-1 ==> s[i]==s[i+1]}
//Ordered indexes
lemma equivalenceNoOrder(s:seq<int>)
ensures allEqual(s) <==> forall i,j::0<=i<=j<|s| ==> s[... | Dafny-Exercises_tmp_tmpjm75muf__Session4Exercises_ExerciseAllEqual.dfy |
092 | 092 | Dafny program: 092 |
predicate strictSorted(s : seq<int>) {
forall u, w :: 0 <= u < w < |s| ==> s[u] < s[w]
}
method mcontained(v:array<int>,w:array<int>,n:int,m:int) returns (b:bool)
//Specify and implement an O(m+n) algorithm that returns b
//v and w are strictly increasing ordered arrays
//b is true iff the first n elements of v a... |
predicate strictSorted(s : seq<int>) {
forall u, w :: 0 <= u < w < |s| ==> s[u] < s[w]
}
method mcontained(v:array<int>,w:array<int>,n:int,m:int) returns (b:bool)
//Specify and implement an O(m+n) algorithm that returns b
//v and w are strictly increasing ordered arrays
//b is true iff the first n elements of v a... | Dafny-Exercises_tmp_tmpjm75muf__Session4Exercises_ExerciseContained.dfy |
093 | 093 | Dafny program: 093 | predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
method mfirstNegative(v:array<int>) returns (b:bool, i:int)
ensures b <==> exists k::0<=k<v.Length && v[k]<0
ensures b ==> 0<=i<v.Length && v[i]<0 && positive(v[0..i])
{
i:=0;
b:=false;
while (i<v.Length && !b)
{
b:=(v[i]<0);
i:=i+1;
}
... | predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
method mfirstNegative(v:array<int>) returns (b:bool, i:int)
ensures b <==> exists k::0<=k<v.Length && v[k]<0
ensures b ==> 0<=i<v.Length && v[i]<0 && positive(v[0..i])
{
i:=0;
b:=false;
while (i<v.Length && !b)
invariant 0<=i<=v.Length
invari... | Dafny-Exercises_tmp_tmpjm75muf__Session4Exercises_ExerciseFirstNegative.dfy |
094 | 094 | Dafny program: 094 |
method mfirstCero(v:array<int>) returns (i:int)
ensures 0 <=i<=v.Length
ensures forall j:: 0<=j<i ==> v[j]!=0
ensures i!=v.Length ==> v[i]==0
{
i:=0;
while (i<v.Length && v[i]!=0)
{i:=i+1;}
}
|
method mfirstCero(v:array<int>) returns (i:int)
ensures 0 <=i<=v.Length
ensures forall j:: 0<=j<i ==> v[j]!=0
ensures i!=v.Length ==> v[i]==0
{
i:=0;
while (i<v.Length && v[i]!=0)
invariant 0<=i<=v.Length
invariant forall j:: 0<=j<i ==> v[j]!=0
decreases v.Length -i
{i:=i+1;}
}
| Dafny-Exercises_tmp_tmpjm75muf__Session4Exercises_ExercisefirstZero.dfy |
095 | 095 | Dafny program: 095 | function SumR(s:seq<int>):int
{
if (s==[]) then 0
else SumR(s[..|s|-1])+s[|s|-1]
}
function SumL(s:seq<int>):int
{
if (s==[]) then 0
else s[0]+SumL(s[1..])
}
lemma concatLast(s:seq<int>,t:seq<int>)
requires t!=[]
ensures (s+t)[..|s+t|-1] == s+(t[..|t|-1])
{}
lemma concatFirst(s:seq<int>,t:seq<int>)
r... | function SumR(s:seq<int>):int
decreases s
{
if (s==[]) then 0
else SumR(s[..|s|-1])+s[|s|-1]
}
function SumL(s:seq<int>):int
decreases s
{
if (s==[]) then 0
else s[0]+SumL(s[1..])
}
lemma concatLast(s:seq<int>,t:seq<int>)
requires t!=[]
ensures (s+t)[..|s+t|-1] == s+(t[..|t|-1])
{}
lemma concatFirst(... | Dafny-Exercises_tmp_tmpjm75muf__Session5Exercises_ExerciseSumElems.dfy |
096 | 096 | Dafny program: 096 |
predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
predicate isEven(i:int)
requires i>=0
{i%2==0}
function CountEven(s:seq<int>):int
requires positive(s)
{if s==[] then 0
else (if (s[|s|-1]%2==0) then 1 else 0)+CountEven(s[..|s|-1])
}
lemma ArrayFacts<T>()
ensures forall v : array<T> :: v[..v.Lengt... |
predicate positive(s:seq<int>)
{forall u::0<=u<|s| ==> s[u]>=0}
predicate isEven(i:int)
requires i>=0
{i%2==0}
function CountEven(s:seq<int>):int
decreases s
requires positive(s)
{if s==[] then 0
else (if (s[|s|-1]%2==0) then 1 else 0)+CountEven(s[..|s|-1])
}
lemma ArrayFacts<T>()
ensures forall v : array<T> ::... | Dafny-Exercises_tmp_tmpjm75muf__Session6Exercises_ExerciseCountEven.dfy |
097 | 097 | Dafny program: 097 | function min(v:array<int>,i:int):int
reads v
requires 1<=i<=v.Length
ensures forall k::0<=k<i==> v[k]>=min(v,i)
{if (i==1) then v[0]
else if (v[i-1]<=min(v,i-1)) then v[i-1]
else min(v,i-1)
}
function countMin(v:array<int>,x:int, i:int):int
reads v
requires 0<=i<=v.Length
ensures !(x in v[0..i]) ==> co... | function min(v:array<int>,i:int):int
decreases i
reads v
requires 1<=i<=v.Length
ensures forall k::0<=k<i==> v[k]>=min(v,i)
{if (i==1) then v[0]
else if (v[i-1]<=min(v,i-1)) then v[i-1]
else min(v,i-1)
}
function countMin(v:array<int>,x:int, i:int):int
decreases i
reads v
requires 0<=i<=v.Length
ensure... | Dafny-Exercises_tmp_tmpjm75muf__Session6Exercises_ExerciseCountMin.dfy |
098 | 098 | Dafny program: 098 |
predicate isPeek(v:array<int>,i:int)
reads v
requires 0<=i<v.Length
{forall k::0<=k<i ==> v[i]>=v[k]}
function peekSum(v:array<int>,i:int):int
reads v
requires 0<=i<=v.Length
{
if (i==0) then 0
else if isPeek(v,i-1) then v[i-1]+peekSum(v,i-1)
else peekSum(v,i-1)
}
method mPeekSum(v:array<int>) retu... |
predicate isPeek(v:array<int>,i:int)
reads v
requires 0<=i<v.Length
{forall k::0<=k<i ==> v[i]>=v[k]}
function peekSum(v:array<int>,i:int):int
decreases i
reads v
requires 0<=i<=v.Length
{
if (i==0) then 0
else if isPeek(v,i-1) then v[i-1]+peekSum(v,i-1)
else peekSum(v,i-1)
}
method mPeekSum(v:ar... | Dafny-Exercises_tmp_tmpjm75muf__Session6Exercises_ExercisePeekSum.dfy |
099 | 099 | Dafny program: 099 | predicate sorted(s : seq<int>) {
forall u, w :: 0 <= u < w < |s| ==> s[u] <= s[w]
}
method binarySearch(v:array<int>, elem:int) returns (p:int)
requires sorted(v[0..v.Length])
ensures -1<=p<v.Length
ensures (forall u::0<=u<=p ==> v[u]<=elem) && (forall w::p<w<v.Length ==> v[w]>elem)
{
var c,f:=0,v.Length-1;
w... | predicate sorted(s : seq<int>) {
forall u, w :: 0 <= u < w < |s| ==> s[u] <= s[w]
}
method binarySearch(v:array<int>, elem:int) returns (p:int)
requires sorted(v[0..v.Length])
ensures -1<=p<v.Length
ensures (forall u::0<=u<=p ==> v[u]<=elem) && (forall w::p<w<v.Length ==> v[w]>elem)
{
var c,f:=0,v.Length-1;
w... | Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseBinarySearch.dfy |
100 | 100 | Dafny program: 100 | 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... | Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseBubbleSort.dfy |
101 | 101 | Dafny program: 101 |
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;
}
}
|
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 ... | Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseReplace.dfy |
102 | 102 | Dafny program: 102 | 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)
... | Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseSelSort.dfy |
103 | 103 | Dafny program: 103 | 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 ... | Dafny-Exercises_tmp_tmpjm75muf__Session7Exercises_ExerciseSeparate.dfy |
104 | 104 | Dafny program: 104 |
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... | Dafny-Exercises_tmp_tmpjm75muf__Session8Exercises_ExerciseInsertionSort.dfy |
105 | 105 | Dafny program: 105 |
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... |
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... | Dafny-Exercises_tmp_tmpjm75muf__Session9Exercises_ExerciseSeqMaxSum.dfy |
106 | 106 | Dafny program: 106 | /*
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, ... | Dafny-Grind75_tmp_tmpsxfz3i4r_problems_twoSum.dfy |
107 | 107 | Dafny program: 107 | 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);
... | Dafny-Practice_tmp_tmphnmt4ovh_BST.dfy |
108 | 108 | Dafny program: 108 |
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... | Dafny-Practice_tmp_tmphnmt4ovh_Pattern Matching.dfy |
109 | 109 | Dafny program: 109 | 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 ... | Dafny-Projects_tmp_tmph399drhy_p2_arraySplit.dfy |
111 | 111 | Dafny program: 111 | /*******************************************************************************
* 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... | Dafny-VMC_tmp_tmpzgqv0i1u_src_Math_Helper.dfy |
113 | 113 | Dafny program: 113 | 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.
... | Dafny-experiences_tmp_tmp150sm9qy_dafny_started_tutorial_dafny_tutorial_array.dfy |
114 | 114 | Dafny program: 114 | /*
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<... | Dafny-programs_tmp_tmpnso9eu7u_Algorithms + sorting_bubble-sort.dfy |
115 | 115 | Dafny program: 115 | 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];
... | 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... | DafnyExercises_tmp_tmpd6qyevja_Part1_Q1.dfy |
118 | 118 | Dafny program: 118 | /**
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
... | /**
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... | DafnyPrograms_tmp_tmp74_f9k_c_invertarray.dfy |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 5