Zulip Chat Archive

Stream: lean4

Topic: Interpolation in comments


Siddhartha Gadgil (Jul 30 2024 at 10:18):

Can someone help with the correct way to do this:

def mkTheoremWithDoc (name: Name)(type: Syntax.Term)
    (doc: String) :
        CoreM String := do
    let name := mkIdent name
    let stx  `(command|/-- $doc -/
    theorem $name : $type := by sorry)
    let fmt  ppCategory `command stx
    return fmt.pretty

The problem is that $doc is taken as part of a comment, so ignored. I can make a separate string for the comment, but it seems aesthetically better to tell Lean what I mean.

To clarify, I got a warning that the variable doc is never used. I did not actually test.

Damiano Testa (Jul 30 2024 at 10:23):

It is unused, since it uses the literal text /-- $doc -/ as its doc.

Damiano Testa (Jul 30 2024 at 10:23):

Oh, you already discovered this!

Siddhartha Gadgil (Jul 30 2024 at 10:24):

Yes, I am asking if there is a way to make it be used.

Damiano Testa (Jul 30 2024 at 10:30):

This seems to work:

def mkTheoremWithDoc (name: Name)(type: Syntax.Term)
    (doc: String) :
        CoreM String := do
    let name := mkIdent name
    let docs := mkNode ``Lean.Parser.Command.docComment #[mkAtom "/--", mkAtom (doc ++ " -/")]
    let stx  `(command| $docs:docComment theorem $name : $type := by sorry)
    let fmt  ppCategory `command stx
    return fmt.pretty

Siddhartha Gadgil (Jul 30 2024 at 10:31):

Thanks.


Last updated: May 02 2025 at 03:31 UTC