leanprover / lean4

Lean 4 programming language and theorem prover
https://lean-lang.org
Apache License 2.0
4.62k stars 412 forks source link

RFC: have tactic should emit warning on data #3559

Open fpvandoorn opened 7 months ago

fpvandoorn commented 7 months ago

Explanation

The have tactic is an enormous footgun for new users, since if you write have : <some type> := ... (i.e. not a proposition but data), then the value of the data is forgotten. This is especially problematic if the type is a class, since it can then be used without the user realizing.

Typical example:

example (n m : Nat) : True := by
  have h1 : n + m = m + n := Nat.add_comm n m
  have : Add Nat := inferInstance
  -- many other steps
  have : n + m = m + n := h1 -- error

This is treacherous, since the offending have can occur many lines before the error.

Example where a user was confused about this here.

Proposal


Community Feedback

Overwhelming support on Zulip. Other Zulip threads: 1 2 3

Impact

Add :+1: to issues you consider important. If others benefit from the changes in this proposal being added, please ask them to add :+1: to it.

nomeata commented 7 months ago

Thanks! Sounds reasonable, but I have a few questions:

fpvandoorn commented 7 months ago

Good points!

nomeata commented 7 months ago

I'd suggest logging an error, but not throwing one. You are right it shouldn't get in the way of showing proof states etc, but it also shouldn't be left in the end if it's considered a footgun.

YaelDillies commented 6 months ago

Should this not be implemented as a syntax linter rather than in have itself so that we can disable the linter locally (and therefore bypass the need for the hypothetical have!)?

fpvandoorn commented 6 months ago

I would also be happy with that, as long as the linter also runs and displays errors when there is a sorry, an elaboration error in a later tactic, or an unsolved goal.

kmill commented 6 months ago

Damiano Testa made a linter for this, and you can see the results here: https://github.com/leanprover-community/mathlib4/pull/12157

Zulip discussion

nomeata commented 1 month ago

How is it going with the mathlib linter? Has it been actively useful to users?

I’m still mildly in favor of this idea, but am not convinced that it’s worth the attention and code complexity. How many beginners or users really stumble due to the lack of this?