Chapter 1 · Foundations

Terms, types and functions

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

Step 1 of 22 · Concept

Every value has a type

By the end of this lesson you define your own functions and prove facts about them.

Every value has a type. Three types to start with:

typeits values
Natthe numbers 0, 1, 2 and up
Booltrue and false: #eval 3 < 5 shows true
Stringtext in quotes, such as "Lean"

#check e shows a term with its type, as e : T. #check 42 shows 42 : Nat, and #check "Lean" shows "Lean" : String.

#eval 3 - 5
#eval 7 / 2
#eval 7 % 2
#eval 2 ^ 100
def greeting : String := "Hello"
#eval greeting
def double (n : Nat) : Nat := n + n
def add (a b : Nat) : Nat := a + b
#eval double 3 + 1
#eval double (3 + 1)
#eval add 10 4
def f : Nat → Nat := fun n => n + 1
#eval f 5
-- def area (w h : Nat) : Nat, w times h
-- then print area 3 4 with #eval
def double (n : Nat) : Nat := n + n
def area (w h : Nat) : Nat := w * h
theorem area_three_four : area 3 4 = 12 := by
  sorry
-- double_area : double (area 2 5) = 20
-- def triple (n : Nat) : Nat, three times n
-- print triple 7 with #eval
-- triple_seven : triple 7 = 21
-- triple_five : triple (2 + 3) = 15
def greet (name : String) : String := "Hello, " ++ name
-- greet_length : (greet "Ada").length = 10
Terminal
Step 1 of 8Arithmetic on Nat

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)