Skip to main content

cmp_fn_total_on

Function cmp_fn_total_on 

Source
pub open spec fn cmp_fn_total_on<T, F: FnMut(&T, &T) -> Ordering>(
    compare: F,
    s: Seq<T>,
) -> bool
Expand description
{
    &&& forall |i: int, j: int, o1: Ordering, o2: Ordering| {
        0 <= i < s.len() && 0 <= j < s.len() && call_ensures(compare, (&s[i], &s[j]), o1)
            && call_ensures(compare, (&s[j], &s[i]), o2)
            ==> o2
                == match o1 {
                    Ordering::Less => Ordering::Greater,
                    Ordering::Equal => Ordering::Equal,
                    Ordering::Greater => Ordering::Less,
                }
    }
    &&& forall |i: int, j: int, o1: Ordering, o2: Ordering| {
        0 <= i < s.len() && 0 <= j < s.len() && call_ensures(compare, (&s[i], &s[j]), o1)
            && call_ensures(compare, (&s[i], &s[j]), o2) ==> o1 == o2
    }
    &&& forall |i: int, j: int, k: int, o1: Ordering, o2: Ordering, o3: Ordering| {
        0 <= i < s.len() && 0 <= j < s.len() && 0 <= k < s.len()
            && call_ensures(compare, (&s[i], &s[j]), o1)
            && call_ensures(compare, (&s[j], &s[k]), o2)
            && call_ensures(compare, (&s[i], &s[k]), o3) && o1 != Ordering::Greater
            && o2 != Ordering::Greater
            ==> o3 != Ordering::Greater
                && (o1 == Ordering::Less || o2 == Ordering::Less
                    ==> o3 == Ordering::Less)
    }

}

compare is a total order on the elements of s (antisymmetric, transitive, and deterministic), which std’s sorts require: since Rust 1.81 they may panic otherwise (“user-provided comparison function does not correctly implement a total order”).