Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat(Data/Fin/Tuple): Define Fin.take and initial theorems #17196

Closed
wants to merge 20 commits into from
Closed
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1da58ae
define `Fin.take` and initial supporting theorems
quangvdao Sep 28, 2024
5080d0d
fixed simpNF error, added new theorems about `take_append`
quangvdao Sep 28, 2024
0772539
removed `simp` attribute for `take_append` theorems
quangvdao Sep 28, 2024
86f64a9
Merge branch 'master' of https://github.com/leanprover-community/math…
quangvdao Oct 16, 2024
4f2d214
pushed suggested changes by Yael
quangvdao Oct 16, 2024
adeac12
moved `Fin.take` to its own file, added a theorem about `List.take`
quangvdao Oct 17, 2024
18b9f67
finished theorems about `List.take` and `Fin.take`
quangvdao Oct 17, 2024
6af779a
silly typo fix
quangvdao Oct 17, 2024
135a9de
added `Fin/Tuple/Take` to main `Mathlib` file
quangvdao Oct 17, 2024
482155c
removed `simp` attribute from `take_repeat`
quangvdao Oct 17, 2024
428ac6e
added theorems about `take` and `addCases`
quangvdao Oct 17, 2024
d5a6fac
added `take_take`
quangvdao Oct 17, 2024
19ca32b
renaming theorems about `Fin.take` and `List.take`
quangvdao Oct 22, 2024
47e6ec8
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
c3146f1
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
8c7abd8
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
b731ced
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
36d8e43
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
1531e9d
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
f508575
Update Mathlib/Data/Fin/Tuple/Take.lean
quangvdao Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions Mathlib/Data/Fin/Tuple/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ For a **pivot** `p : Fin (n + 1)`,
satisfied.
* `Fin.append a b` : append two tuples.
* `Fin.repeat n a` : repeat a tuple `n` times.
* `Fin.take m h` : take the first `h : m ≤ n` elements of a tuple.

-/

Expand Down Expand Up @@ -1071,6 +1072,103 @@ theorem contractNth_apply_of_ne (j : Fin (n + 1)) (op : α → α → α) (g : F

end ContractNth

section Take
quangvdao marked this conversation as resolved.
Show resolved Hide resolved

variable {n : ℕ} {α : Fin n → Sort*}

/-- Take the first `m` elements of an `n`-tuple where `m ≤ n`, returning an `m`-tuple. -/
def take (v : (i : Fin n) → α i) (m : ℕ) (h : m ≤ n) : (i : Fin m) → α (castLE h i) :=
quangvdao marked this conversation as resolved.
Show resolved Hide resolved
fun i ↦ v (castLE h i)

@[simp]
theorem take_def (v : (i : Fin n) → α i) (m : ℕ) (h : m ≤ n) (i : Fin m) :
(take v m h) i = v (castLE h i) := rfl

@[simp]
theorem take_zero (v : (i : Fin n) → α i) : take v 0 (Nat.zero_le n) = fun i ↦ elim0 i := by
ext i; exact elim0 i
quangvdao marked this conversation as resolved.
Show resolved Hide resolved

@[simp]
theorem take_eq_self (v : (i : Fin n) → α i) :
take v n (le_refl n) = v := by ext i; simp [take]

/-- Taking `m + 1` elements is equal to taking `m` elements and adding the `(m + 1)`th one. -/
theorem take_succ_eq_snoc (v : (i : Fin n) → α i) (m : ℕ) (h : m < n) :
take v m.succ h = snoc (take v m (le_of_lt h)) (v ⟨m, h⟩) := by
ext i
induction m with
| zero =>
have h' : i = 0 := by
ext
simp only [Nat.succ_eq_add_one, Nat.reduceAdd, coe_fin_one]
subst h'
simp [take, snoc, castLE]
| succ m _ =>
induction i using reverseInduction with
| last => simp [take, snoc, castLT]; congr
| cast i _ => simp [snoc_cast_add]

/-- `take` commutes with `update` for indices in the range of `take`. -/
@[simp]
theorem take_update_of_lt (v : (i : Fin n) → α i) (m : ℕ) (h : m ≤ n) (i : Fin m)
(x : α (castLE h i)) : take (update v (castLE h i) x) m h = update (take v m h) i x := by
ext j
by_cases h' : j = i
· rw [h']
simp only [take, update_same]
· have : castLE h j ≠ castLE h i := by simp [h']
simp only [take, update_noteq h', update_noteq this]

/-- `take` is the same after `update` for indices outside the range of `take`. -/
@[simp]
theorem take_update_of_ge (v : (i : Fin n) → α i) (m : ℕ) (h : m ≤ n) (i : Fin n) (hi : i ≥ m)
(x : α i) : take (update v i x) m h = take v m h := by
ext j
have : castLE h j ≠ i := by
refine ne_of_val_ne ?_
simp only [coe_castLE]
exact Nat.ne_of_lt (lt_of_lt_of_le j.isLt hi)
simp only [take, update_noteq this]

/-- Taking the first `m ≤ n` elements of an `append u v`, where `u` is a `n`-tuple, is the same as
taking the first `m` elements of `u`. -/
theorem take_append_left {n' m : ℕ} {α : Sort*} (u : (i : Fin n) → α) (v : (i : Fin n') → α)
(h : m ≤ n) : take (append u v) m (Nat.le_add_right_of_le h) = take u m h := by
ext i
have : castLE (Nat.le_add_right_of_le h) i = castAdd n' (castLE h i) := rfl
simp only [take, append_left, this]

/-- Taking the first `n + m` elements of an `append u v`, where `v` is a `n'`-tuple and `m ≤ n'`,
is the same as appending `u` with the first `m` elements of `v`. -/
theorem take_append_right {n' m : ℕ} {α : Sort*} (u : (i : Fin n) → α) (v : (i : Fin n') → α)
(h : m ≤ n') : take (append u v) (n + m) (Nat.add_le_add_left h n) = append u (take v m h) := by
ext i
by_cases h' : i < n
· have : castLE (Nat.add_le_add_left h n) i = castAdd n' ⟨i.val, h'⟩ := by
simp only [castAdd_mk]
rfl
rw [take, this, append_left]
have : i = castAdd m ⟨i.val, h'⟩ := by simp only [castAdd_mk]
conv_rhs => rw [this, append_left]
· simp only [not_lt] at h'
let j := subNat n (cast (Nat.add_comm _ _) i) h'
have : i = natAdd n j := by simp [j]
conv_rhs => rw [this, append_right, take]
have : castLE (Nat.add_le_add_left h n) i = natAdd n (castLE h j) := by
simp_all only [natAdd_subNat_cast, j]
ext : 1
simp_all only [coe_castLE, coe_natAdd, coe_subNat, coe_cast, Nat.add_sub_cancel']
rw [take, this, append_right]

@[simp]
theorem take_init {α : Fin (n + 1) → Sort*} (v : (i : Fin (n + 1)) → α i) (m : ℕ) (h : m ≤ n) :
take (init v) m h = take v m (Nat.le_succ_of_le h) := by
ext i
simp only [take, init]
congr

quangvdao marked this conversation as resolved.
Show resolved Hide resolved
end Take

/-- To show two sigma pairs of tuples agree, it to show the second elements are related via
`Fin.cast`. -/
theorem sigma_eq_of_eq_comp_cast {α : Type*} :
Expand Down
Loading