Built with doc-gen4, running Lean4. Bubbles () indicate interactive fragments: hover for details, tap to reveal contents. Use Ctrl+↑Ctrl+↓to navigate, Ctrl+🖱️to focus. On Mac, use Cmdinstead of Ctrl.
import Mathlib
/-!
# Welcome to the course

We start with a quick tour, where we:

* Use Lean as a calculator
* Define some functions and call them.
* Look at some types.
* Look at some proofs.

We will then see
* A glimpse of AI.
* A detailed example with programs and proofs.
-/

/-!
## Lean as a calculator.

We begin by using Lean as a calculator. We can use `#eval` to evaluate expressions.

```lean
#eval 1 + 2 -- 3

#eval "Hello " ++ "world!" -- "Hello world!"
```
-/
3
1: ?m.5
1
+
2: ?m.16
2
"Hello world!"
"Hello ": String
"Hello "
++
"world!": String
"world!"
/-- An arbitrary number. -/ def
some_number:
some_number
:=
42: ?m.1146
42
/-! We next evaluate an expression involving a definition. ```lean #eval some_number + 23 -- 65 ``` -/
65
some_number:
some_number
+
23: ?m.1178
23
/-! ## Defining functions We next define some functions. These are defined in terms of previously defined functions. -/ /-- Add `2` to a natural number -/ def
add_two:
add_two
(
n:
n
:
: Type
) :
: Type
:=
n:
n
+
2: ?m.1702
2
/-- Cube a natural number -/ def
cube:
cube
(
n:
n
:
: Type
) :
: Type
:=
n:
n
*
n:
n
*
n:
n
/-! ```lean #eval cube (add_two 3) -- 125 ``` -/
125
cube:
cube
(
add_two:
add_two
3: ?m.1931
3
) /-- Cube a natural number -/ def
cube': ?m.2373
cube'
:= fun (
n:
n
:
: Type
) ↦
n:
n
*
n:
n
*
n:
n
/-- Cube a natural number -/ def
cube'':
cube''
:
: Type
: Type
:= fun
n: ?m.2495
n
n: ?m.2495
n
*
n: ?m.2495
n
*
n: ?m.2495
n
example:
example
:= λ (
n:
n
:
: Type
) =>
n:
n
*
n:
n
*
n:
n
/-! ## Types Terms in Lean, including functions, have types, which can be seen using `#check` ```lean #check 1 + 2 -- ℕ #check "Hello " ++ "world!" -- String #check add_two -- ℕ → ℕ #check cube -- ℕ → ℕ #check ℕ -- Type #check Type -- Type 1 #check ℕ → ℕ -- Type ``` -/
1 + 2 : ℕ
1: ?m.2729
1
+
2: ?m.2740
2
"Hello " ++ "world!" : String
"Hello ": String
"Hello "
++
"world!": String
"world!"
add_two (n : ℕ) : ℕ
add_two:
add_two
cube (n : ℕ) : ℕ
cube:
cube
ℕ : Type
: Type
Type : Type 1
Type: Type 1
Type
ℕ → ℕ : Type
: Type
: Type
/-! We next define a function of two arguments, and look at its type. We see that this is defined as a function from `ℕ` to a function from `ℕ` to `ℕ`. -/ /-- Sum of squares of natural numbers `x` and `y` -/ def
sum_of_squares:
sum_of_squares
(
x:
x
y:
y
:
: Type
) :
: Type
:=
x:
x
*
x:
x
+
y:
y
*
y:
y
/-! ```lean #check sum_of_squares -- ℕ → ℕ → ℕ #check sum_of_squares 3 -- ℕ → ℕ ``` We can also define this in a way that makes the type clearer. -/
sum_of_squares (x y : ℕ) : ℕ
sum_of_squares:
sum_of_squares
-- ℕ → (ℕ → ℕ)
sum_of_squares 3 : ℕ → ℕ
sum_of_squares:
sum_of_squares
3: ?m.3135
3
-- ℕ → ℕ /-- Sum of squares of natural numbers `x` and `y` -/ def
sum_of_squares':
sum_of_squares'
:
: Type
: Type
: Type
:= fun
x: ?m.3152
x
fun
y: ?m.3155
y
x: ?m.3152
x
*
x: ?m.3152
x
+
y: ?m.3155
y
*
y: ?m.3155
y