Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 3d5e5ee

Browse files
committed
feat(data/list/*): Miscellaneous lemmas (#13577)
A few lemmas about `list.chain`, `list.pairwise`. Also rename `list.chain_of_pairwise` to `list.pairwise.chain` for dot notation.
1 parent c83488b commit 3d5e5ee

File tree

6 files changed

+58
-15
lines changed

6 files changed

+58
-15
lines changed

src/data/list/basic.lean

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ cons_injective.eq_iff
5757
theorem exists_cons_of_ne_nil {l : list α} (h : l ≠ nil) : ∃ b L, l = b :: L :=
5858
by { induction l with c l', contradiction, use [c,l'], }
5959

60+
lemma set_of_mem_cons (l : list α) (a : α) : {x | x ∈ a :: l} = insert a {x | x ∈ l} := rfl
61+
6062
/-! ### mem -/
6163

6264
theorem mem_singleton_self (a : α) : a ∈ [a] := mem_cons_self _ _

src/data/list/chain.lean

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open nat
2222

2323
namespace list
2424

25-
variables {α : Type u} {β : Type v} {R : α → α → Prop}
25+
variables {α : Type u} {β : Type v} {R r : α → α → Prop} {l l₁ l₂ : list α} {a b : α}
2626

2727
mk_iff_of_inductive_prop list.chain list.chain_iff
2828

@@ -105,21 +105,31 @@ begin
105105
{ simp [H _ _ _ _ (rel_of_chain_cons hl₂), l_ih _ _ (chain_of_chain_cons hl₂)] }
106106
end
107107

108-
theorem chain_of_pairwise {a : α} {l : list α} (p : pairwise R (a :: l)) : chain R a l :=
108+
protected lemma pairwise.chain (p : pairwise R (a :: l)) : chain R a l :=
109109
begin
110110
cases pairwise_cons.1 p with r p', clear p,
111111
induction p' with b l r' p IH generalizing a, {exact chain.nil},
112112
simp only [chain_cons, forall_mem_cons] at r,
113113
exact chain_cons.2 ⟨r.1, IH r'⟩
114114
end
115115

116+
protected lemma chain.pairwise (tr : transitive R) :
117+
∀ {a : α} {l : list α}, chain R a l → pairwise R (a :: l)
118+
| a [] chain.nil := pairwise_singleton _ _
119+
| a _ (@chain.cons _ _ _ b l h hb) := hb.pairwise.cons begin
120+
simp only [mem_cons_iff, forall_eq_or_imp, h, true_and],
121+
exact λ c hc, tr h (rel_of_pairwise_cons hb.pairwise hc),
122+
end
123+
116124
theorem chain_iff_pairwise (tr : transitive R) {a : α} {l : list α} :
117125
chain R a l ↔ pairwise R (a :: l) :=
118-
⟨λ c, begin
119-
induction c with b b c l r p IH, {exact pairwise_singleton _ _},
120-
apply IH.cons _, simp only [mem_cons_iff, forall_eq_or_imp, r, true_and],
121-
show ∀ x ∈ l, R b x, from λ x m, (tr r (rel_of_pairwise_cons IH m)),
122-
end, chain_of_pairwise⟩
126+
⟨chain.pairwise tr, pairwise.chain⟩
127+
128+
protected lemma chain.sublist [is_trans α R] (hl : l₂.chain R a) (h : l₁ <+ l₂) : l₁.chain R a :=
129+
by { rw chain_iff_pairwise (transitive_of_trans R) at ⊢ hl, exact hl.sublist (h.cons_cons a) }
130+
131+
protected lemma chain.rel [is_trans α R] (hl : l.chain R a) (hb : b ∈ l) : R a b :=
132+
by { rw chain_iff_pairwise (transitive_of_trans R) at hl, exact rel_of_pairwise_cons hl hb }
123133

124134
theorem chain_iff_nth_le {R} : ∀ {a : α} {l : list α},
125135
chain R a l ↔ (∀ h : 0 < length l, R a (nth_le l 0 h)) ∧ (∀ i (h : i < length l - 1),
@@ -192,13 +202,16 @@ theorem chain'_map_of_chain' {S : β → β → Prop} (f : α → β)
192202

193203
theorem pairwise.chain' : ∀ {l : list α}, pairwise R l → chain' R l
194204
| [] _ := trivial
195-
| (a :: l) h := chain_of_pairwise h
205+
| (a :: l) h := pairwise.chain h
196206

197207
theorem chain'_iff_pairwise (tr : transitive R) : ∀ {l : list α},
198208
chain' R l ↔ pairwise R l
199209
| [] := (iff_true_intro pairwise.nil).symm
200210
| (a :: l) := chain_iff_pairwise tr
201211

212+
protected lemma chain'.sublist [is_trans α R] (hl : l₂.chain' R) (h : l₁ <+ l₂) : l₁.chain' R :=
213+
by { rw chain'_iff_pairwise (transitive_of_trans R) at ⊢ hl, exact hl.sublist h }
214+
202215
theorem chain'.cons {x y l} (h₁ : R x y) (h₂ : chain' R (y :: l)) :
203216
chain' R (x :: y :: l) :=
204217
chain'_cons.2 ⟨h₁, h₂⟩
@@ -289,7 +302,6 @@ lemma chain'.append_overlap : ∀ {l₁ l₂ l₃ : list α}
289302
exact ⟨h₁.1, chain'.append_overlap h₁.2 h₂ (cons_ne_nil _ _)⟩
290303
end
291304

292-
variables {r : α → α → Prop} {a b : α}
293305
/--
294306
If `a` and `b` are related by the reflexive transitive closure of `r`, then there is a `r`-chain
295307
starting from `a` and ending on `b`.

src/data/list/cycle.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ begin
802802
have Ha : a ∈ ((a :: l) : cycle α) := by simp,
803803
have Hl : ∀ {b} (hb : b ∈ l), b ∈ ((a :: l) : cycle α) := λ b hb, by simp [hb],
804804
rw cycle.chain_coe_cons,
805-
apply chain_of_pairwise,
805+
apply pairwise.chain,
806806
rw pairwise_cons,
807807
refine ⟨λ b hb, _, pairwise_append.2 ⟨pairwise_of_forall_mem_list
808808
(λ b hb c hc, hs b (Hl hb) c (Hl hc)), pairwise_singleton r a, λ b hb c hc, _⟩⟩,

src/data/list/pairwise.lean

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,24 @@ protected lemma pairwise.sublist : Π {l₁ l₂ : list α}, l₁ <+ l₂ → pa
104104
| ._ ._ (sublist.cons2 l₁ l₂ a s) (pairwise.cons i h) :=
105105
(h.sublist s).cons (ball.imp_left s.subset i)
106106

107-
lemma pairwise.forall_of_forall (H : symmetric R) (H₁ : ∀ x ∈ l, R x x) (H₂ : l.pairwise R) :
107+
lemma pairwise.forall_of_forall_of_flip (h₁ : ∀ x ∈ l, R x x) (h₂ : l.pairwise R)
108+
(h₃ : l.pairwise (flip R)) :
108109
∀ ⦃x⦄, x ∈ l → ∀ ⦃y⦄, y ∈ l → R x y :=
109110
begin
110-
induction l with a l IH, { exact forall_mem_nil _ },
111-
cases forall_mem_cons.1 H₁ with H₁₁ H₁₂,
112-
cases pairwise_cons.1 H₂ with H₂₁ H₂₂,
111+
induction l with a l ih,
112+
{ exact forall_mem_nil _ },
113+
rw pairwise_cons at h₂ h₃,
113114
rintro x (rfl | hx) y (rfl | hy),
114-
exacts [H₁₁, H₂₁ _ hy, H (H₂₁ _ hx), IH H₁₂ H₂₂ hx hy]
115+
{ exact h₁ _ (l.mem_cons_self _) },
116+
{ exact h₂.1 _ hy },
117+
{ exact h₃.1 _ hx },
118+
{ exact ih (λ x hx, h₁ _ $ mem_cons_of_mem _ hx) h₂.2 h₃.2 hx hy }
115119
end
116120

121+
lemma pairwise.forall_of_forall (H : symmetric R) (H₁ : ∀ x ∈ l, R x x) (H₂ : l.pairwise R) :
122+
∀ ⦃x⦄, x ∈ l → ∀ ⦃y⦄, y ∈ l → R x y :=
123+
H₂.forall_of_forall_of_flip H₁ $ by rwa H.flip_eq
124+
117125
lemma pairwise.forall (hR : symmetric R) (hl : l.pairwise R) :
118126
∀ ⦃a⦄, a ∈ l → ∀ ⦃b⦄, b ∈ l → a ≠ b → R a b :=
119127
pairwise.forall_of_forall

src/logic/basic.lean

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,21 @@ lemma congr_arg2 {α β γ : Sort*} (f : α → β → γ) {x x' : α} {y y' :
907907
(hx : x = x') (hy : y = y') : f x y = f x' y' :=
908908
by { subst hx, subst hy }
909909

910+
variables {β : α → Sort*} {γ : Π a, β a → Sort*} {δ : Π a b, γ a b → Sort*}
911+
912+
lemma congr_fun₂ {f g : Π a b, γ a b} (h : f = g) (a : α) (b : β a) : f a b = g a b :=
913+
congr_fun (congr_fun h _) _
914+
915+
lemma congr_fun₃ {f g : Π a b c, δ a b c} (h : f = g) (a : α) (b : β a) (c : γ a b) :
916+
f a b c = g a b c :=
917+
congr_fun₂ (congr_fun h _) _ _
918+
919+
lemma funext₂ {f g : Π a, β a → Prop} (h : ∀ a b, f a b = g a b) : f = g :=
920+
funext $ λ _, funext $ h _
921+
922+
lemma funext₃ {f g : Π a b, γ a b → Prop} (h : ∀ a b c, f a b c = g a b c) : f = g :=
923+
funext $ λ _, funext₂ $ h _
924+
910925
end equality
911926

912927
/-! ### Declarations about quantifiers -/

src/logic/relation.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ is_refl.reflexive.ne_imp_iff
7171

7272
protected lemma symmetric.iff (H : symmetric r) (x y : α) : r x y ↔ r y x := ⟨λ h, H h, λ h, H h⟩
7373

74+
lemma symmetric.flip_eq (h : symmetric r) : flip r = r := funext₂ $ λ _ _, propext $ h.iff _ _
75+
lemma symmetric.swap_eq : symmetric r → swap r = r := symmetric.flip_eq
76+
77+
lemma flip_eq_iff : flip r = r ↔ symmetric r := ⟨λ h x y, (congr_fun₂ h _ _).mp, symmetric.flip_eq⟩
78+
lemma swap_eq_iff : swap r = r ↔ symmetric r := flip_eq_iff
79+
7480
end ne_imp
7581

7682
section comap

0 commit comments

Comments
 (0)