Module 1: Why Lean·Lesson 3 of 16·3 min read

Every Lean Expression Has a Type, and Type Errors Are Your Friend

The problem: bugs you only find at runtime

In many languages you find out that you passed text where a number was expected only when the program runs, sometimes in production. The mistake was visible in the code all along, but nothing looked at it before running.

Lean takes the opposite approach. Every expression has a type, and Lean checks all the types before anything runs. That sounds strict, and it is. But that strictness is exactly what later lets Lean check proofs, so it is worth getting comfortable with now.

The idea: everything has a type

A term is any expression: 42, "hi", 1 + 2, a function, and later, a proof. Every term has exactly one type that Lean can work out. You ask for it with #check:

lean
#check 42
#check (-3 : Int)
#check "hi"
#check true
#check 'a'
#check Nat

Lean answers:

lean
-- 42 : Nat
-- -3 : Int
-- "hi" : String
-- Bool.true : Bool
-- 'a' : Char
-- Nat : Type

A few things to notice. Nat is the natural numbers 0, 1, 2 and so on, with no negatives. Int includes negatives. Writing (-3 : Int) is a type ascription: you tell Lean which type you mean. true is shown with its full name Bool.true. And Nat itself has a type, Type, because in Lean even types are terms.

Nat is not Int

The difference between Nat and Int is not a detail. Try:

lean
#eval (5 : Nat) - 7
#eval (5 : Int) - 7

Lean prints:

lean
-- 0
-- -2

Natural numbers cannot go below zero, so subtraction on Nat stops at 0. This is called truncated subtraction. It is a common surprise for beginners, and a good example of why you should always know the type you are working in. A statement that is true for Int can be false for Nat.

Function types

Functions have types too. A function that takes a Nat and returns a Nat has type Nat → Nat. You can type the arrow as -> or →, and both mean the same thing.

lean
def double (n : Nat) : Nat := n * 2

#check double
#check double 21
#eval double 21
#check String.length
#check Nat.add

Lean prints:

lean
-- double (n : Nat) : Nat
-- double 21 : Nat
-- 42
-- String.length (b : String) : Nat
-- Nat.add : Nat → Nat → Nat

Look at the last line. Nat.add has type Nat → Nat → Nat. Read it as "give me a Nat, then another Nat, and I give back a Nat". The arrow groups to the right, so this really means Nat → (Nat → Nat): a function that takes one number and returns a function waiting for the second. You can use it with both arguments and never think about this, but it explains why Lean writes function types this way.

Also notice #check double 21 gives Nat without computing 42. #check only looks at types. #eval is the one that runs code.

Animation: Nat subtraction stops at 0, and Nat → Nat → Nat takes one Nat, then another, and gives back a Nat.

Type errors are the feedback loop

Here is what happens when the types do not fit:

lean
def double (n : Nat) : Nat := n * 2

#eval double "five"

Lean stops with:

lean
-- error: Application type mismatch: The argument
--   "five"
-- has type
--   String
-- but is expected to have type
--   Nat
-- in the application
--   double "five"

This message has a pattern you will see hundreds of times: what you gave, what type it has, what type was expected, and where. Read them in that order and most errors explain themselves.

It might feel like Lean is being fussy. But this exact mechanism is how Lean checks proofs. In a few lessons you will see that a statement like 2 + 2 = 4 is a type, and a proof is a term of that type. A wrong proof produces a type mismatch, just like double "five". Getting good at reading type errors now is getting good at proving things later.

Asking Lean to fill in types

You do not have to write every type. In #check 42, Lean decided on Nat by itself, because number literals default to Nat when nothing else forces a choice. In def double (n : Nat) : Nat := n * 2, you could leave out the return type and Lean would infer it.

A good habit while learning: write the types anyway. They document what you mean, and when Lean disagrees, the error points at the exact spot where your idea and your code split apart.

Try this

Paste into https://live.lean-lang.org and guess each result first:

lean
#check (7 : Int)
#eval (3 : Nat) - 10
#eval (3 : Int) - 10
#check String.append
#eval String.length "proof"

Then write #check String.length 5 and read the error using the pattern above.

Sources

Try it yourselfEasy

Make the types line up

Each definition has the wrong body or a missing piece. Fill in the sorry holes so the file type-checks. `half` should return an Int that is the input divided by 2; `isLong` should be true when a string has more than 5 characters; `apply2` should apply a function twice. Until you fill in the sorry holes, the #eval lines show an error saying the expression depends on sorry; that is expected.

def half (n : Int) : Int :=
  sorry

def isLong (s : String) : Bool :=
  sorry

def apply2 (f : Nat → Nat) (x : Nat) : Nat :=
  sorry

#check apply2          -- (Nat → Nat) → Nat → Nat
#eval half (-9)        -- -5 (Int division rounds down)
#eval isLong "kernel"  -- true
#eval apply2 (· + 3) 1 -- 7
Open in the Lean web editor

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.

Saved in this browser. Pass the quiz while signed in to count it for the certificate.

Check what you learned

Answer all 3 questions. You need at least 2 right to pass.

1. What does #eval (5 : Nat) - 7 print?

2. How should you read the type Nat → Nat → Nat?

3. What is the type of Nat itself?