Introduction
References:
Tools:
elan: managerlake: compiler
Types
Every expression in Lean must have a type.
Nat is the default type for non-negative integer literals.
#check Nat#eval 1 - 2#check Int#eval (1 - 2 : Int)#check Float#eval 1.0 - 2.0#check String#eval String.append "Hello" " Lean"Definitions
Definitions and functions are defined using the def keyword.
namespace DEFINITIONSDefine Types
Functions
Function arrows associate to the right.
example : Nat → (Nat → Nat) := maxAnonymous Functions
#eval (fun (n : Nat) => 2 * n) 1Local Definitions
Recursive let definitions must be indicated by let rec.
end DEFINITIONSStructures
namespace STRUCTURESstructure Point where
x : Float -- accessor function Point.x
y : FloatProvide values for structure field inside of curly braces.
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) ^ 2#eval distance origin pUpdate structure.
Default constructor name is mk.
#check Point.mkOverride constructor name.
structure Point3D where
point ::
x : Float
y : Float
z : FloatDatatypes
namespace DATATYPESUnlike structure, inductive datatypes may have multiple constructors.
inductive Nat where
| zero : Nat -- constructor zero
| succ (n : Nat) : Nat -- constructor succ takes an argument named nPattern Matching
predecessor.
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')#eval plus one twoend DATATYPESPolymorphism
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
#check [1, 2, 3]end POLYMORPHISMClasses
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 operationOverload plus for a particular type.
#eval Plus.plus 1 1Positive 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.plus#eval Plus.plus two twoLiteral Numbers
Three type classes used to overload numeric literals:
ZeroOneOfNat
instance : One Pos where
one := Pos.one#eval (1 : Pos)instance : OfNat Pos 2 where
ofNat := two#eval (2 : Pos)end CLASSESPropositions
A proposition is a statement that can be true or false.
namespace PROPOSITIONSexample : 1 + 1 = 2 := rflTactics
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 ∧ BOr
A ∨ BNot
¬Aimply
A → BIff
A ↔ B
end PROPOSITIONSQuantifiers
∀ 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 QUANTIFIERSInduction
Principle of mathematical induction:
Base case: prove P(0).
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