Skip to content

Commit

Permalink
chore: basic cleanups for bv_decide (#5408)
Browse files Browse the repository at this point in the history
  • Loading branch information
hargoniX committed Sep 20, 2024
1 parent eceba0f commit a6830f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/Lean/Elab/Tactic/BVDecide/Frontend/BVDecide.lean
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Reconstruct bit by bit which value expression must have had which `BitVec` value
expression - pair values.
-/
def reconstructCounterExample (var2Cnf : Std.HashMap BVBit Nat) (assignment : Array (Bool × Nat))
(aigSize : Nat) (atomsAssignment : Std.HashMap Nat Expr) :
(aigSize : Nat) (atomsAssignment : Std.HashMap Nat (Nat × Expr)) :
Array (Expr × BVExpr.PackedBitVec) := Id.run do
let mut sparseMap : Std.HashMap Nat (RBMap Nat Bool Ord.compare) := {}
for (bitVar, cnfVar) in var2Cnf.toArray do
Expand Down Expand Up @@ -70,7 +70,7 @@ def reconstructCounterExample (var2Cnf : Std.HashMap BVBit Nat) (assignment : Ar
if bitValue then
value := value ||| (1 <<< currentBit)
currentBit := currentBit + 1
let atomExpr := atomsAssignment.get! bitVecVar
let atomExpr := atomsAssignment.get! bitVecVar |>.snd
finalMap := finalMap.push (atomExpr, ⟨BitVec.ofNat currentBit value⟩)
return finalMap

Expand All @@ -83,14 +83,14 @@ structure UnsatProver.Result where
proof : Expr
lratCert : LratCert

abbrev UnsatProver := ReflectionResult → Std.HashMap Nat Expr → MetaM UnsatProver.Result
abbrev UnsatProver := ReflectionResult → Std.HashMap Nat (Nat × Expr) → MetaM UnsatProver.Result

/--
Contains values that will be used to diagnose spurious counter examples.
-/
structure DiagnosisInput where
unusedHypotheses : Std.HashSet FVarId
atomsAssignment : Std.HashMap Nat Expr
atomsAssignment : Std.HashMap Nat (Nat × Expr)

/--
The result of a spurious counter example diagnosis.
Expand All @@ -104,14 +104,14 @@ abbrev DiagnosisM : Type → Type := ReaderT DiagnosisInput <| StateRefT Diagnos
namespace DiagnosisM

def run (x : DiagnosisM Unit) (unusedHypotheses : Std.HashSet FVarId)
(atomsAssignment : Std.HashMap Nat Expr) : MetaM Diagnosis := do
(atomsAssignment : Std.HashMap Nat (Nat × Expr)) : MetaM Diagnosis := do
let (_, issues) ← ReaderT.run x { unusedHypotheses, atomsAssignment } |>.run {}
return issues

def unusedHyps : DiagnosisM (Std.HashSet FVarId) := do
return (← read).unusedHypotheses

def atomsAssignment : DiagnosisM (Std.HashMap Nat Expr) := do
def atomsAssignment : DiagnosisM (Std.HashMap Nat (Nat × Expr)) := do
return (← read).atomsAssignment

def addUninterpretedSymbol (e : Expr) : DiagnosisM Unit :=
Expand All @@ -131,7 +131,7 @@ Diagnose spurious counter examples, currently this checks:
- Whether all hypotheses which contain any variable that was bitblasted were included
-/
def diagnose : DiagnosisM Unit := do
for (_, expr) in ← atomsAssignment do
for (_, (_, expr)) in ← atomsAssignment do
match_expr expr with
| BitVec.ofBool x =>
match x with
Expand All @@ -158,7 +158,7 @@ def explainers : List (Diagnosis → Option MessageData) :=
[uninterpretedExplainer, unusedRelevantHypothesesExplainer]

def explainCounterExampleQuality (unusedHypotheses : Std.HashSet FVarId)
(atomsAssignment : Std.HashMap Nat Expr) : MetaM MessageData := do
(atomsAssignment : Std.HashMap Nat (Nat × Expr)) : MetaM MessageData := do
let diagnosis ← DiagnosisM.run DiagnosisM.diagnose unusedHypotheses atomsAssignment
let folder acc explainer := if let some m := explainer diagnosis then acc.push m else acc
let explanations := explainers.foldl (init := #[]) folder
Expand All @@ -172,7 +172,7 @@ def explainCounterExampleQuality (unusedHypotheses : Std.HashSet FVarId)
return err

def lratBitblaster (cfg : TacticContext) (reflectionResult : ReflectionResult)
(atomsAssignment : Std.HashMap Nat Expr) :
(atomsAssignment : Std.HashMap Nat (Nat × Expr)) :
MetaM UnsatProver.Result := do
let bvExpr := reflectionResult.bvExpr
let entry ←
Expand Down Expand Up @@ -242,7 +242,7 @@ def closeWithBVReflection (g : MVarId) (unsatProver : UnsatProver) :
reflectBV g
trace[Meta.Tactic.bv] "Reflected bv logical expression: {reflectionResult.bvExpr}"

let atomsPairs := (← getThe State).atoms.toList.map (fun (expr, _, ident) => (ident, expr))
let atomsPairs := (← getThe State).atoms.toList.map (fun (expr, ⟨width, ident) => (ident, (width, expr)))
let atomsAssignment := Std.HashMap.ofList atomsPairs
let ⟨bvExprUnsat, cert⟩ ← unsatProver reflectionResult atomsAssignment
let proveFalse ← reflectionResult.proveFalse bvExprUnsat
Expand Down
5 changes: 3 additions & 2 deletions src/Lean/Elab/Tactic/BVDecide/Frontend/LRAT.lean
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ This will obtain an `LratCert` if the formula is UNSAT and throw errors otherwis
def runExternal (cnf : CNF Nat) (solver : System.FilePath) (lratPath : System.FilePath)
(trimProofs : Bool) (timeout : Nat) (binaryProofs : Bool)
: MetaM (Except (Array (Bool × Nat)) LratCert) := do
IO.FS.withTempFile fun _ cnfPath => do
IO.FS.withTempFile fun cnfHandle cnfPath => do
withTraceNode `sat (fun _ => return "Serializing SAT problem to DIMACS file") do
-- lazyPure to prevent compiler lifting
IO.FS.writeFile cnfPath (← IO.lazyPure (fun _ => cnf.dimacs))
cnfHandle.putStr (← IO.lazyPure (fun _ => cnf.dimacs))
cnfHandle.flush

let res ←
withTraceNode `sat (fun _ => return "Running SAT solver") do
Expand Down
6 changes: 3 additions & 3 deletions src/Std/Tactic/BVDecide/Bitblast/BVExpr/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ def toString : BVUnOp → String
| not => "~"
| shiftLeftConst n => s!"<< {n}"
| shiftRightConst n => s!">> {n}"
| rotateLeft n => s! "rotL {n}"
| rotateRight n => s! "rotR {n}"
| rotateLeft n => s!"rotL {n}"
| rotateRight n => s!"rotR {n}"
| arithShiftRightConst n => s!">>a {n}"

instance : ToString BVUnOp := ⟨toString⟩
Expand Down Expand Up @@ -238,7 +238,7 @@ def toString : BVExpr w → String
| .var idx => s!"var{idx}"
| .const val => ToString.toString val
| .zeroExtend v expr => s!"(zext {v} {expr.toString})"
| .extract hi lo expr => s!"{expr.toString}[{hi}:{lo}]"
| .extract start len expr => s!"{expr.toString}[{start}, {len}]"
| .bin lhs op rhs => s!"({lhs.toString} {op.toString} {rhs.toString})"
| .un op operand => s!"({op.toString} {toString operand})"
| .append lhs rhs => s!"({toString lhs} ++ {toString rhs})"
Expand Down

0 comments on commit a6830f9

Please sign in to comment.