Datasets:
Download level_3/22.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/22.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/22.rs
-
curl -L -o 22.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/22.rs
2.58 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| fn f1_reverse_to_k(list: &Vec<i32>, n: usize) -> (reversed_list: Vec<i32>) | |
| requires | |
| list@.len() > 0, | |
| 0 < n < list@.len(), | |
| { | |
| let mut reversed_list = Vec::new(); | |
| let mut index = 0; | |
| while index < n | |
| { | |
| reversed_list.push(list[n - 1 - index]); | |
| index += 1; | |
| } | |
| index = n; | |
| while index < list.len() | |
| { | |
| reversed_list.push(list[index]); | |
| index += 1; | |
| } | |
| reversed_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 | |
| } | |
| fn f3_contains(arr: &Vec<i32>, key: i32) -> (result: bool) | |
| { | |
| let mut i = 0; | |
| while i < arr.len() | |
| { | |
| if (arr[i] == key) { | |
| return true; | |
| } | |
| i += 1; | |
| } | |
| false | |
| } | |
| fn f3_intersection(arr1: &Vec<i32>, arr2: &Vec<i32>) -> (result: Vec<i32>) | |
| { | |
| let mut output_arr = Vec::new(); | |
| let mut index = 0; | |
| while index < arr1.len() | |
| { | |
| if (f3_contains(arr2, arr1[index]) && !f3_contains(&output_arr, arr1[index])) { | |
| output_arr.push(arr1[index]); | |
| } | |
| index += 1; | |
| } | |
| output_arr | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: usize, outp: Seq<i32>) -> bool { | |
| (outp == __hp0.subrange(0, __hp1 as int).reverse().add( | |
| __hp0.subrange(__hp1 as int, __hp0.len() as int), | |
| )) | |
| } | |
| 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() ==> outp[2 * k] == p2_elem) | |
| && (forall|k: int| 0 <= k < inp.len() ==> outp[2 * k + 1] == inp[k]) | |
| } | |
| spec fn step3(inp: Seq<i32>, p3_key: i32, outp: bool) -> bool { | |
| (outp == (exists|i: int| 0 <= i < inp.len() && (inp[i] == p3_key))) | |
| } | |
| fn chain_driver(list: &Vec<i32>, n: usize, p2_elem: i32, p3_key: i32) -> (r_final: bool) | |
| requires | |
| list@.len() > 0, | |
| 0 < n < list@.len(), | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(list@, n, v1) && step2(v1, p2_elem, v2) && step3(v2, p3_key, r_final), | |
| { | |
| let x1 = f1_reverse_to_k(list, n); | |
| let x2 = f2_insert_before_each(&x1, p2_elem); | |
| let x3 = f3_contains(&x2, p3_key); | |
| proof { | |
| assert(step1(list@, n, x1@)); | |
| assert(step2(x1@, p2_elem, x2@)); | |
| assert(step3(x2@, p3_key, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |