Problem: k_smallest

Your task is to specify the K-th Smallest Element algorithm acting on a vector of integers and verify its correctness properties in Verus. You need to implement a selection algorithm that rearranges elements to find the value that would occupy the $k$-th position if the array were fully sorted. Specifically, you must prove the following: (1) Data Integrity (Permutation): The final state of the mutable vector must be a valid permutation of its initial state, ensuring that the set of values remains identical and no elements are created or destroyed. (2) Correctness of Selection: The returned result must be proven to be the $k$-th smallest value of the original input; formally, this means there exists a theoretical sorted permutation of the input sequence where the value at index $k$ is exactly the returned result.

use vstd::prelude::*;

verus! {
    // Following is the block for necessary definitions
    // <preamble>
    spec fn is_sorted(s: Seq<i32>) -> bool {
        forall|i: int, j: int| 0 <= i < j < s.len() ==> s[i] <= s[j]
    }

    spec fn is_valid_index_permutation(p: Seq<int>, n: int) -> bool {
        &&& p.len() == n
        &&& (forall|i: int| 0 <= i < n ==> 0 <= #[trigger] p[i] < n)
        &&& (forall|i: int, j: int| 0 <= i < j < n ==> #[trigger] p[i] != #[trigger] p[j])
    }

    spec fn is_permutation(v1: Seq<i32>, v2: Seq<i32>) -> bool {
        exists|p: Seq<int>| 
            is_valid_index_permutation(p, v1.len() as int) 
            && v1.len() == v2.len()
            && (forall|i: int| 0 <= i < v1.len() ==> v2[i] == v1[#[trigger] p[i]])
    }

    // Mathematical definition: "val" is the k-th smallest element of "s" if
    // the sorted version of "s" has "val" at index "k".
    spec fn is_kth_smallest(s: Seq<i32>, k: int, val: i32) -> bool {
        exists|sorted_s: Seq<i32>|
            is_permutation(s, sorted_s)
            // Manual trigger added here to satisfy the solver without warning
            && #[trigger] is_sorted(sorted_s)
            && 0 <= k < s.len()
            && sorted_s[k] == val
    }
    // </preamble>

    // Following is the block for potential helper specifications
    // <helpers>
    
    // </helpers>

    // Following is the block for proofs of lemmas
    // <proofs>

    // </proofs>

    // Following is the block for the main specification
    // <spec>
    fn quick_select(v: &mut Vec<i32>, k: usize) -> (res: i32)
        requires
            0 <= k < old(v).len(),
        ensures
            // 1. Data Integrity: The array remains a valid permutation of the input
            is_permutation(old(v)@, v@),
            
            // 2. Correctness: The return value is truly the k-th smallest element
            is_kth_smallest(old(v)@, k as int, res),
    // </spec>
    // <code>
    {
        // Implement and verify the algorithm to find the k-th smallest element
    }
    // </code>

    fn main() {}
}