Chapter 4 · Your own data

Structures

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

Step 1 of 22 · Concept

A structure groups named values

In this lesson you group values into one, prove facts about them, and put a proof inside data.

structure Point where
  x : Nat
  y : Nat
deriving Repr

A structure is a type whose values hold named fields. Every Point has a number x and a number y. { x := 1, y := 2 } is a point, each field named.

structure Point where
  x : Nat
  y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
#eval p
#eval (⟨3, 4⟩ : Point).x
structure Point where
  x : Nat
  y : Nat
deriving Repr
def p : Point := { x := 1, y := 2 }
def q : Point := { p with x := 5 }
#eval q
structure Point where
  x : Nat
  y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
def p : Point := { x := 1, y := 2 }
#eval p.swap
structure Point where
  x : Nat
  y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
theorem swap_swap (p : Point) : p.swap.swap = p := by
  rfl
structure Point where
  x : Nat
  y : Nat
deriving Repr
def Point.swap (p : Point) : Point := { x := p.y, y := p.x }
theorem swap_sum (p : Point) :
    p.swap.x + p.swap.y = p.x + p.y := by
  cases p with
  | mk a b =>
    simp [Point.swap]
    omega
structure Point where
  x : Nat
  y : Nat
deriving Repr
-- the point d steps further right: x + d
def shift (p : Point) (d : Nat) : Point := sorry
theorem shift_x (p : Point) : (shift p 3).x = p.x + 3 := by
  sorry
theorem shift_zero (p : Point) : shift p 0 = p := by
  sorry
structure Point where
  x : Nat
  y : Nat
deriving Repr
def Point.add (p q : Point) : Point :=
  { x := p.x + q.x, y := p.y + q.y }
theorem add_comm' (p q : Point) : p.add q = q.add p := by
  sorry
structure Pos where
  n : Nat
  pos : 0 < n
deriving Repr
def three : Pos := ⟨3, by decide⟩
#eval three
structure Rect where
  w : Nat
  h : Nat
deriving Repr
-- w times h
def Rect.area (r : Rect) : Nat := sorry
-- w and h swapped
def Rect.turn (r : Rect) : Rect := sorry
-- show the area of a 3 by 4 rectangle with #eval
theorem turn_area (r : Rect) : r.turn.area = r.area := by
  sorry
theorem turn_turn (r : Rect) : r.turn.turn = r := by
  sorry
structure Diag where
  x : Nat
  y : Nat
  same : x = y
def two : Diag := ⟨2, 2, by rfl⟩
theorem diag_sum (d : Diag) : d.x + d.y = 2 * d.x := by
  sorry
Terminal
Step 1 of 10See it 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)