1
//! Identity-default selection shares ordinary indexing and definedness handling.
2
use conjure_cp::ast::{
3
    AbstractLiteral, Atom, Expression as Expr, Literal, Metadata, Moo, SymbolTable,
4
};
5
use conjure_cp::rule_engine::{
6
    ApplicationError::RuleNotApplicable, ApplicationResult, RuleEffect, register_rule,
7
};
8

            
9
#[register_rule("Base", 19000, [ElementId])]
10
20129
fn element_id_to_identity_lookup(expr: &Expr, _: &SymbolTable) -> ApplicationResult {
11
20129
    let Expr::ElementId(_, matrix, index) = expr else {
12
11336
        return Err(RuleNotApplicable);
13
    };
14
    // Minion literal lookups must wait for resolved labels and its total element lowering.
15
8793
    if conjure_cp::settings::try_current_solver_family()
16
8793
        == Some(conjure_cp::settings::SolverFamily::Minion)
17
36
        && matches!(
18
36
            matrix.as_ref(),
19
            Expr::AbstractLiteral(_, AbstractLiteral::Matrix(..))
20
                | Expr::Atomic(
21
                    _,
22
                    Atom::Literal(Literal::AbstractLiteral(AbstractLiteral::Matrix(..)))
23
                )
24
        )
25
    {
26
        return Err(RuleNotApplicable);
27
8793
    }
28
8793
    if matches!(matrix.as_ref(), Expr::Atomic(_, Atom::Literal(Literal::AbstractLiteral(AbstractLiteral::Matrix(entries, _)))) if entries.is_empty())
29
8793
        || matrix
30
8793
            .unwrap_matrix_unchecked_ref()
31
            .is_some_and(|(entries, _)| entries.is_empty())
32
    {
33
        let default = if index.domain_of().is_some_and(|domain| domain.is_bool()) {
34
            Expr::ToInt(Metadata::new(), index.clone())
35
        } else {
36
            (**index).clone()
37
        };
38
        return Ok(RuleEffect::pure(default));
39
8793
    }
40
    // Convert Boolean entries before indexing so Boolean bubbling cannot turn an undefined
41
    // lookup into false before catchUndef sees its numeric definedness condition.
42
8793
    let domain = matrix.domain_of().ok_or(RuleNotApplicable)?;
43
8793
    let (entries_domain, indices) = domain.as_matrix().ok_or(RuleNotApplicable)?;
44
8793
    let subject = if entries_domain.is_bool() {
45
        let (entries, labels) =
46
            if let Some((entries, labels)) = matrix.unwrap_matrix_unchecked_ref() {
47
                (entries.to_vec(), labels.clone())
48
            } else {
49
                let [labels] = indices.as_slice() else {
50
                    return Err(RuleNotApplicable);
51
                };
52
                let entries = labels
53
                    .resolve()
54
                    .map_err(|_| RuleNotApplicable)?
55
                    .values()
56
                    .map_err(|_| RuleNotApplicable)?
57
                    .map(|label| {
58
                        conjure_cp::ast::matrix::safe_index_optimised((**matrix).clone(), label)
59
                    })
60
                    .collect::<Option<Vec<_>>>()
61
                    .ok_or(RuleNotApplicable)?;
62
                (entries, labels.clone())
63
            };
64
        let entries = entries
65
            .into_iter()
66
            .map(|entry| Expr::ToInt(Metadata::new(), Moo::new(entry)))
67
            .collect();
68
        Moo::new(conjure_cp::into_matrix_expr![entries; labels])
69
    } else {
70
8793
        matrix.clone()
71
    };
72
8793
    let lookup = Expr::UnsafeIndex(Metadata::new(), subject, vec![(**index).clone()]);
73
17586
    let numeric = |expression: Expr| {
74
17586
        let domain = expression.domain_of()?;
75
17586
        if domain.is_bool() {
76
            Some(Expr::ToInt(Metadata::new(), Moo::new(expression)))
77
17586
        } else if domain.is_int() {
78
17586
            Some(expression)
79
        } else {
80
            None
81
        }
82
17586
    };
83
8793
    let lookup = numeric(lookup).ok_or(RuleNotApplicable)?;
84
8793
    let default = numeric((**index).clone()).ok_or(RuleNotApplicable)?;
85
8793
    Ok(RuleEffect::pure(Expr::CatchUndef(
86
8793
        Metadata::new(),
87
8793
        Moo::new(lookup),
88
8793
        Moo::new(default),
89
8793
    )))
90
20129
}