2. Expressions, functions, and types
Lean is expression-oriented. A function body, a conditional, and a local definition all produce values. In ordinary functional code, new values are expressions built from existing ones.
Read a declaration from left to right
def fee (amount : Nat) : Nat :=
let base := 2
if amount < 100 then base else base + 1
def applyTwice (f : Nat → Nat) (x : Nat) : Nat :=
f (f x)
#eval fee 120
#eval applyTwice (fun n => n + 3) 10
amount is an explicit parameter. The type after the last colon is the result type.
:= introduces the body. The local binding base names a fixed value within the rest of the expression.
Functions are values, and fun n => n + 3 constructs one without naming it.
Function application uses spaces and associates to the left: f x y means (f x) y. Arrow types associate to the right: Nat → Nat → Nat means Nat → (Nat → Nat).
Partial application supplies some arguments to a function. Giving an addition function its first argument produces a function that takes the second.
def add (x y : Nat) : Nat := x + y
def addTen : Nat → Nat := add 10
example : addTen 7 = 17 := by rfl
Choose numerical types intentionally
Nat contains 0 and the positive whole numbers. Its subtraction truncates at 0, so 3 - 5 is 0. Int represents signed whole numbers, so (3 : Int) - 5 is -2.
UInt64 is a machine integer with bounded arithmetic. Choose the type whose arithmetic matches the quantity you are modelling.
Natural-number balances are nonnegative. To reject excessive withdrawals, check affordability explicitly and include that condition in the operation’s contract.
Literal types are inferred from context. A type annotation such as (3 : Int) tells Lean which interpretation is intended. When inference fails, add an annotation where the type first becomes ambiguous.
Polymorphism and implicit arguments
def keep {α : Type} (x : α) : α := x
#check keep
#check @keep
example : keep "account" = "account" := by rfl
α is a type parameter. Braces make it implicit, so Lean usually infers it from x.
@keep exposes implicit arguments for inspection or explicit application. Use keep (α := Nat) 7 to pass it by name.
Type itself has a type, and Lean uses a hierarchy of universes to prevent inconsistent self-reference. Write universe-polymorphic functions with universe u and α : Type u. Lean infers universe levels in most application code.
Definitions and equality
Lean can unfold the body of a definition, and a theorem names a proof. rfl proves definitional equality when both sides become the same under Lean’s computation rules.
For expressions such as n + 0 and 0 + n, the recursion rule for addition determines which expression reduces when n is a variable.
When a proof by rfl fails, ask whether both expressions reduce to the same form or whether you need an established theorem relating them.
Exercises
- Define a polymorphic function that chooses the first of 2 arguments of the same type.
- Define composition for functions
α → βandβ → γ. - Evaluate natural and integer subtraction on the same inputs. Describe which model is appropriate for a signed temperature and which for a list length.