1mod abduction;
4mod arithmetic;
5mod comparison;
6mod conditional;
7mod constraints;
8mod counting_quantifiers;
9pub mod custom_ops;
10mod fixpoint;
11mod fuzzy;
12mod higher_order;
13mod hybrid;
14mod implication;
15mod let_binding;
16mod logic_ops;
17mod modal_temporal;
18mod predicate;
19mod probabilistic;
20mod quantifiers;
21mod set_operations;
22mod strategy_mapping;
23
24use anyhow::Result;
25use tensorlogic_ir::{EinsumGraph, TLExpr};
26
27use crate::context::{CompileState, CompilerContext};
28
29pub(crate) use abduction::{compile_abducible, compile_explain};
30pub(crate) use arithmetic::{
31 compile_abs, compile_add, compile_ceil, compile_cos, compile_div, compile_exp, compile_floor,
32 compile_log, compile_max_binary, compile_min_binary, compile_mod, compile_mul, compile_pow,
33 compile_round, compile_sin, compile_sqrt, compile_sub, compile_tan,
34};
35pub(crate) use comparison::{compile_eq, compile_gt, compile_gte, compile_lt, compile_lte};
36pub(crate) use conditional::{compile_constant, compile_if_then_else};
37pub(crate) use constraints::{compile_all_different, compile_global_cardinality};
38pub(crate) use counting_quantifiers::{
39 compile_counting_exists, compile_counting_forall, compile_exact_count, compile_majority,
40};
41pub use custom_ops::{
42 CustomOpData, CustomOpHandler, CustomOpMetadata, CustomOpRegistry, ExtendedCompilerContext,
43};
44pub(crate) use fixpoint::{compile_greatest_fixpoint, compile_least_fixpoint};
45pub(crate) use fuzzy::{
46 compile_fuzzy_implication, compile_fuzzy_not, compile_tconorm, compile_tnorm,
47};
48pub(crate) use higher_order::{compile_apply, compile_lambda};
49pub(crate) use hybrid::{compile_at, compile_everywhere, compile_nominal, compile_somewhere};
50pub(crate) use implication::compile_imply;
51pub(crate) use let_binding::compile_let;
52pub(crate) use logic_ops::{compile_and, compile_not, compile_or};
53pub(crate) use modal_temporal::{
54 compile_always, compile_box, compile_diamond, compile_eventually, compile_next,
55 compile_release, compile_strong_release, compile_until, compile_weak_until,
56};
57pub(crate) use predicate::compile_predicate;
58pub(crate) use probabilistic::{compile_probabilistic_choice, compile_weighted_rule};
59pub(crate) use quantifiers::{
60 compile_aggregate, compile_exists, compile_forall, compile_soft_exists, compile_soft_forall,
61};
62pub(crate) use set_operations::{
63 compile_empty_set, compile_set_cardinality, compile_set_comprehension, compile_set_difference,
64 compile_set_intersection, compile_set_membership, compile_set_union,
65};
66
67pub(crate) fn infer_domain(expr: &TLExpr, _var: &str) -> Option<String> {
69 match expr {
70 TLExpr::Exists { domain, .. }
71 | TLExpr::ForAll { domain, .. }
72 | TLExpr::Aggregate { domain, .. }
73 | TLExpr::SoftExists { domain, .. }
74 | TLExpr::SoftForAll { domain, .. }
75 | TLExpr::SetComprehension { domain, .. }
76 | TLExpr::CountingExists { domain, .. }
77 | TLExpr::CountingForAll { domain, .. }
78 | TLExpr::ExactCount { domain, .. }
79 | TLExpr::Majority { domain, .. } => Some(domain.clone()),
80 TLExpr::Box(_)
82 | TLExpr::Diamond(_)
83 | TLExpr::Next(_)
84 | TLExpr::Eventually(_)
85 | TLExpr::Always(_)
86 | TLExpr::Until { .. }
87 | TLExpr::Release { .. }
88 | TLExpr::WeakUntil { .. }
89 | TLExpr::StrongRelease { .. } => None,
90 _ => None,
91 }
92}
93
94pub(crate) fn compile_expr(
96 expr: &TLExpr,
97 ctx: &mut CompilerContext,
98 graph: &mut EinsumGraph,
99) -> Result<CompileState> {
100 match expr {
101 TLExpr::Pred { name, args } => compile_predicate(name, args, ctx, graph),
102 TLExpr::And(left, right) => compile_and(left, right, ctx, graph),
103 TLExpr::Or(left, right) => compile_or(left, right, ctx, graph),
104 TLExpr::Not(inner) => compile_not(inner, ctx, graph),
105 TLExpr::Exists { var, domain, body } => compile_exists(var, domain, body, ctx, graph),
106 TLExpr::ForAll { var, domain, body } => compile_forall(var, domain, body, ctx, graph),
107 TLExpr::Aggregate {
108 op,
109 var,
110 domain,
111 body,
112 group_by,
113 } => compile_aggregate(op, var, domain, body, group_by, ctx, graph),
114 TLExpr::Imply(premise, conclusion) => compile_imply(premise, conclusion, ctx, graph),
115 TLExpr::Score(inner) => compile_expr(inner, ctx, graph),
116
117 TLExpr::Add(left, right) => compile_add(left, right, ctx, graph),
119 TLExpr::Sub(left, right) => compile_sub(left, right, ctx, graph),
120 TLExpr::Mul(left, right) => compile_mul(left, right, ctx, graph),
121 TLExpr::Div(left, right) => compile_div(left, right, ctx, graph),
122
123 TLExpr::Eq(left, right) => compile_eq(left, right, ctx, graph),
125 TLExpr::Lt(left, right) => compile_lt(left, right, ctx, graph),
126 TLExpr::Gt(left, right) => compile_gt(left, right, ctx, graph),
127 TLExpr::Lte(left, right) => compile_lte(left, right, ctx, graph),
128 TLExpr::Gte(left, right) => compile_gte(left, right, ctx, graph),
129
130 TLExpr::Pow(left, right) => compile_pow(left, right, ctx, graph),
132 TLExpr::Mod(left, right) => compile_mod(left, right, ctx, graph),
133 TLExpr::Min(left, right) => compile_min_binary(left, right, ctx, graph),
134 TLExpr::Max(left, right) => compile_max_binary(left, right, ctx, graph),
135
136 TLExpr::Abs(inner) => compile_abs(inner, ctx, graph),
138 TLExpr::Floor(inner) => compile_floor(inner, ctx, graph),
139 TLExpr::Ceil(inner) => compile_ceil(inner, ctx, graph),
140 TLExpr::Round(inner) => compile_round(inner, ctx, graph),
141 TLExpr::Sqrt(inner) => compile_sqrt(inner, ctx, graph),
142 TLExpr::Exp(inner) => compile_exp(inner, ctx, graph),
143 TLExpr::Log(inner) => compile_log(inner, ctx, graph),
144 TLExpr::Sin(inner) => compile_sin(inner, ctx, graph),
145 TLExpr::Cos(inner) => compile_cos(inner, ctx, graph),
146 TLExpr::Tan(inner) => compile_tan(inner, ctx, graph),
147
148 TLExpr::IfThenElse {
150 condition,
151 then_branch,
152 else_branch,
153 } => compile_if_then_else(condition, then_branch, else_branch, ctx, graph),
154
155 TLExpr::Constant(value) => compile_constant(*value, ctx, graph),
157
158 TLExpr::Let { var, value, body } => compile_let(var, value, body, ctx, graph),
160
161 TLExpr::TNorm { kind, left, right } => compile_tnorm(*kind, left, right, ctx, graph),
163 TLExpr::TCoNorm { kind, left, right } => compile_tconorm(*kind, left, right, ctx, graph),
164 TLExpr::FuzzyNot { kind, expr } => compile_fuzzy_not(*kind, expr, ctx, graph),
165 TLExpr::FuzzyImplication {
166 kind,
167 premise,
168 conclusion,
169 } => compile_fuzzy_implication(*kind, premise, conclusion, ctx, graph),
170 TLExpr::SoftExists {
171 var,
172 domain,
173 body,
174 temperature,
175 } => compile_soft_exists(var, domain, body, *temperature, ctx, graph),
176 TLExpr::SoftForAll {
177 var,
178 domain,
179 body,
180 temperature,
181 } => compile_soft_forall(var, domain, body, *temperature, ctx, graph),
182 TLExpr::WeightedRule { weight, rule } => compile_weighted_rule(*weight, rule, ctx, graph),
183 TLExpr::ProbabilisticChoice { alternatives } => {
184 compile_probabilistic_choice(alternatives, ctx, graph)
185 }
186
187 TLExpr::Box(inner) => compile_box(inner, ctx, graph),
189 TLExpr::Diamond(inner) => compile_diamond(inner, ctx, graph),
190
191 TLExpr::Next(inner) => compile_next(inner, ctx, graph),
193 TLExpr::Eventually(inner) => compile_eventually(inner, ctx, graph),
194 TLExpr::Always(inner) => compile_always(inner, ctx, graph),
195 TLExpr::Until { before, after } => compile_until(before, after, ctx, graph),
196 TLExpr::Release { released, releaser } => compile_release(releaser, released, ctx, graph),
197 TLExpr::WeakUntil { before, after } => compile_weak_until(before, after, ctx, graph),
198 TLExpr::StrongRelease { released, releaser } => {
199 compile_strong_release(releaser, released, ctx, graph)
200 }
201
202 TLExpr::CountingExists {
204 var,
205 domain,
206 body,
207 min_count,
208 } => compile_counting_exists(var, domain, body, *min_count, ctx, graph),
209 TLExpr::CountingForAll {
210 var,
211 domain,
212 body,
213 min_count,
214 } => compile_counting_forall(var, domain, body, *min_count, ctx, graph),
215 TLExpr::ExactCount {
216 var,
217 domain,
218 body,
219 count,
220 } => compile_exact_count(var, domain, body, *count, ctx, graph),
221 TLExpr::Majority { var, domain, body } => compile_majority(var, domain, body, ctx, graph),
222
223 TLExpr::Lambda {
225 var,
226 var_type,
227 body,
228 } => compile_lambda(var, var_type, body, ctx, graph),
229 TLExpr::Apply { function, argument } => compile_apply(function, argument, ctx, graph),
230
231 TLExpr::SetMembership { element, set } => compile_set_membership(element, set, ctx, graph),
233 TLExpr::SetUnion { left, right } => compile_set_union(left, right, ctx, graph),
234 TLExpr::SetIntersection { left, right } => {
235 compile_set_intersection(left, right, ctx, graph)
236 }
237 TLExpr::SetDifference { left, right } => compile_set_difference(left, right, ctx, graph),
238 TLExpr::SetCardinality { set } => compile_set_cardinality(set, ctx, graph),
239 TLExpr::EmptySet => compile_empty_set(ctx, graph),
240 TLExpr::SetComprehension {
241 var,
242 domain,
243 condition,
244 } => compile_set_comprehension(var, domain, condition, ctx, graph),
245
246 TLExpr::LeastFixpoint { var, body } => compile_least_fixpoint(var, body, ctx, graph),
248 TLExpr::GreatestFixpoint { var, body } => compile_greatest_fixpoint(var, body, ctx, graph),
249
250 TLExpr::Nominal { name } => compile_nominal(name, ctx, graph),
252 TLExpr::At { nominal, formula } => compile_at(nominal, formula, ctx, graph),
253 TLExpr::Somewhere { formula } => compile_somewhere(formula, ctx, graph),
254 TLExpr::Everywhere { formula } => compile_everywhere(formula, ctx, graph),
255
256 TLExpr::AllDifferent { variables } => compile_all_different(variables, ctx, graph),
258 TLExpr::GlobalCardinality {
259 variables,
260 values,
261 min_occurrences,
262 max_occurrences,
263 } => compile_global_cardinality(
264 variables,
265 values,
266 min_occurrences,
267 max_occurrences,
268 ctx,
269 graph,
270 ),
271
272 TLExpr::Abducible { name, cost } => compile_abducible(name, *cost, ctx, graph),
274 TLExpr::Explain { formula } => compile_explain(formula, ctx, graph),
275 }
276}