Zulip Chat Archive
Stream: mathlib4
Topic: Factorial notation parse error
Floris van Doorn (May 08 2023 at 14:18):
In some cases the Nat.factorial notation parses incorrectly.
import Mathlib.Data.Nat.Factorial.Basic
open Nat
example (n : ℕ) (hn : 0 < n) : n ∣ n ! := by -- fine
have : n ∣ n ! := -- fine
dvd_factorial hn le_rfl
have : n ∣ n ! -- parsed incorrectly
· exact dvd_factorial hn le_rfl
exact dvd_factorial hn le_rfl
Floris van Doorn (May 08 2023 at 14:21):
My guess is that the · is parsed as the anonymous variable, because we're still resolving the overloaded notation ! (either not or Nat.factorial)
Mario Carneiro (May 08 2023 at 18:31):
Removing the whitespace works, since it prevents it from being treated as an application of n to ! ·. Of course the lexer issues with n! still apply, but you can write (n)! instead
Kyle Miller (May 08 2023 at 19:47):
There's also using an explicit semicolon (have : n ∣ n !;) or using parentheses around other parts of the expression (like have : n ∣ (n !)).
Mario Carneiro (May 08 2023 at 19:50):
or even (have : n ∣ n !)
Kyle Miller (May 08 2023 at 19:50):
(Javascript also has optional semicolons, and the risk of unexpected parses like this pushed me towards always using semicolons there.)
Kyle Miller (May 08 2023 at 19:51):
It's too bad that · for focusing is also a valid notation in expressions; if focusing used a unique symbol this could be less of an issue.
Ruben Van de Velde (May 08 2023 at 19:51):
Does it work if you use . (period) instead of ·?
Mario Carneiro (May 08 2023 at 19:52):
nope, . works as an ascii replacement for · in both cases
Reid Barton (May 09 2023 at 05:50):
This also wouldn't be an issue if indentation-based syntax worked like in Haskell (as discussed elsewhere)
Johan Commelin (Jun 19 2023 at 17:00):
Sorry for necrobumping this... I think @Kyle Miller had a parser "hack" that made factorial notation work nicely. Maybe we actually want to have that in mathlib
Kyle Miller (Jun 19 2023 at 17:02):
https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/New.20.60propose.60.20tactic/near/351149815
(it's in a propose thread since Johan came across n! not being n ! when creating a propose demo)
Eric Wieser (May 09 2025 at 19:11):
I'll note in this thread that the factorial notation is causing me trouble in
Eric Wieser (Oct 17 2025 at 10:33):
I ran into this again with
import Mathlib
open scoped Nat
theorem fails (n : ℕ) : True := by
let x := n !
sorry
Is there a precedence fix here by making tactic newlines take precedence over !, like they already do over function application?
Snir Broshi (Oct 17 2025 at 11:51):
This works:
import Mathlib
open scoped Nat
theorem fails (n : ℕ) : True := by
let x := (n)!
sorry
and it's the recommended notation: https://github.com/leanprover-community/mathlib4/blob/ae97c1b7752a7c09092be88c91a75a3dd7f29ced/Mathlib/Data/Nat/Factorial/Basic.lean#L40
Last updated: Dec 20 2025 at 21:32 UTC