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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use super::constraint::EmlConstraint;
use crate::tree::EmlNode;

// ----------------------------------------------------------------------------
// Interval arithmetic
// ----------------------------------------------------------------------------

/// Interval `[lo, hi]` for variable bounds during constraint solving.
///
/// Empty when `lo > hi` or when bounds are non-finite. `hull` is the join used
/// to combine bounds.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Interval {
    /// Lower bound (inclusive).
    pub lo: f64,
    /// Upper bound (inclusive).
    pub hi: f64,
}

impl Interval {
    /// Build a new interval `[lo, hi]`.
    pub fn new(lo: f64, hi: f64) -> Self {
        Self { lo, hi }
    }

    /// Build a point interval `[v, v]`.
    pub fn point(v: f64) -> Self {
        Self::new(v, v)
    }

    /// True if the interval is empty (no real points inside).
    pub fn is_empty(&self) -> bool {
        self.lo > self.hi || self.lo.is_nan() || self.hi.is_nan()
    }

    /// Width of the interval (`hi - lo`).
    pub fn width(&self) -> f64 {
        self.hi - self.lo
    }

    /// Midpoint of the interval.
    pub fn midpoint(&self) -> f64 {
        (self.lo + self.hi) / 2.0
    }

    /// True if `x` lies inside the (closed) interval.
    pub fn contains(&self, x: f64) -> bool {
        x >= self.lo && x <= self.hi
    }

    /// Split the interval at the midpoint into two halves.
    pub fn split(&self) -> (Self, Self) {
        let mid = self.midpoint();
        (Self::new(self.lo, mid), Self::new(mid, self.hi))
    }

    /// Intersect two intervals; result is empty if they are disjoint.
    pub fn intersect(&self, other: &Self) -> Self {
        Self::new(self.lo.max(other.lo), self.hi.min(other.hi))
    }

    /// Convex hull (bounding interval containing both).
    pub fn hull(&self, other: &Self) -> Self {
        Self::new(self.lo.min(other.lo), self.hi.max(other.hi))
    }

    /// Forward `exp`: since `exp` is monotone, `exp([lo, hi]) = [exp(lo), exp(hi)]`.
    pub fn exp(&self) -> Self {
        Self::new(self.lo.exp(), self.hi.exp())
    }

    /// Forward `ln`: `ln([lo, hi])` for `lo > 0`, else empty.
    pub fn ln(&self) -> Self {
        if self.lo <= 0.0 || !self.lo.is_finite() || !self.hi.is_finite() {
            // Represent empty as `[+inf, -inf]` so `is_empty()` returns true.
            Self::new(f64::INFINITY, f64::NEG_INFINITY)
        } else {
            Self::new(self.lo.ln(), self.hi.ln())
        }
    }

    /// Inverse of `exp`: given output interval for exp(x), compute x's interval.
    /// Since exp is strictly monotone, this is just `ln([lo, hi])`.
    pub fn exp_inv(&self) -> Self {
        self.ln()
    }

    /// Inverse of `ln`: given output interval for ln(x), compute x's interval.
    /// Since ln is strictly monotone, this is just `exp([lo, hi])`.
    pub fn ln_inv(&self) -> Self {
        self.exp()
    }
}

// ----------------------------------------------------------------------------
// Interval domain and propagation
// ----------------------------------------------------------------------------

/// Result of a constraint propagation step.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PropResult {
    /// At least one variable interval was tightened.
    Changed,
    /// No change; propagation reached fixpoint.
    Stable,
    /// Constraint is provably unsatisfiable on current domain.
    Conflict,
}

/// Variable interval domain for EML constraint propagation.
#[derive(Clone, Debug)]
pub struct IntervalDomain {
    /// One interval per variable (index = variable index).
    pub vars: Vec<Interval>,
}

impl IntervalDomain {
    /// Build a domain from `(lo, hi)` bounds, padded with `(-10, 10)` for any
    /// missing slots, up to `num_vars` variables in total.
    pub fn new(bounds: &[(f64, f64)], num_vars: usize) -> Self {
        let vars = (0..num_vars)
            .map(|i| {
                if i < bounds.len() {
                    Interval::new(bounds[i].0, bounds[i].1)
                } else {
                    Interval::new(-10.0, 10.0)
                }
            })
            .collect();
        Self { vars }
    }

    /// True if any variable's interval is empty.
    pub fn is_empty(&self) -> bool {
        self.vars.iter().any(Interval::is_empty)
    }

    /// Propagate a constraint until fixpoint or conflict.
    ///
    /// Returns `Changed` if any variable was tightened, `Stable` if fixpoint
    /// reached without any change, `Conflict` if unsatisfiable.
    pub fn propagate(&mut self, c: &EmlConstraint) -> PropResult {
        const MAX_ITERATIONS: usize = 20;
        let mut changed_any = false;
        for _ in 0..MAX_ITERATIONS {
            let result = propagate_once(&mut self.vars, c);
            match result {
                PropResult::Conflict => return PropResult::Conflict,
                PropResult::Changed => {
                    changed_any = true;
                    continue;
                }
                PropResult::Stable => break,
            }
        }
        if changed_any {
            PropResult::Changed
        } else {
            PropResult::Stable
        }
    }
}

/// Forward-evaluate an EML subtree on interval-valued variables.
/// Returns the interval of possible output values.
fn eval_interval(node: &EmlNode, vars: &[Interval]) -> Interval {
    match node {
        EmlNode::One => Interval::new(1.0, 1.0),
        EmlNode::Const(v) => Interval::new(*v, *v),
        EmlNode::Var(i) => vars
            .get(*i)
            .copied()
            .unwrap_or_else(|| Interval::new(-10.0, 10.0)),
        EmlNode::Eml { left, right } => {
            let l = eval_interval(left, vars);
            let r = eval_interval(right, vars);
            if l.is_empty() || r.is_empty() {
                return Interval::new(f64::INFINITY, f64::NEG_INFINITY);
            }
            let exp_l = l.exp();
            let ln_r = r.ln();
            if ln_r.is_empty() {
                return Interval::new(f64::INFINITY, f64::NEG_INFINITY);
            }
            // eml(l, r) = exp(l) − ln(r)
            // min = exp(l.lo) − ln(r.hi)
            // max = exp(l.hi) − ln(r.lo)
            Interval::new(exp_l.lo - ln_r.hi, exp_l.hi - ln_r.lo)
        }
    }
}

/// HC4-revise style backward propagation: given the output constraint for a node,
/// tighten the variable domains.
fn backward_propagate(
    node: &EmlNode,
    vars: &mut [Interval],
    out_constraint: Interval,
) -> PropResult {
    if out_constraint.is_empty() {
        return PropResult::Conflict;
    }
    match node {
        EmlNode::Var(i) => {
            let idx = *i;
            let old = vars
                .get(idx)
                .copied()
                .unwrap_or_else(|| Interval::new(-10.0, 10.0));
            let new_iv = old.intersect(&out_constraint);
            if new_iv.is_empty() {
                return PropResult::Conflict;
            }
            if new_iv.lo > old.lo + 1e-12 || new_iv.hi < old.hi - 1e-12 {
                if idx < vars.len() {
                    vars[idx] = new_iv;
                }
                PropResult::Changed
            } else {
                PropResult::Stable
            }
        }
        EmlNode::One => {
            if !out_constraint.contains(1.0) {
                PropResult::Conflict
            } else {
                PropResult::Stable
            }
        }
        EmlNode::Const(v) => {
            if !out_constraint.contains(*v) {
                PropResult::Conflict
            } else {
                PropResult::Stable
            }
        }
        EmlNode::Eml { left, right } => {
            // out = exp(left) - ln(right)
            let left_iv = eval_interval(left, vars);
            let right_iv = eval_interval(right, vars);

            if left_iv.is_empty() || right_iv.is_empty() {
                return PropResult::Conflict;
            }

            let exp_l = left_iv.exp();
            let ln_r = right_iv.ln();

            if ln_r.is_empty() {
                return PropResult::Conflict;
            }

            // Forward output: [exp_l.lo - ln_r.hi, exp_l.hi - ln_r.lo]
            let forward_out = Interval::new(exp_l.lo - ln_r.hi, exp_l.hi - ln_r.lo);
            let out_c = out_constraint.intersect(&forward_out);
            if out_c.is_empty() {
                return PropResult::Conflict;
            }

            // Back-propagate to exp(left):
            // exp(left) = out + ln(right)
            // → exp(left) ∈ [out_c.lo + ln_r.lo, out_c.hi + ln_r.hi]
            let exp_l_back = Interval::new(out_c.lo + ln_r.lo, out_c.hi + ln_r.hi);
            let exp_l_c = exp_l.intersect(&exp_l_back);
            if exp_l_c.is_empty() {
                return PropResult::Conflict;
            }
            // left = ln(exp_l_c) (monotone inverse)
            let left_c = left_iv.intersect(&exp_l_c.exp_inv());

            // Back-propagate to ln(right):
            // ln(right) = exp(left) - out
            // → ln(right) ∈ [exp_l.lo - out_c.hi, exp_l.hi - out_c.lo]
            let ln_r_back = Interval::new(exp_l.lo - out_c.hi, exp_l.hi - out_c.lo);
            let ln_r_c = ln_r.intersect(&ln_r_back);
            if ln_r_c.is_empty() {
                return PropResult::Conflict;
            }
            // right = exp(ln_r_c) (monotone inverse)
            let right_c = right_iv.intersect(&ln_r_c.ln_inv());

            // Recurse
            let r_left = backward_propagate(left, vars, left_c);
            let r_right = backward_propagate(right, vars, right_c);

            match (r_left, r_right) {
                (PropResult::Conflict, _) | (_, PropResult::Conflict) => PropResult::Conflict,
                (PropResult::Changed, _) | (_, PropResult::Changed) => PropResult::Changed,
                _ => PropResult::Stable,
            }
        }
    }
}

/// Single-pass propagation: walk constraint, evaluate intervals, tighten.
fn propagate_once(vars: &mut [Interval], c: &EmlConstraint) -> PropResult {
    match c {
        EmlConstraint::EqZero(tree) => {
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            if v.lo > 0.0 || v.hi < 0.0 {
                return PropResult::Conflict;
            }
            // Backward: output must be exactly 0
            backward_propagate(&tree.root, vars, Interval::new(0.0, 0.0))
        }
        EmlConstraint::GtZero(tree) => {
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            if v.hi <= 0.0 {
                return PropResult::Conflict;
            }
            // Backward: output must be > 0; tighten lower bound
            let eps = f64::EPSILON * 4.0;
            let out_c = Interval::new(v.lo.max(eps), v.hi);
            backward_propagate(&tree.root, vars, out_c)
        }
        EmlConstraint::GeZero(tree) => {
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            if v.hi < 0.0 {
                return PropResult::Conflict;
            }
            // Backward: output must be >= 0
            let out_c = Interval::new(v.lo.max(0.0), v.hi);
            backward_propagate(&tree.root, vars, out_c)
        }
        EmlConstraint::LtZero(tree) => {
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            if v.lo >= 0.0 {
                return PropResult::Conflict;
            }
            let eps = f64::EPSILON * 4.0;
            let out_c = Interval::new(v.lo, v.hi.min(-eps));
            if out_c.is_empty() {
                return PropResult::Conflict;
            }
            backward_propagate(&tree.root, vars, out_c)
        }
        EmlConstraint::LeZero(tree) => {
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            if v.lo > 0.0 {
                return PropResult::Conflict;
            }
            let out_c = Interval::new(v.lo, v.hi.min(0.0));
            if out_c.is_empty() {
                return PropResult::Conflict;
            }
            backward_propagate(&tree.root, vars, out_c)
        }
        EmlConstraint::NeZero(tree) => {
            use super::helpers::NEZERO_EPS;
            let v = eval_interval(&tree.root, vars);
            if v.is_empty() {
                return PropResult::Conflict;
            }
            // Point interval at or near zero → Conflict.
            if (v.hi - v.lo) < NEZERO_EPS && v.lo.abs() < NEZERO_EPS {
                return PropResult::Conflict;
            }
            // Try to nudge bare Var(i) lower/upper bound away from zero.
            if let crate::tree::EmlNode::Var(i) = &*tree.root {
                let idx = *i;
                if let Some(iv) = vars.get(idx).copied() {
                    // Lower bound sits at zero, interval extends above — nudge up.
                    if iv.lo == 0.0 && iv.hi > 0.0 {
                        vars[idx] = Interval::new(0.0_f64.next_up(), iv.hi);
                        return PropResult::Changed;
                    }
                    // Upper bound sits at zero, interval extends below — nudge down.
                    if iv.hi == 0.0 && iv.lo < 0.0 {
                        vars[idx] = Interval::new(iv.lo, 0.0_f64.next_down());
                        return PropResult::Changed;
                    }
                }
            }
            // 0 in the strict interior, or non-Var tree — can't split a single interval.
            PropResult::Stable
        }
        EmlConstraint::Not(inner) => {
            let nnf = (**inner).clone().to_nnf();
            propagate_once(vars, &nnf)
        }
        EmlConstraint::And(constraints) => {
            let mut any_changed = false;
            for inner in constraints {
                match propagate_once(vars, inner) {
                    PropResult::Conflict => return PropResult::Conflict,
                    PropResult::Changed => any_changed = true,
                    PropResult::Stable => {}
                }
            }
            if any_changed {
                PropResult::Changed
            } else {
                PropResult::Stable
            }
        }
        EmlConstraint::Or(constraints) => {
            if constraints.is_empty() {
                return PropResult::Conflict;
            }
            // Save original domains so we never widen beyond them.
            let original: Vec<Interval> = vars.to_vec();

            // Propagate each branch independently, collect feasible survivors.
            let mut survivors: Vec<Vec<Interval>> = Vec::with_capacity(constraints.len());
            for inner in constraints {
                let mut branch = original.clone();
                let result = propagate_once(&mut branch, inner);
                if result != PropResult::Conflict {
                    survivors.push(branch);
                }
            }

            match survivors.len() {
                0 => PropResult::Conflict,
                1 => {
                    // Exactly one feasible branch: adopt it.
                    let branch = survivors.remove(0);
                    let mut any_changed = false;
                    for (i, (new_iv, orig_iv)) in branch.iter().zip(original.iter()).enumerate() {
                        if i < vars.len() {
                            // Intersect with original to be sound.
                            let tightened = new_iv.intersect(orig_iv);
                            if !tightened.is_empty()
                                && (tightened.lo > orig_iv.lo + 1e-12
                                    || tightened.hi < orig_iv.hi - 1e-12)
                            {
                                vars[i] = tightened;
                                any_changed = true;
                            }
                        }
                    }
                    if any_changed {
                        PropResult::Changed
                    } else {
                        PropResult::Stable
                    }
                }
                _ => {
                    // Multiple feasible branches: hull ∩ original — NEVER widen.
                    let mut any_changed = false;
                    for i in 0..vars.len() {
                        // Hull of the survivors for this variable.
                        let hull = survivors
                            .iter()
                            .filter_map(|b| b.get(i).copied())
                            .reduce(|acc, iv| acc.hull(&iv));
                        if let Some(hull_iv) = hull {
                            // Intersect hull with original to never widen.
                            let tightened = hull_iv.intersect(&original[i]);
                            if !tightened.is_empty()
                                && (tightened.lo > original[i].lo + 1e-12
                                    || tightened.hi < original[i].hi - 1e-12)
                            {
                                vars[i] = tightened;
                                any_changed = true;
                            }
                        }
                    }
                    if any_changed {
                        PropResult::Changed
                    } else {
                        PropResult::Stable
                    }
                }
            }
        }
        // Quantifiers: no outer tightening in v1 — sound conservative default.
        EmlConstraint::ForAll { .. } => PropResult::Stable,
        EmlConstraint::Exists { .. } => PropResult::Stable,
    }
}