use std::collections::HashMap;
use super::sat::Lit;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Step {
Add {
id: u64,
clause: Vec<Lit>,
hints: Vec<u64>,
},
Delete {
id: u64,
},
}
pub fn from_drup(cnf: &[Vec<Lit>], proof: &[super::sat::Step]) -> Result<Vec<Step>, String> {
super::drup::check_to_lrat(cnf, proof)
}
pub fn check(cnf: &[Vec<Lit>], proof: &[Step]) -> Result<(), String> {
let mut clauses: HashMap<u64, &[Lit]> = HashMap::with_capacity(cnf.len() + proof.len());
for (k, c) in cnf.iter().enumerate() {
if c.is_empty() {
return Ok(());
}
clauses.insert(k as u64 + 1, c);
}
let mut last = cnf.len() as u64;
let mut falsified: HashMap<Lit, ()> = HashMap::new();
for (n, step) in proof.iter().enumerate() {
match step {
Step::Delete { id } => {
clauses.remove(id);
}
Step::Add { id, clause, hints } => {
if *id <= last {
return Err(format!(
"step {n}: clause {id} is not numbered above {last}"
));
}
last = *id;
falsified.clear();
let mut conflict = false;
for &l in clause {
conflict |= falsified.contains_key(&!l);
falsified.insert(l, ());
}
for h in hints {
if conflict {
break;
}
let Some(c) = clauses.get(h) else {
return Err(format!("step {n}: clause {h} is not there"));
};
let mut open = None;
let mut more = false;
for &l in c.iter() {
if falsified.contains_key(&l) {
continue;
}
if falsified.contains_key(&!l) || open.is_some_and(|o| o != l) {
more = true;
break;
}
open = Some(l);
}
if more {
return Err(format!("step {n}: clause {h} is not unit"));
}
match open {
None => conflict = true,
Some(l) => {
falsified.insert(!l, ());
}
}
}
if !conflict {
return Err(format!(
"step {n}: the hints of clause {id} end without a conflict"
));
}
if clause.is_empty() {
return Ok(());
}
clauses.insert(*id, clause);
}
}
}
Err("the proof does not add the empty clause".into())
}
pub fn to_text(proof: &[Step]) -> String {
use core::fmt::Write;
let mut s = String::new();
let mut last = 0;
for step in proof {
match step {
Step::Add { id, clause, hints } => {
last = *id;
let _ = write!(s, "{id} ");
for l in clause {
let _ = write!(s, "{} ", l.dimacs());
}
s.push_str("0 ");
for h in hints {
let _ = write!(s, "{h} ");
}
s.push_str("0\n");
}
Step::Delete { id } => {
let _ = writeln!(s, "{last} d {id} 0");
}
}
}
s
}
#[cfg(test)]
mod tests {
use super::*;
use crate::prove::sat::Step as Drup;
fn lit(d: i32) -> Lit {
let l = Lit::pos(d.unsigned_abs() - 1);
if d < 0 { !l } else { l }
}
fn clause(ds: &[i32]) -> Vec<Lit> {
ds.iter().map(|&d| lit(d)).collect()
}
#[test]
fn a_drup_proof_becomes_lrat_and_a_wrong_hint_is_refused() {
let cnf = [
clause(&[1, 2]),
clause(&[-1, 2]),
clause(&[1, -2]),
clause(&[-1, -2]),
];
let drup = [
Drup::Add(clause(&[2])),
Drup::Delete(clause(&[1, 2])),
Drup::Add(vec![]),
];
let steps = from_drup(&cnf, &drup).unwrap();
check(&cnf, &steps).unwrap();
assert_eq!(to_text(&steps), "5 2 0 1 2 0\n5 d 1 0\n6 0 5 3 4 0\n");
let with = |f: &dyn Fn(&mut Vec<Step>)| {
let mut s = steps.clone();
f(&mut s);
check(&cnf, &s)
};
let hints = |s: &mut Vec<Step>, k: usize, h: &[u64]| {
if let Step::Add { hints, .. } = &mut s[k] {
*hints = h.to_vec();
}
};
assert!(
with(&|s| hints(s, 0, &[3, 1]))
.unwrap_err()
.contains("not unit")
);
assert!(
with(&|s| hints(s, 0, &[1]))
.unwrap_err()
.contains("without a conflict")
);
assert!(
with(&|s| hints(s, 2, &[5, 1, 4]))
.unwrap_err()
.contains("not there")
);
assert!(with(&|s| s.swap(0, 2)).is_err());
assert!(
with(&|s| s.truncate(2))
.unwrap_err()
.contains("empty clause")
);
assert!(from_drup(&cnf[..3], &[Drup::Add(clause(&[-1]))]).is_err());
}
}