Formal proof assistants like Lean, Coq, and Isabelle verify mathematical proofs by checking every logical step against a small, trusted core of rules. You write a theorem and its proof in a formal language; the assistant compiles it into primitive inferences and confirms that each one is valid. This guarantees that if the proof compiles, it is correct—no hidden assumptions, no skipped cases, no hand-waving.
What You’ll Need
- A computer with an internet connection (to install the software).
- Basic familiarity with mathematical notation (e.g., ∀, ∃, →).
- Willingness to learn a new syntax—Lean and Coq are not like Python or C++.
- Optional: a text editor like VS Code with the Lean or Coq extension for a smoother experience.
Step-by-Step
- Choose a proof assistant. For beginners, Lean 4 is popular because of its active community and extensive math library (mathlib). Coq is also a solid choice, especially if you are interested in program extraction. Isabelle/HOL has a more automated style. This guide uses Lean 4 as the example.
- Install Lean 4. Go to the official Lean website (lean-lang.org) and follow the installation instructions for your operating system. The recommended way is using the
elantoolchain manager, which handles versions. After installing, verify by typinglean --versionin a terminal—you should see a version number. - Set up a project. Create a new folder for your project. Inside it, run
lake init my_project(Lake is Lean’s build system). This creates a basic project structure with alakefile.leanand aMyProjectfolder containingBasic.lean. - Open the file in an editor. If you use VS Code, install the “Lean4” extension. Open your project folder, then open
Basic.lean. The extension will connect to the Lean server and show you errors and information as you type. - Write your first theorem. In
Basic.lean, replace the contents with a simple statement liketheorem add_comm_two : 2 + 3 = 3 + 2 := by rfl. Therfltactic proves equality by reflexivity—both sides reduce to the same normal form (5). Save the file. The editor should show no errors, meaning the proof is accepted. - Understand the verification process. When you save, Lean checks the proof term. Behind the scenes, it converts your tactic script into a proof term (a lambda expression) and then type-checks it against the kernel’s rules. The kernel is deliberately tiny and only accepts a few basic inference rules (like modus ponens and universal generalization). If the term type-checks, the theorem is true.
- Use tactics to build proofs. For more complex proofs, you’ll use tactics like
intro,apply,exact, andrw. For example, to prove∀ n : ℕ, n + 0 = n, you writeby intro n; rw [add_zero]. Theintromoves the universal quantifier into the context;rwrewrites using theadd_zerolemma. The editor shows the current goal state, which updates after each tactic. - Check the proof with
#check. After proving a theorem, you can type#check theorem_nameto see its type. This confirms the theorem is in the environment and its statement is what you intended. - Compile the project. Run
lake buildin the terminal. This compiles all files and verifies all proofs. If there are any errors, they will be listed with file and line numbers. - Explore the math library. Instead of proving everything from scratch, you can use lemmas from mathlib. For example,
import Mathlib.Data.Nat.Basicgives you many facts about natural numbers. You can search for lemmas using#checkor the editor’s autocomplete.
Common Mistakes
- Expecting automation for everything. Lean is not a magic oracle; it requires you to spell out the reasoning. Even simple facts may need several steps.
- Ignoring the goal state. The editor shows the current goal after each tactic. Beginners often write tactics without looking at what the goal actually is, leading to errors.
- Using the wrong syntax. Lean uses
:=for definitions andbyfor tactic proofs. Mixing them up causes confusing errors. - Not importing the right modules. Many lemmas are not available by default. You must import the relevant mathlib files.
- Overusing
sorry.sorryis a placeholder that accepts any proof. It’s useful for development, but if you leave it in, the proof is not verified. Always replace it before finalizing.
Tips
- Start with small, concrete theorems (like
2 + 2 = 4) before tackling abstract ones. - Use the editor’s hover feature to see the type of any expression—it’s like a live documentation.
- Read the error messages carefully. They often say exactly what is missing or what type mismatch occurred.
- Look at existing proofs in mathlib or tutorials to see common patterns (e.g., using
omegafor linear arithmetic). - Use
#checkliberally to find the exact name of a lemma you need. - If you get stuck, try to break the proof into smaller lemmas. Each lemma can be tested independently.
Troubleshooting
- Errors like “unknown identifier”: You probably forgot to import a module or misspelled a name. Check the spelling and add the necessary
importat the top. - “type mismatch”: Your tactic produced a term of the wrong type. Look at the goal and the context to see what is expected. Use
exactwith a term that matches the goal, or usechangeto rewrite the goal. - “failed to synthesize”: This often means Lean cannot find a typeclass instance (e.g., for
Decidable). Try addingopen Classicalor import the relevant instance. - Editor not showing anything: Make sure the Lean extension is active and the file is saved. Check the output panel for server errors.
- Build hangs or crashes: Your proof might be looping due to a tactic like
simpthat goes into an infinite loop. Try replacing it with more explicit steps. - Cannot find a lemma: Use
#checkwith a guessed name, or use thelibrary_searchtactic (tryby library_search). It will suggest lemmas that match the goal.
Frequently Asked Questions
- Is a formal proof always correct? Yes, if the proof compiles and the kernel is sound. The kernel is designed to be minimal and audited, so the risk of error is extremely low—much lower than a human-checked proof.
- Do I need to learn a new language? Yes, each assistant has its own syntax. Lean uses a functional programming style, but you don’t need to be a programmer to use it—just willing to learn the syntax.
- Can I verify my own research proofs? Absolutely. Many mathematicians use Lean to verify complex proofs, especially in areas like number theory and algebraic topology. It’s time-consuming but gives absolute certainty.
- What is the difference between Lean and Coq? Both are based on dependent type theory, but they differ in syntax, library, and community. Lean has a more modern feel and a larger math library; Coq has a longer history and is used in software verification.
- How long does it take to learn? Expect a few weeks to become comfortable with basic tactics, and several months to prove nontrivial theorems. The learning curve is steep but rewarding.
- Can I use AI to help write proofs? There are research tools that suggest tactics, but they are not yet reliable for general use. For now, you’ll write proofs manually.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Skip Ryobi and Milwaukee? Harbor Freight Tools to Buy
- Funny Subreddits That Make You Laugh
- Why Retro Gaming Is Still Worth Playing Today
- Sell Your EV Now Before Prices Plummet?
- KDE Connect Vulnerabilities: Are These Versions at Risk?
- Neptune Facts That Will Blow Your Mind