Datasets:
Download level_2/5.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.4 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_2/5.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_2/5.rs
-
curl -L -o 5.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_2/5.rs
2.4 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 | |
| } | |
| 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_key: i32, outp: usize) -> bool { | |
| (count_frequency_rcr(inp, p2_key) == outp) | |
| } | |
| fn chain_driver(s1: &Vec<i32>, s2: &Vec<i32>, s3: &Vec<i32>, p2_key: i32) -> (r_final: usize) | |
| requires | |
| s1@.len() == s2@.len() && s2@.len() == s3@.len(), | |
| 0 <= (s1@.len() * 3) <= i32::MAX, | |
| ensures | |
| exists|v1: Seq<i32>| step1(s1@, s2@, s3@, v1) && step2(v1, p2_key, r_final), | |
| { | |
| let x1 = f1_interleave(s1, s2, s3); | |
| let x2 = f2_count_frequency(&x1, p2_key); | |
| proof { | |
| assert(step1(s1@, s2@, s3@, x1@)); | |
| assert(step2(x1@, p2_key, x2)); | |
| } | |
| x2 | |
| } | |
| } // verus! | |