Datasets:
File size: 2,870 Bytes
c62beea | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | use vstd::prelude::*;
fn main() {}
verus!{
fn f1_replace_last_element(first: &Vec<i32>, second: &Vec<i32>) -> (replaced_list: Vec<i32>)
requires
first.len() > 0,
{
let mut replaced_list = Vec::new();
let first_end = first.len() - 1;
let mut index = 0;
while index < first_end
{
replaced_list.push(first[index]);
index += 1;
}
index = 0;
while index < second.len()
{
replaced_list.push(second[index]);
index += 1;
}
replaced_list
}
fn f2_insert_before_each(arr: &Vec<i32>, elem: i32) -> (result: Vec<i32>)
{
let mut result: Vec<i32> = Vec::new();
let mut index = 0;
while index < arr.len()
{
result.push(elem);
result.push(arr[index]);
index += 1;
}
result
}
pub open spec fn count_frequency_rcr(seq: Seq<i32>, key: i32) -> int
decreases seq.len(),
{
if seq.len() == 0 {
0
} else {
count_frequency_rcr(seq.drop_last(), key) + if (seq.last() == key) {
1 as int
} else {
0 as int
}
}
}
fn f3_count_frequency(arr: &Vec<i32>, key: i32) -> (frequency: usize)
{
let mut index = 0;
let mut counter = 0;
while index < arr.len()
{
if (arr[index] == key) {
counter += 1;
}
index += 1;
}
counter
}
fn f3_remove_duplicates(arr: &Vec<i32>) -> (unique_arr: Vec<i32>)
{
let mut unique_arr: Vec<i32> = Vec::new();
let input_len = arr.len();
let mut index = 0;
while index < arr.len()
{
if f3_count_frequency(&arr, arr[index]) == 1 {
unique_arr.push(arr[index]);
}
index += 1;
}
unique_arr
}
spec fn step1(__hp0: Seq<i32>, __hp1: Seq<i32>, outp: Seq<i32>) -> bool {
(outp == __hp0.subrange(0, __hp0.len() - 1).add(__hp1))
}
spec fn step2(inp: Seq<i32>, p2_elem: i32, outp: Seq<i32>) -> bool {
(outp.len() == (2 * inp.len()))
&& (forall|k: int| 0 <= k < inp.len() ==> #[trigger] outp[2 * k] == p2_elem)
&& (forall|k: int| 0 <= k < inp.len() ==> #[trigger] outp[2 * k + 1] == inp[k])
}
spec fn step3(inp: Seq<i32>, p3_key: i32, outp: usize) -> bool {
(count_frequency_rcr(inp, p3_key) == outp)
}
fn chain_driver(first: &Vec<i32>, second: &Vec<i32>, p2_elem: i32, p3_key: i32) -> (r_final: usize)
requires
first.len() > 0,
ensures
exists|v1: Seq<i32>, v2: Seq<i32>| #[trigger] step1(first@, second@, v1) && #[trigger] step2(v1, p2_elem, v2) && step3(v2, p3_key, r_final),
{
let x1 = f1_replace_last_element(first, second);
let x2 = f2_insert_before_each(&x1, p2_elem);
let x3 = f3_count_frequency(&x2, p3_key);
proof {
assert(step1(first@, second@, x1@));
assert(step2(x1@, p2_elem, x2@));
assert(step3(x2@, p3_key, x3));
}
x3
}
} // verus!
|