Datasets:
Download level_2/17.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_2/17.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_2/17.rs
-
curl -L -o 17.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_2/17.rs
2.3 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| fn f1_square_nums(nums: &Vec<i32>) -> (squared: Vec<i32>) | |
| requires | |
| forall|k: int| | |
| 0 <= k < nums.len() ==> (0 <= nums[k] * nums[k] < i32::MAX), | |
| { | |
| let mut result: Vec<i32> = Vec::new(); | |
| let mut index = 0; | |
| while index < nums.len() | |
| { | |
| result.push(nums[index] * nums[index]); | |
| index += 1 | |
| } | |
| result | |
| } | |
| proof fn f2_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 f2_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 f2_find_dissimilar(arr1: &Vec<i32>, arr2: &Vec<i32>) -> (result: Vec<i32>) | |
| { | |
| let mut result = Vec::new(); | |
| let mut index = 0; | |
| while index < arr1.len() | |
| { | |
| if (!f2_contains(arr2, arr1[index]) && !f2_contains(&result, arr1[index])) { | |
| result.push(arr1[index]); | |
| } | |
| index += 1; | |
| } | |
| let mut index = 0; | |
| while index < arr2.len() | |
| { | |
| if (!f2_contains(arr1, arr2[index]) && !f2_contains(&result, arr2[index])) { | |
| result.push(arr2[index]); | |
| } | |
| index += 1; | |
| } | |
| result | |
| } | |
| spec fn step1(__hp0: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (__hp0.len() == outp.len()) | |
| && (forall|k: int| 0 <= k < __hp0.len() ==> ( outp[k] == __hp0[k] * __hp0[k])) | |
| } | |
| spec fn step2(inp: Seq<i32>, p2_key: i32, outp: bool) -> bool { | |
| (outp == (exists|i: int| 0 <= i < inp.len() && (inp[i] == p2_key))) | |
| } | |
| fn chain_driver(nums: &Vec<i32>, p2_key: i32) -> (r_final: bool) | |
| requires | |
| forall|k: int| | |
| 0 <= k < nums.len() ==> (0 <= nums[k] * nums[k] < i32::MAX), | |
| ensures | |
| exists|v1: Seq<i32>| step1(nums@, v1) && step2(v1, p2_key, r_final), | |
| { | |
| let x1 = f1_square_nums(nums); | |
| let x2 = f2_contains(&x1, p2_key); | |
| proof { | |
| assert(step1(nums@, x1@)); | |
| assert(step2(x1@, p2_key, x2)); | |
| } | |
| x2 | |
| } | |
| } // verus! | |