Stage 01 · Step 5 of 50

Make a decision.

Core Lean5 minutesLean 4.36.0

01The idea

A signpost sends you one way or another. A conditional chooses between two results.

Lemmi · Your guide for this idea
Lemmi pointing

A Boolean test supplies the yes-or-no answer.

02In maths

A natural number is even exactly when its remainder after division by two is zero.

Picture it

A Boolean test as a fork in the trailA trail reaches a signpost that asks isEven n, which computes n % 2 == 0. The trail forks. The upper branch is labelled true and leads to the then result, "even". The lower branch is labelled false and leads to the else result, "odd". The number 12 has remainder 0 after dividing by 2, so isEven 12 is true and 12 takes the upper path. The number 7 has remainder 1, so isEven 7 is false and 7 takes the lower path to "odd".isEven nn % 2 == 0truefalse"even"then"odd"else1212 % 2 = 077 % 2 = 1a number walks upand asks the sign

Swipe sideways to see the whole picture.

isEven is a signpost: it divides by 2, checks whether the remainder is 0, and answers true or false. An if … then … else is the fork in the trail: true takes the then path, false takes the else path. So if isEven 7 then "even" else "odd" ends at "odd".

03In Lean

This code comes directly from the file checked by Lean.

E05.leanTry it live (opens official Lean editor)
def isEven (n : Nat) : Bool := n % 2 == 0

#eval isEven 12
#eval if isEven 7 then "even" else "odd"

example : isEven 12 = true := by
  rfl

A Boolean is a value of type Bool. A conditional selects its then branch when the condition is true.

The percent symbol computes a remainder. The double equals sign is a Boolean equality test in this example.

A Boolean value and a proposition play different roles in Lean. Next, we will move from computing answers to proving statements.

The final claim records what the decision returns. Lean computes the remainder and checks that the Boolean result is true.

04Step by step

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

After line 6
⊢ isEven 12 = true
After line 6: example : isEven 12 = true := by
⊢ isEven 12 = true
After line 7: rfl
no goals

05Lean says

Captured from Lean 4.36.0
true
"odd"
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

    Define isOdd using the remainder after division by two.

  2. A little stretch

    Define numberLabel to return the text even or odd.

Exercises/E05.leanTry it live (opens official Lean editor)
def isOdd (n : Nat) : Bool := sorry

def numberLabel (n : Nat) : String := sorry

08A quick check

1. What does the remainder operator % give?

Read the answer and explanation

The part left after whole-number division. The remainder is smaller than the positive divisor.

2. Which equality test returns a Bool here?

Read the answer and explanation

The double equals sign. The Boolean equality test returns true or false. A single equals sign states a proposition.

One more idea in your toolkit.

Take your time. Progress is yours to mark.