1
//! Comprehension expansion rules
2

            
3
mod expand_native;
4
mod expand_via_solver;
5
mod expand_via_solver_ac;
6
mod via_solver_common;
7

            
8
pub use expand_native::expand_native;
9
pub use expand_via_solver::expand_via_solver;
10
pub use expand_via_solver_ac::expand_via_solver_ac;
11

            
12
use conjure_cp::{
13
    ast::{
14
        DeclarationPtr, Domain, DomainPtr, Expression as Expr, IntVal, Metadata, Moo, Name, Range,
15
        Reference, SymbolTable, UnresolvedDomain,
16
        ac_operators::ACOperatorKind,
17
        comprehension::{Comprehension, ComprehensionQualifier},
18
        eval_constant,
19
        serde::{HasId, ObjId},
20
    },
21
    bug, into_matrix_expr,
22
    rule_engine::{
23
        ApplicationError::RuleNotApplicable, ApplicationResult, RuleEffect, register_rule,
24
    },
25
    settings::{QuantifiedExpander, comprehension_expander},
26
};
27
use std::collections::{BTreeSet, HashMap};
28
use tracing::debug;
29
use uniplate::{Biplate, Uniplate};
30

            
31
use via_solver_common::simplify_expression;
32

            
33
/// Candidate assignments below this limit are cheaper to enumerate natively. The Pythagorean
34
/// benchmark brackets the crossover between 125 assignments (native wins) and 1,000 assignments
35
/// (solver-backed expansion wins decisively), so use a deliberately coarse power-of-two boundary.
36
const AUTO_SOLVER_THRESHOLD: u64 = 512;
37

            
38
/// Resolve `auto` to one of the concrete expansion implementations for this comprehension.
39
288783
fn selected_comprehension_expander(comprehension: &Comprehension) -> QuantifiedExpander {
40
288783
    match comprehension_expander() {
41
288744
        QuantifiedExpander::Auto => auto_comprehension_expander(comprehension),
42
39
        explicit => explicit,
43
    }
44
288783
}
45

            
46
/// Choose native expansion unless the candidate space is large and the temporary solver model
47
/// has a constraint that can potentially discard quantified assignments.
48
288751
fn auto_comprehension_expander(comprehension: &Comprehension) -> QuantifiedExpander {
49
288751
    let Some(candidate_assignments) = estimated_candidate_assignments(comprehension) else {
50
5473
        if has_dependent_generators_and_nested_return(comprehension) {
51
640
            return QuantifiedExpander::ViaSolverAc;
52
4833
        }
53
4833
        return QuantifiedExpander::Native;
54
    };
55

            
56
283278
    if candidate_assignments < AUTO_SOLVER_THRESHOLD {
57
283099
        return QuantifiedExpander::Native;
58
179
    }
59

            
60
179
    let useful_guard = has_useful_quantified_guard(comprehension);
61
179
    let useful_ac_pruning = match comprehension.skip_operator {
62
        Some(
63
            ACOperatorKind::And
64
            | ACOperatorKind::Or
65
            | ACOperatorKind::Sum
66
            | ACOperatorKind::Product,
67
179
        ) => has_cheap_ac_return_pruning(comprehension),
68
        _ => false,
69
    };
70

            
71
179
    if useful_guard || useful_ac_pruning {
72
29
        QuantifiedExpander::ViaSolverAc
73
    } else {
74
150
        QuantifiedExpander::Native
75
    }
76
288751
}
77

            
78
/// Estimate the maximum number of generator assignments before guards are applied.
79
///
80
/// Dependent or otherwise unresolved domains deliberately return `None`: native expansion already
81
/// handles them one binding at a time, whereas guessing a solver-backed path risks changing which
82
/// models are supported. Multiplication saturates because only comparison with the threshold is
83
/// relevant.
84
288751
fn estimated_candidate_assignments(comprehension: &Comprehension) -> Option<u64> {
85
288751
    let mut assignments = 1u64;
86

            
87
288751
    for qualifier in &comprehension.qualifiers {
88
586279
        match qualifier {
89
            ComprehensionQualifier::Generator { ptr } => {
90
458273
                let domain = ptr.domain()?;
91
458273
                let length = domain.resolve().ok()?.length().ok()?;
92
452800
                assignments = assignments.saturating_mul(length);
93
            }
94
            ComprehensionQualifier::ExpressionGenerator { .. } => return None,
95
128006
            ComprehensionQualifier::Condition(_) => {}
96
        }
97
    }
98

            
99
283278
    Some(assignments)
100
288751
}
101

            
102
/// Detect the supported dependent-generator shape where native expansion repeatedly duplicates a
103
/// nested comprehension. The solver expander binds the outer assignment first and then expands the
104
/// nested return, which is substantially cheaper for models such as car sequencing.
105
///
106
/// This remains a narrow syntactic check: expression generators and missing domains stay native.
107
5473
fn has_dependent_generators_and_nested_return(comprehension: &Comprehension) -> bool {
108
5473
    if !matches!(
109
5473
        comprehension.skip_operator,
110
5473
        Some(
111
5473
            ACOperatorKind::And
112
5473
                | ACOperatorKind::Or
113
5473
                | ACOperatorKind::Sum
114
5473
                | ACOperatorKind::Product
115
5473
        )
116
5473
    ) {
117
        return false;
118
5473
    }
119

            
120
5473
    let mut generator_count = 0;
121
5473
    for qualifier in &comprehension.qualifiers {
122
10946
        match qualifier {
123
10946
            ComprehensionQualifier::Generator { ptr } if ptr.domain().is_some() => {
124
10946
                generator_count += 1;
125
            }
126
            ComprehensionQualifier::Generator { .. }
127
            | ComprehensionQualifier::ExpressionGenerator { .. } => return false,
128
            ComprehensionQualifier::Condition(_) => {}
129
        }
130
    }
131

            
132
5473
    generator_count >= 2
133
5473
        && comprehension
134
5473
            .return_expression
135
26344
            .any_expression(|expression| matches!(expression, Expr::Comprehension(_, _)))
136
5473
}
137

            
138
/// A useful guard is a nonconstant condition whose remaining references include at least one
139
/// quantified variable and no non-quantified decision variable.
140
179
fn has_useful_quantified_guard(comprehension: &Comprehension) -> bool {
141
179
    let quantified_vars: BTreeSet<_> = comprehension.quantified_vars().into_iter().collect();
142

            
143
774
    comprehension.qualifiers.iter().any(|qualifier| {
144
774
        let ComprehensionQualifier::Condition(condition) = qualifier else {
145
503
            return false;
146
        };
147

            
148
271
        comprehension.is_quantified_guard(condition)
149
1
            && Biplate::<Name>::universe_bi(condition)
150
1
                .iter()
151
1
                .any(|name| quantified_vars.contains(name))
152
774
    })
153
179
}
154

            
155
/// Cheaply detect return-expression pruning that is likely to repay solver setup.
156
///
157
/// A fully quantified return can be passed to the temporary solver without dummy variables. For
158
/// mixed returns, require a quantified-only conjunction: this recognises selective filters such as
159
/// the Pythagorean antecedent while rejecting single weak comparisons and pairwise representation
160
/// implications. This is deliberately syntactic; `auto` must not build or simplify a temporary
161
/// model merely to decide whether to build one.
162
179
fn has_cheap_ac_return_pruning(comprehension: &Comprehension) -> bool {
163
179
    let quantified_vars: BTreeSet<_> = comprehension.quantified_vars().into_iter().collect();
164
179
    let return_expression = &comprehension.return_expression;
165

            
166
179
    if !comprehension_has_non_quantified_guards(comprehension)
167
44
        && expression_depends_only_on_quantified_decisions(
168
44
            comprehension,
169
44
            return_expression,
170
44
            &quantified_vars,
171
44
        )
172
    {
173
        return true;
174
179
    }
175

            
176
179
    has_quantified_conjunction_in_pruning_position(
177
179
        comprehension,
178
179
        return_expression,
179
179
        &quantified_vars,
180
179
    )
181
179
}
182

            
183
/// Look only where an `and` can filter AC identities: at the return root or in an implication's
184
/// antecedent. A conjunction in the consequent describes the emitted constraint; treating it as a
185
/// filter made battleship-style returns pay for solver expansion without useful pruning.
186
179
fn has_quantified_conjunction_in_pruning_position(
187
179
    comprehension: &Comprehension,
188
179
    expression: &Expr,
189
179
    quantified_vars: &BTreeSet<Name>,
190
179
) -> bool {
191
179
    match expression {
192
        Expr::And(_, terms) => {
193
            if expression_depends_only_on_quantified_decisions(
194
                comprehension,
195
                expression,
196
                quantified_vars,
197
            ) {
198
                return true;
199
            }
200

            
201
            terms.unwrap_list_cow().is_some_and(|terms| {
202
                terms.iter().any(|term| {
203
                    has_quantified_conjunction_in_pruning_position(
204
                        comprehension,
205
                        term,
206
                        quantified_vars,
207
                    )
208
                })
209
            })
210
        }
211
1087
        Expr::Imply(_, antecedent, _) => antecedent.any_expression(|subexpression| {
212
1087
            matches!(subexpression, Expr::And(_, _))
213
28
                && expression_depends_only_on_quantified_decisions(
214
28
                    comprehension,
215
28
                    subexpression,
216
28
                    quantified_vars,
217
28
                )
218
1087
        }),
219
14
        _ => false,
220
    }
221
179
}
222

            
223
72
fn expression_depends_only_on_quantified_decisions(
224
72
    comprehension: &Comprehension,
225
72
    expression: &Expr,
226
72
    quantified_vars: &BTreeSet<Name>,
227
72
) -> bool {
228
72
    let referenced_names = Biplate::<Name>::universe_bi(expression);
229
72
    referenced_names
230
72
        .iter()
231
84
        .any(|name| quantified_vars.contains(name))
232
70
        && comprehension.is_quantified_guard(expression)
233
72
}
234

            
235
/// Simplifies an expanded comprehension under its AC operator before the rewrite is committed.
236
///
237
/// This folds identities and dominates constant bounds (via [`simplify_expression`] / partial
238
/// evaluation) so huge `and([x > k_i, ...])` expansions collapse before rule-trace snapshots and
239
/// later rewriting see them.
240
71978
fn simplify_expanded_ac_results(results: Vec<Expr>, ac_operator: ACOperatorKind) -> Vec<Expr> {
241
71978
    let simplified = simplify_expression(ac_operator.as_expression(into_matrix_expr!(results)));
242
71978
    match (&ac_operator, &simplified) {
243
        (ACOperatorKind::And, Expr::And(_, matrix))
244
        | (ACOperatorKind::Or, Expr::Or(_, matrix))
245
        | (ACOperatorKind::Sum, Expr::Sum(_, matrix))
246
        | (ACOperatorKind::Product, Expr::Product(_, matrix))
247
        | (ACOperatorKind::Min, Expr::Min(_, matrix))
248
58087
        | (ACOperatorKind::Max, Expr::Max(_, matrix)) => matrix
249
58087
            .unwrap_list_cow()
250
58087
            .map(std::borrow::Cow::into_owned)
251
            .unwrap_or_else(|| vec![simplified]),
252
13891
        _ => vec![simplified],
253
    }
254
71978
}
255

            
256
/// Rewrite top-level `exists` comprehensions into constraints over fresh auxiliary `find`s.
257
///
258
/// `exists` is represented as `or([comprehension])`. The quantified variables become
259
/// [`DeclarationKind::FindAuxiliary`] entries so Minion does not branch on them.
260
#[register_rule("Base", 2003, [Root])]
261
572613
fn exists_quantified_to_finds(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
262
572613
    let Expr::Root(metadata, constraints) = expr else {
263
1713
        return Err(RuleNotApplicable);
264
    };
265

            
266
570900
    let mut new_symbols = symbols.clone();
267
570900
    let mut new_constraints = Vec::with_capacity(constraints.len());
268
570900
    let mut changed = false;
269

            
270
570900
    for constraint in constraints {
271
4409709
        let Some(comprehension) = as_exists_comprehension(constraint) else {
272
4396236
            new_constraints.push(constraint.clone());
273
4396236
            continue;
274
        };
275

            
276
        let Some(new_constraints_for_exists) =
277
13473
            rewrite_exists_comprehension_to_constraints(&comprehension, &mut new_symbols)
278
        else {
279
            new_constraints.push(constraint.clone());
280
            continue;
281
        };
282

            
283
13473
        new_constraints.extend(new_constraints_for_exists);
284
13473
        changed = true;
285
    }
286

            
287
570900
    if changed {
288
13473
        Ok(RuleEffect::with_symbols(
289
13473
            Expr::Root(metadata.clone(), new_constraints),
290
13473
            new_symbols,
291
13473
        ))
292
    } else {
293
557427
        Err(RuleNotApplicable)
294
    }
295
572613
}
296

            
297
/// Expand comprehensions inside AC operators using `--comprehension-expander native`.
298
///
299
/// Guarded comprehensions with symbolic guards are expanded here (not by
300
/// [`expand_comprehension_native`]) so matrix representation can run first.
301
/// Skip semantics come from [`Comprehension::skip_operator`].
302
#[register_rule("Base", 1999, [And, Or, Sum, Product])]
303
256934
fn expand_ac_comprehension_native(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
304
256934
    let ac_operator_kind = expr.to_ac_operator_kind().ok_or(RuleNotApplicable)?;
305
255349
    let children = expr.children();
306
255349
    debug_assert_eq!(
307
255349
        children.len(),
308
255349
        1,
309
        "AC expressions should have exactly one child."
310
    );
311

            
312
255349
    let comprehension = children
313
255349
        .front()
314
255349
        .and_then(as_single_comprehension)
315
255349
        .ok_or(RuleNotApplicable)?;
316

            
317
    if selected_comprehension_expander(&comprehension) != QuantifiedExpander::Native {
318
        return Err(RuleNotApplicable);
319
    }
320

            
321
    if comprehension.skip_operator.is_none() {
322
        return Err(RuleNotApplicable);
323
    }
324

            
325
    if !comprehension_has_symbolic_guards(&comprehension) {
326
        return Err(RuleNotApplicable);
327
    }
328

            
329
    for qual in &comprehension.qualifiers {
330
        if let ComprehensionQualifier::ExpressionGenerator { .. } = qual {
331
            return Err(RuleNotApplicable);
332
        }
333
    }
334

            
335
    let mut symbols = symbols.clone();
336
    let results = expand_native(comprehension, &mut symbols)
337
        .unwrap_or_else(|e| bug!("native AC comprehension expansion failed: {e}"));
338
    let results = simplify_expanded_ac_results(results, ac_operator_kind);
339

            
340
    Ok(RuleEffect::with_symbols(
341
        ac_operator_kind.as_expression(into_matrix_expr!(results)),
342
        symbols,
343
    ))
344
256934
}
345

            
346
/// Expand comprehensions using `--comprehension-expander native`.
347
///
348
/// Does not unroll expression generators (`i <- expr`): quantification over decision variables
349
/// is handled by representation-specific vertical rules, not by enumerating values here.
350
/// Constant quantifier collections are constant-folded via [`eval_constant`] instead.
351
#[register_rule("Base", 2000, [Comprehension])]
352
77019
fn expand_comprehension_native(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
353
77019
    let Expr::Comprehension(_, comprehension) = expr else {
354
1783
        return Err(RuleNotApplicable);
355
    };
356

            
357
75236
    let comprehension = comprehension.as_ref().clone();
358

            
359
75236
    if selected_comprehension_expander(&comprehension) != QuantifiedExpander::Native {
360
        return Err(RuleNotApplicable);
361
75236
    }
362

            
363
75236
    for qual in &comprehension.qualifiers {
364
158496
        if let ComprehensionQualifier::ExpressionGenerator { .. } = qual {
365
            return Err(RuleNotApplicable);
366
158496
        }
367
    }
368

            
369
75236
    let mut symbols = symbols.clone();
370
75236
    let comprehension_domain = comprehension.domain_of();
371
75236
    let skip_operator = comprehension.skip_operator;
372
75236
    let results = expand_native(comprehension, &mut symbols).or(Err(RuleNotApplicable))?;
373
75236
    let results = match skip_operator {
374
71978
        Some(op) => simplify_expanded_ac_results(results, op),
375
3258
        None => results,
376
    };
377
75236
    let expanded = into_matrix_expr!(results);
378
75236
    let expanded = match comprehension_domain {
379
75200
        Some(domain) if expanded.list_len() == Some(0) => {
380
            Expr::DomainAnnotation(Metadata::new(), Moo::new(expanded), domain)
381
        }
382
75236
        _ => expanded,
383
    };
384
75236
    Ok(RuleEffect::with_symbols(expanded, symbols))
385
77019
}
386

            
387
/// Expand comprehensions using `--comprehension-expander via-solver`.
388
///
389
/// Algorithm sketch:
390
/// 1. Match one comprehension node.
391
/// 2. Build a temporary generator submodel from its qualifiers/guards.
392
/// 3. Materialise quantified declarations as temporary `find` declarations.
393
/// 4. Wrap that submodel as a standalone temporary model, with search order restricted to the
394
///    quantified names.
395
/// 5. Rewrite the temporary model using the configured rewriter and Minion-oriented rules.
396
/// 6. Solve the rewritten temporary model with Minion and keep only quantified assignments from
397
///    each solution.
398
/// 7. Instantiate the original return expression under each quantified assignment.
399
/// 8. Replace the comprehension by a matrix literal containing all instantiated return values.
400
#[register_rule("Base", 2000, [Comprehension])]
401
77019
fn expand_comprehension_via_solver(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
402
77019
    let Expr::Comprehension(_, comprehension) = expr else {
403
1783
        return Err(RuleNotApplicable);
404
    };
405

            
406
75236
    let comprehension = comprehension.as_ref().clone();
407

            
408
75236
    if !matches!(
409
75236
        selected_comprehension_expander(&comprehension),
410
75236
        QuantifiedExpander::ViaSolver | QuantifiedExpander::ViaSolverAc
411
75236
    ) {
412
75236
        return Err(RuleNotApplicable);
413
    }
414

            
415
    for qual in &comprehension.qualifiers {
416
        if let ComprehensionQualifier::ExpressionGenerator { .. } = qual {
417
            return Err(RuleNotApplicable);
418
        }
419
    }
420

            
421
    // A guard over non-quantified decision variables can only be re-attached to the expanded
422
    // elements through an enclosing AC operator's skip semantics. Without one there is nothing to
423
    // attach it to, so leave the comprehension alone (the native expander bails here too).
424
    if comprehension.skip_operator.is_none()
425
        && comprehension_has_non_quantified_guards(&comprehension)
426
    {
427
        return Err(RuleNotApplicable);
428
    }
429

            
430
    // A generator model the solver cannot load means this expander cannot do this comprehension
431
    // -- a guard over a type Minion has no encoding for, say -- not that something has gone
432
    // wrong. Expand it natively instead: leaving it alone would strand the comprehension, since
433
    // the native rule only fires for comprehensions `auto` chose it for.
434
    let results = match expand_via_solver(comprehension.clone()) {
435
        Ok(results) => results,
436
        Err(e) => {
437
            debug!(why=%e, "via-solver comprehension expansion failed, falling back to native expansion");
438
            let mut symbols = symbols.clone();
439
            let results = expand_native(comprehension, &mut symbols).or(Err(RuleNotApplicable))?;
440
            return Ok(RuleEffect::with_symbols(
441
                into_matrix_expr!(results),
442
                symbols,
443
            ));
444
        }
445
    };
446
    Ok(RuleEffect::with_symbols(
447
        into_matrix_expr!(results),
448
        symbols.clone(),
449
    ))
450
77019
}
451

            
452
/// Expand comprehensions inside AC operators using `--comprehension-expander via-solver-ac`.
453
///
454
/// Algorithm sketch:
455
/// 1. Match an AC operator whose single child is a comprehension.
456
/// 2. Build a temporary generator submodel from the comprehension qualifiers/guards.
457
/// 3. Add a derived constraint from the return expression to this generator model:
458
///    localise non-local references, and replace non-quantified fragments with dummy variables so
459
///    the constraint depends only on locally solvable symbols.
460
/// 4. Materialise quantified declarations as temporary `find` declarations in the temporary model.
461
/// 5. Rewrite and solve the temporary model with Minion; keep only quantified assignments.
462
/// 6. Instantiate the original return expression under those assignments.
463
/// 7. Rebuild the same AC operator around the instantiated matrix literal.
464
#[register_rule("Base", 2002, [And, Or, Sum, Product])]
465
409183
fn expand_comprehension_via_solver_ac(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
466
    // Is this an ac expression?
467
409183
    let ac_operator_kind = expr.to_ac_operator_kind().ok_or(RuleNotApplicable)?;
468

            
469
407586
    let children = expr.children();
470
407586
    debug_assert_eq!(
471
407586
        children.len(),
472
407586
        1,
473
        "AC expressions should have exactly one child."
474
    );
475

            
476
407586
    let comprehension = children
477
407586
        .front()
478
407586
        .and_then(as_single_comprehension)
479
407586
        .ok_or(RuleNotApplicable)?;
480

            
481
138311
    if selected_comprehension_expander(&comprehension) != QuantifiedExpander::ViaSolverAc {
482
137633
        return Err(RuleNotApplicable);
483
678
    }
484

            
485
678
    for qual in &comprehension.qualifiers {
486
1476
        if let ComprehensionQualifier::ExpressionGenerator { .. } = qual {
487
            return Err(RuleNotApplicable);
488
1476
        }
489
    }
490

            
491
678
    let results = match expand_via_solver_ac(comprehension.clone(), ac_operator_kind) {
492
678
        Ok(results) => results,
493
        // As in `expand_comprehension_via_solver`: fall back to native expansion rather than
494
        // stranding a comprehension this expander cannot build a solvable generator model for.
495
        Err(e) => {
496
            debug!(why=%e, "via-solver-ac comprehension expansion failed, falling back to native expansion");
497
            let mut symbols = symbols.clone();
498
            let results = expand_native(comprehension, &mut symbols).or(Err(RuleNotApplicable))?;
499
            let results = simplify_expanded_ac_results(results, ac_operator_kind);
500
            let new_expr = ac_operator_kind.as_expression(into_matrix_expr!(results));
501
            return Ok(RuleEffect::with_symbols(new_expr, symbols));
502
        }
503
    };
504

            
505
678
    let new_expr = ac_operator_kind.as_expression(into_matrix_expr!(results));
506
678
    Ok(RuleEffect::with_symbols(new_expr, symbols.clone()))
507
409183
}
508

            
509
705755
fn as_single_comprehension(expr: &Expr) -> Option<Comprehension> {
510
705755
    if let Expr::Comprehension(_, comprehension) = expr {
511
151784
        return Some(comprehension.as_ref().clone());
512
553971
    }
513

            
514
    // Borrow the elements: this runs on every `Or` the rewriter visits, and copying the list
515
    // first made the test cost the whole matrix on a wide disjunction.
516
553971
    let [Expr::Comprehension(_, comprehension)] = expr.unwrap_list_ref()? else {
517
540901
        return None;
518
    };
519

            
520
    Some(comprehension.as_ref().clone())
521
705755
}
522

            
523
4409709
fn as_exists_comprehension(expr: &Expr) -> Option<Comprehension> {
524
4409709
    let Expr::Or(_, or_child) = expr else {
525
4366889
        return None;
526
    };
527

            
528
42820
    as_single_comprehension(or_child.as_ref())
529
4409709
}
530

            
531
/// True iff some guard references decision variables the comprehension does not quantify over.
532
///
533
/// Such a guard cannot be evaluated during expansion, however the quantified variables are bound.
534
179
fn comprehension_has_non_quantified_guards(comprehension: &Comprehension) -> bool {
535
477
    comprehension.qualifiers.iter().any(|qualifier| {
536
477
        let ComprehensionQualifier::Condition(condition) = qualifier else {
537
341
            return false;
538
        };
539
136
        !comprehension.is_quantified_guard(condition)
540
477
    })
541
179
}
542

            
543
fn comprehension_has_symbolic_guards(comprehension: &Comprehension) -> bool {
544
    comprehension.qualifiers.iter().any(|qualifier| {
545
        let ComprehensionQualifier::Condition(condition) = qualifier else {
546
            return false;
547
        };
548
        eval_constant(&simplify_expression(condition.clone())).is_none()
549
    })
550
}
551

            
552
13473
fn rewrite_exists_comprehension_to_constraints(
553
13473
    comprehension: &Comprehension,
554
13473
    symbols: &mut SymbolTable,
555
13473
) -> Option<Vec<Expr>> {
556
13473
    let quantified_declarations = quantified_declarations(comprehension)?;
557

            
558
13473
    let mut replacements_by_id: HashMap<ObjId, DeclarationPtr> = HashMap::new();
559
13473
    let mut replacements_by_name: HashMap<Name, DeclarationPtr> = HashMap::new();
560

            
561
13473
    for decl in quantified_declarations {
562
14787
        let domain = decl.domain()?;
563
        let rewritten_domain =
564
14787
            replace_declaration_ptrs_in_domain(domain, &replacements_by_id, &replacements_by_name);
565
14787
        let fresh_decl = symbols.gen_find_auxiliary(&rewritten_domain);
566
14787
        replacements_by_id.insert(decl.id(), fresh_decl.clone());
567
14787
        replacements_by_name.insert(decl.name().clone(), fresh_decl);
568
    }
569

            
570
13473
    let mut conjuncts = Vec::new();
571
13473
    for qualifier in &comprehension.qualifiers {
572
14787
        if let ComprehensionQualifier::Condition(condition) = qualifier {
573
            conjuncts.push(replace_declaration_ptrs_in_expr(
574
                condition.clone(),
575
                &replacements_by_id,
576
                &replacements_by_name,
577
            ));
578
14787
        }
579
    }
580
13473
    conjuncts.push(replace_declaration_ptrs_in_expr(
581
13473
        comprehension.return_expression.clone(),
582
13473
        &replacements_by_id,
583
13473
        &replacements_by_name,
584
13473
    ));
585

            
586
13473
    Some(conjuncts)
587
13473
}
588

            
589
13473
fn quantified_declarations(comprehension: &Comprehension) -> Option<Vec<DeclarationPtr>> {
590
13473
    let quantified_names = comprehension.quantified_vars();
591
13473
    let symbols = comprehension.symbols();
592
13473
    quantified_names
593
13473
        .into_iter()
594
14787
        .map(|name| symbols.lookup_local(&name))
595
        .collect()
596
13473
}
597

            
598
14789
fn replace_declaration_ptrs_in_expr(
599
14789
    expr: Expr,
600
14789
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
601
14789
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
602
14789
) -> Expr {
603
14789
    let rewritten = expr
604
42418
        .transform_bi(&|decl: DeclarationPtr| {
605
42418
            if let Some(replacement) = replacements_by_id.get(&decl.id()) {
606
17443
                return replacement.clone();
607
24975
            }
608

            
609
24975
            let name = decl.name().clone();
610
24975
            replacements_by_name.get(&name).cloned().unwrap_or(decl)
611
42418
        })
612
32257
        .transform_bi(&|reference: Reference| {
613
32257
            replace_reference(reference, replacements_by_id, replacements_by_name)
614
32257
        });
615

            
616
14789
    rewritten.transform_bi(&|mut comprehension: Comprehension| {
617
1
        let shadowed_names = comprehension.quantified_vars();
618
1
        let visible_replacements_by_name = replacements_by_name
619
1
            .iter()
620
1
            .filter(|(name, _)| !shadowed_names.contains(name))
621
1
            .map(|(name, declaration)| (name.clone(), declaration.clone()))
622
            .collect();
623

            
624
1
        comprehension.return_expression = replace_declaration_ptrs_in_expr(
625
1
            comprehension.return_expression,
626
1
            replacements_by_id,
627
1
            &visible_replacements_by_name,
628
1
        );
629
1
        for qualifier in &mut comprehension.qualifiers {
630
            if let ComprehensionQualifier::Condition(condition) = qualifier {
631
                *condition = replace_declaration_ptrs_in_expr(
632
                    condition.clone(),
633
                    replacements_by_id,
634
                    &visible_replacements_by_name,
635
                );
636
            }
637
        }
638

            
639
1
        comprehension
640
1
    })
641
14789
}
642

            
643
14787
fn replace_declaration_ptrs_in_domain(
644
14787
    domain: DomainPtr,
645
14787
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
646
14787
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
647
14787
) -> DomainPtr {
648
14787
    let mut rewritten = domain
649
        .transform_bi(&|expr: Expr| {
650
            replace_declaration_ptrs_in_expr(expr, replacements_by_id, replacements_by_name)
651
        })
652
        .transform_bi(&|reference: Reference| {
653
            replace_reference(reference, replacements_by_id, replacements_by_name)
654
        });
655

            
656
    // `Range<T>` does not participate in the generic biplate traversal, so recurse through
657
    // unresolved domain structure once to rewrite symbolic integer bounds.
658
14787
    rewrite_int_ranges_in_domain_ptr(&mut rewritten, replacements_by_id, replacements_by_name);
659

            
660
14787
    rewritten
661
14787
}
662

            
663
33598
fn replace_reference(
664
33598
    reference: Reference,
665
33598
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
666
33598
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
667
33598
) -> Reference {
668
33598
    let replacement = replacements_by_id
669
33598
        .get(&reference.ptr().id())
670
33598
        .cloned()
671
33598
        .or_else(|| {
672
33598
            let name = reference.name().clone();
673
33598
            replacements_by_name.get(&name).cloned()
674
33598
        });
675

            
676
33598
    replacement.map(Reference::new).unwrap_or(reference)
677
33598
}
678

            
679
14787
fn rewrite_int_ranges_in_domain_ptr(
680
14787
    domain: &mut DomainPtr,
681
14787
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
682
14787
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
683
14787
) {
684
14787
    let mut rewritten = domain.as_ref().clone();
685
14787
    rewrite_int_ranges_in_domain(&mut rewritten, replacements_by_id, replacements_by_name);
686
14787
    *domain = Moo::new(rewritten);
687
14787
}
688

            
689
14787
fn rewrite_int_ranges_in_domain(
690
14787
    domain: &mut Domain,
691
14787
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
692
14787
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
693
14787
) {
694
14787
    let Domain::Unresolved(unresolved) = domain else {
695
12789
        return;
696
    };
697

            
698
1998
    rewrite_int_ranges_in_unresolved_domain(
699
1998
        Moo::make_mut(unresolved),
700
1998
        replacements_by_id,
701
1998
        replacements_by_name,
702
1998
    );
703
14787
}
704

            
705
1998
fn rewrite_int_ranges_in_unresolved_domain(
706
1998
    unresolved: &mut UnresolvedDomain,
707
1998
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
708
1998
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
709
1998
) {
710
1998
    match unresolved {
711
        UnresolvedDomain::Int(ranges, _) => {
712
1998
            for range in ranges {
713
1998
                rewrite_int_range(range, replacements_by_id, replacements_by_name);
714
            }
715
        }
716
        // The collection is an ordinary expression, so quantified references inside it are
717
        // rewritten by the same walk that handles the rest of the comprehension body.
718
        UnresolvedDomain::IntFromValues(_) => {}
719
        UnresolvedDomain::Tuple(inner_domains, _) => {
720
            for inner_domain in inner_domains {
721
                rewrite_int_ranges_in_domain_ptr(
722
                    inner_domain,
723
                    replacements_by_id,
724
                    replacements_by_name,
725
                );
726
            }
727
        }
728
        UnresolvedDomain::Record(entries, _) => {
729
            for entry in entries {
730
                rewrite_int_ranges_in_domain_ptr(
731
                    &mut entry.value,
732
                    replacements_by_id,
733
                    replacements_by_name,
734
                );
735
            }
736
        }
737
        UnresolvedDomain::Variant(entries, _) => {
738
            for entry in entries {
739
                rewrite_int_ranges_in_domain_ptr(
740
                    &mut entry.value,
741
                    replacements_by_id,
742
                    replacements_by_name,
743
                );
744
            }
745
        }
746
        UnresolvedDomain::Matrix(inner, index_domains, _) => {
747
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
748
            for index_domain in index_domains {
749
                rewrite_int_ranges_in_domain_ptr(
750
                    index_domain,
751
                    replacements_by_id,
752
                    replacements_by_name,
753
                );
754
            }
755
        }
756
        UnresolvedDomain::Sequence(attr, inner) => {
757
            rewrite_int_range(&mut attr.size, replacements_by_id, replacements_by_name);
758
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
759
        }
760
        UnresolvedDomain::Set(attr, inner) => {
761
            rewrite_int_range(&mut attr.size, replacements_by_id, replacements_by_name);
762
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
763
        }
764
        UnresolvedDomain::MSet(attr, inner) => {
765
            rewrite_int_range(&mut attr.size, replacements_by_id, replacements_by_name);
766
            rewrite_int_range(
767
                &mut attr.occurrence,
768
                replacements_by_id,
769
                replacements_by_name,
770
            );
771
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
772
        }
773
        UnresolvedDomain::Function(attr, domain, codomain) => {
774
            rewrite_int_range(&mut attr.size, replacements_by_id, replacements_by_name);
775
            rewrite_int_ranges_in_domain_ptr(domain, replacements_by_id, replacements_by_name);
776
            rewrite_int_ranges_in_domain_ptr(codomain, replacements_by_id, replacements_by_name);
777
        }
778
        UnresolvedDomain::Relation(attr, domains) => {
779
            rewrite_int_range(&mut attr.size, replacements_by_id, replacements_by_name);
780
            for domain in domains {
781
                rewrite_int_ranges_in_domain_ptr(domain, replacements_by_id, replacements_by_name);
782
            }
783
        }
784
        UnresolvedDomain::Partition(attr, inner) => {
785
            rewrite_int_range(
786
                &mut attr.num_parts,
787
                replacements_by_id,
788
                replacements_by_name,
789
            );
790
            rewrite_int_range(&mut attr.part_len, replacements_by_id, replacements_by_name);
791
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
792
        }
793
        UnresolvedDomain::Permutation(attr, inner) => {
794
            rewrite_int_range(
795
                &mut attr.num_moved,
796
                replacements_by_id,
797
                replacements_by_name,
798
            );
799
            rewrite_int_ranges_in_domain_ptr(inner, replacements_by_id, replacements_by_name);
800
        }
801
        UnresolvedDomain::EnumeratedType(_, _) | UnresolvedDomain::UnnamedType(_) => {}
802
        UnresolvedDomain::Reference(_) => {}
803
    }
804
1998
}
805

            
806
1998
fn rewrite_int_range(
807
1998
    range: &mut Range<IntVal>,
808
1998
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
809
1998
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
810
1998
) {
811
1998
    match range {
812
        Range::Single(value) | Range::UnboundedL(value) | Range::UnboundedR(value) => {
813
            rewrite_int_value(value, replacements_by_id, replacements_by_name);
814
        }
815
        Range::Bounded(lower, upper) => {
816
1998
            rewrite_int_value(lower, replacements_by_id, replacements_by_name);
817
1998
            rewrite_int_value(upper, replacements_by_id, replacements_by_name);
818
        }
819
        Range::Unbounded => {}
820
    }
821
1998
}
822

            
823
3996
fn rewrite_int_value(
824
3996
    int_val: &mut IntVal,
825
3996
    replacements_by_id: &HashMap<ObjId, DeclarationPtr>,
826
3996
    replacements_by_name: &HashMap<Name, DeclarationPtr>,
827
3996
) {
828
3996
    match int_val {
829
        IntVal::Reference(reference) => {
830
1341
            *reference =
831
1341
                replace_reference(reference.clone(), replacements_by_id, replacements_by_name);
832
        }
833
        IntVal::Expr(expr) => {
834
1314
            let rewritten = replace_declaration_ptrs_in_expr(
835
1314
                (**expr).clone(),
836
1314
                replacements_by_id,
837
1314
                replacements_by_name,
838
1314
            );
839
1314
            *expr = Moo::new(rewritten);
840
        }
841
1341
        IntVal::Const(_) => {}
842
    }
843
3996
}
844

            
845
#[cfg(test)]
846
mod tests {
847
    use conjure_cp::ast::{
848
        Atom, Domain, Literal, SymbolTablePtr, comprehension::ComprehensionBuilder,
849
    };
850

            
851
    use super::*;
852

            
853
16
    fn literal(literal: Literal) -> Expr {
854
16
        Expr::Atomic(Metadata::new(), Atom::Literal(literal))
855
16
    }
856

            
857
13
    fn reference(declaration: DeclarationPtr) -> Expr {
858
13
        Expr::Atomic(Metadata::new(), Atom::new_ref(declaration))
859
13
    }
860

            
861
8
    fn eq(lhs: Expr, rhs: Expr) -> Expr {
862
8
        Expr::Eq(Metadata::new(), Moo::new(lhs), Moo::new(rhs))
863
8
    }
864

            
865
7
    fn comprehension_with_int_generator(
866
7
        upper: i32,
867
7
        parent: SymbolTablePtr,
868
7
    ) -> (Comprehension, DeclarationPtr) {
869
7
        let declaration = DeclarationPtr::new_quantified(
870
7
            Name::user("i"),
871
7
            Domain::int(vec![Range::Bounded(1, upper)]),
872
7
        );
873
7
        let comprehension = ComprehensionBuilder::new(parent)
874
7
            .generator(declaration)
875
7
            .with_return_value(literal(Literal::Bool(true)));
876
7
        let ComprehensionQualifier::Generator { ptr } = &comprehension.qualifiers[0] else {
877
            panic!("expected generator qualifier");
878
        };
879

            
880
7
        (comprehension.clone(), ptr.clone())
881
7
    }
882

            
883
    #[test]
884
1
    fn auto_keeps_small_guarded_comprehensions_native() {
885
1
        let (mut comprehension, quantified) = comprehension_with_int_generator(
886
1
            AUTO_SOLVER_THRESHOLD as i32 - 1,
887
1
            SymbolTablePtr::new(),
888
1
        );
889
1
        comprehension
890
1
            .qualifiers
891
1
            .push(ComprehensionQualifier::Condition(eq(
892
1
                reference(quantified),
893
1
                literal(Literal::Int(1)),
894
1
            )));
895

            
896
1
        assert_eq!(
897
1
            auto_comprehension_expander(&comprehension),
898
1
            QuantifiedExpander::Native
899
        );
900
1
    }
901

            
902
    #[test]
903
1
    fn auto_uses_solver_for_large_quantified_guards() {
904
1
        let parent = SymbolTablePtr::new();
905
1
        let outer = DeclarationPtr::new_find(Name::user("outer"), Domain::bool());
906
1
        parent.write().insert(outer.clone());
907
        let (mut comprehension, quantified) =
908
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
909
1
        comprehension
910
1
            .qualifiers
911
1
            .push(ComprehensionQualifier::Condition(eq(
912
1
                reference(quantified),
913
1
                literal(Literal::Int(1)),
914
1
            )));
915
1
        comprehension.return_expression = reference(outer);
916
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
917

            
918
1
        assert_eq!(
919
1
            auto_comprehension_expander(&comprehension),
920
1
            QuantifiedExpander::ViaSolverAc
921
        );
922
1
    }
923

            
924
    #[test]
925
1
    fn auto_keeps_single_ac_comparison_with_outer_decision_native() {
926
1
        let parent = SymbolTablePtr::new();
927
1
        let outer = DeclarationPtr::new_find(Name::user("outer"), Domain::bool());
928
1
        parent.write().insert(outer.clone());
929
        let (mut comprehension, quantified) =
930
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
931
1
        let quantified_filter = eq(reference(quantified), literal(Literal::Int(1)));
932
1
        comprehension.return_expression = Expr::Imply(
933
1
            Metadata::new(),
934
1
            Moo::new(quantified_filter),
935
1
            Moo::new(reference(outer)),
936
1
        );
937
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
938

            
939
1
        assert_eq!(
940
1
            auto_comprehension_expander(&comprehension),
941
1
            QuantifiedExpander::Native
942
        );
943
1
    }
944

            
945
    #[test]
946
1
    fn auto_uses_solver_for_quantified_conjunction_in_ac_return() {
947
1
        let parent = SymbolTablePtr::new();
948
1
        let outer = DeclarationPtr::new_find(Name::user("outer"), Domain::bool());
949
1
        parent.write().insert(outer.clone());
950
        let (mut comprehension, quantified) =
951
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
952
1
        let filters = vec![
953
1
            eq(reference(quantified.clone()), literal(Literal::Int(1))),
954
1
            eq(reference(quantified), literal(Literal::Int(2))),
955
        ];
956
1
        let quantified_filter = Expr::And(Metadata::new(), Moo::new(into_matrix_expr!(filters)));
957
1
        comprehension.return_expression = Expr::Imply(
958
1
            Metadata::new(),
959
1
            Moo::new(quantified_filter),
960
1
            Moo::new(reference(outer)),
961
1
        );
962
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
963

            
964
1
        assert_eq!(
965
1
            auto_comprehension_expander(&comprehension),
966
1
            QuantifiedExpander::ViaSolverAc
967
        );
968
1
    }
969

            
970
    #[test]
971
1
    fn auto_ignores_quantified_conjunctions_in_implication_consequents() {
972
1
        let parent = SymbolTablePtr::new();
973
1
        let outer = DeclarationPtr::new_find(Name::user("outer"), Domain::bool());
974
1
        parent.write().insert(outer.clone());
975
        let (mut comprehension, quantified) =
976
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
977
1
        let single_filter = eq(reference(quantified.clone()), literal(Literal::Int(1)));
978
1
        let quantified_conjunction = Expr::And(
979
1
            Metadata::new(),
980
1
            Moo::new(into_matrix_expr!(vec![
981
1
                eq(reference(quantified.clone()), literal(Literal::Int(1)),),
982
1
                eq(reference(quantified), literal(Literal::Int(2))),
983
1
            ])),
984
1
        );
985
1
        let consequent = Expr::And(
986
1
            Metadata::new(),
987
1
            Moo::new(into_matrix_expr!(vec![
988
1
                quantified_conjunction,
989
1
                reference(outer),
990
1
            ])),
991
1
        );
992
1
        comprehension.return_expression = Expr::Imply(
993
1
            Metadata::new(),
994
1
            Moo::new(single_filter),
995
1
            Moo::new(consequent),
996
1
        );
997
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
998

            
999
1
        assert_eq!(
1
            auto_comprehension_expander(&comprehension),
1
            QuantifiedExpander::Native
        );
1
    }
    #[test]
1
    fn auto_uses_solver_for_dependent_generators_with_nested_returns() {
1
        let parent = SymbolTablePtr::new();
        let (mut comprehension, outer) =
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
1
        let dependent_domain = Domain::int(vec![Range::Bounded(
1
            IntVal::Const(1),
1
            IntVal::Reference(Reference::new(outer)),
1
        )]);
1
        let inner_generator = DeclarationPtr::new_quantified(Name::user("j"), dependent_domain);
1
        comprehension
1
            .qualifiers
1
            .push(ComprehensionQualifier::Generator {
1
                ptr: inner_generator,
1
            });
1
        let nested = ComprehensionBuilder::new(SymbolTablePtr::new())
1
            .with_return_value(literal(Literal::Bool(true)));
1
        comprehension.return_expression = Expr::Comprehension(Metadata::new(), Moo::new(nested));
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
1
        assert_eq!(
1
            auto_comprehension_expander(&comprehension),
1
            QuantifiedExpander::ViaSolverAc
        );
1
    }
    #[test]
1
    fn auto_keeps_large_unprunable_comprehensions_native() {
1
        let parent = SymbolTablePtr::new();
1
        let outer = DeclarationPtr::new_find(Name::user("outer"), Domain::bool());
1
        parent.write().insert(outer.clone());
        let (mut comprehension, _) =
1
            comprehension_with_int_generator(AUTO_SOLVER_THRESHOLD as i32, parent);
1
        comprehension.return_expression = reference(outer);
1
        comprehension.skip_operator = Some(ACOperatorKind::And);
1
        assert_eq!(
1
            auto_comprehension_expander(&comprehension),
1
            QuantifiedExpander::Native
        );
1
    }
    #[test]
1
    fn replaces_references_in_expressions() {
1
        let quantified = DeclarationPtr::new_quantified(Name::user("matrix_bools"), Domain::bool());
1
        let replacement = DeclarationPtr::new_find(Name::Machine(0), Domain::bool());
1
        let safe_index = Expr::SafeIndex(
1
            Metadata::new(),
1
            Moo::new(Expr::Atomic(
1
                Metadata::new(),
1
                Atom::new_ref(quantified.clone()),
1
            )),
1
            vec![Expr::Atomic(
1
                Metadata::new(),
1
                Atom::Literal(Literal::Int(1)),
1
            )],
1
        );
        let inner_comprehension =
1
            ComprehensionBuilder::new(SymbolTablePtr::new()).with_return_value(safe_index);
1
        let expr = Expr::Comprehension(Metadata::new(), Moo::new(inner_comprehension));
1
        let replacements_by_id = HashMap::from([(quantified.id(), replacement.clone())]);
        let replacements_by_name =
1
            HashMap::from([(quantified.name().clone(), replacement.clone())]);
        let rewritten =
1
            replace_declaration_ptrs_in_expr(expr, &replacements_by_id, &replacements_by_name);
1
        let Expr::Comprehension(_, comprehension) = rewritten else {
            panic!("expected a comprehension expression");
        };
1
        let Expr::SafeIndex(_, subject, _) = &comprehension.return_expression else {
            panic!("expected a safe index return expression");
        };
1
        let Expr::Atomic(_, Atom::Reference(reference)) = subject.as_ref() else {
            panic!("expected a reference subject");
        };
1
        assert_eq!(reference.id(), replacement.id());
1
    }
}