Zulip Chat Archive

Stream: Lean for teaching

Topic: Lean4 cheatsheet


Elisabeth Bonnevier (Mar 07 2023 at 14:18):

I will be giving an intro to Lean4 at the mathematics department of my university this week. Has anyone made a cheatsheet of tactics for Lean4? I think it would be nice for the participants to have one while we are formalising.

Martin Dvořák (Mar 07 2023 at 14:21):

I have a cheatsheet, but it is useful only if you already know Lean 3 tactics:
https://github.com/madvorak/lean3-tactic-lean4

Martin Dvořák (Mar 07 2023 at 14:22):

You would probably like to have something like "if the goal looks like ABC, you can try XYZ", right?

Elisabeth Bonnevier (Mar 07 2023 at 14:25):

Martin Dvořák said:

You would probably like to have something like "if the goal looks like ABC, you can try XYZ", right?

Yes, preferrably

Martin Dvořák (Mar 07 2023 at 14:26):

I can create a draft, but someone else would have to polish it.

Elisabeth Bonnevier (Mar 07 2023 at 14:26):

I found this: https://leanprover-community.github.io//img/lean-tactics.pdf but it is for Lean 3 right?

Elisabeth Bonnevier (Mar 07 2023 at 14:27):

Martin Dvořák said:

I can create a draft, but someone else would have to polish it.

That would be awesome!

Martin Dvořák (Mar 07 2023 at 14:27):

Elisabeth Bonnevier said:

I found this: https://leanprover-community.github.io//img/lean-tactics.pdf but it is for Lean 3 right?

Yes.

Patrick Massot (Mar 07 2023 at 14:29):

Oh, this is an English version of my old teaching cheatsheet!

Martin Dvořák (Mar 07 2023 at 14:31):

Can you provide a .tex file of that document?

Patrick Massot (Mar 07 2023 at 15:21):

https://github.com/leanprover-community/lftcm2020/blob/master/lean-tactics.tex

Martin Dvořák (Mar 07 2023 at 15:25):

Patrick Massot said:

https://github.com/leanprover-community/lftcm2020/blob/master/lean-tactics.tex

Do you allow me to fork it and create a Lean 4 version?

Patrick Massot (Mar 07 2023 at 15:27):

Sure.

Martin Dvořák (Mar 07 2023 at 15:29):

Actually, does it have to be a fork, or can I simply copy the one document into a new repository I will create? In such a case, how can I acknowledge you as the original author?

Johan Commelin (Mar 07 2023 at 15:32):

Forking seems overkill. Just leave a comment in the TeX with some acknowledgements and a pointer to the original.

Martin Dvořák (Mar 07 2023 at 15:32):

Thanks!

Johan Commelin (Mar 07 2023 at 15:32):

My English copy doesn't even acknowledge Patrick :oops: :see_no_evil:

Martin Dvořák (Mar 07 2023 at 15:33):

So my copy will acknowledge you both, ok?

Martin Dvořák (Mar 07 2023 at 19:13):

Elisabeth Bonnevier said:

Martin Dvořák said:

I can create a draft, but someone else would have to polish it.

That would be awesome!

https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf

Martin Dvořák (Mar 07 2023 at 19:14):

Please PR any improvements you will have!
https://github.com/madvorak/lean4-cheatsheet

Elisabeth Bonnevier (Mar 07 2023 at 19:55):

Martin Dvořák said:

Elisabeth Bonnevier said:

Martin Dvořák said:

I can create a draft, but someone else would have to polish it.

That would be awesome!

https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf

This is great! Thank you so much!

Martin Dvořák (Mar 07 2023 at 19:58):

Will you be importing all of Mathlib4 by default? Or should I add what imports are needed for each tactic?

Elisabeth Bonnevier (Mar 07 2023 at 19:59):

Martin Dvořák said:

Will you be importing all of Mathlib4 by default? Or should I add what imports are needed for each tactic?

It would be nice to have which imports are needed :smile:

Martin Dvořák (Mar 07 2023 at 19:59):

OK, I'll add it!

Martin Dvořák (Mar 07 2023 at 20:55):

Done.
https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf
The document is not more cluttered, but hopefully still readable.

Martin Dvořák (Mar 07 2023 at 20:58):

PS: I also wrote import Mathlib.Tactic.Have even though a tactic have exists outside of Mathlib as well (but with a different syntax).

Mario Carneiro (Mar 07 2023 at 20:59):

I don't think we should be recommending people to use cases' except in porting situations

Mario Carneiro (Mar 07 2023 at 21:00):

there really isn't any reason to prefer it over either cases or rcases

Mario Carneiro (Mar 07 2023 at 21:00):

also a good portion of uses are covered also by have <a, b> := e

Martin Dvořák (Mar 07 2023 at 21:01):

What is the syntax of cases without additional symbol?

Martin Dvořák (Mar 07 2023 at 21:01):

Or should I recommend rcases there?

Mario Carneiro (Mar 07 2023 at 21:02):

what do you mean without additional symbol?

Martin Dvořák (Mar 07 2023 at 21:02):

Mario Carneiro said:

I don't think we should be recommending people to use cases' except in porting situations

It also applies to:
https://github.com/madvorak/lean3-tactic-lean4

Martin Dvořák (Mar 07 2023 at 21:02):

Mario Carneiro said:

what do you mean without additional symbol?

not cases' but cases

Martin Dvořák (Mar 07 2023 at 21:03):

Sorry for cluttering the sentence; I didn't feel like ending the backtick right before the question mark, so I added extra words.

Mario Carneiro (Mar 07 2023 at 21:03):

for unpacking an or it looks like:

cases e with
| inl a => ...
| inr b => ...

Martin Dvořák (Mar 07 2023 at 21:03):

Well, I don't want to use this syntax.

Martin Dvořák (Mar 07 2023 at 21:04):

So cases' and rcases are my options.

Henrik Böving (Mar 07 2023 at 21:04):

Why do you not want to use that syntax? It gives a more structure idea of what is happening doesn't it?

Mario Carneiro (Mar 07 2023 at 21:04):

rcases is closer to cases' if you like the way it looks, it just adds | between the names

Martin Dvořák (Mar 07 2023 at 21:05):

Henrik Böving said:

Why do you not want to use that syntax? It gives a more structure idea of what is happening doesn't it?

I am lazy to write so many lines/symbols.

Mario Carneiro (Mar 07 2023 at 21:05):

you can also use obtain which is the same as rcases but with a syntax closer to have

Mario Carneiro (Mar 07 2023 at 21:05):

obtain a | b := e

Mario Carneiro (Mar 07 2023 at 21:06):

Martin Dvořák said:

Henrik Böving said:

Why do you not want to use that syntax? It gives a more structure idea of what is happening doesn't it?

I am lazy to write so many lines/symbols.

Ideally there will be autocomplete thingies for this

Mario Carneiro (Mar 07 2023 at 21:06):

I don't think that alone is a good argument for changing preferred style guides

Martin Dvořák (Mar 07 2023 at 21:07):

I suppose here
https://github.com/madvorak/lean3-tactic-lean4
I will keep cases' as the other options have different syntax.

Martin Dvořák (Mar 07 2023 at 21:07):

On the other hand, here
https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf
I will change it to whatever you tell me to use instead.

Mario Carneiro (Mar 07 2023 at 21:08):

if you are transcribing lean 3 code then yes you should use cases', and in that case linters and the like will tell you how to fix it

Martin Dvořák (Mar 07 2023 at 21:09):

These two documents (repos) have different use cases. I think I don't have to keep them in sync.

Martin Dvořák (Mar 07 2023 at 21:09):

So just tell me which tactic is desirable for the Lean 4 Cheatsheet.

Mario Carneiro (Mar 07 2023 at 21:09):

I think you should recommend cases in the cheatsheet

Mario Carneiro (Mar 07 2023 at 21:10):

there are several alternative options, I don't know whether you want to elaborate on them on the page

Martin Dvořák (Mar 07 2023 at 21:10):

OK, and the whole line in the column "Appears in hypothesis" should be?

Patrick Massot (Mar 07 2023 at 21:10):

I think that mentioning all those imports is completely useless in such a document. You should mention only one import that bring all those tactics. This document is intended only for beginners and they shouldn't need to worry about minimal imports here.

Martin Dvořák (Mar 07 2023 at 21:10):

I need to describe it concisely.

Martin Dvořák (Mar 07 2023 at 21:11):

Patrick Massot said:

I think that mentioning all those imports is completely useless in such a document. You should mention only one import that bring all those tactics. This document is intended only for beginners and they shouldn't need to worry about minimal imports here.

@Elisabeth Bonnevier wanted to see the imports specifically.

Mario Carneiro (Mar 07 2023 at 21:11):

Martin Dvořák said:

OK, and the whole line in the column "Appears in hypothesis" should be?

what I posted above

cases expr with
| inl new_name => proof
| inr new_name => proof

Mario Carneiro (Mar 07 2023 at 21:12):

yeah, it's not one line. Some tactics are like that

Martin Dvořák (Mar 07 2023 at 21:12):

I see it is not one line. But for the sake of the document, I need to fit it into two lines.

Mario Carneiro (Mar 07 2023 at 21:13):

I think you shouldn't

Patrick Massot (Mar 07 2023 at 21:13):

I am pretty sure she didn't expect to have one import per tactic. Otherwise I can only tell her what I told you: this is a bad idea for beginners.

Martin Dvořák (Mar 07 2023 at 21:13):

Martin Dvořák said:

Will you be importing all of Mathlib4 by default? Or should I add what imports are needed for each tactic?

This is what I asked.

Mario Carneiro (Mar 07 2023 at 21:14):

if you try to compress the syntax for cases onto one line it will be confusing for readers to figure out how to use it correctly in multiline scenarios

Patrick Massot (Mar 07 2023 at 21:15):

Martin, you can use rcases for all this. I agree those Lean4-style branching tactics are much harder to explain.

Mario Carneiro (Mar 07 2023 at 21:15):

(It is technically legal to use cases expr with | inl new_name => proof | inr new_name => proof but this doesn't scale very well)

Martin Dvořák (Mar 07 2023 at 21:15):

Mario Carneiro said:

I think you shouldn't

The document is at its limit. I either have to keep cases' or change it to rcases or (in order to to allow cases as we want) I have to delete something else.

Mario Carneiro (Mar 07 2023 at 21:16):

cases expr with match_arms

Martin Dvořák (Mar 07 2023 at 21:17):

Mario Carneiro said:

cases expr with ...

OK, this goes into the document?

Mario Carneiro (Mar 07 2023 at 21:17):

yes

Martin Dvořák (Mar 07 2023 at 21:17):

What is match_arms in your edited response?

Mario Carneiro (Mar 07 2023 at 21:17):

another metavariable-looking thing

Mario Carneiro (Mar 07 2023 at 21:17):

you aren't going to be able to explain the full syntax in these constraints

Martin Dvořák (Mar 07 2023 at 21:18):

Martin Dvořák said:

Mario Carneiro said:

cases expr with ...

OK, this goes into the document?

Into all three occurences?

Mario Carneiro (Mar 07 2023 at 21:18):

for both 'and' and 'exists' you can use let <x, hx> := e

Mario Carneiro (Mar 07 2023 at 21:19):

or have, it doesn't really matter

Martin Dvořák (Mar 07 2023 at 21:20):

Or I can use rcases for all three and the syntax fits into the lines?

Mario Carneiro (Mar 07 2023 at 21:20):

I don't know, this seems to be mixing concerns about space in the document and best practices for tactic usage in a way that is hard to navigate

Martin Dvořák (Mar 07 2023 at 21:22):

Is rcases bad as a practice?

Mario Carneiro (Mar 07 2023 at 21:22):

I'm not a teacher. For classes you often need to specifically curate tactic lists which are optimized to keep the lexicon small

Mario Carneiro (Mar 07 2023 at 21:23):

rcases isn't bad, as I said there is more than one way to do it

Martin Dvořák (Mar 07 2023 at 21:23):

Well, since this isn't for my class, I will wait for what @Elisabeth Bonnevier prefers.

Mario Carneiro (Mar 07 2023 at 21:23):

but asking for one way to be the 'best' is hard, I expect that to be context dependent

Martin Dvořák (Mar 07 2023 at 21:24):

I can also make more branches of the repo, if more than one person is interested in having a document of this type.

Martin Dvořák (Mar 07 2023 at 21:26):

I will eventually also create a version in Czech (my native language) which might possibly differ in the content as well.

Mario Carneiro (Mar 07 2023 at 21:29):

Here are the ways to perform and elimination I can think of off the top of my head:

-- and elim
· let a, b := e
· have a, b := e
· obtain a, b := e
· rcases e with a, b
· cases' e with a b
· induction' e with a b
· match e with
  | a, b => ...
· cases e with
  | intro a b => ...
· cases e
  next a b => ...
· induction e with
· | intro a b => ...
· induction e
  next a b => ...
· refine e.elim fun a b => ?_

-- and elim + intro
· fun a, b => e
· intro a, b
· rintro a, b

Mario Carneiro (Mar 07 2023 at 21:32):

interestingly, all of those examples work for exists elimination as well

Martin Dvořák (Mar 07 2023 at 21:32):

Some of them require more lines, right?

Mario Carneiro (Mar 07 2023 at 21:33):

what do you mean? As you can see some of them are two lines but none of them require more than is shown

Martin Dvořák (Mar 07 2023 at 21:34):

Oh sorry. I thought your claim was about or elimination. My bad!

Mario Carneiro (Mar 07 2023 at 21:35):

for or elimination the list is a bit different

Kyle Miller (Mar 07 2023 at 21:35):

Here's another for your list Mario:

cases e
next a b => ...

Martin Dvořák (Mar 07 2023 at 21:35):

Eliminating exists in the same way as and sounds believable.

Mario Carneiro (Mar 07 2023 at 21:38):

Here's the list for or elim:

-- or elim
· obtain a | b := e
· rcases e with a | b
· cases' e with a b
· induction' e with a b
· match e with
  | .inl a => ...
  | .inr b => ...
· cases e with
  | inl a => ...
  | inr b => ...
· cases e
  next a => ...
  next b => ...
· induction e with
  | inl a => ...
  | inr b => ...
· induction e
  next a => ...
  next b => ...
· refine e.elim (fun a => ?_) (fun b => ?_)

-- or elim + intro
· fun
  | .inl a => ...
  | .inr b => ...
· intro
  | .inl a => ...
  | .inr b => ...
· rintro (a | b)

Martin Dvořák (Mar 07 2023 at 21:51):

Nice! I saved it.

Elisabeth Bonnevier (Mar 08 2023 at 08:22):

Martin Dvořák said:

Will you be importing all of Mathlib4 by default? Or should I add what imports are needed for each tactic?

How do I import all of Mathlib4 by default? I only know how to import specific files with import statements at the start of the file.

Elisabeth Bonnevier (Mar 08 2023 at 08:27):

Martin Dvořák said:

Patrick Massot said:

I think that mentioning all those imports is completely useless in such a document. You should mention only one import that bring all those tactics. This document is intended only for beginners and they shouldn't need to worry about minimal imports here.

Elisabeth Bonnevier wanted to see the imports specifically.

If it is better to have one import that brings all, then do that :smile:

Elisabeth Bonnevier (Mar 08 2023 at 08:30):

Martin Dvořák said:

Well, since this isn't for my class, I will wait for what Elisabeth Bonnevier prefers.

I am an Agda user and have only learned basic Lean 4 in order to give an introduction to computer formalisation to mathematicians. So I don't know what the best tactics are. You who are more experienced with Lean should decide what would be best to recommend to beginners and I will adapt to that :smile:

Kevin Buzzard (Mar 08 2023 at 08:47):

You can import all of mathlib4 with import Mathlib. For my beginner mathematician students working with lean 3 I start every file with import tactic which imports essentially all the mathlib3 tactics; as far as I know there is no analogue of this yet, and perhaps there should be (I want all the tactics available to students to minimise misunderstanding but I don't necessarily want all the technical results about measure theory, functional analysis and commutative algebra to be available at all times!).

Note that import Mathlib should succeed in just a few seconds. If it doesn't then you need to type lake exe cache get into the command line, and this can be a bit flaky (you have to have a relatively recent curl for example). Lean 4 and the tools we use to play with it are still more of a project in places, rather than a finished product. This is why I still teach with lean 3 (well, that and the fact that I need all of the maths library because I'm doing more advanced mathematics with final year students, and only 40% of the maths library is ported to lean 4 right now)

Martin Dvořák (Mar 08 2023 at 08:58):

After you decide @Elisabeth Bonnevier tell me how you want me to change the document.
(1) Keep or remove the imports?
(2) Keep cases' or change it to cases or to rcases (I personally prefer the latter)?

Elisabeth Bonnevier (Mar 08 2023 at 09:07):

Martin Dvořák said:

After you decide Elisabeth Bonnevier tell me how you want me to change the document.
(1) Keep or remove the imports?
(2) Keep cases' or change it to cases or to rcases (I personally prefer the latter)?

Okay, if it's up to me, I would:
(1) keep the information about where the tactics are imported from
(2) use cases (that's what I've been using so far)

Martin Dvořák (Mar 08 2023 at 11:44):

https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf

Martin Dvořák (Mar 08 2023 at 11:46):

For the sake of consistency, wouldn't it be better to write the following?

have new_name : expr

Martin Dvořák (Mar 08 2023 at 11:49):

Kevin Buzzard said:

as far as I know there is no analogue of this yet, and perhaps there should be

Maybe we should make a file like https://github.com/leanprover-community/mathport/blob/e898d4a1467fae2a9941c37081d8fff5041d617c/Mathport/Syntax/Translate/Tactic/Mathlib.lean but for actual Lean 4 / Std4 / Mathlib4 tactics.

Martin Dvořák (Mar 08 2023 at 15:23):

Now that we have covered @Elisabeth Bonnevier 's use case, what do yall want me to change in the document so it will be useful for other teachers/users?

Martin Dvořák (Mar 29 2023 at 16:18):

What do you want me to change in
https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf
for better usability in other courses or groups?

Martin Dvořák (Mar 30 2023 at 11:08):

I should probably remove the imports. Anything else?

Flo (Florent Schaffhauser) (Apr 01 2023 at 16:03):

@Martin Dvořák I like that you have put the imports: this type of information is not easy to find when you start, so it is useful to have it here, at least in my view (even if you don't need it, you will wonder about it). Thanks for putting the cheatsheet together :+1:

Martin Dvořák (Sep 27 2023 at 20:24):

@Alex J. Best noticed an important problem: simp is missing in the cheatsheet.
I will add it, of course.
However, the cheatsheet is getting too long.
I suggest we should remove a few tactics, too.
My candidates for removal are apply? because it has never ever helped me (but other people say it is useful, so I am confused) and exfalso because students can use by_contra instead (I already removed contrapose for similar reasons).
What do you think?

Alex J. Best (Sep 27 2023 at 20:33):

As I said in the other thread while I like convert_to I don't think it belongs on a cheatsheet one page long necessarily

Damiano Testa (Sep 27 2023 at 20:33):

My personal experience: when learning Lean, I never used convert_to, while I hoped a lot to get help using library_search. For this reason, I would remove convert_to before exact?/apply?. It might be reasonable to join the two in the same entry, though.

As for exfalso, I found it very useful as a beginner.

Alex J. Best (Sep 27 2023 at 20:35):

Having rw appear twice seems a bit excessive to me also . Especially as in lean 4 the brackets are not optional so the syntax should already be quite reminiscent of a list

Alex J. Best (Sep 27 2023 at 20:36):

rw and simp can do the job of unfold most of the time so I'm not sure that needs to be there either

Martin Dvořák (Sep 27 2023 at 20:38):

I love exfalso because it documents the intention very well (and it is also a very cool name).
I love convert_to for it saved my ass so many times.
I agree that simp is a must.
I am quite unsure about one rw versus more of them.
About removing unfold I am quite worried (sure, we teach them simp but not dsimp only or what they would need to do the unfolding only).

Eric Rodriguez (Sep 27 2023 at 20:42):

unfold I think should go

Eric Rodriguez (Sep 27 2023 at 20:43):

I would maybe merge exact?/apply? and maybe even rw? together

Jireh Loreaux (Sep 27 2023 at 20:43):

Of course gcongr should be present, and aesop.

Martin Dvořák (Sep 27 2023 at 20:45):

Jireh Loreaux said:

Of course gcongr should be present, and aesop.

I'll first have to learn myself how to use these tactics! I am adding it to my TODO list.

Martin Dvořák (Sep 27 2023 at 20:48):

Alex J. Best said:

Having rw appear twice seems a bit excessive to me also . Especially as in lean 4 the brackets are not optional so the syntax should already be quite reminiscent of a list

Would you keep the first rw entry only?

Martin Dvořák (Sep 27 2023 at 20:52):

Alex J. Best said:

As I said in the other thread while I like convert_to I don't think it belongs on a cheatsheet one page long necessarily

I am a bit worried that if I show only convert then students might think that convert has the effect of convert_to instead.
The comparison makes you kinda understand both tactics, I believe.

Damiano Testa (Sep 27 2023 at 20:56):

I'm still not completely sold on the "one page only" restriction.

Eric Rodriguez (Sep 27 2023 at 21:06):

You could do one page for the basic stuff, and then two pages with extra stuff

Martin Dvořák (Sep 27 2023 at 21:07):

I can make a markdown version that is not restricted by the page (A4) size, which can contain more tactics than the LaTeX version (pdf). Still, I believe that for the document to be useful as a cheatsheet, it shouldn't contain much more stuff than it already does.

Frédéric Dupuis (Sep 27 2023 at 23:38):

I think refine should be there as well, I find this tactic very undertaught given how useful it is.

Floris van Doorn (Sep 28 2023 at 12:46):

I've also never used convert_to. Are all these tactics available in Mathlib.Tactic.Core? If so, I would just write that (once).

Antoine Chambert-Loir (Sep 30 2023 at 15:50):

In some contexts, apply? is very efficient.

Antoine Chambert-Loir (Sep 30 2023 at 15:52):

Maybe, tactics could be grouped according to how they are used.
simp, rw, are close, and so are apply, refine, convert...

Martin Dvořák (Sep 30 2023 at 17:52):

Antoine Chambert-Loir said:

In some contexts, apply? is very efficient.

Can you please give me an example? I have never benefited from what apply? gave me.

I don't doubt your claim. I just want to understand where or when apply? is useful.

Alex J. Best (Sep 30 2023 at 19:21):

I would say https://github.com/alexjbest/lorentz/blob/master/Lorentz/MondayDemo.lean#L70 is a situation where iterating apply? is the easiest way to construct the proof (ok the continuity tactic also exists, but in some similar situation where there is no existing tactic apply? is a life saver when you don't know the names of all the lemmas by heart)

Martin Dvořák (Oct 02 2023 at 15:01):

The first two steps were good!
Subsequently, I couldn't persuade apply? to give me Continuous.comp continuous_sin.
Should I introduce a different intermediate goal in order to get there?
I tried the following but failed...

The goal is:

Continuous fun b => sin (b ^ 2)

I introduced a simpler goal, for which exact? succeeded:

have : Continuous fun x => sin x
· exact continuous_sin

Unfortunately, I cannot extrapolate from this to the original goal. If I simple write

apply continuous_sin

as the next step, it fails. How did you obtain a hint towards

apply Continuous.comp continuous_sin

in your proof?

Alex J. Best (Oct 02 2023 at 15:36):

I just did it again and indeed it likes Continuous.comp' more, I ended up with

  refine Continuous.add ?hf ?hg
  refine Continuous.pow ?hf.h 3
  refine Continuous.comp' ?hf.h.hg ?hf.h.hf
  exact continuous_sin
  exact continuous_pow 2
  refine Continuous.comp' ?hg.hg ?hg.hf
  exact continuous_cos
  exact continuous_mul_left 5

Alex J. Best (Oct 02 2023 at 15:37):

The proof I wrote was hand written (note it uses apply not refine) but the tactic gets roughly the same proof

Martin Dvořák (Oct 02 2023 at 15:42):

Unfortunately Continuous.comp' is pretty low in the output and it doesn't strike me as useful when reading only its name.

Patrick Massot (Oct 02 2023 at 15:43):

You should also read that remaining goals, that' s by far the most important piece of information.

Patrick Massot (Oct 02 2023 at 15:46):

Note also the case of continuity goals is pretty special because there are so many lemmas whose conclusion is a continuity statement and which potentially apply everywhere. For instance, in the case of continuous functions between metric spaces, apply? will always suggest to prove the function is uniformly continuous, or prove it is sequentially continuous, or prove it is continuous at every point, etc. It would be nice if apply? could have heuristic to push these down the list, but this isn' t so obvious (but you can still hope @Scott Morrison will prove me wrong).

Alex J. Best (Oct 02 2023 at 15:46):

Martin Dvořák said:

Unfortunately Continuous.comp' is pretty low in the output and it doesn't strike me as useful when reading only its name.

Its certainly true that the ordering could be improved! but that is a nontrivial problem, as it verges on automated theorem proving :wink: a good algorithm for ordering that always puts what your next step will be at the top very quickly leads to a decent auto proof tactic

Alex J. Best (Oct 02 2023 at 15:47):

Thats not to say we shouldn't improve it with good heuristics, I'd still think there is some low hanging fruit there

Kevin Buzzard (Oct 02 2023 at 15:48):

How about ordering by some measure of "total number of characters in all remaining goals" for a start?

Martin Dvořák (Oct 02 2023 at 15:51):

It took me pretty long to realize that Continuous.comp' was about function composition.
Clearly, I'm the problem here.

Antoine Chambert-Loir (Oct 02 2023 at 17:44):

Martin Dvořák said:

Antoine Chambert-Loir said:

In some contexts, apply? is very efficient.

Can you please give me an example? I have never benefited from what apply? gave me.

I don't doubt your claim. I just want to understand where or when apply? is useful.

Working about permutations, their cycle types, their support, etc there are many equivalent ways of passing from one formulation to another, equivalences which mathlib knows and — at least I felt it so — finds by itself.

Martin Dvořák (Oct 02 2023 at 17:55):

I'm getting a feeling that you guys use apply? in situations where I use convert_to followed by exact? usually.
I think that, if you have the proof idea already in your head, convert_to leads to a pretty consistent workflow.
However, I am willing to believe that apply? is easier for most Lean users.

Alex J. Best (Oct 02 2023 at 21:17):

Kevin Buzzard said:

How about ordering by some measure of "total number of characters in all remaining goals" for a start?

Won't that just put False.elim at the top every time or something like that?

Patrick Massot (Oct 02 2023 at 21:22):

Yes, that's part of why this question isn't easy.

Miguel Marco (Oct 02 2023 at 21:41):

Maybe it is because I am too used to lean3, but I don't use the syntax

cases expr with
| intro a b => ...

in lean4.

Instead, I use the cases' tactic, which works like the casesone did in lean3.

Martin Dvořák (Oct 03 2023 at 08:48):

I was using cases' until very recently, too.
However, with the new quick-fix lightbulb in the VS Code lean4 plugin, I find cases easy to use.
Now I like cases better.

Kevin Buzzard (Oct 03 2023 at 09:47):

Yeah the lightbulb was crucial for me too

Martin Dvořák (Oct 04 2023 at 08:54):

Martin Dvořák said:

I can make a markdown version that is not restricted by the page (A4) size, which can contain more tactics than the LaTeX version (pdf). Still, I believe that for the document to be useful as a cheatsheet, it shouldn't contain much more stuff than it already does.

I started turning the original table into a markdown version:
https://github.com/madvorak/lean4-tactics/tree/main
Unfortunately, GitHub alternates row background between white and gray.
The latter makes the boundary between code and text hard to see.
Can I set the background of all rows to white?
If not, does it make sense to do it (the longer version of the cheatsheet) as a markdown document on GitHub?

Damiano Testa (Oct 04 2023 at 09:49):

I think that it does make sense to have the md version. I use dark mode, and I can barely notice the parity difference in cell coloring, but the code still stands out.

Also, I think that avoiding the parity with markdown may not be possible. If you really wanted, you might write the html equivalent and that may not get autoformatted, but I am not even sure about that.

Martin Dvořák (Oct 04 2023 at 09:53):

It looks OK in the dark mode (both the high-contrast version and the normal one).
Do you recommend that I continue with the formatting I started with?

Damiano Testa (Oct 04 2023 at 10:04):

Martin, if you are happy to go on with the webpage, I will certainly link it from the webpage for my module! I already have a link to your cheatsheet!

Martin Dvořák (Oct 04 2023 at 10:50):

Imma finish the markdown version in a few days.
Today, I was using to spend some time when Mathlib was building.

Julian Berman (Oct 04 2023 at 13:13):

Martin Dvořák said:

Can I set the background of all rows to white?

(You cannot FWIW, at least not while using GitHub Markdown syntax for it and not pure HTML -- but if you're satanic you can hack around it by inserting empty rows in your table -- i.e. https://gist.github.com/Julian/1e06883d37d0448685f3cf8417469e0a

Martin Dvořák (Oct 04 2023 at 13:18):

Haha, nice! And it works even better without the <tr></tr> at the very end.

Martin Dvořák (Oct 04 2023 at 13:24):

Damiano Testa said:

Martin, if you are happy to go on with the webpage, I will certainly link it from the webpage for my module! I already have a link to your cheatsheet!

My part is done. Link here:
https://github.com/madvorak/lean4-tactics

There are four rows with TODOs now.
They correspond to tactics that I don't personally use, but people wanted them in the cheatsheet.
Somebody will certainly fill them in.

Antoine Chambert-Loir (Oct 05 2023 at 09:09):

This page will be very useful (has already been).
Some minor comments :

  • for some tactics, such as rw, the (at expr) indication suggest that parentheses have to be inserted, and it seems to be the part “in parentheses” which is optional, while the part “in brackets” has to be there!

  • sometimes expr means one name, sometimes it means a list.

  • I wouldn't insist on new_name: it can be an already used name, although that makes the preceding uses of that name unusable.

  • simp? is useful, and almost unavoidable if one wishes not to leave bare simp commands in Lean code.

Martin Dvořák (Oct 05 2023 at 14:02):

Thanks a lot for noticing that parentheses vs brackets were messed up!!

I updated the document according to your suggestions (but new_name stayed there).

Martin Dvořák (Oct 05 2023 at 14:04):

@Patrick Massot
@Johan Commelin
@Kevin Buzzard
Do you [any subset of you] want credits for creating the previous-previous document? If so, in which form?

Patrick Massot (Oct 05 2023 at 14:05):

I think the correct action is to make this into a community owned resource, without attribution.

Martin Dvořák (Oct 05 2023 at 14:08):

Should it be written in the (Un)license file?

Patrick Massot (Oct 05 2023 at 14:14):

I think it should simply be put on the community website.

Martin Dvořák (Oct 05 2023 at 14:21):

Patrick Massot said:

I think it should simply be put on the community website.

If you are the community website maintainer, feel free to fork it or hardcopy it to the community website; after all, I unlicensed the document.

Nevertheless, I will keep the original version on my GitHub (in case that I don't like new redesigns, I will simply keep distributing my version to my students).

Patrick Massot (Oct 05 2023 at 14:26):

Of course you can distribute whatever you want to your students.

Martin Dvořák (Oct 05 2023 at 14:28):

Also this table
https://github.com/madvorak/lean3-tactic-lean4
could be hardcopied into
https://github.com/leanprover-community/mathlib4/wiki/Lean-4-survival-guide-for-Lean-3-users
(Lean 4 survival guide for Lean 3 users).

Somo S. (Oct 06 2023 at 08:39):

Martin Dvořák said:

Also this table
https://github.com/madvorak/lean3-tactic-lean4
could be hardcopied into
https://github.com/leanprover-community/mathlib4/wiki/Lean-4-survival-guide-for-Lean-3-users
(Lean 4 survival guide for Lean 3 users).

If there is a more general community article that is something like "Lean 4 survival guide for Lean 3 users", then that would be a more appropriate place. Otherwise new people like me who never got to use lean3 and generally avoid guides that assume I used lean3 before, would never stumble on such a useful cheatsheet.

Martin Dvořák (Oct 06 2023 at 08:51):

If you never used Lean 3, then https://github.com/madvorak/lean3-tactic-lean4 is not for you.

Somo S. (Oct 06 2023 at 08:54):

oh sorry, my fault .. should have read the discussion more carefully.. i though that was a link to your tactic cheatsheet .. which i found very useful despite not having learned lean3

Somo S. (Oct 06 2023 at 08:55):

speaking of which anyway, is the tactic cheatsheet going to be published somewhere on the Community website?

Martin Dvořák (Oct 06 2023 at 09:27):

Somo S. said:

oh sorry, my fault .. should have read the discussion more carefully.. i though that was a link to your tactic cheatsheet .. which i found very useful despite not having learned lean3

Yeah, I have been maintaining more than one sheet. Different users have different needs.

Martin Dvořák (Oct 06 2023 at 09:28):

Somo S. said:

speaking of which anyway, is the tactic cheatsheet going to be published somewhere on the Community website?

I think so; @Patrick Massot is in favor of putting it there.

Patrick Massot (Oct 06 2023 at 12:46):

Yes, I didn't have time for that yet, but anyone can open a PR to the website repository.

Floris van Doorn (Oct 16 2023 at 22:18):

There were some design decisions that I didn't like in Martin's cheat sheet, so I made my own for my Lean course: https://github.com/fpvandoorn/LeanCourse23/blob/master/lean-tactics.pdf
Martin's version was of course very helpful to get started.

Some changes:

  • don't give import suggestions (except for a "please import Mathlib.Tactic" at the top)
  • prefer rcases over cases
  • simplify the syntax by suggesting names of hypotheses and removing parentheses
  • different selection of tactics
  • give more variants of tactics

Martin Dvořák (Oct 17 2023 at 06:26):

I like your version! It looks very clean and clear.

Scott Morrison (Oct 17 2023 at 07:00):

@David Thrane Christiansen just making sure you've seen this. No action necessary. :-)

Scott Morrison (Oct 17 2023 at 07:01):

Relatedly Terry Tao has just recently made his own cheatsheet at https://docs.google.com/spreadsheets/d/1Gsn5al4hlpNc_xKoXdU6XGmMyLiX4q-LFesFVsMlANo/edit#gid=0

Utensil Song (Oct 17 2023 at 07:20):

Tao's version is well organized, it can even support a simple tactic possibly named hint with an optional query to match goal/selected hypotheses and optional fuzzy matching the "mathematical English".

Scott Morrison (Oct 17 2023 at 07:24):

It's a pity we never ported hint from mathlib3. I liked it!

Utensil Song (Oct 17 2023 at 07:37):

Simple, extensible and helpful, like a triage for tactics

Scott Morrison (Oct 17 2023 at 07:42):

I'm hoping std4#215 will eventually be merged. It is a great common UI for suggestion tactics.

Mac Malone (Oct 17 2023 at 17:37):

I really like how Tao named the notion of terms used in proofs as "reasons" (e.g. for exact and have).

Somo S. (Oct 18 2023 at 03:02):

@Floris van Doorn said:

There were some design decisions that I didn't like in Martin's cheat sheet, so I made my own for my Lean course: https://github.com/fpvandoorn/LeanCourse23/blob/master/lean-tactics.pdf
Martin's version was of course very helpful to get started.

Some changes:

  • don't give import suggestions (except for a "please import Mathlib.Tactic" at the top)

nice, though personally, i would prefer to know exactly where Mathlib.Tactic or Std.Tactic is required ahead of time. I think maybe it would be better if you indicated that with a less noisy annotation at the end of each description wherever that applies e.g. "This tactic does xyz[1]" (where [1] is a footnote at the bottom that says "requires Std.Tactic").

Alternatively, there could there could be a hyperlink to the definition of such a tactic, by following that link one could know ahead of time, if they need to import extra things

Utensil Song (Oct 18 2023 at 03:29):

"don't give import suggestions" helps to reduce visual noise for a cheat sheet, a general tip can be given at the top or in footnote to find where the tactic is, either by hover and click into the definition, or look for the name under Mathlib.Tactic source or doc.

Just as @Somo S. suggested, one might need to know if a tactic can be used without Mathlib (maybe for minimal deps in teaching), a small icon with hyperlink (to its definition in Mathlib or Std/Lean) might suffice for this purpose. But I'm afraid there are many subtle differences between the tactics with and without Mathlib, which might create more confusion as opposed to the intention of simplifying things, so one is better just get familiarized with the Mathlib flavor.

Somo S. (Oct 18 2023 at 03:39):

anyway, I think what can only end up ultimately emerging, is different kinds of cheatsheets for different kinds of usecases or pedagogies. In my work, I find it important to try to avoid mathlib or std wherever its not necessary or to favor clarity (of what lean is doing) over brevity.

Johan Commelin (Oct 18 2023 at 04:30):

I think that a "please import Mathlib.Tactic" is totally fine for a cheatsheet for beginners. I understand that other use cases are perfectly valid, but I wouldn't clutter a cheat sheet for maths students such info.

Martin Dvořák (Oct 18 2023 at 06:11):

import Mathlib.Tactic is totally fine for a cheatsheet for beginners

... assuming lake exe cache get works on their computer!

Kevin Buzzard (Oct 18 2023 at 06:22):

If lake exe cache get doesn't work on someone's computer then they should stop what they're doing and get it working, not read a tactic cheatsheet

Mario Carneiro (Oct 18 2023 at 06:26):

agreed, lake exe cache get not working is similar to getting the unknown package 'Mathlib' error: you should debug the issue because you don't yet have a working install

Mario Carneiro (Oct 18 2023 at 06:28):

We should have a troubleshooting page for common installation issues but it would be weird to have such information in a tactic cheatsheet

Shreyas Srinivas (Oct 18 2023 at 09:07):

I already created a PR for this

Shreyas Srinivas (Oct 18 2023 at 09:07):

For the trouble shooting page

Shreyas Srinivas (Oct 18 2023 at 09:07):

On the community website this is PR 370

Somo S. (Oct 19 2023 at 04:30):

@Johan Commelin said:

I wouldn't clutter a cheat sheet for maths students such info.

I thought its a general cheatsheet for anyone learning lean not just for math students? Again maybe there should be one thats for everyone or multiple that emphasize different usecases/applications.

Johan Commelin (Oct 19 2023 at 04:31):

The one by Floris was designed for a course for math students.

Martin Dvořák (Nov 22 2023 at 07:46):

Can somebody (who understands given tactics) please fill those TODOs?
https://github.com/madvorak/lean4-tactics
After that, the cheatsheet should be ready to go to the Lean community website.

Floris van Doorn (Nov 22 2023 at 08:59):

Most of them have an explanation here: https://github.com/fpvandoorn/LeanCourse23/blob/master/lean-tactics.pdf

Martin Dvořák (Dec 12 2023 at 17:37):

Catalan version:
https://github.com/madvorak/lean4-tactics/blob/main/README-CAT.md
Czech version:
https://github.com/madvorak/lean4-tactics/blob/main/README-CZE.md


Last updated: Dec 20 2023 at 11:08 UTC