use crate::{
logic::ord::ord_laws_impl,
prelude::{Clone, Default, *},
};
use core::cmp::Ordering;
#[derive(Default, Clone, Copy, Eq)]
#[non_exhaustive]
#[repr(transparent)]
pub struct PeanoInt(pub u64);
impl DeepModel for PeanoInt {
type DeepModelTy = u64;
#[logic(open, inline)]
fn deep_model(self) -> u64 {
self.0
}
}
impl OrdLogic for PeanoInt {
#[logic(open, inline)]
fn cmp_log(self, o: Self) -> Ordering {
self.0.cmp_log(o.0)
}
#[logic(open, inline)]
fn le_log(self, o: Self) -> bool {
self.0.le_log(o.0)
}
#[logic(open, inline)]
fn lt_log(self, o: Self) -> bool {
self.0.lt_log(o.0)
}
#[logic(open, inline)]
fn ge_log(self, o: Self) -> bool {
self.0.ge_log(o.0)
}
#[logic(open, inline)]
fn gt_log(self, o: Self) -> bool {
self.0.gt_log(o.0)
}
ord_laws_impl! {}
}
impl View for PeanoInt {
type ViewTy = u64;
#[logic(open, inline)]
fn view(self) -> u64 {
self.0
}
}
impl PartialOrd for PeanoInt {
#[check(ghost)]
#[ensures(result == Some((*self).cmp_log(*other)))]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
#[check(ghost)]
#[ensures(result == (self@ < other@))]
fn lt(&self, other: &Self) -> bool {
matches!(self.partial_cmp(other), Some(Ordering::Less))
}
#[check(ghost)]
#[ensures(result == (self@ <= other@))]
fn le(&self, other: &Self) -> bool {
matches!(self.partial_cmp(other), Some(Ordering::Less | Ordering::Equal))
}
#[check(ghost)]
#[ensures(result == (self@ > other@))]
fn gt(&self, other: &Self) -> bool {
matches!(self.partial_cmp(other), Some(Ordering::Greater))
}
#[check(ghost)]
#[ensures(result == (self@ >= other@))]
fn ge(&self, other: &Self) -> bool {
matches!(self.partial_cmp(other), Some(Ordering::Greater | Ordering::Equal))
}
}
impl Ord for PeanoInt {
#[check(ghost)]
#[ensures(result == (*self).cmp_log(*other))]
fn cmp(&self, other: &Self) -> Ordering {
self.0.cmp(&other.0)
}
}
impl PartialEq for PeanoInt {
#[check(ghost)]
#[ensures(result == (*self == *other))]
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl PeanoInt {
#[check(ghost)]
#[ensures(result.0 == 0u64)]
pub fn new() -> Self {
Self(0)
}
#[trusted]
#[check(terminates)]
#[ensures(result.0@ == self.0@ + 1)]
pub fn incr(self) -> Self {
let x = unsafe { core::ptr::read_volatile(&self.0) };
Self(x + 1)
}
#[check(ghost)]
#[ensures(result == self.0)]
pub fn to_u64(self) -> u64 {
self.0
}
#[check(ghost)]
#[trusted]
#[ensures(result@ == self.0@)]
pub fn to_i64(self) -> i64 {
self.0 as i64
}
#[check(ghost)]
#[ensures(result@ == self.0@)]
pub fn to_u128(self) -> u128 {
self.0 as u128
}
#[check(ghost)]
#[ensures(result@ == self.0@)]
pub fn to_i128(self) -> i128 {
self.0 as i128
}
}
impl From<PeanoInt> for u64 {
#[check(ghost)]
#[ensures(result == val.0)]
fn from(val: PeanoInt) -> Self {
val.to_u64()
}
}
impl From<PeanoInt> for i64 {
#[check(ghost)]
#[ensures(result@ == val.0@)]
fn from(val: PeanoInt) -> Self {
val.to_i64()
}
}
impl From<PeanoInt> for u128 {
#[check(ghost)]
#[ensures(result@ == val.0@)]
fn from(val: PeanoInt) -> Self {
val.to_u128()
}
}
impl From<PeanoInt> for i128 {
#[check(ghost)]
#[ensures(result@ == val.0@)]
fn from(val: PeanoInt) -> Self {
val.to_i128()
}
}