clock_dbm/macros.rs
1//! A concise syntax for constructing difference constraints.
2
3/// A clock constraint written as a single-clock bound such as `x >= 2`, or
4/// as a clock difference such as `y - x < 3`.
5///
6/// A constraint restricts the difference `xᵢ - xⱼ` of two clocks by an
7/// integer `c`, either strictly (`< c`) or non-strictly (`<= c`). A plain
8/// bound on one clock `x` is the difference `x - x₀`, where `x₀` is the
9/// reference clock [`Clock::ZERO`], whose value is always `0`. The macro
10/// supplies `x₀` itself for the single-clock forms:
11///
12/// | Macro form | Meaning | Result |
13/// |----------------------------|--------------|-------------------------------------|
14/// | `constraint!(x < c)` | `x < c` | `Constraint::lt(x, Clock::ZERO, c)` |
15/// | `constraint!(x <= c)` | `x <= c` | `Constraint::le(x, Clock::ZERO, c)` |
16/// | `constraint!(x > c)` | `x > c` | `Constraint::lt(Clock::ZERO, x, -c)`|
17/// | `constraint!(x >= c)` | `x >= c` | `Constraint::le(Clock::ZERO, x, -c)`|
18/// | `constraint!(i - j < c)` | `i - j < c` | `Constraint::lt(i, j, c)` |
19/// | `constraint!(i - j <= c)` | `i - j <= c` | `Constraint::le(i, j, c)` |
20///
21/// Lower bounds are rewritten as upper bounds on `x₀ - x`: `x >= c` holds
22/// exactly when `0 - x <= -c`, and `x > c` exactly when `0 - x < -c`.
23///
24/// - `x`, `i` and `j` are identifiers bound to a [`Clock`] or a
25/// [`Variable`]. A [`Variable`] is converted with `Clock::from`.
26/// - `c` is any `i32` expression. Every operand is evaluated exactly once.
27///
28/// The macro returns `Result<Constraint, Error>`, delegating to
29/// [`Constraint::lt`] and [`Constraint::le`]. For lower bounds, it first
30/// checks that negating the constant is representable.
31///
32/// # Errors
33///
34/// [`Error::Range`] if the stored constant is outside the range from
35/// [`MIN_CONSTANT`] to [`MAX_CONSTANT`]. The stored constant is `c` for the
36/// `<`, `<=` and difference forms, and `-c` for `>` and `>=`. In particular,
37/// `x > i32::MIN` and `x >= i32::MIN` return [`Error::Range`] because `-c`
38/// is not an `i32`; the negation never panics or wraps.
39///
40/// # Unsupported forms
41///
42/// `>` and `>=` between two clocks (`y - x >= 3`), other clock expressions,
43/// and combinations of constraints are rejected at compile time:
44///
45/// ```compile_fail
46/// use clock_dbm::{Clock, constraint};
47///
48/// let x = Clock::new(1).unwrap();
49/// let y = Clock::new(2).unwrap();
50/// let _ = constraint!(y - x >= 3);
51/// ```
52///
53/// # Examples
54///
55/// ```
56/// use clock_dbm::{Clock, Constraint, Variable, constraint};
57///
58/// let x = Clock::new(1).unwrap();
59/// let y = Variable::new(2).unwrap();
60///
61/// assert_eq!(constraint!(x <= 10)?, Constraint::le(x, Clock::ZERO, 10)?);
62/// assert_eq!(constraint!(x < 5)?, Constraint::lt(x, Clock::ZERO, 5)?);
63/// assert_eq!(constraint!(x >= 2)?, Constraint::le(Clock::ZERO, x, -2)?);
64/// assert_eq!(constraint!(x > 2)?, Constraint::lt(Clock::ZERO, x, -2)?);
65///
66/// let guard = constraint!(y - x < 3)?;
67/// assert_eq!(guard, Constraint::lt(y.into(), x, 3)?);
68///
69/// let k = 5;
70/// let invariant = constraint!(y <= 2 * k)?;
71/// assert_eq!(invariant, Constraint::le(y.into(), Clock::ZERO, 10)?);
72/// # Ok::<(), clock_dbm::Error>(())
73/// ```
74///
75/// An unrepresentable bound is an error, as for the constructors:
76///
77/// ```
78/// use clock_dbm::{Clock, Error, MAX_CONSTANT, constraint};
79///
80/// let x = Clock::new(1).unwrap();
81/// assert_eq!(constraint!(x <= MAX_CONSTANT + 1), Err(Error::Range));
82/// assert_eq!(constraint!(x >= i32::MIN), Err(Error::Range));
83/// ```
84///
85/// [`Constraint::lt`]: crate::Constraint::lt
86/// [`Constraint::le`]: crate::Constraint::le
87/// [`Clock`]: crate::Clock
88/// [`Clock::ZERO`]: crate::Clock::ZERO
89/// [`Variable`]: crate::Variable
90/// [`Error::Range`]: crate::Error::Range
91/// [`MIN_CONSTANT`]: crate::MIN_CONSTANT
92/// [`MAX_CONSTANT`]: crate::MAX_CONSTANT
93#[macro_export]
94macro_rules! constraint {
95 ($i:ident - $j:ident < $c:expr) => {
96 $crate::Constraint::lt($crate::Clock::from($i), $crate::Clock::from($j), $c)
97 };
98 ($i:ident - $j:ident <= $c:expr) => {
99 $crate::Constraint::le($crate::Clock::from($i), $crate::Clock::from($j), $c)
100 };
101 ($x:ident < $c:expr) => {
102 $crate::Constraint::lt($crate::Clock::from($x), $crate::Clock::ZERO, $c)
103 };
104 ($x:ident <= $c:expr) => {
105 $crate::Constraint::le($crate::Clock::from($x), $crate::Clock::ZERO, $c)
106 };
107 // `x > c` is `0 - x < -c`; `-c` overflows only for `i32::MIN`.
108 ($x:ident > $c:expr) => {
109 match ::core::primitive::i32::checked_neg($c) {
110 ::core::option::Option::Some(c) => {
111 $crate::Constraint::lt($crate::Clock::ZERO, $crate::Clock::from($x), c)
112 }
113 ::core::option::Option::None => ::core::result::Result::Err($crate::Error::Range),
114 }
115 };
116 // `x >= c` is `0 - x <= -c`.
117 ($x:ident >= $c:expr) => {
118 match ::core::primitive::i32::checked_neg($c) {
119 ::core::option::Option::Some(c) => {
120 $crate::Constraint::le($crate::Clock::ZERO, $crate::Clock::from($x), c)
121 }
122 ::core::option::Option::None => ::core::result::Result::Err($crate::Error::Range),
123 }
124 };
125}