1use crate::dbm::MAX_VARIABLES;
51use crate::error::Error;
52use creusot_std::{
53 invariant::{Invariant, inv},
54 logic::real::Real,
55 prelude::{Clone, PartialEq, *},
56};
57
58pub const MAX_CONSTANT: i32 = 0x1FFF_FFFF;
63pub const MIN_CONSTANT: i32 = -0x1FFF_FFFF;
66
67const INF: i32 = 0x7FFF_FFFE;
69const MIN_CODE: i32 = -0x3FFF_FFFE;
71const MAX_CODE: i32 = 0x3FFF_FFFF;
73
74pub enum BoundModel {
77 Lt(Int),
79 Le(Int),
81 Inf,
83}
84
85impl BoundModel {
86 #[logic(open)]
88 pub fn in_range(c: Int) -> bool {
89 pearlite! { MIN_CONSTANT@ <= c && c <= MAX_CONSTANT@ }
90 }
91
92 #[logic(open)]
94 pub fn le(self, other: BoundModel) -> bool {
95 match (self, other) {
96 (_, BoundModel::Inf) => true,
97 (BoundModel::Inf, _) => false,
98 (BoundModel::Lt(a), BoundModel::Lt(b)) => a <= b,
99 (BoundModel::Lt(a), BoundModel::Le(b)) => a <= b,
100 (BoundModel::Le(a), BoundModel::Lt(b)) => a < b,
101 (BoundModel::Le(a), BoundModel::Le(b)) => a <= b,
102 }
103 }
104
105 #[logic(open)]
108 pub fn add(self, other: BoundModel) -> BoundModel {
109 match (self, other) {
110 (BoundModel::Inf, _) | (_, BoundModel::Inf) => BoundModel::Inf,
111 (BoundModel::Le(a), BoundModel::Le(b)) => BoundModel::Le(a + b),
112 (BoundModel::Lt(a), BoundModel::Lt(b))
113 | (BoundModel::Lt(a), BoundModel::Le(b))
114 | (BoundModel::Le(a), BoundModel::Lt(b)) => BoundModel::Lt(a + b),
115 }
116 }
117
118 #[logic(open)]
120 pub fn add_in_range(self, other: BoundModel) -> bool {
121 match (self, other) {
122 (BoundModel::Inf, _) | (_, BoundModel::Inf) => true,
123 (BoundModel::Lt(a) | BoundModel::Le(a), BoundModel::Lt(b) | BoundModel::Le(b)) => {
124 BoundModel::in_range(a + b)
125 }
126 }
127 }
128
129 #[logic(open)]
132 pub fn neg(self) -> BoundModel {
133 match self {
134 BoundModel::Lt(c) => BoundModel::Le(-c),
135 BoundModel::Le(c) => BoundModel::Lt(-c),
136 BoundModel::Inf => BoundModel::Inf,
137 }
138 }
139
140 #[logic(open)]
142 pub fn representable(self) -> bool {
143 match self {
144 BoundModel::Lt(c) | BoundModel::Le(c) => BoundModel::in_range(c),
145 BoundModel::Inf => true,
146 }
147 }
148}
149
150#[logic(open)]
153pub fn sat(b: BoundModel, d: Real) -> bool {
154 match b {
155 BoundModel::Lt(c) => d < Real::from_int(c),
156 BoundModel::Le(c) => d <= Real::from_int(c),
157 BoundModel::Inf => true,
158 }
159}
160
161#[logic]
163#[requires(sat(a, x) && sat(b, y))]
164#[ensures(sat(a.add(b), x + y))]
165pub fn add_sound(a: BoundModel, b: BoundModel, x: Real, y: Real) {}
166
167#[logic]
169#[requires(a.le(b))]
170#[requires(sat(a, d))]
171#[ensures(sat(b, d))]
172pub fn le_sound(a: BoundModel, b: BoundModel, d: Real) {}
173
174#[logic]
176#[requires(forall<d: Real> sat(a, d) ==> sat(b, d))]
177#[ensures(a.le(b))]
178pub fn le_complete(a: BoundModel, b: BoundModel) {
179 pearlite! {
180 match a {
181 BoundModel::Lt(ca) => {
182 let w = Real::from_int(ca) - half();
183 proof_assert!(sat(a, w));
184 proof_assert!(sat(b, w));
185 }
186 BoundModel::Le(ca) => {
187 proof_assert!(sat(a, Real::from_int(ca)));
188 proof_assert!(sat(b, Real::from_int(ca)));
189 }
190 BoundModel::Inf => match b {
191 BoundModel::Lt(cb) | BoundModel::Le(cb) => {
192 proof_assert!(sat(a, Real::from_int(cb + 1)));
193 proof_assert!(sat(b, Real::from_int(cb + 1)));
194 }
195 BoundModel::Inf => {}
196 },
197 }
198 }
199}
200
201#[logic(open)]
202fn half() -> Real {
203 Real::from_int(1) / Real::from_int(2)
204}
205
206#[logic]
208#[ensures(sat(BoundModel::Lt(c + 1), Real::from_int(c) + half()))]
209#[ensures(!sat(BoundModel::Le(c), Real::from_int(c) + half()))]
210pub fn strict_is_not_integer(c: Int) {}
211
212#[logic]
215#[requires(b != BoundModel::Inf)]
216#[ensures(forall<r: Real> !sat(b, r) == sat(b.neg(), -r))]
217pub fn neg_complement(b: BoundModel) {}
218
219#[logic]
223#[requires(BoundModel::in_range(c))]
224#[ensures(BoundModel::in_range(-c))]
225#[ensures(BoundModel::Lt(c).neg().representable() && BoundModel::Le(c).neg().representable())]
226pub fn neg_in_range(c: Int) {}
227
228#[logic]
230#[ensures(BoundModel::Lt(c).le(BoundModel::Le(c)) && !BoundModel::Le(c).le(BoundModel::Lt(c)))]
231#[ensures(BoundModel::Le(c).le(BoundModel::Lt(c + 1)) && !BoundModel::Lt(c + 1).le(BoundModel::Le(c)))]
232#[ensures(BoundModel::Le(c).le(BoundModel::Inf) && !BoundModel::Inf.le(BoundModel::Le(c)))]
233pub fn tightness_chain(c: Int) {}
234
235#[logic]
242#[ensures(a.le(a))]
243#[ensures(a.le(b) && b.le(c) ==> a.le(c))]
244pub fn le_trans(a: BoundModel, b: BoundModel, c: BoundModel) {}
245
246#[logic]
248#[ensures(a.le(b) || b.le(a))]
249#[ensures(a.le(b) && b.le(a) ==> a == b)]
250pub fn le_total(a: BoundModel, b: BoundModel) {}
251
252#[logic]
254#[ensures(a.add(b) == b.add(a))]
255#[ensures(a.add(BoundModel::Le(0)) == a)]
256#[ensures(a.add(BoundModel::Inf) == BoundModel::Inf)]
257pub fn add_comm_unit(a: BoundModel, b: BoundModel) {}
258
259#[logic]
261#[ensures(a.add(b).add(c) == a.add(b.add(c)))]
262pub fn add_assoc(a: BoundModel, b: BoundModel, c: BoundModel) {}
263
264#[logic]
266#[requires(a.le(b))]
267#[ensures(a.add(c).le(b.add(c)))]
268#[ensures(c.add(a).le(c.add(b)))]
269pub fn add_mono(a: BoundModel, b: BoundModel, c: BoundModel) {}
270
271#[logic]
274#[requires(!BoundModel::Le(0).le(b))]
275#[ensures(!sat(b, Real::from_int(0)))]
276pub fn below_le_zero_unsat(b: BoundModel) {}
277
278macro_rules! ord_by_deep_model {
282 ($t:ty) => {
283 impl PartialOrd for $t {
284 #[check(terminates)]
285 #[ensures(result == (*self).deep_model().partial_cmp_log((*other).deep_model()))]
286 fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
287 Some(self.cmp(other))
288 }
289 }
290
291 impl Ord for $t {
292 #[check(terminates)]
293 #[ensures(result == (*self).deep_model().cmp_log((*other).deep_model()))]
294 fn cmp(&self, other: &Self) -> core::cmp::Ordering {
295 self.0.cmp(&other.0)
296 }
297 }
298 };
299}
300
301#[derive(Copy, PartialEq, Eq, Debug)]
321#[cfg_attr(not(creusot), derive(Hash))]
324pub struct Bound(i32);
325
326ord_by_deep_model!(Bound);
327
328impl Clone for Bound {
332 #[check(terminates)]
333 #[ensures(result == *self)]
334 fn clone(&self) -> Bound {
335 *self
336 }
337}
338
339impl Invariant for Bound {
340 #[logic(open(self))]
341 fn invariant(self) -> bool {
342 pearlite! { (MIN_CODE@ <= self.0@ && self.0@ <= MAX_CODE@) || self.0 == INF }
343 }
344}
345
346impl DeepModel for Bound {
347 type DeepModelTy = Int;
348
349 #[logic(open(self))]
351 fn deep_model(self) -> Int {
352 pearlite! { self.0@ }
353 }
354}
355
356impl View for Bound {
357 type ViewTy = BoundModel;
358
359 #[logic(open(self))]
360 fn view(self) -> BoundModel {
361 pearlite! {
362 if self.0 == INF {
363 BoundModel::Inf
364 } else if self.0@.rem_euclid(2) == 0 {
365 BoundModel::Lt(self.0@.div_euclid(2))
366 } else {
367 BoundModel::Le(self.0@.div_euclid(2))
368 }
369 }
370 }
371}
372
373#[logic]
375#[requires(inv(b))]
376#[ensures(b@.representable())]
377pub fn view_representable(b: Bound) {}
378
379#[logic]
382#[requires(inv(a) && inv(b))]
383#[ensures((a@ == b@) == (a.deep_model() == b.deep_model()))]
384pub fn view_injective(a: Bound, b: Bound) {}
385
386#[logic]
389#[requires(inv(a) && inv(b))]
390#[ensures((a.deep_model() <= b.deep_model()) == a@.le(b@))]
391#[ensures((a.deep_model() < b.deep_model()) == (a@.le(b@) && a@ != b@))]
392pub fn order_refines(a: Bound, b: Bound) {}
393
394#[check(terminates)]
396#[bitwise_proof]
397#[ensures(result == 0i32 || result == 1i32)]
398#[ensures((result == 1i32) == (x & 1i32 == 1i32 || y & 1i32 == 1i32))]
399const fn weak_or(x: i32, y: i32) -> i32 {
400 (x | y) & 1
401}
402
403#[check(terminates)]
407#[bitwise_proof]
408#[ensures(x == (x >> 1i32) + (x >> 1i32) + (x & 1i32))]
409#[ensures(x & 1i32 == 0i32 || x & 1i32 == 1i32)]
410#[ensures(-0x4000_0000i32 <= (x >> 1i32) && (x >> 1i32) <= 0x3FFF_FFFFi32)]
411#[allow(unused_variables)]
412const fn bit_split(x: i32) {}
413
414#[check(terminates)]
417#[ensures((x & 1i32)@ == x@.rem_euclid(2))]
418#[ensures((x & 1i32 == 1i32) == (x@.rem_euclid(2) == 1))]
419#[ensures(x & 1i32 == 0i32 || x & 1i32 == 1i32)]
420#[ensures((x >> 1i32)@ == x@.div_euclid(2))]
421const fn low_bit(x: i32) {
422 bit_split(x);
423}
424
425impl Bound {
426 #[ensures(Bound::INFINITY@ == BoundModel::Inf)]
428 pub const INFINITY: Bound = Bound(INF);
429 #[ensures(Bound::LE_ZERO@ == BoundModel::Le(0))]
431 pub const LE_ZERO: Bound = Bound(1);
432 #[ensures(Bound::LT_ZERO@ == BoundModel::Lt(0))]
434 pub const LT_ZERO: Bound = Bound(0);
435
436 #[check(terminates)]
441 #[ensures(match result {
442 Ok(b) => BoundModel::in_range(c@) && b@ == BoundModel::Le(c@),
443 Err(e) => !BoundModel::in_range(c@) && e == Error::Range,
444 })]
445 pub const fn le(c: i32) -> Result<Bound, Error> {
446 if MIN_CONSTANT <= c && c <= MAX_CONSTANT {
447 Ok(Bound(2 * c + 1))
448 } else {
449 Err(Error::Range)
450 }
451 }
452
453 #[check(terminates)]
458 #[ensures(match result {
459 Ok(b) => BoundModel::in_range(c@) && b@ == BoundModel::Lt(c@),
460 Err(e) => !BoundModel::in_range(c@) && e == Error::Range,
461 })]
462 pub const fn lt(c: i32) -> Result<Bound, Error> {
463 if MIN_CONSTANT <= c && c <= MAX_CONSTANT {
464 Ok(Bound(2 * c))
465 } else {
466 Err(Error::Range)
467 }
468 }
469
470 #[check(terminates)]
472 #[ensures(result == (self@ == BoundModel::Inf))]
473 pub const fn is_infinite(self) -> bool {
474 self.0 == INF
475 }
476
477 #[check(terminates)]
481 #[ensures(result == match self@ { BoundModel::Lt(_) => true, _ => false })]
482 pub const fn is_strict(self) -> bool {
483 low_bit(self.0);
484 self.0 != INF && self.0 & 1 == 0
485 }
486
487 #[check(terminates)]
490 #[ensures(match self@ {
491 BoundModel::Lt(c) | BoundModel::Le(c) => match result { Some(r) => r@ == c, None => false },
492 BoundModel::Inf => result == None,
493 })]
494 pub const fn constant(self) -> Option<i32> {
495 if self.0 == INF {
496 None
497 } else {
498 low_bit(self.0);
499 Some(self.0 >> 1)
500 }
501 }
502
503 #[check(terminates)]
510 #[ensures(match result {
511 Ok(r) => self@.add_in_range(other@) && r@ == self@.add(other@),
512 Err(e) => !self@.add_in_range(other@) && e == Error::Range,
513 })]
514 pub const fn checked_add(self, other: Bound) -> Result<Bound, Error> {
515 if self.0 == INF || other.0 == INF {
516 return Ok(Bound::INFINITY);
517 }
518 low_bit(self.0);
519 low_bit(other.0);
520 let r = (self.0 + other.0) - weak_or(self.0, other.0);
523 if MIN_CODE <= r && r <= MAX_CODE {
524 Ok(Bound(r))
525 } else {
526 Err(Error::Range)
527 }
528 }
529}
530
531#[check(terminates)]
545#[requires(exists<c: Int> via@ == BoundModel::Le(c))]
546#[requires(via@.add_in_range(b@))]
547#[ensures(result@ == via@.add(b@))]
548#[inline]
549pub(crate) fn add_weak(b: Bound, via: Bound) -> Bound {
550 snapshot!(enc_view(via));
551 let fin = b.0 != INF;
552 proof_assert!(fin ==> { enc_view(b); enc_range(via@.add(b@)); true });
553 let sum = if fin { b.0 } else { 0 } + (via.0 - 1);
554 proof_assert!(fin ==> sum@ == enc(via@.add(b@)));
555 Bound(if fin { sum } else { INF })
556}
557
558#[check(terminates)]
561#[requires(dst@.len() == src@.len())]
562#[requires(exists<c: Int> via@ == BoundModel::Le(c))]
563#[requires(forall<l: Int> 0 <= l && l < src@.len() ==> via@.add_in_range(src@[l]@))]
564#[ensures((^dst)@.len() == dst@.len())]
565#[ensures(forall<l: Int> 0 <= l && l < dst@.len() ==> (^dst)@[l]@ == via@.add(src@[l]@))]
566#[inline]
567pub(crate) fn add_weak_row(dst: &mut [Bound], via: Bound, src: &[Bound]) {
568 let old = snapshot!(dst@);
569 let n = if dst.len() < src.len() {
571 dst.len()
572 } else {
573 src.len()
574 };
575 let mut l: usize = 0;
576 #[invariant(l@ <= n@ && n@ == old.len() && dst@.len() == old.len())]
577 #[invariant(forall<k: Int> 0 <= k && k < l@ ==> dst@[k]@ == via@.add(src@[k]@))]
578 #[variant(n@ - l@)]
579 while l < n {
580 dst[l] = add_weak(src[l], via);
581 l += 1;
582 }
583}
584
585#[logic(open)]
590pub fn enc(m: BoundModel) -> Int {
591 match m {
592 BoundModel::Lt(c) => 2 * c,
593 BoundModel::Le(c) => 2 * c + 1,
594 BoundModel::Inf => 0,
595 }
596}
597
598#[logic]
601#[requires(x != BoundModel::Inf && y != BoundModel::Inf)]
602#[ensures(x.le(y) == (enc(x) <= enc(y)))]
603#[ensures((enc(x) == enc(y)) == (x == y))]
604pub fn enc_order(x: BoundModel, y: BoundModel) {}
605
606#[logic]
608#[requires(inv(b) && b@ != BoundModel::Inf)]
609#[ensures(enc(b@) == b.deep_model())]
610pub fn enc_view(b: Bound) {}
611
612#[logic]
614#[requires(m != BoundModel::Inf)]
615#[ensures(m.representable() == (MIN_CODE@ <= enc(m) && enc(m) <= MAX_CODE@))]
616pub fn enc_range(m: BoundModel) {}
617
618#[logic(open)]
620pub fn relax_needed(cur: BoundModel, via: BoundModel, s: BoundModel) -> bool {
621 s != BoundModel::Inf && !cur.le(via.add(s))
622}
623
624#[logic(open)]
627pub fn relax_entry(cur: BoundModel, via: BoundModel, s: BoundModel, new: BoundModel) -> bool {
628 if relax_needed(cur, via, s) && via.add_in_range(s) {
629 new == via.add(s)
630 } else {
631 new == cur
632 }
633}
634
635#[check(terminates)]
648#[requires(dst@.len() == src@.len())]
649#[requires(dst@.len() <= u32::MAX@)]
650#[requires(via@ != BoundModel::Inf)]
651#[ensures((^dst)@.len() == dst@.len())]
652#[ensures(forall<l: Int> 0 <= l && l < dst@.len() ==>
653 relax_entry(dst@[l]@, via@, src@[l]@, (^dst)@[l]@))]
654#[ensures(result ==> exists<l: Int> 0 <= l && l < dst@.len()
655 && relax_needed(dst@[l]@, via@, src@[l]@) && !via@.add_in_range(src@[l]@))]
656#[ensures(!result ==> forall<l: Int> 0 <= l && l < dst@.len() ==>
657 !(relax_needed(dst@[l]@, via@, src@[l]@) && !via@.add_in_range(src@[l]@)))]
658#[inline]
659#[allow(clippy::manual_range_contains, clippy::assign_op_pattern)]
662pub(crate) fn relax_row(dst: &mut [Bound], via: Bound, src: &[Bound]) -> bool {
663 let old = snapshot!(dst@);
664 let n = if dst.len() < src.len() {
666 dst.len()
667 } else {
668 src.len()
669 };
670 let v = via.0;
671 low_bit(v);
672 snapshot!(enc_view(via));
673 let mut bad: u32 = 0;
674 let mut l: usize = 0;
675 #[invariant(l@ <= n@ && n@ == old.len() && dst@.len() == old.len())]
676 #[invariant(inv(dst))]
677 #[invariant(forall<k: Int> 0 <= k && k < l@ ==>
678 relax_entry(old[k]@, via@, src@[k]@, dst@[k]@))]
679 #[invariant(forall<k: Int> l@ <= k && k < n@ ==> dst@[k] == old[k])]
680 #[invariant(bad@ <= l@)]
681 #[invariant((bad@ > 0) == exists<k: Int> 0 <= k && k < l@
682 && relax_needed(old[k]@, via@, src@[k]@) && !via@.add_in_range(src@[k]@))]
683 #[variant(n@ - l@)]
684 while l < n {
685 let s = src[l].0;
686 let fin = s != INF;
687 let s2 = if fin { s } else { 0 };
688 low_bit(s2);
689 let sum = (v + s2) - weak_or(v, s2);
690 let cur = dst[l].0;
691 let tighter = fin & (sum < cur);
692 let ok = (MIN_CODE <= sum) & (sum <= MAX_CODE);
693 proof_assert!(fin ==> {
694 enc_view(src@[l@]); enc_view(dst@[l@]);
695 enc_order(via@.add(src@[l@]@), dst@[l@]@);
696 enc_range(via@.add(src@[l@]@));
697 sum@ == enc(via@.add(src@[l@]@))
698 && (tighter == relax_needed(dst@[l@]@, via@, src@[l@]@))
699 && (ok == via@.add_in_range(src@[l@]@))
700 });
701 bad = bad + (tighter & !ok) as u32;
702 let new = Bound(if tighter & ok { sum } else { cur });
703 proof_assert!(tighter && ok ==> {
704 enc_view(new); enc_order(new@, via@.add(src@[l@]@)); new@ == via@.add(src@[l@]@)
705 });
706 dst[l] = new;
707 l += 1;
708 }
709 bad != 0
710}
711
712#[derive(Clone, Copy, PartialEq, Eq, Debug)]
714#[cfg_attr(not(creusot), derive(Hash))]
715pub struct Clock(u32);
716
717ord_by_deep_model!(Clock);
718
719impl Invariant for Clock {
720 #[logic(open(self))]
721 fn invariant(self) -> bool {
722 pearlite! { self.0@ <= MAX_VARIABLES@ }
723 }
724}
725
726impl DeepModel for Clock {
727 type DeepModelTy = Int;
728
729 #[logic(open(self))]
730 fn deep_model(self) -> Int {
731 pearlite! { self.0@ }
732 }
733}
734
735impl View for Clock {
736 type ViewTy = Int;
737
738 #[logic(open(self))]
740 fn view(self) -> Int {
741 pearlite! { self.0@ }
742 }
743}
744
745impl Clock {
746 #[ensures(Clock::ZERO@ == 0)]
748 pub const ZERO: Clock = Clock(0);
749
750 #[check(terminates)]
752 #[ensures(match result {
753 Some(x) => index@ <= MAX_VARIABLES@ && x@ == index@,
754 None => index@ > MAX_VARIABLES@,
755 })]
756 pub const fn new(index: usize) -> Option<Clock> {
757 if index <= MAX_VARIABLES {
758 Some(Clock(index as u32))
759 } else {
760 None
761 }
762 }
763
764 #[check(terminates)]
766 #[ensures(result@ == self@)]
767 pub const fn index(self) -> usize {
768 self.0 as usize
769 }
770}
771
772#[derive(Clone, Copy, PartialEq, Eq, Debug)]
778#[cfg_attr(not(creusot), derive(Hash))]
779pub struct Variable(u32);
780
781ord_by_deep_model!(Variable);
782
783impl Invariant for Variable {
784 #[logic(open(self))]
785 fn invariant(self) -> bool {
786 pearlite! { 1 <= self.0@ && self.0@ <= MAX_VARIABLES@ }
787 }
788}
789
790impl DeepModel for Variable {
791 type DeepModelTy = Int;
792
793 #[logic(open(self))]
794 fn deep_model(self) -> Int {
795 pearlite! { self.0@ }
796 }
797}
798
799impl View for Variable {
800 type ViewTy = Int;
801
802 #[logic(open(self))]
804 fn view(self) -> Int {
805 pearlite! { self.0@ }
806 }
807}
808
809impl Variable {
810 #[check(terminates)]
813 #[ensures(match result {
814 Some(x) => 1 <= index@ && index@ <= MAX_VARIABLES@ && x@ == index@,
815 None => index@ == 0 || index@ > MAX_VARIABLES@,
816 })]
817 pub const fn new(index: usize) -> Option<Variable> {
818 if 1 <= index && index <= MAX_VARIABLES {
819 Some(Variable(index as u32))
820 } else {
821 None
822 }
823 }
824
825 #[check(terminates)]
827 #[ensures(result@ == self@ && 1 <= result@)]
828 pub const fn index(self) -> usize {
829 self.0 as usize
830 }
831}
832
833impl From<Variable> for Clock {
834 #[check(terminates)]
836 #[ensures(result@ == x@)]
837 fn from(x: Variable) -> Clock {
838 Clock(x.0)
839 }
840}
841
842#[logic]
844#[requires(inv(x))]
845#[ensures(x@ != Clock::ZERO@)]
846pub fn variable_not_zero(x: Variable) {}
847
848#[derive(Clone, Copy, PartialEq, Eq, Debug)]
870#[cfg_attr(not(creusot), derive(Hash))]
871pub struct Constraint {
872 i: Clock,
873 j: Clock,
874 bound: Bound,
875}
876
877impl DeepModel for Constraint {
878 type DeepModelTy = (Int, Int, Int);
879
880 #[logic(open(self))]
881 fn deep_model(self) -> (Int, Int, Int) {
882 pearlite! { (self.i.deep_model(), self.j.deep_model(), self.bound.deep_model()) }
883 }
884}
885
886impl View for Constraint {
887 type ViewTy = (Int, Int, BoundModel);
888
889 #[logic(open(self))]
891 fn view(self) -> (Int, Int, BoundModel) {
892 pearlite! { (self.i@, self.j@, self.bound@) }
893 }
894}
895
896impl Constraint {
897 #[check(terminates)]
903 #[ensures(result@ == (i@, j@, bound@))]
904 pub const fn new(i: Clock, j: Clock, bound: Bound) -> Constraint {
905 Constraint { i, j, bound }
906 }
907
908 #[check(terminates)]
912 #[ensures(match result {
913 Ok(g) => BoundModel::in_range(c@) && g@ == (i@, j@, BoundModel::Le(c@)),
914 Err(e) => !BoundModel::in_range(c@) && e == Error::Range,
915 })]
916 pub const fn le(i: Clock, j: Clock, c: i32) -> Result<Constraint, Error> {
917 match Bound::le(c) {
918 Ok(bound) => Ok(Constraint { i, j, bound }),
919 Err(e) => Err(e),
920 }
921 }
922
923 #[check(terminates)]
927 #[ensures(match result {
928 Ok(g) => BoundModel::in_range(c@) && g@ == (i@, j@, BoundModel::Lt(c@)),
929 Err(e) => !BoundModel::in_range(c@) && e == Error::Range,
930 })]
931 pub const fn lt(i: Clock, j: Clock, c: i32) -> Result<Constraint, Error> {
932 match Bound::lt(c) {
933 Ok(bound) => Ok(Constraint { i, j, bound }),
934 Err(e) => Err(e),
935 }
936 }
937
938 #[check(terminates)]
940 #[ensures(result@ == self@.0)]
941 pub const fn i(self) -> Clock {
942 self.i
943 }
944
945 #[check(terminates)]
947 #[ensures(result@ == self@.1)]
948 pub const fn j(self) -> Clock {
949 self.j
950 }
951
952 #[check(terminates)]
955 #[ensures(result@ == self@.2)]
956 pub const fn bound(self) -> Bound {
957 self.bound
958 }
959}
960
961#[cfg(test)]
962mod tests {
963 use super::*;
964
965 fn le(c: i32) -> Bound {
966 Bound::le(c).unwrap()
967 }
968 fn lt(c: i32) -> Bound {
969 Bound::lt(c).unwrap()
970 }
971
972 #[test]
973 fn codes() {
974 assert_eq!(lt(3).0, 6);
975 assert_eq!(le(3).0, 7);
976 assert_eq!(lt(-3).0, -6);
977 assert_eq!(le(-3).0, -5);
978 assert_eq!(Bound::INFINITY.0, 0x7FFF_FFFE);
979 assert_eq!(Bound::LE_ZERO, le(0));
980 assert_eq!(Bound::LT_ZERO, lt(0));
981 }
982
983 #[test]
984 fn literal_constants() {
985 const {
986 assert!(MAX_CONSTANT == (1 << 29) - 1);
987 assert!(MIN_CONSTANT == -MAX_CONSTANT);
988 assert!(MIN_CODE == 2 * MIN_CONSTANT);
989 assert!(MAX_CODE == 2 * MAX_CONSTANT + 1);
990 assert!(MAX_VARIABLES == 46_339);
991 assert!(((MAX_VARIABLES + 1) as u64).pow(2) < 1 << 31);
993 }
994 }
995
996 #[test]
997 fn relax_row_cases() {
998 let inf = Bound::INFINITY;
999 let mut dst = [le(5), inf, lt(3), inf, le(0), le(1)];
1001 let src = [le(1), le(4), le(1), inf, lt(-2), le(-1)];
1002 assert!(!relax_row(&mut dst, le(2), &src));
1003 assert_eq!(dst, [le(3), le(6), lt(3), inf, lt(0), le(1)]);
1006
1007 let big = le(MAX_CONSTANT);
1010 let mut dst = [inf, le(0), lt(-MAX_CONSTANT)];
1011 let src = [big, big, lt(MIN_CONSTANT)];
1012 assert!(relax_row(&mut dst, le(1), &src));
1013 assert_eq!(dst[0], inf);
1014 assert_eq!(dst[1], le(0));
1015 let mut dst = [le(0)];
1016 assert!(!relax_row(&mut dst, le(1), &[big]));
1017 assert_eq!(dst, [le(0)]);
1018 let mut dst = [lt(MIN_CONSTANT)];
1020 assert!(relax_row(&mut dst, le(-1), &[le(MIN_CONSTANT)]));
1021 assert_eq!(dst, [lt(MIN_CONSTANT)]);
1022 }
1023
1024 #[test]
1025 fn codes_at_limits() {
1026 assert_eq!(le(MAX_CONSTANT).0, (1 << 30) - 1);
1027 assert_eq!(lt(MAX_CONSTANT).0, (1 << 30) - 2);
1028 assert_eq!(lt(MIN_CONSTANT).0, -(1 << 30) + 2);
1029 assert_eq!(le(MIN_CONSTANT).0, -(1 << 30) + 3);
1030 assert_eq!(MIN_CODE, lt(MIN_CONSTANT).0);
1031 assert_eq!(MAX_CODE, le(MAX_CONSTANT).0);
1032 const {
1035 assert!(MAX_CODE < INF - 1);
1036 assert!(2 * (MIN_CODE as i64) > i32::MIN as i64);
1037 assert!(2 * (MAX_CODE as i64) < i32::MAX as i64);
1038 }
1039 }
1040}