Zulip Chat Archive

Stream: mathlib4

Topic: Probability theorem notation clashes with GetElem


Eric Wieser (Sep 18 2025 at 12:09):

Can anything be done to avoid this? This code

import Mathlib

open scoped ProbabilityTheory

example (L : List ) (i : ) : L[i] = L[i] := sorry

produces

Ambiguous term
  L[i]
Possible interpretations:
   (x : ?m.1), ?m.8 ∂?m.6 : ?m.2

  L[i] : 

but I'd like it to produce

failed to prove index is valid, possible solutions:
...

(continued from #lean4 > Kernel error in list index notation @ 💬 )

Eric Wieser (Sep 18 2025 at 12:10):

Can the elaborator for tell Lean that it certainly isn't a match, and let Lean try the other syntax instead?

Kim Morrison (Sep 18 2025 at 12:15):

Beating a dead horse here, but yes, something can be done to avoid this: don't use [ ] in any notations besides getElem and List!

Eric Wieser (Sep 18 2025 at 12:17):

Would Lean.Elab.throwUnsupportedSyntax if we can't type L as a Measure work here?

Eric Wieser (Sep 18 2025 at 12:18):

Oh, maybe the other answer here is that we should define a GetElem instance for measures?

Rémy Degenne (Sep 18 2025 at 12:18):

What could we use in the probability notation, that looks somewhat like standard math (so looks like the current P[...] notation) and is not some weird unicode thing that looks like [ ] but isn't?

Eric Wieser (Sep 18 2025 at 12:25):

Eric Wieser said:

Oh, maybe the other answer here is that we should define a GetElem instance for measures?

This has the weirdness that it doesn't expand directly to the integral, and we have to rewrite back and forth with GetElem

Kenny Lau (Sep 18 2025 at 12:26):

Somehow I failed to minimise it:

import Lean

axiom Real : Type
axiom Expectation {α β : Type} : α  β  Real

namespace ProbabilityTheory

-- https://github.com/leanprover-community/mathlib4/blob/ea6efff623794bdbaf004a17d17eb98316155442/Mathlib/Probability/Notation.lean#L48
scoped macro:max P:term noWs "[" X:term "]" : term => `(Expectation $X $P)

end ProbabilityTheory

-- failed to prove index is valid ...
example (L : List Real) (i : Nat) : L[i] = L[i] := sorry

open scoped ProbabilityTheory

-- ⊢ Expectation i L = Expectation i L
example (L : List Real) (i : Nat) : L[i] = L[i] := sorry

Kenny Lau (Sep 18 2025 at 12:26):

Kim Morrison said:

Beating a dead horse here, but yes, something can be done to avoid this: don't use [ ] in any notations besides getElem and List!

I can't test it yet (because I failed to minimise it above), but I think in my experience with the other PR it seems to work if we put atomic("[" stuff "]")

Kenny Lau (Sep 18 2025 at 12:27):

also just generally I disagree with this suggestion

David Ledvinka (Sep 18 2025 at 12:35):

Rémy Degenne said:

What could we use in the probability notation, that looks somewhat like standard math (so looks like the current P[...] notation) and is not some weird unicode thing that looks like [ ] but isn't?

Possibly something like 𝔼[X; P] (which mimics Var[X; P])?

Jovan Gerbscheid (Sep 18 2025 at 12:53):

Kenny Lau said:

Somehow I failed to minimise it:

Maybe Lean gives priority to notation defined in the current file when it's otherwise unclear?

Eric Wieser (Sep 18 2025 at 12:59):

I think it's because Expectation is too simple

Kenny Lau (Sep 18 2025 at 13:00):

could you please try to minimise it?

Kenny Lau (Sep 18 2025 at 13:01):

i added [Field β] to Expectation but still didn't get the same error message

Jovan Gerbscheid (Sep 18 2025 at 13:05):

I'd think that if you're defining a term noWs "[" term "]" syntax, you can't have that and have the list indexing notation at the same time. (Note that this problem doesn't apply to the polynomial notation that we're working on now, because in that notation the thing between the square brackets does not parse as a term)

Eric Wieser (Sep 18 2025 at 13:06):

structure Wrapper (α) where val : α

instance {α : Type} [NatCast α] : NatCast (Wrapper α) := sorry

def foo {α : Type} (μ : Wrapper α) (f : α  Nat) : Nat := sorry

macro:max P:term noWs "[" term "]" : term => `(foo $P fun x => 0)

theorem ambiguous
    (L : List Nat) (i : Nat) :
    L[i] = 0 := sorry

Kenny Lau (Sep 18 2025 at 13:06):

thanks!

Kenny Lau (Sep 18 2025 at 13:07):

and atomic doesn't solve it...

Eric Wieser (Sep 18 2025 at 13:08):

I think you have to have the parsing for the notation exactly match the GetElem one

Eric Wieser (Sep 18 2025 at 13:22):

I can't work out the relevance of NatCast here...

Eric Wieser (Sep 18 2025 at 13:22):

set_option trace.Meta.synthInstance true has some surprising outputs

Jovan Gerbscheid (Sep 18 2025 at 13:30):

Wow, that's interesting. It's looking for a way to coerce List Nat into Wrapper ?_. For that it asks if anything coerces into Wrapper ?_. And it finds your instance, showing that Nat may coerce to it. Importantly, this means that it gets stuck, instead of failing outright. And if all possible options get stuck, then the parsing is ambiguous.

Eric Wieser (Sep 18 2025 at 13:48):

Indeed, instance {α : Type} : Coe α (Wrapper α) := sorry is sufficient to trigger this

Eric Wieser (Sep 18 2025 at 13:54):

And in fact, the trouble with the probability notation can be fixed with

-scoped macro:max P:term noWs "[" X:term "]" : term => `(∫ x, ↑($X x) ∂$P)
+scoped macro:max P:term noWs "[" X:term "]" : term => `(∫ x, $X x ∂$P)

though presumably this causes issues elsewhere

Kenny Lau (Sep 18 2025 at 14:04):

we can change the (priority := of the macros right

Eric Wieser (Sep 18 2025 at 14:19):

Priority on notation I believe means that if one fails the other is never tried

Eric Wieser (Sep 19 2025 at 00:43):

Having realized that actually this isn't a blocker for me, I think what I'd like to see here is that the error message is extended to something like

Ambiguous term
  L[i]
Possible interpretations:
   (x : ?m.1), ?m.8 ∂?m.6 : ?m.2
    which failed with:
       could not synthesize placeholder ...

  L[i] : 
    which failed with:
       failed to prove index is valid, possible solutions:

Is this possible given the semantics of overloaded notation, or do I have an incorrect mental model of the elaboration order?

Eric Wieser (Sep 19 2025 at 00:43):

(in fact the kernel issue was the problem that mattered, which is now fixed)

Jovan Gerbscheid (Sep 19 2025 at 06:05):

Somehow I'd be happier using a notation if it did not always cause a nonsensical type class search for GetElem

Eric Wieser (Sep 19 2025 at 08:38):

Does that happen here even if the probability interpretation elaborates correctly?

Jovan Gerbscheid (Sep 19 2025 at 12:39):

If the priority is the same, doesn't it need to try elaborating both syntaxes to see whether there is an ambiguity?


Last updated: Dec 20 2025 at 21:32 UTC