use crate::BitVec;
use crate::expr::Expr;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Explanation {
Fold {
known_zero: BitVec,
known_one: BitVec,
value: BitVec,
},
Linear {
konst: BitVec,
terms: Vec<(Expr, BitVec)>,
},
Xor {
konst: BitVec,
terms: Vec<(Expr, BitVec)>,
},
Bitwise {
atoms: Vec<Expr>,
table: u64,
},
Relations {
x: Expr,
y: Expr,
set: u8,
},
Intervals {
x: Expr,
set: Vec<(BitVec, BitVec)>,
},
LinearMba {
atoms: Vec<Expr>,
corners: Vec<BitVec>,
form: &'static str,
},
Shuffle {
bits: Vec<Source>,
},
Casts {
source: Expr,
lo: u16,
bits: u16,
sign_extended: bool,
},
Demanded {
operand: Expr,
mask: BitVec,
},
Invert {
layers: Vec<Peel>,
},
Division {
dividend: Expr,
divisor: BitVec,
signed: bool,
},
Carries {
konst: BitVec,
terms: Vec<(Expr, BitVec)>,
proved: bool,
},
Mba {
atoms: Vec<Expr>,
evidence: &'static str,
test: Option<&'static str>,
points: u64,
},
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Source {
Zero,
One,
Bit {
of: Expr,
bit: u16,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Peel {
pub node: Expr,
pub inner: Expr,
pub map: &'static str,
pub bijective: bool,
pub preimage: Option<BitVec>,
}
pub(super) fn exprs(why: &Explanation, f: &mut dyn FnMut(Expr)) {
match why {
Explanation::Linear { terms, .. }
| Explanation::Xor { terms, .. }
| Explanation::Carries { terms, .. } => terms.iter().for_each(|(e, _)| f(*e)),
Explanation::Bitwise { atoms, .. }
| Explanation::LinearMba { atoms, .. }
| Explanation::Mba { atoms, .. } => atoms.iter().for_each(|&e| f(e)),
Explanation::Relations { x, y, .. } => {
f(*x);
f(*y);
}
Explanation::Intervals { x, .. } => f(*x),
Explanation::Shuffle { bits } => {
for b in bits {
if let Source::Bit { of, .. } = b {
f(*of);
}
}
}
Explanation::Demanded { operand, .. } => f(*operand),
Explanation::Casts { source, .. } => f(*source),
Explanation::Invert { layers } => {
for l in layers {
f(l.node);
f(l.inner);
}
}
Explanation::Division { dividend, .. } => f(*dividend),
Explanation::Fold { .. } => {}
}
}
pub(super) fn write(out: &mut String, why: &Explanation, id: &dyn Fn(Expr) -> u64) {
use super::trace::{Obj, jstr, num};
let hex = |v: &BitVec| format!("{v:#x}");
let pairs = |o: &mut Obj<'_>, k: &str, terms: &[(Expr, BitVec)]| {
o.key(k);
o.out.push('[');
for (j, (e, v)) in terms.iter().enumerate() {
if j > 0 {
o.out.push(',');
}
o.out.push('[');
num(o.out, id(*e));
o.out.push(',');
jstr(o.out, &hex(v));
o.out.push(']');
}
o.out.push(']');
};
let ids = |o: &mut Obj<'_>, k: &str, es: &[Expr]| {
o.key(k);
o.out.push('[');
for (j, e) in es.iter().enumerate() {
if j > 0 {
o.out.push(',');
}
num(o.out, id(*e));
}
o.out.push(']');
};
let mut o = Obj::new(out);
match why {
Explanation::Fold {
known_zero,
known_one,
value,
} => {
o.str("kind", "fold");
o.str("zero", &hex(known_zero));
o.str("one", &hex(known_one));
o.str("value", &hex(value));
}
Explanation::Linear { konst, terms } => {
o.str("kind", "linear");
o.str("konst", &hex(konst));
pairs(&mut o, "terms", terms);
}
Explanation::Xor { konst, terms } => {
o.str("kind", "xor");
o.str("konst", &hex(konst));
pairs(&mut o, "terms", terms);
}
Explanation::Bitwise { atoms, table } => {
o.str("kind", "bitwise");
ids(&mut o, "atoms", atoms);
o.str("table", &format!("{table:#x}"));
}
Explanation::Relations { x, y, set } => {
o.str("kind", "relations");
o.num("x", id(*x));
o.num("y", id(*y));
o.num("set", u64::from(*set));
}
Explanation::Intervals { x, set } => {
o.str("kind", "intervals");
o.num("x", id(*x));
o.key("set");
o.out.push('[');
for (j, (lo, hi)) in set.iter().enumerate() {
if j > 0 {
o.out.push(',');
}
o.out.push('[');
jstr(o.out, &hex(lo));
o.out.push(',');
jstr(o.out, &hex(hi));
o.out.push(']');
}
o.out.push(']');
}
Explanation::LinearMba {
atoms,
corners,
form,
} => {
o.str("kind", "linear_mba");
ids(&mut o, "atoms", atoms);
o.key("corners");
o.out.push('[');
for (j, v) in corners.iter().enumerate() {
if j > 0 {
o.out.push(',');
}
jstr(o.out, &hex(v));
}
o.out.push(']');
o.str("form", form);
}
Explanation::Shuffle { bits } => {
o.str("kind", "shuffle");
o.key("bits");
o.out.push('[');
for (j, b) in bits.iter().enumerate() {
if j > 0 {
o.out.push(',');
}
match b {
Source::Zero => o.out.push('0'),
Source::One => o.out.push('1'),
Source::Bit { of, bit } => {
o.out.push('[');
num(o.out, id(*of));
o.out.push(',');
num(o.out, u64::from(*bit));
o.out.push(']');
}
}
}
o.out.push(']');
}
Explanation::Casts {
source,
lo,
bits,
sign_extended,
} => {
o.str("kind", "casts");
o.num("source", id(*source));
o.num("lo", u64::from(*lo));
o.num("bits", u64::from(*bits));
o.bool("sign_extended", *sign_extended);
}
Explanation::Demanded { operand, mask } => {
o.str("kind", "demanded");
o.num("operand", id(*operand));
o.str("mask", &hex(mask));
}
Explanation::Invert { layers } => {
o.str("kind", "invert");
o.key("layers");
o.out.push('[');
for (k, l) in layers.iter().enumerate() {
if k > 0 {
o.out.push(',');
}
let mut p = Obj::new(o.out);
p.num("node", id(l.node));
p.num("inner", id(l.inner));
p.str("map", l.map);
p.bool("bijective", l.bijective);
p.key("preimage");
match &l.preimage {
Some(v) => jstr(p.out, &hex(v)),
None => p.out.push_str("null"),
}
p.end();
}
o.out.push(']');
}
Explanation::Division {
dividend,
divisor,
signed,
} => {
o.str("kind", "division");
o.num("dividend", id(*dividend));
o.str("divisor", &hex(divisor));
o.bool("signed", *signed);
}
Explanation::Carries {
konst,
terms,
proved,
} => {
o.str("kind", "carries");
o.str("konst", &hex(konst));
pairs(&mut o, "terms", terms);
o.bool("proved", *proved);
}
Explanation::Mba {
atoms,
evidence,
test,
points,
} => {
o.str("kind", "mba");
ids(&mut o, "atoms", atoms);
o.str("evidence", evidence);
if let Some(t) = test {
o.str("test", t);
}
o.num("points", *points);
}
}
o.end();
}