Datasets:
Download level_3/14.rs from CodeWalk/verus-proof-completion: direct link, hf CLI and curl.
- Browser
- Download file 2.25 kB
-
https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/14.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/14.rs
-
curl -L -o 14.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/14.rs
2.25 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| fn f1_replace_last_element(first: &Vec<i32>, second: &Vec<i32>) -> (replaced_list: Vec<i32>) | |
| requires | |
| first.len() > 0, | |
| { | |
| let mut replaced_list = Vec::new(); | |
| let first_end = first.len() - 1; | |
| let mut index = 0; | |
| while index < first_end | |
| { | |
| replaced_list.push(first[index]); | |
| index += 1; | |
| } | |
| index = 0; | |
| while index < second.len() | |
| { | |
| replaced_list.push(second[index]); | |
| index += 1; | |
| } | |
| replaced_list | |
| } | |
| 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_all_elements_equals(arr: &Vec<i32>, element: i32) -> (result: bool) | |
| { | |
| let mut index = 0; | |
| while index < arr.len() | |
| { | |
| if arr[index] != element { | |
| return false; | |
| } | |
| index += 1; | |
| } | |
| true | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (outp == __hp0.subrange(0, __hp0.len() - 1).add(__hp1)) | |
| } | |
| 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_element: i32, outp: bool) -> bool { | |
| (outp == (forall|i: int| 0 <= i < inp.len() ==> (inp[i] == p3_element))) | |
| } | |
| fn chain_driver(first: &Vec<i32>, second: &Vec<i32>, p2_elem: i32, p3_element: i32) -> (r_final: bool) | |
| requires | |
| first.len() > 0, | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(first@, second@, v1) && step2(v1, p2_elem, v2) && step3(v2, p3_element, r_final), | |
| { | |
| let x1 = f1_replace_last_element(first, second); | |
| let x2 = f2_insert_before_each(&x1, p2_elem); | |
| let x3 = f3_all_elements_equals(&x2, p3_element); | |
| proof { | |
| assert(step1(first@, second@, x1@)); | |
| assert(step2(x1@, p2_elem, x2@)); | |
| assert(step3(x2@, p3_element, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |