File size: 1,668 Bytes
c62beea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use vstd::prelude::*;
fn main() {}
verus!{



fn f1_split_and_append(list: &Vec<i32>, n: usize) -> (new_list: Vec<i32>)
    requires
        list@.len() > 0,
        0 < n < list@.len(),
{
    let mut new_list = Vec::new();
    let mut index = n;
    while index < list.len()
    {
        new_list.push(list[index]);
        index += 1;
    }
    let mut index = 0;
    while index < n
    {
        new_list.push(list[index]);
        index += 1;
    }
    new_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
}



spec fn step1(__hp0: Seq<i32>, __hp1: usize, outp: Seq<i32>) -> bool {
    (outp == __hp0.subrange(__hp1 as int, __hp0.len() as int).add(__hp0.subrange(0, __hp1 as int)))
}

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() ==> #[trigger] outp[2 * k] == p2_elem)
    && (forall|k: int| 0 <= k < inp.len() ==> #[trigger] outp[2 * k + 1] == inp[k])
}

fn chain_driver(list: &Vec<i32>, n: usize, p2_elem: i32) -> (r_final: Vec<i32>)
    requires
        list@.len() > 0,
        0 < n < list@.len(),
    ensures
        exists|v1: Seq<i32>| #[trigger] step1(list@, n, v1) && step2(v1, p2_elem, r_final@),
{
    let x1 = f1_split_and_append(list, n);
    let x2 = f2_insert_before_each(&x1, p2_elem);
    proof {
        assert(step1(list@, n, x1@));
        assert(step2(x1@, p2_elem, x2@));
    }
    x2
}

} // verus!