1
//! Rules for variables in domains.
2

            
3
use std::collections::HashMap;
4

            
5
use conjure_cp::{
6
    ast::{
7
        Atom, DecisionVariable, DeclarationKind, Domain, DomainPtr, Expression as Expr, HasDomain,
8
        IntVal, Literal as Lit, Metadata, Moo, Name, Range, Reference, SymbolTable,
9
    },
10
    bug,
11
    rule_engine::{ApplicationError, ApplicationResult, RuleEffect, register_rule},
12
};
13

            
14
use ApplicationError::RuleNotApplicable;
15

            
16
type IntBoundsCache = HashMap<Name, (i32, i32)>;
17
type VisitingStack = Vec<Name>;
18

            
19
/// Rewrites variables in domains.
20
///
21
/// Solvers require variable declarations to have ground domains. For integer domains that contain variables in them, we widen to a finite ground superset-domain and add constraints that enforce membership in the original (possibly variable-dependent) domain.
22
#[register_rule("Base", 8990, [Root], symbols_only)]
23
2072519
fn handle_variables_in_domains(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
24
2072519
    let Expr::Root(_, _) = expr else {
25
10502
        return Err(RuleNotApplicable);
26
    };
27

            
28
    // Fast path: models whose finds are already ground never need widening, and this rule is
29
    // attempted at the root after every rewrite, so keep the symbol-table scan behind a cheap
30
    // test.
31
2062017
    if !symbols_need_domain_widening(symbols) {
32
2059398
        return Err(RuleNotApplicable);
33
2619
    }
34

            
35
2619
    let mut known_int_bounds: IntBoundsCache = HashMap::new();
36
2619
    let mut domain_guards = Vec::new();
37
2619
    let mut declaration_updates = Vec::new();
38

            
39
    // Collect declarations first to avoid iterator invalidation while mutating declarations.
40
2619
    let declarations: Vec<_> = symbols
41
2619
        .clone()
42
2619
        .into_iter_local()
43
        .map(|(_, decl)| decl)
44
        .collect();
45

            
46
2619
    for decl in declarations {
47
21879
        let Some(domain) = decl.as_find().map(|var| var.domain_of()) else {
48
8
            continue;
49
        };
50

            
51
21871
        if let Some(bounds) =
52
21871
            int_domain_bounds_from_domain(&domain, symbols, &mut known_int_bounds, &mut Vec::new())
53
        {
54
7291
            known_int_bounds.insert(decl.name().clone(), bounds);
55
14580
        }
56

            
57
21871
        if domain.resolve().is_ok() {
58
18603
            continue;
59
3268
        }
60

            
61
        let Some(widened_domain) =
62
3268
            resolve_or_widen_int_domain(&domain, symbols, &mut known_int_bounds, &mut Vec::new())
63
        else {
64
            return Err(RuleNotApplicable);
65
        };
66

            
67
3268
        let Some(guards) = domain_consistency_constraints(&decl, &domain) else {
68
            return Err(RuleNotApplicable);
69
        };
70

            
71
3268
        domain_guards.extend(guards);
72
3268
        let updated_kind = if decl.is_find_auxiliary() {
73
657
            DeclarationKind::FindAuxiliary(DecisionVariable::new(widened_domain))
74
        } else {
75
2611
            DeclarationKind::Find(DecisionVariable::new(widened_domain))
76
        };
77
3268
        declaration_updates.push((decl, updated_kind));
78
    }
79

            
80
2619
    if declaration_updates.is_empty() {
81
8
        return Err(RuleNotApplicable);
82
2611
    }
83

            
84
2611
    Ok(
85
2611
        RuleEffect::new(expr.clone(), domain_guards, SymbolTable::new())
86
2611
            .with_declaration_updates(declaration_updates),
87
2611
    )
88
2072519
}
89

            
90
/// Returns true iff some local declaration still has a non-ground domain.
91
///
92
/// Only unresolved domains can need widening / consistency guards. Ground integer finds must not
93
/// trigger the expensive symbol-table scan in [`handle_variables_in_domains`].
94
2062019
fn symbols_need_domain_widening(symbols: &SymbolTable) -> bool {
95
1012623986
    symbols.iter_local().any(|(_, declaration)| {
96
1012623986
        declaration.domain().is_some_and(|domain| {
97
            // `Domain::Unresolved` only means "not yet resolved", not "unresolvable": a domain
98
            // written against a `letting` is unresolved but resolves fine. Widening is needed only
99
            // when resolution actually fails, which is the condition the body acts on below.
100
            // Testing that here keeps the O(declarations) pass off every Root attempt in models
101
            // that merely name a domain letting.
102
1012623986
            matches!(domain.as_ref(), Domain::Unresolved(_)) && domain.resolve().is_err()
103
1012623986
        })
104
1012623986
    })
105
2062019
}
106

            
107
/// Resolves an integer domain when possible; otherwise computes a finite widened domain
108
/// by replacing symbolic bounds with conservative numeric bounds.
109
7174
fn resolve_or_widen_int_domain(
110
7174
    domain: &DomainPtr,
111
7174
    symbols: &SymbolTable,
112
7174
    known_int_bounds: &mut IntBoundsCache,
113
7174
    visiting: &mut VisitingStack,
114
7174
) -> Option<DomainPtr> {
115
7174
    if let Ok(resolved) = domain.resolve() {
116
3906
        return Some(resolved.into());
117
3268
    }
118

            
119
3268
    let ranges = domain.as_ref().as_int()?;
120
3268
    let widened_ranges: Vec<Range<i32>> = ranges
121
3268
        .iter()
122
3925
        .map(|range| int_range_bounds(range, symbols, known_int_bounds, visiting))
123
3925
        .map(|bounds| bounds.map(|(lo, hi)| Range::new(Some(lo), Some(hi))))
124
        .collect::<Option<Vec<_>>>()?;
125

            
126
3268
    Some(Domain::int_ground(widened_ranges))
127
7174
}
128

            
129
/// Returns overall numeric bounds for an unresolved integer domain.
130
25777
fn int_domain_bounds_from_domain(
131
25777
    domain: &DomainPtr,
132
25777
    symbols: &SymbolTable,
133
25777
    known_int_bounds: &mut IntBoundsCache,
134
25777
    visiting: &mut VisitingStack,
135
25777
) -> Option<(i32, i32)> {
136
25777
    let ranges = domain.as_ref().as_int()?;
137
11197
    let mut lower = i32::MAX;
138
11197
    let mut upper = i32::MIN;
139

            
140
11197
    for range in ranges {
141
13204
        let (lo, hi) = int_range_bounds(&range, symbols, known_int_bounds, visiting)?;
142
13204
        lower = lower.min(lo);
143
13204
        upper = upper.max(hi);
144
    }
145

            
146
11197
    Some((lower, upper))
147
25777
}
148

            
149
/// Computes numeric bounds for a possibly symbolic integer range.
150
17129
fn int_range_bounds(
151
17129
    range: &Range<IntVal>,
152
17129
    symbols: &SymbolTable,
153
17129
    known_int_bounds: &mut IntBoundsCache,
154
17129
    visiting: &mut VisitingStack,
155
17129
) -> Option<(i32, i32)> {
156
17129
    match range {
157
1278
        Range::Single(v) => int_val_bounds(v, symbols, known_int_bounds, visiting),
158
        Range::Bounded(l, r) => {
159
15851
            let (ll, lh) = int_val_bounds(l, symbols, known_int_bounds, visiting)?;
160
15851
            let (rl, rh) = int_val_bounds(r, symbols, known_int_bounds, visiting)?;
161
15851
            Some((ll.min(lh), rl.max(rh)))
162
        }
163
        Range::Unbounded | Range::UnboundedL(_) | Range::UnboundedR(_) => None,
164
    }
165
17129
}
166

            
167
/// Computes numeric bounds for an unresolved integer value.
168
32980
fn int_val_bounds(
169
32980
    value: &IntVal,
170
32980
    symbols: &SymbolTable,
171
32980
    known_int_bounds: &mut IntBoundsCache,
172
32980
    visiting: &mut VisitingStack,
173
32980
) -> Option<(i32, i32)> {
174
32980
    if let Ok(v) = value.resolve() {
175
23816
        return Some((v, v));
176
9164
    }
177

            
178
9164
    match value {
179
        IntVal::Const(v) => Some((*v as i32, *v as i32)),
180
        IntVal::Reference(reference) => {
181
5258
            let name = reference.name().clone();
182
5258
            int_bounds_for_name(&name, symbols, known_int_bounds, visiting)
183
        }
184
        IntVal::Expr(expr) => {
185
3906
            let domain = expression_int_bounds(expr, symbols, known_int_bounds, visiting)?;
186
3906
            int_domain_bounds_from_domain(&domain, symbols, known_int_bounds, visiting)
187
        }
188
    }
189
32980
}
190

            
191
/// Resolves cached or derived bounds for a declaration by name.
192
5258
fn int_bounds_for_name(
193
5258
    name: &Name,
194
5258
    symbols: &SymbolTable,
195
5258
    known_int_bounds: &mut IntBoundsCache,
196
5258
    visiting: &mut VisitingStack,
197
5258
) -> Option<(i32, i32)> {
198
5258
    if let Some(bounds) = known_int_bounds.get(name).copied() {
199
5258
        return Some(bounds);
200
    }
201

            
202
    if visiting.contains(name) {
203
        return None;
204
    }
205

            
206
    visiting.push(name.clone());
207
    let maybe_bounds = symbols
208
        .lookup(name)
209
        .and_then(|decl| decl.domain())
210
        .and_then(|domain| {
211
            int_domain_bounds_from_domain(&domain, symbols, known_int_bounds, visiting)
212
        });
213
    visiting.pop();
214
    let bounds = maybe_bounds?;
215
    known_int_bounds.insert(name.clone(), bounds);
216
    Some(bounds)
217
5258
}
218

            
219
/// Computes a conservative ground integer domain for an expression.
220
3906
fn expression_int_bounds(
221
3906
    expr: &Moo<Expr>,
222
3906
    symbols: &SymbolTable,
223
3906
    known_int_bounds: &mut IntBoundsCache,
224
3906
    visiting: &mut VisitingStack,
225
3906
) -> Option<DomainPtr> {
226
3906
    if let Some(Lit::Int(v)) = conjure_cp::ast::eval_constant(expr) {
227
        return Some(Domain::int_ground(vec![Range::Single(v)]));
228
3906
    }
229

            
230
3906
    let domain = expr.as_ref().domain_of()?;
231
3906
    resolve_or_widen_int_domain(&domain, symbols, known_int_bounds, visiting)
232
3906
}
233

            
234
/// Builds guards ensuring widened integer find domains still satisfy original symbolic bounds.
235
3268
fn domain_consistency_constraints(
236
3268
    declaration: &conjure_cp::ast::DeclarationPtr,
237
3268
    original_domain: &DomainPtr,
238
3268
) -> Option<Vec<Expr>> {
239
3268
    if original_domain.resolve().is_ok() {
240
        return Some(Vec::new());
241
3268
    }
242

            
243
3268
    let ranges = original_domain.as_ref().as_int()?;
244
3268
    if ranges.is_empty() {
245
        return None;
246
3268
    }
247

            
248
3268
    let var_expr = Expr::Atomic(
249
3268
        Metadata::new(),
250
3268
        Atom::Reference(Reference::new(declaration.clone())),
251
3268
    );
252
3268
    let mut allowed_intervals = Vec::new();
253

            
254
    let make_expr =
255
        |iv: IntVal| Expr::try_from(iv).unwrap_or_else(|e| bug!("invalid int domain range: {e}"));
256

            
257
3268
    for range in ranges {
258
3925
        let interval = match range {
259
            Range::Single(v) => Expr::Eq(
260
                Metadata::new(),
261
                Moo::new(var_expr.clone()),
262
                Moo::new(make_expr(v)),
263
            ),
264
            Range::Bounded(l, r) => {
265
3925
                let geq = Expr::Geq(
266
3925
                    Metadata::new(),
267
3925
                    Moo::new(var_expr.clone()),
268
3925
                    Moo::new(make_expr(l)),
269
3925
                );
270
3925
                let leq = Expr::Leq(
271
3925
                    Metadata::new(),
272
3925
                    Moo::new(var_expr.clone()),
273
3925
                    Moo::new(make_expr(r)),
274
3925
                );
275
3925
                Expr::And(
276
3925
                    Metadata::new(),
277
3925
                    Moo::new(conjure_cp::into_matrix_expr!(vec![geq, leq])),
278
3925
                )
279
            }
280
            Range::Unbounded | Range::UnboundedL(_) | Range::UnboundedR(_) => return None,
281
        };
282
3925
        allowed_intervals.push(interval);
283
    }
284

            
285
3268
    let guard = if allowed_intervals.len() == 1 {
286
2611
        allowed_intervals.remove(0)
287
    } else {
288
657
        Expr::Or(
289
657
            Metadata::new(),
290
657
            Moo::new(conjure_cp::into_matrix_expr!(allowed_intervals)),
291
657
        )
292
    };
293

            
294
3268
    Some(vec![guard])
295
3268
}
296

            
297
#[cfg(test)]
298
mod tests {
299
    use super::*;
300
    use conjure_cp::ast::{DeclarationPtr, Expression};
301
    use conjure_cp::rule_engine::{ApplicationError, get_rule_by_name};
302

            
303
    /// Builds a Root expression with no constraints (domain widening only looks at symbols).
304
2
    fn empty_root() -> Expression {
305
2
        Expression::Root(Metadata::new(), vec![])
306
2
    }
307

            
308
    #[test]
309
1
    fn symbols_need_domain_widening_false_for_ground_int_finds() {
310
1
        let mut symbols = SymbolTable::new();
311
1
        let x = DeclarationPtr::new_find(Name::user("x"), Domain::int(vec![Range::Bounded(1, 3)]));
312
1
        symbols.insert(x).expect("insert x");
313
1
        assert!(!symbols_need_domain_widening(&symbols));
314
1
    }
315

            
316
    #[test]
317
1
    fn symbols_need_domain_widening_true_for_unresolved_int_find() {
318
1
        let mut symbols = SymbolTable::new();
319
1
        let x = DeclarationPtr::new_find(Name::user("x"), Domain::int(vec![Range::Bounded(1, 3)]));
320
1
        symbols.insert(x.clone()).expect("insert x");
321
1
        let y_domain = Domain::int(vec![Range::Bounded(
322
1
            IntVal::Const(1),
323
1
            IntVal::Reference(Reference::new(x)),
324
1
        )]);
325
1
        let y = DeclarationPtr::new_find(Name::user("y"), y_domain);
326
1
        symbols.insert(y).expect("insert y");
327
1
        assert!(symbols_need_domain_widening(&symbols));
328
1
    }
329

            
330
    #[test]
331
1
    fn handle_variables_in_domains_skips_ground_only_models() {
332
1
        let mut symbols = SymbolTable::new();
333
1
        let x = DeclarationPtr::new_find(Name::user("x"), Domain::int(vec![Range::Bounded(1, 3)]));
334
1
        symbols.insert(x).expect("insert x");
335
1
        let rule = get_rule_by_name("handle_variables_in_domains").expect("rule registered");
336
1
        let err = rule.apply(&empty_root(), &symbols).unwrap_err();
337
1
        assert!(matches!(err, ApplicationError::RuleNotApplicable));
338
1
    }
339

            
340
    #[test]
341
1
    fn handle_variables_in_domains_widens_unresolved_int_find() {
342
1
        let mut symbols = SymbolTable::new();
343
1
        let x = DeclarationPtr::new_find(Name::user("x"), Domain::int(vec![Range::Bounded(1, 3)]));
344
1
        symbols.insert(x.clone()).expect("insert x");
345
1
        let y_domain = Domain::int(vec![Range::Bounded(
346
1
            IntVal::Const(1),
347
1
            IntVal::Reference(Reference::new(x)),
348
1
        )]);
349
1
        let y = DeclarationPtr::new_find(Name::user("y"), y_domain);
350
1
        symbols.insert(y.clone()).expect("insert y");
351

            
352
1
        let rule = get_rule_by_name("handle_variables_in_domains").expect("rule registered");
353
1
        let result = rule.apply(&empty_root(), &symbols).expect("should widen y");
354
1
        let updated: Vec<_> = result.updated_declaration_names().collect();
355
1
        assert!(
356
1
            updated.contains(&Name::user("y")),
357
            "expected declaration update for unresolved find y, got {updated:?}"
358
        );
359
1
        assert!(
360
1
            !result.new_top.is_empty(),
361
            "expected consistency guards for widened domain"
362
        );
363
1
    }
364
}