1
use std::{
2
    cell::RefCell,
3
    collections::{BTreeSet, HashMap, VecDeque},
4
    sync::{Arc, Mutex},
5
};
6

            
7
use conjure_cp::{
8
    ast::{
9
        Atom, DeclarationPtr, Expression, Metadata, Model, Moo, Name, Reference, ReturnType,
10
        SymbolTable, Typeable as _,
11
        ac_operators::ACOperatorKind,
12
        comprehension::{Comprehension, ComprehensionQualifier},
13
        serde::HasId as _,
14
    },
15
    bug_assert,
16
    rule_engine::resolve_rule_sets,
17
    settings::{SolverFamily, current_rewriter},
18
    solver::{Solver, SolverError, adaptors::Minion},
19
};
20
use tracing::debug;
21
use uniplate::{Biplate, zipper::Zipper};
22

            
23
use super::via_solver_common::{
24
    detach_symbols, instantiate_return_expressions_from_values, retain_quantified_solution_values,
25
    rewrite_model_with_configured_rewriter, simplify_expression, split_symbolic_guards,
26
    temporarily_materialise_quantified_vars_as_finds, with_temporary_model,
27
};
28

            
29
/// Expands the comprehension using Minion, returning the resulting expressions.
30
///
31
/// This method is only suitable for comprehensions inside an AC operator. The AC operator that
32
/// contains this comprehension should be passed into the `ac_operator` argument.
33
///
34
/// This method performs additional pruning of "uninteresting" values, only possible when the
35
/// comprehension is inside an AC operator.
36
678
pub fn expand_via_solver_ac(
37
678
    comprehension: Comprehension,
38
678
    ac_operator: ACOperatorKind,
39
678
) -> Result<Vec<Expression>, SolverError> {
40
    // Guards over non-quantified decision variables belong to the outer model, not the generator
41
    // submodel; hold them back and re-apply them per element with this operator's skip semantics.
42
678
    let (comprehension, symbolic_guards) = split_symbolic_guards(&comprehension);
43
678
    let quantified_vars = comprehension.quantified_vars();
44

            
45
    // Add the return-expression constraint only when it can restrict at least one quantified
46
    // variable. If dummy replacement removes every quantified dependency, the constraint cannot
47
    // prune the generator assignments and ordinary guard-only solver expansion is cheaper.
48
678
    let (generator_model, _) = prepare_ac_generator_model(&comprehension, ac_operator);
49

            
50
    // REWRITE GENERATOR MODEL AND PASS TO MINION
51
    // ==========================================
52

            
53
678
    let generator_model = with_temporary_model(generator_model, Some(quantified_vars.clone()));
54

            
55
678
    let extra_rule_sets = &["Base", "Bubble"];
56

            
57
678
    let rule_sets = resolve_rule_sets(SolverFamily::Minion, extra_rule_sets).unwrap();
58
678
    let configured_rewriter = current_rewriter();
59

            
60
    // REWRITE RETURN EXPRESSION
61
    // =========================
62

            
63
    // Keep return expressions unreduced until quantified assignments are substituted.
64
    // Rewriting before substitution can introduce index auxiliaries that remain symbolic and may
65
    // produce unsupported Minion shapes after expansion.
66
    let return_expression_model =
67
678
        with_temporary_model(comprehension.to_return_expression_model(), None);
68

            
69
    let values = {
70
678
        let solver_model = generator_model.clone();
71
        // Minion expects quantified variables in the temporary generator model as find
72
        // declarations. Keep this conversion scoped to solver-backed expansion.
73
        let _temp_finds =
74
678
            temporarily_materialise_quantified_vars_as_finds(&solver_model, &quantified_vars);
75

            
76
        // Rewrite with quantified vars materialised as finds so Minion flattening can
77
        // introduce auxiliaries for constraints involving quantified variables.
78
        let solver_model =
79
678
            rewrite_model_with_configured_rewriter(solver_model, &rule_sets, configured_rewriter);
80
678
        let symbols_for_solution = solver_model.symbols().clone();
81

            
82
678
        let minion = Solver::new(Minion::new());
83
678
        let minion = minion.load_model(solver_model);
84

            
85
678
        let minion = match minion {
86
            Err(e) => {
87
                // Callers fall back to native expansion, so this is a routine outcome rather
88
                // than a problem; the model is large, so keep it out of the default log.
89
                debug!(why=%e,model=%generator_model,"Loading generator model failed, failing solver-backed AC comprehension expansion rule");
90
                return Err(e);
91
            }
92
678
            Ok(minion) => minion,
93
        };
94

            
95
678
        let values = Arc::new(Mutex::new(Vec::new()));
96
678
        let values_ptr = Arc::clone(&values);
97
678
        let quantified_vars_for_solution = quantified_vars.clone();
98

            
99
        // SOLVE FOR THE QUANTIFIED VARIABLES, AND SUBSTITUTE INTO THE RETURN EXPRESSION
100
        // ============================================================================
101

            
102
        tracing::debug!(model=%generator_model,comprehension=%comprehension,"Minion solving comprehnesion (ac mode)");
103

            
104
9403
        minion.solve(Box::new(move |sols| {
105
            // Only keep quantified assignments; discard solver auxiliaries/locals.
106
9403
            let values = &mut *values_ptr.lock().unwrap();
107
9403
            values.push(retain_quantified_solution_values(
108
9403
                sols,
109
9403
                &quantified_vars_for_solution,
110
9403
                &symbols_for_solution,
111
9403
            ));
112
9403
            true
113
9403
        }))?;
114

            
115
678
        values.lock().unwrap().clone()
116
    };
117
678
    instantiate_return_expressions_from_values(
118
678
        values,
119
678
        &return_expression_model,
120
678
        &quantified_vars,
121
678
        &symbolic_guards,
122
678
        Some(ac_operator),
123
678
    )
124
678
}
125

            
126
/// Build the detached AC generator model and add its derived return constraint when useful.
127
678
fn prepare_ac_generator_model(
128
678
    comprehension: &Comprehension,
129
678
    ac_operator: ACOperatorKind,
130
678
) -> (Model, bool) {
131
678
    let mut generator_model = comprehension.to_generator_model();
132
678
    detach_symbols(&mut generator_model);
133

            
134
678
    let return_expression = comprehension.return_expression.clone();
135
678
    if contains_comprehension(&return_expression) {
136
        // A nested comprehension may have generator domains depending on variables this model is
137
        // still solving for. It is expanded later, once those variables have concrete bindings.
138
639
        return (generator_model, false);
139
39
    }
140

            
141
39
    let quantified_vars: BTreeSet<_> = comprehension.quantified_vars().into_iter().collect();
142
    let constraint = {
143
39
        let mut symtab = generator_model.symbols_mut();
144
39
        derive_return_expression_constraint(
145
39
            return_expression,
146
39
            &mut symtab,
147
39
            &quantified_vars,
148
39
            &ac_operator,
149
39
        )
150
    };
151
39
    let constraint = simplify_expression(constraint);
152
39
    let useful = Biplate::<Name>::universe_bi(&constraint)
153
39
        .iter()
154
39
        .any(|name| quantified_vars.contains(name));
155

            
156
39
    if useful {
157
39
        generator_model.add_constraint(constraint);
158
    }
159

            
160
39
    (generator_model, useful)
161
678
}
162

            
163
/// True iff `expr` contains a comprehension.
164
///
165
/// Comprehensions are leaves of the traversal, so this finds nested ones without descending into
166
/// their own scopes.
167
678
fn contains_comprehension(expr: &Expression) -> bool {
168
4242
    expr.any_expression(|subexpr| matches!(subexpr, Expression::Comprehension(_, _)))
169
678
}
170

            
171
/// Eliminate all references to non-quantified variables by introducing dummy variables, then
172
/// derive the constraint that discards AC-identity return values.
173
///
174
/// Dummy variables must be the same type as the AC operators identity value.
175
///
176
/// To reduce the number of dummy variables, we turn the largest expression containing only
177
/// non-quantified variables and of the correct type into a dummy variable.
178
///
179
/// If there is no such expression, (e.g. and[(a<i) | i: int(1..10)]) , we use the smallest
180
/// expression of the correct type that contains a non-quantified variable. This ensures that
181
/// we lose as few references to quantified variables as possible.
182
39
fn derive_return_expression_constraint(
183
39
    return_expression: Expression,
184
39
    symtab: &mut SymbolTable,
185
39
    quantified_vars: &BTreeSet<Name>,
186
39
    ac_operator: &ACOperatorKind,
187
39
) -> Expression {
188
39
    let return_expression = localise_non_local_references_deep(return_expression, symtab);
189

            
190
39
    let mut zipper = Zipper::new(return_expression);
191

            
192
    // for sum/product we want to put integer expressions into dummy variables,
193
    // for and/or we want to put boolean expressions into dummy variables.
194
39
    let dummy_var_type = ac_operator.identity().return_type();
195

            
196
    'outer: loop {
197
165
        let focus: &mut Expression = zipper.focus_mut();
198

            
199
        let (non_quantified_vars, quantified_references) =
200
165
            partition_variables(focus, quantified_vars);
201

            
202
        // an expression or its descendants needs to be turned into a dummy variable if it
203
        // contains non-quantified variables.
204
165
        let has_non_quantified_vars = !non_quantified_vars.is_empty();
205

            
206
        // does this expression contain quantified variables?
207
165
        let has_quantified_vars = !quantified_references.is_empty();
208

            
209
        // can this expression be turned into a dummy variable?
210
165
        let can_be_dummy_var = can_be_dummy_variable(focus, &dummy_var_type);
211

            
212
        // The expression and its descendants don't need a dummy variables, so we don't
213
        // need to descend into its children.
214
165
        if !has_non_quantified_vars {
215
            // go to next node or quit
216
39
            while zipper.go_right().is_none() {
217
                let Some(()) = zipper.go_up() else {
218
                    // visited all nodes
219
                    break 'outer;
220
                };
221
            }
222
39
            continue;
223
126
        }
224

            
225
        // The expression contains non-quantified variables:
226

            
227
        // does this expression have any children that can be turned into dummy variables?
228
1635
        let has_eligible_child = focus.any_expression_descendant(|expr| {
229
            // eligible if it can be turned into a dummy variable, and turning it into a
230
            // dummy variable removes a non-quantified variable from the model.
231
1635
            can_be_dummy_variable(expr, &dummy_var_type)
232
204
                && contains_non_quantified_variables(expr, quantified_vars)
233
1635
        });
234

            
235
        // This expression has no child that can be turned into a dummy variable, but can
236
        // be a dummy variable => turn it into a dummy variable and continue.
237
126
        if !has_eligible_child && can_be_dummy_var {
238
            // introduce dummy var and continue
239
63
            let dummy_domain = focus.domain_of().unwrap();
240
63
            let dummy_decl = symtab.gen_find_auxiliary(&dummy_domain);
241
63
            *focus = Expression::Atomic(
242
63
                Metadata::new(),
243
63
                Atom::Reference(conjure_cp::ast::Reference::new(dummy_decl)),
244
63
            );
245

            
246
            // go to next node
247
126
            while zipper.go_right().is_none() {
248
102
                let Some(()) = zipper.go_up() else {
249
                    // visited all nodes
250
39
                    break 'outer;
251
                };
252
            }
253
24
            continue;
254
        }
255
        // This expression has no child that can be turned into a dummy variable, and
256
        // cannot be a dummy variable => backtrack upwards to find a parent that can be a
257
        // dummy variable, and make it a dummy variable.
258
63
        else if !has_eligible_child && !can_be_dummy_var {
259
            // TODO: remove this case, make has_eligible_child check better?
260

            
261
            // go upwards until we find something that can be a dummy variable, make it
262
            // a dummy variable, then continue.
263
            while let Some(()) = zipper.go_up() {
264
                let focus = zipper.focus_mut();
265
                if can_be_dummy_variable(focus, &dummy_var_type) {
266
                    // TODO: this expression we are rewritng might already contain
267
                    // dummy vars - we might need a pass to get rid of the unused
268
                    // ones!
269
                    //
270
                    // introduce dummy var and continue
271
                    let dummy_domain = focus.domain_of().unwrap();
272
                    let dummy_decl = symtab.gen_find_auxiliary(&dummy_domain);
273
                    *focus = Expression::Atomic(
274
                        Metadata::new(),
275
                        Atom::Reference(conjure_cp::ast::Reference::new(dummy_decl)),
276
                    );
277

            
278
                    // go to next node
279
                    while zipper.go_right().is_none() {
280
                        let Some(()) = zipper.go_up() else {
281
                            // visited all nodes
282
                            break 'outer;
283
                        };
284
                    }
285
                    continue;
286
                }
287
            }
288
            unreachable!();
289
        }
290
        // If the expression contains quantified variables as well as non-quantified
291
        // variables, try to retain the quantified variables by finding a child that can be
292
        // made a dummy variable which has only non-quantified variables.
293
63
        else if has_eligible_child && has_quantified_vars {
294
63
            zipper
295
63
                .go_down()
296
63
                .expect("we know the focus has a child, so zipper.go_down() should succeed");
297
        }
298
        // This expression contains no quantified variables, so no point trying to turn a
299
        // child into a dummy variable.
300
        else if has_eligible_child && !has_quantified_vars {
301
            // introduce dummy var and continue
302
            let dummy_domain = focus.domain_of().unwrap();
303
            let dummy_decl = symtab.gen_find_auxiliary(&dummy_domain);
304
            *focus = Expression::Atomic(
305
                Metadata::new(),
306
                Atom::Reference(conjure_cp::ast::Reference::new(dummy_decl)),
307
            );
308

            
309
            // go to next node
310
            while zipper.go_right().is_none() {
311
                let Some(()) = zipper.go_up() else {
312
                    // visited all nodes
313
                    break 'outer;
314
                };
315
            }
316
        } else {
317
            unreachable!()
318
        }
319
    }
320

            
321
39
    let new_return_expression = Expression::Neq(
322
39
        Metadata::new(),
323
39
        Moo::new(Expression::Atomic(
324
39
            Metadata::new(),
325
39
            ac_operator.identity().into(),
326
39
        )),
327
39
        Moo::new(zipper.rebuild_root()),
328
39
    );
329

            
330
    // double check that the above transformation didn't miss any stray non-quantified vars
331
    bug_assert!(
332
39
        Biplate::<DeclarationPtr>::universe_bi(&new_return_expression)
333
39
            .iter()
334
255
            .all(|x| symtab.lookup_local(&x.name()).is_some()),
335
        "generator model should only contain references to variables in its symbol table."
336
    );
337

            
338
39
    new_return_expression
339
39
}
340

            
341
/// Replaces references to declarations outside `symtab` with local dummy declarations.
342
///
343
/// This preserves locality by construction for temporary generator models passed to Minion.
344
/// All rewrites then operate purely on local references and cannot reintroduce parent-scope vars.
345
39
fn localise_non_local_references_deep(expr: Expression, symtab: &mut SymbolTable) -> Expression {
346
39
    let symtab_cell = RefCell::new(symtab);
347

            
348
39
    let expr = expr.transform_bi(&|mut comprehension: Comprehension| {
349
        {
350
            let mut symtab_borrow = symtab_cell.borrow_mut();
351
            let symtab_ref: &mut SymbolTable = &mut symtab_borrow;
352

            
353
            comprehension.return_expression = localise_non_local_references_deep(
354
                comprehension.return_expression.clone(),
355
                symtab_ref,
356
            );
357

            
358
            for qualifier in &mut comprehension.qualifiers {
359
                if let ComprehensionQualifier::Condition(condition) = qualifier {
360
                    *condition = localise_non_local_references_deep(condition.clone(), symtab_ref);
361
                }
362
            }
363
        }
364

            
365
        comprehension
366
    });
367

            
368
39
    let mut symtab_borrow = symtab_cell.borrow_mut();
369
39
    let symtab_ref: &mut SymbolTable = &mut symtab_borrow;
370
39
    localise_non_local_references_shallow(expr, symtab_ref)
371
39
}
372

            
373
39
fn localise_non_local_references_shallow(expr: Expression, symtab: &mut SymbolTable) -> Expression {
374
39
    let dummy_vars_by_decl_id: RefCell<HashMap<_, DeclarationPtr>> = RefCell::new(HashMap::new());
375
39
    let symtab = RefCell::new(symtab);
376

            
377
750
    expr.transform_bi(&|atom: Atom| {
378
750
        let Atom::Reference(reference) = atom else {
379
144
            return atom;
380
        };
381

            
382
606
        let reference_name = reference.name().clone();
383

            
384
        // Already local to this temporary generator model.
385
606
        if symtab.borrow().lookup_local(&reference_name).is_some() {
386
480
            return Atom::Reference(reference);
387
126
        }
388

            
389
126
        let decl = reference.ptr().clone();
390
126
        let decl_id = decl.id();
391

            
392
126
        let existing_dummy = dummy_vars_by_decl_id.borrow().get(&decl_id).cloned();
393
126
        let dummy_decl = if let Some(existing_dummy) = existing_dummy {
394
87
            existing_dummy
395
        } else {
396
            let new_dummy = {
397
39
                let domain = decl.domain().unwrap_or_else(|| {
398
                    panic!("non-local reference '{}' has no domain", decl.name())
399
                });
400
39
                symtab.borrow_mut().gen_find_auxiliary(&domain)
401
            };
402
39
            dummy_vars_by_decl_id
403
39
                .borrow_mut()
404
39
                .insert(decl_id, new_dummy.clone());
405
39
            new_dummy
406
        };
407

            
408
126
        Atom::Reference(Reference::new(dummy_decl))
409
750
    })
410
39
}
411

            
412
/// Returns the non-quantified and quantified references inside the expression.
413
///
414
/// Membership is tested against the comprehension's original quantified names. In particular,
415
/// dummy declarations introduced while localising outer references are local to the temporary
416
/// model but remain non-quantified for pruning analysis.
417
#[inline]
418
165
fn partition_variables(
419
165
    expr: &Expression,
420
165
    quantified_vars: &BTreeSet<Name>,
421
165
) -> (VecDeque<Name>, VecDeque<Name>) {
422
    let (non_quantified_vars, quantified_vars): (VecDeque<Name>, VecDeque<Name>) =
423
165
        Biplate::<Name>::universe_bi(expr)
424
165
            .into_iter()
425
1500
            .partition(|name| !quantified_vars.contains(name));
426

            
427
165
    (non_quantified_vars, quantified_vars)
428
165
}
429

            
430
/// Returns `true` if `expr` can be turned into a dummy variable.
431
#[inline]
432
1800
fn can_be_dummy_variable(expr: &Expression, dummy_variable_type: &ReturnType) -> bool {
433
    // do not put root expression in a dummy variable or things go wrong.
434
1800
    if matches!(expr, Expression::Root(_, _)) {
435
        return false;
436
1800
    };
437

            
438
    // is the expression the same type as the dummy variable?
439
1800
    expr.return_type() == *dummy_variable_type
440
1800
}
441

            
442
/// Returns `true` if `expr` or its descendants contains non-quantified variables.
443
#[inline]
444
204
fn contains_non_quantified_variables(expr: &Expression, quantified_vars: &BTreeSet<Name>) -> bool {
445
204
    let names_referenced: VecDeque<Name> = expr.universe_bi();
446
204
    names_referenced
447
204
        .iter()
448
507
        .any(|name| !quantified_vars.contains(name))
449
204
}