Lean 4 theorem prover for formal verification. Use when: formal theorem proving, mathematical verification, proof search, type theory exploration. NOT for: numerical computation (use scipy), symbolic math (use sympy), statistical analysis (use statsmodels).
Formal theorem proving, mathematical verification, proof search, and type theory exploration.
Use when: formal theorem proving, mathematical verification, proof search, type-theoretic reasoning, formalized mathematics with Mathlib, program verification.
NOT for: numerical computation (use scipy/numpy), symbolic algebra or calculus (use sympy), statistical analysis (use statsmodels), quick calculations.
If lean is not available, install via elan (the Lean version manager):
curl https://elan.lean-lang.org/install.sh -sSf | sh
# or on macOS:
brew install elan-init
elan default leanprover/lean4:stable
-- Simple proposition proof
theorem my_first_theorem : 1 + 1 = 2 := by
rfl
-- Implication
theorem modus_ponens (P Q : Prop) (hp : P) (hpq : P → Q) : Q := by
apply hpq
exact hp
-- Universal quantifier
theorem add_comm_example : ∀ (a b : Nat), a + b = b + a := by
intro a b
omega
-- intro: introduce hypotheses / universally quantified variables
-- apply: apply a function or lemma to the goal
-- exact: provide the exact proof term
-- rfl: reflexivity (proves a = a or definitional equalities)
-- simp: simplification using simp lemmas
-- ring: prove equalities in commutative rings
-- omega: decide linear arithmetic over Nat and Int
-- linarith: linear arithmetic reasoning with hypotheses
-- cases / rcases: case split on inductive types
-- induction: structural induction
-- constructor: prove a conjunction or existential
-- contradiction: close goal from contradictory hypotheses
-- Example combining tactics
theorem example_proof (n : Nat) (h : n > 0) : n + n > n := by
linarith
theorem ring_example (a b : Int) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
ring
# Create a new Lean 4 project
lake new my_project
cd my_project
# Project structure:
# my_project/
# lakefile.lean -- build config
# lean-toolchain -- Lean version
# MyProject/
# Basic.lean -- source files
-- lakefile.lean (with Mathlib dependency)
import Lake
open Lake DSL
package my_project where
leanOptions := #[⟨`autoImplicit, false⟩]
require mathlib from git
"https://github.com/leanprover-community/mathlib4" @ "master"
@[default_target]
lean_lib MyProject where
srcDir := "MyProject"
lake update # fetch dependencies
lake build # build the project
import Mathlib.Tactic
import Mathlib.Data.Nat.Basic
import Mathlib.Data.List.Basic
-- Mathlib provides thousands of lemmas and tactics
theorem list_length_append (l1 l2 : List α) :
(l1 ++ l2).length = l1.length + l2.length := by
simp [List.length_append]
-- norm_num: evaluate numerical expressions
example : (7 : ℤ) ∣ 42 := by norm_num
-- positivity: prove positivity/nonnegativity
example (x : ℝ) : 0 ≤ x ^ 2 + 1 := by positivity
-- polyrith: polynomial arithmetic (requires Mathlib)
-- gcongr: generalized congruence
-- field_simp: clear denominators in field expressions
# Run a single Lean file
lean MyFile.lean
# Check a file and print messages
lean --run MyFile.lean
# Interactive: use VS Code with lean4 extension for goal state
# Or use lean4 language server directly
-- Use #check, #eval, #print for exploration
#check Nat.add_comm -- view type signature
#eval 2 ^ 10 -- evaluate expressions
#print Nat.rec -- print definition
autoImplicit to false in lakefile to catch typos in variable names.sorry as a placeholder while developing proofs, then eliminate all before finalizing.by block and use ? suffix tactics (e.g., simp?, exact?) to discover lemmas.#check liberally to inspect types and available lemmas.lake exe cache get to download prebuilt oleans and speed up builds.omega for natural/integer arithmetic goals; ring for polynomial identities; linarith when hypotheses are needed.