oxieml 0.1.2

EML operator: all elementary functions from exp(x) - ln(y)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use super::helpers::check_constraint;
use super::*;
use crate::EmlTree;
use crate::canonical::Canonical;

#[test]
fn test_smt_sat_exp_positive() {
    let x = EmlTree::var(0);
    let one = EmlTree::one();
    let exp_x = EmlTree::eml(&x, &one);
    let c = EmlConstraint::GtZero(exp_x);
    let solver = EmlSmtSolver::new(vec![(-3.0, 3.0)]);
    match solver.check_sat(&c).expect("check_sat error") {
        SmtResult::Sat(_) => {}
        other => panic!("expected Sat, got {other:?}"),
    }
}

#[test]
fn test_smt_ln_bracket() {
    // ln(x) > 0 on [1.1, 5.0] should be Sat.
    let x = EmlTree::var(0);
    let ln_x = Canonical::ln(&x);
    let gt = EmlConstraint::GtZero(ln_x);
    let solver = EmlSmtSolver::new(vec![(1.1, 5.0)]);
    assert!(matches!(
        solver.check_sat(&gt).expect("check_sat error"),
        SmtResult::Sat(_)
    ));
}

#[test]
fn test_smt_unsat_ln_of_negative() {
    // ln(x) > 0 with x in [-2, -1] is unsat (ln undefined on that domain).
    let x = EmlTree::var(0);
    let ln_x = Canonical::ln(&x);
    let c = EmlConstraint::GtZero(ln_x);
    let solver = EmlSmtSolver::new(vec![(-2.0, -1.0)]);
    assert!(matches!(
        solver.check_sat(&c).expect("check_sat error"),
        SmtResult::Unsat
    ));
}

#[test]
fn test_smt_witness_verifies() {
    let x = EmlTree::var(0);
    let one = EmlTree::one();
    let exp_x = EmlTree::eml(&x, &one);
    let c = EmlConstraint::GtZero(exp_x);
    let solver = EmlSmtSolver::new(vec![(-1.0, 1.0)]);
    match solver.check_sat(&c).expect("check_sat error") {
        SmtResult::Sat(sol) => {
            let ctx = crate::eval::EvalCtx::new(&sol.assignments);
            assert!(check_constraint(&c, &ctx));
        }
        other => panic!("expected Sat, got {other:?}"),
    }
}

#[test]
fn test_smt_constant_true() {
    // ln(1) = 0 satisfies EqZero trivially; no free variables.
    let one = EmlTree::one();
    let ln_one = Canonical::ln(&one);
    let c = EmlConstraint::EqZero(ln_one);
    let solver = EmlSmtSolver::default();
    assert!(matches!(
        solver.check_sat(&c).expect("check_sat error"),
        SmtResult::Sat(_)
    ));
}

#[cfg(test)]
mod f3_tests {
    use super::super::constraint::EmlConstraint;
    use super::super::helpers::check_constraint;
    use crate::canonical::Canonical;
    use crate::eval::EvalCtx;

    #[test]
    fn lt_zero_basic() {
        let x = crate::EmlTree::var(0);
        let one = crate::EmlTree::one();
        let constraint = EmlConstraint::LtZero(Canonical::sub(&x, &one));
        let ctx_satisfy = EvalCtx::new(&[0.5]);
        let ctx_violate = EvalCtx::new(&[2.0]);
        assert!(check_constraint(&constraint, &ctx_satisfy));
        assert!(!check_constraint(&constraint, &ctx_violate));
    }

    #[test]
    fn not_eq_zero_becomes_ne_zero() {
        let x = crate::EmlTree::var(0);
        let c = EmlConstraint::Not(Box::new(EmlConstraint::EqZero(x)));
        let nnf = c.to_nnf();
        assert!(matches!(nnf, EmlConstraint::NeZero(_)));
    }

    #[test]
    fn ne_zero_excludes_only_zero() {
        let x = crate::EmlTree::var(0);
        let c = EmlConstraint::NeZero(x);
        let ctx_zero = EvalCtx::new(&[0.0]);
        let ctx_nonzero = EvalCtx::new(&[1.0]);
        assert!(!check_constraint(&c, &ctx_zero));
        assert!(check_constraint(&c, &ctx_nonzero));
    }

    #[test]
    fn binary_lt_helper() {
        let a = crate::EmlTree::var(0);
        let b = crate::EmlTree::var(1);
        let c = EmlConstraint::lt(a, b);
        let ctx_pass = EvalCtx::new(&[1.0, 2.0]);
        let ctx_fail = EvalCtx::new(&[3.0, 2.0]);
        assert!(check_constraint(&c, &ctx_pass));
        assert!(!check_constraint(&c, &ctx_fail));
    }
}

#[cfg(test)]
mod f1_tests {
    use super::super::helpers::check_constraint;
    use super::*;
    use crate::eval::EvalCtx;

    #[test]
    fn every_sat_re_verifies_exp_gt_zero() {
        let solver = EmlSmtSolver::default();
        let x = crate::EmlTree::var(0);
        let one = crate::EmlTree::one();
        let exp_x = crate::EmlTree::eml(&x, &one); // exp(x)
        let c = EmlConstraint::GtZero(exp_x);
        if let Ok(SmtResult::Sat(sol)) = solver.check_sat(&c) {
            let ctx = EvalCtx::new(&sol.assignments);
            assert!(
                check_constraint(&c, &ctx),
                "Sat witness should satisfy the constraint"
            );
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// J1 — Bounded quantifiers + J3 — disjunction hull / NeZero splitting tests
// ────────────────────────────────────────────────────────────────────────────
#[cfg(test)]
mod j1_j3_tests {
    use super::super::constraint::EmlConstraint;
    use super::super::helpers::{QuantResult, decide_exists, decide_forall};
    use super::super::interval::{Interval, IntervalDomain, PropResult};
    use crate::EmlTree;
    use crate::canonical::Canonical;

    // ── J1: NeZero NNF / negate ───────────────────────────────────────────

    #[test]
    fn nnf_forall_negate_yields_exists() {
        // ¬(∀x∈[0,1].φ) should become ∃x∈[0,1].¬φ
        let x = EmlTree::var(0);
        let body = EmlConstraint::GtZero(x.clone());
        let forall = EmlConstraint::ForAll {
            var: 0,
            lo: 0.0,
            hi: 1.0,
            body: Box::new(body),
        };
        let negated = EmlConstraint::Not(Box::new(forall)).to_nnf();
        assert!(
            matches!(negated, EmlConstraint::Exists { .. }),
            "¬∀ should become ∃"
        );
    }

    #[test]
    fn nnf_exists_negate_yields_forall() {
        // ¬(∃x∈[0,1].φ) should become ∀x∈[0,1].¬φ
        let x = EmlTree::var(0);
        let body = EmlConstraint::GtZero(x.clone());
        let exists = EmlConstraint::Exists {
            var: 0,
            lo: 0.0,
            hi: 1.0,
            body: Box::new(body),
        };
        let negated = EmlConstraint::Not(Box::new(exists)).to_nnf();
        assert!(
            matches!(negated, EmlConstraint::ForAll { .. }),
            "¬∃ should become ∀"
        );
    }

    // ── J1: decide_forall ─────────────────────────────────────────────────

    #[test]
    fn forall_refutation_trivially_true() {
        // ∀x∈[1,10]. x > 0  is trivially true — negation x ≤ 0 conflicts with [1,10].
        let x = EmlTree::var(0);
        // body: x > 0  →  GtZero(x - 0) = GtZero(x)
        let body = EmlConstraint::GtZero(x);
        let result = decide_forall(0, 1.0, 10.0, &body, 1);
        assert!(
            matches!(result, QuantResult::True),
            "∀x∈[1,10].x>0 should be detected as True by interval refutation"
        );
    }

    #[test]
    fn forall_counterexample_falsified() {
        // ∀x∈[-5,5]. x > 0  is false; sampling will find negative counterexample.
        let x = EmlTree::var(0);
        let body = EmlConstraint::GtZero(x);
        let result = decide_forall(0, -5.0, 5.0, &body, 1);
        assert!(
            matches!(result, QuantResult::FalseWithCounterexample { .. }),
            "∀x∈[-5,5].x>0 should be falsified by a counterexample"
        );
    }

    // ── J1: decide_exists ─────────────────────────────────────────────────

    #[test]
    fn exists_witness_found() {
        // ∃x∈[0,10]. x > 5  — midpoint 5.0 barely doesn't satisfy, but 7.5 does.
        let x = EmlTree::var(0);
        let five = EmlTree::const_val(5.0);
        let body = EmlConstraint::GtZero(Canonical::sub(&x, &five));
        let result = decide_exists(0, 0.0, 10.0, &body, 1);
        assert!(
            matches!(result, QuantResult::TrueWithWitness(_)),
            "∃x∈[0,10].x>5 should find a witness"
        );
    }

    #[test]
    fn exists_unknown_when_no_witness_in_narrow_band() {
        // ∃x∈[10,20]. x == 0 — no sample will satisfy this, returns Unknown.
        let x = EmlTree::var(0);
        let body = EmlConstraint::EqZero(x);
        let result = decide_exists(0, 10.0, 20.0, &body, 1);
        // We expect Unknown (never False — sound over-approximation).
        assert!(
            matches!(result, QuantResult::Unknown),
            "∃x∈[10,20].x==0 should return Unknown (not False)"
        );
    }

    // ── J1: EmlSmtSolver integration ──────────────────────────────────────

    #[test]
    fn smt_solver_forall_trivially_true() {
        // ∀x∈[1,5]. exp(x) > 0 — always true; should return Sat.
        let x = EmlTree::var(0);
        let one = EmlTree::one();
        let exp_x = EmlTree::eml(&x, &one);
        let body = EmlConstraint::GtZero(exp_x);
        let c = EmlConstraint::ForAll {
            var: 0,
            lo: 1.0,
            hi: 5.0,
            body: Box::new(body),
        };
        let solver = super::EmlSmtSolver::new(vec![(-10.0, 10.0)]);
        let result = solver.check_sat(&c).expect("check_sat should not error");
        assert!(
            matches!(result, super::SmtResult::Sat(_) | super::SmtResult::Unknown),
            "∀x∈[1,5].exp(x)>0 should be Sat or Unknown, got {result:?}"
        );
    }

    #[test]
    fn smt_solver_exists_finds_witness() {
        // ∃x∈[0,5]. x > 3 — solver should return Sat.
        let x = EmlTree::var(0);
        let three = EmlTree::const_val(3.0);
        let body = EmlConstraint::GtZero(Canonical::sub(&x, &three));
        let c = EmlConstraint::Exists {
            var: 0,
            lo: 0.0,
            hi: 5.0,
            body: Box::new(body),
        };
        let solver = super::EmlSmtSolver::new(vec![(-10.0, 10.0)]);
        let result = solver.check_sat(&c).expect("check_sat should not error");
        assert!(
            matches!(result, super::SmtResult::Sat(_)),
            "∃x∈[0,5].x>3 should be Sat, got {result:?}"
        );
    }

    // ── J3: Or hull tightening ────────────────────────────────────────────

    #[test]
    fn or_all_infeasible_yields_conflict() {
        // Or([exp(x) < 0, ln(x) > 0 where x ∈ [-2,-1]])
        // Both branches infeasible → Conflict.
        let x = EmlTree::var(0);
        let one = EmlTree::one();
        let exp_x = EmlTree::eml(&x, &one);
        let ln_x = Canonical::ln(&x);

        let c = EmlConstraint::Or(vec![
            // exp(x) < 0 — always false
            EmlConstraint::LtZero(exp_x),
            // ln(x) > 0 with x ∈ [-2,-1] — domain makes ln undefined
            EmlConstraint::GtZero(ln_x),
        ]);
        let mut domain = IntervalDomain::new(&[(-2.0, -1.0)], 1);
        let result = domain.propagate(&c);
        assert_eq!(
            result,
            PropResult::Conflict,
            "Both Or-branches infeasible → Conflict"
        );
    }

    #[test]
    fn or_single_feasible_branch_adopted() {
        // Or([exp(x) < 0, exp(x) > 0]) with x ∈ [-5, 5].
        // Branch exp(x) < 0 is always infeasible (exp > 0 everywhere).
        // Branch exp(x) > 0 is always feasible.
        // Result must not be Conflict.
        let x = EmlTree::var(0);
        let one = EmlTree::one();
        let exp_x = EmlTree::eml(&x, &one); // exp(x)

        let c = EmlConstraint::Or(vec![
            // exp(x) < 0 — always infeasible
            EmlConstraint::LtZero(exp_x.clone()),
            // exp(x) > 0 — always feasible
            EmlConstraint::GtZero(exp_x),
        ]);
        let mut domain = IntervalDomain::new(&[(-5.0, 5.0)], 1);
        let result = domain.propagate(&c);
        // The feasible branch should prevent Conflict.
        assert_ne!(
            result,
            PropResult::Conflict,
            "At least one Or-branch is feasible"
        );
    }

    // ── J3: NeZero propagation ────────────────────────────────────────────

    #[test]
    fn nezero_conflict_on_point_zero() {
        // x ≠ 0 with x ∈ [0, 0] → Conflict.
        let x = EmlTree::var(0);
        let c = EmlConstraint::NeZero(x);
        let mut domain = IntervalDomain::new(&[(0.0, 0.0)], 1);
        let result = domain.propagate(&c);
        assert_eq!(
            result,
            PropResult::Conflict,
            "x≠0 with x=[0,0] must Conflict"
        );
    }

    #[test]
    fn nezero_nudges_lower_bound_off_zero() {
        // x ≠ 0 with x ∈ [0.0, 5.0] → lower bound should be nudged above 0.
        let x = EmlTree::var(0);
        let c = EmlConstraint::NeZero(x);
        let mut domain = IntervalDomain::new(&[(0.0, 5.0)], 1);
        let result = domain.propagate(&c);
        assert_ne!(
            result,
            PropResult::Conflict,
            "x≠0 with x∈[0,5] should not Conflict"
        );
        let lo = domain.vars[0].lo;
        assert!(
            lo > 0.0,
            "lower bound should be nudged above 0, got lo={lo}"
        );
    }

    #[test]
    fn nezero_nudges_upper_bound_off_zero() {
        // x ≠ 0 with x ∈ [-5.0, 0.0] → upper bound should be nudged below 0.
        let x = EmlTree::var(0);
        let c = EmlConstraint::NeZero(x);
        let mut domain = IntervalDomain::new(&[(-5.0, 0.0)], 1);
        let result = domain.propagate(&c);
        assert_ne!(
            result,
            PropResult::Conflict,
            "x≠0 with x∈[-5,0] should not Conflict"
        );
        let hi = domain.vars[0].hi;
        assert!(
            hi < 0.0,
            "upper bound should be nudged below 0, got hi={hi}"
        );
    }

    #[test]
    fn nezero_stable_when_zero_interior() {
        // x ≠ 0 with x ∈ [-1.0, 1.0] → 0 is interior, can't split — Stable.
        let x = EmlTree::var(0);
        let c = EmlConstraint::NeZero(x);
        let mut domain = IntervalDomain::new(&[(-1.0, 1.0)], 1);
        let result = domain.propagate(&c);
        // Should be Stable (can't represent a hole in a single interval).
        assert_ne!(
            result,
            PropResult::Conflict,
            "x≠0 with 0 interior should not Conflict"
        );
    }

    // ── Display (smoke test) ─────────────────────────────────────────────

    #[test]
    fn forall_exists_display() {
        let x = EmlTree::var(0);
        let body = EmlConstraint::GtZero(x);
        let fa = EmlConstraint::ForAll {
            var: 0,
            lo: 0.0,
            hi: 1.0,
            body: Box::new(body.clone()),
        };
        let ex = EmlConstraint::Exists {
            var: 0,
            lo: -1.0,
            hi: 1.0,
            body: Box::new(body),
        };
        let fa_s = format!("{fa}");
        let ex_s = format!("{ex}");
        assert!(
            fa_s.contains("∀x0"),
            "ForAll display should contain ∀x0, got: {fa_s}"
        );
        assert!(
            ex_s.contains("∃x0"),
            "Exists display should contain ∃x0, got: {ex_s}"
        );
    }

    // ── Interval domain public API smoke tests ────────────────────────────

    #[test]
    fn interval_hull_and_intersect_sanity() {
        let a = Interval::new(0.0, 3.0);
        let b = Interval::new(2.0, 5.0);
        let hull = a.hull(&b);
        assert!((hull.lo - 0.0).abs() < 1e-12);
        assert!((hull.hi - 5.0).abs() < 1e-12);
        let inter = a.intersect(&b);
        assert!((inter.lo - 2.0).abs() < 1e-12);
        assert!((inter.hi - 3.0).abs() < 1e-12);
    }
}