Datasets:
Download level_3/52.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.68 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/52.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/52.rs
-
curl -L -o 52.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/52.rs
2.68 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| fn f1_interleave(s1: &Vec<i32>, s2: &Vec<i32>, s3: &Vec<i32>) -> (res: Vec<i32>) | |
| requires | |
| s1@.len() == s2@.len() && s2@.len() == s3@.len(), | |
| 0 <= (s1@.len() * 3) <= i32::MAX, | |
| { | |
| let new_seq_len = s1.len() * 3; | |
| let mut output_seq = Vec::with_capacity(new_seq_len); | |
| let mut index = 0; | |
| while index < s1.len() | |
| { | |
| output_seq.push(s1[index]); | |
| output_seq.push(s2[index]); | |
| output_seq.push(s3[index]); | |
| index += 1; | |
| } | |
| output_seq | |
| } | |
| 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_any_value_exists(arr1: &Vec<i32>, arr2: &Vec<i32>) -> (result: bool) | |
| { | |
| let mut index = 0; | |
| while index < arr1.len() | |
| { | |
| if (f3_contains(arr2, arr1[index])) { | |
| return true; | |
| } | |
| index += 1; | |
| } | |
| false | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: Seq<i32>, __hp2: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (outp.len() == __hp0.len() * 3) | |
| && (forall|i: int| | |
| 0 <= i < __hp0.len() ==> (outp[3 * i] == __hp0[i] && outp[3 * i + 1] == __hp1[i] && outp[3 * i + 2] | |
| == __hp2[i])) | |
| } | |
| 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(s1: &Vec<i32>, s2: &Vec<i32>, s3: &Vec<i32>, p2_elem: i32, p3_key: i32) -> (r_final: bool) | |
| requires | |
| s1@.len() == s2@.len() && s2@.len() == s3@.len(), | |
| 0 <= (s1@.len() * 3) <= i32::MAX, | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(s1@, s2@, s3@, v1) && step2(v1, p2_elem, v2) && step3(v2, p3_key, r_final), | |
| { | |
| let x1 = f1_interleave(s1, s2, s3); | |
| let x2 = f2_insert_before_each(&x1, p2_elem); | |
| let x3 = f3_contains(&x2, p3_key); | |
| proof { | |
| assert(step1(s1@, s2@, s3@, x1@)); | |
| assert(step2(x1@, p2_elem, x2@)); | |
| assert(step3(x2@, p3_key, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |