Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bv_automata fallible #670

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions SSA/Experimental/Bits/Fast/Tactic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ open Lean Elab Tactic
open Lean Meta
open scoped Qq


/-!
# BitVec Automata Tactic
There are two ways of expressing BitVec expressions. One is:
Expand Down Expand Up @@ -296,6 +295,40 @@ def introduceMapIndexToFVar : TacticM Unit := withMainContext <| do

elab "introduceMapIndexToFVar" : tactic => introduceMapIndexToFVar

/- Copy-pasted from Lean/Elab/Tactic/ElabTerm.lean
-/

private def preprocessPropToDecide (expectedType : Expr) : TermElabM Expr := do
let mut expectedType ← instantiateMVars expectedType
if expectedType.hasFVar then
expectedType ← zetaReduce expectedType
if expectedType.hasFVar || expectedType.hasMVar then
throwError "expected type must not contain free or meta variables{indentExpr expectedType}"
return expectedType

private def mkNativeAuxDecl (baseName : Name) (type value : Expr) : TermElabM Name := do
let auxName ← Term.mkAuxName baseName
let decl := Declaration.defnDecl {
name := auxName, levelParams := [], type, value
hints := .abbrev
safety := .safe
}
addDecl decl
compileDecl decl
pure auxName

elab "safe_native_decide" : tactic =>
Lean.Elab.Tactic.closeMainGoalUsing `safeNativeDecide fun expectedType => do
let expectedType ← preprocessPropToDecide expectedType
let d ← mkDecide expectedType
let auxDeclName ← mkNativeAuxDecl `_nativeDecide (Lean.mkConst `Bool) d
-- new lines
unless ← reduceBoolNative auxDeclName do
throwError "The statement is false"
let rflPrf ← mkEqRefl (toExpr true)
let s := d.appArg! -- get instance from `d`
return mkApp3 (Lean.mkConst ``of_decide_eq_true) expectedType s <| mkApp3 (Lean.mkConst ``Lean.ofReduceBool) (Lean.mkConst auxDeclName) (toExpr true) rflPrf

/--
Create bv_automata tactic which solves equalities on bitvectors.
-/
Expand All @@ -322,7 +355,7 @@ macro "bv_automata" : tactic =>
intros _ _
apply congrFun
apply congrFun
native_decide
safe_native_decide
))

/-!
Expand All @@ -337,6 +370,10 @@ info: 'alive_1' depends on axioms: [propext, sorryAx, Classical.choice, Lean.ofR
-/
#guard_msgs in #print axioms alive_1

def false_statement {w : ℕ} (x y : BitVec w) : x = y := by
try bv_automata
sorry

def test_OfNat_ofNat (x : BitVec 1) : 1 + x = x + 1 := by
bv_automata

Expand Down
6 changes: 0 additions & 6 deletions SSA/Projects/InstCombine/ForLean.lean
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,6 @@ theorem width_one_cases (a : BitVec 1) : a = 0#1 ∨ a = 1#1 := by
subst h
simp

@[simp]
lemma sub_eq_xor (a b : BitVec 1) : a - b = a ^^^ b := by
have ha : a = 0 ∨ a = 1 := width_one_cases _
have hb : b = 0 ∨ b = 1 := width_one_cases _
rcases ha with h | h <;> (rcases hb with h' | h' <;> (simp [h, h']))

@[simp]
lemma add_eq_xor (a b : BitVec 1) : a + b = a ^^^ b := by
have ha : a = 0 ∨ a = 1 := width_one_cases _
Expand Down
Loading