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

Commit f9de183

Browse files
committed
feat(data/multiset): working on multisets, fix rcases bug
1 parent d579a56 commit f9de183

File tree

11 files changed

+508
-66
lines changed

11 files changed

+508
-66
lines changed

algebra/functions.lean

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,36 @@ lemma lt_max_iff : a < max b c ↔ a < b ∨ a < c :=
3535
| or.inr h := lt_of_lt_of_le h (le_max_right _ _)
3636
end)⟩
3737

38+
lemma max_min_distrib_left : max a (min b c) = min (max a b) (max a c) :=
39+
begin
40+
apply le_antisymm (le_min
41+
(max_le (le_max_left _ _) (le_trans (min_le_left _ _) (le_max_right _ _)))
42+
(max_le (le_max_left _ _) (le_trans (min_le_right _ _) (le_max_right _ _)))),
43+
cases le_total b a with ba ab,
44+
{ rw max_eq_left ba, exact le_trans (min_le_left _ _) (le_max_left _ _) },
45+
cases le_total c a with ca ac,
46+
{ rw max_eq_left ca, exact le_trans (min_le_right _ _) (le_max_left _ _) },
47+
rw [max_eq_right ab, max_eq_right ac], apply le_max_right
48+
end
49+
50+
lemma max_min_distrib_right : max (min a b) c = min (max a c) (max b c) :=
51+
by rw [max_comm, max_min_distrib_left, max_comm, max_comm b]
52+
53+
lemma min_max_distrib_left : min a (max b c) = max (min a b) (min a c) :=
54+
begin
55+
refine le_antisymm _ (max_le
56+
(le_min (min_le_left _ _) (le_trans (min_le_right _ _) (le_max_left _ _)))
57+
(le_min (min_le_left _ _) (le_trans (min_le_right _ _) (le_max_right _ _)))),
58+
cases le_total a b with ab ba,
59+
{ rw min_eq_left ab, exact le_trans (min_le_left _ _) (le_max_left _ _) },
60+
cases le_total a c with ac ca,
61+
{ rw min_eq_left ac, exact le_trans (min_le_left _ _) (le_max_right _ _) },
62+
rw [min_eq_right ba, min_eq_right ca], apply min_le_right
63+
end
64+
65+
lemma min_max_distrib_right : min (max a b) c = max (min a c) (min b c) :=
66+
by rw [min_comm, min_max_distrib_left, min_comm, min_comm b]
67+
3868
end
3969

4070
section decidable_linear_ordered_comm_group

analysis/topology/uniform_space.lean

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,15 +860,15 @@ instance {α : Type u} [u : uniform_space α] : uniform_space (quotient (separat
860860
is_open_uniformity := assume s,
861861
have ∀a, ⟦a⟧ ∈ s →
862862
({p:α×α | p.1 = a → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets ↔
863-
{p:α×α | p.1⟧ = ⟦a⟧ → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets),
863+
{p:α×α | p.1 ≈ a → ⟦p.2⟧ ∈ s} ∈ (@uniformity α _).sets),
864864
from assume a ha,
865865
⟨assume h,
866866
let ⟨t, ht, hts⟩ := comp_mem_uniformity_sets h in
867867
have hts : ∀{a₁ a₂}, (a, a₁) ∈ t → (a₁, a₂) ∈ t → ⟦a₂⟧ ∈ s,
868868
from assume a₁ a₂ ha₁ ha₂, @hts (a, a₂) ⟨a₁, ha₁, ha₂⟩ rfl,
869-
have ht' : ∀{a₁ a₂}, ⟦a₁⟧ = ⟦a₂⟧ → (a₁, a₂) ∈ t,
870-
from assume a₁ a₂ h, sInter_subset_of_mem ht (quotient.exact h),
871-
uniformity.upwards_sets ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' h₂.symm) h₁,
869+
have ht' : ∀{a₁ a₂}, a₁ ≈ a₂ → (a₁, a₂) ∈ t,
870+
from assume a₁ a₂ h, sInter_subset_of_mem ht h,
871+
uniformity.upwards_sets ht $ assume ⟨a₁, a₂⟩ h₁ h₂, hts (ht' $ setoid.symm h₂) h₁,
872872
assume h, uniformity.upwards_sets h $ by simp {contextual := tt}⟩,
873873
begin
874874
simp [topological_space.coinduced, u.is_open_uniformity, uniformity, forall_quotient_iff],

data/list/basic.lean

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Authors: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn, M
55
66
Basic properties of lists.
77
-/
8-
import logic.basic data.nat.basic data.option data.bool
8+
import logic.basic data.nat.basic data.option data.bool data.prod
99
tactic.interactive algebra.group
1010
open function nat
1111

@@ -923,6 +923,15 @@ by induction l; simp [list.join, *] at *
923923

924924
end monoid
925925

926+
@[simp] theorem sum_const_nat (m n : ℕ) : sum (list.repeat m n) = m * n :=
927+
by induction n; simp [*, nat.mul_succ]
928+
929+
@[simp] theorem length_join (L : list (list α)) : length (join L) = sum (map length L) :=
930+
by induction L; simp *
931+
932+
@[simp] theorem length_bind (l : list α) (f : α → list β) : length (bind l f) = sum (map (length ∘ f) l) :=
933+
by rw [bind, length_join, map_map]
934+
926935
/- all & any, bounded quantifiers over lists -/
927936

928937
theorem forall_mem_nil (p : α → Prop) : ∀ x ∈ @nil α, p x :=
@@ -1517,36 +1526,47 @@ def transpose : list (list α) → list (list α)
15171526

15181527
/- permutations -/
15191528

1520-
def permutations_aux2 (t : α) (ts : list α) : list α → (list α → β) → list β → list α × list β
1521-
| [] f r := (ts, r)
1522-
| (y::ys) f r := let (us, zs) := permutations_aux2 ys (λx : list α, f (y::x)) r in
1523-
(y :: us, f (t :: y :: us) :: zs)
1529+
section permutations
15241530

1525-
private def meas : list α × list α → ℕ × ℕ | (l, i) := (length l + length i, length l)
1531+
def permutations_aux2 (t : α) (ts : list α) (r : list β) : list α → (list α → β) → list α × list β
1532+
| [] f := (ts, r)
1533+
| (y::ys) f := let (us, zs) := permutations_aux2 ys (λx : list α, f (y::x)) in
1534+
(y :: us, f (t :: y :: us) :: zs)
15261535

1527-
local infix ` ≺ `:50 := inv_image (prod.lex (<) (<)) meas
1536+
private def meas : (Σ'_:list α, list α) → ℕ × ℕ | ⟨l, i⟩ := (length l + length i, length l)
15281537

1529-
def permutations_aux.F : Π (x : list α × list α), (Π (y : list α × list α), y ≺ x → list (list α)) → list (list α)
1530-
| ([], is) IH := []
1531-
| (t::ts, is) IH :=
1532-
have h1 : (ts, t :: is) ≺ (t :: ts, is), from
1533-
show prod.lex _ _ (succ (length ts + length is), length ts) (succ (length ts) + length is, length (t :: ts)),
1534-
by rw nat.succ_add; exact prod.lex.right _ _ (lt_succ_self _),
1535-
have h2 : (is, []) ≺ (t :: ts, is), from prod.lex.left _ _ _ (lt_add_of_pos_left _ (succ_pos _)),
1536-
foldr (λy r, (permutations_aux2 t ts y id r).2) (IH (ts, t::is) h1) (is :: IH (is, []) h2)
1538+
local infix ` ≺ `:50 := inv_image (prod.lex (<) (<)) meas
15371539

1538-
def permutations_aux : list α × list α → list (list α) :=
1539-
well_founded.fix (inv_image.wf meas (prod.lex_wf lt_wf lt_wf)) permutations_aux.F
1540+
@[elab_as_eliminator] def permutations_aux.rec {C : list α → list α → Sort v}
1541+
(H0 : ∀ is, C [] is)
1542+
(H1 : ∀ t ts is, C ts (t::is) → C is [] → C (t::ts) is) : ∀ l₁ l₂, C l₁ l₂
1543+
| [] is := H0 is
1544+
| (t::ts) is :=
1545+
have h1 : ⟨ts, t :: is⟩ ≺ ⟨t :: ts, is⟩, from
1546+
show prod.lex _ _ (succ (length ts + length is), length ts) (succ (length ts) + length is, length (t :: ts)),
1547+
by rw nat.succ_add; exact prod.lex.right _ _ (lt_succ_self _),
1548+
have h2 : ⟨is, []⟩ ≺ ⟨t :: ts, is⟩, from prod.lex.left _ _ _ (lt_add_of_pos_left _ (succ_pos _)),
1549+
H1 t ts is (permutations_aux.rec ts (t::is)) (permutations_aux.rec is [])
1550+
using_well_founded {
1551+
dec_tac := tactic.assumption,
1552+
rel_tac := λ _ _, `[exact ⟨(≺), @inv_image.wf _ _ _ meas (prod.lex_wf lt_wf lt_wf)⟩] }
1553+
1554+
def permutations_aux : list α → list α → list (list α) :=
1555+
@@permutations_aux.rec (λ _ _, list (list α)) (λ is, [])
1556+
(λ t ts is IH1 IH2, foldr (λy r, (permutations_aux2 t ts r y id).2) IH1 (is :: IH2))
15401557

15411558
def permutations (l : list α) : list (list α) :=
1542-
l :: permutations_aux (l, [])
1559+
l :: permutations_aux l []
1560+
1561+
@[simp] theorem permutations_aux_nil (is : list α) : permutations_aux [] is = [] :=
1562+
by simp [permutations_aux, permutations_aux.rec]
15431563

1544-
def permutations_aux.eqn_1 (is : list α) : permutations_aux ([], is) = [] :=
1545-
well_founded.fix_eq _ _ _
1564+
@[simp] theorem permutations_aux_cons (t : α) (ts is : list α) :
1565+
permutations_aux (t :: ts) is = foldr (λy r, (permutations_aux2 t ts r y id).2)
1566+
(permutations_aux ts (t::is)) (permutations is) :=
1567+
by simp [permutations_aux, permutations_aux.rec, permutations]
15461568

1547-
def permutations_aux.eqn_2 (t : α) (ts is) : permutations_aux (t::ts, is) =
1548-
foldr (λy r, (permutations_aux2 t ts y id r).2) (permutations_aux (ts, t::is)) (permutations is) :=
1549-
well_founded.fix_eq _ _ _
1569+
end permutations
15501570

15511571
/- insert -/
15521572
section insert
@@ -1652,15 +1672,16 @@ theorem erase_sublist_erase (a : α) : ∀ {l₁ l₂ : list α}, l₁ <+ l₂
16521672
then by rw [h, erase_cons_head, erase_cons_head]; exact s
16531673
else by rw [erase_cons_tail _ h, erase_cons_tail _ h]; exact (erase_sublist_erase s).cons2 _ _ _
16541674

1655-
theorem mem_erase_of_ne_of_mem {a b : α} {l : list α} (ab : a ≠ b) (al : a ∈ l) : a ∈ l.erase b :=
1656-
if h : b ∈ l then match l, l.erase b, exists_erase_eq h, al with
1657-
| ._, ._, ⟨l₁, l₂, _, rfl, rfl⟩, al :=
1658-
by simp at *; exact or.resolve_left al ab
1659-
end else by simp [h, al]
1660-
16611675
theorem mem_of_mem_erase {a b : α} {l : list α} : a ∈ l.erase b → a ∈ l :=
16621676
@erase_subset _ _ _ _ _
16631677

1678+
@[simp] theorem mem_erase_of_ne {a b : α} {l : list α} (ab : a ≠ b) : a ∈ l.erase b ↔ a ∈ l :=
1679+
⟨mem_of_mem_erase, λ al,
1680+
if h : b ∈ l then match l, l.erase b, exists_erase_eq h, al with
1681+
| ._, ._, ⟨l₁, l₂, _, rfl, rfl⟩, al :=
1682+
by simp at *; exact or.resolve_left al ab
1683+
end else by simp [h, al]⟩
1684+
16641685
theorem erase_comm (a b : α) (l : list α) : (l.erase a).erase b = (l.erase b).erase a :=
16651686
if ab : a = b then by simp [ab] else
16661687
if ha : a ∈ l then
@@ -1932,6 +1953,40 @@ ball.imp_left (λ x, mem_of_mem_inter_right) h
19321953

19331954
end inter
19341955

1956+
/- bag_inter -/
1957+
section bag_inter
1958+
variable [decidable_eq α]
1959+
1960+
@[simp] theorem nil_bag_inter (l : list α) : [].bag_inter l = [] :=
1961+
by cases l; refl
1962+
1963+
@[simp] theorem bag_inter_nil (l : list α) : l.bag_inter [] = [] :=
1964+
by cases l; refl
1965+
1966+
@[simp] theorem cons_bag_inter_of_pos {a} (l₁ : list α) {l₂} (h : a ∈ l₂) :
1967+
(a :: l₁).bag_inter l₂ = a :: l₁.bag_inter (l₂.erase a) :=
1968+
by cases l₂; exact if_pos h
1969+
1970+
@[simp] theorem cons_bag_inter_of_neg {a} (l₁ : list α) {l₂} (h : a ∉ l₂) :
1971+
(a :: l₁).bag_inter l₂ = l₁.bag_inter l₂ :=
1972+
by cases l₂; simp [h, list.bag_inter]
1973+
1974+
theorem mem_bag_inter {a : α} : ∀ {l₁ l₂ : list α}, a ∈ l₁.bag_inter l₂ ↔ a ∈ l₁ ∧ a ∈ l₂
1975+
| [] l₂ := by simp
1976+
| (b::l₁) l₂ := by
1977+
by_cases b ∈ l₂; simp [*, and_or_distrib_left];
1978+
by_cases a = b with ba; simp *
1979+
1980+
theorem bag_inter_sublist_left : ∀ l₁ l₂ : list α, l₁.bag_inter l₂ <+ l₁
1981+
| [] l₂ := by simp [nil_sublist]
1982+
| (b::l₁) l₂ := begin
1983+
by_cases b ∈ l₂; simp [h],
1984+
{ apply cons_sublist_cons, apply bag_inter_sublist_left },
1985+
{ apply sublist_cons_of_sublist, apply bag_inter_sublist_left }
1986+
end
1987+
1988+
end bag_inter
1989+
19351990
/- pairwise relation (generalized no duplicate) -/
19361991

19371992
section pairwise
@@ -2483,6 +2538,10 @@ theorem range'_subset_right {s m n : ℕ} : range' s m ⊆ range' s n ↔ m ≤
24832538
(mem_range'.1 $ h $ mem_range'.2 ⟨le_add_right _ _, nat.add_lt_add_left hn s⟩).2,
24842539
λ h, subset_of_sublist (range'_sublist_right.2 h)⟩
24852540

2541+
theorem nth_range' : ∀ s {m n : ℕ}, m < n → nth (range' s n) m = some (s + m)
2542+
| s 0 (n+1) _ := by simp
2543+
| s (m+1) (n+1) h := by simp [nth_range' (s+1) (lt_of_add_lt_add_right h)]
2544+
24862545
theorem range'_concat (s n : ℕ) : range' s (n + 1) = range' s n ++ [s+n] :=
24872546
by rw add_comm n 1; exact (range'_append s n 1).symm
24882547

@@ -2514,6 +2573,9 @@ by simp [range_eq_range', zero_le]
25142573
@[simp] theorem not_mem_range_self {n : ℕ} : n ∉ range n :=
25152574
mt mem_range.1 $ lt_irrefl _
25162575

2576+
theorem nth_range {m n : ℕ} (h : m < n) : nth (range n) m = some m :=
2577+
by simp [range_eq_range', nth_range' _ h]
2578+
25172579
theorem iota_eq_reverse_range' : ∀ n : ℕ, iota n = reverse (range' 1 n)
25182580
| 0 := rfl
25192581
| (n+1) := by simp [iota, range'_concat, iota_eq_reverse_range' n]

0 commit comments

Comments
 (0)