Writing Functions in Lean: def, fun, Curly Braces and match
The problem: repeating yourself
Once you can compute 3 * 3 and 7 * 7, you want to say "square any number" once and reuse it. Every language solves this with functions. Lean's functions look a little different from Python or JavaScript, and they come with two features you will rely on in proofs: implicit arguments and pattern matching.
This lesson covers the four tools you need: def, fun, arguments in curly braces, and match.
def: naming a function
You already met def. Here it is with two arguments, plus an anonymous function written with fun:
leandef add (x y : Nat) : Nat := x + y #eval add 2 3 def square : Nat -> Nat := fun n => n * n #eval square 7 #eval (fun x => x + 1) 4 #eval [1, 2, 3].map (fun x => x * 10)
Lean prints:
lean-- 5 -- 49 -- 5 -- [10, 20, 30]
In def add (x y : Nat) : Nat := x + y, the part (x y : Nat) declares two arguments of type Nat, the : Nat after it is the return type, and everything after := is the body. There is no return keyword. The body is an expression and its value is the result.
Calling a function needs no parentheses or commas: add 2 3, not add(2, 3). Function application is just a space.
fun: functions without names
fun n => n * n is a function with no name, often called a lambda. It takes n and returns n * n. Writing def square : Nat -> Nat := fun n => n * n means exactly the same as def square (n : Nat) : Nat := n * n. The second style is just shorter.
Anonymous functions shine when you pass a function to another function. [1, 2, 3].map (fun x => x * 10) applies the lambda to each element and gives [10, 20, 30].
Implicit arguments in curly braces
Say you want an identity function that works for numbers, strings, anything. You need the type itself as an argument:
leandef identity {α : Type} (x : α) : α := x #eval identity 5 #eval identity "hi" #check @identity #eval @identity Nat 5
Lean prints:
lean-- 5 -- "hi" -- @identity : {α : Type} → α → α -- 5
The argument {α : Type} is in curly braces, which makes it implicit. You never pass it yourself. When you write identity 5, Lean sees that 5 is a Nat and fills in α := Nat for you. When you write identity "hi", it fills in String.
The @ sign turns implicit arguments back into explicit ones. That is why #check @identity shows the full type with the braces, and why @identity Nat 5 works: you passed the type by hand.
Implicit arguments matter far more in proofs than in programs. Many theorems take dozens of things Lean can figure out from context, and curly braces keep you from having to write them all out.
match: pattern matching
Often you want different behaviour for different shapes of input. Lean does this with match:
leandef describe (n : Nat) : String := match n with | 0 => "zero" | 1 => "one" | _ => "many" #eval describe 0 #eval describe 1 #eval describe 50 def isZero : Nat -> Bool | 0 => true | _ => false #eval isZero 0 #eval isZero 3
Lean prints:
lean-- "zero" -- "one" -- "many" -- true -- false
match n with checks n against each pattern from top to bottom and uses the first one that fits. The underscore _ matches anything. In isZero you see a shorter form: when a function is defined directly by cases, you can drop the argument name and the match line and list the patterns right after the type.
Animation: Each input goes to the first pattern that fits: 0, 1, and then the wildcard for everything else.
Lean checks that you covered every case
Here is the main difference from a switch statement in many languages. Delete the last case and see:
leandef describe (n : Nat) : String := match n with | 0 => "zero" | 1 => "one"
Lean refuses with:
lean-- error: Missing cases: -- (Nat.succ (Nat.succ _))
Lean will not let a function crash on an input you forgot. It even tells you which inputs are missing. Nat.succ (Nat.succ _) means "2 or more", written in terms of how natural numbers are built, which is the topic of the next lesson.
This rule is not just tidiness. In Lean, a function must return a value for every input. That guarantee is what lets you later prove statements about all inputs. A function with a hole in it would make those proofs meaningless.
Putting it together
A good mental model: def gives a name, fun makes a function value, curly braces let Lean fill in boring arguments, and match splits into cases that must cover everything. Almost every Lean program you read uses all four.
Try this
Paste into https://live.lean-lang.org and check that it prints 9:
leandef max2 (a b : Nat) : Nat := if a > b then a else b #eval max2 4 9
Now write max3 (a b c : Nat) : Nat that reuses max2, and test it with #eval max3 4 9 2. Then write sign : Int -> String that returns "negative", "zero" or "positive".
Sources
Write a function with pattern matching
Write `trafficNext`, which says which light comes after the current one: red to green, green to amber, amber to red. Use `match` and cover every case. Then prove the cycle comes back after three steps for red.
def trafficNext (light : String) : String := match light with | "red" => sorry | "green" => sorry | _ => sorry example : trafficNext (trafficNext (trafficNext "red")) = "red" := by sorry
The official Lean editor runs in your browser and checks the file on Lean's server. Replace every sorry until no errors or warnings are left, then mark it solved.
The worked solution unlocks after your first attempt.
Check what you learned
Answer all 3 questions. You need at least 2 right to pass.
1. What do curly braces like {α : Type} make an argument?
2. What does the @ in @identity do?
3. What happens if a match does not cover every input?