Zulip Chat Archive
Stream: metaprogramming / tactics
Topic: Constructing syntax for natural number literal
Segev Elazar Mittelman (Oct 10 2025 at 16:01):
Hi I attempted to create a TSyntax for a natural number literal given a Literal of the form .natVal n I did
def mkLiteral (l : Literal) : MetaM (TSyntax `term) :=
match l with
| .natVal n => `($(Syntax.mkNumLit (toString n)))
| .strVal s => `($(Syntax.mkStrLit s))
But when I to elaborate something which uses mkLiteral, it creates for instance, "Eq n_1 (OfNat.ofNat («5»))", and I get this error:
Unknown identifier «5»
Am I constructing the syntax wrong?
Bhavik Mehta (Oct 10 2025 at 16:21):
I think you want to use Syntax.mkNatLit in the first case
Segev Elazar Mittelman (Oct 10 2025 at 16:34):
Unfortunately both choices display the error.
Robin Arnez (Oct 10 2025 at 16:40):
I can't tell what's wrong, it seems to work on my side, what do you do with the output?
Segev Elazar Mittelman (Oct 10 2025 at 16:42):
Try
#eval (Eq 1 (OfNat.ofNat («5»)))
Aaron Liu (Oct 10 2025 at 16:48):
what is the expected output and what is the actual output
Robin Arnez (Oct 10 2025 at 16:49):
I mean this works?
import Lean
open Lean Meta
def mkLiteral (l : Literal) : MetaM (TSyntax `term) :=
match l with
| .natVal n => `($(Syntax.mkNumLit (toString n)))
| .strVal s => `($(Syntax.mkStrLit s))
run_elab
let num ← mkLiteral (.natVal 5)
let term ← `(Eq 1 (OfNat.ofNat $num))
Lean.logInfo (← Lean.Elab.Term.elabTerm term none)
Segev Elazar Mittelman (Oct 10 2025 at 17:04):
Hm, I suspect this issue is caused elsewhere. I'll continue looking.
Last updated: Dec 20 2025 at 21:32 UTC