use crate::prelude::*;
use core::cmp::Ordering;
pub trait PartialOrdLogic {
#[logic]
fn lt_log(self, other: Self) -> bool;
#[logic(open, inline, sealed)]
fn gt_log(self, other: Self) -> bool {
other < self
}
#[logic(open, inline)]
fn le_log(self, other: Self) -> bool {
pearlite! { self < other || self == other }
}
#[logic(open, inline, sealed)]
fn ge_log(self, other: Self) -> bool {
other <= self
}
#[logic(law)]
#[ensures(!(self < self))]
fn irreflexive(self);
#[logic(law)]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self);
#[logic(law)]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self);
#[logic(open, sealed)]
fn partial_cmp_log(self, other: Self) -> Option<Ordering> {
if self == other {
Some(Ordering::Equal)
} else if self < other {
Some(Ordering::Less)
} else if other < self {
Some(Ordering::Greater)
} else {
None
}
}
}
#[allow(unused)]
pub trait OrdLogic: PartialOrdLogic {
#[logic(law)]
#[ensures(self < other || self == other || other < self)]
fn lt_log_total(self, other: Self);
#[logic(open, sealed)]
fn cmp_log(self, other: Self) -> Ordering {
if self == other {
Ordering::Equal
} else if self < other {
Ordering::Less
} else {
Ordering::Greater
}
}
}
#[macro_export]
macro_rules! partial_ord_laws_impl {
( $($lemma:stmt)* ) => {
#[::creusot_std::macros::logic(law)]
#[::creusot_std::macros::ensures(!(self < self))]
fn irreflexive(self) {
$($lemma)*
}
#[::creusot_std::macros::logic(law)]
#[::creusot_std::macros::requires(x < y)]
#[::creusot_std::macros::requires(y < z)]
#[::creusot_std::macros::ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {
$($lemma)*
}
#[::creusot_std::macros::logic(law)]
#[::creusot_std::macros::ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {
$($lemma)*
}
};
}
pub use partial_ord_laws_impl;
impl<T: PartialOrdLogic> PartialOrdLogic for &T {
#[logic(open, inline)]
fn lt_log(self, other: Self) -> bool {
*self < *other
}
#[logic(open, inline)]
fn le_log(self, other: Self) -> bool {
*self <= *other
}
partial_ord_laws_impl! {}
}
impl<T: OrdLogic> OrdLogic for &T {
#[logic]
#[ensures(self < other || self == other || other < self)]
fn lt_log_total(self, other: Self) {
let _ = T::lt_log_total;
}
}
impl PartialOrdLogic for Int {
#[logic]
#[builtin("int.Int.(<=)")]
fn le_log(self, _: Self) -> bool {
dead
}
#[logic]
#[builtin("int.Int.(<)")]
fn lt_log(self, _: Self) -> bool {
dead
}
#[logic]
#[ensures(!(self < self))]
fn irreflexive(self) {}
#[logic]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {}
#[logic]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {}
}
impl OrdLogic for Int {
#[logic]
#[ensures(self < other || self == other || other < self)]
fn lt_log_total(self, other: Self) {}
}
macro_rules! ord_logic_impl {
($t:ty, $module:literal) => {
impl PartialOrdLogic for $t {
#[logic]
#[builtin(concat!($module, ".le"))]
fn le_log(self, _: Self) -> bool {
dead
}
#[logic]
#[builtin(concat!($module, ".lt"))]
fn lt_log(self, _: Self) -> bool {
dead
}
#[logic]
#[ensures(!(self < self))]
fn irreflexive(self) {}
#[logic]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {}
#[logic]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {}
}
impl OrdLogic for $t {
#[logic]
#[ensures(self < other || self == other || other < self)]
fn lt_log_total(self, other: Self) {}
}
};
}
impl PartialOrdLogic for f32 {
#[logic]
#[builtin("creusot.float.Float32.lt")]
fn lt_log(self, _: Self) -> bool {
dead
}
#[logic]
#[ensures(!(self < self))]
fn irreflexive(self) {}
#[logic]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {}
#[logic]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {}
}
impl PartialOrdLogic for f64 {
#[logic]
#[builtin("creusot.float.Float64.lt")]
fn lt_log(self, _: Self) -> bool {
dead
}
#[logic]
#[ensures(!(self < self))]
fn irreflexive(self) {}
#[logic]
#[requires(x < y)]
#[requires(y < z)]
#[ensures(x < z)]
fn transitive(x: Self, y: Self, z: Self) {}
#[logic]
#[ensures((self <= other) == (self < other || self == other))]
fn le_lt_log(self, other: Self) {}
}
ord_logic_impl!(u8, "creusot.int.UInt8$BW$");
ord_logic_impl!(u16, "creusot.int.UInt16$BW$");
ord_logic_impl!(u32, "creusot.int.UInt32$BW$");
ord_logic_impl!(u64, "creusot.int.UInt64$BW$");
ord_logic_impl!(u128, "creusot.int.UInt128$BW$");
#[cfg(target_pointer_width = "64")]
ord_logic_impl!(usize, "creusot.int.UInt64$BW$");
#[cfg(target_pointer_width = "32")]
ord_logic_impl!(usize, "creusot.int.UInt32$BW$");
#[cfg(target_pointer_width = "16")]
ord_logic_impl!(usize, "creusot.int.UInt16$BW$");
ord_logic_impl!(i8, "creusot.int.Int8$BW$");
ord_logic_impl!(i16, "creusot.int.Int16$BW$");
ord_logic_impl!(i32, "creusot.int.Int32$BW$");
ord_logic_impl!(i64, "creusot.int.Int64$BW$");
ord_logic_impl!(i128, "creusot.int.Int128$BW$");
#[cfg(target_pointer_width = "64")]
ord_logic_impl!(isize, "creusot.int.Int64$BW$");
#[cfg(target_pointer_width = "32")]
ord_logic_impl!(isize, "creusot.int.Int32$BW$");
#[cfg(target_pointer_width = "16")]
ord_logic_impl!(isize, "creusot.int.Int16$BW$");
ord_logic_impl!(char, "creusot.prelude.Char");
ord_logic_impl!(bool, "creusot.prelude.Bool");
impl<A: PartialOrdLogic, B: PartialOrdLogic> PartialOrdLogic for (A, B) {
#[logic(open)]
fn lt_log(self, o: Self) -> bool {
self.0 == o.0 && self.1 < o.1 || self.0 < o.0
}
#[logic(open)]
fn le_log(self, o: Self) -> bool {
self.0 == o.0 && self.1 <= o.1 || self.0 < o.0
}
partial_ord_laws_impl! {}
}