Chapter 4 · Your own data

One symbol, many types

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

Step 1 of 22 · Concept

Int has negative numbers

You will make + work on your own type, prove facts about it, and write functions for any type.

Nat has no negative numbers (lesson 3). The type Int has them. (3 : Int) is 3 as an Int: the type is named as in lesson 17's (⟨3, 4⟩ : Point).

linevalue
(3 : Int) - 5-2
(3 : Nat) - 50

This course keeps to Nat, so subtraction stops at 0 in its statements.

#eval (3 : Int) - 5
#eval (3 : Nat) - 5
structure Point where
  x : Nat
  y : Nat
deriving Repr
instance : Add Point where
  add p q := { x := p.x + q.x, y := p.y + q.y }
#eval ({ x := 1, y := 2 } : Point) + { x := 10, y := 20 }
structure Point where
  x : Nat
  y : Nat
deriving Repr
instance : Add Point where
  add p q := { x := p.x + q.x, y := p.y }
#eval ({ x := 1, y := 2 } : Point) + { x := 10, y := 20 }
structure Pair where
  a : Nat
  b : Nat
deriving Repr
-- add the a fields, add the b fields
instance : Add Pair where
  add p q := sorry
theorem pair_sum : (⟨1, 2⟩ + ⟨3, 4⟩ : Pair) = ⟨4, 6⟩ := by
  sorry
def twice {α : Type} (f : α → α) (x : α) : α := f (f x)
#eval twice (fun n => n + 1) 5
#eval twice (fun s => s ++ "!") "Hi"
def double' {α : Type} [Add α] (x : α) : α := x + x
#eval double' 21
#eval double' (-4 : Int)
#eval double' "ab"
def sum : List Nat → Nat
  | [] => 0
  | x :: xs => x + sum xs
-- the elements added with +, starting from zero,
-- for every type α with an Add instance
def total {α : Type} [Add α] (zero : α) : List α → α
  := sorry
theorem total_sum (xs : List Nat) : total 0 xs = sum xs := by
  sorry
#eval 2 == 3
#check 2 = 3
#eval if 2 = 3 then "same" else "different"
example : ∀ n : Nat, n + 0 = n := by decide
structure Point where
  x : Nat
  y : Nat
deriving Repr
-- add the x fields, add the y fields
instance : Add Point where
  add p q := sorry
theorem add_zero_right (p : Point) : p + ⟨0, 0⟩ = p := by
  sorry
theorem add_x (p q : Point) : (p + q).x = p.x + q.x := by
  sorry
structure Point where
  x : Nat
  y : Nat
deriving Repr
instance : Add Point where
  add p q := { x := p.x + q.x, y := p.y + q.y }
theorem zero_add_left (p : Point) :
    (⟨0, 0⟩ : Point) + p = p := by
  sorry
Terminal
Step 1 of 11See 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)