1
//! Normalising rules for `Sum`
2

            
3
use ApplicationError::RuleNotApplicable;
4
use conjure_cp::{
5
    ast::{Expression as Expr, SymbolTable},
6
    rule_engine::{ApplicationError, ApplicationResult, Reduction, register_rule},
7
};
8

            
9
/// Removes sums with a single argument.
10
///
11
/// ```text
12
/// sum([a]) ~> a
13
/// ```
14
#[register_rule(("Base", 8800))]
15
fn remove_unit_vector_sum(expr: &Expr, _: &SymbolTable) -> ApplicationResult {
16
    let Expr::Sum(_, e) = expr else {
17
        return Err(RuleNotApplicable);
18
    };
19

            
20
    let exprs = e.as_ref().clone().unwrap_list().ok_or(RuleNotApplicable)?;
21

            
22
    if exprs.len() == 1 {
23
        Ok(Reduction::pure(exprs[0].clone()))
24
    } else {
25
        Err(RuleNotApplicable)
26
    }
27
}