Skip to content

Commit 8be9b2d

Browse files
committed
feat: speed up library_search by discarding lemma with non-specific discrimination keys. (#2888)
As discussed on [zulip](https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/potential.20improvements.20to.20library_search). This results in a 3-4x speed-up in the test file. (After allowing for building the cache, which is still slow.) Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent 00e3de7 commit 8be9b2d

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

Mathlib/Tactic/LibrarySearch.lean

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ example : Nat := by library_search
2424
```
2525
-/
2626

27+
namespace Lean.Meta.DiscrTree
28+
29+
/--
30+
Inserts a new key into a discrimination tree,
31+
but only if it is not of the form `#[*]` or `#[=, *, *, *]`.
32+
-/
33+
def insertIfSpecific {α : Type} {s : Bool} [BEq α] (d : DiscrTree α s)
34+
(keys : Array (DiscrTree.Key s)) (v : α) : DiscrTree α s :=
35+
if keys == #[Key.star] || keys == #[Key.const `Eq 3, Key.star, Key.star, Key.star] then
36+
d
37+
else
38+
d.insertCore keys v
39+
40+
end Lean.Meta.DiscrTree
41+
2742
namespace Mathlib.Tactic.LibrarySearch
2843

2944
open Lean Meta Std.Tactic.TryThis
@@ -61,15 +76,15 @@ initialize librarySearchLemmas : DeclCache (DiscrTree (Name × DeclMod) true)
6176
withNewMCtxDepth do withReducible do
6277
let (_, _, type) ← forallMetaTelescopeReducing constInfo.type
6378
let keys ← DiscrTree.mkPath type
64-
let lemmas := lemmas.insertCore keys (name, .none)
79+
let lemmas := lemmas.insertIfSpecific keys (name, .none)
6580
match type.getAppFnArgs with
6681
| (``Eq, #[_, lhs, rhs]) => do
6782
let keys_symm ← DiscrTree.mkPath (← mkEq rhs lhs)
68-
pure (lemmas.insertCore keys_symm (name, .symm))
83+
pure (lemmas.insertIfSpecific keys_symm (name, .symm))
6984
| (``Iff, #[lhs, rhs]) => do
7085
let keys_mp ← DiscrTree.mkPath rhs
7186
let keys_mpr ← DiscrTree.mkPath lhs
72-
pure <| (lemmas.insertCore keys_mp (name, .mp)).insertCore keys_mpr (name, .mpr)
87+
pure <| (lemmas.insertIfSpecific keys_mp (name, .mp)).insertIfSpecific keys_mpr (name, .mpr)
7388
| _ => pure lemmas
7489

7590
/-- Shortcut for calling `solveByElim`. -/

test/librarySearch.lean

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Mathlib.Algebra.Order.Ring.Canonical
44

55
noncomputable section
66

7+
set_option maxHeartbeats 400000 in
78
example (x : Nat) : x ≠ x.succ := ne_of_lt (by library_search)
89
example : 01 + 1 := ne_of_lt (by library_search)
910
example (x y : Nat) : x + y = y + x := by library_search
@@ -20,10 +21,10 @@ example (X : Type) (P : Prop) (x : X) (h : ∀ x : X, x = x → P) : P := by lib
2021

2122
example (α : Prop) : α → α := by library_search -- says: `exact id`
2223

23-
example (p : Prop) : (¬¬p) → p := by library_search -- says: `exact not_not.mp`
24-
example (a b : Prop) (h : a ∧ b) : a := by library_search -- says: `exact h.left`
25-
26-
example (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := by library_search
24+
-- Note: these examples no longer work after we turned off lemmas with discrimination key `#[*]`.
25+
-- example (p : Prop) : (¬¬p) → p := by library_search -- says: `exact not_not.mp`
26+
-- example (a b : Prop) (h : a ∧ b) : a := by library_search -- says: `exact h.left`
27+
-- example (P Q : Prop) : (¬ Q → ¬ P) → (P → Q) := by library_search -- say: `exact Function.mtr`
2728

2829
example (a b : ℕ) : a + b = b + a :=
2930
by library_search -- says: `exact add_comm a b`
@@ -76,20 +77,17 @@ end synonym
7677
example : ∀ P : Prop, ¬(P ↔ ¬P) := by library_search
7778

7879
-- We even find `iff` results:
79-
8080
example {a b c : ℕ} (h₁ : a ∣ c) (h₂ : a ∣ b + c) : a ∣ b := by library_search -- says `exact (Nat.dvd_add_iff_left h₁).mpr h₂`
8181

82-
example {α : Sort u} (h : Empty) : α := by library_search
83-
example {α : Type _} (h : Empty) : α := by library_search
84-
85-
example (f : A → C) (g : B → C) : (A ⊕ B) → C := by library_search
82+
-- Note: these examples no longer work after we turned off lemmas with discrimination key `#[*]`.
83+
-- example {α : Sort u} (h : Empty) : α := by library_search -- says `exact Empty.elim h`
84+
-- example (f : A → C) (g : B → C) : (A ⊕ B) → C := by library_search -- says `exact Sum.elim f g`
85+
-- example (n : ) (r : ℚ) : ℚ := by library_search using n, r -- exact nsmulRec n r
8686

8787
opaque f : ℕ → ℕ
8888
axiom F (a b : ℕ) : f a ≤ f b ↔ a ≤ b
8989
example (a b : ℕ) (h : a ≤ b) : f a ≤ f b := by library_search
9090

91-
theorem nonzero_gt_one (n : ℕ) : ¬ n = 0 → n ≥ 1 := by library_search -- `exact nat.pos_of_ne_zero`
92-
9391
example (L _M : List (List ℕ)) : List ℕ := by library_search using L
9492

9593
example (P _Q : List ℕ) (h : ℕ) : List ℕ := by library_search using h, P
@@ -102,9 +100,6 @@ example (n m : ℕ) : ℕ := by library_search using n, m -- exact rightAdd n m
102100
example (P Q : List ℕ) (_h : ℕ) : List ℕ :=
103101
by library_search using P, Q -- exact P ∩ Q
104102

105-
example (n : ℕ) (r : ℚ) : ℚ :=
106-
by library_search using n, r -- exact nsmulRec n r
107-
108103
-- Check that we don't use sorryAx:
109104
-- (see https://github.com/leanprover-community/mathlib4/issues/226)
110105

0 commit comments

Comments
 (0)