logicaffeine_kernel/omega.rs
1//! Omega Test: True Integer Arithmetic Decision Procedure
2//!
3//! This module implements the Omega test for linear integer arithmetic,
4//! handling the discrete nature of integers correctly.
5//!
6//! # Difference from LIA
7//!
8//! Unlike [`crate::lia`] (which uses rational arithmetic), this module
9//! handles integers with proper semantics:
10//!
11//! - `x > 1` becomes `x >= 2` (strict to non-strict for integers)
12//! - `3x <= 10` implies `x <= 3` (integer division with floor)
13//! - `2x = 5` is unsatisfiable (odd number cannot equal even expression)
14//!
15//! # Algorithm
16//!
17//! The algorithm is similar to Fourier-Motzkin elimination but with
18//! integer-aware semantics:
19//!
20//! 1. **Normalize**: Scale constraints and normalize by GCD
21//! 2. **Convert strict**: Transform `<` to `<=` using integer shift
22//! 3. **Eliminate**: Fourier-Motzkin with integer coefficient handling
23//! 4. **Check**: Verify constant constraints for contradictions
24//!
25//! # When to Use
26//!
27//! Use omega when you need exact integer semantics. Use lia when
28//! rational arithmetic is acceptable (faster but may miss integer-specific
29//! unsatisfiability).
30//!
31//! # Exactness
32//!
33//! Coefficients are arbitrary-precision ([`BigInt`]): Fourier-Motzkin
34//! combinations multiply coefficients pairwise, and the verdict feeds trusted
35//! reflection reductions, so a wrapped product would flip satisfiable into
36//! unsatisfiable.
37
38use std::collections::{BTreeMap, HashSet};
39
40use logicaffeine_base::numeric::BigInt;
41
42use crate::reify::{extract_binary_app, extract_slit, extract_sname, extract_svar, VarInterner};
43use crate::term::Term;
44
45/// Integer linear expression of the form c + a₁x₁ + a₂x₂ + ... + aₙxₙ.
46///
47/// Similar to [`crate::lia::LinearExpr`] but uses integer coefficients
48/// instead of rationals for exact integer arithmetic.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct IntExpr {
51 /// The constant term c.
52 pub constant: BigInt,
53 /// Maps variable index to its integer coefficient (sparse representation).
54 pub coeffs: BTreeMap<i64, BigInt>,
55}
56
57impl IntExpr {
58 /// Create a constant expression
59 pub fn constant(c: impl Into<BigInt>) -> Self {
60 IntExpr {
61 constant: c.into(),
62 coeffs: BTreeMap::new(),
63 }
64 }
65
66 /// Create a single variable expression: 1*x_idx + 0
67 pub fn var(idx: i64) -> Self {
68 let mut coeffs = BTreeMap::new();
69 coeffs.insert(idx, BigInt::from_i64(1));
70 IntExpr {
71 constant: BigInt::zero(),
72 coeffs,
73 }
74 }
75
76 /// Add two expressions
77 pub fn add(&self, other: &Self) -> Self {
78 let mut result = self.clone();
79 result.constant = result.constant.add(&other.constant);
80 for (&v, c) in &other.coeffs {
81 let entry = result.coeffs.entry(v).or_insert_with(BigInt::zero);
82 *entry = entry.add(c);
83 if entry.is_zero() {
84 result.coeffs.remove(&v);
85 }
86 }
87 result
88 }
89
90 /// Negate an expression
91 pub fn neg(&self) -> Self {
92 IntExpr {
93 constant: self.constant.negated(),
94 coeffs: self.coeffs.iter().map(|(&v, c)| (v, c.negated())).collect(),
95 }
96 }
97
98 /// Subtract two expressions
99 pub fn sub(&self, other: &Self) -> Self {
100 self.add(&other.neg())
101 }
102
103 /// Scale by an integer constant
104 pub fn scale(&self, k: impl Into<BigInt>) -> Self {
105 let k = k.into();
106 if k.is_zero() {
107 return IntExpr::constant(0);
108 }
109 IntExpr {
110 constant: self.constant.mul(&k),
111 coeffs: self
112 .coeffs
113 .iter()
114 .map(|(&v, c)| (v, c.mul(&k)))
115 .filter(|(_, c)| !c.is_zero())
116 .collect(),
117 }
118 }
119
120 /// Check if this is a constant expression (no variables)
121 pub fn is_constant(&self) -> bool {
122 self.coeffs.is_empty()
123 }
124
125 /// Get coefficient of a variable (0 if not present)
126 pub fn get_coeff(&self, var: i64) -> BigInt {
127 self.coeffs.get(&var).cloned().unwrap_or_else(BigInt::zero)
128 }
129}
130
131/// Integer constraint representing `expr <= 0` or `expr < 0`.
132///
133/// For integers, strict inequalities can be converted to non-strict:
134/// `x < k` is equivalent to `x <= k - 1`.
135#[derive(Debug, Clone)]
136pub struct IntConstraint {
137 /// The linear expression (constraint is expr OP 0).
138 pub expr: IntExpr,
139 /// If true, this is a strict inequality (`< 0`).
140 /// If false, this is a non-strict inequality (`<= 0`).
141 pub strict: bool,
142}
143
144impl IntConstraint {
145 /// Check if a constant constraint is satisfied
146 pub fn is_satisfied_constant(&self) -> bool {
147 if !self.expr.is_constant() {
148 return true; // Can't determine yet
149 }
150 let c = &self.expr.constant;
151 if self.strict {
152 c.is_negative() // c < 0
153 } else {
154 !c.is_positive() // c ≤ 0
155 }
156 }
157
158 /// Normalize by GCD of all coefficients
159 pub fn normalize(&mut self) {
160 let g = self
161 .expr
162 .coeffs
163 .values()
164 .chain(std::iter::once(&self.expr.constant))
165 .filter(|x| !x.is_zero())
166 .fold(BigInt::zero(), |a, b| gcd(&a, &b.abs()));
167
168 if g > BigInt::from_i64(1) {
169 self.expr.constant = exact_div(&self.expr.constant, &g);
170 for v in self.expr.coeffs.values_mut() {
171 *v = exact_div(v, &g);
172 }
173 }
174 }
175}
176
177/// GCD using the Euclidean algorithm (arguments non-negative).
178fn gcd(a: &BigInt, b: &BigInt) -> BigInt {
179 if b.is_zero() {
180 a.clone()
181 } else {
182 let (_, r) = a.div_rem(b).expect("gcd divisor is nonzero");
183 gcd(b, &r)
184 }
185}
186
187/// Divide exactly (the divisor is a common divisor of the dividend).
188fn exact_div(a: &BigInt, g: &BigInt) -> BigInt {
189 a.div_rem(g).expect("gcd is nonzero").0
190}
191
192/// Reify a Syntax term to an integer linear expression.
193///
194/// Converts the deep embedding (Syntax) into an integer linear expression.
195/// Similar to [`crate::lia::reify_linear`] but produces integer coefficients.
196///
197/// # Supported Forms
198///
199/// - `SLit n` - Integer literal becomes a constant
200/// - `SVar i` - De Bruijn variable becomes a linear variable
201/// - `SName "x"` - Named global becomes a linear variable (interned)
202/// - `add`, `sub`, `mul` - Arithmetic operations (mul only if one operand is constant)
203///
204/// Every term reified for one goal (hypotheses and conclusion alike) must
205/// share one `vars` interner, or their variable indices will not line up.
206///
207/// # Returns
208///
209/// `Some(expr)` on success, `None` if the term is non-linear or malformed.
210pub fn reify_int_linear(term: &Term, vars: &mut VarInterner) -> Option<IntExpr> {
211 // SLit n -> constant
212 if let Some(n) = extract_slit(term) {
213 return Some(IntExpr::constant(n));
214 }
215
216 // SVar i -> variable
217 if let Some(i) = extract_svar(term) {
218 return Some(IntExpr::var(i));
219 }
220
221 // SName "x" -> named variable (global constant treated as free variable)
222 if let Some(name) = extract_sname(term) {
223 return Some(IntExpr::var(vars.intern(&name)));
224 }
225
226 // Binary operations
227 if let Some((op, a, b)) = extract_binary_app(term) {
228 match op.as_str() {
229 "add" => {
230 let la = reify_int_linear(&a, vars)?;
231 let lb = reify_int_linear(&b, vars)?;
232 return Some(la.add(&lb));
233 }
234 "sub" => {
235 let la = reify_int_linear(&a, vars)?;
236 let lb = reify_int_linear(&b, vars)?;
237 return Some(la.sub(&lb));
238 }
239 "mul" => {
240 let la = reify_int_linear(&a, vars)?;
241 let lb = reify_int_linear(&b, vars)?;
242 // Only linear if one side is constant
243 if la.is_constant() {
244 return Some(lb.scale(la.constant));
245 }
246 if lb.is_constant() {
247 return Some(la.scale(lb.constant));
248 }
249 return None; // Non-linear
250 }
251 _ => return None,
252 }
253 }
254
255 None
256}
257
258/// Extract comparison from goal: (SApp (SApp (SName "Lt"|"Le"|"Gt"|"Ge") lhs) rhs)
259pub fn extract_comparison(term: &Term) -> Option<(String, Term, Term)> {
260 if let Some((rel, lhs, rhs)) = extract_binary_app(term) {
261 match rel.as_str() {
262 "Lt" | "Le" | "Gt" | "Ge" | "lt" | "le" | "gt" | "ge" => {
263 return Some((rel, lhs, rhs));
264 }
265 _ => {}
266 }
267 }
268 None
269}
270
271/// Convert a goal to constraints for validity checking using integer semantics.
272///
273/// Key difference from lia: strict inequalities are converted for integers.
274/// - x < k becomes x <= k - 1 (since x must be an integer)
275/// - x > k becomes x >= k + 1
276///
277/// To prove a goal is valid, we check if its negation is unsatisfiable.
278pub fn goal_to_negated_constraint(
279 rel: &str,
280 lhs: &IntExpr,
281 rhs: &IntExpr,
282) -> Option<IntConstraint> {
283 // diff = lhs - rhs
284 let diff = lhs.sub(rhs);
285 let one = IntExpr::constant(1);
286
287 match rel {
288 // Lt: a < b valid iff NOT(a >= b)
289 // For integers: a >= b means a - b >= 0
290 // We check if a - b >= 0 is satisfiable
291 // Constraint form for unsatisfiability check: -(a - b) <= 0, i.e., (b - a) <= 0
292 "Lt" | "lt" => Some(IntConstraint {
293 expr: rhs.sub(lhs),
294 strict: false,
295 }),
296
297 // Le: a <= b valid iff NOT(a > b)
298 // For integers: a > b means a - b >= 1 (strict to non-strict!)
299 // So negation is: a - b >= 1, i.e., a - b - 1 >= 0
300 // Constraint: -(a - b - 1) <= 0, i.e., (b - a + 1) <= 0
301 // Equivalently: (b - a) <= -1
302 "Le" | "le" => Some(IntConstraint {
303 expr: rhs.sub(lhs).add(&one),
304 strict: false,
305 }),
306
307 // Gt: a > b valid iff NOT(a <= b)
308 // For integers: a <= b means a - b <= 0
309 // Constraint: (a - b) <= 0
310 "Gt" | "gt" => Some(IntConstraint {
311 expr: diff,
312 strict: false,
313 }),
314
315 // Ge: a >= b valid iff NOT(a < b)
316 // For integers: a < b means a - b <= -1 (strict to non-strict!)
317 // Constraint: (a - b) <= -1, i.e., (a - b + 1) <= 0
318 "Ge" | "ge" => Some(IntConstraint {
319 expr: diff.add(&one),
320 strict: false,
321 }),
322
323 _ => None,
324 }
325}
326
327/// Check if integer constraints are unsatisfiable using the Omega test.
328///
329/// This is the main entry point for the omega decision procedure. It uses
330/// integer-aware Fourier-Motzkin elimination to check for contradictions.
331///
332/// # Integer Semantics
333///
334/// Unlike rational Fourier-Motzkin, this procedure:
335/// - Normalizes constraints by their GCD
336/// - Handles strict inequalities by integer shift (`< k` becomes `<= k-1`)
337/// - Detects integer-specific unsatisfiability
338///
339/// # Returns
340///
341/// - `true` if no integer assignment satisfies all constraints (unsatisfiable)
342/// - `false` if the constraints may be satisfiable
343///
344/// # Usage for Validity
345///
346/// To prove a goal G is valid over integers, check if NOT(G) is unsatisfiable.
347/// If `omega_unsat(negation_constraints)` returns true, the goal is valid.
348pub fn omega_unsat(constraints: &[IntConstraint]) -> bool {
349 if constraints.is_empty() {
350 return false;
351 }
352
353 // Normalize all constraints
354 let mut current: Vec<IntConstraint> = constraints.to_vec();
355 for c in &mut current {
356 c.normalize();
357 }
358
359 // Check for immediate contradictions
360 for c in ¤t {
361 if c.expr.is_constant() && !c.is_satisfied_constant() {
362 return true;
363 }
364 }
365
366 // Collect all variables
367 let vars: Vec<i64> = current
368 .iter()
369 .flat_map(|c| c.expr.coeffs.keys().copied())
370 .collect::<HashSet<_>>()
371 .into_iter()
372 .collect();
373
374 // Eliminate each variable
375 for var in vars {
376 current = eliminate_variable_int(¤t, var);
377
378 // Early termination: check for constant contradictions
379 for c in ¤t {
380 if c.expr.is_constant() && !c.is_satisfied_constant() {
381 return true;
382 }
383 }
384 }
385
386 // Check all remaining constant constraints
387 current
388 .iter()
389 .any(|c| c.expr.is_constant() && !c.is_satisfied_constant())
390}
391
392/// Eliminate a variable from constraints using integer-aware Fourier-Motzkin.
393fn eliminate_variable_int(constraints: &[IntConstraint], var: i64) -> Vec<IntConstraint> {
394 let mut lower: Vec<(IntExpr, BigInt)> = vec![]; // (rest, |coeff|) for lower bounds
395 let mut upper: Vec<(IntExpr, BigInt)> = vec![]; // (rest, coeff) for upper bounds
396 let mut independent: Vec<IntConstraint> = vec![];
397
398 for c in constraints {
399 let coeff = c.expr.get_coeff(var);
400 if coeff.is_zero() {
401 independent.push(c.clone());
402 } else {
403 // c.expr = coeff*var + rest <= 0
404 let mut rest = c.expr.clone();
405 rest.coeffs.remove(&var);
406
407 if coeff.is_positive() {
408 // coeff*var + rest <= 0
409 // var <= -rest/coeff (upper bound)
410 upper.push((rest, coeff));
411 } else {
412 // coeff*var + rest <= 0, coeff < 0
413 // |coeff|*(-var) + rest <= 0
414 // -var <= -rest/|coeff|
415 // var >= rest/|coeff| (lower bound)
416 lower.push((rest, coeff.negated()));
417 }
418 }
419 }
420
421 // Combine lower and upper bounds
422 // If lo/a <= var <= -hi/b, then lo/a <= -hi/b
423 // Multiply out: b*lo <= -a*hi
424 // Rearrange: b*lo + a*hi <= 0
425 for (lo_rest, lo_coeff) in &lower {
426 for (hi_rest, hi_coeff) in &upper {
427 // Lower: var >= lo_rest / lo_coeff (lo_coeff is positive)
428 // Upper: var <= -hi_rest / hi_coeff (hi_coeff is positive)
429 // Combined: lo_rest / lo_coeff <= -hi_rest / hi_coeff
430 // => hi_coeff * lo_rest <= -lo_coeff * hi_rest
431 // => hi_coeff * lo_rest + lo_coeff * hi_rest <= 0
432 let new_expr = lo_rest
433 .scale(hi_coeff.clone())
434 .add(&hi_rest.scale(lo_coeff.clone()));
435
436 let mut new_constraint = IntConstraint {
437 expr: new_expr,
438 strict: false,
439 };
440 new_constraint.normalize();
441 independent.push(new_constraint);
442 }
443 }
444
445 independent
446}
447
448#[cfg(test)]
449#[path = "../../logicaffeine_tests/unit/logicaffeine_kernel/omega/tests.rs"]
450mod tests;