Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command): Add #where command, dumping environment info #489

Merged
merged 1 commit into from
Dec 20, 2018

Conversation

khoek
Copy link
Collaborator

@khoek khoek commented Nov 22, 2018

Add the #where command.

The command tells you your current namespace (wherever you write it),
the current includes, and the current variables (variables must have been
used at least once).

Some spurious name manipulation and lean.parser code-generation facilities have been added to tactic/basic.lean for this purpose. (Maybe a tactic/parser.lean file will be eventually be warranted?)

We also add a few useful parser library functions (at the bottom of tactic.where): e.g. If you're in the lean.parser monad you can just call get_namespace to retrieve the name of the current namespace. If you're defining a tactic, you can add (p : interactive.parse lean.parser.get_namespace) to its parameters to parse the name p of the current namespace.



TO CONTRIBUTORS:

Make sure you have:

  • reviewed and applied the coding style: coding, naming
  • for tactics:
  • make sure definitions and lemmas are put in the right files
  • make sure definitions and lemmas are not redundant

For reviewers: code review check list

@khoek khoek force-pushed the where-cmd branch 2 times, most recently from 51f95e4 to 3b7e065 Compare November 23, 2018 01:55
@jcommelin
Copy link
Member

jcommelin commented Nov 24, 2018

I gave this a little spin, and I think it is going to be really useful. But I couldn't fully understand the results. Here is what I did.
I went to data/nat/basic.lean, and added import tactic.where on line 9. On line 17 I added #where.
This printed

namespace: nat
variables: {m : ℕ} {n : ℕ} (m : ℕ) (n : ℕ) {k : ℕ} (k : ℕ) (n : less_than_or_equal #4 #0) (n : less_than_or_equal #2 #0)

The namespace is as expected, but there is a lot of duplication in the variables. Also, I have no idea what the less_than_or_equal stuff means.
So, kudos for giving us this information! Because this is already going to help me quite often when I'm totally lost about the environment I'm in. But this test got me thinking, and maybe there is some room for improvement. (But I don't know if this is feasible...)
What would be really useful is if one could just copy paste the variables line to setup an environment. (In particular I suggest removing the colons after namespace and variables.)
Typical use case: I want to add something about categorical limits, but the file about categorical limits is currently being PR'd. I don't want to add to this PR, so I want to add it in some misc file. If I could just type #where at the point in the limits file where I would have liked to add my stuff, and then copy paste the environment to the misc file, that would be very useful! It just reduces the mental effort of thinking which variables etc I need to add. And in the parts that are higher up in the hierarchy you sometimes need to set up some 10 variables before you can make useful statements.
Please let me know if something like this is possible!

@khoek
Copy link
Collaborator Author

khoek commented Nov 24, 2018

Hi @jcommelin sorry, I didn't think of this. The problems + garbage in there is highly fixable. I'll try to do it today.

@khoek
Copy link
Collaborator Author

khoek commented Nov 24, 2018

@jcommelin Should be all fixed now. You should now see exactly what you expected (and was intended---plus your nice "namespace" suggestion).

@khoek khoek force-pushed the where-cmd branch 2 times, most recently from 698ddab to 0fb4126 Compare November 24, 2018 11:17
@rwbarton
Copy link
Collaborator

Could we make this exported by tactic.interactive? Then it will be available in almost all mathlib files by default, which would be more convenient.

@khoek
Copy link
Collaborator Author

khoek commented Nov 25, 2018

Done

@jcommelin
Copy link
Member

Here's a typical piece of output that I got

namespace category_theory.limits

variables {K : Type ?} {C : Type ?} {J : Type ?} [𝒞 : category C] [𝒦 : small_category K] [_inst_1 : small_category J]
include C 𝒞 K 𝒦



end category_theory.limits

I think this is really helpful. However, as you can see, it didn't figure out the universe parameters. Is that within reach?
(Also, you could consider hiding the "_inst_1 : " string, but I don't know if that is actually a good idea or not.)

@khoek khoek force-pushed the where-cmd branch 5 times, most recently from 71234fc to 9e6001c Compare November 29, 2018 04:26
@robertylewis
Copy link
Member

@johoelzl @digama0 This PR looks good to me. @khoek were you planning to add anything else?

tactic/interactive.lean Outdated Show resolved Hide resolved
@khoek
Copy link
Collaborator Author

khoek commented Dec 5, 2018

@robertylewis No, everything should be in there now. Just so that it is recorded here, I fixed Johan's problem and added the [_inst_1 : xxx] feature he suggested in the force push after his comment.

@rwbarton
Copy link
Collaborator

rwbarton commented Dec 7, 2018

I don't think this should be imported with the default set. It's a transitory command, so it won't be needed in most of mathlib. Perhaps we should have an import set for transitory commands like #find and #where but they shouldn't be imported in basic files that don't need them.

Hmm, I think this will significantly hamper the convenience of #where. Instead of just editing the file at the point I care about, now I have to add an import at the top at the file as well (possibly triggering a bunch of recompilation which Lean otherwise wouldn't do?) and also remember to remove it when I'm done. I mostly imagine using this temporarily in mathlib files I'm not editing, but where I want to understand the types of the lemmas, or set up the same variable environment in my own files.

I also predict that if this command is not imported by default, then you will start receiving PRs with import tactic.where in them and have to tell people to delete that line. This alone makes it worthwhile to import by default I think.

@digama0
Copy link
Member

digama0 commented Dec 7, 2018

I agree that people will accidentally import tactic.where, but that doesn't make it a good reason to import by default. It's like adding debugging imports - they should be removed before going into "production". Conceivably lean could support these kinds of imports through a command line --debug option, but currently the easiest way to express this kind of thing is through imports.

@jcommelin
Copy link
Member

I completely agree with Reid. I understand Mario's objections, but they are in some sense ideological, I think. From a practical point of view, #where should always be available, immediately.

@rwbarton
Copy link
Collaborator

rwbarton commented Dec 7, 2018

#check is the same kind of transient debugging command as #where yet it doesn't require an import. Sure, #check is a native Lean command while #where is implemented as a user command, but that's only because we didn't have the option to implement it natively in C++ (I'm sure it would have been easier!)

If you were implementing #where as a built-in command, wouldn't you make it always available, without requiring any imports or anything? In that case, why should we not try to approximate that hypothetical situation as closely as we can given the options which are open to us?

@khoek
Copy link
Collaborator Author

khoek commented Dec 11, 2018

It'd be cool to be able to tell VSCode to import some files by default which change how some pieces of the environment work

@jcommelin
Copy link
Member

Wouldn't those new imports trigger massive recompilation?

@khoek
Copy link
Collaborator Author

khoek commented Dec 12, 2018

Sure
But they could be your own personal like debug-imports
Just an idea

tactic/where.lean Outdated Show resolved Hide resolved
tactic/where.lean Outdated Show resolved Hide resolved
The command tells you your current namespace (wherever you write it),
the current `include`s, and the current `variables` which have been
used at least once.
@khoek
Copy link
Collaborator Author

khoek commented Dec 20, 2018

All fixed up now. :)

@digama0 digama0 merged commit caa2076 into leanprover-community:master Dec 20, 2018
@digama0 digama0 deleted the where-cmd branch December 20, 2018 08:40
cipher1024 pushed a commit that referenced this pull request Feb 8, 2019
The command tells you your current namespace (wherever you write it),
the current `include`s, and the current `variables` which have been
used at least once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants