Skip to main content

elinvar_engine/
prelude.rs

1//! What a model written in Rust needs, in one import (R23):
2//! `use elinvar_engine::prelude::*;`.
3//!
4//! ```
5//! use elinvar_engine::prelude::*;
6//!
7//! let mut m = Model::new();
8//! let item = m.set("Item");
9//! let active = m.changing_subset("Active", &item);
10//! m.fact(active.no());
11//! let _activate = m.action("activate", [&item], |s, [i]| {
12//!     s.when(i.is_in(&active).not());
13//!     s.add(&active, i);
14//! });
15//! let report = m
16//!     .check(&always(active.is_in(&item)))
17//!     .named("ActiveAreItems")
18//!     .scope(2)
19//!     .steps(3)
20//!     .report();
21//! assert_eq!(report.verdict, Some(Verdict::Holds));
22//! ```
23
24pub use crate::logic::{Decl, Expr, Formula, Multiplicity, Quantifier, Variable};
25pub use crate::{Action, Command, Model, Options, Report, Step, Verdict};
26
27const NAMES: [&str; 6] = ["x", "y", "z", "u", "v", "w"];
28
29fn quantified<const N: usize>(
30    q: Quantifier,
31    sets: [&Expr; N],
32    body: impl FnOnce([Expr; N]) -> Formula,
33) -> Formula {
34    let vars: [Variable; N] = std::array::from_fn(|i| {
35        Variable::new(
36            NAMES
37                .get(i)
38                .map_or_else(|| format!("x{i}"), |n| (*n).to_string()),
39        )
40    });
41    let exprs: [Expr; N] = std::array::from_fn(|i| Expr::variable(&vars[i]));
42    let decls = vars
43        .iter()
44        .zip(sets)
45        .map(|(v, s)| Decl::new(v, s))
46        .collect();
47    Formula::quantified(q, decls, &body(exprs))
48}
49
50/// `all x: a, y: b | body(x, y)`
51pub fn all<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
52    quantified(Quantifier::All, sets, body)
53}
54
55/// `some x: a, y: b | body(x, y)`
56pub fn some<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
57    quantified(Quantifier::Some, sets, body)
58}
59
60/// `no x: a, y: b | body(x, y)`
61pub fn no<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
62    quantified(Quantifier::No, sets, body)
63}
64
65/// `one x: a, y: b | body(x, y)`
66pub fn one<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
67    quantified(Quantifier::One, sets, body)
68}
69
70/// `lone x: a, y: b | body(x, y)`
71pub fn lone<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
72    quantified(Quantifier::Lone, sets, body)
73}
74
75/// `always f`
76pub fn always(f: Formula) -> Formula {
77    f.always()
78}
79
80/// `eventually f`
81pub fn eventually(f: Formula) -> Formula {
82    f.eventually()
83}