lean4-htt/tests/lean/run/cdotTests.lean
Kyle Miller d45cc674ea
feat: make cdot expansion take hygiene into account (#9443)
This PR makes cdot function expansion take hygiene information into
account, fixing "parenthesis capturing" errors that can make erroneous
cdots trigger cdot expansion in conjunction with macros. For example,
given
```lean
macro "baz% " t:term : term => `(1 + ($t))
```
it used to be that `baz% ·` would expand to `1 + fun x => x`, but now
the parentheses in `($t)` do not capture the cdot. We also fix an
oversight where cdot function expansion ignored the fact that type
ascriptions and tuples were supposed to delimit expansion, and also now
the quotation prechecker ignores the identifier in `hygieneInfo`. (#9491
added the hygiene information to the parenthesis and cdot syntaxes.)

This fixes a bug discovered by [Google
DeepMind](https://storage.googleapis.com/deepmind-media/DeepMind.com/Blog/imo-2024-solutions/P1/index.html),
which made use of `useλy . x=>y.rec λS p=>?_`. The `use` tactic from
Mathlib wrapped the provided term in a type ascription, and so this was
equivalent to `use fun x => λy x x=>y.rec λS p=>?_`. (Note that cdot
function expansion is not able to take into account *where* the cdots
are located, and it is syntactically valid to insert an identifier into
the binder list like this. If we ever want to address this in the
future, we could have cdots expand into a special term that wraps an
identifier that evaluates to a local, but which would cause errors in
other contexts.)

Design note: we put the `hygieneInfo` on the open parenthesis rather
than at the end, since that way the hygiene information is available
even when there are parsing errors. This is important since we rely on
being able to elaborate partial syntax to get elab info (e.g. in `(a.`
to get completion info). Note that syntax matchers check that the
`hygieneInfo` is actually present, so such partial syntax would not be
matched.
2025-07-24 00:43:32 +00:00

89 lines
2.2 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-!
# Tests of cdot functions
-/
set_option pp.mvars false
class Inc (α : Type) :=
(inc : αα)
export Inc (inc)
instance {α} [Inc α] : Inc (List α) :=
{ inc := (·.map inc) }
instance : Inc Nat :=
{ inc := Nat.succ }
#guard inc 10 == 11
#guard inc [1, 2, 3] == [2, 3, 4]
theorem ex1 : [(1, "hello"), (2, "world")].map (·.1) = [1, 2] :=
rfl
theorem ex2 : [(1, "hello"), (2, "world")].map (·.snd) = ["hello", "world"] :=
rfl
def sum (xs : List Nat) : Nat :=
(·.2) $ Id.run $ StateT.run (s:=0) do
xs.forM fun x => modify (· + x)
#guard sum [1, 2, 3, 4] == 10
theorem ex3 : sum [1, 2, 3] = 6 :=
rfl
theorem ex4 : sum [1, 2, 3, 4] = 10 :=
rfl
/-!
Check that ambiguous notation inside cdot notation still has only a single argument.
(Need to process choice nodes specially.)
-/
def tag (_ : α) (y : α) := y
notation "f" x => tag 1 x
notation "f" x => tag "2" x
/-- info: fun x => (f x).length : String → Nat -/
#guard_msgs in
#check (String.length <| f ·)
/-!
Check that cdots can't be captured in `let` functions
(there is a type ascription the body syntax is inserted into).
-/
/--
error: invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`)
---
error: invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`)
-/
#guard_msgs in
def a : Nat → Nat → Nat :=
let (add) : Nat → Nat → Nat := · + ·
add
/-!
Check that cdots can't be captured in macros.
-/
macro "baz% " t:term : term => `(1 + ($t))
/--
error: invalid occurrence of `·` notation, it must be surrounded by parentheses (e.g. `(· + 1)`)
---
info: 1 + sorry : Nat
-/
#guard_msgs in #check baz% ·
/-!
Note that cdots still work here since they are expanded before the inner macro is expanded.
-/
/-- info: fun x => 1 + x : Nat → Nat -/
#guard_msgs in #check (baz% ·)
/-!
Check that parentheses, type ascriptions, and tuples each delimit cdot expansion.
-/
/-- info: fun x => x ∘ fun x => x : (?_ → ?_) → ?_ → ?_ -/
#guard_msgs in #check (· ∘ (·))
/-- info: fun x => x ∘ fun x => x : (Nat → ?_) → Nat → ?_ -/
#guard_msgs in #check (· ∘ (· : Nat → Nat))
/-- info: fun x => (x, fun x_1 => (1, x_1)) : (x : ?_) → ?_ × (?_ x → Nat × ?_ x) -/
#guard_msgs in #check (·, (1, ·))