Repository navigation
Conversation
kmill
commented
May 16, 2023
|
@digama0 Here's a port of the |
|
I'm surprised that typechecking takes a relatively large amount of time, and I worry that there's some mistake I've made that's causing the kernel to do too much reduction work. For example: -- type checking took 906ms
example : Nat.fib 10000 + Nat.fib 10001 = Nat.fib 10002 := by norm_num1
-- tactic execution of Lean.Parser.Tactic.refl took 1.18s
-- type checking took 1.17s
example : Nat.fib 10000 + Nat.fib 10001 = Nat.fib 10002 := by rflAt least it's better that For a more direct comparison, I changed the extension to generate a /-- Evaluates the `Nat.fib` function. -/
@[norm_num Nat.fib _]
def evalNatFib : NormNumExt where eval {u α} e := do
let .app _ (x : Q(ℕ)) ← Meta.whnfR e | failure
let sℕ : Q(AddMonoidWithOne ℕ) := q(instAddMonoidWithOneNat)
let ⟨ex, p⟩ ← deriveNat x sℕ
have y := Nat.fib ex.natLit!
have ey : Q(ℕ) := mkRawNatLit y
have pf : Q(Nat.fib $ex = $ey) := (q(Eq.refl $ey) : Expr)
let pf' : Q(IsNat (Nat.fib $x) $ey) := q(isNat_fib $p $pf)
return .isNat sℕ ey pf'
-- type checking took 1.27s
example : Nat.fib 10000 + Nat.fib 10001 = Nat.fib 10002 := by norm_num1Amusingly, changing the iteration definition of def fib (n : ℕ) : ℕ :=
match n with
| 0 => 0
| 1 => 1
| n+2 => fib n + fib (n + 1)actually brings both down to around 900ms. I guess the cache is able to memoize the reduction just fine. |
|
As a followup, I defined |
|
One oddity that seems worth mentioning is that example : Nat.fib 10000 = Nat.fib 10000 := by norm_num1is fast (and one can check it does generate the full example : Nat.fib 10000 + 0 = Nat.fib 10000 := by norm_num1triggers a slow kernel check, even with the second |
|
I can confirm that this seems to be a problem with any norm num extension where the kernel reduction is slow. In #3892 I can reproduce example : Nat.minFac 1277 + 0 = 1277 := by norm_num1 -- slow
example : Nat.minFac 1277 + 0 = Nat.minFac 1277 := by norm_num1 -- slow
example : Nat.minFac 1277 = 1277 := by norm_num1 -- fast
example : Nat.minFac 1277 = Nat.minFac 1277 := by norm_num1 -- fast
example : Nat.minFac (1277 + 0) = Nat.minFac 1277 := by norm_num1 -- fast(slow means ">8000ms", fast means "<50ms") |
|
Here is a minimal example of something similar, but I'm not sure if it's exactly the same issue. This might be slow in the elaborator, not the kernel. def A : Nat → Nat → Nat
| 0, m => m + 1
| n + 1, 0 => A n 1
| n + 1, m + 1 => A n (A (n + 1) m)
termination_by _ a b => (a, b)
example : A 3 5 + 0 = A 3 5 := rfl -- slow |
|
bors r+ |
Co-authored-by: Mario Carneiro <[email protected]>
|
Pull request successfully merged into master. Build succeeded! The publicly hosted instance of bors-ng is deprecated and will go away soon. If you want to self-host your own instance, instructions are here. If you want to switch to GitHub's built-in merge queue, visit their help page. |
Nat.fib extension for norm_numNat.fib extension for norm_num
This applies a similar technique as was used for the `ofScientific` norm_num extension to the core field operations. This ensures that lean doesn't try to prove `Add.add (inst := inst1) 1234 56 = Add.add (inst := inst2) 1234 56` by evaluating the addition itself, and instead tries to prove the instances equal and let norm_num do the adding. Reported at #4036 (comment) , although I can't find a test case that doesn't involve the base norm_num implementation because all the core operations on nat are already overridden.