Zulip Chat Archive

Stream: lean4

Topic: Macro expanding to binder


Adam Topaz (Mar 13 2023 at 21:44):

I'm trying to figure out whether it's possible to write a macro that expands to a binder.
As a simple example, let's say I want [[foo X]] to expand to [Mul X] [Add X] and to be able to write things like

def test (X : Type) [[foo X]] (x : X) : X := x + x * x

Is this possible?

Mario Carneiro (Mar 13 2023 at 21:46):

no

Mario Carneiro (Mar 13 2023 at 21:46):

not unless you replace def

Mario Carneiro (Mar 13 2023 at 21:47):

or change core, of course

Adam Topaz (Mar 13 2023 at 21:49):

Okay, thanks. That's unfortunate.

Adam Topaz (Mar 13 2023 at 21:51):

Semirelated: does lean4/mathlib4 have an analogue of docs#lean.parser.emit_code_here

James Gallicchio (Mar 13 2023 at 21:55):

Mario Carneiro said:

not unless you replace def

(replace = overload?)

Mario Carneiro (Mar 14 2023 at 00:32):

Adam Topaz said:

Semirelated: does lean4/mathlib4 have an analogue of docs#lean.parser.emit_code_here

It's not really necessary, as you can just call elabCommand with whatever syntax you like

Mario Carneiro (Mar 14 2023 at 00:33):

James Gallicchio said:

Mario Carneiro said:

not unless you replace def

(replace = overload?)

No, overload here would presumably mean defining a new elab_rules for the existing syntax, while in this case you would want to create a new syntax that shadows the old one

Mario Carneiro (Mar 14 2023 at 00:34):

they have a similar effect, but one changes the behavior while the other changes the syntax

Tomas Skrivan (Mar 14 2023 at 12:55):

Recently I had the same question, there is a code example for overloading lambda function notation.


Last updated: Dec 20 2023 at 11:08 UTC