Zilin Jiang
Arizona State University
November 15, 2024
Lean 4 is a dependently typed programming language and interactive theorem prover.
#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
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"
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]
A proposition is a type in Lean, defined in the universe Prop.
#check 1 + 1 = 2
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
Logic side | Programming side |
---|---|
formula | type |
proof | term |
formula is true | type has an element |
formula is false | type does not have an element |
logical constant ⊤ (truth) | unit type |
logical constant ⊥ (falsehood) | empty type |
implication | function type |
conjunction | product type |
disjunction | sum type |
universal quantification | dependent product type |
existential quantification | dependent sum type |
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