1
#![allow(dead_code)]
2
use crate::ast::enumerated::EnumVariant;
3
use crate::ast::{
4
    AbstractLiteral, Atom, DeclarationKind, Expression as Expr, Field, Literal as Lit, Metadata,
5
    Moo,
6
    comprehension::{Comprehension, ComprehensionQualifier},
7
    matrix,
8
};
9
use crate::bug_assert;
10
use crate::into_matrix;
11
use itertools::{Itertools as _, izip};
12
use std::cmp::Ordering as CmpOrdering;
13
use std::collections::HashSet;
14

            
15
use super::partial_eval::{run_partial_evaluator, run_partial_evaluator_local};
16

            
17
3610
pub(crate) fn factorial_i32(n: i32) -> Option<i32> {
18
3610
    if n < 0 {
19
        return None;
20
3610
    }
21

            
22
3610
    (1..=n).try_fold(1_i32, i32::checked_mul)
23
3610
}
24

            
25
fn eval_constant_set(expr: &Expr) -> Option<Vec<Lit>> {
26
    let Lit::AbstractLiteral(AbstractLiteral::Set(values)) = eval_constant(expr)? else {
27
        return None;
28
    };
29

            
30
    Some(values)
31
}
32

            
33
/// Compare literals only when their outer Essence literal kinds agree.
34
///
35
/// Inner types are guaranteed by expression type checking; keeping this guard here also makes
36
/// direct evaluator calls on unlike primitive or abstract literal kinds safely unevaluable.
37
10903962
fn equal_constant_literals(lhs: &Lit, rhs: &Lit) -> Option<bool> {
38
10903962
    match (lhs, rhs) {
39
18
        (Lit::EnumVariant(lhs), Lit::EnumVariant(rhs)) if lhs.ty == rhs.ty => {
40
18
            Some(lhs.variant == rhs.variant)
41
        }
42
12
        (Lit::Unnamed(lhs), Lit::Unnamed(rhs)) if lhs.ty == rhs.ty => Some(lhs.value == rhs.value),
43
        (Lit::Int(_), Lit::Int(_)) | (Lit::Bool(_), Lit::Bool(_)) => {
44
10554993
            Some(lhs.essence_cmp(rhs) == CmpOrdering::Equal)
45
        }
46
        (Lit::AbstractLiteral(lhs_abstract), Lit::AbstractLiteral(rhs_abstract))
47
348877
            if std::mem::discriminant(lhs_abstract) == std::mem::discriminant(rhs_abstract) =>
48
        {
49
348877
            Some(lhs.essence_cmp(rhs) == CmpOrdering::Equal)
50
        }
51
62
        _ => None,
52
    }
53
10903962
}
54

            
55
/// Preserve already-distinct literal order; constant duplicates denote the same set member.
56
614261893
fn normalise_constant_set(mut literal: Lit) -> Lit {
57
614261893
    if let Lit::AbstractLiteral(AbstractLiteral::Set(items)) = &mut literal
58
4013988
        && super::literals::distinct_literal_count(items) != items.len()
59
    {
60
42659
        items.sort_by(Lit::essence_cmp);
61
143564
        items.dedup_by(|a, b| a.essence_cmp(b).is_eq());
62
614219234
    }
63
614261893
    literal
64
614261893
}
65

            
66
/// Simplify an expression to a constant using only constants already present at this node.
67
///
68
/// This is intended for the rewriter: child expressions should have been simplified by the
69
/// scheduler before their parent is considered. Use [`eval_constant`] when a caller explicitly
70
/// wants recursive evaluation of an arbitrary expression.
71
1758822330
pub fn eval_constant_local(expr: &Expr) -> Option<Lit> {
72
1758822330
    if !has_only_locally_evaluable_operands(expr) {
73
524516847
        return None;
74
1234305483
    }
75

            
76
1234305483
    eval_constant(expr)
77
1758822330
}
78

            
79
/// Applies the evaluator normalisation hook to a focused expression.
80
///
81
/// Evaluators are privileged simplifications, not ordinary rewrite rules. The rewriter invokes
82
/// this hook before normal rule scheduling and immediately after a successful ordinary rule,
83
/// walking upward while evaluation keeps simplifying parents. This exploits the semantic property
84
/// that local constant and partial evaluation is always preferable to trying lower-priority rules,
85
/// while avoiding millions of failed universal `constant_evaluator` rule attempts.
86
///
87
/// Away from [`Expr::Root`], the hook is pure and local: it does not create auxiliaries, mutate
88
/// the symbol table, or recursively inspect arbitrary descendants. Children are expected to have
89
/// been normalised by the scheduler before their parent is evaluated.
90
///
91
/// At [`Expr::Root`], a selective deep pass runs over top-level constraints (skipping solver-flat
92
/// forms). Callers that know which root child changed should prefer
93
/// [`normalise_root_selective_deep_expr`] with `only_constraint` so sibling constraints are not
94
/// re-traversed. Local root-list reshaping (flatten top-level `and`) is intentionally deferred to
95
/// [`finish_root_evaluator_normalisation`]: doing it mid-loop materialises huge root lists and
96
/// explodes worklist rule attempts.
97
1799353889
pub fn normalise_evaluator_local(expr: &Expr) -> Option<Expr> {
98
1799353889
    match expr {
99
19428180
        Expr::Root(_, exprs) => normalise_root_constraints_selective_deep(exprs, None),
100
        // Focused `AbstractLiteral` literals must not repeatedly refold to themselves; parents
101
        // still see the `Atomic(Literal(...))` form when the hook walks upward.
102
15818837
        Expr::Atomic(_, Atom::Literal(Lit::AbstractLiteral(_))) => None,
103
1764106872
        _ => fold_constant_expression_local(expr).or_else(|| {
104
1753811998
            run_partial_evaluator_local(expr)
105
1753811998
                .ok()
106
6361557
                .map(|reduction| reduction.new_expression)
107
1753811998
        }),
108
    }
109
17671473
    .filter(|new_expr| new_expr != expr)
110
1799353889
}
111

            
112
/// Applies local root-list partial evaluation (strip `true`, propagate `false`, flatten `and`s).
113
1759576
pub fn normalise_root_constraints_local(exprs: &[Expr]) -> Option<Expr> {
114
1759576
    if exprs.is_empty() {
115
        return Some(Expr::Root(Metadata::new(), vec![true.into()]));
116
1759576
    }
117

            
118
1759576
    let root = Expr::Root(Metadata::new(), exprs.to_vec());
119
1759576
    run_partial_evaluator_local(&root)
120
1759576
        .ok()
121
312528
        .map(|reduction| reduction.new_expression)
122
312528
        .filter(|new_root| new_root != &root)
123
1759576
}
124

            
125
/// Deep-normalises selected non-flat top-level constraints in `root`.
126
5
pub fn normalise_root_selective_deep_expr(
127
5
    root: &Expr,
128
5
    only_constraint: Option<usize>,
129
5
) -> Option<Expr> {
130
5
    let Expr::Root(_, exprs) = root else {
131
        return None;
132
    };
133

            
134
5
    normalise_root_constraints_selective_deep(exprs, only_constraint)
135
5
}
136

            
137
4
pub fn normalise_root_constraints_deep(root: &Expr) -> Option<Expr> {
138
4
    normalise_root_selective_deep_expr(root, None)
139
4
}
140

            
141
/// Finishes evaluator normalisation on the model root after rewriting completes.
142
///
143
/// Applies local root-list partial evaluation to a fixpoint (strip `true`, propagate `false`,
144
/// flatten top-level `and`). Deep evaluation of individual constraints already runs during
145
/// rewriting via [`normalise_evaluator_local`] / [`normalise_root_selective_deep_expr`]; this
146
/// finish pass must not be inlined into the mid-loop Root hook because flattening `and` early
147
/// explodes worklist size on large expansions.
148
1447048
pub fn finish_root_evaluator_normalisation(root: &Expr) -> Option<Expr> {
149
1447048
    let Expr::Root(_, exprs) = root else {
150
        return None;
151
    };
152

            
153
1447048
    let mut current = Expr::Root(Metadata::new(), exprs.clone());
154
1447048
    let mut changed = false;
155
1759574
    while let Expr::Root(_, current_exprs) = &current {
156
1759574
        let Some(next) = normalise_root_constraints_local(current_exprs) else {
157
1447048
            break;
158
        };
159
312526
        current = next;
160
312526
        changed = true;
161
    }
162
1447048
    changed.then_some(current)
163
1447048
}
164

            
165
/// Deep-normalises a single top-level constraint, or returns `None` if it is already normal.
166
///
167
/// Callers that know which root child changed should prefer this over rebuilding the whole root:
168
/// the root hook runs after every rewrite, so touching siblings there costs O(model size) per
169
/// rewrite.
170
196219011
pub fn normalise_root_constraint_deep(constraint: &Expr) -> Option<Expr> {
171
196219011
    if constraint_skips_deep_root_normalisation(constraint) {
172
271
        return None;
173
196218740
    }
174

            
175
196218740
    normalise_constraint_deep_to_fixpoint(constraint)
176
196219011
}
177

            
178
19428185
fn normalise_root_constraints_selective_deep(
179
19428185
    exprs: &[Expr],
180
19428185
    only_constraint: Option<usize>,
181
19428185
) -> Option<Expr> {
182
19428185
    if exprs.is_empty() {
183
988661
        return Some(Expr::Root(Metadata::new(), vec![true.into()]));
184
18439524
    }
185

            
186
    // Normalise first and only rebuild the root list once something has actually changed. Building
187
    // the replacement eagerly clones every sibling constraint on each call, which dominates the
188
    // post-rewrite root hook on models with many top-level constraints.
189
18439524
    let mut normalised: Vec<(usize, Expr)> = Vec::new();
190
18439524
    for (index, constraint) in exprs.iter().enumerate() {
191
196218402
        if only_constraint.is_some_and(|only| only != index) {
192
1
            continue;
193
196218401
        }
194

            
195
196218401
        if let Some(replacement) = normalise_root_constraint_deep(constraint) {
196
39404
            normalised.push((index, replacement));
197
196178997
        }
198
    }
199

            
200
18439524
    if normalised.is_empty() {
201
18413140
        return None;
202
26384
    }
203

            
204
26384
    let mut constraints = exprs.to_vec();
205
26384
    for (index, replacement) in normalised {
206
39404
        constraints[index] = replacement;
207
    }
208

            
209
26384
    Some(Expr::Root(Metadata::new(), constraints))
210
19428185
}
211

            
212
/// Whether a top-level constraint has already been lowered to solver-flat form.
213
///
214
/// Deep root normalisation must not rewrite these: doing so can disturb auxiliaries introduced
215
/// for Minion (for example chained `FlatProductEq` constraints) or repeat expensive work.
216
196219011
fn constraint_skips_deep_root_normalisation(expr: &Expr) -> bool {
217
196219011
    match expr {
218
        Expr::FlatProductEq(_, _, _, _)
219
        | Expr::FlatSumLeq(_, _, _)
220
        | Expr::FlatSumGeq(_, _, _)
221
        | Expr::FlatMinEq(_, _, _)
222
        | Expr::FlatIneq(_, _, _, _)
223
        | Expr::FlatMinusEq(_, _, _)
224
        | Expr::FlatAbsEq(_, _, _)
225
        | Expr::FlatAllDiff(_, _)
226
        | Expr::FlatTable(..)
227
        | Expr::FlatShortTable(..)
228
        | Expr::SmtDistinct(_, _)
229
        | Expr::FlatWeightedSumLeq(_, _, _, _)
230
        | Expr::FlatWeightedSumGeq(_, _, _, _)
231
        | Expr::FlatWatchedLiteral(_, _, _)
232
        | Expr::MinionDivEqUndefZero(_, _, _, _)
233
        | Expr::MinionModuloEqUndefZero(_, _, _, _)
234
        | Expr::MinionPow(_, _, _, _)
235
        | Expr::MinionReify(_, _, _)
236
        | Expr::MinionReifyImply(_, _, _)
237
        | Expr::MinionWInIntervalSet(_, _, _)
238
        | Expr::MinionWInSet(_, _, _)
239
271
        | Expr::MinionElementOne(_, _, _, _) => true,
240
6700185
        Expr::AuxDeclaration(_, _, inner) => matches!(
241
6700185
            inner.as_ref(),
242
            Expr::Product(_, _) | Expr::FlatProductEq(_, _, _, _)
243
        ),
244
189518555
        _ => false,
245
    }
246
196219011
}
247

            
248
154527914
fn fold_constant_expression_deep(expr: &Expr) -> Option<Expr> {
249
154527914
    let constant = eval_constant(expr)?;
250
39405
    fold_constant_expression(expr, constant).filter(|folded| folded != expr)
251
154527914
}
252

            
253
196218740
fn normalise_constraint_deep_to_fixpoint(constraint: &Expr) -> Option<Expr> {
254
196218740
    if matches!(constraint, Expr::Atomic(_, Atom::Literal(_))) {
255
41730230
        return None;
256
154488510
    }
257

            
258
154488510
    let mut current = constraint.clone();
259
154488510
    let mut changed = false;
260

            
261
154527914
    while let Some(step) = fold_constant_expression_deep(&current)
262
154527912
        .or_else(|| partial_evaluator_deep_step(&current))
263
39404
        .filter(|step| step != &current)
264
    {
265
39404
        current = step;
266
39404
        changed = true;
267
    }
268

            
269
154488510
    changed
270
154488510
        .then_some(current)
271
39404
        .filter(|current| current != constraint)
272
196218740
}
273

            
274
154527912
fn partial_evaluator_deep_step(expr: &Expr) -> Option<Expr> {
275
154527912
    run_partial_evaluator(expr)
276
154527912
        .ok()
277
39402
        .map(|reduction| reduction.new_expression)
278
39402
        .filter(|new_expr| new_expr != expr)
279
154527912
}
280

            
281
/// Constant-folds `expr` locally unless doing so would inline a referenced matrix literal.
282
1764106872
fn fold_constant_expression_local(expr: &Expr) -> Option<Expr> {
283
1764106872
    let constant = match expr {
284
        // Comprehensions are atomic in arena traversal; evaluate them in one step here rather than
285
        // via operand checks that would repeat the same work for every parent.
286
5284542
        Expr::Comprehension(_, _) => eval_constant(expr)?,
287
1758822330
        _ => eval_constant_local(expr)?,
288
    };
289
213985369
    fold_constant_expression(expr, constant).filter(|folded| folded != expr)
290
1764106872
}
291

            
292
214024774
fn fold_constant_expression(expr: &Expr, constant: Lit) -> Option<Expr> {
293
214024774
    if let Expr::Atomic(_, Atom::Literal(existing)) = expr
294
203613921
        && existing == &constant
295
    {
296
203613921
        return None;
297
10410853
    }
298

            
299
10410853
    if matches!(
300
10410853
        (expr, constant.clone()),
301
        (
302
            Expr::Atomic(_, Atom::Reference(_)),
303
            Lit::AbstractLiteral(AbstractLiteral::Matrix(_, _))
304
        )
305
    ) {
306
100130
        return None;
307
10310723
    }
308

            
309
10310723
    let folded = Expr::Atomic(Metadata::new(), Atom::Literal(constant));
310
10310723
    if let Expr::TypeAnnotation(_, _, domain) = expr
311
9145
        && let Expr::Atomic(
312
9145
            _,
313
9145
            Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Matrix(elems, _))),
314
9145
        ) = &folded
315
9145
        && elems.is_empty()
316
    {
317
9145
        return Some(Expr::TypeAnnotation(
318
9145
            Metadata::new(),
319
9145
            Moo::new(folded),
320
9145
            domain.clone(),
321
9145
        ));
322
10301578
    }
323

            
324
10301578
    if let Expr::DomainAnnotation(_, _, domain) = expr
325
6702
        && let Expr::Atomic(
326
6702
            _,
327
6702
            Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Matrix(elems, _))),
328
6702
        ) = &folded
329
6702
        && elems.is_empty()
330
    {
331
6702
        return Some(Expr::DomainAnnotation(
332
6702
            Metadata::new(),
333
6702
            Moo::new(folded),
334
6702
            domain.clone(),
335
6702
        ));
336
10294876
    }
337

            
338
10294876
    if let Expr::Comprehension(_, comprehension) = expr
339
38138
        && let Expr::Atomic(
340
38138
            _,
341
38138
            Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Matrix(elems, _))),
342
38138
        ) = &folded
343
38138
        && elems.is_empty()
344
6702
        && let Some(domain) = comprehension.domain_of()
345
    {
346
6702
        return Some(Expr::DomainAnnotation(
347
6702
            Metadata::new(),
348
6702
            Moo::new(folded),
349
6702
            domain,
350
6702
        ));
351
10288174
    }
352

            
353
10288174
    Some(folded)
354
214024774
}
355

            
356
1758822330
fn has_only_locally_evaluable_operands(expr: &Expr) -> bool {
357
1758822330
    match expr {
358
203574518
        Expr::Atomic(_, Atom::Literal(_)) => true,
359
        // Whether a reference resolves to a constant is the evaluator's result, not an
360
        // applicability question. Resolving it here would clone/evaluate the value letting and
361
        // then make eval_constant do the same work again.
362
623274755
        Expr::Atomic(_, Atom::Reference(_)) => true,
363
275286747
        Expr::AbstractLiteral(_, lit) => abstract_literal_children_are_locally_evaluable(lit),
364
        Expr::TypeAnnotation(_, inner, _) | Expr::DomainAnnotation(_, inner, _) => {
365
15871
            is_locally_evaluable_expr(inner.as_ref())
366
        }
367
        Expr::Comprehension(_, _) | Expr::Root(_, _) => false,
368
        // Visit children by reference. `Uniplate::children()` clones the whole child list, so on a
369
        // wide node (e.g. `or` over a large matrix) this test alone costs O(subtree); the evaluator
370
        // hook runs it on every ancestor after every rewrite, which makes rewriting quadratic.
371
        _ => {
372
656670439
            let mut all_constant = true;
373
890829326
            expr.for_each_expr_child(&mut |child| {
374
890829326
                all_constant = all_constant && is_locally_evaluable_expr(child);
375
890829326
            });
376
656670439
            all_constant
377
        }
378
    }
379
1758822330
}
380

            
381
1836344702
fn is_locally_evaluable_expr(expr: &Expr) -> bool {
382
1836344702
    match expr {
383
132476631
        Expr::Atomic(_, Atom::Literal(_)) => true,
384
908115185
        Expr::Atomic(_, Atom::Reference(_)) => true,
385
271220168
        Expr::AbstractLiteral(_, lit) => abstract_literal_children_are_locally_evaluable(lit),
386
        Expr::TypeAnnotation(_, inner, _) | Expr::DomainAnnotation(_, inner, _) => {
387
15871
            is_locally_evaluable_expr(inner.as_ref())
388
        }
389
524516847
        _ => false,
390
    }
391
1836344702
}
392

            
393
546506915
fn abstract_literal_children_are_locally_evaluable(lit: &AbstractLiteral<Expr>) -> bool {
394
546506915
    match lit {
395
        AbstractLiteral::Set(items)
396
        | AbstractLiteral::MSet(items)
397
        | AbstractLiteral::Tuple(items)
398
540150582
        | AbstractLiteral::Matrix(items, _) => items.iter().all(is_locally_evaluable_expr),
399
19995
        AbstractLiteral::Record(fields) => fields
400
19995
            .iter()
401
39990
            .all(|field| is_locally_evaluable_expr(&field.value)),
402
5686082
        AbstractLiteral::Sequence(items) => items.iter().all(is_locally_evaluable_expr),
403
2263
        AbstractLiteral::Function(items) => items
404
2263
            .iter()
405
6789
            .all(|(from, to)| is_locally_evaluable_expr(from) && is_locally_evaluable_expr(to)),
406
2263
        AbstractLiteral::Relation(items) => items
407
2263
            .iter()
408
6789
            .all(|tuple| tuple.iter().all(is_locally_evaluable_expr)),
409
558
        AbstractLiteral::Partition(parts) => parts
410
558
            .iter()
411
558
            .all(|part| part.iter().all(is_locally_evaluable_expr)),
412
17422
        AbstractLiteral::Permutation(cycles) => cycles
413
17422
            .iter()
414
17887
            .all(|cycle| cycle.iter().all(is_locally_evaluable_expr)),
415
627750
        AbstractLiteral::Variant(field) => is_locally_evaluable_expr(&field.value),
416
    }
417
546506915
}
418

            
419
/// Simplify an expression to a constant if possible
420
/// Returns:
421
/// `None` if the expression cannot be simplified to a constant (e.g. if it contains a variable)
422
/// `Some(Const)` if the expression can be simplified to a constant
423
2624701709
pub fn eval_constant(expr: &Expr) -> Option<Lit> {
424
2624701709
    match expr {
425
        Expr::TypeAnnotation(_, expr, _) | Expr::DomainAnnotation(_, expr, _) => {
426
31766
            eval_constant(expr)
427
        }
428
        Expr::Supset(_, a, b) => {
429
            let (
430
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
431
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
432
372
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
433
            else {
434
                return None;
435
            };
436

            
437
372
            let a_set: HashSet<Lit> = a.iter().cloned().collect();
438
372
            let b_set: HashSet<Lit> = b.iter().cloned().collect();
439

            
440
372
            if a_set.difference(&b_set).count() > 0 {
441
279
                Some(Lit::Bool(a_set.is_superset(&b_set)))
442
            } else {
443
93
                Some(Lit::Bool(false))
444
            }
445
        }
446
        Expr::SupsetEq(_, a, b) => {
447
            let (
448
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
449
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
450
372
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
451
            else {
452
                return None;
453
            };
454

            
455
372
            Some(Lit::Bool(
456
372
                a.iter()
457
372
                    .cloned()
458
372
                    .collect::<HashSet<Lit>>()
459
372
                    .is_superset(&b.iter().cloned().collect::<HashSet<Lit>>()),
460
372
            ))
461
        }
462
        Expr::Subset(_, a, b) => {
463
            let (
464
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
465
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
466
103571
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
467
            else {
468
                return None;
469
            };
470

            
471
465
            let a_set: HashSet<Lit> = a.iter().cloned().collect();
472
465
            let b_set: HashSet<Lit> = b.iter().cloned().collect();
473

            
474
465
            if b_set.difference(&a_set).count() > 0 {
475
372
                Some(Lit::Bool(a_set.is_subset(&b_set)))
476
            } else {
477
93
                Some(Lit::Bool(false))
478
            }
479
        }
480
        Expr::SubsetEq(_, a, b) => {
481
            let (
482
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
483
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
484
191515
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
485
            else {
486
                return None;
487
            };
488

            
489
403
            Some(Lit::Bool(
490
403
                a.iter()
491
403
                    .cloned()
492
403
                    .collect::<HashSet<Lit>>()
493
403
                    .is_subset(&b.iter().cloned().collect::<HashSet<Lit>>()),
494
403
            ))
495
        }
496
        Expr::Intersect(_, a, b) => {
497
            let (
498
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
499
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
500
35340
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
501
            else {
502
                return None;
503
            };
504

            
505
16833
            let mut res: Vec<Lit> = Vec::new();
506
16833
            for lit in a {
507
30907
                if b.contains(&lit) && !res.contains(&lit) {
508
16647
                    res.push(lit);
509
14260
                }
510
            }
511
16833
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(res)))
512
        }
513
        Expr::Difference(_, a, b) => {
514
            let (
515
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
516
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
517
5661
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
518
            else {
519
                return None;
520
            };
521

            
522
4495
            let mut res: Vec<Lit> = Vec::new();
523
4495
            for lit in a {
524
13485
                if !b.contains(&lit) && !res.contains(&lit) {
525
8990
                    res.push(lit);
526
4495
                }
527
            }
528
4495
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(res)))
529
        }
530
        Expr::Union(_, a, b) => {
531
            let (
532
                Lit::AbstractLiteral(AbstractLiteral::Set(a)),
533
                Lit::AbstractLiteral(AbstractLiteral::Set(b)),
534
18119
            ) = (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?)
535
            else {
536
                return None;
537
            };
538

            
539
14446
            let mut res: Vec<Lit> = Vec::new();
540
14446
            for lit in a {
541
28334
                res.push(lit);
542
            }
543
14446
            for lit in b {
544
28334
                if !res.contains(&lit) {
545
18569
                    res.push(lit);
546
9765
                }
547
            }
548
14446
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(res)))
549
        }
550
        Expr::In(_, a, b) => {
551
6425736
            let member = eval_constant(a)?;
552
5052839
            let collection = eval_constant(b)?;
553
97506
            let values = generator_values_from_constant_collection(&collection)?;
554
167311
            Some(Lit::Bool(values.iter().any(|value| {
555
167311
                value.essence_cmp(&member) == CmpOrdering::Equal
556
167311
            })))
557
        }
558
        Expr::FromSolution(_, _) => None,
559
        Expr::DominanceRelation(_, _) => None,
560
        Expr::InDomain(_, e, domain) => {
561
30194
            let Expr::Atomic(_, Atom::Literal(lit)) = e.as_ref() else {
562
30194
                return None;
563
            };
564

            
565
            domain.contains(lit).ok().map(Into::into)
566
        }
567
565552146
        Expr::Atomic(_, Atom::Literal(c)) => Some(normalise_constant_set(c.clone())),
568
        Expr::Atomic(_, Atom::Reference(reference)) => {
569
1146150280
            reference.resolve_constant().map(normalise_constant_set)
570
        }
571
106433118
        Expr::AbstractLiteral(_, a) => Some(normalise_constant_set(Lit::AbstractLiteral(
572
106433118
            a.clone().into_literals()?,
573
        ))),
574
        Expr::Comprehension(_, comprehension) => {
575
9618945
            eval_constant_comprehension(comprehension.as_ref())
576
        }
577
723851
        Expr::RecordField(_, rec, fld_name) => match eval_constant(rec.as_ref())? {
578
            Lit::AbstractLiteral(AbstractLiteral::Record(ents)) => {
579
9083
                for Field { name, value } in ents {
580
13609
                    if name.eq(fld_name) {
581
9083
                        return Some(value);
582
4526
                    }
583
                }
584
                None
585
            }
586
36209
            Lit::AbstractLiteral(AbstractLiteral::Variant(field)) if field.name == *fld_name => {
587
36209
                Some(field.value.clone())
588
            }
589
            _ => None,
590
        },
591
        Expr::UnsafeIndex(_, subject, indices) | Expr::SafeIndex(_, subject, indices) => {
592
86172290
            let subject: Lit = eval_constant(subject.as_ref())?;
593
14676004
            let indices: Vec<Lit> = indices
594
14676004
                .iter()
595
14676004
                .map(eval_constant)
596
14676004
                .collect::<Option<Vec<Lit>>>()?;
597

            
598
13997692
            match subject {
599
                Lit::AbstractLiteral(subject @ AbstractLiteral::Matrix(_, _)) => {
600
10762656
                    matrix::flatten_enumerate(subject)
601
40408339
                        .find(|(i, _)| i == &indices)
602
                        .map(|(_, x)| x)
603
                }
604
                Lit::AbstractLiteral(subject @ AbstractLiteral::Tuple(_)) => {
605
3235036
                    let AbstractLiteral::Tuple(elems) = subject else {
606
                        return None;
607
                    };
608

            
609
                    bug_assert!(indices.len() == 1, "nested tuples not supported yet");
610

            
611
3235036
                    let Lit::Int(index) = indices[0].clone() else {
612
                        return None;
613
                    };
614

            
615
3235036
                    if elems.len() < index as usize || index < 1 {
616
                        return None;
617
3235036
                    }
618

            
619
                    // -1 for 0-indexing vs 1-indexing
620
3235036
                    let item = elems[index as usize - 1].clone();
621

            
622
3235036
                    Some(item)
623
                }
624
                Lit::AbstractLiteral(subject @ AbstractLiteral::Record(_)) => {
625
                    let AbstractLiteral::Record(elems) = subject else {
626
                        return None;
627
                    };
628

            
629
                    bug_assert!(indices.len() == 1, "nested record not supported yet");
630

            
631
                    let Lit::Int(index) = indices[0].clone() else {
632
                        return None;
633
                    };
634

            
635
                    if elems.len() < index as usize || index < 1 {
636
                        return None;
637
                    }
638

            
639
                    // -1 for 0-indexing vs 1-indexing
640
                    let item = elems[index as usize - 1].clone();
641
                    Some(item.value)
642
                }
643
                _ => None,
644
            }
645
        }
646
        Expr::UnsafeSlice(_, subject, indices) | Expr::SafeSlice(_, subject, indices) => {
647
1702232
            let subject: Lit = eval_constant(subject.as_ref())?;
648
2263
            let Lit::AbstractLiteral(subject @ AbstractLiteral::Matrix(_, _)) = subject else {
649
                return None;
650
            };
651

            
652
2263
            let hole_dim = indices
653
2263
                .iter()
654
2263
                .cloned()
655
4526
                .position(|x| x.is_none())
656
2263
                .expect("slice expression should have a hole dimension");
657

            
658
2263
            let missing_domain = matrix::index_domains(&subject)[hole_dim].clone();
659

            
660
2263
            let indices: Vec<Option<Lit>> = indices
661
2263
                .iter()
662
2263
                .cloned()
663
4526
                .map(|x| {
664
                    // the outer option represents success of this iterator, the inner the index
665
                    // slice.
666
4526
                    match x {
667
2263
                        Some(x) => eval_constant(&x).map(Some),
668
2263
                        None => Some(None),
669
                    }
670
4526
                })
671
                .collect::<Option<Vec<Option<Lit>>>>()?;
672

            
673
2263
            let indices_in_slice: Vec<Vec<Lit>> = missing_domain
674
2263
                .values()
675
2263
                .ok()?
676
6789
                .map(|i| {
677
6789
                    let mut indices = indices.clone();
678
6789
                    indices[hole_dim] = Some(i);
679
                    // These unwraps will only fail if we have multiple holes.
680
                    // As this is invalid, panicking is fine.
681
13578
                    indices.into_iter().map(|x| x.unwrap()).collect_vec()
682
6789
                })
683
                .collect_vec();
684

            
685
            // Note: indices_in_slice is not necessarily sorted, so this is the best way.
686
2263
            let elems = matrix::flatten_enumerate(subject)
687
20367
                .filter(|(i, _)| indices_in_slice.contains(i))
688
                .map(|(_, elem)| elem)
689
                .collect();
690

            
691
2263
            Some(Lit::AbstractLiteral(into_matrix![elems]))
692
        }
693
366978
        Expr::Abs(_, e) => un_op::<i32, i32>(|a| a.abs(), e).map(Lit::Int),
694
58147621
        Expr::Eq(_, a, b) => Some(Lit::Bool(equal_constant_literals(
695
58147621
            &eval_constant(a)?,
696
7614071
            &eval_constant(b)?,
697
31
        )?)),
698
14032788
        Expr::Neq(_, a, b) => Some(Lit::Bool(!equal_constant_literals(
699
14032788
            &eval_constant(a)?,
700
4378836
            &eval_constant(b)?,
701
31
        )?)),
702
10388836
        Expr::Lt(_, a, b) => bin_op::<i32, bool>(|a, b| a < b, a, b).map(Lit::Bool),
703
2380938
        Expr::Gt(_, a, b) => bin_op::<i32, bool>(|a, b| a > b, a, b).map(Lit::Bool),
704
24386468
        Expr::Leq(_, a, b) => bin_op::<i32, bool>(|a, b| a <= b, a, b).map(Lit::Bool),
705
3349386
        Expr::Geq(_, a, b) => bin_op::<i32, bool>(|a, b| a >= b, a, b).map(Lit::Bool),
706
117494731
        Expr::Not(_, expr) => un_op::<bool, bool>(|e| !e, expr).map(Lit::Bool),
707
        Expr::And(_, e) => {
708
47975262
            vec_lit_op::<bool, bool>(|e| e.iter().all(|&e| e), e.as_ref()).map(Lit::Bool)
709
        }
710
1321778
        Expr::Table(_, _, _) | Expr::FlatTable(..) | Expr::FlatShortTable(..) => None,
711
545600
        Expr::NegativeTable(_, _, _) | Expr::ShortTable(_, _, _) => None,
712
730825
        Expr::AtLeast(_, _, _, _) => None,
713
153884
        Expr::AtMost(_, _, _, _) => None,
714
6169
        Expr::Gcc(_, _, _, _) | Expr::GccWeak(_, _, _, _) => None,
715
1578
        Expr::Root(_, _) => None,
716
        Expr::Or(_, es) => {
717
            // possibly cheating; definitely should be in partial eval instead
718
132129365
            for e in es.unwrap_list_cow()?.iter() {
719
353740105
                if let Expr::Atomic(_, Atom::Literal(Lit::Bool(true))) = e {
720
92831
                    return Some(Lit::Bool(true));
721
353647274
                };
722
            }
723

            
724
131544649
            vec_lit_op::<bool, bool>(|e| e.iter().any(|&e| e), es.as_ref()).map(Lit::Bool)
725
        }
726
        // A `catchUndef` still standing at constant-folding time has no bubble to consult, so the
727
        // inner expression is total here and the default is unreachable.
728
484034
        Expr::CatchUndef(_, a, _) => eval_constant(a),
729
15010547
        Expr::Imply(_, a, b) => bin_op::<bool, bool>(|a, b| !a || b, a, b).map(Lit::Bool),
730
9027729
        Expr::Iff(_, a, b) => bin_op::<bool, bool>(|a, b| a == b, a, b).map(Lit::Bool),
731
32819269
        Expr::Sum(_, exprs) => vec_lit_op::<i32, i32>(|e| e.iter().sum(), exprs).map(Lit::Int),
732
        Expr::Product(_, exprs) => {
733
15018347
            vec_lit_op::<i32, i32>(|e| e.iter().product(), exprs).map(Lit::Int)
734
        }
735
        Expr::FlatIneq(_, a, b, c) => {
736
494679
            let a: i32 = a.try_into().ok()?;
737
450538
            let b: i32 = b.try_into().ok()?;
738
            let c: i32 = c.try_into().ok()?;
739

            
740
            Some(Lit::Bool(a <= b + c))
741
        }
742
        Expr::FlatSumGeq(_, exprs, a) => {
743
193213
            let sum = exprs.iter().try_fold(0, |acc, atom: &Atom| {
744
193213
                let n: i32 = atom.try_into().ok()?;
745
80480
                let acc = acc + n;
746
80480
                Some(acc)
747
193213
            })?;
748

            
749
31
            Some(Lit::Bool(sum >= a.try_into().ok()?))
750
        }
751
        Expr::FlatSumLeq(_, exprs, a) => {
752
156231
            let sum = exprs.iter().try_fold(0, |acc, atom: &Atom| {
753
156231
                let n: i32 = atom.try_into().ok()?;
754
61217
                let acc = acc + n;
755
61217
                Some(acc)
756
156231
            })?;
757

            
758
31
            Some(Lit::Bool(sum >= a.try_into().ok()?))
759
        }
760
        Expr::FlatMinEq(_, vars, result) => {
761
341
            let min = vars
762
341
                .iter()
763
372
                .try_fold(None, |acc: Option<i32>, atom: &Atom| {
764
372
                    let n: i32 = atom.try_into().ok()?;
765
31
                    Some(Some(acc.map_or(n, |m| m.min(n))))
766
372
                })??;
767
            let result: i32 = result.try_into().ok()?;
768
            Some(Lit::Bool(min == result))
769
        }
770
        Expr::Min(_, e) => {
771
755843
            opt_vec_lit_op::<i32, i32>(|e| e.iter().min().copied(), e.as_ref()).map(Lit::Int)
772
        }
773
        Expr::Max(_, e) => {
774
640946
            opt_vec_lit_op::<i32, i32>(|e| e.iter().max().copied(), e.as_ref()).map(Lit::Int)
775
        }
776
        Expr::UnsafeDiv(_, a, b) | Expr::SafeDiv(_, a, b) => {
777
14868402
            super::floor_div(unwrap_expr::<i32>(a)?, unwrap_expr::<i32>(b)?).map(Lit::Int)
778
        }
779
        Expr::UnsafeMod(_, a, b) | Expr::SafeMod(_, a, b) => {
780
8403711
            super::floor_mod(unwrap_expr::<i32>(a)?, unwrap_expr::<i32>(b)?).map(Lit::Int)
781
        }
782
12338
        Expr::Substring(_, s, t) => match (s.as_ref(), t.as_ref()) {
783
            (
784
                Expr::Atomic(_, Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Sequence(s)))),
785
                Expr::Atomic(_, Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Sequence(t)))),
786
            ) => {
787
                if s.len() > t.len() {
788
                    return Some(Lit::Bool(false));
789
                }
790

            
791
                let found = t.windows(s.len()).any(|window| window == s.as_slice());
792
                Some(Lit::Bool(found))
793
            }
794
12338
            _ => None,
795
        },
796
2697
        Expr::Subsequence(_, s, t) => match (s.as_ref(), t.as_ref()) {
797
            (
798
                Expr::Atomic(_, Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Sequence(s)))),
799
                Expr::Atomic(_, Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Sequence(t)))),
800
            ) => {
801
                let mut i = 0;
802
                let mut j = 0;
803

            
804
                while i < s.len() && j < t.len() {
805
                    if s[i] == t[j] {
806
                        i += 1;
807
                    }
808
                    j += 1;
809
                }
810

            
811
                Some(Lit::Bool(i == s.len()))
812
            }
813
2697
            _ => None,
814
        },
815
        Expr::MinionDivEqUndefZero(_, a, b, c) => {
816
            // div always rounds down
817
133780
            let a: i32 = a.try_into().ok()?;
818
31
            let b: i32 = b.try_into().ok()?;
819
            let c: i32 = c.try_into().ok()?;
820

            
821
            if b == 0 {
822
                return None;
823
            }
824

            
825
            let a = a as f32;
826
            let b = b as f32;
827
            let div: i32 = (a / b).floor() as i32;
828
            Some(Lit::Bool(div == c))
829
        }
830
258667
        Expr::Bubble(_, a, b) => bin_op::<bool, bool>(|a, b| a && b, a, b).map(Lit::Bool),
831
        Expr::MinionReify(_, a, b) => {
832
2035
            let result = eval_constant(a)?;
833

            
834
1153
            let result: bool = result.try_into().ok()?;
835
1153
            let b: bool = b.try_into().ok()?;
836

            
837
            Some(Lit::Bool(b == result))
838
        }
839
        Expr::MinionReifyImply(_, a, b) => {
840
648
            let result = eval_constant(a)?;
841

            
842
            let result: bool = result.try_into().ok()?;
843
            let b: bool = b.try_into().ok()?;
844

            
845
            if b {
846
                Some(Lit::Bool(result))
847
            } else {
848
                Some(Lit::Bool(true))
849
            }
850
        }
851
        Expr::MinionModuloEqUndefZero(_, a, b, c) => {
852
            // From Savile Row. Same semantics as division.
853
            //
854
            //   a - (b * floor(a/b))
855
            //
856
            // We don't use % as it has the same semantics as /. We don't use / as we want to round
857
            // down instead, not towards zero.
858

            
859
136012
            let a: i32 = a.try_into().ok()?;
860
31
            let b: i32 = b.try_into().ok()?;
861
            let c: i32 = c.try_into().ok()?;
862

            
863
            if b == 0 {
864
                return None;
865
            }
866

            
867
            let modulo = super::floor_mod(a, b)?;
868
            Some(Lit::Bool(modulo == c))
869
        }
870
        Expr::MinionPow(_, a, b, c) => {
871
            // only available for positive a b c
872

            
873
1969
            let a: i32 = a.try_into().ok()?;
874
            let b: i32 = b.try_into().ok()?;
875
            let c: i32 = c.try_into().ok()?;
876

            
877
            if a <= 0 {
878
                return None;
879
            }
880

            
881
            if b <= 0 {
882
                return None;
883
            }
884

            
885
            if c <= 0 {
886
                return None;
887
            }
888

            
889
            Some(Lit::Bool(a ^ b == c))
890
        }
891
279
        Expr::MinionWInSet(_, _, _) => None,
892
        Expr::MinionWInIntervalSet(_, x, intervals) => {
893
651
            let x_lit: &Lit = x.try_into().ok()?;
894

            
895
            let x_lit = match x_lit.clone() {
896
                Lit::Int(i) => Some(i),
897
                Lit::Bool(true) => Some(1),
898
                Lit::Bool(false) => Some(0),
899
                _ => None,
900
            }?;
901

            
902
            let mut intervals = intervals.iter();
903
            while let Some(lower) = intervals.next() {
904
                let Some(upper) = intervals.next() else {
905
                    break;
906
                };
907
                if &x_lit >= lower && &x_lit <= upper {
908
                    return Some(Lit::Bool(true));
909
                }
910
            }
911

            
912
            Some(Lit::Bool(false))
913
        }
914
        Expr::Flatten(_, _, _) => {
915
            // TODO
916
3254814
            None
917
        }
918
        // Constant folding treats the SMT-native form exactly like the one it came from.
919
        Expr::AllDiff(_, e) | Expr::SmtDistinct(_, e) => {
920
1603955
            let es = e.unwrap_list_cow()?;
921
31248
            let mut lits: HashSet<Lit> = HashSet::new();
922
31248
            for expr in es.iter() {
923
36146
                let Expr::Atomic(_, Atom::Literal(x)) = expr else {
924
28892
                    return None;
925
                };
926
7254
                match x {
927
                    Lit::Int(_) | Lit::Bool(_) | Lit::EnumVariant(_) | Lit::Unnamed(_) => {
928
7254
                        if lits.contains(x) {
929
                            return Some(Lit::Bool(false));
930
                        } else {
931
7254
                            lits.insert(x.clone());
932
                        }
933
                    }
934
                    Lit::AbstractLiteral(_) => return None, // Reject AbstractLiteral cases
935
                }
936
            }
937
2356
            Some(Lit::Bool(true))
938
        }
939
        Expr::FlatAllDiff(_, es) => {
940
8047
            let mut lits: HashSet<Lit> = HashSet::new();
941
8047
            for atom in es {
942
8047
                let Atom::Literal(x) = atom else {
943
8047
                    return None;
944
                };
945

            
946
                match x {
947
                    Lit::Int(_) | Lit::Bool(_) | Lit::EnumVariant(_) | Lit::Unnamed(_) => {
948
                        if lits.contains(x) {
949
                            return Some(Lit::Bool(false));
950
                        } else {
951
                            lits.insert(x.clone());
952
                        }
953
                    }
954
                    Lit::AbstractLiteral(_) => return None, // Reject AbstractLiteral cases
955
                }
956
            }
957
            Some(Lit::Bool(true))
958
        }
959
1422486
        Expr::FlatWatchedLiteral(_, _, _) => None,
960
6731700
        Expr::AuxDeclaration(_, _, _) => None,
961
394220
        Expr::Neg(_, a) => match eval_constant(a.as_ref())? {
962
108239
            Lit::Int(a) => Some(Lit::Int(-a)),
963
            _ => None,
964
        },
965
2697
        Expr::Factorial(_, a) => match eval_constant(a.as_ref())? {
966
2697
            Lit::Int(a) => factorial_i32(a).map(Lit::Int),
967
            _ => None,
968
        },
969
20828744
        Expr::Minus(_, a, b) => bin_op::<i32, i32>(|a, b| a - b, a, b).map(Lit::Int),
970
        Expr::FlatMinusEq(_, a, b) => {
971
2480
            let a: i32 = a.try_into().ok()?;
972
            let b: i32 = b.try_into().ok()?;
973
            Some(Lit::Bool(a == -b))
974
        }
975
        Expr::FlatProductEq(_, a, b, c) => {
976
8298
            let a: i32 = a.try_into().ok()?;
977
            let b: i32 = b.try_into().ok()?;
978
            let c: i32 = c.try_into().ok()?;
979
            Some(Lit::Bool(a * b == c))
980
        }
981
        Expr::FlatWeightedSumLeq(_, cs, vs, total) => {
982
14227
            let cs: Vec<i32> = cs
983
14227
                .iter()
984
40983
                .map(|x| TryInto::<i32>::try_into(x).ok())
985
                .collect::<Option<Vec<i32>>>()?;
986
14227
            let vs: Vec<i32> = vs
987
14227
                .iter()
988
23248
                .map(|x| TryInto::<i32>::try_into(x).ok())
989
14227
                .collect::<Option<Vec<i32>>>()?;
990
            let total: i32 = total.try_into().ok()?;
991

            
992
            let sum: i32 = izip!(cs, vs).fold(0, |acc, (c, v)| acc + (c * v));
993

            
994
            Some(Lit::Bool(sum <= total))
995
        }
996
        Expr::FlatWeightedSumGeq(_, cs, vs, total) => {
997
13576
            let cs: Vec<i32> = cs
998
13576
                .iter()
999
39495
                .map(|x| TryInto::<i32>::try_into(x).ok())
                .collect::<Option<Vec<i32>>>()?;
13576
            let vs: Vec<i32> = vs
13576
                .iter()
22628
                .map(|x| TryInto::<i32>::try_into(x).ok())
13576
                .collect::<Option<Vec<i32>>>()?;
            let total: i32 = total.try_into().ok()?;
            let sum: i32 = izip!(cs, vs).fold(0, |acc, (c, v)| acc + (c * v));
            Some(Lit::Bool(sum >= total))
        }
        Expr::FlatAbsEq(_, x, y) => {
465
            let x: i32 = x.try_into().ok()?;
31
            let y: i32 = y.try_into().ok()?;
            Some(Lit::Bool(x == y.abs()))
        }
        Expr::UnsafePow(_, a, b) | Expr::SafePow(_, a, b) => {
1686137
            let a: &Atom = a.try_into().ok()?;
1591680
            let a: i32 = a.try_into().ok()?;
1174154
            let b: &Atom = b.try_into().ok()?;
1174154
            let b: i32 = b.try_into().ok()?;
1173782
            if (a != 0 || b != 0) && b >= 0 {
1173782
                Some(Lit::Int(a.pow(b as u32)))
            } else {
                None
            }
        }
        Expr::Metavar(_, _) => None,
26480
        Expr::MinionElementOne(_, _, _, _) => None,
        Expr::ToInt(_, expression) => {
108686242
            let lit = eval_constant(expression.as_ref())?;
33125420
            match lit {
                Lit::Int(_) => Some(lit),
17246160
                Lit::Bool(true) => Some(Lit::Int(1)),
15879260
                Lit::Bool(false) => Some(Lit::Int(0)),
                _ => None,
            }
        }
234442
        Expr::SatObjective(..) => None,
        Expr::SATInt(_, _, _, _) => {
            // TODO: If this SATInt is composed of literals, we should evaluate it back to an
            // integer literal.
            //
            // This is important because `is_all_constant` currently returns true for SATInts
            // containing no references. If we don't evaluate them here, bubble rules will skip
            // them (thinking they'll be constant-folded later), but they'll actually reach
            // the solver adaptors as un-encoded unsafe operations, causing panics.
38654835
            None
        }
        Expr::PairwiseSum(_, a, b) => {
            match (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?) {
                (Lit::Int(a_int), Lit::Int(b_int)) => Some(Lit::Int(a_int + b_int)),
                _ => None,
            }
        }
        Expr::PairwiseProduct(_, a, b) => {
            match (eval_constant(a.as_ref())?, eval_constant(b.as_ref())?) {
                (Lit::Int(a_int), Lit::Int(b_int)) => Some(Lit::Int(a_int * b_int)),
                _ => None,
            }
        }
        Expr::Defined(_, f) => {
21359
            let Lit::AbstractLiteral(AbstractLiteral::Function(pairs)) = eval_constant(f)? else {
                return None;
            };
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(
                pairs.into_iter().map(|(key, _)| key).collect(),
            )))
        }
        Expr::Range(_, f) => {
47802
            let Lit::AbstractLiteral(AbstractLiteral::Function(pairs)) = eval_constant(f)? else {
                return None;
            };
            let mut values = Vec::new();
            for (_, value) in pairs {
                if !values.contains(&value) {
                    values.push(value);
                }
            }
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(values)))
        }
        Expr::Image(_, f, arg) => {
998950
            let arg = eval_constant(arg)?;
184518
            match eval_constant(f)? {
4526
                Lit::AbstractLiteral(AbstractLiteral::Function(pairs)) => pairs
4526
                    .into_iter()
11315
                    .find(|(key, _)| *key == arg)
                    .map(|(_, value)| value),
                // A sequence is a function from int(1..n), so applying it reads the entry at
                // that one-based position. Out of range is undefined, so unevaluable.
                Lit::AbstractLiteral(AbstractLiteral::Sequence(values)) => {
                    let Lit::Int(index) = arg else {
                        return None;
                    };
                    usize::try_from(index)
                        .ok()
                        .filter(|index| *index >= 1)
                        .and_then(|index| values.get(index - 1).cloned())
                }
                // Cycle notation: find which cycle (if any) mentions `arg` and return the next
                // element in it (wrapping around); an element mentioned in no cycle is an
                // implicit fixed point, mapping to itself.
                Lit::AbstractLiteral(AbstractLiteral::Permutation(cycles)) => {
11321
                    for cycle in &cycles {
20384
                        if let Some(pos) = cycle.iter().position(|x| *x == arg) {
6794
                            return Some(cycle[(pos + 1) % cycle.len()].clone());
4529
                        }
                    }
4527
                    Some(arg)
                }
                _ => None,
            }
        }
        Expr::PreImage(_, f, img) => {
            let Lit::AbstractLiteral(AbstractLiteral::Function(pairs)) = eval_constant(f)? else {
                return None;
            };
            let img = eval_constant(img)?;
            let mut keys = Vec::new();
            for (key, value) in pairs {
                if value == img && !keys.contains(&key) {
                    keys.push(key);
                }
            }
            Some(Lit::AbstractLiteral(AbstractLiteral::Set(keys)))
        }
        // Not yet needed by any in-scope function case; the partial evaluator already refuses
        // these gracefully (Err(RuleNotApplicable)) rather than panicking.
        Expr::ImageSet(_, _, _) => None,
1550
        Expr::Inverse(_, _, _) => None,
        Expr::PermInverse(_, _) => None,
        Expr::Compose(_, _, _) => None,
        Expr::Restrict(_, _, _) => None,
        Expr::ToSet(_, _) => None,
        Expr::ToMSet(_, _) => None,
        Expr::ToRelation(_, _) => None,
        Expr::Active(_, variant, alternative) => {
            let Lit::AbstractLiteral(AbstractLiteral::Variant(field)) =
206586
                eval_constant(variant.as_ref())?
            else {
                return None;
            };
72418
            Some(Lit::Bool(field.name == *alternative))
        }
        Expr::RelationProj(_, _, _) => todo!(),
        // Not yet needed by any in-scope partition case; the partial evaluator already refuses
        // these gracefully (Err(RuleNotApplicable)) rather than panicking. Constant partition
        // literal equality/membership already works via the generic Literal equality path above,
        // without going through these operators at all.
        Expr::Apart(_, _, _) => None,
        Expr::Together(_, _, _) => None,
        Expr::Participants(_, _) => None,
        Expr::Party(_, _, _) => None,
        Expr::Parts(_, _) => None,
        Expr::Card(_, collection) => {
367271
            let Lit::AbstractLiteral(collection) = eval_constant(collection)? else {
                return None;
            };
153615
            let length = match collection {
141301
                AbstractLiteral::Set(values) => super::literals::distinct_literal_count(&values),
                AbstractLiteral::MSet(values)
                | AbstractLiteral::Sequence(values)
5525
                | AbstractLiteral::Matrix(values, _) => values.len(),
                AbstractLiteral::Function(entries) => entries.len(),
2263
                AbstractLiteral::Relation(entries) => entries.len(),
                // A permutation's cardinality is its numMoved count -- how many elements are
                // mentioned in some cycle -- not the number of cycles, matching the same
                // "unmentioned = fixed point" convention used everywhere else for permutations
                // (contains()'s size check, PermutationAsFunction's own numMoved structural
                // constraint).
4526
                AbstractLiteral::Permutation(cycles) => cycles.iter().flatten().count(),
                _ => return None,
            };
153615
            i32::try_from(length).ok().map(Lit::Int)
        }
        Expr::LexLt(_, a, b) => {
795429
            let lt = vec_expr_pairs_op::<i32, _>(a, b, |pairs, (a_len, b_len)| {
372
                pairs
372
                    .iter()
744
                    .find_map(|(a, b)| match a.cmp(b) {
186
                        CmpOrdering::Less => Some(true),     // First difference is <
                        CmpOrdering::Greater => Some(false), // First difference is >
558
                        CmpOrdering::Equal => None,          // No difference
744
                    })
372
                    .unwrap_or(a_len < b_len) // [1,1] <lex [1,1,x]
795057
            })?;
372
            Some(lt.into())
        }
        Expr::LexLeq(_, a, b) => {
819547
            let lt = vec_expr_pairs_op::<i32, _>(a, b, |pairs, (a_len, b_len)| {
186
                pairs
186
                    .iter()
372
                    .find_map(|(a, b)| match a.cmp(b) {
186
                        CmpOrdering::Less => Some(true),
                        CmpOrdering::Greater => Some(false),
186
                        CmpOrdering::Equal => None,
372
                    })
186
                    .unwrap_or(a_len <= b_len) // [1,1] <=lex [1,1,x]
819361
            })?;
186
            Some(lt.into())
        }
64511
        Expr::LexGt(_, a, b) => eval_constant(&Expr::LexLt(Metadata::new(), b.clone(), a.clone())),
        Expr::LexGeq(_, a, b) => {
139376
            eval_constant(&Expr::LexLeq(Metadata::new(), b.clone(), a.clone()))
        }
        Expr::FlatLexLt(_, a, b) => {
9703
            let lt = atoms_pairs_op::<i32, _>(a, b, |pairs, (a_len, b_len)| {
                pairs
                    .iter()
                    .find_map(|(a, b)| match a.cmp(b) {
                        CmpOrdering::Less => Some(true),
                        CmpOrdering::Greater => Some(false),
                        CmpOrdering::Equal => None,
                    })
                    .unwrap_or(a_len < b_len)
9703
            })?;
            Some(lt.into())
        }
        Expr::FlatLexLeq(_, a, b) => {
3286
            let lt = atoms_pairs_op::<i32, _>(a, b, |pairs, (a_len, b_len)| {
                pairs
                    .iter()
                    .find_map(|(a, b)| match a.cmp(b) {
                        CmpOrdering::Less => Some(true),
                        CmpOrdering::Greater => Some(false),
                        CmpOrdering::Equal => None,
                    })
                    .unwrap_or(a_len <= b_len)
3286
            })?;
            Some(lt.into())
        }
        Expr::Pred(_, expr) => {
            let ev = unwrap_expr::<EnumVariant>(expr)?;
            ev.pred().map(Lit::EnumVariant)
        }
        Expr::Succ(_, expr) => {
24
            let ev = unwrap_expr::<EnumVariant>(expr)?;
24
            ev.succ().map(Lit::EnumVariant)
        }
        Expr::AllDifferentExcept(_, _, _) | Expr::ElementId(_, _, _) | Expr::IndexOf(_, _, _) => {
1384894
            None
        }
        // Needs the target's domain to expand, which this function has no access to; the
        // fallback rewrite rule (passes::attribute_as_constraint) handles expansion instead.
        Expr::AttributeAsConstraint(_, _, _, _)
        | Expr::SatElement(_, _, _, _)
1379128
        | Expr::SatAllDifferentComparisons(_, _) => None,
    }
2624701709
}
117861709
pub fn un_op<T, A>(f: fn(T) -> A, a: &Expr) -> Option<A>
117861709
where
117861709
    T: TryFrom<Lit>,
{
117861709
    let a = unwrap_expr::<T>(a)?;
363162
    Some(f(a))
117861709
}
85631315
pub fn bin_op<T, A>(f: fn(T, T) -> A, a: &Expr, b: &Expr) -> Option<A>
85631315
where
85631315
    T: TryFrom<Lit>,
{
85631315
    let a = unwrap_expr::<T>(a)?;
43778345
    let b = unwrap_expr::<T>(b)?;
28966312
    Some(f(a, b))
85631315
}
#[allow(dead_code)]
pub fn tern_op<T, A>(f: fn(T, T, T) -> A, a: &Expr, b: &Expr, c: &Expr) -> Option<A>
where
    T: TryFrom<Lit>,
{
    let a = unwrap_expr::<T>(a)?;
    let b = unwrap_expr::<T>(b)?;
    let c = unwrap_expr::<T>(c)?;
    Some(f(a, b, c))
}
pub fn vec_op<T, A>(f: fn(Vec<T>) -> A, a: &[Expr]) -> Option<A>
where
    T: TryFrom<Lit>,
{
    let a = a.iter().map(unwrap_expr).collect::<Option<Vec<T>>>()?;
    Some(f(a))
}
227357527
pub fn vec_lit_op<T, A>(f: fn(Vec<T>) -> A, a: &Expr) -> Option<A>
227357527
where
227357527
    T: TryFrom<Lit>,
{
227357527
    Some(f(eval_list_items(a)?))
227357527
}
type PairsCallback<T, A> = fn(Vec<(T, T)>, (usize, usize)) -> A;
/// Calls the given function on each consecutive pair of elements in the list expressions.
/// Also passes the length of the two lists.
1614976
fn vec_expr_pairs_op<T, A>(a: &Expr, b: &Expr, f: PairsCallback<T, A>) -> Option<A>
1614976
where
1614976
    T: TryFrom<Lit>,
{
1614976
    let a_exprs = a.clone().unwrap_matrix_unchecked()?.0;
618171
    let b_exprs = b.clone().unwrap_matrix_unchecked()?.0;
618171
    let lens = (a_exprs.len(), b_exprs.len());
618171
    let lit_pairs = std::iter::zip(a_exprs, b_exprs)
618729
        .map(|(a, b)| Some((unwrap_expr(&a)?, unwrap_expr(&b)?)))
617613
        .collect::<Option<Vec<(T, T)>>>()?;
558
    Some(f(lit_pairs, lens))
1614976
}
/// Same as [`vec_expr_pairs_op`], but over slices of atoms.
12989
fn atoms_pairs_op<T, A>(a: &[Atom], b: &[Atom], f: PairsCallback<T, A>) -> Option<A>
12989
where
12989
    T: TryFrom<Atom>,
{
12989
    let lit_pairs = Iterator::zip(a.iter(), b.iter())
12989
        .map(|(a, b)| Some((a.clone().try_into().ok()?, b.clone().try_into().ok()?)))
12989
        .collect::<Option<Vec<(T, T)>>>()?;
    Some(f(lit_pairs, (a.len(), b.len())))
12989
}
pub fn opt_vec_op<T, A>(f: fn(Vec<T>) -> Option<A>, a: &[Expr]) -> Option<A>
where
    T: TryFrom<Lit>,
{
    let a = a.iter().map(unwrap_expr).collect::<Option<Vec<T>>>()?;
    f(a)
}
1396789
pub fn opt_vec_lit_op<T, A>(f: fn(Vec<T>) -> Option<A>, a: &Expr) -> Option<A>
1396789
where
1396789
    T: TryFrom<Lit>,
{
1396789
    f(eval_list_items(a)?)
1396789
}
#[allow(dead_code)]
pub fn flat_op<T, A>(f: fn(Vec<T>, T) -> A, a: &[Expr], b: &Expr) -> Option<A>
where
    T: TryFrom<Lit>,
{
    let a = a.iter().map(unwrap_expr).collect::<Option<Vec<T>>>()?;
    let b = unwrap_expr::<T>(b)?;
    Some(f(a, b))
}
532316260
pub fn unwrap_expr<T: TryFrom<Lit>>(expr: &Expr) -> Option<T> {
532316260
    let c = eval_constant(expr)?;
142897636
    TryInto::<T>::try_into(c).ok()
532316260
}
228754316
fn eval_list_items<T>(expr: &Expr) -> Option<Vec<T>>
228754316
where
228754316
    T: TryFrom<Lit>,
{
228754316
    if let Some((items, _)) = expr.unwrap_matrix_unchecked_ref() {
217312706
        return items.iter().map(unwrap_expr).collect();
11441610
    }
11441610
    let collection = eval_constant(expr)?;
6825975
    generator_values_from_constant_collection(&collection)?
        .into_iter()
6825975
        .map(TryInto::try_into)
        .collect::<Result<Vec<_>, _>>()
        .ok()
228754316
}
9618945
fn eval_constant_comprehension(comprehension: &Comprehension) -> Option<Lit> {
9618945
    let mut values = Vec::new();
9618945
    eval_comprehension_qualifiers(comprehension, 0, &mut values)?;
49497
    Some(Lit::AbstractLiteral(
49497
        AbstractLiteral::matrix_implied_indices(values),
49497
    ))
9618945
}
22071629
fn eval_comprehension_qualifiers(
22071629
    comprehension: &Comprehension,
22071629
    qualifier_index: usize,
22071629
    values: &mut Vec<Lit>,
22071629
) -> Option<()> {
22071629
    if qualifier_index == comprehension.qualifiers.len() {
8250036
        values.push(eval_constant(&comprehension.return_expression)?);
201283
        return Some(());
13821593
    }
13821593
    match &comprehension.qualifiers[qualifier_index] {
        ComprehensionQualifier::Generator { ptr } => {
11629124
            let domain = ptr.domain()?;
11629124
            let generator_values = domain
11629124
                .resolve()
11628470
                .and_then(|x| x.values())
654
                .ok()?
                .collect_vec();
11628470
            for value in generator_values {
11833848
                with_temporary_quantified_binding(ptr, &value, || {
11833848
                    eval_comprehension_qualifiers(comprehension, qualifier_index + 1, values)
11833848
                })?;
            }
        }
        ComprehensionQualifier::ExpressionGenerator { ptr } => {
            // clone immediately so the read lock guard is dropped
960320
            let expr = ptr.as_quantified_expr()?.clone();
960320
            let generator_values = generator_values_from_expr(&expr)?;
224446
            for value in generator_values {
291468
                with_temporary_quantified_binding(ptr, &value, || {
291468
                    eval_comprehension_qualifiers(comprehension, qualifier_index + 1, values)
291468
                })?;
            }
        }
1232149
        ComprehensionQualifier::Condition(condition) => match eval_constant(condition)? {
            Lit::Bool(true) => {
327368
                eval_comprehension_qualifiers(comprehension, qualifier_index + 1, values)?
            }
120614
            Lit::Bool(false) => {}
            _ => return None,
        },
    }
540345
    Some(())
22071629
}
/// Values for a constant collection expression used during constant folding.
///
/// This does not enumerate decision-variable domains; quantification over decisions is not
/// unrolled here.
960320
pub fn generator_values_from_expr(expr: &Expr) -> Option<Vec<Lit>> {
960320
    generator_values_from_constant_collection(&eval_constant(expr)?)
960320
}
7147927
pub(crate) fn generator_values_from_constant_collection(lit: &Lit) -> Option<Vec<Lit>> {
7147927
    match lit {
        Lit::AbstractLiteral(AbstractLiteral::Set(values))
        | Lit::AbstractLiteral(AbstractLiteral::MSet(values))
983680
        | Lit::AbstractLiteral(AbstractLiteral::Tuple(values)) => Some(values.clone()),
6164247
        Lit::AbstractLiteral(AbstractLiteral::Matrix(values, _)) => Some(values.clone()),
        Lit::AbstractLiteral(list) => list.unwrap_list().cloned(),
        _ => None,
    }
7147927
}
12125316
fn with_temporary_quantified_binding<T>(
12125316
    quantified: &crate::ast::DeclarationPtr,
12125316
    value: &Lit,
12125316
    f: impl FnOnce() -> Option<T>,
12125316
) -> Option<T> {
12125316
    let mut targets = vec![quantified.clone()];
12125316
    if let DeclarationKind::Quantified(inner) = &*quantified.kind()
11833848
        && let Some(generator) = inner.generator()
    {
        targets.push(generator.clone());
12125316
    }
12125316
    let mut originals = Vec::with_capacity(targets.len());
12125316
    for mut target in targets {
12125316
        let old_kind = target.replace_kind(DeclarationKind::TemporaryValueLetting(Expr::Atomic(
12125316
            Metadata::new(),
12125316
            Atom::Literal(value.clone()),
12125316
        )));
12125316
        originals.push((target, old_kind));
    }
12125316
    let result = f();
12125316
    for (mut target, old_kind) in originals.into_iter().rev() {
12125316
        let _ = target.replace_kind(old_kind);
    }
12125316
    result
12125316
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::matrix_expr;
38
    fn int_lit(value: i32) -> Expr {
38
        Expr::Atomic(Metadata::new(), Atom::Literal(Lit::Int(value)))
38
    }
7
    fn bool_lit(value: bool) -> Expr {
7
        Expr::Atomic(Metadata::new(), Atom::Literal(Lit::Bool(value)))
7
    }
2
    fn variant_lit(alternative: &str, value: Expr) -> Expr {
2
        Expr::AbstractLiteral(
2
            Metadata::new(),
2
            AbstractLiteral::Variant(Moo::new(Field {
2
                name: crate::ast::Name::user(alternative),
2
                value,
2
            })),
2
        )
2
    }
16
    fn root(exprs: Vec<Expr>) -> Expr {
16
        Expr::Root(Metadata::new(), exprs)
16
    }
    #[test]
1
    fn evaluates_active_on_variant_literals() {
1
        let variant = Moo::new(variant_lit("value", int_lit(2)));
1
        let active = Expr::Active(
1
            Metadata::new(),
1
            variant.clone(),
1
            crate::ast::Name::user("value"),
1
        );
1
        let inactive = Expr::Active(Metadata::new(), variant, crate::ast::Name::user("flag"));
1
        assert_eq!(eval_constant(&active), Some(Lit::Bool(true)));
1
        assert_eq!(eval_constant(&inactive), Some(Lit::Bool(false)));
1
        assert!(run_partial_evaluator_local(&active).is_ok());
1
        let field = Expr::RecordField(
1
            Metadata::new(),
1
            Moo::new(variant_lit("value", int_lit(2))),
1
            crate::ast::Name::user("value"),
1
        );
1
        assert_eq!(eval_constant(&field), Some(Lit::Int(2)));
1
    }
    #[test]
1
    fn local_root_partial_eval_strips_true_constraints() {
1
        let expr = root(vec![bool_lit(true), int_lit(1)]);
1
        let Expr::Root(_, exprs) = &expr else {
            panic!("expected root");
        };
1
        let normalised = normalise_root_constraints_local(exprs).unwrap();
1
        assert_eq!(normalised, root(vec![int_lit(1)]));
1
    }
    #[test]
1
    fn local_root_partial_eval_propagates_false() {
1
        let expr = root(vec![bool_lit(false), int_lit(1)]);
1
        let Expr::Root(_, exprs) = &expr else {
            panic!("expected root");
        };
1
        let normalised = normalise_root_constraints_local(exprs).unwrap();
1
        assert_eq!(normalised, root(vec![bool_lit(false)]));
1
    }
    #[test]
1
    fn deep_root_normalisation_folds_ground_constraint() {
1
        let expr = root(vec![Expr::Sum(
1
            Metadata::new(),
1
            Moo::new(matrix_expr![int_lit(1), int_lit(2), int_lit(3)]),
1
        )]);
1
        let normalised = normalise_root_constraints_deep(&expr).unwrap();
1
        assert_eq!(normalised, root(vec![int_lit(6)]));
1
    }
    #[test]
1
    fn deep_root_normalisation_applies_partial_eval_steps() {
1
        let expr = root(vec![Expr::Or(
1
            Metadata::new(),
1
            Moo::new(matrix_expr![bool_lit(false), int_lit(1)]),
1
        )]);
1
        let normalised = normalise_root_constraints_deep(&expr).unwrap();
1
        assert_eq!(
1
            normalised,
1
            root(vec![Expr::Or(
1
                Metadata::new(),
1
                Moo::new(matrix_expr![int_lit(1)]),
1
            )])
        );
1
    }
    #[test]
1
    fn selective_deep_root_normalisation_skips_solver_flat_constraints() {
1
        let flat = Expr::FlatProductEq(
1
            Metadata::new(),
1
            Moo::new(Atom::Literal(Lit::Int(1))),
1
            Moo::new(Atom::Literal(Lit::Int(2))),
1
            Moo::new(Atom::Literal(Lit::Int(3))),
1
        );
1
        let expr = root(vec![bool_lit(true), flat]);
1
        assert!(normalise_root_constraints_deep(&expr).is_none());
1
    }
    #[test]
1
    fn deep_root_normalisation_terminates_on_already_folded_constraint() {
1
        let expr = root(vec![int_lit(5)]);
1
        assert!(normalise_root_constraints_deep(&expr).is_none());
1
    }
    #[test]
1
    fn local_evaluator_normalisation_terminates_on_already_folded_literal() {
1
        let expr = int_lit(5);
1
        assert!(normalise_evaluator_local(&expr).is_none());
1
    }
    #[test]
1
    fn constant_set_cardinality_is_folded() {
1
        let set = Expr::Atomic(
1
            Metadata::new(),
1
            Atom::Literal(Lit::AbstractLiteral(AbstractLiteral::Set(vec![
1
                Lit::Int(1),
1
                Lit::Int(2),
1
            ]))),
1
        );
1
        let cardinality = Expr::Card(Metadata::new(), Moo::new(set));
1
        assert_eq!(eval_constant(&cardinality), Some(Lit::Int(2)));
1
    }
    #[test]
1
    fn constant_set_duplicates_are_normalised_before_cardinality() {
1
        let literal = Lit::AbstractLiteral(AbstractLiteral::Set(vec![1.into(), 1.into()]));
1
        let atomic = Expr::from(literal);
1
        let expression = Expr::AbstractLiteral(
1
            Metadata::new(),
1
            AbstractLiteral::Set(vec![1.into(), 1.into()]),
1
        );
1
        for set in [atomic, expression] {
2
            let cardinality = Expr::Card(Metadata::new(), Moo::new(set));
2
            assert_eq!(eval_constant(&cardinality), Some(Lit::Int(1)));
        }
1
        let multiset = Expr::from(Lit::AbstractLiteral(AbstractLiteral::MSet(vec![
1
            1.into(),
1
            1.into(),
1
        ])));
1
        assert_eq!(
1
            eval_constant(&Expr::Card(Metadata::new(), Moo::new(multiset))),
1
            Some(Lit::Int(2))
        );
1
    }
    #[test]
1
    fn constant_set_minimum_and_maximum_are_folded() {
1
        let set = Expr::from(Lit::AbstractLiteral(AbstractLiteral::Set(vec![
1
            Lit::Int(4),
1
            Lit::Int(1),
1
            Lit::Int(3),
1
        ])));
1
        assert_eq!(
1
            eval_constant(&Expr::Min(Metadata::new(), Moo::new(set.clone()))),
1
            Some(Lit::Int(1))
        );
1
        assert_eq!(
1
            eval_constant(&Expr::Max(Metadata::new(), Moo::new(set))),
1
            Some(Lit::Int(4))
        );
1
    }
    #[test]
1
    fn constant_set_membership_supports_composite_values() {
        let member =
1
            Lit::AbstractLiteral(AbstractLiteral::Tuple(vec![Lit::Bool(true), Lit::Int(2)]));
1
        let set = Lit::AbstractLiteral(AbstractLiteral::Set(vec![member.clone()]));
1
        let membership = Expr::In(
1
            Metadata::new(),
1
            Moo::new(Expr::from(member)),
1
            Moo::new(Expr::from(set)),
1
        );
1
        assert_eq!(eval_constant(&membership), Some(Lit::Bool(true)));
1
    }
    #[test]
1
    fn constant_equality_supports_composite_values() {
1
        let tuple = Lit::AbstractLiteral(AbstractLiteral::Tuple(vec![Lit::Int(1), Lit::Int(3)]));
1
        let equal = Expr::Eq(
1
            Metadata::new(),
1
            Moo::new(Expr::from(tuple.clone())),
1
            Moo::new(Expr::from(tuple)),
1
        );
1
        let unequal = Expr::Neq(
1
            Metadata::new(),
1
            Moo::new(Expr::from(Lit::AbstractLiteral(AbstractLiteral::Tuple(
1
                vec![Lit::Int(1), Lit::Int(3)],
1
            )))),
1
            Moo::new(Expr::from(Lit::AbstractLiteral(AbstractLiteral::Tuple(
1
                vec![Lit::Int(1), Lit::Int(4)],
1
            )))),
1
        );
1
        assert_eq!(eval_constant(&equal), Some(Lit::Bool(true)));
1
        assert_eq!(eval_constant(&unequal), Some(Lit::Bool(true)));
1
        let set_equality = Expr::Eq(
1
            Metadata::new(),
1
            Moo::new(Expr::from(Lit::AbstractLiteral(AbstractLiteral::Set(
1
                vec![Lit::Int(1), Lit::Int(2)],
1
            )))),
1
            Moo::new(Expr::from(Lit::AbstractLiteral(AbstractLiteral::Set(
1
                vec![Lit::Int(2), Lit::Int(1)],
1
            )))),
1
        );
1
        assert_eq!(eval_constant(&set_equality), Some(Lit::Bool(true)));
1
    }
    #[test]
1
    fn finish_root_normalisation_applies_local_root_rules() {
1
        let expr = root(vec![bool_lit(true), int_lit(1)]);
1
        let normalised = finish_root_evaluator_normalisation(&expr).unwrap();
1
        assert_eq!(normalised, root(vec![int_lit(1)]));
1
    }
    #[test]
1
    fn selective_deep_only_constraint_folds_target_index() {
1
        let ground = Expr::Sum(
1
            Metadata::new(),
1
            Moo::new(matrix_expr![int_lit(1), int_lit(2), int_lit(3)]),
1
        );
1
        let untouched = Expr::Sum(
1
            Metadata::new(),
1
            Moo::new(matrix_expr![int_lit(4), int_lit(5)]),
1
        );
1
        let expr = root(vec![ground, untouched.clone()]);
1
        let normalised = normalise_root_selective_deep_expr(&expr, Some(0)).unwrap();
1
        assert_eq!(normalised, root(vec![int_lit(6), untouched]));
1
    }
    #[test]
1
    fn finish_root_normalisation_flattens_top_level_and() {
1
        let expr = root(vec![Expr::And(
1
            Metadata::new(),
1
            Moo::new(matrix_expr![bool_lit(true), int_lit(1), int_lit(2)]),
1
        )]);
1
        let normalised = finish_root_evaluator_normalisation(&expr).unwrap();
1
        assert_eq!(normalised, root(vec![int_lit(1), int_lit(2)]));
1
    }
2
    fn permutation_lit(cycles: Vec<Vec<Expr>>) -> Expr {
2
        Expr::AbstractLiteral(Metadata::new(), AbstractLiteral::Permutation(cycles))
2
    }
    #[test]
1
    fn evaluates_image_on_a_permutation_literal_moved_point() {
1
        let p = Moo::new(permutation_lit(vec![
1
            vec![int_lit(1), int_lit(3), int_lit(5)],
1
            vec![int_lit(2), int_lit(4)],
1
        ]));
1
        let image_1 = Expr::Image(Metadata::new(), p.clone(), Moo::new(int_lit(1)));
1
        let image_3 = Expr::Image(Metadata::new(), p.clone(), Moo::new(int_lit(3)));
1
        let image_5 = Expr::Image(Metadata::new(), p.clone(), Moo::new(int_lit(5)));
1
        let image_2 = Expr::Image(Metadata::new(), p.clone(), Moo::new(int_lit(2)));
1
        let image_4 = Expr::Image(Metadata::new(), p, Moo::new(int_lit(4)));
1
        assert_eq!(eval_constant(&image_1), Some(Lit::Int(3)));
1
        assert_eq!(eval_constant(&image_3), Some(Lit::Int(5)));
1
        assert_eq!(eval_constant(&image_5), Some(Lit::Int(1)));
1
        assert_eq!(eval_constant(&image_2), Some(Lit::Int(4)));
1
        assert_eq!(eval_constant(&image_4), Some(Lit::Int(2)));
1
    }
    #[test]
1
    fn evaluates_image_on_a_permutation_literal_fixed_point() {
1
        let p = Moo::new(permutation_lit(vec![vec![int_lit(1), int_lit(2)]]));
1
        let image_3 = Expr::Image(Metadata::new(), p, Moo::new(int_lit(3)));
        // 3 is not mentioned in any cycle, so it is an implicit fixed point.
1
        assert_eq!(eval_constant(&image_3), Some(Lit::Int(3)));
1
    }
}