Research-Stack/0-Core-Formalism/lean/Semantics/Semantics/CopyIfTactic.lean
Brandon Schneider 7037af42f3 feat: CopyIfTactic — native Lean 4 pre-filter tactic
Implements the copy-if pattern as a Lean tactic:
- 'copy_if' tries rfl → decide → omega → norm_num → simp in order
- 'copy_if?' reports which closer worked (for profiling)
- Each closer is a 'zero delta' check — if goal is in normal form,
  closes instantly; if not, tries next closer

This is the Lean-native equivalent of:
- Blog post: vpcompressd register-dest = 40x faster
- VCN: numpy copy-if = 3.3x faster
- Pre-filter: skip trivial theorems = 2.7x faster
- LSP: copy_if tactic = instant close for zero-delta goals

Usage:
  import Semantics.CopyIfTactic
  theorem foo : 1 = 1 := by copy_if
  theorem bar : x + 0 = x := by copy_if
  theorem baz : complex := by copy_if  -- falls through to simp

3298 jobs, 0 errors. Tests pass for rfl, decide, omega, norm_num.
2026-05-29 02:58:19 -05:00

60 lines
1.6 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
CopyIfTactic.lean — Pre-filter tactic for Lean 4
Implements the copy-if pattern as a native Lean tactic:
1. Check if goal is trivially closable (zero delta) → close immediately
2. If non-trivial (non-zero delta) → delegate to solver
Usage:
theorem foo : 1 = 1 := by copy_if
theorem bar : x + 0 = x := by copy_if
theorem baz : complex_statement := by copy_if
The tactic tries fast closers in order of cost:
1. rfl (instant — zero delta)
2. decide (fast — decidable)
3. omega (fast — linear arithmetic)
4. simp (slow — full simplification)
-/
import Mathlib.Tactic
namespace Semantics.CopyIfTactic
open Lean Elab Tactic
/-- The copy_if tactic: try fast closers in order, fail if none work.
Lean implementation of the vectorized copy_if pattern.
Each closer is a "zero delta" check — if the goal is already in
normal form for that tactic, it closes instantly. -/
macro "copy_if" : tactic => `(tactic|
first
| rfl
| decide
| omega
| norm_num
| simp
| fail "copy_if: non-trivial goal, needs solver"
)
/-- The copy_if? tactic: like copy_if but reports which closer worked. -/
elab "copy_if?" : tactic => do
let tactics : List (String × Syntax) := [
("rfl", ← `(tactic| rfl)),
("decide", ← `(tactic| decide)),
("omega", ← `(tactic| omega)),
("norm_num",← `(tactic| norm_num)),
("simp", ← `(tactic| simp)),
]
for (name, tac) in tactics do
try
evalTactic tac
logInfo s!"copy_if?: closed with {name}"
return
catch _ =>
continue
logWarning "copy_if?: non-trivial goal"
throwError "copy_if?: goal is non-trivial"
end Semantics.CopyIfTactic