Datasets:
Download level_3/29.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/29.rs
- Command line
-
hf download hf://datasets/CodeWalk/verus-proof-completion/level_3/29.rs
-
curl -L -o 29.rs https://huggingface.co/datasets/CodeWalk/verus-proof-completion/resolve/main/level_3/29.rs
3.13 kB
| use vstd::prelude::*; | |
| fn main() {} | |
| verus!{ | |
| proof fn f1_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 f1_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 f1_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 (!f1_contains(arr2, arr1[index]) && !f1_contains(&result, arr1[index])) { | |
| result.push(arr1[index]); | |
| } | |
| index += 1; | |
| } | |
| let mut index = 0; | |
| while index < arr2.len() | |
| { | |
| if (!f1_contains(arr1, arr2[index]) && !f1_contains(&result, arr2[index])) { | |
| result.push(arr2[index]); | |
| } | |
| index += 1; | |
| } | |
| result | |
| } | |
| 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_has_only_one_distinct_element(arr: &Vec<i32>) -> (result: bool) | |
| { | |
| if arr.len() <= 1 { | |
| return true; | |
| } | |
| let mut index = 1; | |
| while index < arr.len() | |
| { | |
| if arr[0] != arr[index] { | |
| return false; | |
| } | |
| index += 1; | |
| } | |
| true | |
| } | |
| spec fn step1(__hp0: Seq<i32>, __hp1: Seq<i32>, outp: Seq<i32>) -> bool { | |
| (forall|i: int| | |
| 0 <= i < __hp0.len() ==> (!__hp1.contains( __hp0[i]) ==> outp.contains( | |
| __hp0[i], | |
| ))) | |
| && (forall|i: int| | |
| 0 <= i < __hp1.len() ==> (!__hp0.contains( __hp1[i]) ==> outp.contains( | |
| __hp1[i], | |
| ))) | |
| && (forall|i: int, j: int| | |
| 0 <= i < j < outp.len() ==> outp[i] != outp[j]) | |
| } | |
| 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>, outp: bool) -> bool { | |
| (outp == (forall|i: int| 1 <= i < inp.len() ==> inp[0] == inp[i])) | |
| } | |
| fn chain_driver(arr1: &Vec<i32>, arr2: &Vec<i32>, p2_elem: i32) -> (r_final: bool) | |
| ensures | |
| exists|v1: Seq<i32>, v2: Seq<i32>| step1(arr1@, arr2@, v1) && step2(v1, p2_elem, v2) && step3(v2, r_final), | |
| { | |
| let x1 = f1_find_dissimilar(arr1, arr2); | |
| let x2 = f2_insert_before_each(&x1, p2_elem); | |
| let x3 = f3_has_only_one_distinct_element(&x2); | |
| proof { | |
| assert(step1(arr1@, arr2@, x1@)); | |
| assert(step2(x1@, p2_elem, x2@)); | |
| assert(step3(x2@, x3)); | |
| } | |
| x3 | |
| } | |
| } // verus! | |