Zulip Chat Archive

Stream: general

Topic: Is it possible to use "..." in syntax or macro definitions?


samuela (Oct 13 2025 at 19:31):

I would like to support numpy-esque syntax like arr[i, j, ...]. so far i have not managed to find a way to do this in lean4. is it possible or is this just a locked down symbol?

MWE

Kenny Lau (Oct 13 2025 at 19:34):

we definitely have something like that for range

samuela (Oct 13 2025 at 19:38):

@Kenny Lau Interesting, perhaps I'm missing something in my usage? What would the range thing look like?

Kenny Lau (Oct 13 2025 at 19:55):

@samuela i removed some spaces and it seem to work better:

macro:max "test" "[" indices:term,+ ",...]" : term =>
  `([$indices,*])

#check test[1, 2,...]

Kenny Lau (Oct 13 2025 at 19:55):

btw next time instead of posting a link it would be better to post the code in #backticks

Kenny Lau (Oct 13 2025 at 19:56):

it automatically generates a link on the top right corner of the box

samuela (Oct 13 2025 at 22:49):

ok figured this out:

inductive SliceTerm where
  | Singleton : Nat -> SliceTerm
  | Ellipsis : SliceTerm

instance : OfNat SliceTerm n where
  ofNat := SliceTerm.Singleton n

syntax "..." : term
macro_rules
  | `(...) => `(SliceTerm.Ellipsis)

syntax "test[" term,+ "]" : term
macro_rules
  | `(test[$indices,*]) => `([$indices,*])

#check test[1, 2, ...]

which i'm happy with now

samuela (Oct 13 2025 at 22:55):

actually this allows for arr[1, ..., 2, ..., 3] which i would like to avoid. it'd be nice to be able to write something to the effect of

syntax "test[" term,* "..." term,* "]" : term

would something like that possible? eg could one match on a specific type of term, esp non-"..." terms?

Sebastian Ullrich (Oct 14 2025 at 07:43):

If rejecting some syntax combinations during parsing is too hard, you can always do it in the macro/elaborator. That usually leads to better error messages anyway, e.g. "Cannot combine multiple ... in the same indexing operation"

samuela (Oct 15 2025 at 00:32):

Ah ok, thanks @Sebastian Ullrich ! I didn't realize you could handle that in elaboration


Last updated: Dec 20 2025 at 21:32 UTC