Add length-related theorems and getters.

finite-set-exercises
Joshua Potter 2023-02-21 07:40:36 -07:00
parent 89f22ea1b8
commit 9a50ea5a78
1 changed files with 61 additions and 24 deletions

View File

@ -3,6 +3,9 @@
1. Enderton, Herbert B. A Mathematical Introduction to Logic. 2nd ed. San Diego: 1. Enderton, Herbert B. A Mathematical Introduction to Logic. 2nd ed. San Diego:
Harcourt/Academic Press, 2001. Harcourt/Academic Press, 2001.
2. Axler, Sheldon. Linear Algebra Done Right. Undergraduate Texts in
Mathematics. Cham: Springer International Publishing, 2015.
https://doi.org/10.1007/978-3-319-11080-6.
-/ -/
import Mathlib.Tactic.Ring import Mathlib.Tactic.Ring
@ -10,47 +13,81 @@ import Mathlib.Tactic.Ring
universe u universe u
/--[1] /--[1]
An n-tuple is defined recursively as: An `n`-tuple is defined recursively as:
⟨x₁, ..., xₙ₊₁⟩ = ⟨⟨x₁, ..., xₙ⟩, xₙ₊₁⟩ `⟨x₁, ..., xₙ₊₁⟩ = ⟨⟨x₁, ..., xₙ⟩, xₙ₊₁⟩`
TODO: As [1] notes, it is useful to define ⟨x⟩ = x. Is this syntactically As [1] notes, it is useful to define `⟨x⟩ = x`. It is not clear this would be
possible in Lean? possible in Lean though.
Though [1] does not describe a notion of an empty tuple, [2] does (though under
the name of a "list").
--/ --/
inductive Tuple : (α : Type u) → Nat → Type u where inductive Tuple : (α : Type u) → Nat → Type u where
| only : α → Tuple α 1 | nil : Tuple α 0
| cons : {n : Nat} → Tuple α n → α → Tuple α (n + 1) | snoc : {n : Nat} → Tuple α n → α → Tuple α (n + 1)
syntax (priority := high) "⟨" term,+ "⟩" : term syntax (priority := high) "⟨" term,+ "⟩" : term
-- Notice the ambiguity this syntax introduces. For example, pattern `⟨a, b⟩`
-- could refer to a `2`-tuple or an `n`-tuple, where `a` is an `(n-1)`-tuple.
macro_rules macro_rules
| `(⟨$x⟩) => `(Tuple.only $x) | `(⟨$x⟩) => `(Tuple.snoc Tuple.nil $x)
| `(⟨$xs:term,*, $x⟩) => `(Tuple.cons ⟨$xs,*⟩ $x) | `(⟨$xs:term,*, $x⟩) => `(Tuple.snoc ⟨$xs,*⟩ $x)
namespace Tuple namespace Tuple
/-- def length : Tuple α n → Nat
Returns the value at the nth-index of the given tuple. | Tuple.nil => 0
-/ | Tuple.snoc init _ => length init + 1
def index (t : Tuple α n) (m : Nat) : 1 ≤ m ∧ m ≤ n → α := by
intro h theorem nil_length_zero : length (@Tuple.nil α) = 0 :=
cases t rfl
· case only last => exact last
. case cons n' init last => theorem snoc_length_succ : length (Tuple.snoc init last) = length init + 1 :=
by_cases k : m = n' + 1 rfl
theorem tuple_length {n : Nat} (t : Tuple α n) : length t = n :=
Tuple.recOn t nil_length_zero
fun _ _ ih => by
rw [snoc_length_succ]
norm_num
exact ih
def head : {n : Nat} → Tuple α n → n ≥ 1 → α
| n + 1, Tuple.snoc init last, h => by
by_cases k : 0 = n
· exact last · exact last
· exact index init m (And.intro h.left (by · have h' : 0 ≤ n := Nat.le_of_succ_le_succ h
have h₂ : m + 1 ≤ n' + 1 := Nat.lt_of_le_of_ne h.right k exact head init (Nat.lt_of_le_of_ne h' k)
norm_num at h₂
exact h₂)) def last : Tuple α n → n ≥ 1 → α
| Tuple.snoc _ last, _ => last
def index : {n : Nat} → Tuple α n → (k : Nat) → 1 ≤ k ∧ k ≤ n → α
| 0, _, m, h => by
have ff : 1 ≤ 0 := Nat.le_trans h.left h.right
ring_nf at ff
exact False.elim ff
| n + 1, Tuple.snoc init last, k, h => by
by_cases hₖ : k = n + 1
· exact last
· exact index init k $ And.intro
h.left
(Nat.le_of_lt_succ $ Nat.lt_of_le_of_ne h.right hₖ)
/-
-- TODO: Prove `eq_by_index`. -- TODO: Prove `eq_by_index`.
-- TODO: Prove Lemma 0A [1]. -- TODO: Prove Lemma 0A [1].
theorem eq_by_index (t₁ t₂ : Tuple α n) theorem eq_by_index (t₁ t₂ : Tuple α n)
: (t₁ = t₂) ↔ (∀ i : Nat, 1 ≤ i ∧ i ≤ n → index t₁ i = index t₂ i) := by : (t₁ = t₂) ↔ (∀ i : Nat, (p : 1 ≤ i ∧ i ≤ n) → index t₁ i p = index t₂ i p) := by
apply Iff.intro apply Iff.intro
· sorry · intro teq i hᵢ
sorry
· sorry · sorry
-/
end Tuple end Tuple