Review: build it without help

Define a type, a record and functions on them, and predict their output, with no step checklist.

Rewritten20 minBeginnerreviewno checklist
0/11 completed in this course

No new material: each exercise mixes ideas from the whole course, without a checklist of steps. Hints only point in a direction; the full answer stays behind Reveal a solution.

Work the way you would on your own. Read the task, decide which kind of Lean you need (a def, a structure, an inductive, a pattern match, an if), write it, check it, and read the reply. If you are stuck, the lesson that introduced the idea is one click away in the course list.

Worked example

-- A reminder of the shapes, on a different type.
inductive Coin where
  | heads
  | tails

def turnOver : Coin → Coin
  | .heads => .tails
  | .tails => .heads

example : turnOver (turnOver .heads) = .heads := by
  rfl
Step through the proof1 / 2

At the start of the proof on line 10. Everything above ⊢ may be used; the line below it is what must be proved.

⊢turnOver (turnOver Coin.heads) = Coin.heads

An inductive type, a function by pattern matching, and a quick test. The exercises ask for each of these on other data.

Takeaway

You can model data, define functions on it and predict what they compute. The next course shows how to prove things about them.

Exercise 1 of 4

Define an inductive type Light with constructors red, yellow and green. Define next : Light → Light so that red is followed by green, green by yellow and yellow by red. Define canGo : Light → Bool, which is true only for green.

Exercise.lean
Loading editor…

These lines run underneath whatever you write. They are what decides the exercise, so your names and types have to match them.

example : next Light.red = Light.green := by rfl
example : next Light.green = Light.yellow := by rfl
example : next Light.yellow = Light.red := by rfl
example : canGo Light.green = true := by rfl
example : canGo Light.red = false := by rfl
example : canGo Light.yellow = false := by rfl
ReadyLn 1, Col 1Lean 4
Draft saved in this browser
Stuck?
Recall and apply

Knowledge check

Answer without looking back, then check your reasoning.

0of 2
correct
Question 1 of 2You add a fourth constructor to Light. What happens to next?
Question 2 of 2What is fee 10?