Datasets:
Download level_3/26.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 3.13 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/26.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/26.rs
-
curl -L -o 26.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/26.rs
3.13 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 | |
| } | |
| 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 f2_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 f2_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 f2_count_frequency(&arr, arr[index]) == 1 { | |
| unique_arr.push(arr[index]); | |
| } | |
| index += 1; | |
| } | |
| unique_arr | |
| } | |
| 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>, outp: Seq<i32>) -> bool { | |
| (outp == inp.filter(|x: i32| count_frequency_rcr(inp, x) == 1)) | |
| } | |
| 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>, 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, v2) && step3(v2, p3_key, r_final), | |
| { | |
| let x1 = f1_interleave(s1, s2, s3); | |
| let x2 = f2_remove_duplicates(&x1); | |
| let x3 = f3_contains(&x2, p3_key); | |
| proof { | |
| assert(step1(s1@, s2@, s3@, x1@)); | |
| assert(step2(x1@, x2@)); | |
| assert(step3(x2@, p3_key, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |