Chapter 5 · Verified programs

Specifications

You are reading the whole lesson on one page. Back to one step at a time

Step 1 of 21 · Concept

A definition can choose

In this lesson you write a function, state what it must do as theorems, and prove that it does.

if c then a else b gives a when c holds, and b otherwise:

def maxN (a b : Nat) : Nat := if a ≤ b then b else a

Lean must be able to decide c, true or false. a ≤ b is Decidable (lesson 18), so this if works.

def maxN (a b : Nat) : Nat := if a ≤ b then b else a
#eval maxN 3 7
#eval maxN 7 3
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem le_maxN (a b : Nat) : a ≤ maxN a b := by
  rw [maxN]
  split
  · omega
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem maxN_cases (a b : Nat) :
    maxN a b = a ∨ maxN a b = b := by
  by_cases h : a ≤ b
  · simp [maxN, h]
  · simp [maxN, h]
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem le_maxN_right (a b : Nat) : b ≤ maxN a b := by
  sorry
def isEven (n : Nat) : Bool := n % 2 == 0
theorem even_ten : isEven 10 = true := by
  sorry
theorem odd_seven : isEven 7 = false := by
  sorry
theorem even_double (n : Nat) : isEven (n + n) = true := by
  sorry
def maxBad (a b : Nat) : Nat := if a ≤ b then a else b
#eval maxBad 7 3
example : 7 ≤ maxBad 7 3 := by decide
-- the smaller of a and b
def minN (a b : Nat) : Nat := sorry
theorem minN_le_left (a b : Nat) : minN a b ≤ a := by
  sorry
theorem minN_le_right (a b : Nat) : minN a b ≤ b := by
  sorry
theorem minN_cases (a b : Nat) :
    minN a b = a ∨ minN a b = b := by
  sorry
def clamp (lo hi x : Nat) : Nat :=
  if x < lo then lo else if hi < x then hi else x
theorem clamp_ge (lo hi x : Nat) (h : lo ≤ hi) :
    lo ≤ clamp lo hi x := by
  sorry
theorem clamp_le (lo hi x : Nat) (h : lo ≤ hi) :
    clamp lo hi x ≤ hi := by
  sorry
def maxN (a b : Nat) : Nat := if a ≤ b then b else a
theorem maxN_eq_max (a b : Nat) : maxN a b = max a b := by
  sorry
Terminal
Step 1 of 9See maxN run

Keyboard shortcuts

The step

ContinueEnter
Previous / next stepAlt+[ · Alt+]
Move along the steps bar← → Home End, then Enter
From a quiz's title to its answers↓
Skip to the stepTab, then Enter (first on the page)
Course outlineAlt+O
These shortcuts?

The workspace

Run the stepCtrl+Enter (⌘↵ on a Mac)
Lesson → editor → consoleF6 (Shift+F6 back)
Output ↔ TerminalCtrl+`
Focus the editorCtrl+. (⌘. on a Mac)
Hide or show the lessonAlt+L
Restore, close a messageEsc (outside the editor and the terminal)

The editor

Leave the editorF6 (Shift+F6 back); or Ctrl+M (⌃⇧M on a Mac), then Tab moves the focus (not in the vim mode)
Open a file by nameCtrl+P (⌘P on a Mac)
All commandsCtrl+Shift+P (⌘⇧P on a Mac) or F1
Go to a lineCtrl+G (⌃G on a Mac)
Show or hide the filesCtrl+B (⌘B on a Mac)
Previous / next file tabCtrl+Shift+[ · Ctrl+Shift+] (⌘⇧[ · ⌘⇧] on a Mac)
The file tabs← → Home End on a tab; Delete closes it
Save the files nowCtrl+S (⌘S on a Mac)
Go to a symbol in the fileCtrl+Shift+O (⌘⇧O on a Mac)
Go to the definitionF12
Format the fileCtrl+Shift+F (⌘⇧F on a Mac), or Shift+Alt+F
Go back / forward after a jumpAlt+← · Alt+→ (⌃- · ⌃⇧- on a Mac)