Skip to content

Commit fe83087

Browse files
abdoo8080zqy1018
andcommitted
Run intros and contra automatically on goal.
Co-Authored-By: Qiyuan Zhao <[email protected]>
1 parent 66457c1 commit fe83087

29 files changed

+153
-128
lines changed

Smt/Preprocess.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Authors: Abdalrhman Mohamed, Tomaz Gomes Mascarenhas
66
-/
77

88
import Smt.Preprocess.Embedding
9+
import Smt.Preprocess.Intros
910
import Smt.Preprocess.Mono
1011
import Smt.Preprocess.Normalize
12+
import Smt.Preprocess.NegateGoal
1113
import Smt.Preprocess.PushHintsToCtx

Smt/Preprocess/Embedding.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def embedding (mv : MVarId) (hs : Array Expr) : MetaM Result := mv.withContext d
5656
let (fvs, mv) ← mv.revert fvs true
5757
-- Simplify the goal using the embedding theorems.
5858
let congrTheorems ← Meta.getSimpCongrTheorems
59-
let ctx ← Meta.Simp.mkContext {} simpTheorems congrTheorems
59+
let ctx ← Meta.Simp.mkContext { zeta := false } simpTheorems congrTheorems
6060
let (some mv, _) ← Meta.simpTarget mv ctx simpProcs (mayCloseGoal := false) | throwError "[embedding] simplification failed"
6161
-- Extend `fvs` to account for `nonneg` assumptions.
6262
let bts := bts.pop -- Do not consider `Bool` for assumptions.

Smt/Preprocess/Embedding/Nat.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ attribute [embedding ↓] Int.cast_ofNat_Int
1717
attribute [embedding ↓] Int.natCast_add
1818
attribute [embedding ↓] Int.natCast_mul
1919
attribute [embedding ↓] Int.natCast_ediv
20+
attribute [embedding ↓] Int.natCast_emod
2021
attribute [embedding ↓ ←] Int.ofNat_inj
2122
attribute [embedding ↓ ←] Int.ofNat_le
2223
attribute [embedding ↓ ←] Int.ofNat_lt

Smt/Preprocess/Intros.lean

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/-
2+
Copyright (c) 2021-2026 by the authors listed in the file AUTHORS and their
3+
institutional affiliations. All rights reserved.
4+
Released under Apache 2.0 license as described in the file LICENSE.
5+
Authors: Abdalrhman Mohamed
6+
-/
7+
8+
import Smt.Preprocess.Basic
9+
import Lean.Meta.Tactic.Assert
10+
11+
namespace Smt.Preprocess
12+
13+
open Lean
14+
15+
/-- Returns `true` if `fv` corresponds to a proposition in the local context. -/
16+
def isPropHyp (fv : FVarId) : MetaM Bool := do
17+
let localDecl ← fv.getDecl
18+
unless localDecl.isImplementationDetail do
19+
if ← pure !(isNonEmpty localDecl.type) <&&> Meta.isProp localDecl.type then
20+
return true
21+
return false
22+
where
23+
isNonEmpty (e : Expr) : Bool :=
24+
match e with
25+
| .app (.const ``Nonempty _) _ => true
26+
| .forallE _ _ b _ => isNonEmpty b
27+
| _ => false
28+
29+
def intros (mv : MVarId) (hs : Array Expr) : MetaM Result := do
30+
let (fvs, mv) ← mv.intros
31+
let hfvs := fvs.map Expr.fvar
32+
return { map := Std.HashMap.insertMany {} (hfvs.zip (hfvs.map (#[·]))), hs := hs ++ hfvs, mv }
33+
34+
end Smt.Preprocess

Smt/Preprocess/NegateGoal.lean

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/-
2+
Copyright (c) 2021-2026 by the authors listed in the file AUTHORS and their
3+
institutional affiliations. All rights reserved.
4+
Released under Apache 2.0 license as described in the file LICENSE.
5+
Authors: Abdalrhman Mohamed
6+
-/
7+
8+
import Smt.Preprocess.Basic
9+
import Lean.Meta.Tactic.Apply
10+
import Lean.Meta.Tactic.Intro
11+
12+
namespace Smt.Preprocess
13+
14+
open Lean
15+
16+
def negateGoal (mv : MVarId) (hs : Array Expr) : MetaM Result := do
17+
let target ← mv.getType
18+
if target.isFalse then return { map := {}, hs, mv }
19+
let [mv] ← mv.applyConst ``Classical.byContradiction
20+
| throwError "[negateGoal] Unexpected result after applying {``Classical.byContradiction}"
21+
let (fv, mv) ← mv.intro1
22+
return { map := Std.HashMap.insert {} (.fvar fv) #[.fvar fv], hs := hs.push (.fvar fv), mv }
23+
24+
end Smt.Preprocess

Smt/Preprocess/Normalize.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def normalize (mv : MVarId) (hs : Array Expr) : MetaM Result := mv.withContext d
3434
let (fvs', mv) ← mv.revert fvs true
3535
-- Simplify the goal using the smt_normalize theorems.
3636
let congrTheorems ← Meta.getSimpCongrTheorems
37-
let ctx ← Meta.Simp.mkContext {} simpTheorems congrTheorems
37+
let ctx ← Meta.Simp.mkContext { zeta := false } simpTheorems congrTheorems
3838
let (some mv, _) ← Meta.simpTarget mv ctx simpProcs (mayCloseGoal := false) | throwError "[smt_normalize] simplification failed"
3939
-- Re-introduce all free vars.
4040
let (fvs'', mv) ← mv.introNP fvs'.size

Smt/Preprocess/Normalize/Int.lean

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ theorem Int.lt_def : Int.lt a b = (a < b) := rfl
2525
@[smt_normalize ↓]
2626
theorem Int.le_def' : Int.le a b = (a ≤ b) := rfl
2727

28-
attribute [smt_normalize ↓] Int.add_def Int.mul_def Int.div_def
28+
attribute [smt_normalize ↓] Int.add_def Int.mul_def
2929
Int.ofNat_eq_natCast Int.negSucc_eq Int.cast_ofNat_Int
30+
31+
attribute [smt_normalize ↓ ←] Int.div_def

Smt/Reconstruct/Prop/Lemmas.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,9 @@ theorem implies_of_not_and : ¬(andN (ps ++ [¬q])) → impliesN ps q := by
705705
| .inl hnp => contradiction
706706
| .inr hnps => exact ih hnps
707707

708+
theorem implies_false_of_not_and : ¬(andN ps) → impliesN ps False := by
709+
simp_all [implies_of_not_and]
710+
708711
syntax "flipCongrArg " term ("[" term "]")? : term
709712
macro_rules
710713
| `(flipCongrArg $premiss:term [$arg:term]) => `(congrArg $arg $premiss)

Smt/Reconstruct/UF.lean

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def getFVarOrConstExpr! (n : String) : ReconstructM Expr := do
4242
let some n := (← read).sortCard[t.getSort]? | throwError "unknown sort {t.getSort}"
4343
let s := t.toString
4444
let endPos := (s.rawEndPos - t.getSort.toString).decreaseBy 2
45-
let endPos := if endPos.dec.get? s == some '|' then endPos.dec else endPos
46-
let startPos := (s.revFindAux (· != '_') endPos).get!
47-
let i : Nat := (String.Pos.Raw.extract s startPos endPos).toNat!
45+
let endPos := s.pos! (if endPos.dec.get? s == some '|' then endPos.dec else endPos)
46+
let startPos := (endPos.revFind? (· != '_')).get!
47+
let i : Nat := (s.extract startPos endPos).toNat!
4848
if h : i < n then
4949
let i : Fin n := ⟨i, h⟩
5050
return toExpr i

Smt/Reconstruct/Util.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ partial def expandLet : Expr → MetaM Expr
173173
| some (.ldecl _ _ userName _ value _ _) =>
174174
match userName with
175175
| .str _ userNameStr =>
176-
let userNamePref: String := userNameStr.take 3
177-
if userNamePref = "let" then expandLet value else pure (fvar fid)
176+
let userNamePref := userNameStr.take 3
177+
if userNamePref == "let" then expandLet value else pure (fvar fid)
178178
| _ => pure (fvar fid)
179179
| _ => pure (fvar fid)
180180
| app f x => do pure (app (← expandLet f) (← expandLet x))

0 commit comments

Comments
 (0)