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