lean4-htt/tests/lean/run/aig_optimizations.lean
George Rennie 34c0535844
refactor: encode constants as inversions of false in AIG framework (#7649)
This PR changes the AIG representation of constants from `const (b :
Bool)` to a single constructor `false`. Since #7381 `Ref` contains an
`invert` flag meaning the constant `true` can be represented as a `Ref`
to `false` with `invert` set, so no expressivity is lost.

The main advantage to this representation is that it allows pattern
matching on constants to match just on the `invert` field rather than on
both `invert` and the constant value or having to XOR the two together.
This representation is also standard in other AIG frameworks, such as
the [Aiger standard](https://fmv.jku.at/aiger/FORMAT.aiger).

This PR also generalizes the idempotency rule in `mkGateCached` from `(a
/\ b) = a` when `(a = b)` to also cover `(¬a /\ ¬b) = ¬a` when `a = b`
as it was not covered.
2025-03-24 10:43:00 +00:00

53 lines
1.4 KiB
Text

import Std.Tactic.BVDecide.Bitblast
open Std.Sat
open Std.Tactic.BVDecide
def mkFalseCollapsible (n : Nat) : BVLogicalExpr :=
match n with
| 0 => .const false
| n + 1 =>
let tree := mkFalseCollapsible n
.gate .and tree tree
/-- info: #[Std.Sat.AIG.Decl.false] -/
#guard_msgs in
#eval BVLogicalExpr.bitblast (mkFalseCollapsible 1) |>.aig.decls
/-- info: #[Std.Sat.AIG.Decl.false] -/
#guard_msgs in
#eval BVLogicalExpr.bitblast (mkFalseCollapsible 16) |>.aig.decls
def mkTrueCollapsible (n : Nat) : BVLogicalExpr :=
match n with
| 0 => .const true
| n + 1 =>
let tree := mkTrueCollapsible n
.gate .and tree tree
/-- info: #[Std.Sat.AIG.Decl.false] -/
#guard_msgs in
#eval BVLogicalExpr.bitblast (mkTrueCollapsible 1) |>.aig.decls
/-- info: #[Std.Sat.AIG.Decl.false] -/
#guard_msgs in
#eval BVLogicalExpr.bitblast (mkTrueCollapsible 16) |>.aig.decls
def mkConstantCollapsible (n : Nat) : BVLogicalExpr :=
match n with
| 0 => .const false
| n + 1 =>
let tree := mkTrueCollapsible n
.gate .and (.gate .and tree tree) (.const false)
/-- info: (1, Std.Sat.AIG.Decl.false) -/
#guard_msgs in
#eval
let entry := BVLogicalExpr.bitblast (mkConstantCollapsible 1)
(entry.aig.decls.size, entry.aig.decls[entry.ref.gate]!)
/-- info: (1, Std.Sat.AIG.Decl.false) -/
#guard_msgs in
#eval
let entry := BVLogicalExpr.bitblast (mkConstantCollapsible 16)
(entry.aig.decls.size, entry.aig.decls[entry.ref.gate]!)