Put a small file together

Combine a structure, record update, evaluation and multi-step theorem.

18 minBeginnerreviewstructures and theoremsrw#evala local Lean project
0/14 completed in this course

Put the pieces together in a small file: model data with a structure, define a transformation, evaluate a concrete input, and record a property as a named theorem. The definition says what the program does; the theorem states what callers may rely on.

The supplied Score type has a named points field. award returns a record update, preserving the shape of the value while changing that field. Your theorem works for any score whose points are known to be five, so it needs both a parameter and a hypothesis.

The new tactic is rw [h], short for rewrite. When h : s.points = 5, it replaces every s.points in the goal by 5, the same substitution you would do on paper. After unfolding award the goal mentions s.points + 3, and one rewrite turns it into a calculation that Lean finishes by itself.

You can keep your work with Download .lean below the editor. The downloaded file includes the definitions and exercise checks, so it can be opened as ordinary Lean code. When you want a local project, elan manages Lean versions, Lake builds the project, and the Lean 4 extension for VS Code displays goals as you edit. Start with the same Lean 4.30 toolchain used here.

Worked example

structure Account where
  balance : Nat

def credit (a : Account) (amount : Nat) : Account :=
  { a with balance := a.balance + amount }

theorem credit_known (a : Account) (h : a.balance = 10) : (credit a 2).balance = 12 := by
  unfold credit
  rw [h]

#eval (credit { balance := 4 } 3).balance

This miniature project updates a structure, proves a property under a hypothesis and evaluates a concrete call. Your exercise repeats that workflow with a score.

Takeaway

A small Lean project can model data, transform it, evaluate examples and prove general properties from hypotheses. These parts reinforce one another.

Exercise 1 of 2

Prove award_from_five: a score with five points has eight after an award of three. Then, below the theorem, use #eval to print the points after awarding 4 to a score of 7. The exercise is complete when Lean prints 11.

Suggested steps
  1. State award_from_five with a score and a hypothesis
  2. Unfold the update and rewrite the known points
  3. Evaluate awarding four points to a score of seven

These marks only recognize text. Another correct proof may use different steps; Lean checks whether it works.

Exercise.lean
1structure Score where
2 points : Nat
3
4def award (s : Score) (bonus : Nat) : Score :=
5 { s with points := s.points + bonus }
given
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 (s : Score) (h : s.points = 5) : (award s 3).points = 8 := award_from_five s h
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 3
correct
Question 1 of 3What does award_from_five establish?
Question 2 of 3With h : s.points = 5, what does rw [h] do to a goal containing s.points + 3?
Question 3 of 3What do you need to continue this file locally?