Zulip Chat Archive

Stream: lean4

Topic: CLI library design


Henrik Böving (Feb 19 2022 at 18:42):

What's your opinion on CLI library design for Lean 4? I'm not aware of anything yet and would like to write a proper CLI for doc-gen4 but phase that out into a library.

The designs I'm aware of generally are:

Which of these do you think would be the best fit for lean? With the meta programming capabilities it should definitely not be an issue to implement either of these but I'm wondering what you think would be the most idiomatic to implement.

Mario Carneiro (Feb 19 2022 at 19:01):

I think the clap macro approach makes more sense than the haskell applicative stuff. There isn't really a need for using higher order functions (with lots of sigils) to get a nice interface when a macro can more directly express the desired input syntax as well as being able to produce values in structs made just for the purpose, or even structs constructed by the macro itself like rust structopt

Mario Carneiro (Feb 19 2022 at 19:02):

clap is a really full featured library though (to the point that you can usually see it in compile times). You probably want to trim that down a bit for an initial implementation

Henrik Böving (Feb 19 2022 at 19:09):

Hmmm, so this would require attributes in field declarations right? This does however not seem possible right now so I guess a good point to start would be to try and add this to the structure elaborator? I'd imagine we could just tag the resulting projection functions with the attributes right?

Mario Carneiro (Feb 19 2022 at 19:13):

No, it would use a custom syntax unrelated to structure

Mario Carneiro (Feb 19 2022 at 19:14):

although you can make it visually resemble a structure declaration if you want

Mario Carneiro (Feb 19 2022 at 19:16):

although I should point out that field declarations can actually have attributes

Henrik Böving (Feb 19 2022 at 19:17):

image.png my Lean does not seem to believe so.

Mario Carneiro (Feb 19 2022 at 19:17):

well, at least syntactically

Henrik Böving (Feb 19 2022 at 19:17):

Ah yes, the error is from the elaborator.

Mario Carneiro (Feb 19 2022 at 19:18):

which for a macro is really all you need

Mario Carneiro (Feb 19 2022 at 19:19):

I believe attributes in that position are intended to work in the future, that error looks like a todo

Henrik Böving (Feb 19 2022 at 19:20):

In that case why make up a different macro language?

Mario Carneiro (Feb 19 2022 at 19:22):

I'm not sure that structure + attributes is actually the best way to express requirements here. It's done that way in rust because it's a limitation of how attribute macros work

Henrik Böving (Feb 19 2022 at 19:23):

Do you have another "vision" for how one could express them then? If yes please do share

Mario Carneiro (Feb 19 2022 at 19:24):

The macro is deprecated now for clap v3 but clap v2 includes a clap_app! macro which expresses the same information in a very different way. It ends up looking more like the help text

Mario Carneiro (Feb 19 2022 at 19:24):

Here's an example of use from my project: https://github.com/digama0/mm0/blob/master/mm0-rs/src/main.rs#L5-L39

Patrick Massot (Feb 19 2022 at 19:25):

See also https://github.com/mhuisi/lean4-cli

Patrick Massot (Feb 19 2022 at 19:26):

From @Marc Huisinga

Henrik Böving (Feb 19 2022 at 19:26):

Heh, seems like they updated it to work with Lake just 3 minutes ago, it appears we are being watched silently :eye:

Marc Huisinga (Feb 19 2022 at 19:29):

It should be compatible with Lake now, I think.

Marc Huisinga (Feb 19 2022 at 19:31):

It doesn't have autocompletion yet and the final type conversion step isn't type safe. Those are two things that should likely still be improved upon.

Simon Hudon (Feb 20 2022 at 00:25):

I also have something that I'm playing with. The idea is to annotate your structure declaration and use a deriving command to get the parser out. E.g.:

structure Flags where
  traceCmd :
    CmdLineFlag "-c" (some "--cmd")
    "tracing: print command"
  traceSubst :
    CmdLineFlag "-s" none
    "tracing: print module renaming"
  traceRoot :
    CmdLineFlag "-r" none
    "tracing: print command"
  optValue :
    CmdLineOpt "-t" none Nat
    "tracing: test option parsing"
  dryRun :
    CmdLineFlag "-d" none
    "dry run: calculate parameters but perform no action"

Here is the library: https://github.com/cipher1024/lean4-prog/tree/main/cmd-line-args

It still needs some work and some cleaning up but any feedback would be welcome


Last updated: Dec 20 2023 at 11:08 UTC