1
pub 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
1
/// 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
/// ```
44
1
pub use conjure_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>, ...));
55
/// ```
56
///
57
/// # Example
58
///
59
/// ```rust
60
1
/// use conjure_core::rule_engine::register_rule_set;
61
///
62
/// register_rule_set!("MyRuleSet", ("DependencyRuleSet", "AnotherRuleSet"));
63
/// ```
64
1
#[doc(inline)]
65
pub use conjure_macros::register_rule_set;
66
pub use resolve_rules::{get_rules, get_rules_grouped, resolve_rule_sets, RuleData};
67
pub use rewrite::rewrite_model;
68
pub use rewrite_naive::rewrite_naive;
69
pub use rewriter_common::RewriteError;
70
pub use rule::{ApplicationError, ApplicationResult, Reduction, Rule};
71
pub use rule_set::RuleSet;
72

            
73
use crate::solver::SolverFamily;
74

            
75
mod resolve_rules;
76
mod rewrite;
77
mod rewrite_naive;
78
mod rewriter_common;
79
mod rule;
80
mod rule_set;
81

            
82
#[doc(hidden)]
83
#[distributed_slice]
84
pub static RULES_DISTRIBUTED_SLICE: [Rule<'static>];
85

            
86
#[doc(hidden)]
87
#[distributed_slice]
88
pub static RULE_SETS_DISTRIBUTED_SLICE: [RuleSet<'static>];
89

            
90
pub mod _dependencies {
91
    pub use linkme;
92
    pub use linkme::distributed_slice;
93
}
94

            
95
/// Returns a copied `Vec` of all rules registered with the `register_rule` macro.
96
///
97
/// Rules are not guaranteed to be in any particular order.
98
///
99
/// # Example
100
/// ```rust
101
/// # use conjure_core::rule_engine::{ApplicationResult, Reduction, get_all_rules};
102
/// # use conjure_core::ast::Expression;
103
/// # use conjure_core::ast::SymbolTable;
104
/// # use conjure_core::rule_engine::register_rule;
105
///
106
/// #[register_rule]
107
/// fn identity(expr: &Expression, symbols: &SymbolTable) -> ApplicationResult {
108
///   Ok(Reduction::pure(expr.clone()))
109
/// }
110
///
111
/// fn main() {
112
1
///   println!("Rules: {:?}", get_all_rules());
113
1
/// }
114
1
/// ```
115
///
116
/// This will print (if no other rules are registered):
117
/// ```text
118
///   Rules: [Rule { name: "identity", application: MEM }]
119
/// ```
120
/// Where `MEM` is the memory address of the `identity` function.
121
544
pub fn get_all_rules() -> Vec<&'static Rule<'static>> {
122
544
    RULES_DISTRIBUTED_SLICE.iter().collect()
123
544
}
124

            
125
/// Get a rule by name.
126
/// Returns the rule with the given name or None if it doesn't exist.
127
///
128
/// # Example
129
/// ```rust
130
/// use conjure_core::rule_engine::register_rule;
131
/// use conjure_core::rule_engine::{Rule, ApplicationResult, Reduction, get_rule_by_name};
132
/// use conjure_core::ast::Expression;
133
/// use conjure_core::ast::SymbolTable;
134
///
135
/// #[register_rule]
136
/// fn identity(expr: &Expression, symbols: &SymbolTable) -> ApplicationResult {
137
///  Ok(Reduction::pure(expr.clone()))
138
/// }
139
///
140
/// fn main() {
141
1
/// println!("Rule: {:?}", get_rule_by_name("identity"));
142
1
/// }
143
1
/// ```
144
///
145
/// This will print:
146
/// ```text
147
/// Rule: Some(Rule { name: "identity", application: MEM })
148
/// ```
149
289
pub fn get_rule_by_name(name: &str) -> Option<&'static Rule<'static>> {
150
289
    get_all_rules()
151
289
        .iter()
152
5593
        .find(|rule| rule.name == name)
153
289
        .cloned()
154
289
}
155

            
156
/// Get all rule sets
157
/// Returns a `Vec` of static references to all rule sets registered with the `register_rule_set` macro.
158
/// Rule sets are not guaranteed to be in any particular order.
159
///
160
/// # Example
161
/// ```rust
162
1
/// use conjure_core::rule_engine::register_rule_set;
163
/// use conjure_core::rule_engine::get_all_rule_sets;
164
///
165
/// register_rule_set!("MyRuleSet", ("AnotherRuleSet"));
166
/// register_rule_set!("AnotherRuleSet", ());
167
///
168
/// println!("Rule sets: {:?}", get_all_rule_sets());
169
1
/// ```
170
1
///
171
/// This will print (if no other rule sets are registered):
172
/// ```text
173
/// Rule sets: [
174
///   RuleSet { name: "MyRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: ["AnotherRuleSet"] },
175
///   RuleSet { name: "AnotherRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: [] }
176
/// ]
177
/// ```
178
///
179
6273
pub fn get_all_rule_sets() -> Vec<&'static RuleSet<'static>> {
180
6273
    RULE_SETS_DISTRIBUTED_SLICE.iter().collect()
181
6273
}
182

            
183
/// Get a rule set by name.
184
/// Returns the rule set with the given name or None if it doesn't exist.
185
///
186
/// # Example
187
/// ```rust
188
1
/// use conjure_core::rule_engine::register_rule_set;
189
/// use conjure_core::rule_engine::get_rule_set_by_name;
190
///
191
/// register_rule_set!("MyRuleSet", ("DependencyRuleSet", "AnotherRuleSet"));
192
///
193
/// println!("Rule set: {:?}", get_rule_set_by_name("MyRuleSet"));
194
1
/// ```
195
1
///
196
/// This will print:
197
/// ```text
198
/// Rule set: Some(RuleSet { name: "MyRuleSet", rules: OnceLock { state: Uninitialized }, dependencies: ["DependencyRuleSet", "AnotherRuleSet"] })
199
/// ```
200
4675
pub fn get_rule_set_by_name(name: &str) -> Option<&'static RuleSet<'static>> {
201
4675
    get_all_rule_sets()
202
4675
        .iter()
203
17102
        .find(|rule_set| rule_set.name == name)
204
4675
        .cloned()
205
4675
}
206

            
207
/// Get all rule sets for a given solver family.
208
/// Returns a `Vec` of static references to all rule sets that are applicable to the given solver family.
209
///
210
/// # Example
211
///
212
/// ```rust
213
1
/// use conjure_core::solver::SolverFamily;
214
/// use conjure_core::rule_engine::get_rule_sets_for_solver_family;
215
///
216
/// let rule_sets = get_rule_sets_for_solver_family(SolverFamily::SAT);
217
1
/// assert_eq!(rule_sets.len(), 1);
218
1
/// assert_eq!(rule_sets[0].name, "CNF");
219
1
/// ```
220
1489
pub fn get_rule_sets_for_solver_family(
221
1581
    solver_family: SolverFamily,
222
1581
) -> Vec<&'static RuleSet<'static>> {
223
1581
    get_all_rule_sets()
224
1581
        .iter()
225
7905
        .filter(|rule_set| {
226
7905
            rule_set
227
7905
                .solver_families
228
7905
                .iter()
229
7905
                .any(|family| family.eq(&solver_family))
230
7905
        })
231
1581
        .cloned()
232
1581
        .collect()
233
1581
}