Introduction

References:

Tools:

  • elan: manager

  • lake: compiler

Types

Every expression in Lean must have a type.

Nat is the default type for non-negative integer literals.

Nat : Type#check Nat
Nat : Type
0#eval 1 - 2
0
Int : Type#check Int
Int : Type
-1#eval (1 - 2 : Int)
-1
Float : Type#check Float
Float : Type
-1.000000#eval 1.0 - 2.0
-1.000000
String : Type#check String
String : Type
"Hello Lean"#eval String.append "Hello" " Lean"
"Hello Lean"

Definitions

Definitions and functions are defined using the def keyword.

namespace DEFINITIONS

Define Types

abbrev N : Type := Nat2 : N#check (2 : N)
2 : N

Functions

def max (n m : Nat) : Nat := if n <= m then m else n2#eval max 1 2
2

Function arrows associate to the right.

example : Nat → (Nat → Nat) := max

Anonymous Functions

2#eval (fun (n : Nat) => 2 * n) 1
2

Local Definitions

Recursive let definitions must be indicated by let rec.

end DEFINITIONS

Structures

namespace STRUCTURESstructure Point where x : Float -- accessor function Point.x y : Float

Provide values for structure field inside of curly braces.

def origin : Point := { x := 0.0, y := 0.0 }

Positional structure arguments.

def p : Point := ⟨1, 0⟩
def distance (p1 p2 : Point) : Float := Float.sqrt (p2.x - p1.x) ^ 2 + (p2.y - p1.y) ^ 21.000000#eval distance origin p
1.000000

Update structure.

def zeroX (p : Point) : Point := {p with x := 0}
{ x := 0.000000, y := 0.000000 }#eval zeroX p
{ x := 0.000000, y := 0.000000 }

Default constructor name is mk.

STRUCTURES.Point.mk (x y : Float) : Point#check Point.mk
STRUCTURES.Point.mk (x y : Float) : Point

Override constructor name.

structure Point3D where point :: x : Float y : Float z : Float
STRUCTURES.Point3D.point (x y z : Float) : Point3D#check Point3D.point
STRUCTURES.Point3D.point (x y z : Float) : Point3D
end STRUCTURES

Datatypes

namespace DATATYPES

Unlike structure, inductive datatypes may have multiple constructors.

inductive Nat where | zero : Nat -- constructor zero | succ (n : Nat) : Nat -- constructor succ takes an argument named n
def one : Nat := Nat.succ Nat.zerodef two : Nat := Nat.succ one

Pattern Matching

predecessor.

def pred (n : Nat) : Nat := match n with | Nat.zero => Nat.zero | Nat.succ k => k
DATATYPES.Nat.succ (DATATYPES.Nat.zero)#eval pred two
DATATYPES.Nat.succ (DATATYPES.Nat.zero)

Recursive Functions

Definitions that refer to the name being defined are recursive definitions.

def plus (n k : Nat) : Nat := match k with | Nat.zero => n | Nat.succ k' => Nat.succ (plus n k')DATATYPES.Nat.succ (DATATYPES.Nat.succ (DATATYPES.Nat.succ (DATATYPES.Nat.zero)))#eval plus one two
DATATYPES.Nat.succ (DATATYPES.Nat.succ (DATATYPES.Nat.succ (DATATYPES.Nat.zero)))
end DATATYPES

Polymorphism

Polymorphism refers to definitions that take types as arguments.

namespace POLYMORPHISMstructure Point (α : Type) where x : α y : αdef origin : Point Nat := { x := 0, y := 0}

Linked Lists

[1, 2, 3] : List Nat#check [1, 2, 3]
[1, 2, 3] : List Nat
end POLYMORPHISM

Classes

A type class describes types that allow a collection of overloadable operations.

An instance provides an implementation of an operation.

namespace CLASSESclass Plus (α : Type) where plus : α → α → α -- overloaded operation

Overload plus for a particular type.

instance : Plus Nat where plus := Nat.add
2#eval Plus.plus 1 1
2

Positive Numbers

inductive Pos where | one : Pos | succ : Pos → Posdef two : Pos := Pos.succ Pos.onedef Pos.plus (n k : Pos) : Pos := match n with | Pos.one => Pos.succ k | Pos.succ n' => Pos.succ (Pos.plus n' k)instance : Plus Pos where plus := Pos.plusCLASSES.Pos.succ (CLASSES.Pos.succ (CLASSES.Pos.succ (CLASSES.Pos.one)))#eval Plus.plus two two
CLASSES.Pos.succ (CLASSES.Pos.succ (CLASSES.Pos.succ (CLASSES.Pos.one)))

Literal Numbers

Three type classes used to overload numeric literals:

  • Zero

  • One

  • OfNat

instance : One Pos where one := Pos.oneCLASSES.Pos.one#eval (1 : Pos)
CLASSES.Pos.one
instance : OfNat Pos 2 where ofNat := twoCLASSES.Pos.succ (CLASSES.Pos.one)#eval (2 : Pos)
CLASSES.Pos.succ (CLASSES.Pos.one)
end CLASSES

Propositions

A proposition is a statement that can be true or false.

namespace PROPOSITIONSexample : 1 + 1 = 2 := rfl

Tactics

example : 1 + 1 = 2 := ⊢ 1 + 1 = 2 All goals completed! 🐙example : 1 + 1 = 2 := ⊢ 1 + 1 = 2 All goals completed! 🐙

Logic

Logical connectives:

  • True

  • False

  • And A ∧ B

  • Or A ∨ B

  • Not ¬A

  • imply A → B

  • Iff A ↔ B

end PROPOSITIONS

Quantifiers

∀ x : α, P x denotes (x : α) → P x.

namespace QUANTIFIERSexample : ∀ n : Nat, n = n := ⊢ ∀ (n : Nat), n = n n:Nat⊢ n = n All goals completed! 🐙end QUANTIFIERS

Induction

Principle of mathematical induction:

  1. Base case: prove P(0).

  2. Inductive step: assume P(n) is true and prove P(n + 1).

namespace INDUCTIONexample : ∀ n : Nat, n = n := ⊢ ∀ (n : Nat), n = n n:Nat⊢ n = n induction n with ⊢ 0 = 0 All goals completed! 🐙 -- 0 = 0 n:Natih:n = n⊢ n + 1 = n + 1 All goals completed! 🐙 -- n + 1 = n + 1end INDUCTION