Zulip Chat Archive

Stream: lean4

Topic: generating inductive syntax


James Gallicchio (Apr 03 2023 at 07:04):

Still largely unfamiliar with metaprogramming, so I'm sure I am missing something obvious, but how do I generate the TSyntax `Lean.Parser.Command.ctor necessary for this metaprogram to work?

def keywordsRaw := #[
    "struct"
  , "typedef"
  , "if"
  , "..." ]

open Lean Macro in
scoped macro "genKeywordType!" : command => do
  `(inductive $(mkIdent `Keyword) : Type where
    $[ $(
      keywordsRaw.map (fun kw => sorry)
    ) ]*
  )

I've tried lots of different quotations to try to fill in the sorry, but haven't found the right one. I see from the parser for ctor that it should look something like `(| $kw) but that does not work. I also can't find any documentation in the metaprogramming book or on zulip about how to generate inductive decl syntax D:

Should I just be building an elab command instead of a macro for this?

Mario Carneiro (Apr 03 2023 at 07:06):

I think you probably don't want to generate the ctor yourself; it looks like you just want to make an identifier

Mario Carneiro (Apr 03 2023 at 07:07):

in which case you can do something like

scoped macro "genKeywordType!" : command => do
  `(inductive $(mkIdent `Keyword) : Type where
    $[ | $(keywordsRaw):ident ]*
  )

James Gallicchio (Apr 03 2023 at 07:08):

aha!

scoped macro "genKeywordType!" : command => do
  `(inductive $(mkIdent `Keyword) : Type where
    $[ | $(keywordsRaw.map mkIdent):ident ]*
  )

James Gallicchio (Apr 03 2023 at 07:09):

what does the :ident do there? if i remove it Lean complains about a syntax error at the $[! :joy:

Mario Carneiro (Apr 03 2023 at 07:11):

A general antiquotation could be standing in for a variety of syntax categories, so it is somewhat ambiguous for the parser

James Gallicchio (Apr 03 2023 at 07:12):

Got it. So when I have weird parse errors I should annotate my antiquotations.

Mario Carneiro (Apr 03 2023 at 07:12):

it can't use type information because it is happening at parse time


Last updated: Dec 20 2023 at 11:08 UTC