···1414#check E = m * c**2
1515#check E = m * v**2 / 2 + m * g * h
1616-- #check E = m * c**3 -- fails to typecheck
1717+-- #check m + v -- fails to typecheck
17181819example (hv : v = 60 • kilo meter / hour) : (StandardUnit _).in v = 50/(3:ℝ) := by
1920 simp [←Scalar.val_inj, hour, minute, kilo, meter, second, hv]
+8-2
analysis/Analysis/Misc/UnitsSystemExamples.lean
···44open UnitsSystem
55variable [UnitsSystem]
6677--- Many algebraic identities involving `Scalar` can be established by first using `←toFormal_inj`
88--- to coerce to `Formal`, using `simp` to push coercions inside, then appealing to `ring`. We give some examples below.
77+/- Many algebraic identities involving `Scalar` can be established by first using `simp [←toFormal_inj]` to coerce to `Formal` and push coercions inside, then appealing to `ring`. We give some examples below.
88+99+Alternatively, one can "work in coordinates" by using `simp [←val_inj]` in place of `simp [←toFormal_inj]`.
1010+-/
9111012/-- A cast is needed here because `(d₁+d₂)+d₃` is not definitionally equal to `d₁+(d₂+d₃)`. -/
1113theorem UnitsSystem.Scalar.hMul_assoc {d₁ d₂ d₃:Dimensions} (a:Scalar d₁) (b:Scalar d₂) (c:Scalar d₃):
···2325/-- A cast is needed here because `2 • d` is not definitionally equal to `d+d`. -/
2426theorem UnitsSystem.Scalar.sq_add {d:Dimensions} (a b:Scalar d) : (a+b)**2 = a**2 + (2 • a * b).cast + b**2 := by
2527 simp [←toFormal_inj]; ring
2828+2929+/-- An alternate proof based on working in coordinates-/
3030+theorem UnitsSystem.Scalar.sq_add' {d:Dimensions} (a b:Scalar d) : (a+b)**2 = a**2 + (2 • a * b).cast + b**2 := by
3131+ simp [←val_inj]; ring
+9-9
analysis/Analysis/Section_2_1.lean
···11import Mathlib.Tactic
2233/-!
44-# Analysis I, Section 2.1
44+# Analysis I, Section 2.1: The Peano Axioms
5566This file is a translation of Section 2.1 of Analysis I to Lean 4. All numbering refers to the
77original text.
···1818 Chapter2 namespace. (In the book, the natural numbers are treated in a purely axiomatic
1919 fashion, as a type that obeys the Peano axioms; but here we take advantage of Lean's native
2020 inductive types to explicitly construct a version of the natural numbers that obey those
2121- axioms. One could also proceed more axiomatically, as is done in Section 3 for set theory, but
2222- we leave this as an exercise for the reader.)
2323-- Establishment of the Peano axioms for `Chapter2.Nat`
2424-- Recursive definitions for `Chapter2.Nat`
2121+ axioms. One could also proceed more axiomatically, as is done in Section 3 for set theory:
2222+ see the epilogue to this chapter.)
2323+- Establishment of the Peano axioms for `Chapter2.Nat`.
2424+- Recursive definitions for `Chapter2.Nat`.
25252626-Note: at the end of this Chapter, the `Chapter2.Nat` class will be deprecated in favor of the
2626+Note: at the end of this chapter, the `Chapter2.Nat` class will be deprecated in favor of the
2727standard Mathlib class `_root_.Nat`, or `ℕ`. However, we will develop the properties of
2828`Chapter2.Nat` "by hand" in the next few sections for pedagogical purposes.
2929···52525353/--
5454 Definition 2.1.3 (Definition of the numerals 0, 1, 2, etc.). Note: to avoid ambiguity, one may
5555- need to use explicit casts such as (0:Nat), (1:Nat), etc. to refer to this Chapter's version of
5555+ need to use explicit casts such as (0:Nat), (1:Nat), etc. to refer to this chapter's version of
5656 the natural numbers.
5757-/
5858instance Nat.instOfNat {n:_root_.Nat} : OfNat Nat n where
···9090-/
9191theorem Nat.succ_cancel {n m:Nat} (hnm: n++ = m++) : n = m := by
9292 injection hnm
9393-9393+9494/--
9595 Axiom 2.4 (Different natural numbers have different successors).
9696 Compare with Mathlib's `Nat.succ_ne_succ`.
···116116 decide
117117118118/--
119119- Axiom 2.5 (principle of mathematical induction). The `induction` (or `induction'`) tactic in
119119+ Axiom 2.5 (Principle of mathematical induction). The `induction` (or `induction'`) tactic in
120120 Mathlib serves as a substitute for this axiom.
121121-/
122122theorem Nat.induction (P : Nat → Prop) (hbase : P 0) (hind : ∀ n, P n → P (n++)) :
+67-49
analysis/Analysis/Section_2_2.lean
···22import Analysis.Section_2_1
3344/-!
55-# Analysis I, Section 2.2
55+# Analysis I, Section 2.2: Addition
6677-This file is a translation of Section 2.2 of Analysis I to Lean 4.
88-All numbering refers to the original text.
77+This file is a translation of Section 2.2 of Analysis I to Lean 4. All numbering refers to the
88+original text.
991010I have attempted to make the translation as faithful a paraphrasing as possible of the original
1111text. When there is a choice between a more idiomatic Lean solution and a more faithful
···15151616Main constructions and results of this section:
17171818-- Definition of addition and order for the "Chapter 2" natural numbers, `Chapter2.Nat`
1919-- Establishment of basic properties of addition and order
1818+- Definition of addition and order for the "Chapter 2" natural numbers, `Chapter2.Nat`.
1919+- Establishment of basic properties of addition and order.
20202121Note: at the end of this chapter, the `Chapter2.Nat` class will be deprecated in favor of the
2222standard Mathlib class `_root_.Nat`, or `ℕ`. However, we will develop the properties of
···3333instance Nat.instAdd : Add Nat where
3434 add := add
35353636-/-- Compare with Mathlib's `Nat.zero_add`-/
3636+/-- Compare with Mathlib's `Nat.zero_add`. -/
3737@[simp]
3838theorem Nat.zero_add (m: Nat) : 0 + m = m := recurse_zero (fun _ sum ↦ sum++) _
39394040-/-- Compare with Mathlib's `Nat.succ_add` -/
4040+/-- Compare with Mathlib's `Nat.succ_add`. -/
4141theorem Nat.succ_add (n m: Nat) : n++ + m = (n+m)++ := by rfl
42424343-/-- Compare with Mathlib's `Nat.one_add` -/
4343+/-- Compare with Mathlib's `Nat.one_add`. -/
4444theorem Nat.one_add (m:Nat) : 1 + m = m++ := by
4545 rw [show 1 = 0++ from rfl, succ_add, zero_add]
4646···5050example : (2:Nat) + 3 = 5 := by
5151 rw [Nat.two_add, show 3++=4 from rfl, show 4++=5 from rfl]
52525353--- sum of two natural numbers is again a natural number
5353+-- The sum of two natural numbers is again a natural number.
5454#check (fun (n m:Nat) ↦ n + m)
55555656-/-- Lemma 2.2.2 (n + 0 = n). Compare with Mathlib's `Nat.add_zero` -/
5656+/-- Lemma 2.2.2 (n + 0 = n). Compare with Mathlib's `Nat.add_zero`. -/
5757@[simp]
5858lemma Nat.add_zero (n:Nat) : n + 0 = n := by
5959- -- this proof is written to follow the structure of the original text.
5959+ -- This proof is written to follow the structure of the original text.
6060 revert n; apply induction
6161 . exact zero_add 0
6262 intro n ih
···6464 (n++) + 0 = (n + 0)++ := by rfl
6565 _ = n++ := by rw [ih]
66666767-/-- Lemma 2.2.3 (n+(m++) = (n+m)++). Compare with Mathlib's `Nat.add_succ` -/
6767+/-- Lemma 2.2.3 (n+(m++) = (n+m)++). Compare with Mathlib's `Nat.add_succ`. -/
6868lemma Nat.add_succ (n m:Nat) : n + (m++) = (n + m)++ := by
6969 -- this proof is written to follow the structure of the original text.
7070 revert n; apply induction
···8888 rw [add_succ, ih]
89899090/-- Proposition 2.2.5 (Addition is associative) / Exercise 2.2.1
9191- Compare with Mathlib's `Nat.add_assoc` -/
9191+ Compare with Mathlib's `Nat.add_assoc`. -/
9292theorem Nat.add_assoc (a b c:Nat) : (a + b) + c = a + (b + c) := by
9393 sorry
94949595-/-- Proposition 2.2.6 (Cancellation law)
9696- Compare with Mathlib's `Nat.add_left_cancel` -/
9595+/-- Proposition 2.2.6 (Cancellation law).
9696+ Compare with Mathlib's `Nat.add_left_cancel`. -/
9797theorem Nat.add_left_cancel (a b c:Nat) (habc: a + b = a + c) : b = c := by
9898- -- this proof is written to follow the structure of the original text.
9898+ -- This proof is written to follow the structure of the original text.
9999 revert a; apply induction
100100 . intro hbc
101101 rwa [zero_add, zero_add] at hbc
···106106 exact ih hbc
107107108108109109-/-- (Not from textbook) Nat can be given the structure of a commutative additive monoid. -/
109109+/-- (Not from textbook) Nat can be given the structure of a commutative additive monoid.
110110+This permits tactics such as `abel` to apply to the Chapter 2 natural numbers. -/
110111instance Nat.addCommMonoid : AddCommMonoid Nat where
111112 add_assoc := add_assoc
112113 add_comm := add_comm
···114115 add_zero := add_zero
115116 nsmul := nsmulRec
116117118118+/-- This illustration of the `abel` tactic is not from the
119119+ textbook. -/
120120+example (a b c d:ℕ) : (a+b)+(c+0+d) = (b+c)+(d+a) := by abel
121121+117122/-- Definition 2.2.7 (Positive natural numbers).-/
118123def Nat.IsPos (n:Nat) : Prop := n ≠ 0
119124120125theorem Nat.isPos_iff (n:Nat) : n.IsPos ↔ n ≠ 0 := by rfl
121126122127/-- Proposition 2.2.8 (positive plus natural number is positive).
123123- Compare with Mathlib's `Nat.add_pos_left` -/
128128+ Compare with Mathlib's `Nat.add_pos_left`. -/
124129theorem Nat.add_pos_left {a:Nat} (b:Nat) (ha: a.IsPos) : (a + b).IsPos := by
125125- -- this proof is written to follow the structure of the original text.
130130+ -- This proof is written to follow the structure of the original text.
126131 revert b; apply induction
127132 . rwa [add_zero]
128133 intro b hab
···130135 have : (a+b)++ ≠ 0 := succ_ne _
131136 exact this
132137133133-/-- Compare with Mathlib's `Nat.add_pos_right` -/
138138+/-- Compare with Mathlib's `Nat.add_pos_right`. -/
134139theorem Nat.add_pos_right {a:Nat} (b:Nat) (ha: a.IsPos) : (b + a).IsPos := by
135140 rw [add_comm]
136141 exact add_pos_left _ ha
137142138143/-- Corollary 2.2.9 (if sum vanishes, then summands vanish).
139139- Compare with Mathlib's `Nat.add_eq_zero` -/
144144+ Compare with Mathlib's `Nat.add_eq_zero`. -/
140145theorem Nat.add_eq_zero (a b:Nat) (hab: a + b = 0) : a = 0 ∧ b = 0 := by
141141- -- this proof is written to follow the structure of the original text.
146146+ -- This proof is written to follow the structure of the original text.
142147 by_contra h
143148 simp only [not_and_or, ←ne_eq] at h
144149 rcases h with ha | hb
···150155 contradiction
151156152157/-
153153-The following API for ∃! may be useful for the next problem. Also, the `obtain` tactic is useful
154154-for extracting witnesses from existential statements; for instance, `obtain ⟨ x, hx ⟩ := h`
158158+The API in `Tools/ExistsUnique.Lean`, and the method `existsUnique_of_exists_of_unique` in
159159+particular, may be useful for the next problem. Also, the `obtain` tactic is
160160+useful for extracting witnesses from existential statements; for instance, `obtain ⟨ x, hx ⟩ := h`
155161extracts a witness `x` and a proof `hx : P x` of the property from a hypothesis `h : ∃ x, P x`.
156162-/
157163158164#check existsUnique_of_exists_of_unique
159159-#check ExistsUnique.exists
160160-#check ExistsUnique.unique
161165162166/-- Lemma 2.2.10 (unique predecessor) / Exercise 2.2.2 -/
163167lemma Nat.uniq_succ_eq (a:Nat) (ha: a.IsPos) : ∃! b, b++ = a := by
164168 sorry
165169166166-/-- Definition 2.2.11 (Ordering of the natural numbers)
170170+/-- Definition 2.2.11 (Ordering of the natural numbers).
167171 This defines the `≤` operation on the natural numbers. -/
168172instance Nat.instLE : LE Nat where
169173 le n m := ∃ a:Nat, m = n + a
170174171171-/-- Definition 2.2.11 (Ordering of the natural numbers)
175175+/-- Definition 2.2.11 (Ordering of the natural numbers).
172176 This defines the `<` notation on the natural numbers. -/
173177instance Nat.instLT : LT Nat where
174178 lt n m := n ≤ m ∧ n ≠ m
···177181178182lemma Nat.lt_iff (n m:Nat) : n < m ↔ (∃ a:Nat, m = n + a) ∧ n ≠ m := by rfl
179183180180-/-- Compare with Mathlib's `ge_iff_le` -/
184184+/-- Compare with Mathlib's `ge_iff_le`. -/
181185lemma Nat.ge_iff_le (n m:Nat) : n ≥ m ↔ m ≤ n := by rfl
182186183183-/-- Compare with Mathlib's `gt_iff_lt` -/
187187+/-- Compare with Mathlib's `gt_iff_lt`. -/
184188lemma Nat.gt_iff_lt (n m:Nat) : n > m ↔ m < n := by rfl
185189186186-/-- Compare with Mathlib's `Nat.le_of_lt` -/
190190+/-- Compare with Mathlib's `Nat.le_of_lt`. -/
187191lemma Nat.le_of_lt {n m:Nat} (hnm: n < m) : n ≤ m := hnm.1
188192189189-/-- Compare with Mathlib's `Nat.le_iff_lt_or_eq` -/
193193+/-- Compare with Mathlib's `Nat.le_iff_lt_or_eq`. -/
190194lemma Nat.le_iff_lt_or_eq (n m:Nat) : n ≤ m ↔ n < m ∨ n = m := by
191195 rw [Nat.le_iff, Nat.lt_iff]
192196 by_cases h : n = m
···203207 use 3
204208 decide
205209206206-/-- Compare with Mathlib's `Nat.lt_succ_self`-/
210210+/-- Compare with Mathlib's `Nat.lt_succ_self`. -/
207211theorem Nat.succ_gt_self (n:Nat) : n++ > n := by
208212 sorry
209213210214/-- Proposition 2.2.12 (Basic properties of order for natural numbers) / Exercise 2.2.3
211215212212-(a) (Order is reflexive). Compare with Mathlib's `Nat.le_refl`-/
216216+(a) (Order is reflexive). Compare with Mathlib's `Nat.le_refl`.-/
213217theorem Nat.ge_refl (a:Nat) : a ≥ a := by
214218 sorry
215219216220/-- (b) (Order is transitive). The `obtain` tactic will be useful here.
217217- Compare with Mathlib's `Nat.le_trans` -/
221221+ Compare with Mathlib's `Nat.le_trans`. -/
218222theorem Nat.ge_trans {a b c:Nat} (hab: a ≥ b) (hbc: b ≥ c) : a ≥ c := by
219223 sorry
220224221221-/-- (c) (Order is anti-symmetric). Compare with Mathlib's `Nat.le_antisymm` -/
225225+/-- (c) (Order is anti-symmetric). Compare with Mathlib's `Nat.le_antisymm`. -/
222226theorem Nat.ge_antisymm {a b:Nat} (hab: a ≥ b) (hba: b ≥ a) : a = b := by
223227 sorry
224228225225-/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_right` -/
229229+/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_right`. -/
226230theorem Nat.add_ge_add_right (a b c:Nat) : a ≥ b ↔ a + c ≥ b + c := by
227231 sorry
228232229229-/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_left` -/
233233+/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_left`. -/
230234theorem Nat.add_ge_add_left (a b c:Nat) : a ≥ b ↔ c + a ≥ c + b := by
231235 simp only [add_comm]
232236 exact add_ge_add_right _ _ _
233237234234-/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_right` -/
238238+/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_right`. -/
235239theorem Nat.add_le_add_right (a b c:Nat) : a ≤ b ↔ a + c ≤ b + c := add_ge_add_right _ _ _
236240237237-/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_left` -/
241241+/-- (d) (Addition preserves order). Compare with Mathlib's `Nat.add_le_add_left`. -/
238242theorem Nat.add_le_add_left (a b c:Nat) : a ≤ b ↔ c + a ≤ c + b := add_ge_add_left _ _ _
239243240240-/-- (e) a < b iff a++ ≤ b. Compare with Mathlib's `Nat.succ_le_iff` -/
244244+/-- (e) a < b iff a++ ≤ b. Compare with Mathlib's `Nat.succ_le_iff`. -/
241245theorem Nat.lt_iff_succ_le (a b:Nat) : a < b ↔ a++ ≤ b := by
242246 sorry
243247···262266263267264268/-- Proposition 2.2.13 (Trichotomy of order for natural numbers) / Exercise 2.2.4
265265- Compare with Mathlib's `trichotomous` -/
269269+ Compare with Mathlib's `trichotomous`. -/
266270theorem Nat.trichotomous (a b:Nat) : a < b ∨ a = b ∨ a > b := by
267267- -- this proof is written to follow the structure of the original text.
271271+ -- This proof is written to follow the structure of the original text.
268272 revert a; apply induction
269273 . have why : 0 ≤ b := by
270274 sorry
···287291 followed by `exact Classical.decRel _`, but this would make this definition (as well as some
288292 instances below) noncomputable.
289293290290- Compare with Mathlib's `Nat.decLe`
294294+ Compare with Mathlib's `Nat.decLe`.
291295-/
292296def Nat.decLe : (a b : Nat) → Decidable (a ≤ b)
293297 | 0, b => by
···310314instance Nat.decidableRel : DecidableRel (· ≤ · : Nat → Nat → Prop) := Nat.decLe
311315312316313313-/-- (Not from textbook) Nat has the structure of a linear ordering. -/
317317+/-- (Not from textbook) Nat has the structure of a linear ordering. This allows for tactics
318318+such as `order` to be applicable to the Chapter 2 natural numbers. -/
314319instance Nat.linearOrder : LinearOrder Nat where
315320 le_refl := ge_refl
316321 le_trans a b c hab hbc := ge_trans hbc hab
···319324 le_total := sorry
320325 toDecidableLE := decidableRel
321326322322-/-- (Not from textbook) Nat has the structure of an ordered monoid. -/
327327+/-- This illustration of the `order` tactic is not from the
328328+ textbook. -/
329329+example (a b c d:ℕ) (hab: a ≤ b) (hbc: b ≤ c) (hcd: c ≤ d)
330330+ (hda: d ≤ a) : a = c := by order
331331+332332+/-- (Not from textbook) Nat has the structure of an ordered monoid. This allows for tactics
333333+such as `gcongr` to be applicable to the Chapter 2 natural numbers. -/
323334instance Nat.isOrderedAddMonoid : IsOrderedAddMonoid Nat where
324335 add_le_add_left := by
325336 intro a b hab c
326337 exact (add_le_add_left a b c).mp hab
327338339339+/-- This illustration of the `gcongr` tactic is not from the
340340+ textbook. -/
341341+example (a b c d e:ℕ) (hab: a ≤ b) (hbc: b < c) (hde: d ≤ e) :
342342+ a+d < c + e := by
343343+ gcongr
344344+ order
345345+328346/-- Proposition 2.2.14 (Strong principle of induction) / Exercise 2.2.5
329329- Compare with Mathlib's `Nat.strong_induction_on`
347347+ Compare with Mathlib's `Nat.strong_induction_on`.
330348-/
331349theorem Nat.strong_induction {m₀:Nat} {P: Nat → Prop}
332350 (hind: ∀ m, m ≥ m₀ → (∀ m', m₀ ≤ m' ∧ m' < m → P m') → P m) :
···334352 sorry
335353336354/-- Exercise 2.2.6 (backwards induction)
337337- Compare with Mathlib's `Nat.decreasingInduction` -/
355355+ Compare with Mathlib's `Nat.decreasingInduction`. -/
338356theorem Nat.backwards_induction {n:Nat} {P: Nat → Prop}
339357 (hind: ∀ m, P (m++) → P m) (hn: P n) :
340358 ∀ m, m ≤ n → P m := by
341359 sorry
342360343361/-- Exercise 2.2.7 (induction from a starting point)
344344- Compare with Mathlib's `Nat.le_induction` -/
362362+ Compare with Mathlib's `Nat.le_induction`. -/
345363theorem Nat.induction_from {n:Nat} {P: Nat → Prop} (hind: ∀ m, P m → P (m++)) :
346364 P n → ∀ m, m ≥ n → P m := by
347365 sorry
+9-6
analysis/Analysis/Section_2_3.lean
···22import Analysis.Section_2_2
3344/-!
55-# Analysis I, Section 2.3
55+# Analysis I, Section 2.3: Multiplication
6677-This file is a translation of Section 2.3 of Analysis I to Lean 4.
88-All numbering refers to the original text.
77+This file is a translation of Section 2.3 of Analysis I to Lean 4. All numbering refers to the
88+original text.
991010I have attempted to make the translation as faithful a paraphrasing as possible of the original
1111text. When there is a choice between a more idiomatic Lean solution and a more faithful
···1616Main constructions and results of this section:
17171818- Definition of multiplication and exponentiation for the "Chapter 2" natural numbers,
1919- `Chapter2.Nat`
1919+ `Chapter2.Nat`.
20202121Note: at the end of this chapter, the `Chapter2.Nat` class will be deprecated in favor of the
2222standard Mathlib class `_root_.Nat`, or `ℕ`. However, we will develop the properties of
···2828/-- Definition 2.3.1 (Multiplication of natural numbers) -/
2929abbrev Nat.mul (n m : Nat) : Nat := Nat.recurse (fun _ prod ↦ prod + m) 0 n
30303131+/-- This instance allows for the `*` notation to be used for natural number multiplication. -/
3132instance Nat.instMul : Mul Nat where
3233 mul := mul
3334···6566lemma Nat.pos_mul_pos {n m: Nat} (h₁: n.IsPos) (h₂: m.IsPos) : (n * m).IsPos := by
6667 sorry
67686868-/-- Lemma 2.3.3 (Positive natural numbers have no zero divisors) / Exercise 2.3.2. Compare with Mathlib's Nat.mul_eq_zero. -/
6969+/-- Lemma 2.3.3 (Positive natural numbers have no zero divisors) / Exercise 2.3.2.
7070+ Compare with Mathlib's `Nat.mul_eq_zero`. -/
6971lemma Nat.mul_eq_zero (n m: Nat) : n * m = 0 ↔ n = 0 ∨ m = 0 := by
7072 sorry
7173···8789theorem Nat.mul_assoc (a b c: Nat) : (a * b) * c = a * (b * c) := by
8890 sorry
89919090-/-- (Not from textbook) Nat is a commutative semiring. -/
9292+/-- (Not from textbook) Nat is a commutative semiring.
9393+ This allows tactics such as `ring` to apply to the Chapter 2 natural numbers. -/
9194instance Nat.instCommSemiring : CommSemiring Nat where
9295 left_distrib := mul_add
9396 right_distrib := add_mul