Stage 01 · Step 2 of 50

Everything has a type.

Core Lean5 minutesLean 4.36.0

01The idea

A label on a box tells you what belongs inside. A type plays a similar role.

Lemmi · Your guide for this idea
Lemmi pointing

Lean checks that each value fits the type expected in its position.

02In maths

A natural number, a piece of text, and a Boolean value belong to different types.

Picture it

Types as labelled binsFour open bins, each with a label. The Nat bin holds whole numbers such as 42, 0 and 7. The String bin holds text such as "hello" and "Lean". The Bool bin holds exactly two values, true and false. The bin labelled Nat → Nat holds functions that take a natural number and return one, such as fun n => n and fun n => n + 1. Every value belongs in the bin whose label is its type.4207Natwhole numbers 0, 1, 2, …"hello""Lean"Stringtext in quotation markstruefalseBoolexactly two valuesfun n => nfun n => n + 1Nat → Natmachines: Nat in, Nat out

Swipe sideways to see the whole picture.

Think of each type as a bin with a label, and each value as something that belongs in exactly one bin. #check reads the label for you: 42 goes in Nat, "hello" in String. Even functions have a bin: Nat → Nat holds every machine that turns a natural number into a natural number.

03In Lean

This code comes directly from the file checked by Lean.

E02.leanTry it live (opens official Lean editor)
#check (42 : Nat)
#check "hello"
#check true
#check Nat → Nat

example : (fun n : Nat => n) 42 = 42 := by
  rfl

A type tells Lean what sort of value an expression has. A value or expression inhabiting a type is called a term.

Nat is the type of natural numbers. String is the type of text. Bool has the two values true and false.

The symbol → means “to” in this function type. Type it with \to, then a space, in the Lean editor. Later, an arrow between propositions will mean “implies.”

The final claim uses a function that returns the number it receives. Lean checks that passing it 42 gives 42 back.

04Step by step

Choose a line to see Lean’s recorded goal state after that line.

After line 6
⊢ (fun n => n) 42 = 42
After line 6: example : (fun n : Nat => n) 42 = 42 := by
⊢ (fun n => n) 42 = 42
After line 7: rfl
no goals

05Lean says

Captured from Lean 4.36.0
42 : Nat
"hello" : String
Bool.true : Bool
Nat → Nat : Type
no goals

A goal is what remains to prove. The symbol ⊢ points to it. Meet the turnstile.

07Your turn

sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.

  1. Warm-up

    Give aNumber a natural-number value.

  2. A little stretch

    Give aMessage a text value.

Exercises/E02.leanTry it live (opens official Lean editor)
def aNumber : Nat := sorry

def aMessage : String := sorry

08A quick check

1. What does #check report?

Read the answer and explanation

The type of an expression. Type checking tells us what kind of expression we have.

2. What does Nat → Nat describe?

Read the answer and explanation

A function from natural numbers to natural numbers. The arrow here describes a function type. It takes one Nat and returns a Nat.

One more idea in your toolkit.

Take your time. Progress is yours to mark.