Skip to content

[Merged by Bors] - port: Nat.fib extension for norm_num - #4036

Closed
kmill wants to merge 6 commits into
masterfrom
kmill_port_norm_num_fib
Closed

kmill wants to merge 6 commits into
masterfrom
kmill_port_norm_num_fib

Conversation

@kmill

@kmill kmill commented May 16, 2023

Copy link
Copy Markdown
Contributor

Open in Gitpod

@kmill kmill added awaiting-review t-meta Tactics, attributes or user commands labels May 16, 2023
@kmill

kmill commented May 16, 2023

Copy link
Copy Markdown
Contributor Author

@digama0 Here's a port of the Nat.fib extension that we wrote (well, the one that I wrote poorly and that you rewrote from scratch 😉)

@kmill

kmill commented May 16, 2023 •

Copy link
Copy Markdown
Contributor Author

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 rfl

At least it's better that rfl, but less than I'd expect.

For a more direct comparison, I changed the extension to generate a rfl proof to avoid time spent unifying in the elaborator:

/-- 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_num1

Amusingly, changing the iteration definition of Fib to the naive

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.

@kmill

kmill commented May 16, 2023 •

Copy link
Copy Markdown
Contributor Author

As a followup, I defined fib as an axiom, and norm_num can prove Nat.fib 10000 + Nat.fib 10001 = Nat.fib 10002 in just 30ms, so somehow the current implementation is causing fib to be reduced by the kernel even though it doesn't need to...

@kmill

kmill commented May 16, 2023 •

Copy link
Copy Markdown
Contributor Author

One oddity that seems worth mentioning is that

example : Nat.fib 10000 = Nat.fib 10000 := by norm_num1

is fast (and one can check it does generate the full Nat.fib proofs, it's not just a trivial rfl), but

example : Nat.fib 10000 + 0 = Nat.fib 10000 := by norm_num1

triggers a slow kernel check, even with the second Nat.fib 10000 replaced with its value 3364476 ... lots and lots of digits ... 947366875.

@fpvandoorn

fpvandoorn commented May 17, 2023 •

Copy link
Copy Markdown
Member

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")
It seems like that if the kernel sees an operation that it can deal with efficiently (like +) it will also reduce all arguments.

@fpvandoorn

Copy link
Copy Markdown
Member

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

@kim-em kim-em added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label May 23, 2023
@kim-em kim-em added merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) and removed merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) labels May 25, 2023
@kim-em kim-em removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label May 27, 2023
@digama0

digama0 commented May 27, 2023

Copy link
Copy Markdown
Member

bors r+

@github-actions github-actions Bot added ready-to-merge This PR has been sent to bors. and removed awaiting-review labels May 27, 2023
bors Bot pushed a commit that referenced this pull request May 27, 2023
@bors

bors Bot commented May 27, 2023

Copy link
Copy Markdown

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.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors Bot changed the title port: Nat.fib extension for norm_num [Merged by Bors] - port: Nat.fib extension for norm_num May 27, 2023
@bors bors Bot closed this May 27, 2023
@bors
bors Bot deleted the kmill_port_norm_num_fib branch May 27, 2023 11:16
bors Bot pushed a commit that referenced this pull request May 28, 2023
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has been sent to bors. t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants