Zulip Chat Archive

Stream: lean4

Topic: sending elaboration error to custom syntax category


Tomas Skrivan (Oct 29 2025 at 23:53):

I have a custom syntax category cexpr and I have a macro that turns it into syntax of type term. How can I correctly propagate elaboration errors back to cexpr?

import Lean

open Lean

declare_syntax_cat cexpr

syntax ident : cexpr
syntax cexpr "+" cexpr : cexpr
syntax "(" cexpr ")" : cexpr

syntax (name:=cexprStx) "cexpr% " cexpr : term

macro_rules (kind:=cexprStx)
| `(cexpr% $id:ident) => `(term| $id:ident)
| `(cexpr% $x:cexpr + $y:cexpr) => `(term| (cexpr% $x:cexpr) + (cexpr% $y:cexpr))
| `(cexpr% ( $x:cexpr )) => `(term| (cexpr% $x:cexpr))

variable (a b c : Float) (s : String)

#check a + (b + s)
#check cexpr% a + (b + s)

Is there a way to make the red squiggle to show under (b + s) in cexpr% a + (b + s) ?
image.png

Aaron Liu (Oct 29 2025 at 23:54):

while it's certainly possible (it's done for term after all) I can't think of any easy way to do it

Aaron Liu (Oct 29 2025 at 23:55):

oh I just thought of an easy way to do it (if it works)

Aaron Liu (Oct 30 2025 at 00:04):

oh it totally didn't work

Tomas Skrivan (Oct 30 2025 at 00:04):

Ahh what a shame :cry:

Aaron Liu (Oct 30 2025 at 00:05):

final product (failure)

Aaron Liu (Oct 30 2025 at 00:06):

all that syntax referencing didn't make a difference since I guess it just goes back to the place it entered term-land

Aaron Liu (Oct 30 2025 at 00:06):

oh maybe that's why "unsolved goals" highlights the entire tactic block

Tomas Skrivan (Oct 30 2025 at 00:12):

Very interesting attempt! Unfortunately I have no idea if it can be salvaged or not.

Thomas Murrills (Oct 30 2025 at 01:56):

I think all you need to do is attach the position info of the component syntax to the constructed syntax (via e.g. withRef) :)

image.png

I haven't thought very hard about whether this could produce any unexpected behavior sometimes, though (or if each of these cases is exactly correct, especially the last). :thinking:

(The hygiene management might be interesting for other reasons...! :eyes:)

Thomas Murrills (Oct 30 2025 at 02:06):

To be slightly more careful (but still possibly not sufficiently careful!), I might rewrite the macro_rules above slightly to

code

(the difference being mainly the position info of c appearing on the final + term)

Tomas Skrivan (Oct 30 2025 at 08:32):

Thanks, this works exactly as I want to. I need to test it also when I convert to doElem but I don't see a reason why it wouldn't work.


Last updated: Dec 20 2025 at 21:32 UTC