Through Part 4, we used Lean as a purely functional programming language. Now we step onto Lean’s main stage: **theorem proving**.
Save the first Lean block, including its two #check commands, as FirstProofs.lean and run lean FirstProofs.lean. Use the goal snapshots in Section 2 to compare what you see in VS Code’s InfoView.
1. A Proposition Is a Type, and a Proof Is a Value of That Type
Three pieces of notation appear in the display below. First, means “we name the equality claim as ,” and means “this claim is a type belonging to the universe of logical propositions (Prop).” Finally, reads “ (named two_plus_three in the code) is a proof term, a piece of evidence establishing that the proposition holds.”
The equality is the proposition to be proved, and two_plus_three is a proof term inhabiting that proposition. Separate from merely computing a numeric result, we bind a named piece of evidence that the two sides are equal.
Running #check two_plus_three at the bottom of the code below prints the proposition 2 + 3 = 5 in the type position instead of a number type (and checking the proposition itself with #check (2 + 3 = 5) prints 2 + 3 = 5 : Prop). Likewise, #check and_swap displays the constant signature and_swap (P Q : Prop) : P ∧ Q → Q ∧ P, so any subsequent proof can call that name to reuse its evidence.1
In theorem name : proposition := proof, what you want to establish goes after the colon, and its evidence goes after :=. The first theorem supplies rfl directly; the others build the evidence with steps after by. The (P Q : Prop) in and_swap takes two arbitrary propositions so you can reuse the proof with different claims.
theorem two_plus_three : 2 + 3 = 5 := rfl
theorem subst_example (a b : Nat) (h : a = b) : a + 1 = b + 1 := by
rw [h]
theorem imp_refl (P : Prop) : P → P := by
intro hp
exact hp
theorem and_swap (P Q : Prop) : P ∧ Q → Q ∧ P := by
intro h
exact ⟨h.right, h.left⟩
#check two_plus_three
#check and_swapOutput:
two_plus_three : 2 + 3 = 5
and_swap (P Q : Prop) : P ∧ Q → Q ∧ P2. Four Essential Tools: rfl, rw, intro, exact
The first theorem closes immediately by computation alone. The remaining proofs introduce hypotheses or rewrite terms to narrow the goal step by step.
rfl(reflexivity): used when both sides of an equation reduce to the exact same term just by unfolding definitions and computing. Because2 + 3and5both evaluate to5,rflfinishes the proof on the spot.byandrw [h](rewrite): writing:= byenters **tactic mode**. Given an equality hypothesish : a = b, runningrw [h]replacesawithbin the goal to turn it intob + 1 = b + 1and then automatically closes the resulting reflexive goal.intro(introduce hypothesis): when the goal has the implication shapeA → B(“if A then B”), callingintro hplaces the premiseAinto your context as a hypothesis namedhand reduces the remaining goal to provingB.exact(supply exact proof term): when a hypothesis or expression already has the exact type required by the current goal,exact exprcompletes the proof. A proof ofP ∧ Q(“P and Q”) is a pair bundling both pieces of evidence, so passing⟨h.right, h.left⟩(typed as\<and\>in VS Code) withh.right(proof ofQ) first andh.left(proof ofP) second finishes the proof ofQ ∧ P.
Four symbols in the display below show the logical rule behind each tactic. First, the triple-bar equals sign (definitional equality) means that unfolding definitions and computing reduces both sides to the exact same term, which is what rfl checks. Second, the horizontal bar is not division but a logical inference rule: if the condition above the bar holds, then the conclusion below the bar follows.
Here (the capital Greek letter “Gamma”) stands for your current context of variables and hypotheses, and (the turnstile symbol, identical to ⊢ in VS Code InfoView) marks the boundary reading “from the hypotheses on the left, prove the goal on the right.”
The inference rule says that solving the upper judgment establishes the lower judgment. When the goal is , intro h adds evidence to the context and shrinks the goal to , which exact t then fills with a term of matching type.
In contrast, rw [h] uses an existing equality hypothesis to replace the left side of with ; what matters at that step is the equality evidence , not concrete numeric values for and .
In a conjunction (written P ∧ Q in code, meaning “ and ”), the hypothesis h holds a pair consisting of h.left (evidence for ) and h.right (evidence for ).
Because the target of and_swap is the reversed conjunction , we supply the right component first and the left component second inside angle brackets as ⟨h.right, h.left⟩. Watch how the InfoView panel on the right side of VS Code changes as you move your cursor down each line of and_swap.2
For any natural number n, the equation n + 0 = n closes immediately by unfolding the definition of addition. The flipped equation 0 + n = n is also true, of course, but it does not close by rfl alone; in Part 6 we will see why and prove it with simp.3
Whenever you place your cursor between intro h and exact ... in VS Code, InfoView displays your current hypothesis (h : P ∧ Q) and remaining goal (⊢ Q ∧ P) like a live status HUD. In Part 6, we will combine this tactic mode with **mathematical induction (induction)** to prove a universal property of the list-summing function sumList we wrote in Part 4 across all possible input lists.
Footnotes
- Lean Language Reference 4.36.0. Propositions · Propositional Equality. Explains treating proofs of propositions (
Prop) as terms of those propositional types and propositional equality written with=. ↩ - Lean Language Reference 4.36.0. Tactic Reference: Relations (`rfl`) · Tactic Reference: Rewriting (`rw`) · Tactic Reference: Quantifiers (`intro`) · Tactic Reference: Lemmas (`exact`). Defines
rflfor definitional equality,rwfor equality substitution,introfor hypothesis introduction, andexactfor submitting a matching proof term. ↩ - Jeremy Avigad et al. Theorem Proving in Lean 4. Tactics. Includes examples of proving equalities with
rflwhen two terms reduce to the same value by definition. ↩