1
use conjure_core::ast::{Expression, ReturnType};
2
use conjure_core::metadata::Metadata;
3
use conjure_core::rule_engine::{
4
    register_rule, register_rule_set, ApplicationError, ApplicationError::*, ApplicationResult,
5
    Reduction,
6
};
7
use conjure_core::Model;
8
use uniplate::Uniplate;
9

            
10
use super::utils::is_all_constant;
11

            
12
register_rule_set!("Bubble", 100, ("Base"));
13

            
14
// Bubble reduction rules
15

            
16
/*
17
    Reduce bubbles with a boolean expression to a conjunction with their condition.
18

            
19
    e.g. (a / b = c) @ (b != 0) => (a / b = c) & (b != 0)
20
*/
21
#[register_rule(("Bubble", 8900))]
22
93075
fn expand_bubble(expr: &Expression, _: &Model) -> ApplicationResult {
23
221
    match expr {
24
221
        Expression::Bubble(_, a, b) if a.return_type() == Some(ReturnType::Bool) => {
25
221
            Ok(Reduction::pure(Expression::And(
26
221
                Metadata::new(),
27
221
                vec![*a.clone(), *b.clone()],
28
221
            )))
29
        }
30
92854
        _ => Err(ApplicationError::RuleNotApplicable),
31
    }
32
93075
}
33

            
34
/*
35
    Bring bubbles with a non-boolean expression higher up the tree.
36

            
37
    E.g. ((a / b) @ (b != 0)) = c => (a / b = c) @ (b != 0)
38
*/
39
#[register_rule(("Bubble", 8900))]
40
93075
fn bubble_up(expr: &Expression, _: &Model) -> ApplicationResult {
41
93075
    let mut sub = expr.children();
42
93075
    let mut bubbled_conditions = vec![];
43
99586
    for e in sub.iter_mut() {
44
99586
        if let Expression::Bubble(_, a, b) = e {
45
374
            if a.return_type() != Some(ReturnType::Bool) {
46
272
                bubbled_conditions.push(*b.clone());
47
272
                *e = *a.clone();
48
272
            }
49
99212
        }
50
    }
51
93075
    if bubbled_conditions.is_empty() {
52
92803
        return Err(ApplicationError::RuleNotApplicable);
53
272
    }
54
272
    Ok(Reduction::pure(Expression::Bubble(
55
272
        Metadata::new(),
56
272
        Box::new(expr.with_children(sub)),
57
272
        Box::new(Expression::And(Metadata::new(), bubbled_conditions)),
58
272
    )))
59
93075
}
60

            
61
// Bubble applications
62

            
63
/*
64
    Convert an unsafe division to a safe division with a bubble condition.
65

            
66
    Division by zero is undefined and therefore not allowed, so we add a condition to check for it.
67
    This condition is brought up the tree and expanded into a conjunction with the first boolean-type expression it is paired with.
68

            
69
    E.g. a / b => (a / b) @ (b != 0)
70

            
71
*/
72
#[register_rule(("Bubble", 6000))]
73
93075
fn div_to_bubble(expr: &Expression, _: &Model) -> ApplicationResult {
74
93075
    if is_all_constant(expr) {
75
19057
        return Err(RuleNotApplicable);
76
74018
    }
77
74018
    if let Expression::UnsafeDiv(_, a, b) = expr {
78
        // bubble bottom up
79
323
        if a.can_be_undefined() || b.can_be_undefined() {
80
102
            return Err(RuleNotApplicable);
81
221
        }
82

            
83
        // only do one bubble at a time
84
221
        if matches!(**a, Expression::Bubble(_, _, _)) || matches!(**b, Expression::Bubble(_, _, _))
85
        {
86
            return Err(RuleNotApplicable);
87
221
        }
88
221

            
89
221
        return Ok(Reduction::pure(Expression::Bubble(
90
221
            Metadata::new(),
91
221
            Box::new(Expression::SafeDiv(Metadata::new(), a.clone(), b.clone())),
92
221
            Box::new(Expression::Neq(
93
221
                Metadata::new(),
94
221
                b.clone(),
95
221
                Box::new(Expression::from(0)),
96
221
            )),
97
221
        )));
98
73695
    }
99
73695
    Err(ApplicationError::RuleNotApplicable)
100
93075
}