Datasets:
Download level_3/51.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/51.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/51.rs
-
curl -L -o 51.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/51.rs
3.27 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| proof fn f1_lemma_vec_push<T>(vec: Seq<T>, i: T, l: usize) | |
| requires | |
| l == vec.len(), | |
| ensures | |
| forall|k: int| 0 <= k < vec.len() ==> vec[k] == vec.push(i)[k], | |
| vec.push(i).index(l as int) == i, | |
| { | |
| } | |
| fn f1_contains(arr: &Vec<i32>, key: i32) -> (result: bool) | |
| { | |
| let mut index = 0; | |
| while index < arr.len() | |
| { | |
| if (arr[index] == key) { | |
| return true; | |
| } | |
| index += 1; | |
| } | |
| false | |
| } | |
| fn f1_difference(arr1: &Vec<i32>, arr2: &Vec<i32>) -> (result: Vec<i32>) | |
| { | |
| let mut result = Vec::new(); | |
| let mut index = 0; | |
| while index < arr1.len() | |
| { | |
| if (!f1_contains(arr2, arr1[index]) && !f1_contains(&result, arr1[index])) { | |
| result.push(arr1[index]); | |
| } | |
| index += 1; | |
| } | |
| index = 0; | |
| while index < arr2.len() | |
| { | |
| if (!f1_contains(arr1, arr2[index]) && !f1_contains(&result, arr2[index])) { | |
| result.push(arr2[index]); | |
| } | |
| index += 1; | |
| } | |
| result | |
| } | |
| fn f2_find_negative_numbers(arr: &Vec<i32>) -> (negative_list: Vec<i32>) | |
| { | |
| let mut negative_list: Vec<i32> = Vec::new(); | |
| let input_len = arr.len(); | |
| let mut index = 0; | |
| while index < arr.len() | |
| { | |
| if (arr[index] < 0) { | |
| negative_list.push(arr[index]); | |
| } | |
| index += 1; | |
| } | |
| negative_list | |
| } | |
| 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_shared_elements(list1: &Vec<i32>, list2: &Vec<i32>) -> (shared: Vec<i32>) | |
| { | |
| let mut shared = Vec::new(); | |
| let mut index = 0; | |
| while index < list1.len() | |
| { | |
| if (f3_contains(list2, list1[index]) && !f3_contains(&shared, list1[index])) { | |
| shared.push(list1[index]); | |
| } | |
| index += 1 | |
| } | |
| shared | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (forall|i: int| | |
| 0 <= i < __hp0.len() ==> (!__hp1.contains( __hp0[i]) ==> outp.contains( | |
| __hp0[i], | |
| ))) | |
| && (forall|i: int| | |
| 0 <= i < __hp1.len() ==> (!__hp0.contains( __hp1[i]) ==> outp.contains( | |
| __hp1[i], | |
| ))) | |
| && (forall|i: int, j: int| | |
| 0 <= i < j < outp.len() ==> outp[i] != outp[j]) | |
| } | |
| spec fn step2(inp: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (outp == inp.filter(|x: i32| x < 0)) | |
| } | |
| 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(arr1: &Vec<i32>, arr2: &Vec<i32>, p3_key: i32) -> (r_final: bool) | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(arr1@, arr2@, v1) && step2(v1, v2) && step3(v2, p3_key, r_final), | |
| { | |
| let x1 = f1_difference(arr1, arr2); | |
| let x2 = f2_find_negative_numbers(&x1); | |
| let x3 = f3_contains(&x2, p3_key); | |
| proof { | |
| assert(step1(arr1@, arr2@, x1@)); | |
| assert(step2(x1@, x2@)); | |
| assert(step3(x2@, p3_key, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |