-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Lean 4 survival guide for Lean 3 users
Floris van Doorn edited this page May 25, 2023
·
50 revisions
- Lean 4 cheatsheet: https://github.com/madvorak/lean4-cheatsheet/blob/main/lean-tactics.pdf
- Differences with Lean 3 from the official Lean 4 manual: https://leanprover.github.io/lean4/doc/lean3changes.html
- Function notation changed from
λ x, f xtofun x => f xorλ x => f x. If you import (almost) any file from mathlib, you can also usefun x ↦ f xorλ x ↦ f x. -
Π x : A, P xis not legal anymore. Use∀ x : A, P xor(x : A) → P x - For structures,
{ field1 := value1, field2 := value2, ..parent1, ..parent2 }is now entered as
{ parent1
parent2 with
field1 := value1
field2 := value2 }If you have no additional fields, this becomes { parent1, parent2 with }.
- For infix notation like
+you cannot use(+)or(+ n)anymore, instead use(· + ·)or(· + n). The·(entered with\., not\cdot) is an anonymous lambda expression. The lambda expression will be inserted at the nearest enclosing parentheses (see linked manual page). -
f $ xhas a new notationf <| x(although the old notation still works)
- Enter tactic mode by using
byand then a newline, and indenting the tactics. Example:
example : True := by
trivial- Don't place comma's after tactics, you can go to the next tactic if you write it on a new line (in the same column)
- If you have multiple goals, you can work on the first goal using
· <tactics>instead of{<tactics>}. Note that·is\., not\cdot.
example : (True → p) → q → p ∧ q := by
intros hp hq
constructor
· apply hp
trivial
· exact hq- If you want multiple tactics on the same line, you can use
tac1; tac2(which corresponds totac1, tac2in Lean 3) ortac1 <;> tac2(which corresponds totac1; tac2in Lean 3) - Certain new Lean 4 tactics (like
refine) treat_and?_differently._should be used for implicit arguments that can be figured out by the elaborator.?_should be used for underscores that produce a new subgoal. You can name these subgoals using?myNameinstead of?_.
- Lean 3's
refineis Lean 4'srefine'. - Lean 3's
casesis Lean 4'scases' - Lean 3's
inductionis Lean 4'sinduction' - The square brackets in
rw [h]are mandatory -
splithas been removed. Useconstructor.
(This list is taken from the porting wiki. The instructions there might be more up to date in certain edge cases. There are also some examples on that page)
- Terms of
Props (e.g. proofs, theorem names) are insnake_case. -
Props andTypes (orSort) (inductive types, structures, classes) are inUpperCamelCase. - Functions are named the same way as their return values (e.g. a function of type
A → B → Cis named as though it is a term of typeC). - All other terms of
Types (basically anything else) are inlowerCamelCase. - When something named with
UpperCamelCaseis part of something named withsnake_case, it is referenced inlowerCamelCase. - Acronyms like
LEare written upper-/lowercase as a group, depending on what the first character would be. - Rules 1-6 apply to fields of a structure or constructors of an inductive type in the same way.
There are some rare counter-exceptions to preserve local naming symmetry: e.g., we use Ne rather than NE to follow the example of Eq; outParam has a Sort output but is not UpperCamelCase; etc. Any such counter-exceptions should be discussed on Zulip.
- Named implicit arguments, like
f (x := 3)(see manual) -
f ..for multiple unnamed arguments (see manual) -
#alignis a command to connect Lean 3 names to Lean 4 names, used by mathport. They will be deleted from mathlib at some point after the port is finished.
- Metaprogramming works completely differently in Lean 4, see the Lean 4 Metaprogramming book and Functional Programming in Lean.