1
use conjure_cp::ast::{Atom, Expression as Expr, Literal};
2
use conjure_cp::ast::{SATIntEncoding, SymbolTable};
3
use conjure_cp::rule_engine::ApplicationError;
4
use conjure_cp::rule_engine::{
5
    ApplicationError::RuleNotApplicable, ApplicationResult, RuleEffect, register_rule,
6
};
7

            
8
use conjure_cp::ast::Metadata;
9
use conjure_cp::ast::Moo;
10
use conjure_cp::into_matrix_expr;
11

            
12
use crate::backends::sat::boolean::tseytin_or;
13

            
14
use conjure_cp::ast::SatEncodingDecision;
15
/// This function confirms that all of the input expressions are direct SATInts, and returns vectors for each input of their bits
16
/// This function also normalizes direct SATInt operands to a common value range by zero-padding.
17
38403
pub fn validate_direct_int_operands(
18
38403
    exprs: Vec<Expr>,
19
38403
) -> Result<(Vec<Vec<Expr>>, i32, i32), ApplicationError> {
20
    // TODO: In the future it may be possible to optimize operations between integers with different bit sizes
21
    // Collect inner bit vectors from each SATInt
22

            
23
    // Iterate over all inputs
24
    // Check they are direct and calulate a lower and upper bound
25
38403
    let mut global_min: i32 = i32::MAX;
26
38403
    let mut global_max: i32 = i32::MIN;
27

            
28
38403
    for operand in &exprs {
29
38403
        let Expr::SATInt(_, SATIntEncoding::Direct, _, (local_min, local_max)) = operand else {
30
31113
            return Err(RuleNotApplicable);
31
        };
32
7290
        global_min = global_min.min(*local_min);
33
7290
        global_max = global_max.max(*local_max);
34
    }
35

            
36
    // build out by iterating over each operand and expanding it to match the new bounds
37

            
38
7290
    let out: Vec<Vec<Expr>> = exprs
39
7290
        .into_iter()
40
7290
        .map(|expr| {
41
7290
            let Expr::SATInt(_, SATIntEncoding::Direct, inner, (local_min, local_max)) = expr
42
            else {
43
                return Err(RuleNotApplicable);
44
            };
45

            
46
7290
            let Some(v) = inner.as_ref().clone().into_list() else {
47
                return Err(RuleNotApplicable);
48
            };
49

            
50
            // calulcate how many zeroes to prepend/append
51
7290
            let prefix_len = (local_min - global_min) as usize;
52
7290
            let postfix_len = (global_max - local_max) as usize;
53

            
54
7290
            let mut bits = Vec::with_capacity(v.len() + prefix_len + postfix_len);
55

            
56
            // add 0s to start
57
7290
            bits.extend(std::iter::repeat_n(
58
7290
                Expr::Atomic(Metadata::new(), Atom::Literal(Literal::Bool(false))),
59
7290
                prefix_len,
60
7290
            ));
61

            
62
7290
            bits.extend(v);
63

            
64
            // add 0s to end
65
7290
            bits.extend(std::iter::repeat_n(
66
7290
                Expr::Atomic(Metadata::new(), Atom::Literal(Literal::Bool(false))),
67
7290
                postfix_len,
68
7290
            ));
69

            
70
7290
            Ok(bits)
71
7290
        })
72
        .collect::<Result<_, _>>()?;
73

            
74
7290
    Ok((out, global_min, global_max))
75
38403
}
76

            
77
/// Converts a - expression for a SATInt to a new SATInt
78
///
79
/// ```text
80
/// -SATInt(a) ~> SATInt(b)
81
///
82
/// ```
83
#[register_rule("SAT", 9100, [Neg])]
84
22617
fn neg_sat_direct(expr: &Expr, _: &SymbolTable) -> ApplicationResult {
85
22617
    let Expr::Neg(_, value) = expr else {
86
27
        return Err(RuleNotApplicable);
87
    };
88

            
89
22590
    let (binding, old_min, old_max) = validate_direct_int_operands(vec![value.as_ref().clone()])?;
90
1620
    let [val_bits] = binding.as_slice() else {
91
        return Err(RuleNotApplicable);
92
    };
93

            
94
1620
    let new_min = -old_max;
95
1620
    let new_max = -old_min;
96

            
97
1620
    let mut out = val_bits.clone();
98
1620
    out.reverse();
99

            
100
1620
    Ok(RuleEffect::pure(Expr::SATInt(
101
1620
        Metadata::new(),
102
1620
        SATIntEncoding::Direct,
103
1620
        Moo::new(into_matrix_expr!(out)),
104
1620
        (new_min, new_max),
105
1620
    )))
106
22617
}
107

            
108
use super::super::floor_div;
109

            
110
/// Converts a / expression between two direct SATInts to a new direct SATInt
111
/// using the "lookup table" method.
112
///
113
/// ```text
114
/// SafeDiv(SATInt(a), SATInt(b)) ~> SATInt(c)
115
///
116
/// ```
117
#[register_rule("SAT", 9100, [SafeDiv])]
118
439614
fn safediv_sat_direct(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
119
439614
    let Expr::SafeDiv(_, numer_expr, denom_expr) = expr else {
120
27
        return Err(RuleNotApplicable);
121
    };
122

            
123
    let Expr::SATInt(_, SATIntEncoding::Direct, numer_inner, (numer_min, numer_max)) =
124
439587
        numer_expr.as_ref()
125
    else {
126
310797
        return Err(RuleNotApplicable);
127
    };
128
128790
    let Some(numer_bits) = numer_inner.as_ref().clone().into_list() else {
129
        return Err(RuleNotApplicable);
130
    };
131

            
132
    let Expr::SATInt(_, SATIntEncoding::Direct, denom_inner, (denom_min, denom_max)) =
133
128790
        denom_expr.as_ref()
134
    else {
135
        return Err(RuleNotApplicable);
136
    };
137

            
138
128790
    let Some(denom_bits) = denom_inner.as_ref().clone().into_list() else {
139
        return Err(RuleNotApplicable);
140
    };
141

            
142
128790
    let mut quot_min = i32::MAX;
143
128790
    let mut quot_max = i32::MIN;
144

            
145
128790
    for i in *numer_min..=*numer_max {
146
1375785
        for j in *denom_min..=*denom_max {
147
1590840
            let k = if j == 0 {
148
35640
                0
149
            } else {
150
1555200
                floor_div(i, j).ok_or(RuleNotApplicable)?
151
            };
152
1590840
            quot_min = quot_min.min(k);
153
1590840
            quot_max = quot_max.max(k);
154
        }
155
    }
156

            
157
128790
    let mut new_symbols = symbols.clone();
158
128790
    let mut quot_bits = Vec::new();
159

            
160
    // generate boolean variables for all possible quotients
161
128790
    for _ in quot_min..=quot_max {
162
696600
        let decl = new_symbols.gen_find_auxiliary(&conjure_cp::ast::Domain::bool());
163
696600
        quot_bits.push(Expr::Atomic(
164
696600
            Metadata::new(),
165
696600
            Atom::Reference(conjure_cp::ast::Reference::new(decl)),
166
696600
        ));
167
    }
168

            
169
128790
    let mut new_sat_decisions = vec![];
170

            
171
    // Record lookup-table implications: (n_i AND d_j) => q_k
172
128790
    for i in *numer_min..=*numer_max {
173
1375785
        let numer_bit = &numer_bits[(i - numer_min) as usize];
174
1375785
        for j in *denom_min..=*denom_max {
175
1590840
            let denom_bit = &denom_bits[(j - denom_min) as usize];
176

            
177
1590840
            let k = if j == 0 {
178
35640
                0
179
            } else {
180
1555200
                floor_div(i, j).ok_or(RuleNotApplicable)?
181
            };
182

            
183
1590840
            let quot_bit = &quot_bits[(k - quot_min) as usize];
184

            
185
1590840
            let premise = Expr::And(
186
1590840
                Metadata::new(),
187
1590840
                Moo::new(into_matrix_expr!(vec![
188
1590840
                    numer_bit.clone(),
189
1590840
                    denom_bit.clone()
190
1590840
                ])),
191
1590840
            );
192
1590840
            new_sat_decisions.push(SatEncodingDecision::Assert(Expr::Imply(
193
1590840
                Metadata::new(),
194
1590840
                Moo::new(premise),
195
1590840
                Moo::new(quot_bit.clone()),
196
1590840
            )));
197
        }
198
    }
199

            
200
128790
    new_sat_decisions.push(SatEncodingDecision::AtMostOne {
201
128790
        inputs: quot_bits.clone(),
202
128790
        encoding: None,
203
128790
    });
204

            
205
128790
    let quot_int = Expr::SATInt(
206
128790
        Metadata::new(),
207
128790
        SATIntEncoding::Direct,
208
128790
        Moo::new(into_matrix_expr!(quot_bits)),
209
128790
        (quot_min, quot_max),
210
128790
    );
211

            
212
128790
    Ok(RuleEffect::sat(quot_int, new_sat_decisions, new_symbols))
213
439614
}
214

            
215
/// Matches a `|SATInt|` with an absolute value operation and rewrites it to a direct-encoded absolute-value `SATInt` by grouping input indicator bits by `|value|` and OR-ing each group (named here as buckets) into the corresponding output bit.
216
#[register_rule("SAT", 9100, [Abs])]
217
15840
fn abs_value_sat_direct(expr: &Expr, symbols: &SymbolTable) -> ApplicationResult {
218
15840
    let Expr::Abs(_, value_expr) = expr else {
219
27
        return Err(RuleNotApplicable);
220
    };
221

            
222
    let (binding, old_min, old_max) =
223
15813
        validate_direct_int_operands(vec![value_expr.as_ref().clone()])?;
224

            
225
5670
    let [val_bits] = binding.as_slice() else {
226
        return Err(RuleNotApplicable);
227
    };
228

            
229
    // The new range is from the minimum absolute value to the maximum absolute value. The minimum absolute value is either 0 if the old range includes 0, or the smaller of the absolute values of the old min and max if the old range does not include 0. The maximum absolute value is the larger of the absolute values of the old min and max.
230
5670
    let new_min = if old_min <= 0 && old_max >= 0 {
231
4860
        0
232
    } else {
233
810
        old_min.abs().min(old_max.abs())
234
    };
235
5670
    let new_max = old_min.abs().max(old_max.abs());
236

            
237
5670
    let mut new_symbols = symbols.clone();
238
5670
    let mut new_sat_decisions = vec![];
239

            
240
5670
    let bucket_count = (new_max - new_min + 1) as usize;
241
5670
    let mut buckets: Vec<Vec<Expr>> = vec![Vec::new(); bucket_count];
242

            
243
    // Iterates over all possible input values, calculate their absolute value, and place them in the corresponding bucket. Each bucket corresponds to a possible output value, and contains the disjunction of all input bits that could produce that output.
244
5670
    for value in old_min..=old_max {
245
61155
        let input_bit = val_bits[(value - old_min) as usize].clone();
246
61155
        let abs_value = value.abs();
247
61155
        let bucket_idx = (abs_value - new_min) as usize;
248
61155
        buckets[bucket_idx].push(input_bit);
249
    }
250

            
251
    // For each bucket, if it's empty then the output bit is false, if it contains one element then the output bit is that element, and if it contains multiple elements then the output bit is the OR of all elements in the bucket.
252
5670
    let mut abs_bits = Vec::with_capacity(bucket_count);
253
5670
    for bucket in buckets {
254
37665
        let out_bit = match bucket.len() {
255
            0 => Expr::Atomic(Metadata::new(), Atom::Literal(Literal::Bool(false))),
256
14175
            1 => bucket[0].clone(),
257
23490
            _ => tseytin_or(&bucket, &mut new_sat_decisions, &mut new_symbols),
258
        };
259

            
260
37665
        abs_bits.push(out_bit);
261
    }
262

            
263
5670
    let abs_int = Expr::SATInt(
264
5670
        Metadata::new(),
265
5670
        SATIntEncoding::Direct,
266
5670
        Moo::new(into_matrix_expr!(abs_bits)),
267
5670
        (new_min, new_max),
268
5670
    );
269

            
270
5670
    Ok(RuleEffect::sat(abs_int, new_sat_decisions, new_symbols))
271
15840
}