Zulip Chat Archive

Stream: Lean for teaching

Topic: Creating aliases for a tactic


Lucas Arruda (Apr 17 2025 at 22:08):

Is there any easy way to do this? No change to behavior, just add a more convenient name for a tactic.

Siddhartha Gadgil (Apr 18 2025 at 02:45):

Yes.

macro "trust_me" : tactic => do
  `(tactic|sorry)

example: 1 =2 := by trust_me

Siddhartha Gadgil (Apr 18 2025 at 02:46):

If you want parameters:

macro "use_function" f:term : tactic => do
  `(tactic| apply $f)

example : 1  1 := by use_function Nat.le_refl

Patrick Massot (Apr 18 2025 at 09:28):

To be honest, it can get more complicated if the tactic has complicated parameters, or optional ones. So don’t hesitate to ask here if you can get in macro world.

Lucas Arruda (Apr 18 2025 at 13:02):

I run into some problems when trying to create a macro that expands to intro something.

open Lean Elab

macro "mIntro " h:term : tactic => do
  `(tactic| intro $h)
-- application type mismatch
--  h.raw
-- argument
--  h
-- has type
--  TSyntax `term : Type
-- but is expected to have type
--  TSyntax `Lean.Parser.Term.matchAlts : Type
open Lean Elab
declare_syntax_cat Lean.Parser.Term.matchAlts
macro "mIntro " h:Lean.Parser.Term.matchAlts : tactic => do
  `(tactic| intro $h)
-- application type mismatch
--  h.raw
-- argument
--  h
-- has type
--  TSyntax `Lean.Parser.Term.matchAlts : Type
-- but is expected to have type
--  TSyntax `term : Type

Patrick Massot (Apr 18 2025 at 13:06):

intro is indeed a bit complicated since it can take several kind of arguments. Would you be happy with a version taking exactly one identifier?

Lucas Arruda (Apr 18 2025 at 13:08):

It would be a start, i guess. But it'd also be nice to have a version that can do exactly what intro does

Patrick Massot (Apr 18 2025 at 13:08):

Actually having multiple ones isn’t really harder:

Patrick Massot (Apr 18 2025 at 13:08):

macro "mIntro " h:term* : tactic => do
  `(tactic| intro $h:term*)

Patrick Massot (Apr 18 2025 at 13:10):

What won’t work is the pattern stuff.

Patrick Massot (Apr 18 2025 at 13:10):

I mean things like intro ⟨x, h⟩

Patrick Massot (Apr 18 2025 at 13:12):

We could make it work of course, but it will require copying stuff from the core intro elaborator.

Lucas Arruda (Apr 18 2025 at 13:20):

oh, that is already very useful! Thank you!

if i want to go ahead and try copying stuff from the core intro elaborator, could you give me some advice?

Patrick Massot (Apr 18 2025 at 13:26):

You can simply write intro in your proof and use go to definition to see the elaborator.

Patrick Massot (Apr 18 2025 at 13:28):

It should bring you to https://github.com/leanprover/lean4/blob/02b206af9b857a2bbc796394aec14fe192ab59d7/src/Lean/Elab/Tactic/BuiltinTactic.lean#L282

Patrick Massot (Apr 18 2025 at 13:28):

If you need more help about that, I recommend switching to the metaprogramming channel which is more suitable for technical meta-stuff.

Lucas Arruda (Apr 18 2025 at 13:29):

Thank you, again!

Patrick Massot (Apr 18 2025 at 13:29):

And zulip doesn’t want to create that link :sad:

Kevin Buzzard (Apr 18 2025 at 13:51):

#metaprogramming / tactics

I get it by #metaprogr and then tab and then tab again (but it still seems to link to "last message posted in the channel" as opposed to anything like an overview)


Last updated: May 02 2025 at 03:31 UTC