Stage 01 · Step 4 of 50
Words and collections.
01The idea
A word can join another word. A row of items can join another row.
Lean keeps these two kinds of joining separate through their types.
02In maths
Joining lists preserves the order of their elements. The length counts how many elements a list contains.
Picture it
Swipe sideways to see the whole picture.
++ couples the second train behind the first, so the order stays 2, 4, then 6. length just counts wagons, which is why ([2, 4] ++ [6]).length is 3. The same ++ joins text too, but Lean only lets you join text to text and lists to lists.03In Lean
This code comes directly from the file checked by Lean.
#eval "Lean" ++ " Trail"
#eval ([2, 4, 6] : List Nat).length
#eval ([2, 4] : List Nat) ++ [6]
example : (([2, 4] : List Nat) ++ [6]).length = 3 := by
rfl
A string stores text. A list stores a sequence of elements of one type. An element is one item in that sequence.
Double quotation marks surround strings. Square brackets surround a list literal. Commas separate its elements.
The symbol ++ means append for these examples. Its meaning depends on the types of the things being joined.
The final claim checks the length after joining two lists. Once joined, the list has three entries.
04Step by step
Choose a line to see Lean’s recorded goal state after that line.
⊢ ([2, 4] ++ [6]).length = 3
After line 5: example : (([2, 4] : List Nat) ++ [6]).length = 3 := by
⊢ ([2, 4] ++ [6]).length = 3
After line 6: rfl
no goals
05Lean says
"Lean Trail" 3 [2, 4, 6] 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.
#eval "Lean" + ([1, 2] : List Nat)
failed to synthesize instance of type class HAdd String (List Nat) ?m.11 Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
The chosen operation cannot add a string and a natural-number list.
The fix: Use matching types and the append operator for the kind of values you want to join.
07Your turn
sorry is a placeholder for unfinished work. Replace it as you solve each task. A finished proof has no placeholders.
- Warm-up
Set greeting to the text Hello followed by a space and Lean.
Show a hint
Both sides of the join must be strings.
One more hint
Use the text-joining operator shown in the example.
- A little stretch
Set trail by joining the list containing one and two with the list containing three and four.
Show a hint
Each list uses square brackets.
One more hint
Use the same joining operator between the two lists.
def greeting : String := sorry
def trail : List Nat := sorry
Compare with the checked solutions
There are often several valid proofs. These solutions were compiled with the same pinned Lean version.
def greeting : String := "Hello" ++ " Lean"
#eval greeting
def trail : List Nat := [1, 2] ++ [3, 4]
#eval trail
"Hello Lean" [1, 2, 3, 4]
08A quick check
09New words & tools
Take your time. Progress is yours to mark.