Zulip Chat Archive

Stream: metaprogramming / tactics

Topic: interpolating localDecls into syntax quotations


Eric Wieser (Jun 06 2024 at 12:12):

What's the right way of writing quick tactics like this?

import Lean
import Batteries.Lean.Expr

open Lean Meta Elab Tactic

example (a b c : Nat) (ha : a = 1) (hb : b = 1) (hc : c = 1) : True := by
  -- flip them all
  replace ha := ha.symm
  replace hb := hb.symm
  replace hc := hc.symm

  -- now do the same thing in a loop
  run_tac do
    for n in [`ha, `hb, `hc] do
      let d  getLocalDeclFromUserName n
      let s := mkIdent d.userName
      let ds : Term  d.toExpr.toSyntax
      evalTactic <|  `(tactic| replace $s := $(ds).symm)
  sorry

The above fails with

synthetic hole has already been defined and assigned to value incompatible with the current context
  ha

Damiano Testa (Jun 06 2024 at 12:22):

withMainContext?

Damiano Testa (Jun 06 2024 at 15:41):

I.e.

import Lean
import Batteries.Lean.Expr

open Lean Meta Elab Tactic

example (a b c : Nat) (ha : a = 1) (hb : b = 1) (hc : c = 1) : True := by
  -- flip them all
  replace ha := ha.symm
  replace hb := hb.symm
  replace hc := hc.symm

  -- now do the same thing in a loop
  run_tac withMainContext do
    for n in [`ha, `hb, `hc] do
      let d  getLocalDeclFromUserName n
      let s := mkIdent d.userName
      let ds : Term  d.toExpr.toSyntax
      evalTactic <|  `(tactic| replace $s := $(ds).symm)
  /-
  ha: a = 1
  hb: b = 1
  hc: c = 1
  -/
  sorry

Last updated: May 02 2025 at 03:31 UTC