elinvar-engine 0.1.2

The engine of Elinvar, a relational model finder: models, translation, the SAT solver, and verdicts
Documentation
//! What a model written in Rust needs, in one import (R23):
//! `use elinvar_engine::prelude::*;`.
//!
//! ```
//! use elinvar_engine::prelude::*;
//!
//! let mut m = Model::new();
//! let item = m.set("Item");
//! let active = m.changing_subset("Active", &item);
//! m.fact(active.no());
//! let _activate = m.action("activate", [&item], |s, [i]| {
//!     s.when(i.is_in(&active).not());
//!     s.add(&active, i);
//! });
//! let report = m
//!     .check(&always(active.is_in(&item)))
//!     .named("ActiveAreItems")
//!     .scope(2)
//!     .steps(3)
//!     .report();
//! assert_eq!(report.verdict, Some(Verdict::Holds));
//! ```

pub use crate::logic::{Decl, Expr, Formula, Multiplicity, Quantifier, Variable};
pub use crate::{Action, Command, Model, Options, Report, Step, Verdict};

const NAMES: [&str; 6] = ["x", "y", "z", "u", "v", "w"];

fn quantified<const N: usize>(
    q: Quantifier,
    sets: [&Expr; N],
    body: impl FnOnce([Expr; N]) -> Formula,
) -> Formula {
    let vars: [Variable; N] = std::array::from_fn(|i| {
        Variable::new(
            NAMES
                .get(i)
                .map_or_else(|| format!("x{i}"), |n| (*n).to_string()),
        )
    });
    let exprs: [Expr; N] = std::array::from_fn(|i| Expr::variable(&vars[i]));
    let decls = vars
        .iter()
        .zip(sets)
        .map(|(v, s)| Decl::new(v, s))
        .collect();
    Formula::quantified(q, decls, &body(exprs))
}

/// `all x: a, y: b | body(x, y)`
pub fn all<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
    quantified(Quantifier::All, sets, body)
}

/// `some x: a, y: b | body(x, y)`
pub fn some<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
    quantified(Quantifier::Some, sets, body)
}

/// `no x: a, y: b | body(x, y)`
pub fn no<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
    quantified(Quantifier::No, sets, body)
}

/// `one x: a, y: b | body(x, y)`
pub fn one<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
    quantified(Quantifier::One, sets, body)
}

/// `lone x: a, y: b | body(x, y)`
pub fn lone<const N: usize>(sets: [&Expr; N], body: impl FnOnce([Expr; N]) -> Formula) -> Formula {
    quantified(Quantifier::Lone, sets, body)
}

/// `always f`
pub fn always(f: Formula) -> Formula {
    f.always()
}

/// `eventually f`
pub fn eventually(f: Formula) -> Formula {
    f.eventually()
}