The type of natural numbers, starting at zero. It is defined as an inductive type freely generated by "zero is a natural number" and "the successor of a natural number is a natural number".
You can prove a theorem P n about n : Nat by induction n, which will
expect a proof of the theorem for P 0, and a proof of P (succ i) assuming
a proof of P i. The same method also works to define functions by recursion
on natural numbers: induction and recursion are two expressions of the same
operation from Lean's point of view.
open Nat
example (n : Nat) : n < succ n := n:Nat⊢ n < n.succ
induction n with
| zero =>
⊢ 0 < 1
All goals completed! 🐙
| succ i ih => -- ih : i < succ i
i:Natih:i < i.succ⊢ i.succ < i.succ.succ
All goals completed! 🐙
This type is special-cased by both the kernel and the compiler:
-
The type of expressions contains "
Natliterals" as a primitive constructor, and the kernel knows how to reduce zero/succ expressions to nat literals. -
If implemented naively, this type would represent a numeral
nin unary as a linked list withnlinks, which is horribly inefficient. Instead, the runtime itself has a special representation forNatwhich stores numbers up to 2^63 directly and larger numbers use an arbitrary precision "bignum" library (usually GMP).
Constructors
zero : Nat