Skip to main content

clock_dbm/
lib.rs

1//! # clock-dbm
2//!
3//! Difference-bound matrices (DBMs) for representing zones of timed automata.
4//! Clocks range over the **nonnegative real numbers**; constraints have integer
5//! bounds, including strict inequalities.
6//!
7//! A [`Dbm`] stores a zone in canonical form (or represents the empty zone).
8//! Its operations include guard insertion, time elapse ([`Dbm::future`]) and
9//! its backward counterpart ([`Dbm::past`]), resets, intersection,
10//! inclusion, and maximal-constant extrapolation. [`SharedDbm`] provides cheap
11//! immutable snapshots and copy-on-write updates for single-threaded exploration.
12//!
13//! ## Quick start
14//!
15//! This example describes two clocks, `x` and `y`, and then branches from the
16//! original zone without changing the saved snapshot.
17//!
18//! ```rust
19//! use clock_dbm::{constraint, Clock, Dbm, SharedDbm, Variable, Workspace};
20//!
21//! # fn main() -> Result<(), clock_dbm::Error> {
22//! let x = Clock::new(1).unwrap();
23//! let y = Clock::new(2).unwrap();
24//! let mut ws = Workspace::new();
25//!
26//! // 2 <= x <= 10 and y - x < 3.
27//! let zone = Dbm::try_from_constraints(2, &[
28//!     constraint!(x >= 2)?,
29//!     constraint!(x <= 10)?,
30//!     constraint!(y - x < 3)?,
31//! ], &mut ws)?;
32//! assert!(!zone.is_empty());
33//! assert!(zone.entails(&constraint!(x <= 10)?)?);
34//!
35//! // All clocks elapse together; then y is reset to zero.
36//! let reset = zone.clone().future().try_reset_zero(Variable::new(2).unwrap())?;
37//! assert!(reset.entails(&constraint!(y <= 0)?)?);
38//!
39//! // Time predecessors: every valuation that can reach the zone by waiting.
40//! let before = zone.clone().past();
41//! assert!(zone.is_subset_of(&before)?);
42//! assert!(before.entails(&constraint!(x <= 10)?)?);
43//!
44//! // A shared branch leaves the original zone unchanged.
45//! let snapshot = SharedDbm::from(zone);
46//! let branch = snapshot.clone().try_constrain(constraint!(x < 5)?)?;
47//! assert!(!branch.is_empty());
48//! assert!(branch.is_subset_of(&snapshot)?);
49//! # Ok(())
50//! # }
51//! ```
52//!
53//! ## Mathematical model
54//!
55//! A zone is a set of valuations of `n` clocks `x₁, …, xₙ` over the
56//! **nonnegative real numbers**. A DBM adds a reference clock `x₀ = 0`,
57//! represented by [`Clock::ZERO`], so its matrix dimension is `d = n + 1`.
58//! The case `n = 0` is supported.
59//!
60//! Each matrix entry `D[i][j]` bounds the difference `xᵢ - xⱼ` by one
61//! of `< c`, `<= c`, or infinity (no restriction). The integers `c` are
62//! represented as [`Bound`] values. A valuation belongs to the zone
63//! precisely when it satisfies **every** entry:
64//!
65//! ```text
66//! Z(D) = { v ∈ (ℝ≥0)ⁿ | for all i,j ∈ {0,…,n},
67//!                      v(xᵢ) - v(xⱼ) satisfies D[i][j] },
68//! with v(x₀) = 0.
69//! ```
70//!
71//! [`Constraint::le`] and [`Constraint::lt`] construct difference guards.
72//! For example, `x >= 2` is `Constraint::le(Clock::ZERO, x, -2)`, while
73//! `x - y < 3` is `Constraint::lt(x, y, 3)`. The [`constraint!`] macro
74//! provides the shorter notations `constraint!(x >= 2)` and
75//! `constraint!(x - y < 3)` for clock identifiers; single-clock forms with
76//! `<`, `<=`, `>` and `>=` use the reference clock implicitly.
77//! Use [`Clock`] for constraints and [`Variable`] for reset/free operations,
78//! which cannot target the reference clock.
79//!
80//! ## Canonical zones
81//!
82//! Every nonempty public [`Dbm`] satisfies the canonicality conditions:
83//!
84//! - **C1 — Representability:** every stored bound is finite and within
85//!   [`MIN_CONSTANT`]–[`MAX_CONSTANT`], or infinity.
86//! - **C2 — Diagonal:** `D[i][i] = Le(0)` for every clock.
87//! - **C3 — Nonnegativity:** `D[0][i]` is at least as tight as `Le(0)`,
88//!   ensuring that all actual clocks are nonnegative.
89//! - **C4 — Closure:** `D[i][j]` is at least as tight as
90//!   `D[i][k] + D[k][j]` for all `i, j, k`.
91//! - **C5 — Nonemptiness:** the represented zone is nonempty.
92//!
93//! Conditions C2–C4 imply C5. Thus each entry is the tightest implied
94//! difference bound. Two canonical DBMs with the same clock count denote
95//! the same zone exactly when their entries coincide; [`Dbm`] equality
96//! and hashing follow this semantic equality.
97//!
98//! The empty zone is represented separately and **remembers its clock count**:
99//! `Dbm::empty(1)? != Dbm::empty(2)?`. Every successful update returns
100//! a canonical or empty zone; no partially updated matrix becomes public.
101//!
102//! ## Results and errors
103//!
104//! Operations taking ownership of a zone return a new canonical [`Dbm`] or
105//! [`SharedDbm`]. Mathematically inconsistent constraints return **`Ok` with
106//! an empty zone**, detectable with [`Dbm::is_empty`]. They do not return an
107//! error. [`Error::Range`] instead means an exact finite bound needed by the
108//! computation lies outside the range from [`MIN_CONSTANT`] to
109//! [`MAX_CONSTANT`]; it must not be interpreted as emptiness. Bad clock indices and incompatible dimensions
110//! are also errors. [`Workspace`] reuses scratch buffers but never changes
111//! the result.
112//!
113//! ```rust
114//! use clock_dbm::{Bound, Clock, Constraint, Dbm, Error, MAX_CONSTANT};
115//!
116//! # fn main() -> Result<(), Error> {
117//! let x = Clock::new(1).unwrap();
118//! let impossible = Dbm::universe(1)?
119//!     .try_constrain(Constraint::lt(x, Clock::ZERO, 0)?)?;
120//! assert!(impossible.is_empty()); // x >= 0 and x < 0
121//! assert_eq!(Bound::le(MAX_CONSTANT + 1), Err(Error::Range));
122//! # Ok(())
123//! # }
124//! ```
125//!
126//! ## Supported abstractions
127//!
128//! This release provides maximal-constant extrapolation (ExtraM). More
129//! advanced abstractions, including separate lower/upper (LU) bounds and
130//! related variants, are left for future development.
131//!
132//! ## Developer references
133//!
134//! The public API pages describe each operation's behavior, errors, and
135//! complexity without requiring other documents. For implementation and
136//! verification work, the [developer reference index](guide) provides the
137//! algorithm guide, proof-status report, and pinned toolchain information.
138//!
139//! The executable DBM operations have been checked with Creusot against
140//! their stated zone semantics. The shared layer relies on trusted `Rc`
141//! specifications; hashing is outside the machine-checked subset.
142
143// Ghost-only bindings (logic imports) are unused once contracts are erased.
144#![cfg_attr(not(creusot), allow(unused_imports, unused_variables))]
145// `#[derive(DeepModel)]` generates undocumented model types under Creusot.
146#![cfg_attr(creusot, allow(missing_docs))]
147// The trusted `Rc` specifications of `shared` name `Rc<T, A>`'s allocator.
148#![cfg_attr(creusot, feature(allocator_api))]
149
150pub mod bounds;
151pub mod dbm;
152pub mod error;
153pub mod guide;
154mod macros;
155pub mod ops;
156pub mod shared;
157
158// The exact reference oracle, shared by the differential integration tests and
159// the differential unit tests of `dbm` and `ops::constraints`.
160#[cfg(test)]
161#[allow(dead_code, unused_imports)]
162#[path = "../tests/support/oracle/mod.rs"]
163mod oracle;
164
165pub use bounds::{Bound, Clock, Constraint, MAX_CONSTANT, MIN_CONSTANT, Variable};
166pub use dbm::{Dbm, MAX_VARIABLES, Workspace};
167pub use error::Error;
168pub use ops::extra_m::MaxBounds;
169pub use shared::SharedDbm;