migrate all tutorials from Markdown to Org mode
Converted with pandoc 3.7 (markdown → org), all 17 files: - README, references, 15 tutorial units - Internal file links updated from .md to .org - Source code blocks (#+begin_src lean) preserved - Tables, math notation, links intact For the Emacs workflow.
This commit is contained in:
@@ -1,107 +0,0 @@
|
||||
# Unit 1 — Types and Functions
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Define functions with `def` and lambda notation
|
||||
- Understand Lean's basic types: `Nat`, `Int`, `String`, `Bool`, `List`, `Option`
|
||||
- Use pattern matching and `match` expressions
|
||||
- Run code with `#eval`
|
||||
|
||||
## Source
|
||||
|
||||
*Functional Programming in Lean* (FPIL), Chapters 1–2
|
||||
→ https://lean-lang.org/functional_programming_in_lean/
|
||||
|
||||
## Concepts
|
||||
|
||||
| Concept | Lean syntax | Example |
|
||||
|---------|------------|---------|
|
||||
| Function definition | `def f (x : Nat) : Nat :=` | `def double (x : Nat) : Nat := x + x` |
|
||||
| Lambda | `fun x => ...` | `fun x => x + 1` |
|
||||
| Pattern matching | `match e with \| pat => ...` | `match n with \| 0 => "zero" \| _ => "non-zero"` |
|
||||
| Recursion | `def f ... := match ...` | Structural recursion only (no general fixpoint) |
|
||||
| `#eval` | `#eval expr` | `#eval double 21` prints `42` |
|
||||
|
||||
## Exercises
|
||||
|
||||
> **How to work**: Create a file `Unit1.lean`. Write each exercise, then
|
||||
> `#eval` to test it (if it's a function) or just make it compile (if it's a
|
||||
> proof or definition). Fill in the `sorry` placeholders.
|
||||
|
||||
### Exercise 1.1 — Warm-up functions
|
||||
|
||||
```lean
|
||||
-- (a) Write a function that returns the absolute difference between two nats
|
||||
def absDiff (a b : Nat) : Nat :=
|
||||
sorry
|
||||
|
||||
#eval absDiff 7 3 -- expected: 4
|
||||
#eval absDiff 3 7 -- expected: 4
|
||||
#eval absDiff 5 5 -- expected: 0
|
||||
|
||||
-- (b) Write a function that checks whether a list contains an element
|
||||
def contains [BEq α] (x : α) (xs : List α) : Bool :=
|
||||
sorry
|
||||
|
||||
#eval contains 3 [1, 2, 3, 4] -- expected: true
|
||||
#eval contains 5 [1, 2, 3, 4] -- expected: false
|
||||
|
||||
-- (c) Write `map` (without looking at the standard library)
|
||||
def myMap (f : α → β) (xs : List α) : List β :=
|
||||
sorry
|
||||
|
||||
#eval myMap (fun x => x * 2) [1, 2, 3] -- expected: [2, 4, 6]
|
||||
```
|
||||
|
||||
### Exercise 1.2 — Recursion on lists
|
||||
|
||||
```lean
|
||||
-- (a) Sum of a list
|
||||
def sumList (xs : List Nat) : Nat :=
|
||||
sorry
|
||||
|
||||
#eval sumList [1, 2, 3, 4] -- expected: 10
|
||||
|
||||
-- (b) Reverse a list (the slow way is fine)
|
||||
def myReverse (xs : List α) : List α :=
|
||||
sorry
|
||||
|
||||
#eval myReverse [1, 2, 3] -- expected: [3, 2, 1]
|
||||
|
||||
-- (c) Filter: keep elements satisfying p
|
||||
def myFilter (p : α → Bool) (xs : List α) : List α :=
|
||||
sorry
|
||||
|
||||
#eval myFilter (fun x => x % 2 == 0) [1, 2, 3, 4, 5, 6] -- expected: [2, 4, 6]
|
||||
```
|
||||
|
||||
### Exercise 1.3 — Option type
|
||||
|
||||
```lean
|
||||
-- (a) Safe head
|
||||
def safeHead (xs : List α) : Option α :=
|
||||
sorry
|
||||
|
||||
#eval safeHead ([1, 2, 3] : List Nat) -- expected: some 1
|
||||
#eval safeHead ([] : List Nat) -- expected: none
|
||||
|
||||
-- (b) Safe division
|
||||
def safeDiv (a b : Nat) : Option Nat :=
|
||||
sorry
|
||||
|
||||
#eval safeDiv 10 2 -- expected: some 5
|
||||
#eval safeDiv 10 0 -- expected: none
|
||||
|
||||
-- (c) Lookup in an association list
|
||||
def lookup [BEq α] (key : α) (alist : List (α × β)) : Option β :=
|
||||
sorry
|
||||
|
||||
#eval lookup "b" [("a", 1), ("b", 2), ("c", 3)] -- expected: some 2
|
||||
#eval lookup "d" [("a", 1), ("b", 2), ("c", 3)] -- expected: none
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
← [Back to README](../README.md) · Next: [Unit 2 — Inductive Types](02-inductive-types.md)
|
||||
126
tutorial-01-basics/01-types-and-functions.org
Normal file
126
tutorial-01-basics/01-types-and-functions.org
Normal file
@@ -0,0 +1,126 @@
|
||||
* Unit 1 --- Types and Functions
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-1-types-and-functions
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Define functions with =def= and lambda notation
|
||||
- Understand Lean's basic types: =Nat=, =Int=, =String=, =Bool=, =List=, =Option=
|
||||
- Use pattern matching and =match= expressions
|
||||
- Run code with =#eval=
|
||||
|
||||
** Source
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: source
|
||||
:END:
|
||||
/Functional Programming in Lean/ (FPIL), Chapters 1--2
|
||||
→ https://lean-lang.org/functional_programming_in_lean/
|
||||
|
||||
** Concepts
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: concepts
|
||||
:END:
|
||||
| Concept | Lean syntax | Example |
|
||||
|---------------------+------------------------------+--------------------------------------------------|
|
||||
| Function definition | =def f (x : Nat) : Nat :== | =def double (x : Nat) : Nat := x + x= |
|
||||
| Lambda | =fun x => ...= | =fun x => x + 1= |
|
||||
| Pattern matching | =match e with \| pat => ...= | =match n with \| 0 => "zero" \| _ => "non-zero"= |
|
||||
| Recursion | =def f ... := match ...= | Structural recursion only (no general fixpoint) |
|
||||
| =#eval= | =#eval expr= | =#eval double 21= prints =42= |
|
||||
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
|
||||
#+begin_quote
|
||||
*How to work*: Create a file =Unit1.lean=. Write each exercise, then
|
||||
=#eval= to test it (if it's a function) or just make it compile (if it's a
|
||||
proof or definition). Fill in the =sorry= placeholders.
|
||||
#+end_quote
|
||||
|
||||
*** Exercise 1.1 --- Warm-up functions
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-1.1-warm-up-functions
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Write a function that returns the absolute difference between two nats
|
||||
def absDiff (a b : Nat) : Nat :=
|
||||
sorry
|
||||
|
||||
#eval absDiff 7 3 -- expected: 4
|
||||
#eval absDiff 3 7 -- expected: 4
|
||||
#eval absDiff 5 5 -- expected: 0
|
||||
|
||||
-- (b) Write a function that checks whether a list contains an element
|
||||
def contains [BEq α] (x : α) (xs : List α) : Bool :=
|
||||
sorry
|
||||
|
||||
#eval contains 3 [1, 2, 3, 4] -- expected: true
|
||||
#eval contains 5 [1, 2, 3, 4] -- expected: false
|
||||
|
||||
-- (c) Write `map` (without looking at the standard library)
|
||||
def myMap (f : α → β) (xs : List α) : List β :=
|
||||
sorry
|
||||
|
||||
#eval myMap (fun x => x * 2) [1, 2, 3] -- expected: [2, 4, 6]
|
||||
#+end_src
|
||||
|
||||
*** Exercise 1.2 --- Recursion on lists
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-1.2-recursion-on-lists
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Sum of a list
|
||||
def sumList (xs : List Nat) : Nat :=
|
||||
sorry
|
||||
|
||||
#eval sumList [1, 2, 3, 4] -- expected: 10
|
||||
|
||||
-- (b) Reverse a list (the slow way is fine)
|
||||
def myReverse (xs : List α) : List α :=
|
||||
sorry
|
||||
|
||||
#eval myReverse [1, 2, 3] -- expected: [3, 2, 1]
|
||||
|
||||
-- (c) Filter: keep elements satisfying p
|
||||
def myFilter (p : α → Bool) (xs : List α) : List α :=
|
||||
sorry
|
||||
|
||||
#eval myFilter (fun x => x % 2 == 0) [1, 2, 3, 4, 5, 6] -- expected: [2, 4, 6]
|
||||
#+end_src
|
||||
|
||||
*** Exercise 1.3 --- Option type
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-1.3-option-type
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Safe head
|
||||
def safeHead (xs : List α) : Option α :=
|
||||
sorry
|
||||
|
||||
#eval safeHead ([1, 2, 3] : List Nat) -- expected: some 1
|
||||
#eval safeHead ([] : List Nat) -- expected: none
|
||||
|
||||
-- (b) Safe division
|
||||
def safeDiv (a b : Nat) : Option Nat :=
|
||||
sorry
|
||||
|
||||
#eval safeDiv 10 2 -- expected: some 5
|
||||
#eval safeDiv 10 0 -- expected: none
|
||||
|
||||
-- (c) Lookup in an association list
|
||||
def lookup [BEq α] (key : α) (alist : List (α × β)) : Option β :=
|
||||
sorry
|
||||
|
||||
#eval lookup "b" [("a", 1), ("b", 2), ("c", 3)] -- expected: some 2
|
||||
#eval lookup "d" [("a", 1), ("b", 2), ("c", 3)] -- expected: none
|
||||
#+end_src
|
||||
|
||||
--------------
|
||||
|
||||
← [[../README.org][Back to README]] · Next: [[file:02-inductive-types.org][Unit 2 --- Inductive Types]]
|
||||
@@ -1,34 +1,46 @@
|
||||
# Unit 2 — Inductive Types
|
||||
* Unit 2 --- Inductive Types
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-2-inductive-types
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Define inductive data types with the `inductive` keyword
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Define inductive data types with the =inductive= keyword
|
||||
- Understand constructors and pattern matching
|
||||
- Write structurally recursive functions over inductive types
|
||||
- Define `Nat`, `List`, and `Tree` from scratch
|
||||
- Define =Nat=, =List=, and =Tree= from scratch
|
||||
|
||||
## Sources
|
||||
|
||||
- *Theorem Proving in Lean 4* (TPIL), Chapter 7 "Inductive Types"
|
||||
** Sources
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: sources
|
||||
:END:
|
||||
- /Theorem Proving in Lean 4/ (TPIL), Chapter 7 "Inductive Types"
|
||||
→ https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
- *Functional Programming in Lean* (FPIL), Chapter 1
|
||||
- /Functional Programming in Lean/ (FPIL), Chapter 1
|
||||
|
||||
## Concepts
|
||||
** Concepts
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: concepts
|
||||
:END:
|
||||
| Concept | Lean syntax |
|
||||
|-------------------------------+-----------------------------------------------------------|
|
||||
| Inductive definition | =inductive Name where \| ctor1 : ... → Name= |
|
||||
| Pattern matching constructors | =match t with \| ctor1 x => ... \| ctor2 y => ...= |
|
||||
| Structural recursion | Recursion on /structurally smaller/ subterms only |
|
||||
| =deriving= clauses | =deriving Repr, BEq, DecidableEq= for auto-generated code |
|
||||
|
||||
| Concept | Lean syntax |
|
||||
|---------|------------|
|
||||
| Inductive definition | `inductive Name where \| ctor1 : ... → Name` |
|
||||
| Pattern matching constructors | `match t with \| ctor1 x => ... \| ctor2 y => ...` |
|
||||
| Structural recursion | Recursion on *structurally smaller* subterms only |
|
||||
| `deriving` clauses | `deriving Repr, BEq, DecidableEq` for auto-generated code |
|
||||
|
||||
## Exercises
|
||||
|
||||
### Exercise 2.1 — Define `Nat` from scratch
|
||||
|
||||
```lean
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
*** Exercise 2.1 --- Define =Nat= from scratch
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-2.1-define-nat-from-scratch
|
||||
:END:
|
||||
#+begin_src lean
|
||||
inductive MyNat where
|
||||
| zero : MyNat
|
||||
| succ : MyNat → MyNat
|
||||
@@ -46,11 +58,13 @@ def myMul (a b : MyNat) : MyNat :=
|
||||
|
||||
-- (c) Prove that zero is a right identity: myAdd a MyNat.zero = a
|
||||
-- (We'll do proofs properly in Unit 3, but try `by rfl` for now)
|
||||
```
|
||||
#+end_src
|
||||
|
||||
### Exercise 2.2 — Define `List α` from scratch
|
||||
|
||||
```lean
|
||||
*** Exercise 2.2 --- Define =List α= from scratch
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-2.2-define-list-α-from-scratch
|
||||
:END:
|
||||
#+begin_src lean
|
||||
inductive MyList (α : Type) where
|
||||
| nil : MyList α
|
||||
| cons : α → MyList α → MyList α
|
||||
@@ -66,11 +80,13 @@ def myAppend {α : Type} (xs ys : MyList α) : MyList α :=
|
||||
|
||||
-- (c) Prove: myLength (myAppend xs ys) = myLength xs + myLength ys
|
||||
-- (Try this after Unit 4/5)
|
||||
```
|
||||
#+end_src
|
||||
|
||||
### Exercise 2.3 — Binary trees
|
||||
|
||||
```lean
|
||||
*** Exercise 2.3 --- Binary trees
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-2.3-binary-trees
|
||||
:END:
|
||||
#+begin_src lean
|
||||
inductive BinTree (α : Type) where
|
||||
| leaf : BinTree α
|
||||
| node : BinTree α → α → BinTree α → BinTree α
|
||||
@@ -99,8 +115,8 @@ def inorder {α : Type} (t : BinTree α) : List α :=
|
||||
-- Hint: you may want a helper with min/max bounds.
|
||||
def isBST (t : BinTree Int) : Bool :=
|
||||
sorry
|
||||
```
|
||||
#+end_src
|
||||
|
||||
---
|
||||
--------------
|
||||
|
||||
← [Previous: Unit 1](01-types-and-functions.md) · Next: [Unit 3 — Propositions and Proofs](03-propositions-and-proofs.md)
|
||||
← [[file:01-types-and-functions.org][Previous: Unit 1]] · Next: [[file:03-propositions-and-proofs.org][Unit 3 --- Propositions and Proofs]]
|
||||
@@ -1,90 +0,0 @@
|
||||
# Unit 3 — Propositions and Proofs
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Understand propositions as types (Curry-Howard)
|
||||
- Write basic proofs using `example` and `theorem`
|
||||
- Use the tactic language: `intro`, `apply`, `exact`, `have`
|
||||
|
||||
## Source
|
||||
|
||||
*Theorem Proving in Lean 4* (TPIL), Chapters 2–3
|
||||
→ https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
|
||||
## Concepts
|
||||
|
||||
| Concept | Lean |
|
||||
|---------|------|
|
||||
| Proposition | `P : Prop` — a type whose terms are proofs |
|
||||
| Implication `P → Q` | Function from proofs of `P` to proofs of `Q` |
|
||||
| Conjunction `P ∧ Q` | `And.intro` (constructor), `.left` / `.right` (projections) |
|
||||
| Disjunction `P ∨ Q` | `Or.inl` / `Or.inr` |
|
||||
| Negation `¬ P` | `P → False` |
|
||||
| Tactics | `intro`, `apply`, `exact`, `have`, `assumption` |
|
||||
|
||||
## Exercises
|
||||
|
||||
### Exercise 3.1 — Implicational logic
|
||||
|
||||
```lean
|
||||
-- (a) Identity
|
||||
theorem id_prop (A : Prop) : A → A :=
|
||||
by
|
||||
intro h
|
||||
exact h
|
||||
|
||||
-- (b) Composition
|
||||
theorem compose (A B C : Prop) : (A → B) → (B → C) → (A → C) :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Currying
|
||||
theorem curry (A B C : Prop) : (A → B → C) → (A ∧ B → C) :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
|
||||
### Exercise 3.2 — Conjunction and disjunction
|
||||
|
||||
```lean
|
||||
-- (a) Swap conjuncts
|
||||
theorem and_comm (A B : Prop) : A ∧ B → B ∧ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (b) Disjunction is symmetric
|
||||
theorem or_comm (A B : Prop) : A ∨ B → B ∨ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Forward reasoning with `have`
|
||||
theorem forward_example (A B C : Prop) (h1 : A → B) (h2 : B → C) (ha : A) : C :=
|
||||
by
|
||||
have hb : B := h1 ha
|
||||
sorry
|
||||
```
|
||||
|
||||
### Exercise 3.3 — Negation
|
||||
|
||||
```lean
|
||||
-- (a) Contradiction
|
||||
theorem contrapositive (A B : Prop) : (A → B) → (¬ B → ¬ A) :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (b) Double negation introduction
|
||||
theorem double_neg_intro (A : Prop) : A → ¬ ¬ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Ex falso quodlibet
|
||||
theorem ex_falso (A : Prop) : False → A :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
← [Previous: Unit 2](02-inductive-types.md) · Next: [Unit 4 — Quantifiers and Equality](04-quantifiers-and-equality.md)
|
||||
106
tutorial-01-basics/03-propositions-and-proofs.org
Normal file
106
tutorial-01-basics/03-propositions-and-proofs.org
Normal file
@@ -0,0 +1,106 @@
|
||||
* Unit 3 --- Propositions and Proofs
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-3-propositions-and-proofs
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Understand propositions as types (Curry-Howard)
|
||||
- Write basic proofs using =example= and =theorem=
|
||||
- Use the tactic language: =intro=, =apply=, =exact=, =have=
|
||||
|
||||
** Source
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: source
|
||||
:END:
|
||||
/Theorem Proving in Lean 4/ (TPIL), Chapters 2--3
|
||||
→ https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
|
||||
** Concepts
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: concepts
|
||||
:END:
|
||||
| Concept | Lean |
|
||||
|---------------------+-------------------------------------------------------------|
|
||||
| Proposition | =P : Prop= --- a type whose terms are proofs |
|
||||
| Implication =P → Q= | Function from proofs of =P= to proofs of =Q= |
|
||||
| Conjunction =P ∧ Q= | =And.intro= (constructor), =.left= / =.right= (projections) |
|
||||
| Disjunction =P ∨ Q= | =Or.inl= / =Or.inr= |
|
||||
| Negation =¬ P= | =P → False= |
|
||||
| Tactics | =intro=, =apply=, =exact=, =have=, =assumption= |
|
||||
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
*** Exercise 3.1 --- Implicational logic
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-3.1-implicational-logic
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Identity
|
||||
theorem id_prop (A : Prop) : A → A :=
|
||||
by
|
||||
intro h
|
||||
exact h
|
||||
|
||||
-- (b) Composition
|
||||
theorem compose (A B C : Prop) : (A → B) → (B → C) → (A → C) :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Currying
|
||||
theorem curry (A B C : Prop) : (A → B → C) → (A ∧ B → C) :=
|
||||
by
|
||||
sorry
|
||||
#+end_src
|
||||
|
||||
*** Exercise 3.2 --- Conjunction and disjunction
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-3.2-conjunction-and-disjunction
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Swap conjuncts
|
||||
theorem and_comm (A B : Prop) : A ∧ B → B ∧ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (b) Disjunction is symmetric
|
||||
theorem or_comm (A B : Prop) : A ∨ B → B ∨ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Forward reasoning with `have`
|
||||
theorem forward_example (A B C : Prop) (h1 : A → B) (h2 : B → C) (ha : A) : C :=
|
||||
by
|
||||
have hb : B := h1 ha
|
||||
sorry
|
||||
#+end_src
|
||||
|
||||
*** Exercise 3.3 --- Negation
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-3.3-negation
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Contradiction
|
||||
theorem contrapositive (A B : Prop) : (A → B) → (¬ B → ¬ A) :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (b) Double negation introduction
|
||||
theorem double_neg_intro (A : Prop) : A → ¬ ¬ A :=
|
||||
by
|
||||
sorry
|
||||
|
||||
-- (c) Ex falso quodlibet
|
||||
theorem ex_falso (A : Prop) : False → A :=
|
||||
by
|
||||
sorry
|
||||
#+end_src
|
||||
|
||||
--------------
|
||||
|
||||
← [[file:02-inductive-types.org][Previous: Unit 2]] · Next: [[file:04-quantifiers-and-equality.org][Unit 4 --- Quantifiers and Equality]]
|
||||
@@ -1,32 +1,44 @@
|
||||
# Unit 4 — Quantifiers and Equality
|
||||
* Unit 4 --- Quantifiers and Equality
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-4-quantifiers-and-equality
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Use `∀` (forall) and `∃` (exists) quantifiers
|
||||
- Manipulate equality with `rfl`, `rw`, `calc`
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Use =∀= (forall) and =∃= (exists) quantifiers
|
||||
- Manipulate equality with =rfl=, =rw=, =calc=
|
||||
- Combine quantifiers with logical connectives
|
||||
|
||||
## Source
|
||||
|
||||
*Theorem Proving in Lean 4* (TPIL), Chapter 4
|
||||
** Source
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: source
|
||||
:END:
|
||||
/Theorem Proving in Lean 4/ (TPIL), Chapter 4
|
||||
→ https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
|
||||
## Concepts
|
||||
** Concepts
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: concepts
|
||||
:END:
|
||||
| Concept | Lean |
|
||||
|------------------------+------------------------------------------------------|
|
||||
| Universal =∀ x, P x= | =intro x= to prove; =h x= or =apply h= to use |
|
||||
| Existential =∃ x, P x= | =⟨x, h⟩= to prove; =rcases h with ⟨x, hx⟩= to use |
|
||||
| Equality =a = b= | =rfl= when definitional; =rw [h]= when propositional |
|
||||
| =calc= block | Chain equalities: =calc a = b := h1; _ = c := h2= |
|
||||
|
||||
| Concept | Lean |
|
||||
|---------|------|
|
||||
| Universal `∀ x, P x` | `intro x` to prove; `h x` or `apply h` to use |
|
||||
| Existential `∃ x, P x` | `⟨x, h⟩` to prove; `rcases h with ⟨x, hx⟩` to use |
|
||||
| Equality `a = b` | `rfl` when definitional; `rw [h]` when propositional |
|
||||
| `calc` block | Chain equalities: `calc a = b := h1; _ = c := h2` |
|
||||
|
||||
## Exercises
|
||||
|
||||
### Exercise 4.1 — Universal quantifier
|
||||
|
||||
```lean
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
*** Exercise 4.1 --- Universal quantifier
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-4.1-universal-quantifier
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Prove a simple forall
|
||||
theorem all_imp (P Q : Nat → Prop) (h : ∀ n, P n → Q n) (x : Nat) (hp : P x) : Q x :=
|
||||
by
|
||||
@@ -46,11 +58,13 @@ def Even (n : Nat) : Prop := ∃ k, n = 2 * k
|
||||
theorem all_even_implies_42 : (∀ n, Even n) → Even 42 :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
#+end_src
|
||||
|
||||
### Exercise 4.2 — Existential quantifier
|
||||
|
||||
```lean
|
||||
*** Exercise 4.2 --- Existential quantifier
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-4.2-existential-quantifier
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Prove existence
|
||||
theorem exists_square (a : Nat) : ∃ n, n = a * a :=
|
||||
by
|
||||
@@ -67,11 +81,13 @@ theorem exists_or_distrib (P Q : Nat → Prop) : (∃ n, P n ∨ Q n) ↔ (∃ n
|
||||
theorem even_and_gt10_implies_gt5 : (∃ n, Even n ∧ n > 10) → (∃ n, n > 5) :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
#+end_src
|
||||
|
||||
### Exercise 4.3 — Equality and rewriting
|
||||
|
||||
```lean
|
||||
*** Exercise 4.3 --- Equality and rewriting
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercise-4.3-equality-and-rewriting
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- (a) Prove symmetry and transitivity of equality (without using `symm`/`trans`)
|
||||
theorem eq_symm {α : Type} (a b : α) (h : a = b) : b = a :=
|
||||
by
|
||||
@@ -90,8 +106,8 @@ theorem calc_example (a b c d : Nat) (h1 : a = b) (h2 : b = c + 1) (h3 : c + 1 =
|
||||
theorem rewrite_example (a b : Nat) (h : a = b + 1) : a * 2 = (b + 1) * 2 :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
#+end_src
|
||||
|
||||
---
|
||||
--------------
|
||||
|
||||
← [Previous: Unit 3](03-propositions-and-proofs.md) · Next: [Unit 5 — Advanced Tactics](05-advanced-tactics.md)
|
||||
← [[file:03-propositions-and-proofs.org][Previous: Unit 3]] · Next: [[file:05-advanced-tactics.org][Unit 5 --- Advanced Tactics]]
|
||||
@@ -1,52 +0,0 @@
|
||||
# Unit 5 — Advanced Tactics
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Prove properties by induction with `induction`
|
||||
- Use `simp`, `ring`, `omega` for automation
|
||||
- Use `rcases` and `obtain` for case analysis
|
||||
- Work with `Nat` arithmetic proofs
|
||||
|
||||
## Sources
|
||||
|
||||
- TPIL Chapters 5–6 → https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
- *Mathematics in Lean* (MiL), Chapter 1 → https://leanprover-community.github.io/mathematics_in_lean/
|
||||
|
||||
## Exercises
|
||||
|
||||
```lean
|
||||
open Nat
|
||||
|
||||
-- 5.1 — Induction on naturals
|
||||
theorem add_assoc (a b c : Nat) : (a + b) + c = a + (b + c) := by
|
||||
sorry
|
||||
|
||||
theorem add_comm (a b : Nat) : a + b = b + a := by
|
||||
sorry
|
||||
|
||||
theorem mul_comm (a b : Nat) : a * b = b * a := by
|
||||
sorry
|
||||
|
||||
theorem zero_mul (n : Nat) : 0 * n = 0 := by
|
||||
sorry
|
||||
|
||||
-- 5.2 — Induction on lists
|
||||
theorem reverse_reverse (xs : List α) : xs.reverse.reverse = xs := by
|
||||
sorry
|
||||
|
||||
theorem length_append (xs ys : List α) : (xs ++ ys).length = xs.length + ys.length := by
|
||||
sorry
|
||||
|
||||
theorem map_id (xs : List α) : xs.map id = xs := by
|
||||
sorry
|
||||
|
||||
-- 5.3 — Using `simp` and `ring`
|
||||
theorem square_sum (a b : Nat) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
|
||||
sorry
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
← [Previous: Unit 4](04-quantifiers-and-equality.md) · Next: [Unit 6 — Structures and Type Classes](06-structures-type-classes.md)
|
||||
60
tutorial-01-basics/05-advanced-tactics.org
Normal file
60
tutorial-01-basics/05-advanced-tactics.org
Normal file
@@ -0,0 +1,60 @@
|
||||
* Unit 5 --- Advanced Tactics
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-5-advanced-tactics
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Prove properties by induction with =induction=
|
||||
- Use =simp=, =ring=, =omega= for automation
|
||||
- Use =rcases= and =obtain= for case analysis
|
||||
- Work with =Nat= arithmetic proofs
|
||||
|
||||
** Sources
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: sources
|
||||
:END:
|
||||
- TPIL Chapters 5--6 → https://leanprover.github.io/theorem_proving_in_lean4/
|
||||
- /Mathematics in Lean/ (MiL), Chapter 1 → https://leanprover-community.github.io/mathematics_in_lean/
|
||||
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
#+begin_src lean
|
||||
open Nat
|
||||
|
||||
-- 5.1 — Induction on naturals
|
||||
theorem add_assoc (a b c : Nat) : (a + b) + c = a + (b + c) := by
|
||||
sorry
|
||||
|
||||
theorem add_comm (a b : Nat) : a + b = b + a := by
|
||||
sorry
|
||||
|
||||
theorem mul_comm (a b : Nat) : a * b = b * a := by
|
||||
sorry
|
||||
|
||||
theorem zero_mul (n : Nat) : 0 * n = 0 := by
|
||||
sorry
|
||||
|
||||
-- 5.2 — Induction on lists
|
||||
theorem reverse_reverse (xs : List α) : xs.reverse.reverse = xs := by
|
||||
sorry
|
||||
|
||||
theorem length_append (xs ys : List α) : (xs ++ ys).length = xs.length + ys.length := by
|
||||
sorry
|
||||
|
||||
theorem map_id (xs : List α) : xs.map id = xs := by
|
||||
sorry
|
||||
|
||||
-- 5.3 — Using `simp` and `ring`
|
||||
theorem square_sum (a b : Nat) : (a + b) ^ 2 = a ^ 2 + 2 * a * b + b ^ 2 := by
|
||||
sorry
|
||||
#+end_src
|
||||
|
||||
--------------
|
||||
|
||||
← [[file:04-quantifiers-and-equality.org][Previous: Unit 4]] · Next: [[file:06-structures-type-classes.org][Unit 6 --- Structures and Type Classes]]
|
||||
@@ -1,22 +1,30 @@
|
||||
# Unit 6 — Structures and Type Classes
|
||||
* Unit 6 --- Structures and Type Classes
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-6-structures-and-type-classes
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
|
||||
- Define `structure` for records with named fields
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Define =structure= for records with named fields
|
||||
- Understand type classes as structures + instance resolution
|
||||
- Use `deriving` for `Repr`, `BEq`, `Inhabited`
|
||||
- Write monadic code with `do` notation
|
||||
|
||||
## Sources
|
||||
- Use =deriving= for =Repr=, =BEq=, =Inhabited=
|
||||
- Write monadic code with =do= notation
|
||||
|
||||
** Sources
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: sources
|
||||
:END:
|
||||
- TPIL Chapter 9 (Structures), Chapter 10 (Type Classes)
|
||||
- FPIL Chapters 3–5 (overloading, monads)
|
||||
- FPIL Chapters 3--5 (overloading, monads)
|
||||
|
||||
## Exercises
|
||||
|
||||
```lean
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- 6.1 — Structures
|
||||
structure Point where
|
||||
x : Float
|
||||
@@ -61,8 +69,8 @@ def compute (x y z : Nat) : Option Nat :=
|
||||
|
||||
#eval compute 10 2 3 -- expected: some 2
|
||||
#eval compute 10 0 3 -- expected: none
|
||||
```
|
||||
#+end_src
|
||||
|
||||
---
|
||||
--------------
|
||||
|
||||
← [Previous: Unit 5](05-advanced-tactics.md) · Next: [Unit 7 — Dependent Types](07-dependent-types.md)
|
||||
← [[file:05-advanced-tactics.org][Previous: Unit 5]] · Next: [[file:07-dependent-types.org][Unit 7 --- Dependent Types]]
|
||||
@@ -1,33 +1,43 @@
|
||||
# Unit 7 — Dependent Types
|
||||
|
||||
**Tutorial 1: Lean 4 Fundamentals** · [← Back to README](../README.md)
|
||||
|
||||
## Goals
|
||||
* Unit 7 --- Dependent Types
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: unit-7-dependent-types
|
||||
:END:
|
||||
*Tutorial 1: Lean 4 Fundamentals* · [[../README.org][← Back to README]]
|
||||
|
||||
** Goals
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: goals
|
||||
:END:
|
||||
- Understand types that depend on values
|
||||
- Work with `Fin n` (numbers < n), `Vector α n` (fixed-length lists)
|
||||
- Work with =Fin n= (numbers < n), =Vector α n= (fixed-length lists)
|
||||
- Write functions whose return type depends on input
|
||||
- See why dependent types matter for PL semantics
|
||||
|
||||
## Sources
|
||||
|
||||
** Sources
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: sources
|
||||
:END:
|
||||
- TPIL Chapter 2 "Dependent Type Theory"
|
||||
- FPIL Chapter 7 "Programming with Dependent Types"
|
||||
- Henson 2025: "Beginner Resources for Formalizing Lambda Calculi"
|
||||
|
||||
## Concepts
|
||||
** Concepts
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: concepts
|
||||
:END:
|
||||
| Concept | Lean |
|
||||
|---------------------------+-----------------------------------------------------|
|
||||
| Pi type =(x : α) → β x= | Function where return type depends on argument |
|
||||
| Sigma type =Σ x : α, β x= | Dependent pair: value + property |
|
||||
| =Fin n= | Natural numbers =< n= |
|
||||
| =Vector α n= | Lists with length tracked in the type |
|
||||
| =h : a = b → ...= | Equality can rewrite types (substitution principle) |
|
||||
|
||||
| Concept | Lean |
|
||||
|---------|------|
|
||||
| Pi type `(x : α) → β x` | Function where return type depends on argument |
|
||||
| Sigma type `Σ x : α, β x` | Dependent pair: value + property |
|
||||
| `Fin n` | Natural numbers `< n` |
|
||||
| `Vector α n` | Lists with length tracked in the type |
|
||||
| `h : a = b → ...` | Equality can rewrite types (substitution principle) |
|
||||
|
||||
## Exercises
|
||||
|
||||
```lean
|
||||
** Exercises
|
||||
:PROPERTIES:
|
||||
:CUSTOM_ID: exercises
|
||||
:END:
|
||||
#+begin_src lean
|
||||
-- 7.1 — Fin n
|
||||
-- Define a safe nth function for lists that *cannot* fail at runtime
|
||||
def safeNth {α : Type} (xs : List α) (i : Fin xs.length) : α :=
|
||||
@@ -70,8 +80,8 @@ inductive HasType : Expr → Ty → Prop where
|
||||
theorem isZero_returns_bool (e : Expr) : HasType (Expr.isZero e) Ty.nat → HasType e Ty.nat :=
|
||||
by
|
||||
sorry
|
||||
```
|
||||
#+end_src
|
||||
|
||||
---
|
||||
--------------
|
||||
|
||||
← [Previous: Unit 6](06-structures-type-classes.md) · [← Tutorial 1 Index](./) · Next: [Tutorial 2 — Unit 8](../tutorial-02-semantics/08-syntax-representation.md)
|
||||
← [[file:06-structures-type-classes.org][Previous: Unit 6]] · [[./][← Tutorial 1 Index]] · Next: [[../tutorial-02-semantics/08-syntax-representation.org][Tutorial 2 --- Unit 8]]
|
||||
Reference in New Issue
Block a user