conjure_core/rule_engine/mod.rs
1pub use linkme::distributed_slice;
2
3/// This procedural macro registers a decorated function with `conjure_rules`' global registry, and
4/// adds the rule to one or more `RuleSet`'s.
5///
6/// It may be used in any downstream crate.
7/// For more information on linker magic, see the [`linkme`](https://docs.rs/linkme/latest/linkme/) crate.
8///
9/// **IMPORTANT**: Since the resulting rule may not be explicitly referenced, it may be removed by the compiler's dead code elimination.
10/// To prevent this, you must ensure that either:
11/// 1. codegen-units is set to 1, i.e. in Cargo.toml:
12/// ```toml
13/// [profile.release]
14/// codegen-units = 1
15/// ```
16/// 2. The function is included somewhere else in the code
17///
18/// <hr>
19///
20/// Functions must have the signature `fn(&Expr) -> ApplicationResult`.
21/// The created rule will have the same name as the function.
22///
23/// Intermediary static variables are created to allow for the decentralized registry, with the prefix `CONJURE_GEN_`.
24/// Please ensure that other variable names in the same scope do not conflict with these.
25///
26/// This macro must decorate a function with the given signature.
27/// As arguments, it excepts a tuple of 2-tuples in the format:
28/// `((<RuleSet name>, <Priority in RuleSet>), ...)`
29///
30/// <hr>
31///
32/// For example:
33/// ```rust
34/// use conjure_core::ast::Expression;
35/// use conjure_core::ast::SymbolTable;
36/// use conjure_core::rule_engine::{ApplicationError, ApplicationResult, Reduction};
37/// use conjure_core::rule_engine::register_rule;
38///
39/// #[register_rule(("RuleSetName", 10))]
40/// fn identity(expr: &Expression, symbols: &SymbolTable) -> ApplicationResult {
41/// Ok(Reduction::pure(expr.clone()))
42/// }
43/// ```
44pub use conjure_rule_macros::register_rule;
45
46/// This procedural macro registers a rule set with the global registry.
47/// It may be used in any downstream crate.
48///
49/// For more information on linker magic, see the [`linkme`](https://docs.rs/linkme/latest/linkme/) crate.
50///
51/// This macro uses the following syntax:
52///
53/// ```text
54/// register_rule_set!(<RuleSet name>, (<DependencyRuleSet1>, <DependencyRuleSet2>, ...), <SolverFamily>);
55/// ```
56///
57/// # Example
58///
59/// Register a rule set with no dependencies:
60///
61/// ```rust
62/// use conjure_core::rule_engine::register_rule_set;
63/// register_rule_set!("MyRuleSet");
64/// ```
65///
66/// Register a rule set with dependencies:
67///
68/// ```rust
69/// use conjure_core::rule_engine::register_rule_set;
70/// register_rule_set!("MyRuleSet", ("DependencyRuleSet", "AnotherRuleSet"));
71/// ```
72///
73/// Register a rule set for a specific solver family or families:
74///
75/// ```rust
76/// use conjure_core::rule_engine::register_rule_set;
77/// use conjure_core::solver::SolverFamily;
78/// register_rule_set!("MyRuleSet", (), SolverFamily::Minion);
79/// register_rule_set!("AnotherRuleSet", (), (SolverFamily::Minion, SolverFamily::SAT));
80/// ```
81#[doc(inline)]
82pub use conjure_rule_macros::register_rule_set;
83pub use resolve_rules::{get_rules, get_rules_grouped, resolve_rule_sets, RuleData};
84pub use rewrite_naive::rewrite_naive;
85pub use rewriter_common::RewriteError;
86pub use rule::{ApplicationError, ApplicationResult, Reduction, Rule};
87pub use rule_set::RuleSet;
88mod submodel_zipper;
89
90use crate::solver::SolverFamily;
91
92mod resolve_rules;
93mod rewrite_naive;
94mod rewriter_common;
95mod rule;
96mod rule_set;
97
98#[doc(hidden)]
99#[distributed_slice]
100pub static RULES_DISTRIBUTED_SLICE: [Rule<'static>];
101
102#[doc(hidden)]
103#[distributed_slice]
104pub static RULE_SETS_DISTRIBUTED_SLICE: [RuleSet<'static>];
105
106pub mod _dependencies {
107 pub use linkme;
108 pub use linkme::distributed_slice;
109}
110
111/// Returns a copied `Vec` of all rules registered with the `register_rule` macro.
112///
113/// Rules are not guaranteed to be in any particular order.
114///
115/// # Example
116/// ```rust
117/// # use conjure_core::rule_engine::{ApplicationResult, Reduction, get_all_rules};
118/// # use conjure_core::ast::Expression;
119/// # use conjure_core::ast::SymbolTable;
120/// # use conjure_core::rule_engine::register_rule;
121///
122/// #[register_rule]
123/// fn identity(expr: &Expression, symbols: &SymbolTable) -> ApplicationResult {
124/// Ok(Reduction::pure(expr.clone()))
125/// }
126///
127/// fn main() {
128/// println!("Rules: {:?}", get_all_rules());
129/// }
130/// ```
131///
132/// This will print (if no other rules are registered):
133/// ```text
134/// Rules: [Rule { name: "identity", application: MEM }]
135/// ```
136/// Where `MEM` is the memory address of the `identity` function.
137pub fn get_all_rules() -> Vec<&'static Rule<'static>> {
138 RULES_DISTRIBUTED_SLICE.iter().collect()
139}
140
141/// Get a rule by name.
142/// Returns the rule with the given name or None if it doesn't exist.
143///
144/// # Example
145/// ```rust
146/// use conjure_core::rule_engine::register_rule;
147/// use conjure_core::rule_engine::{Rule, ApplicationResult, Reduction, get_rule_by_name};
148/// use conjure_core::ast::Expression;
149/// use conjure_core::ast::SymbolTable;
150///
151/// #[register_rule]
152/// fn identity(expr: &Expression, symbols: &SymbolTable) -> ApplicationResult {
153/// Ok(Reduction::pure(expr.clone()))
154/// }
155///
156/// fn main() {
157/// println!("Rule: {:?}", get_rule_by_name("identity"));
158/// }
159/// ```
160///
161/// This will print:
162/// ```text
163/// Rule: Some(Rule { name: "identity", application: MEM })
164/// ```
165pub fn get_rule_by_name(name: &str) -> Option<&'static Rule<'static>> {
166 get_all_rules()
167 .iter()
168 .find(|rule| rule.name == name)
169 .cloned()
170}
171
172/// Get all rule sets
173/// Returns a `Vec` of static references to all rule sets registered with the `register_rule_set` macro.
174/// Rule sets are not guaranteed to be in any particular order.
175///
176/// # Example
177/// ```rust
178/// use conjure_core::rule_engine::register_rule_set;
179/// use conjure_core::rule_engine::get_all_rule_sets;
180///
181/// register_rule_set!("MyRuleSet", ("AnotherRuleSet"));
182/// register_rule_set!("AnotherRuleSet", ());
183///
184/// println!("Rule sets: {:?}", get_all_rule_sets());
185/// ```
186///
187/// This will print (if no other rule sets are registered):
188/// ```text
189/// Rule sets: [
190/// RuleSet { name: "MyRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: ["AnotherRuleSet"] },
191/// RuleSet { name: "AnotherRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: [] }
192/// ]
193/// ```
194///
195pub fn get_all_rule_sets() -> Vec<&'static RuleSet<'static>> {
196 RULE_SETS_DISTRIBUTED_SLICE.iter().collect()
197}
198
199/// Get a rule set by name.
200/// Returns the rule set with the given name or None if it doesn't exist.
201///
202/// # Example
203/// ```rust
204/// use conjure_core::rule_engine::register_rule_set;
205/// use conjure_core::rule_engine::get_rule_set_by_name;
206///
207/// register_rule_set!("MyRuleSet", ("DependencyRuleSet", "AnotherRuleSet"));
208///
209/// println!("Rule set: {:?}", get_rule_set_by_name("MyRuleSet"));
210/// ```
211///
212/// This will print:
213/// ```text
214/// Rule set: Some(RuleSet { name: "MyRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: ["DependencyRuleSet", "AnotherRuleSet"] })
215/// ```
216pub fn get_rule_set_by_name(name: &str) -> Option<&'static RuleSet<'static>> {
217 get_all_rule_sets()
218 .iter()
219 .find(|rule_set| rule_set.name == name)
220 .cloned()
221}
222
223/// Get all rule sets for a given solver family.
224/// Returns a `Vec` of static references to all rule sets that are applicable to the given solver family.
225///
226/// # Example
227///
228/// ```rust
229/// use conjure_core::solver::SolverFamily;
230/// use conjure_core::rule_engine::{get_rule_sets_for_solver_family, register_rule_set};
231///
232/// register_rule_set!("CNF", (), SolverFamily::SAT);
233///
234/// let rule_sets = get_rule_sets_for_solver_family(SolverFamily::SAT);
235/// assert_eq!(rule_sets.len(), 1);
236/// assert_eq!(rule_sets[0].name, "CNF");
237/// ```
238pub fn get_rule_sets_for_solver_family(
239 solver_family: SolverFamily,
240) -> Vec<&'static RuleSet<'static>> {
241 get_all_rule_sets()
242 .iter()
243 .filter(|rule_set| {
244 rule_set
245 .solver_families
246 .iter()
247 .any(|family| family.eq(&solver_family))
248 })
249 .cloned()
250 .collect()
251}