Exploring Lean 4

Zilin Jiang
Arizona State University
November 15, 2024

Lean 4 is a dependently typed programming language and interactive theorem prover.

Hello, World!

#eval "Hello, World!"
#check 1
#check true
def x : Int := 10
def double (x : Int) := 2 * x
#check double
def projection (x y : Int) := x
#check projection

What is a Type?

In Lean 4, types classify terms (values).

Types themselves are organized in a hierarchy of universes.

def f (α β : Sort u) (a : α) (b : β) : α := a
#eval f Nat String 1 "hello"

Why Insist on Termination?

Ensures all functions in Lean are total (defined for all inputs).

Aligns with Lean's foundations in constructive mathematics.

#eval 1/2
#eval 1/0
def quicksort : List Int → List Int
| [] => []
| x :: xs =>
  let smaller := xs.filter (fun y => y ≤ x)
  let larger := xs.filter (fun y => y > x)
  quicksort smaller ++ [x] ++ quicksort larger
#eval! quicksort [3,1,4,1,5,9,2,6,5]

What is a Proposition?

A proposition is a type in Lean, defined in the universe Prop.

#check 1 + 1 = 2

What is a Proof?

A proof is a value (or term) that inhabits a proposition.

Proving a proposition means constructing a term of the corresponding type.

example : double 4 = 8 := rfl
#check True.intro
theorem trivial' : True := True.intro
theorem implication (P Q : Prop) : P → Q → Q := fun p q => q
def prop := 2 + 2 = 4
#check prop
theorem easy : prop := rfl
def FermatLastTheorem := ∀ x y z n : Nat, n > 2 ∧ x * y * z ≠ 0 → x ^ n + y ^ n ≠ z ^ n
#check FermatLastTheorem
theorem hard : FermatLastTheorem := sorry

The Curry-Howard Correspondence

Logic sideProgramming side
formulatype
proofterm
formula is truetype has an element
formula is falsetype does not have an element
logical constant ⊤ (truth)unit type
logical constant ⊥ (falsehood)empty type
implicationfunction type
conjunctionproduct type
disjunctionsum type
universal quantificationdependent product type
existential quantificationdependent sum type

More proofs

def mk_symm (xs : List α) :=
  xs ++ xs.reverse
#eval mk_symm [1,2]
theorem revser_mk_symm : (mk_symm xs).reverse = mk_symm xs := by
  simp [mk_symm]
theorem tst : (xs ++ mk_symm ys).reverse = mk_symm ys ++ xs.reverse := by
  simp [revser_mk_symm]
#print tst

Proof Automation and AI

gptf

Resources

Official Homepage of Lean

Zulip chat

Theorem Proving in Lean 4

Mathematics in Lean

Functional Programming in Lean