About this notebook

Companion to Lecture 1: Propositions as types. Every cell below was executed against the course’s pinned Lean 4 + Mathlib (see lean/lean-toolchain); each Lean result is exactly what Lean printed. Download the notebook (.ipynb)

Lecture 1 claims that a proof is a program and a statement is its type. This notebook lets you poke at that claim until it feels obvious. You will:

  1. ask Lean for the types of terms, and see a proposition sitting among ordinary types;
  2. write one proof per logical connective, then break proofs on purpose and read the errors;
  3. draw proof terms as trees, and check that a tactic proof builds the same tree as a term proof;
  4. look at the fully explicit term the kernel checks;
  5. draw a small dependency graph and audit axioms with #print axioms.

Each grey %%lean cell is sent to a real Lean process. The coloured box under it is what Lean answered: green means accepted, gold means accepted with a sorry left open, and a red-brown “rejected, as intended” box is a cell that is meant to fail.

import leanmagic
from leanmagic import viz
 
leanmagic.setup("import LeanFV.Tools.ExprJson")
Lean 4.35.0 ready; header `import LeanFV.Tools.ExprJson` elaborated.

The header import LeanFV.Tools.ExprJson loads plain Lean plus two small commands written for this course, #expr_json and #deps_json, which the pictures below use. No Mathlib: everything here works in core Lean.

1. Terms have types, and so do statements

#check t prints t : T, the type Lean infers for t. Look at the last two lines: a statement is a term whose type is Prop, and Prop itself has a type.

#check 3
#check fun (n : Nat) => n + 1
#check Nat → Nat
#check 2 + 2 = 4
#check Prop

✓ Lean accepted this

3 : Nat
fun n ↦ n + 1 : Nat → Nat
Nat → Nat : Type
2 + 2 = 4 : Prop
Prop : Type

A theorem gives a name to a term of a statement’s type. rfl has type a = a; Lean accepts it here because 2 + 2 computes to 4.

theorem two_add_two : 2 + 2 = 4 := rfl
#check two_add_two

✓ Lean accepted this

two_add_two : 2 + 2 = 4

Now ask for something false. The same term rfl cannot have type 2 + 2 = 5, because the two sides compute to different numerals, so the type check fails. A false statement has no proof because no term has that type. Lean reports two errors. The first mentions ?m, a placeholder for the value rfl is about, which Lean never managed to fill in; the second gives the real reason.

This cell is meant to fail:

theorem two_add_two_wrong : 2 + 2 = 5 := rfl

✗ rejected, as intended

error, line 1Type mismatch
  rfl
has type
  ?m.16 = ?m.16
but is expected to have type
  2 + 2 = 5
error, line 1Not a definitional equality: the left-hand side
  2 + 2
is not definitionally equal to the right-hand side
  5

2. One proof per connective

Each declaration below is a row of the Curry–Howard dictionary from Lecture 1:

  logic        proof term                     using it
  P → Q        fun hp => ...                  apply:   hpq hp
  P ∧ Q        ⟨hp, hq⟩                       project: h.1  h.2
  P ∨ Q        Or.inl hp  /  Or.inr hq        match on the two cases
  ¬P           fun hp => (proof of False)     apply it to a proof of P
  ∀ x, P x     fun x => ...                   apply it to a value
  ∃ x, P x     ⟨witness, proof⟩               match / obtain

A green box with “(no messages)” is the whole success story: Lean type-checked every term.

theorem modus_ponens (p q : Prop) (hp : p) (hpq : p → q) : q :=
  hpq hp
 
theorem swap_and (p q : Prop) (h : p ∧ q) : q ∧ p :=
  ⟨h.2, h.1⟩
 
theorem swap_or (p q : Prop) (h : p ∨ q) : q ∨ p :=
  match h with
  | Or.inl hp => Or.inr hp
  | Or.inr hq => Or.inl hq
 
theorem not_both (p : Prop) : ¬(p ∧ ¬p) :=
  fun h => h.2 h.1
 
theorem all_refl : ∀ n : Nat, n = n :=
  fun n => (rfl : n = n)
 
theorem seven_sq : ∃ n : Nat, n * n = 49 :=
  ⟨7, rfl⟩

✓ Lean accepted this (no messages)

Wrong proofs are programs of the wrong type, and Lean’s error says which types disagree. Knowing p ∨ q is not knowing p:

This cell is meant to fail:

example (p q : Prop) (h : p ∨ q) : p := h

✗ rejected, as intended

error, line 1Type mismatch
  h
has type
  p ∨ q
but is expected to have type
  p

Logically equivalent is not the same type either. q ∧ p and p ∧ q say the same thing, but they are different types, so h alone does not fit; you must rebuild the pair.

This cell is meant to fail:

example (p q : Prop) (h : q ∧ p) : p ∧ q := h

✗ rejected, as intended

error, line 1Type mismatch
  h
has type
  q ∧ p
but is expected to have type
  p ∧ q

A proof with a hole in it is still accepted, with a warning. Lean reports what the hole must prove: the goal, written ⊢ p under the hypotheses you have. This is how you will work most of the time: write the shape of the proof, leave sorry, read the goal.

Your turn

Replace the sorry with a term. The goal box tells you its type.

theorem left_of_and (p q : Prop) : p ∧ q → p :=
  fun h => sorry

◐ accepted with sorry: goals left open

goal at line 2p q : Prop
h : p ∧ q
⊢ p

3. Proof terms as pictures

viz.term_tree(name) asks Lean for the proof term of name and draws it. Every box is subterm : its type, so each box is a proof of the proposition after its colon, built from the boxes below it. That is the natural-deduction derivation from Lecture 1, drawn with the conclusion at the top. The edge labels name the argument each child fills.

fig = viz.term_tree("swap_and")
The proof term of swap_and. And.intro pairs the two projections And.right h and And.left h out of the hypothesis h : p ∧ q.

The proof term of swap_and. And.intro pairs the two projections And.right h and And.left h out of the hypothesis h : p ∧ q.

Now the same theorem with tactics. constructor asks for an And.intro, leaving one goal per component, and each exact fills one. #print shows the term the tactics wrote.

theorem swap_and' (p q : Prop) (h : p ∧ q) : q ∧ p := by
  constructor
  · exact h.2
  · exact h.1
 
#print swap_and'

✓ Lean accepted this

theorem swap_and' : ∀ (p q : Prop), p ∧ q → q ∧ p :=
fun p q h ↦ ⟨h.right, h.left⟩

Is it really the same term? Compare the two trees as data, not by eye:

same = viz.term_json("swap_and")["tree"] == viz.term_json("swap_and'")["tree"]
print("swap_and and swap_and' have identical proof terms:", same)
swap_and and swap_and' have identical proof terms: True

4. What the kernel sees

The printer hides arguments that Lean can infer, such as the p and q in And.intro. The kernel sees none of that convenience. pp.explicit prints the term with every argument written out, which is what the kernel actually checks.

set_option pp.explicit true in
#print swap_and

✓ Lean accepted this

theorem swap_and : ∀ (p q : Prop), And p q → And q p :=
fun p q h ↦ @And.intro q p (@And.right p q h) (@And.left p q h)

The elaborator filled in q and p for And.intro, and p q for each projection. If it had filled them in wrongly, the kernel would reject the term: the elaborator is not trusted, only the kernel is.

5. Dependencies and axioms

Real developments are built from lemmas. Here is a tiny one, started --fresh so that only these four declarations exist. viz.deps_graph() draws who uses whom; a large project draws the same kind of graph as its blueprint (Lecture 5).

theorem get_left (p q : Prop) (h : p ∧ q) : p := h.1
 
theorem get_right (p q : Prop) (h : p ∧ q) : q := h.2
 
theorem my_swap (p q : Prop) (h : p ∧ q) : q ∧ p :=
  ⟨get_right p q h, get_left p q h⟩
 
theorem swap_twice (p q : Prop) (h : p ∧ q) : p ∧ q :=
  my_swap q p (my_swap p q h)

✓ Lean accepted this (no messages)

fig = viz.deps_graph()
Dependency graph of the four declarations: the projections feed my_swap, which swap_twice uses twice.

Dependency graph of the four declarations: the projections feed my_swap, which swap_twice uses twice.

Everything so far was constructive: it used only the rules in the dictionary. Excluded middle is not among them, so Lean provides it through axioms. #print axioms lists exactly what a theorem rests on, and it is the first thing to run when you audit someone’s proof.

theorem em_example (p : Prop) : p ∨ ¬p := Classical.em p
 
#print axioms swap_twice
#print axioms em_example

✓ Lean accepted this

'swap_twice' does not depend on any axioms
'em_example' depends on axioms: [propext, Classical.choice, Quot.sound]

propext, Classical.choice and Quot.sound are Lean’s three standard axioms. In an audit, anything else is the thing to ask about: sorryAx means an unfinished proof, and a project’s own axiom is an assumption the project is asking you to accept.

Try this

  • Prove (p → q) → (q → r) → p → r as a term, draw it with viz.term_tree, and find the two function applications in the picture.
  • Add a cell at the end of section 3 with viz.term_tree("swap_or"). The match became a call to an auxiliary function; Lecture 4 explains what that function is built from.
  • In section 2, run #print axioms on two_add_two and on all_refl. Which axioms, if any, does computing with rfl need? (Sections 1 to 4 are gone once the --fresh cell of section 5 has run, so put these cells before it.)
  • Prove ¬(p ∨ q) → ¬p ∧ ¬q, add it to the --fresh cell of section 5, use it in a new theorem, and redraw the dependency graph.