Dataset Viewer
id
int64 1
60
| name
stringlengths 3
21
| docstring
stringlengths 17
491
| annotations
stringlengths 52
529
| ground_truth
stringlengths 69
1.42k
|
|---|---|---|---|---|
1
|
abs
|
Calculate the absolute value. If input is positive, return the input. Otherwise, return the negation of the input.
|
method Abs(x: int) returns (y: int)
ensures x>=0 ==> x==y
ensures x<0 ==> x+y==0
|
method Abs(x: int) returns (y: int)
ensures x>=0 ==> x==y
ensures x<0 ==> x+y==0
{
if x < 0 {
return -x;
} else {
return x;
}
}
|
2
|
all_digits
|
Returns `true` if all characters in the string `s` are decimal digits (0-9), `false` otherwise.
|
method allDigits(s: string) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |s| ==> s[i] in "0123456789")
|
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;
}
}
}
|
3
|
array_append
|
Appends integer b to the end of array a and returns the resulting array c.
|
method append(a:array<int>, b:int) returns (c:array<int>)
ensures a[..] + [b] == c[..]
|
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;
}
|
4
|
array_concat
|
Concatenates two arrays a and b, returning the resulting array c.
|
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]
|
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 <= c.Length
invariant if i<a.Length then c[..i]==a[..i] else c[..i]==a[..]+b[..(i-a.Length)]
{
c[i] := if i<a.Length then a[i] else b[i-a.Length];
i:=i+1;
}
}
|
5
|
array_copy
|
Iteratively copy an array.
|
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]
|
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;
}
}
|
6
|
array_product
|
Calculate the pair-wise product of two arrays.
|
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]
|
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[j] * b[j]==c[j]
{
c[i]:=a[i]*b[i];
i:=i+1;
}
}
|
7
|
array_sum
|
Calculate the pair-wise sum of two arrays.
|
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]
|
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] + b[j]==c[j]
{
c[i]:=a[i]+b[i];
i:=i+1;
}
}
|
8
|
avg
|
Calculate the average value of two integers. There are no restrictions nor overflow protection on the inputs.
|
method ComputeAvg(a: int, b: int) returns (avg:int)
ensures avg == (a+b)/2
|
method ComputeAvg(a: int, b: int) returns (avg:int)
ensures avg == (a+b)/2
{
avg:= (a + b) / 2;
}
|
9
|
below_zero
|
You're given a list of deposit and withdrawal operations on a bank account that starts with zero balance. Your task is to detect if at any point the balance of account fallls below zero, and at that point function should return the presum array of operations and True. Otherwise it should return the presum array and False. Presum array s of operations is an array with length |operations| + 1, representing the cumulative sum at each step, initialized with s[0]==0.
|
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 i :: 0 <= i < s.Length ==> s[i] >= 0
|
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 i :: 0 <= i < s.Length ==> s[i] >= 0
{
result := false;
s := new int[|operations| + 1];
var i := 0;
s[i] := 0;
while i < s.Length
invariant 0 <= i <= s.Length
invariant s[0]==0
invariant s.Length == |operations| + 1
invariant forall x :: 0 <= x < i-1 ==> s[x+1]==s[x]+operations[x]
{
if i>0{
s[i] := s[i - 1] + operations[i - 1];
}
i := i + 1;
}
i:=0;
while i < s.Length
invariant 0 <= i <= s.Length
invariant forall x :: 0 <= x < i ==> s[x] >= 0
{
if s[i] < 0 {
result := true;
return;
}
i := i + 1;
}
}
|
10
|
binary_search
|
Binary search a sorted possibly empty array for a key. Return the index such that array[index-1] < key <= array[index].
|
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
{
var lo, hi := 0, a.Length;
while lo<hi
invariant 0<= lo <= hi <= a.Length
invariant forall i :: 0<=i<lo ==> a[i] < key
invariant forall i :: hi<=i<a.Length ==> a[i] >= key
{
var mid := (lo + hi) / 2;
if a[mid] < key {
lo := mid + 1;
} else {
hi := mid;
}
}
n:=lo;
}
|
11
|
bubble_sort
|
Sorts the array a in non-decreasing order. Ensure that the sorted elements make up the same set of elements as the original.
|
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[..]))
|
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 ==> a[ii] <= a[jj]
invariant forall k,k'::0<=k<=i<k'<a.Length==>a[k]<=a[k']
invariant multiset(a[..])==multiset(old(a[..]))
{
var j := 0;
while (j < i)
invariant 0 < i < a.Length && 0 <= j <= i
invariant forall ii,jj::i<= ii <= jj <a.Length ==> a[ii] <= a[jj]
invariant forall k, k'::0<=k<=i<k'<a.Length==>a[k]<=a[k']
invariant forall k :: 0 <= k <= j ==> a[k] <= a[j]
invariant multiset(a[..])==multiset(old(a[..]))
{
if (a[j] > a[j + 1])
{
a[j], a[j + 1] := a[j + 1], a[j];
}
j := j + 1;
}
i := i - 1;
}
}
|
12
|
cal_ans
|
Calculate the quotient and remainder of 191 divided by 7. Ensure that the quotient equals to 191/7 and the remainder equals to 191%7.
|
method CalDiv() returns (x:int, y:int)
ensures x==191/7
ensures y==191%7
|
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;
}
}
|
13
|
cal_sum
|
Calculate the sum from 1 to N. Require that N is a non-negative integer. Return the sum.
|
method Sum(N:int) returns (s:int)
requires N >= 0
ensures s == N * (N + 1) / 2
|
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;
}
}
|
14
|
canyon_search
|
Calculate the smallest difference of any two values from two non-decreasing arrays. Require that two input arrays to be non-empty and sorted. Ensure that the return value exists as one difference and is the smallest one.
|
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]-b[j])
ensures forall i,j:: 0<=i<a.Length && 0<=j<b.Length ==> d<=if a[i] < b[j] then (b[j]-a[i]) else (a[i]-b[j])
|
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]-b[j])
ensures forall i,j:: 0<=i<a.Length && 0<=j<b.Length ==> d<=if a[i] < b[j] then (b[j]-a[i]) else (a[i]-b[j])
{
var m,n:=0,0;
d:=if a[0] < b[0] then (b[0]-a[0]) else (a[0]-b[0]);
while m<a.Length && n<b.Length
invariant 0<=m<=a.Length && 0<=n<=b.Length
decreases a.Length -m+b.Length-n
invariant 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]-b[j])
invariant forall i,j:: 0<=i<a.Length && 0<=j<b.Length ==> d<=(if a[i] < b[j] then (b[j]-a[i]) else (a[i]-b[j]))|| (m<=i&&n<=j)
{
var t := if a[m] < b[n] then (b[n]-a[m]) else (a[m]-b[n]);
d:=if t<d then t else d;
if
case a[m]<=b[n] =>
m:=m+1;
case b[n]<=a[m] =>
n:=n+1;
}
}
|
15
|
compare
|
Return true if two elements are equal. No requirements on inputs.
|
method Compare<T(==)>(a: T, b: T) returns (eq: bool)
ensures a==b ==> eq==true
ensures a!=b ==> eq==false
|
method Compare<T(==)>(a: T, b: T) returns (eq: bool)
ensures a==b ==> eq==true
ensures a!=b ==> eq==false
{
if a == b { eq := true; } else { eq := false; }
}
|
16
|
convert_map_key
|
Converts the keys of the inputs map using the function f. Require that f is an injective function from domain natural numbers to range natural numbers. Ensure that returned new map r has f(key) as the new key and the same value.
|
method convert_map_key(inputs: map<nat, bool>, f: nat->nat) returns(r:map<nat, bool>)
requires forall n1: nat, n2: nat :: n1 != n2 ==> f(n1) != f(n2)
ensures forall k :: k in inputs <==> f(k) in r
ensures forall k :: k in inputs ==> r[f(k)] == inputs[k]
|
method convert_map_key(inputs: map<nat, bool>, f: nat->nat) returns(r:map<nat, bool>)
requires forall n1: nat, n2: nat :: n1 != n2 ==> f(n1) != f(n2)
ensures forall k :: k in inputs <==> f(k) in r
ensures forall k :: k in inputs ==> r[f(k)] == inputs[k]
{
r:= map k | k in inputs :: f(k) := inputs[k];
}
|
17
|
copy_part
|
Copies `len` elements from `src` array starting at `sStart` to `dest` array starting at `dStart` and returns the modified `dest` array. Ensure r and dest have the same length. Ensure original `dest` elements outside copied range stay the same.
|
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 r[dStart..len+dStart] == src[sStart..len+sStart]
|
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 r[dStart..len+dStart] == src[sStart..len+sStart]
{
if len == 0 { return dest; }
var i: nat := 0;
r := new int[dest.Length];
while (i < r.Length)
invariant i <= r.Length
invariant r[..i] == dest[..i]
{
r[i] := dest[i];
i := i + 1;
}
assert r[..]==dest[..];
i := 0;
while (i < len)
invariant i <= len
invariant r[..dStart] == dest[..dStart]
invariant r[(dStart + len)..] == dest[(dStart + len)..]
invariant r[dStart .. dStart + i] == src[sStart .. sStart + i]
{
assert r[(dStart + len)..] == dest[(dStart + len)..];
r[dStart + i] := src[sStart + i];
i := i + 1;
}
}
|
18
|
count_lessthan
|
Counts the number of integers in the numbers set that are less than the provided threshold and returns the count.
|
method CountLessThan(numbers: set<int>, threshold: int) returns (count: int)
ensures count == |set i | i in numbers && i < threshold|
|
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 count == |set i | i in grow && i < threshold|
{
var i: int :| i in shrink;
shrink := shrink - {i};
var grow' := grow+{i};
assert (set i | i in grow' && i < threshold) ==
(set i | i in grow && i < threshold )+ if i < threshold then {i} else {};
grow := grow + {i};
if i < threshold {
count := count + 1;
}
}
}
|
19
|
double_array_elements
|
Doubles each element of the input integer array in place.
|
method double_array_elements(s: array<int>)
modifies s
ensures forall i :: 0 <= i < s.Length ==> s[i] == 2 * old(s[i])
|
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])
{
s[i] := 2 * s[i];
i := i + 1;
}
}
|
20
|
double_quadruple
|
Returns two integers, where the first is double the input value x and the second is quadruple the input value x.
|
method DoubleQuadruple(x: int) returns (a: int, b: int)
ensures a == 2 * x && b == 4 * x
|
method DoubleQuadruple(x: int) returns (a: int, b: int)
ensures a == 2 * x && b == 4 * x
{
a := 2 * x;
b := 2 * a;
}
|
21
|
even_list
|
Extracts and returns an array containing all the even numbers from the provided arr, preserving their original order. An element appears in the output if and only if it is even and in the input. Ensure the original order is preserved.
|
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 k, l :: 0 <= k < l < evenNumbers.Length ==>
|
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 k, l :: 0 <= k < l < evenNumbers.Length ==>
exists n, m :: 0 <= n < m < arr.Length && evenNumbers[k] == arr[n] && evenNumbers[l] == arr[m]
{
var evenList: seq<int> := [];
ghost var indices: seq<int> := [];
for i := 0 to arr.Length
invariant 0 <= i <= arr.Length
invariant 0 <= |evenList| <= i
invariant forall x {:trigger (x%2) }:: (x in arr[..i] && (x%2==0) )==> x in evenList[..]
invariant forall k :: 0 <= k < |evenList| ==> evenList[k] % 2 == 0
invariant forall x :: x !in arr[..i] ==> x !in evenList
invariant |evenList| == |indices|
invariant forall k :: 0 <= k < |indices| ==> indices[k] < i
invariant forall k, l :: 0 <= k < l < |indices| ==> indices[k] < indices[l]
invariant forall k :: 0 <= k < |evenList| ==> 0 <= indices[k] < i <= arr.Length && arr[indices[k]] == evenList[k]
{
if arr[i]%2==0
{
evenList := evenList + [arr[i]];
indices := indices + [i];
}
}
evenNumbers := new int[|evenList|](i requires 0 <= i < |evenList| => evenList[i]);
assert evenList == evenNumbers[..];
}
|
22
|
find
|
Find a key in a possibly empty array. Return the index of its first occurance. Ensure index is in range. If not found, return -1.
|
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)
|
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
invariant (forall i::0 <= i < index==>a[i] != key)
{
if a[index] == key { return; }
index := index + 1;
}
if index >= a.Length {
index := -1;
}
}
|
23
|
has_close_elements
|
Check if in given list of numbers, are any two numbers closer to each other than given non-negative threshold.
|
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
ensures !res ==> (forall i: int, j: int :: 1 <= i < |numbers| && 0 <= j < i ==> (if numbers[i] - numbers[j] < 0.0 then numbers[j] - numbers[i] else numbers[i] - numbers[j]) >= threshold)
|
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
ensures !res ==> (forall i: int, j: int :: 1 <= i < |numbers| && 0 <= j < i ==> (if numbers[i] - numbers[j] < 0.0 then numbers[j] - numbers[i] else numbers[i] - numbers[j]) >= threshold)
{
res := false;
var idx: int := 0;
while idx < |numbers| && !res
invariant 0 <= idx <= |numbers|
invariant !res
invariant forall i: int, j: int :: 0 <= i < idx && 0 <= j < i ==> (if numbers[i] - numbers[j] < 0.0 then numbers[j] - numbers[i] else numbers[i] - numbers[j]) >= threshold
{
var idx2: int := 0;
while idx2 < idx && !res
invariant 0 <= idx <= |numbers|
invariant 0 <= idx2 <= idx
invariant !res
invariant forall j: int :: 0 <= j < idx2 ==> (if numbers[idx] - numbers[j] < 0.0 then numbers[j] - numbers[idx] else numbers[idx] - numbers[j]) >= threshold
{
var distance := (if numbers[idx2] - numbers[idx] < 0.0 then numbers[idx] - numbers[idx2] else numbers[idx2] - numbers[idx]);
if distance < threshold {
res := true;
return;
}
idx2 := idx2 + 1;
}
idx := idx + 1;
}
}
|
24
|
insert
|
This insert method inserts elements from array nl into array line at the position at, shifting subsequent elements to the right. The length of line and nl before insertion are represented by l and p respectively. It mandates that 0 <= l + p <= line.Length, 0 <= p <= nl.Length, and 0 <= at <= l. The method modifies line such that for all i in 0 <= i < p, line[at + i] equals nl[i], and all other elements of line maintain their original values in the adjusted positions after the insertion.
|
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<=i<l+p) ==> line[i] == old(line[i-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<=i<l+p) ==> line[i] == old(line[i-p])
{
ghost var initialLine := line[..];
var i:int := l;
while(i>at)
invariant line[0..i] == initialLine[0..i]
invariant line[i+p..l+p] == initialLine[i..l]
invariant at<=i<=l
{
i := i - 1;
line[i+p] := line[i];
}
assert line[0..at] == initialLine[0..at];
assert line[at+p..l+p] == initialLine[at..l];
i := 0;
while(i<p)
invariant 0<=i<=p
invariant line[0..at] == initialLine[0..at]
invariant line[at..at+i] == nl[0..i]
invariant line[at+p..l+p] == initialLine[at..l]
{
line[at + i] := nl[i];
i := i + 1;
}
assert line[0..at] == initialLine[0..at];
assert line[at..at+p] == nl[0..p];
assert line[at+p..l+p] == initialLine[at..l];
}
|
25
|
integer_square_root
|
Find the square root of a natural number.
|
method SquareRoot(N:nat) returns (r:nat)
ensures r*r <= N < (r+1)*(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;
}
}
|
26
|
is_even
|
Check if an integer is even. The integer can be either positive or negative or zero.
|
method ComputeIsEven(x:int) returns (is_even:bool)
ensures (x % 2 == 0)==is_even
|
method ComputeIsEven(x:int) returns (is_even:bool)
ensures (x % 2 == 0)==is_even
{
is_even:=false;
if x%2==0{
is_even:=true;
}
}
|
27
|
is_palindrome
|
Given an seq<char> x, return true if x is a palindrome, and false otherwise.
|
method IsPalindrome(x: seq<char>) returns (result: bool)
ensures result <==> (forall i :: 0 <= i < |x| ==> x[i] == x[|x| - i - 1])
|
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 (forall k :: 0 <= k < i ==> x[k] == x[|x| - k - 1])
{
if x[i] != x[j] {
result := false;
return;
}
i := i + 1;
j := j - 1;
}
}
|
28
|
linear_search1
|
Linearly search for an element in a possibly empty array and return the index of its first appearance. Return the length of the array if not found.
|
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]
|
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;
}
}
|
29
|
linear_search2
|
Linearly search for an element in a possibly empty array and return the index of its first appearance. Require that the input array contains the element. Ensure the returned index is the first appearance of the element.
|
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
|
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]{
return;
}
n:=n+1;
}
}
|
30
|
linear_search3
|
Linearly search for an existing element that satisfies a predicate in an array. Return the index of its first appearance. Require that the input array contains such an element. Ensure that no previous elements satisfy such predicate.
|
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])
|
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])
invariant forall k :: 0 <= k < n ==> !P(a[k])
decreases a.Length - n
{
if P(a[n]) {
return;
}
n := n + 1;
}
}
|
31
|
longest_prefix
|
This method returns the longest common prefix between two possibly empty sequences of characters. Ensure first that the prefix is shorter than two inputs, then ensure that it is equal to the first few chars of the inputs. Then ensure that it is the longest prefix, which means that either prefix is equal to either of the inputs, or the next character in inputs does not equal.
|
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|])
|
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 := if |str1| <|str2| then |str1| else |str2|;
for idx:= 0 to minLength
invariant |prefix|==idx <= minLength<=|str1| && minLength<=|str2|
invariant |prefix| <= |str1| && prefix == str1[0..|prefix|]&& |prefix| <= |str2| && prefix == str2[0..|prefix|]
{
if str1[idx] != str2[idx] {
return;
}
prefix := prefix + [str1[idx]];
}
}
|
32
|
match
|
Input strings p and s are of equal length. Return true if, for every character in s, it matches the corresponding character in p or the character in p is a wildcard '?'.
|
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] == '?'
|
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] != '?'
{
return false;
}
i := i + 1;
}
return true;
}
|
33
|
max_array
|
Returns the maximum value m present in the array a. Ensure that such an element appears in the array and is the maximum element.
|
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]
|
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 ==> m >= a[k]
invariant exists k :: 0 <= k < index && m == a[k]
decreases a.Length - index
{
m := if m>a[index] then m else a[index];
index := index + 1;
}
}
|
34
|
min_array
|
Return the value of the minimum element in a non-empty array. Ensure that such an element appears in the array and is the minimum element.
|
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]
|
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 exists x :: 0 <= x < i && r == a[x]
{
if r>a[i]{
r:=a[i];
}
i:=i+1;
}
}
|
35
|
min_of_two
|
Return the min of two values.
|
method Min(x: int, y:int) returns (z: int)
ensures x<=y ==> z==x
ensures x>y ==> z==y
|
method Min(x: int, y:int) returns (z: int)
ensures x<=y ==> z==x
ensures x>y ==> z==y
{
if x < y {
return x;
} else {
return y;
}
}
|
36
|
modify_2d_array
|
Modifies the value at one specified index of a 2D array of natural numbers.
Require each inner array is unique.
Ensure all first-order arraies is the same.
Ensure that only 'arr[index1][index2]' is set to `val` and no other elements are changed.
|
method modify_array_element(arr: array<array<nat>>, index1: nat, index2: nat, val: nat)
requires index1 < arr.Length
requires index2 < arr[index1].Length
requires forall i: nat, j:nat :: i < arr.Length && j < arr.Length && i != j ==> arr[i] != arr[j]
modifies arr[index1]
ensures forall i: nat :: 0 <= i < arr.Length ==> arr[i] == old(arr[i])
ensures forall i: nat, j: nat :: 0 <= i < arr.Length && 0 <= j < arr[i].Length && (i != index1 || j != index2) ==> arr[i][j] == old(arr[i][j])
ensures arr[index1][index2] == val
|
method modify_array_element(arr: array<array<nat>>, index1: nat, index2: nat, val: nat)
requires index1 < arr.Length
requires index2 < arr[index1].Length
requires forall i: nat, j:nat :: i < arr.Length && j < arr.Length && i != j ==> arr[i] != arr[j]
modifies arr[index1]
ensures forall i: nat :: 0 <= i < arr.Length ==> arr[i] == old(arr[i])
ensures forall i: nat, j: nat :: 0 <= i < arr.Length && 0 <= j < arr[i].Length && (i != index1 || j != index2) ==> arr[i][j] == old(arr[i][j])
ensures arr[index1][index2] == val
{
arr[index1][index2] := val;
}
|
37
|
multi_return
|
Return the sum and difference of two integers.
|
method MultipleReturns(x: int, y: int) returns (more: int, less: int)
ensures more == x+y
ensures less == x-y
|
method MultipleReturns(x: int, y: int) returns (more: int, less: int)
ensures more == x+y
ensures less == x-y
{
more := x + y;
less := x - y;
}
|
38
|
online_max
|
Require that the input array to be non-empty and x is an non-zero index into the array. Return the index of the first element that is strictly larger than m, the maximum element in the first x elements, or return the last index. Ensure the returned index is larger than or equal to x. Ensure the ghost variable represents the largest element in the first x elements of the array (DO NOT use max).
|
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[i]<=m) ==> p==a.Length-1
|
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[i]<=m) ==> p==a.Length-1
{
p:= 0;
var best := a[0];
var i:=1;
while i<x
invariant 0<=i<=x
invariant forall j::0<=j<i==> a[j]<=best
invariant exists j::0<=j<i && a[j]==best
{
if a[i]>best{
best:=a[i];
}
i:=i+1;
}
m:=best;
i:=x;
while i<a.Length
invariant x<=i<=a.Length
invariant forall j::x<=j<i ==> a[j]<=m
{
if a[i]>best{
p:=i;
return;
}
i:=i+1;
}
p:=a.Length-1;
}
|
39
|
only_once
|
Determines whether key appears exactly once in the array a.
|
method only_once<T(==)>(a: array<T>, key: T) returns (b:bool)
ensures (multiset(a[..])[key] ==1 ) <==> b
|
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)
{
keyCount := keyCount + 1;
}
if (keyCount == 1)
{ b := true; }
else
{ b := false; }
i := i + 1;
}
assert a[..a.Length] == a[..];
}
|
40
|
quotient
|
Find the quotient and remainder of a division of a dividsor and a dividend.
Require the dividend to be non-zero.
Ensure that the divisor equals to quotient times dividend plus remainder. Ensure that the quotient and remainder are positive. Ensure the remainder less than th dividend.
|
method Quotient(x: nat, y:nat) returns (r:int, q:int)
requires y != 0
ensures q * y + r == x && 0 <= r < y && 0 <= q
|
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;
}
}
|
41
|
remove_front
|
Removes the front element from the array a and returns the resulting array c.
|
method remove_front(a:array<int>) returns (c:array<int>)
requires a.Length>0
ensures a[1..] == c[..]
|
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;
}
}
|
42
|
replace
|
Replace all elements in the old array strictly greater than k with -1. Elements in the old array less than or equal to k remains unchanged. Use twostate specifiation. Distinguish old(arr) and arr.
|
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])
|
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.Length
invariant forall j :: 0 <= j < i ==> old(arr[j]) > k ==> arr[j] == -1
invariant forall j :: 0 <= j < i ==> old(arr[j]) <= k ==> arr[j] == old(arr[j])
invariant forall j :: i <= j < arr.Length ==> old(arr[j]) == arr[j]
{
if arr[i] > k {
arr[i] := -1;
}
i := i + 1;
}
}
|
43
|
return_seven
|
Return integer 7 regardless of input.
|
method M(x: int) returns (seven: int)
ensures seven==7
|
method M(x: int) returns (seven: int)
ensures seven==7
{
seven := 7;
}
|
44
|
reverse
|
This method reverses the provided integer array `a` in-place.
|
method reverse(a: array<int>)
modifies a
ensures forall i :: 0 <= i < a.Length ==> a[i] == old(a[a.Length - 1 - i])
|
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 k :: i <= k <= a.Length-1-i ==> a[k] == old(a[k])
{
a[i], a[a.Length-1-i] := a[a.Length-1-i], a[i];
i := i + 1;
}
}
|
45
|
rotate
|
Rotate the array by the offset to the left.
|
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]
|
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+offset)%a.Length]
{
b[i]:=a[(i+offset)%a.Length];
i:=i+1;
}
}
|
46
|
selectionsort
|
Use selection sort to sort a possibly empty array. Ensure that all elements remain the same.
|
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[..]))
|
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 forall i,j :: 0 <= i < j < n ==> a[i] <= a[j]
invariant multiset(a[..]) == old(multiset(a[..]))
{
var mindex, m := n, n+1;
while m != a.Length
invariant n <= mindex < m <= a.Length
invariant forall i :: n <= i < m ==> a[mindex] <= a[i]
{
if a[m] < a[mindex] {
mindex := m;
}
m := m+1;
}
a[n], a[mindex] := a[mindex], a[n];
n := n+1;
}
}
|
47
|
seq_to_array
|
Convert a sequence into an array in the original order.
|
method ToArray<T>(xs: seq<T>) returns (a: array<T>)
ensures fresh(a)
ensures a.Length == |xs|
ensures forall i :: 0 <= i < |xs| ==> a[i] == xs[i]
|
method ToArray<T>(xs: seq<T>) returns (a: array<T>)
ensures fresh(a)
ensures a.Length == |xs|
ensures forall i :: 0 <= i < |xs| ==> a[i] == xs[i]
{
a := new T[|xs|](i requires 0 <= i < |xs| => xs[i]);
}
|
48
|
set_to_seq
|
Convert a set into a seq. Ensure that the set contains the same set of elements as the seq.
|
method SetToSeq<T>(s: set<T>) returns (xs: seq<T>)
ensures multiset(s) == multiset(xs)
|
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];
}
}
|
49
|
slope_search
|
Search for the index of an existing key in a slope represented by an 2-D array. Require the slope to be a plane whose height is non-decreasing on the same x-axis (horizontal line) or same y-axis (vertical line). Require that such an element exists.
|
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<a.Length0 && 0<=n<a.Length1
ensures a[m,n]==key
|
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<a.Length0 && 0<=n<a.Length1
ensures a[m,n]==key
{
m,n:=0, a.Length1-1;
while a[m,n] !=key
invariant 0<=m<a.Length0 && 0<=n<a.Length1
invariant exists i,j :: m<=i<a.Length0 && 0<=j<=n && a[i,j]==key
decreases a.Length0-m+n
{
if a[m,n] < key {
m:=m+1;
}else{
n:=n-1;
}
}
}
|
50
|
swap
|
Swap two integers.
|
method Swap(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
|
method Swap(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
{
x, y := X, Y;
var tmp := x;
x := y;
y := tmp;
assert x == Y && y == X;
}
|
51
|
swap_arith
|
Swap two integers. There are no restrictions on the inputs.
|
method SwapArithmetic(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
|
method SwapArithmetic(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
{
x, y := X, Y;
x := y - x;
y := y - x;
x := y + x;
}
|
52
|
swap_bitvector
|
Swap two bitvectors.
|
method SwapBitvectors(X: bv8, Y: bv8) returns(x: bv8, y: bv8)
ensures x==Y
ensures y==X
|
method SwapBitvectors(X: bv8, Y: bv8) returns(x: bv8, y: bv8)
ensures x==Y
ensures y==X
{
x, y := X, Y;
x := x ^ y;
y := x ^ y;
x := x ^ y;
}
|
53
|
swap_in_array
|
Swaps the elements at indices i and j in the array arr and make sure no other elements change.
|
method swap(arr: array<int>, i: int, j: int)
requires 0 <= i < arr.Length && 0 <= j < arr.Length
modifies arr
ensures arr[i] == old(arr[j]) && arr[j] == old(arr[i])
ensures forall k :: 0 <= k < arr.Length && k != i && k != j ==> arr[k] == old(arr[k])
|
method swap(arr: array<int>, i: int, j: int)
requires 0 <= i < arr.Length && 0 <= j < arr.Length
modifies arr
ensures arr[i] == old(arr[j]) && arr[j] == old(arr[i])
ensures forall k :: 0 <= k < arr.Length && k != i && k != j ==> arr[k] == old(arr[k])
{
var tmp := arr[i];
arr[i] := arr[j];
arr[j] := tmp;
}
|
54
|
swap_sim
|
Simultaneously swaps the values of X and Y and returns the swapped values as x and y.
|
method SwapSimultaneous(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
|
method SwapSimultaneous(X: int, Y: int) returns(x: int, y: int)
ensures x==Y
ensures y==X
{
x, y := X, Y;
x, y := y, x;
}
|
55
|
test_array
|
Test an array assignment operation. Index j is within range of array. Assign index j to be 60. All other elements remain unchanged.
|
method TestArrayElements(a:array<int>, j: nat)
requires 0<=j < a.Length
modifies a
ensures a[j] == 60
ensures forall k :: 0 <= k < a.Length && k != j ==> a[k] == old(a[k])
|
method TestArrayElements(a:array<int>, j: nat)
requires 0<=j < a.Length
modifies a
ensures a[j] == 60
ensures forall k :: 0 <= k < a.Length && k != j ==> a[k] == old(a[k])
{
a[j] := 60;
}
|
56
|
triple
|
Triple the input.
|
method Triple (x:int) returns (r:int)
ensures r==3*x
|
method Triple (x:int) returns (r:int)
ensures r==3*x
{
r:= x*3;
}
|
57
|
triple2
|
Triple the input. There are no restrictions on the input.
|
method Triple (x:int) returns (r:int)
ensures r==3*x
|
method Triple (x:int) returns (r:int)
ensures r==3*x
{
if {
case x<18 =>
var a,b := 2*x, 4*x;
r:=(a+b)/2;
case 0<=x =>
var y:=2*x;
r:= x+y;
}
}
|
58
|
two_sum
|
Given an array of integers nums and an integer target, return the first pair of indices i < j of the two numbers such that they add up to target.
Require that such pair (i , j) exist in the array.
Ensure for all i less than returned i and for all j, that no other i, j exists.
Ensure for all j between returned i j, no other j exist.
|
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] + nums[jj] != target
ensures forall jj:: i < jj < j ==> nums[i] + nums[jj] != target
|
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] + nums[jj] != target
ensures forall jj:: i < jj < j ==> nums[i] + nums[jj] != target
{
var n := nums.Length;
i := 0;
j := 1;
while i < n - 1
invariant 0 <= i < j <= n
invariant forall ii, jj :: 0 <= ii < i && ii < jj < n ==> nums[ii] + nums[jj] != target
{
j := i + 1;
while j < n
invariant 0 <= i < j <= n
invariant forall jj :: i < jj < j ==> nums[i] + nums[jj] != target
{
if nums[i] + nums[j] == target {
return;
}
j := j + 1;
}
i := i + 1;
}
}
|
59
|
update_array
|
Verify some array assignment operations. Index 7 is assigned to 516. Index 4 increments by 3. All other elements stay the same. The array is at least 8 in length.
|
method UpdateElements(a: array<int>)
requires a.Length >= 8
modifies a
ensures old(a[4]) +3 == a[4]
ensures a[7]==516
ensures forall i::0 <= i<a.Length ==> i != 7 && i != 4 ==> a[i] == old(a[i])
|
method UpdateElements(a: array<int>)
requires a.Length >= 8
modifies a
ensures old(a[4]) +3 == a[4]
ensures a[7]==516
ensures forall i::0 <= i<a.Length ==> i != 7 && i != 4 ==> a[i] == old(a[i])
{
a[4] := a[4] + 3;
a[7] := 516;
}
|
60
|
update_map
|
Merges two maps m1 and m2, where keys present in m2 take precedence over m1. Ensure no new elements appear.
|
method update_map<K(!new), V>(m1: map<K, V>, m2: map<K, V>) returns (r: map<K, V>)
ensures (forall k :: k in m2 ==> k in r)
ensures (forall k :: k in m1 ==> k in r)
ensures (forall k :: k in m2 ==> r[k] == m2[k])
ensures (forall k :: !(k in m2) && k in m1 ==> r[k] == m1[k])
ensures (forall k :: !(k in m2) && !(k in m1) ==> !(k in r))
|
method update_map<K(!new), V>(m1: map<K, V>, m2: map<K, V>) returns (r: map<K, V>)
ensures (forall k :: k in m2 ==> k in r)
ensures (forall k :: k in m1 ==> k in r)
ensures (forall k :: k in m2 ==> r[k] == m2[k])
ensures (forall k :: !(k in m2) && k in m1 ==> r[k] == m1[k])
ensures (forall k :: !(k in m2) && !(k in m1) ==> !(k in r))
{
r:= map k | k in (m1.Keys + m2.Keys) :: if k in m2 then m2[k] else m1[k];
}
|
README.md exists but content is empty.
- Downloads last month
- 10