-
Notifications
You must be signed in to change notification settings - Fork 451
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
refactor: rewrite: produce simpler proof terms #3121
Conversation
this is so far a learning exercise for me, trying to understand the Chesterton’s fence here, with the intention to comment the code better once CI tells me why the simpler version doesn't work.
!bench |
Here are the benchmark results for commit 522810c. Benchmark Metric Change
=========================================
+ stdlib task-clock -1.0% (-36.8 σ) |
|
The test suit succeeds but mathlib fails. I hope I can distill a test case from this. |
Hmm, the four breaking cases were easy to fix (leanprover-community/mathlib4@af6cccb925 ):
Then mathlib builds, but it’s test suite fails, e.g. at
so there is something fishy going on with side-conditions. |
Note to myself: Using
(but it is after a bit more evaluation; maybe |
This reverts commit c5c6661.
@nomeata, the "something fishy" is the This is obviously evil, but I'm not sure of a better way to do it. In any case it should be easy to update that function to match your new scheme. Let me know if you'd like me to do something in that file. |
Ha, should have thought of that! I can do it myself when I come back to this (non-pressing) experiment, now that I know where to look. Should |
Here another benchmark run:
So the gains look relatively stable. |
commit 05ed454 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 21:11:11 2024 +0000 Trigger CI for leanprover/lean4#3121 commit f984c96 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 19:02:16 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 0dcffb9 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 17:27:43 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 60c2d87 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 16:41:44 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 5b3c8cb Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:54:42 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 9cdd75f Author: Joachim Breitner <[email protected]> Date: Mon Jan 15 15:48:45 2024 +0100 Reduce diff to nightly-testing commit d85e0f1 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:35:23 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 4239974 Merge: fee326a 2a84dcf Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:17:15 2024 +0000 Trigger CI for leanprover/lean4#3121 commit fee326a Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 8 13:40:55 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 84a9d48 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 8 13:16:22 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 27867dd Author: Joachim Breitner <[email protected]> Date: Mon Jan 8 12:42:07 2024 +0100 Try to fix rw? commit 5189cc8 Author: Joachim Breitner <[email protected]> Date: Mon Jan 8 12:39:41 2024 +0100 Trigger CI commit 9dd93f9 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Sat Dec 30 11:09:38 2023 +0000 Trigger CI for leanprover/lean4#3121 commit 0494ec5 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 18:22:37 2023 +0000 Trigger CI for leanprover/lean4#3121 commit aa43b20 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 15:48:58 2023 +0000 Trigger CI for leanprover/lean4#3121 commit af6cccb Author: Joachim Breitner <[email protected]> Date: Thu Dec 28 15:56:05 2023 +0100 Fix some fallout (reorder goals) commit cb55f12 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 12:32:36 2023 +0000 Trigger CI for leanprover/lean4#3121 commit 780f9f2 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 11:36:42 2023 +0000 Update lean-toolchain for testing leanprover/lean4#3121 commit 8e0fec8 Merge: a21ddd8 e352bb7 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 21 09:30:49 2023 +0000 Merge master into nightly-testing commit a21ddd8 Author: Scott Morrison <[email protected]> Date: Thu Dec 21 19:37:19 2023 +1100 merge lean-pr-testing-2964
commit 05ed454 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 21:11:11 2024 +0000 Trigger CI for leanprover/lean4#3121 commit f984c96 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 19:02:16 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 0dcffb9 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 17:27:43 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 60c2d87 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 16:41:44 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 5b3c8cb Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:54:42 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 9cdd75f Author: Joachim Breitner <[email protected]> Date: Mon Jan 15 15:48:45 2024 +0100 Reduce diff to nightly-testing commit d85e0f1 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:35:23 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 4239974 Merge: fee326a 2a84dcf Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 15 14:17:15 2024 +0000 Trigger CI for leanprover/lean4#3121 commit fee326a Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 8 13:40:55 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 84a9d48 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Mon Jan 8 13:16:22 2024 +0000 Trigger CI for leanprover/lean4#3121 commit 27867dd Author: Joachim Breitner <[email protected]> Date: Mon Jan 8 12:42:07 2024 +0100 Try to fix rw? commit 5189cc8 Author: Joachim Breitner <[email protected]> Date: Mon Jan 8 12:39:41 2024 +0100 Trigger CI commit 9dd93f9 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Sat Dec 30 11:09:38 2023 +0000 Trigger CI for leanprover/lean4#3121 commit 0494ec5 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 18:22:37 2023 +0000 Trigger CI for leanprover/lean4#3121 commit aa43b20 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 15:48:58 2023 +0000 Trigger CI for leanprover/lean4#3121 commit af6cccb Author: Joachim Breitner <[email protected]> Date: Thu Dec 28 15:56:05 2023 +0100 Fix some fallout (reorder goals) commit cb55f12 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 12:32:36 2023 +0000 Trigger CI for leanprover/lean4#3121 commit 780f9f2 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 28 11:36:42 2023 +0000 Update lean-toolchain for testing leanprover/lean4#3121 commit 8e0fec8 Merge: a21ddd8 e352bb7 Author: leanprover-community-mathlib4-bot <[email protected]> Date: Thu Dec 21 09:30:49 2023 +0000 Merge master into nightly-testing commit a21ddd8 Author: Scott Morrison <[email protected]> Date: Thu Dec 21 19:37:19 2023 +1100 merge lean-pr-testing-2964
Encouraged by the performance gains from making `rewrite` produce smaller proof objects (#3121) I am here looking for low-hanging fruit in `simp`. Consider this typical example: ``` set_option pp.explicit true theorem test (a : Nat) (b : Nat) (c : Nat) (heq : a = b) (h : (c.add (c.add ((c.add b).add c))).add c = c) : (c.add (c.add ((c.add a).add c))).add c = c ``` We get a rather nice proof term when using ``` := by rw [heq]; assumption ``` namely ``` theorem test : ∀ (a b c : Nat), @eq Nat a b → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c := fun a b c heq h => @Eq.mpr (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c) (@congrArg Nat Prop a b (fun _a => @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c _a) c))) c) c) heq) h ``` (this is with #3121). But with `by simp only [heq]; assumption`, it looks rather different: ``` theorem test : ∀ (a b c : Nat), @eq Nat a b → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c := fun a b c heq h => @Eq.mpr (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c) (@id (@eq Prop (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c)) (@congrFun Nat (fun a => Prop) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c)) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c)) (@congrArg Nat (Nat → Prop) (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) (@eq Nat) (@congrFun Nat (fun a => Nat) (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c)))) (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c)))) (@congrArg Nat (Nat → Nat) (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) Nat.add (@congrArg Nat Nat (Nat.add c (Nat.add (Nat.add c a) c)) (Nat.add c (Nat.add (Nat.add c b) c)) (Nat.add c) (@congrArg Nat Nat (Nat.add (Nat.add c a) c) (Nat.add (Nat.add c b) c) (Nat.add c) (@congrFun Nat (fun a => Nat) (Nat.add (Nat.add c a)) (Nat.add (Nat.add c b)) (@congrArg Nat (Nat → Nat) (Nat.add c a) (Nat.add c b) Nat.add (@congrArg Nat Nat a b (Nat.add c) heq)) c)))) c)) c)) h ``` Since simp uses only single-step `congrArg`/`congrFun` congruence lemmas here, the proof term grows very large, likely quadratic in this case. Can we do better? Every nesting of `congrArg` (and it's little brother `congrFun`) can be turned into a single `congrArg` call. In this PR I make making the smart app builders `Meta.mkCongrArg` and `Meta.mkCongrFun` a bit smarter and not only fuse with `Eq.refl`, but also with `congrArg`/`congrFun`. Now we get, in this simple example, ``` theorem test : ∀ (a b c : Nat), @eq Nat a b → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c → @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c := fun a b c heq h => @Eq.mpr (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c a) c))) c) c) (@eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c b) c))) c) c) (@congrArg Nat Prop a b (fun x => @eq Nat (Nat.add (Nat.add c (Nat.add c (Nat.add (Nat.add c x) c))) c) c) heq) h ``` Let’s see if it works and how much we gain.
Consider
Before, a typical proof term for
rewrite
looked like this:which is rather round-about, applying
ndrec
torefl
. It would be more direct to writewhich this change does.
This makes proof terms smaller, causing mild general speed up throughout the code; if the brenchmarks don’t lie the highlights are
H'T to @digama0 for advice and help.
NB: One might even expect the even simpler
but that would require non-local changes to the source code, so one step at a time.