|
| 1 | +use super::super::prelude::*; |
| 2 | +use super::cmp::{ |
| 3 | + OrdSpec, OrdSpecImpl, PartialEqSpec, PartialEqSpecImpl, PartialOrdSpec, PartialOrdSpecImpl, |
| 4 | +}; |
| 5 | +use super::convert::FromSpecImpl; |
| 6 | +use super::ops::{BitOrSpec, BitOrSpecImpl}; |
| 7 | +use core::cmp::Ordering; |
| 8 | +use core::num::{NonZero, ZeroablePrimitive}; |
| 9 | +use core::ops::BitOr; |
| 10 | + |
| 11 | +verus! { |
| 12 | + |
| 13 | +#[verifier::external_trait_specification] |
| 14 | +#[verifier::external_trait_extension(ZeroablePrimitiveSpec via ZeroablePrimitiveSpecImpl)] |
| 15 | +#[verifier::external_trait_private_bound(core::num::nonzero::private::Sealed)] |
| 16 | +pub trait ExZeroablePrimitive: Sized + Copy { |
| 17 | + type ExternalTraitSpecificationFor: ZeroablePrimitive; |
| 18 | + |
| 19 | + spec fn is_zero(self) -> bool; |
| 20 | +} |
| 21 | + |
| 22 | +macro_rules! impl_zeroable_primitive_spec_impl { |
| 23 | + ($($t:ty),*) => { |
| 24 | + $( |
| 25 | + verus! { |
| 26 | + impl ZeroablePrimitiveSpecImpl for $t { |
| 27 | + open spec fn is_zero(self) -> bool { |
| 28 | + self == 0 |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + )* |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +// The implementators of `ZeroablePrimitive` coincide with `Integer`. |
| 37 | +impl_zeroable_primitive_spec_impl!(char, u8, u16, u32, u64, usize, i8, i16, i32, i64, isize); |
| 38 | + |
| 39 | +#[verifier::external_type_specification] |
| 40 | +#[verifier::external_body] |
| 41 | +#[verifier::reject_recursive_types(T)] |
| 42 | +pub struct ExNonZero<T: ZeroablePrimitive>(NonZero<T>); |
| 43 | + |
| 44 | +impl<T: ZeroablePrimitive> View for NonZero<T> { |
| 45 | + type V = T; |
| 46 | + |
| 47 | + uninterp spec fn view(&self) -> Self::V; |
| 48 | +} |
| 49 | + |
| 50 | +// Need this to define `BitOrSpecImpl` |
| 51 | +pub uninterp spec fn nonzero_from_primitive<T: ZeroablePrimitive>(n: T) -> NonZero<T>; |
| 52 | + |
| 53 | +pub broadcast axiom fn axiom_nonzero_from_primitive_view_eq<T: ZeroablePrimitive>(n: T) |
| 54 | + requires |
| 55 | + !n.is_zero(), |
| 56 | + ensures |
| 57 | + #[trigger] nonzero_from_primitive(n)@ == n, |
| 58 | +; |
| 59 | + |
| 60 | +pub broadcast axiom fn axiom_view_nonzero_from_primitive_eq<T: ZeroablePrimitive>(n: NonZero<T>) |
| 61 | + ensures |
| 62 | + #[trigger] nonzero_from_primitive(n@) == n, |
| 63 | +; |
| 64 | + |
| 65 | +pub broadcast axiom fn axiom_nonzero_is_not_zero<T: ZeroablePrimitive>(n: NonZero<T>) |
| 66 | + ensures |
| 67 | + !(#[trigger] n@).is_zero(), |
| 68 | +; |
| 69 | + |
| 70 | +pub assume_specification<T: ZeroablePrimitive>[ NonZero::<T>::new ](n: T) -> (ret: Option< |
| 71 | + NonZero<T>, |
| 72 | +>) |
| 73 | + ensures |
| 74 | + match ret { |
| 75 | + Some(nz) => nz@ == n && !n.is_zero(), |
| 76 | + None => n.is_zero(), |
| 77 | + }, |
| 78 | + opens_invariants none |
| 79 | + no_unwind |
| 80 | +; |
| 81 | + |
| 82 | +pub assume_specification<T: ZeroablePrimitive>[ NonZero::<T>::new_unchecked ](n: T) -> (ret: |
| 83 | + NonZero<T>) |
| 84 | + requires |
| 85 | + !n.is_zero(), |
| 86 | + ensures |
| 87 | + ret@ == n, |
| 88 | + opens_invariants none |
| 89 | + no_unwind |
| 90 | +; |
| 91 | + |
| 92 | +#[verifier::inline] |
| 93 | +pub open spec fn nonzero_spec_get<T: ZeroablePrimitive>(n: NonZero<T>) -> T { |
| 94 | + n@ |
| 95 | +} |
| 96 | + |
| 97 | +#[verifier::when_used_as_spec(nonzero_spec_get)] |
| 98 | +pub assume_specification<T: ZeroablePrimitive>[ NonZero::<T>::get ](n: NonZero<T>) -> T |
| 99 | + returns |
| 100 | + n@, |
| 101 | + opens_invariants none |
| 102 | + no_unwind |
| 103 | +; |
| 104 | + |
| 105 | +impl<T: ZeroablePrimitive + PartialEqSpec> PartialEqSpecImpl for NonZero<T> { |
| 106 | + open spec fn obeys_eq_spec() -> bool { |
| 107 | + true |
| 108 | + } |
| 109 | + |
| 110 | + open spec fn eq_spec(&self, other: &Self) -> bool { |
| 111 | + self.get().eq_spec(&other.get()) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// Ord is not implemented because of the [`Destruct`](https://doc.rust-lang.org/std/marker/trait.Destruct.html) trait bound. |
| 116 | +impl<T: ZeroablePrimitive + PartialOrdSpec> PartialOrdSpecImpl for NonZero<T> { |
| 117 | + open spec fn obeys_partial_cmp_spec() -> bool { |
| 118 | + true |
| 119 | + } |
| 120 | + |
| 121 | + open spec fn partial_cmp_spec(&self, other: &Self) -> Option<Ordering> { |
| 122 | + self.get().partial_cmp_spec(&other.get()) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<T: ZeroablePrimitive + BitOrSpec<Output = T>> BitOrSpecImpl<T> for NonZero<T> { |
| 127 | + open spec fn obeys_bitor_spec() -> bool { |
| 128 | + true |
| 129 | + } |
| 130 | + |
| 131 | + open spec fn bitor_req(self, rhs: T) -> bool { |
| 132 | + self.get().bitor_req(rhs) |
| 133 | + } |
| 134 | + |
| 135 | + open spec fn bitor_spec(self, rhs: T) -> Self::Output { |
| 136 | + nonzero_from_primitive(self.get().bitor_spec(rhs)) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<T: ZeroablePrimitive + BitOrSpec<Output = T>> BitOrSpecImpl<NonZero<T>> for NonZero<T> { |
| 141 | + open spec fn obeys_bitor_spec() -> bool { |
| 142 | + true |
| 143 | + } |
| 144 | + |
| 145 | + open spec fn bitor_req(self, rhs: NonZero<T>) -> bool { |
| 146 | + self.get().bitor_req(rhs.get()) |
| 147 | + } |
| 148 | + |
| 149 | + open spec fn bitor_spec(self, rhs: NonZero<T>) -> Self::Output { |
| 150 | + nonzero_from_primitive(self.get().bitor_spec(rhs.get())) |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +impl<T: ZeroablePrimitive> FromSpecImpl<NonZero<T>> for T { |
| 155 | + open spec fn obeys_from_spec() -> bool { |
| 156 | + true |
| 157 | + } |
| 158 | + |
| 159 | + open spec fn from_spec(nz: NonZero<T>) -> Self { |
| 160 | + nz.get() |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +pub assume_specification<T: ZeroablePrimitive>[ <NonZero<T> as Clone>::clone ]( |
| 165 | + nz: &NonZero<T>, |
| 166 | +) -> NonZero<T> |
| 167 | + returns |
| 168 | + nz, |
| 169 | +; |
| 170 | + |
| 171 | +pub broadcast group group_nonzero_axioms { |
| 172 | + axiom_nonzero_from_primitive_view_eq, |
| 173 | + axiom_view_nonzero_from_primitive_eq, |
| 174 | + axiom_nonzero_is_not_zero, |
| 175 | +} |
| 176 | + |
| 177 | +} // verus! |
0 commit comments