Stage 01 · Step 2 of 50
Everything has a type.
01The idea
A label on a box tells you what belongs inside. A type plays a similar role.
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
Swipe sideways to see the whole picture.
#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.
#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.
⊢ (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
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.
06A common slip
Lean is telling us something useful here.
#check (42 : String)
failed to synthesize instance of type class OfNat String 42 numerals are polymorphic in Lean, but the numeral `42` cannot be used in a context where the expected type is String due to the absence of the instance above Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
The number literal cannot be used as a string.
The fix: Use a natural-number annotation for this number, or write text in quotation marks.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Give aNumber a natural-number value.
Show a hint
The declared type is Nat.
One more hint
A nonnegative whole-number literal fits.
- A little stretch
Give aMessage a text value.
Show a hint
The declared type is String.
One more hint
Put your text between double quotation marks.
def aNumber : Nat := sorry
def aMessage : String := sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
def aNumber : Nat := 42
#check aNumber
def aMessage : String := "hello"
#check aMessage
aNumber : Nat aMessage : String
08A quick check
09New words & tools
Take your time. Progress is yours to mark.