lemma-engine 0.8.12

A language that means business.
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! World-based inverse reasoning for Lemma rules
//!
//! Determines what inputs produce desired outputs through world enumeration.
//! A "world" is a complete assignment of which branch is active for each rule.
//!
//! The main entry point is [`invert()`], which returns an [`InversionResponse`]
//! containing all valid solutions with their domains.

mod constraint;
mod derived;
mod domain;
mod solve;
mod target;
mod world;

pub use derived::{DerivedExpression, DerivedExpressionKind};
pub use domain::{extract_domains_from_constraint, Bound, Domain};
pub use target::{Target, TargetOp};
pub use world::World;

use crate::evaluation::operations::VetoType;
use crate::planning::semantics::{DataPath, Expression, LiteralValue, ValueKind};
use crate::planning::ExecutionPlan;
use crate::{Error, OperationResult};
use serde::ser::{Serialize, SerializeStruct, Serializer};
use std::collections::{HashMap, HashSet};

use world::{WorldEnumerator, WorldSolution};

// ============================================================================
// Solution and Response types
// ============================================================================

/// A single solution from inversion
///
/// Contains the outcome for a solution. For data constraints,
/// use the corresponding entry in `InversionResponse.domains`.
#[derive(Debug, Clone, serde::Serialize)]
pub struct Solution {
    /// The outcome (value or veto)
    pub outcome: OperationResult,
    /// The world (branch assignment) that produced this solution
    pub world: World,
    /// For underdetermined systems: the expression that must equal the target
    /// e.g., for `total = price * quantity` with target 100, this would be `price * quantity`
    #[serde(skip_serializing_if = "Option::is_none")]
    pub shape: Option<Expression>,
}

/// Response from inversion containing all valid solutions
#[derive(Debug, Clone)]
pub struct InversionResponse {
    /// All valid solutions
    pub solutions: Vec<Solution>,
    /// Domain constraints for each solution (indexed by solution index)
    pub domains: Vec<HashMap<DataPath, Domain>>,
    /// Data that still need values (appear in conditions but aren't fully constrained)
    pub undetermined_data: Vec<DataPath>,
    /// True if all data are fully constrained to specific values
    pub is_determined: bool,
}

impl InversionResponse {
    /// Create a new inversion response, computing metadata from solutions and domains
    pub fn new(solutions: Vec<Solution>, domains: Vec<HashMap<DataPath, Domain>>) -> Self {
        let undetermined_data = compute_undetermined_data(&domains);
        let is_determined = compute_is_determined(&domains);
        Self {
            solutions,
            domains,
            undetermined_data,
            is_determined,
        }
    }

    /// Check if the response is empty (no solutions)
    pub fn is_empty(&self) -> bool {
        self.solutions.is_empty()
    }

    /// Get the number of solutions
    pub fn len(&self) -> usize {
        self.solutions.len()
    }

    /// Iterate over solutions with their domains
    pub fn iter(&self) -> impl Iterator<Item = (&Solution, &HashMap<DataPath, Domain>)> {
        self.solutions.iter().zip(self.domains.iter())
    }
}

impl Serialize for InversionResponse {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("InversionResponse", 4)?;
        state.serialize_field("solutions", &self.solutions)?;

        let domains_serializable: Vec<HashMap<String, String>> = self
            .domains
            .iter()
            .map(|d| {
                d.iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect()
            })
            .collect();
        state.serialize_field("domains", &domains_serializable)?;

        let undetermined_serializable: Vec<String> = self
            .undetermined_data
            .iter()
            .map(|fp| fp.to_string())
            .collect();
        state.serialize_field("undetermined_data", &undetermined_serializable)?;
        state.serialize_field("is_determined", &self.is_determined)?;
        state.end()
    }
}

// ============================================================================
// Main inversion function
// ============================================================================

/// Invert a rule to find input domains that produce a desired outcome.
///
/// Given an execution plan and rule name, determines what values the unknown
/// data must have to produce the target outcome.
///
/// The `provided_data` set contains data paths that are fixed (user-provided values).
/// Only these data are substituted during hydration; other data values remain as
/// undetermined data for inversion.
///
/// Returns an [`InversionResponse`] containing all valid solutions.
pub fn invert(
    rule_name: &str,
    target: Target,
    plan: &ExecutionPlan,
    provided_data: &HashSet<DataPath>,
) -> Result<InversionResponse, Error> {
    let executable_rule = plan.get_rule(rule_name).ok_or_else(|| {
        Error::request(
            format!("Rule not found: {}.{}", plan.spec_name, rule_name),
            None::<String>,
        )
    })?;

    let rule_path = executable_rule.path.clone();

    // Enumerate all valid worlds for this rule
    let mut enumerator = WorldEnumerator::new(plan, &rule_path)?;
    let enumeration_result = enumerator.enumerate(provided_data)?;

    // Build Solution objects with domains
    let mut solutions = Vec::new();
    let mut all_domains = Vec::new();

    // Process literal solutions (outcomes that are concrete values)
    let filtered_literal_solutions =
        filter_literal_solutions_by_target(enumeration_result.literal_solutions, &target);

    for world_solution in filtered_literal_solutions {
        let constraint_domains = extract_domains_from_constraint(&world_solution.constraint)?;

        let solution = Solution {
            outcome: world_solution.outcome,
            world: world_solution.world,
            shape: None,
        };

        solutions.push(solution);
        all_domains.push(constraint_domains);
    }

    // Process arithmetic solutions (outcomes that are expressions needing algebraic solving)
    if let Some(OperationResult::Value(target_value)) = &target.outcome {
        // For equality targets, try algebraic solving first
        let solved_indices: std::collections::HashSet<usize> = if target.op == TargetOp::Eq {
            let algebraic_solutions = solve::solve_arithmetic_batch(
                enumeration_result.arithmetic_solutions.clone(),
                target_value,
                provided_data,
            );

            // Track which arithmetic solutions were successfully solved
            let indices: std::collections::HashSet<usize> = algebraic_solutions
                .iter()
                .filter_map(|(ws, _, _)| {
                    enumeration_result
                        .arithmetic_solutions
                        .iter()
                        .position(|orig| orig.world == ws.world)
                })
                .collect();

            // Add algebraically solved solutions (only if solved values satisfy constraints)
            for (world_solution, solved_outcome, solved_domains) in algebraic_solutions {
                let constraint_domains =
                    extract_domains_from_constraint(&world_solution.constraint)?;

                // Check if solved values are compatible with constraint domains
                let mut is_valid = true;
                for (data_path, solved_domain) in &solved_domains {
                    if let Some(constraint_domain) = constraint_domains.get(data_path) {
                        // Check if the solved value is within the constraint domain
                        if let Domain::Enumeration(values) = solved_domain {
                            for value in values.iter() {
                                if !constraint_domain.contains(value) {
                                    is_valid = false;
                                    break;
                                }
                            }
                        }
                    }
                    if !is_valid {
                        break;
                    }
                }

                if !is_valid {
                    continue; // Skip this solution as solved value violates constraint
                }

                let solved_outcome_result = OperationResult::Value(Box::new(solved_outcome));

                let mut combined_domains = constraint_domains;
                for (data_path, domain) in solved_domains {
                    combined_domains.insert(data_path, domain);
                }

                let solution = Solution {
                    outcome: solved_outcome_result,
                    world: world_solution.world,
                    shape: None,
                };

                solutions.push(solution);
                all_domains.push(combined_domains);
            }

            indices
        } else {
            std::collections::HashSet::new()
        };

        // For arithmetic solutions that couldn't be solved algebraically (multiple unknowns)
        // or for non-equality operators, add them with the shape representing the constraint
        for (idx, arith_solution) in enumeration_result.arithmetic_solutions.iter().enumerate() {
            if solved_indices.contains(&idx) {
                continue; // Already solved algebraically
            }

            // Add as underdetermined solution with shape
            let mut combined_domains = extract_domains_from_constraint(&arith_solution.constraint)?;

            // Extract unknown data from the shape expression and add them as Unconstrained
            let unknown_data =
                extract_data_paths_from_expression(&arith_solution.outcome_expression);
            for data_path in unknown_data {
                // Only add if not already constrained and not a provided data
                if !combined_domains.contains_key(&data_path) && !provided_data.contains(&data_path)
                {
                    combined_domains.insert(data_path, Domain::Unconstrained);
                }
            }

            let solution = Solution {
                outcome: OperationResult::Value(Box::new(target_value.as_ref().clone())),
                world: arith_solution.world.clone(),
                shape: Some(arith_solution.outcome_expression.clone()),
            };

            solutions.push(solution);
            all_domains.push(combined_domains);
        }
    }

    Ok(InversionResponse::new(solutions, all_domains))
}

// ============================================================================
// Helper functions
// ============================================================================

/// Filter literal solutions by the target outcome
fn filter_literal_solutions_by_target(
    solutions: Vec<WorldSolution>,
    target: &Target,
) -> Vec<WorldSolution> {
    let mut filtered = Vec::new();

    for solution in solutions {
        let matches = match (&target.outcome, &solution.outcome) {
            (None, _) => {
                // Target::any_value() - accept any outcome (including veto)
                true
            }
            (Some(OperationResult::Value(target_value)), OperationResult::Value(outcome_value)) => {
                // Specific value target, outcome is a value.
                // Compare by semantic value only (ValueKind), not full LiteralValue,
                // because type metadata (e.g. LemmaType.name) may differ between the
                // target (constructed externally) and the outcome (from constant folding).
                match target.op {
                    TargetOp::Eq => outcome_value.value == target_value.value,
                    TargetOp::Neq => outcome_value.value != target_value.value,
                    TargetOp::Lt => {
                        compare_values(outcome_value, target_value)
                            == Some(std::cmp::Ordering::Less)
                    }
                    TargetOp::Lte => {
                        let cmp = compare_values(outcome_value, target_value);
                        cmp == Some(std::cmp::Ordering::Less)
                            || cmp == Some(std::cmp::Ordering::Equal)
                    }
                    TargetOp::Gt => {
                        compare_values(outcome_value, target_value)
                            == Some(std::cmp::Ordering::Greater)
                    }
                    TargetOp::Gte => {
                        let cmp = compare_values(outcome_value, target_value);
                        cmp == Some(std::cmp::Ordering::Greater)
                            || cmp == Some(std::cmp::Ordering::Equal)
                    }
                }
            }
            (Some(OperationResult::Veto(target_reason)), OperationResult::Veto(outcome_reason)) => {
                match target_reason {
                    VetoType::UserDefined { message: None } => true, // Target any veto
                    VetoType::UserDefined {
                        message: Some(ref t_msg),
                    } => matches!(
                        outcome_reason,
                        VetoType::UserDefined {
                            message: Some(ref o_msg)
                        }
                        if o_msg == t_msg
                    ),
                    _ => false,
                }
            }
            _ => false, // Mismatch between value/veto targets and outcomes
        };

        if matches {
            filtered.push(solution);
        }
    }

    filtered
}

/// Compare two literal values for ordering
fn compare_values(a: &LiteralValue, b: &LiteralValue) -> Option<std::cmp::Ordering> {
    match (&a.value, &b.value) {
        (ValueKind::Number(a_val), ValueKind::Number(b_val)) => Some(a_val.cmp(b_val)),
        (ValueKind::Ratio(a_val, _), ValueKind::Ratio(b_val, _)) => Some(a_val.cmp(b_val)),
        (ValueKind::Scale(a_val, _), ValueKind::Scale(b_val, _)) => Some(a_val.cmp(b_val)),
        (ValueKind::Duration(a_val, unit_a), ValueKind::Duration(b_val, unit_b)) => {
            if unit_a == unit_b {
                Some(a_val.cmp(b_val))
            } else {
                None
            }
        }
        _ => None,
    }
}

/// Extract all DataPath references from a derived expression
fn extract_data_paths_from_expression(expr: &Expression) -> Vec<DataPath> {
    let mut set = std::collections::HashSet::new();
    expr.collect_data_paths(&mut set);
    set.into_iter().collect()
}

/// Compute the list of undetermined data from all solution domains
fn compute_undetermined_data(all_domains: &[HashMap<DataPath, Domain>]) -> Vec<DataPath> {
    let mut undetermined: HashSet<DataPath> = HashSet::new();

    for solution_domains in all_domains {
        for (data_path, domain) in solution_domains {
            let is_determined = matches!(
                domain,
                Domain::Enumeration(values) if values.len() == 1
            );
            if !is_determined {
                undetermined.insert(data_path.clone());
            }
        }
    }

    let mut result: Vec<DataPath> = undetermined.into_iter().collect();
    result.sort_by_key(|a| a.to_string());
    result
}

/// Check if all data across all solutions are fully determined
fn compute_is_determined(all_domains: &[HashMap<DataPath, Domain>]) -> bool {
    if all_domains.is_empty() {
        return true;
    }

    for solution_domains in all_domains {
        for domain in solution_domains.values() {
            let is_single_value = matches!(
                domain,
                Domain::Enumeration(values) if values.len() == 1
            );
            if !is_single_value {
                return false;
            }
        }
    }

    true
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parsing::ast::DateTimeValue;
    use crate::Engine;
    use rust_decimal::Decimal;
    use std::collections::HashMap;
    use std::sync::Arc;

    #[test]
    fn test_format_target_eq() {
        let target = Target::value(LiteralValue::number(Decimal::from(42)));
        let formatted = target.format();
        assert_eq!(formatted, "= 42");
    }

    #[test]
    fn test_format_target_any() {
        let target = Target::any_value();
        let formatted = target.format();
        assert_eq!(formatted, "= any");
    }

    #[test]
    fn test_compute_undetermined_data_empty() {
        let domains: Vec<HashMap<DataPath, Domain>> = vec![];
        let undetermined = compute_undetermined_data(&domains);
        assert!(undetermined.is_empty());
    }

    #[test]
    fn test_compute_undetermined_data_single_value() {
        let mut domain_map = HashMap::new();
        domain_map.insert(
            DataPath::new(vec![], "age".to_string()),
            Domain::Enumeration(Arc::new(vec![LiteralValue::number(Decimal::from(25))])),
        );
        let domains = vec![domain_map];
        let undetermined = compute_undetermined_data(&domains);
        assert!(undetermined.is_empty());
    }

    #[test]
    fn test_compute_undetermined_data_range() {
        let mut domain_map = HashMap::new();
        domain_map.insert(
            DataPath::new(vec![], "age".to_string()),
            Domain::Range {
                min: Bound::Exclusive(Arc::new(LiteralValue::number(Decimal::from(18)))),
                max: Bound::Unbounded,
            },
        );
        let domains = vec![domain_map];
        let undetermined = compute_undetermined_data(&domains);
        assert_eq!(undetermined.len(), 1);
    }

    #[test]
    fn test_compute_is_determined_empty() {
        let domains: Vec<HashMap<DataPath, Domain>> = vec![];
        assert!(compute_is_determined(&domains));
    }

    #[test]
    fn test_compute_is_determined_true() {
        let mut domain_map = HashMap::new();
        domain_map.insert(
            DataPath::new(vec![], "age".to_string()),
            Domain::Enumeration(Arc::new(vec![LiteralValue::number(Decimal::from(25))])),
        );
        let domains = vec![domain_map];
        assert!(compute_is_determined(&domains));
    }

    #[test]
    fn test_compute_is_determined_false() {
        let mut domain_map = HashMap::new();
        domain_map.insert(
            DataPath::new(vec![], "age".to_string()),
            Domain::Range {
                min: Bound::Exclusive(Arc::new(LiteralValue::number(Decimal::from(18)))),
                max: Bound::Unbounded,
            },
        );
        let domains = vec![domain_map];
        assert!(!compute_is_determined(&domains));
    }

    #[test]
    fn test_invert_strict_rule_reference_expands_constraints() {
        // Regression-style test: rule references should be expanded during inversion,
        // and veto conditions should constrain the domains.
        let code = r#"
spec example
data x: number
rule base: x
  unless x > 3 then veto "too much"
  unless x < 0 then veto "too little"

rule another: base
  unless x > 5 then veto "way too much"
"#;

        let mut engine = Engine::new();
        engine
            .load(code, crate::SourceType::Labeled("test.lemma"))
            .unwrap();
        let now = DateTimeValue::now();

        let inv = engine
            .invert(
                "example",
                Some(&now),
                "another",
                Target::value(LiteralValue::number(3.into())),
                HashMap::new(),
            )
            .expect("inversion should succeed");

        assert!(!inv.is_empty(), "expected at least one solution");

        let x = DataPath::new(vec![], "x".to_string());
        let three = LiteralValue::number(3.into());

        // For target value 3, x must be exactly 3 (not just within a broad range).
        for (_solution, domains) in inv.iter() {
            let d = domains.get(&x).expect("domain for x should exist");
            assert!(
                d.contains(&three),
                "x domain should contain 3. Domain: {}",
                d
            );
        }
    }

    #[test]
    fn test_invert_strict_no_solution_when_value_is_blocked_by_veto() {
        let code = r#"
spec example
data x: number
rule base: x
  unless x > 3 then veto "too much"
  unless x < 0 then veto "too little"

rule another: base
  unless x > 5 then veto "way too much"
"#;

        let mut engine = Engine::new();
        engine
            .load(code, crate::SourceType::Labeled("test.lemma"))
            .unwrap();
        let now = DateTimeValue::now();

        let inv = engine
            .invert(
                "example",
                Some(&now),
                "another",
                Target::value(LiteralValue::number(7.into())),
                HashMap::new(),
            )
            .expect("inversion should succeed");

        assert!(
            inv.is_empty(),
            "Should have no solutions because another can never equal 7"
        );
    }

    #[test]
    fn test_invert_strict_veto_target_constrains_domain() {
        let code = r#"
spec example
data x: number
rule base: x
  unless x > 3 then veto "too much"
  unless x < 0 then veto "too little"

rule another: base
  unless x > 5 then veto "way too much"
"#;

        let mut engine = Engine::new();
        engine
            .load(code, crate::SourceType::Labeled("test.lemma"))
            .unwrap();
        let now = DateTimeValue::now();

        let inv = engine
            .invert(
                "example",
                Some(&now),
                "another",
                Target::veto(Some("way too much".to_string())),
                HashMap::new(),
            )
            .expect("inversion should succeed");

        assert!(!inv.is_empty(), "expected solutions for veto query");

        let x = DataPath::new(vec![], "x".to_string());
        let five = LiteralValue::number(5.into());
        let six = LiteralValue::number(6.into());

        for (solution, domains) in inv.iter() {
            assert_eq!(
                solution.outcome,
                OperationResult::Veto(VetoType::UserDefined {
                    message: Some("way too much".to_string()),
                }),
                "Expected solution outcome to be veto('way too much'), got: {:?}",
                solution.outcome
            );

            let d = domains.get(&x).expect("domain for x should exist");
            match d {
                Domain::Range { min, max } => {
                    assert!(
                        matches!(min, Bound::Exclusive(v) if v.as_ref() == &five),
                        "Expected min bound to be (5), got: {}",
                        d
                    );
                    assert!(
                        matches!(max, Bound::Unbounded),
                        "Expected max bound to be +inf, got: {}",
                        d
                    );
                }
                other => panic!("Expected range domain for x, got: {}", other),
            }
            assert!(
                !d.contains(&five),
                "x=5 should not be in veto('way too much') domain. Domain: {}",
                d
            );
            assert!(
                d.contains(&six),
                "x=6 should be in veto('way too much') domain. Domain: {}",
                d
            );
        }
    }

    #[test]
    fn test_invert_strict_any_veto_target_matches_all_veto_ranges() {
        let code = r#"
spec example
data x: number
rule base: x
  unless x > 3 then veto "too much"
  unless x < 0 then veto "too little"

rule another: base
  unless x > 5 then veto "way too much"
"#;

        let mut engine = Engine::new();
        engine
            .load(code, crate::SourceType::Labeled("test.lemma"))
            .unwrap();

        let now = DateTimeValue::now();
        let inv = engine
            .invert(
                "example",
                Some(&now),
                "another",
                Target::any_veto(),
                HashMap::new(),
            )
            .expect("inversion should succeed");

        assert!(!inv.is_empty(), "expected solutions for any-veto query");

        let x = DataPath::new(vec![], "x".to_string());
        let minus_one = LiteralValue::number((-1).into());
        let zero = LiteralValue::number(0.into());
        let two = LiteralValue::number(2.into());
        let three = LiteralValue::number(3.into());
        let four = LiteralValue::number(4.into());
        let five = LiteralValue::number(5.into());
        let six = LiteralValue::number(6.into());

        let mut saw_too_little = false;
        let mut saw_too_much = false;
        let mut saw_way_too_much = false;

        for (solution, domains) in inv.iter() {
            let d = domains.get(&x).expect("domain for x should exist");
            assert!(
                !d.contains(&two),
                "x=2 should not be in any-veto domain. Domain: {}",
                d
            );

            match &solution.outcome {
                OperationResult::Veto(VetoType::UserDefined {
                    message: Some(ref msg),
                }) if msg == "too little" => {
                    saw_too_little = true;

                    match d {
                        Domain::Range { min, max } => {
                            assert!(
                                matches!(min, Bound::Unbounded),
                                "Expected min bound to be -inf for 'too little', got: {}",
                                d
                            );
                            assert!(
                                matches!(max, Bound::Exclusive(v) if v.as_ref() == &zero),
                                "Expected max bound to be (0) for 'too little', got: {}",
                                d
                            );
                        }
                        other => panic!("Expected range domain for x, got: {}", other),
                    }

                    assert!(
                        d.contains(&minus_one),
                        "x=-1 should be in veto('too little') domain. Domain: {}",
                        d
                    );
                    assert!(
                        !d.contains(&zero),
                        "x=0 should not be in veto('too little') domain. Domain: {}",
                        d
                    );
                }
                OperationResult::Veto(VetoType::UserDefined {
                    message: Some(ref msg),
                }) if msg == "too much" => {
                    saw_too_much = true;

                    match d {
                        Domain::Range { min, max } => {
                            assert!(
                                matches!(min, Bound::Exclusive(v) if v.as_ref() == &three),
                                "Expected min bound to be (3) for 'too much', got: {}",
                                d
                            );
                            assert!(
                                matches!(max, Bound::Inclusive(v) if v.as_ref() == &five),
                                "Expected max bound to be [5] for 'too much', got: {}",
                                d
                            );
                        }
                        other => panic!("Expected range domain for x, got: {}", other),
                    }

                    assert!(
                        d.contains(&four),
                        "x=4 should be in veto('too much') domain. Domain: {}",
                        d
                    );
                    assert!(
                        d.contains(&five),
                        "x=5 should be in veto('too much') domain. Domain: {}",
                        d
                    );
                    assert!(
                        !d.contains(&three),
                        "x=3 should not be in veto('too much') domain. Domain: {}",
                        d
                    );
                    assert!(
                        !d.contains(&six),
                        "x=6 should not be in veto('too much') domain. Domain: {}",
                        d
                    );
                }
                OperationResult::Veto(VetoType::UserDefined {
                    message: Some(ref msg),
                }) if msg == "way too much" => {
                    saw_way_too_much = true;

                    match d {
                        Domain::Range { min, max } => {
                            assert!(
                                matches!(min, Bound::Exclusive(v) if v.as_ref() == &five),
                                "Expected min bound to be (5) for 'way too much', got: {}",
                                d
                            );
                            assert!(
                                matches!(max, Bound::Unbounded),
                                "Expected max bound to be +inf for 'way too much', got: {}",
                                d
                            );
                        }
                        other => panic!("Expected range domain for x, got: {}", other),
                    }

                    assert!(
                        d.contains(&six),
                        "x=6 should be in veto('way too much') domain. Domain: {}",
                        d
                    );
                    assert!(
                        !d.contains(&five),
                        "x=5 should not be in veto('way too much') domain. Domain: {}",
                        d
                    );
                }
                OperationResult::Veto(other) => {
                    panic!("Unexpected veto in any-veto results: {:?}", other)
                }
                OperationResult::Value(v) => {
                    panic!("Unexpected value result in any-veto results: {:?}", v)
                }
            }
        }

        assert!(
            saw_too_little,
            "Expected at least one veto('too little') solution"
        );
        assert!(
            saw_too_much,
            "Expected at least one veto('too much') solution"
        );
        assert!(
            saw_way_too_much,
            "Expected at least one veto('way too much') solution"
        );
    }
}