Skip to main content

logicaffeine_compile/codegen_sva/
sva_to_proof.rs

1//! Bounded SVA/FOL obligation → `ProofExpr` (the certified-prover seam).
2//!
3//! `BoundedExpr` is an SVA assertion (or Kripke-lowered FOL spec) already unrolled to
4//! discrete timesteps, with each signal-at-time a `signal@t` variable. This module lowers
5//! that bounded obligation into the `logicaffeine_proof::ProofExpr` vocabulary, where the
6//! pure-Rust CDCL → RUP → kernel trust tiers discharge it with a certificate — the very seam
7//! the grid solver uses, and the reason hardware proving runs in the browser with no Z3.
8//!
9//! Boolean (1-bit signal) obligations lower directly. Data-path operands — integers,
10//! bitvectors, arrays, comparisons, quantifiers, uninterpreted applications — return `None`,
11//! signalling that the caller must bit-blast them to the Boolean fragment first (the next
12//! layer). Returning `None` rather than guessing keeps the lowering fail-closed.
13
14use super::sva_to_verify::BoundedExpr;
15use logicaffeine_proof::ProofExpr;
16
17#[inline]
18fn boxed(e: ProofExpr) -> Box<ProofExpr> {
19    Box::new(e)
20}
21
22/// `ProofExpr` has no Boolean-literal node, so the constants are encoded as a tautology /
23/// contradiction over a reserved atom. `Or(c, ¬c)` is true and `And(c, ¬c)` is false
24/// regardless of `c`, so the encoding stays correct even if a signal happened to share the
25/// reserved name.
26fn truth() -> ProofExpr {
27    let c = ProofExpr::Atom("__bool_const".to_string());
28    ProofExpr::Or(boxed(c.clone()), boxed(ProofExpr::Not(boxed(c))))
29}
30fn falsity() -> ProofExpr {
31    let c = ProofExpr::Atom("__bool_const".to_string());
32    ProofExpr::And(boxed(c.clone()), boxed(ProofExpr::Not(boxed(c))))
33}
34
35/// Lower a Boolean-fragment `BoundedExpr` to `ProofExpr`, or `None` for any node outside the
36/// propositional fragment (integers, bitvectors, arrays, ordered comparisons, quantifiers,
37/// uninterpreted applications) — those require bit-blasting, not direct lowering.
38pub fn bounded_to_proof(e: &BoundedExpr) -> Option<ProofExpr> {
39    match e {
40        BoundedExpr::Var(name) => Some(ProofExpr::Atom(name.clone())),
41        BoundedExpr::Bool(true) => Some(truth()),
42        BoundedExpr::Bool(false) => Some(falsity()),
43        BoundedExpr::Not(p) => Some(ProofExpr::Not(boxed(bounded_to_proof(p)?))),
44        BoundedExpr::And(p, q) => Some(ProofExpr::And(
45            boxed(bounded_to_proof(p)?),
46            boxed(bounded_to_proof(q)?),
47        )),
48        BoundedExpr::Or(p, q) => Some(ProofExpr::Or(
49            boxed(bounded_to_proof(p)?),
50            boxed(bounded_to_proof(q)?),
51        )),
52        BoundedExpr::Implies(p, q) => Some(ProofExpr::Implies(
53            boxed(bounded_to_proof(p)?),
54            boxed(bounded_to_proof(q)?),
55        )),
56        // Equality of two Booleans is the biconditional; if either side is not Boolean, fall
57        // back to bit-blasted bitvector equality.
58        BoundedExpr::Eq(p, q) => match (bounded_to_proof(p), bounded_to_proof(q)) {
59            (Some(a), Some(b)) => Some(ProofExpr::Iff(boxed(a), boxed(b))),
60            _ => super::bitblast::lower_bool(e),
61        },
62        // Everything else: try the bit-blaster (datapath comparisons over bitvectors). It
63        // returns `None` for genuinely unsupported nodes (Int arithmetic, quantifiers, …).
64        _ => super::bitblast::lower_bool(e),
65    }
66}
67
68#[cfg(test)]
69#[path = "../../../logicaffeine_tests/unit/logicaffeine_compile/codegen_sva/sva_to_proof/tests.rs"]
70mod tests;