@@ -1137,28 +1137,43 @@ begin
1137
1137
{ simpa [succ_above_above _ _ (le_of_not_lt H)] using succ_pos _ },
1138
1138
end
1139
1139
1140
- /-- The range of `p.succ_above` is everything except `p`. -/
1141
- lemma range_succ_above (p : fin (n + 1 )) : set.range (p.succ_above) = { i | i ≠ p } :=
1140
+ @[simp] lemma succ_above_cast_lt {x y : fin (n + 1 )} (h : x < y)
1141
+ (hx : x.1 < n := lt_of_lt_of_le h y.le_last) :
1142
+ y.succ_above (x.cast_lt hx) = x :=
1143
+ by { rw [succ_above_below, cast_succ_cast_lt], exact h }
1144
+
1145
+ @[simp] lemma succ_above_pred {x y : fin (n + 1 )} (h : x < y)
1146
+ (hy : y ≠ 0 := (x.zero_le.trans_lt h).ne') :
1147
+ x.succ_above (y.pred hy) = y :=
1148
+ by { rw [succ_above_above, succ_pred], simpa [le_iff_coe_le_coe] using nat.le_pred_of_lt h }
1149
+
1150
+ lemma cast_lt_succ_above {x : fin n} {y : fin (n + 1 )} (h : cast_succ x < y)
1151
+ (h' : (y.succ_above x).1 < n := lt_of_lt_of_le ((succ_above_lt_iff _ _).2 h) (le_last y)) :
1152
+ (y.succ_above x).cast_lt h' = x :=
1153
+ by simp only [succ_above_below _ _ h, cast_lt_cast_succ]
1154
+
1155
+ lemma pred_succ_above {x : fin n} {y : fin (n + 1 )} (h : y ≤ cast_succ x)
1156
+ (h' : y.succ_above x ≠ 0 := (y.zero_le.trans_lt $ (lt_succ_above_iff _ _).2 h).ne') :
1157
+ (y.succ_above x).pred h' = x :=
1158
+ by simp only [succ_above_above _ _ h, pred_succ]
1159
+
1160
+ lemma exists_succ_above_eq {x y : fin (n + 1 )} (h : x ≠ y) : ∃ z, y.succ_above z = x :=
1142
1161
begin
1143
- ext,
1144
- simp only [set.mem_range, ne.def, set.mem_set_of_eq],
1145
- split,
1146
- { rintro ⟨y, rfl⟩,
1147
- exact succ_above_ne _ _ },
1148
- { intro h,
1149
- cases lt_or_gt_of_ne h with H H,
1150
- { refine ⟨x.cast_lt _, _⟩,
1151
- { exact lt_of_lt_of_le H p.le_last },
1152
- { rw succ_above_below,
1153
- { simp },
1154
- { exact H } } },
1155
- { refine ⟨x.pred _, _⟩,
1156
- { exact (ne_of_lt (lt_of_le_of_lt p.zero_le H)).symm },
1157
- { rw succ_above_above,
1158
- { simp },
1159
- { simpa [le_iff_coe_le_coe] using nat.le_pred_of_lt H } } } }
1162
+ cases h.lt_or_lt with hlt hlt,
1163
+ exacts [⟨_, succ_above_cast_lt hlt⟩, ⟨_, succ_above_pred hlt⟩],
1164
+ end
1165
+
1166
+ @[simp] lemma exists_succ_above_eq_iff {x y : fin (n + 1 )} : (∃ z, x.succ_above z = y) ↔ y ≠ x :=
1167
+ begin
1168
+ refine ⟨_, exists_succ_above_eq⟩,
1169
+ rintro ⟨y, rfl⟩,
1170
+ exact succ_above_ne _ _
1160
1171
end
1161
1172
1173
+ /-- The range of `p.succ_above` is everything except `p`. -/
1174
+ @[simp] lemma range_succ_above (p : fin (n + 1 )) : set.range (p.succ_above) = {p}ᶜ :=
1175
+ set.ext $ λ _, exists_succ_above_eq_iff
1176
+
1162
1177
/-- Given a fixed pivot `x : fin (n + 1)`, `x.succ_above` is injective -/
1163
1178
lemma succ_above_right_injective {x : fin (n + 1 )} : injective (succ_above x) :=
1164
1179
(succ_above x).injective
@@ -1173,7 +1188,7 @@ lemma succ_above_left_injective : injective (@succ_above n) :=
1173
1188
λ _ _ h, by simpa [range_succ_above] using congr_arg (λ f : fin n ↪o fin (n + 1 ), (set.range f)ᶜ) h
1174
1189
1175
1190
/-- `succ_above` is injective at the pivot -/
1176
- lemma succ_above_left_inj {x y : fin (n + 1 )} :
1191
+ @[simp] lemma succ_above_left_inj {x y : fin (n + 1 )} :
1177
1192
x.succ_above = y.succ_above ↔ x = y :=
1178
1193
succ_above_left_injective.eq_iff
1179
1194
@@ -1390,24 +1405,6 @@ begin
1390
1405
simp }
1391
1406
end
1392
1407
1393
- lemma forall_iff_succ_above {p : fin (n + 1 ) → Prop } (i : fin (n + 1 )) :
1394
- (∀ j, p j) ↔ p i ∧ ∀ j, p (i.succ_above j) :=
1395
- ⟨λ h, ⟨h _, λ j, h _⟩,
1396
- λ h j, if hj : j = i then (hj.symm ▸ h.1 ) else
1397
- begin
1398
- cases n,
1399
- { convert h.1 },
1400
- { cases lt_or_gt_of_ne hj with lt gt,
1401
- { rcases j.zero_le.eq_or_lt with rfl|H,
1402
- { convert h.2 0 , rw succ_above_below; simp [lt] },
1403
- { have ltl : j < last _ := lt.trans_le i.le_last,
1404
- convert h.2 j.cast_pred,
1405
- simp [succ_above_below, cast_succ_cast_pred ltl, lt] } },
1406
- { convert h.2 (j.pred (i.zero_le.trans_lt gt).ne.symm),
1407
- rw succ_above_above;
1408
- simp [le_cast_succ_iff, gt.lt] } }
1409
- end ⟩
1410
-
1411
1408
end pred_above
1412
1409
1413
1410
/-- `min n m` as an element of `fin (m + 1)` -/
@@ -1721,58 +1718,49 @@ section insert_nth
1721
1718
1722
1719
variables {α : fin (n+1 ) → Type u} {β : Type v}
1723
1720
1724
- /-- Insert an element into a tuple at a given position, auxiliary definition.
1725
- For the general definition, see `insert_nth`. -/
1726
- def insert_nth' {α : fin (n + 2 ) → Type u} (i : fin (n + 2 )) (x : α i)
1727
- (p : Π j : fin (n + 1 ), α (i.succ_above j)) (j : fin (n + 2 )) : α j :=
1728
- if h : i = j
1729
- then _root_.cast (congr_arg α h) x
1730
- else if h' : j < i then _root_.cast (congr_arg α $ begin
1731
- obtain ⟨k, hk⟩ : ∃ (k : fin (n + 1 )), k.cast_succ = j,
1732
- { refine ⟨⟨(j : ℕ), _⟩, _⟩,
1733
- { exact lt_of_lt_of_le h' i.is_le, },
1734
- { simp } },
1735
- subst hk,
1736
- simp [succ_above_below, h'],
1737
- end )
1738
- (p j.cast_pred) else _root_.cast (congr_arg α $ begin
1739
- have lt : i < j := lt_of_le_of_ne (le_of_not_lt h') h,
1740
- have : j ≠ 0 := (ne_of_gt (lt_of_le_of_lt i.zero_le lt)),
1741
- rw [←succ_pred j this , ←le_cast_succ_iff] at lt,
1742
- simp [pred_above_zero this , succ_above_above _ _ lt]
1743
- end ) (p (fin.pred_above 0 j))
1721
+ /-- Define a function on `fin (n + 1)` from a value on `i : fin (n + 1)` and values on each
1722
+ `fin.succ_above i j`, `j : fin n`. This version is elaborated as eliminator and works for
1723
+ propositions, see also `fin.insert_nth` for a version without an `@[elab_as_eliminator]`
1724
+ attribute. -/
1725
+ @[elab_as_eliminator]
1726
+ def succ_above_cases {α : fin (n + 1 ) → Sort u} (i : fin (n + 1 )) (x : α i)
1727
+ (p : Π j : fin n, α (i.succ_above j)) (j : fin (n + 1 )) : α j :=
1728
+ if hj : j = i then eq.rec x hj.symm
1729
+ else if hlt : j < i then eq.rec_on (succ_above_cast_lt hlt) (p _)
1730
+ else eq.rec_on (succ_above_pred $ (ne.lt_or_lt hj).resolve_left hlt) (p _)
1731
+
1732
+ lemma forall_iff_succ_above {p : fin (n + 1 ) → Prop } (i : fin (n + 1 )) :
1733
+ (∀ j, p j) ↔ p i ∧ ∀ j, p (i.succ_above j) :=
1734
+ ⟨λ h, ⟨h _, λ j, h _⟩, λ h, succ_above_cases i h.1 h.2 ⟩
1744
1735
1745
1736
/-- Insert an element into a tuple at a given position. For `i = 0` see `fin.cons`,
1746
- for `i = fin.last n` see `fin.snoc`. -/
1747
- def insert_nth : Π {n : ℕ} {α : fin (n + 1 ) → Type u} (i : fin (n + 1 )) (x : α i)
1748
- ( p : Π j : fin n, α (i.succ_above j)) (j : fin (n + 1 )), α j
1749
- | 0 _ _ x _ _ := _root_.cast ( by congr) x
1750
- | (n + 1 ) _ i x p j := insert_nth' i x p j
1737
+ for `i = fin.last n` see `fin.snoc`. See also `fin.succ_above_cases` for a version elaborated
1738
+ as an eliminator. -/
1739
+ def insert_nth (i : fin (n + 1 )) (x : α i) ( p : Π j : fin n, α (i.succ_above j)) (j : fin (n + 1 )) :
1740
+ α j :=
1741
+ succ_above_cases i x p j
1751
1742
1752
1743
@[simp] lemma insert_nth_apply_same (i : fin (n + 1 )) (x : α i) (p : Π j, α (i.succ_above j)) :
1753
1744
insert_nth i x p i = x :=
1754
- by { cases n; simp [insert_nth, insert_nth'] }
1745
+ by simp [insert_nth, succ_above_cases]
1755
1746
1756
1747
@[simp] lemma insert_nth_apply_succ_above (i : fin (n + 1 )) (x : α i) (p : Π j, α (i.succ_above j))
1757
1748
(j : fin n) :
1758
1749
insert_nth i x p (i.succ_above j) = p j :=
1759
1750
begin
1760
- cases n,
1761
- { exact j.elim0 },
1762
- simp only [insert_nth, insert_nth', dif_neg (succ_above_ne _ _).symm],
1763
- cases succ_above_lt_ge i j with h h,
1764
- { rw dif_pos,
1765
- refine eq_of_heq ((cast_heq _ _).trans _),
1766
- { simp [h] },
1767
- { congr,
1768
- simp [succ_above_below, h] } },
1769
- { rw dif_neg,
1770
- refine eq_of_heq ((cast_heq _ _).trans _),
1771
- { simp [h] },
1772
- { congr,
1773
- simp [succ_above_above, h, succ_ne_zero] } }
1751
+ simp only [insert_nth, succ_above_cases, dif_neg (succ_above_ne _ _)],
1752
+ by_cases hlt : j.cast_succ < i,
1753
+ { rw [dif_pos ((succ_above_lt_iff _ _).2 hlt)],
1754
+ apply eq_of_heq ((eq_rec_heq _ _).trans _),
1755
+ rw [cast_lt_succ_above hlt] },
1756
+ { rw [dif_neg (mt (succ_above_lt_iff _ _).1 hlt)],
1757
+ apply eq_of_heq ((eq_rec_heq _ _).trans _),
1758
+ rw [pred_succ_above (le_of_not_lt hlt)] }
1774
1759
end
1775
1760
1761
+ @[simp] lemma succ_above_cases_eq_insert_nth :
1762
+ @succ_above_cases.{u + 1 } = @insert_nth.{u} := rfl
1763
+
1776
1764
@[simp] lemma insert_nth_comp_succ_above (i : fin (n + 1 )) (x : β) (p : fin n → β) :
1777
1765
insert_nth i x p ∘ i.succ_above = p :=
1778
1766
funext $ insert_nth_apply_succ_above i x p
@@ -1814,6 +1802,41 @@ end
1814
1802
@insert_nth _ (λ _, β) (last n) x p = snoc p x :=
1815
1803
by simp [insert_nth_last]
1816
1804
1805
+ @[simp] lemma insert_nth_zero_right [Π j, has_zero (α j)] (i : fin (n + 1 )) (x : α i) :
1806
+ i.insert_nth x 0 = pi.single i x :=
1807
+ insert_nth_eq_iff.2 $ by simp [succ_above_ne, pi.zero_def]
1808
+
1809
+ lemma insert_nth_binop (op : Π j, α j → α j → α j) (i : fin (n + 1 ))
1810
+ (x y : α i) (p q : Π j, α (i.succ_above j)) :
1811
+ i.insert_nth (op i x y) (λ j, op _ (p j) (q j)) =
1812
+ λ j, op j (i.insert_nth x p j) (i.insert_nth y q j) :=
1813
+ insert_nth_eq_iff.2 $ by simp
1814
+
1815
+ @[simp] lemma insert_nth_mul [Π j, has_mul (α j)] (i : fin (n + 1 ))
1816
+ (x y : α i) (p q : Π j, α (i.succ_above j)) :
1817
+ i.insert_nth (x * y) (p * q) = i.insert_nth x p * i.insert_nth y q :=
1818
+ insert_nth_binop (λ _, (*)) i x y p q
1819
+
1820
+ @[simp] lemma insert_nth_add [Π j, has_add (α j)] (i : fin (n + 1 ))
1821
+ (x y : α i) (p q : Π j, α (i.succ_above j)) :
1822
+ i.insert_nth (x + y) (p + q) = i.insert_nth x p + i.insert_nth y q :=
1823
+ insert_nth_binop (λ _, (+)) i x y p q
1824
+
1825
+ @[simp] lemma insert_nth_div [Π j, has_div (α j)] (i : fin (n + 1 ))
1826
+ (x y : α i) (p q : Π j, α (i.succ_above j)) :
1827
+ i.insert_nth (x / y) (p / q) = i.insert_nth x p / i.insert_nth y q :=
1828
+ insert_nth_binop (λ _, (/)) i x y p q
1829
+
1830
+ @[simp] lemma insert_nth_sub [Π j, has_sub (α j)] (i : fin (n + 1 ))
1831
+ (x y : α i) (p q : Π j, α (i.succ_above j)) :
1832
+ i.insert_nth (x - y) (p - q) = i.insert_nth x p - i.insert_nth y q :=
1833
+ insert_nth_binop (λ _, has_sub.sub) i x y p q
1834
+
1835
+ @[simp] lemma insert_nth_sub_same [Π j, add_group (α j)] (i : fin (n + 1 ))
1836
+ (x y : α i) (p : Π j, α (i.succ_above j)) :
1837
+ i.insert_nth x p - i.insert_nth y p = pi.single i (x - y) :=
1838
+ by simp_rw [← insert_nth_sub, ← insert_nth_zero_right, pi.sub_def, sub_self, pi.zero_def]
1839
+
1817
1840
variables [Π i, preorder (α i)]
1818
1841
1819
1842
lemma insert_nth_le_iff {i : fin (n + 1 )} {x : α i} {p : Π j, α (i.succ_above j)} {q : Π j, α j} :
0 commit comments