2015-04-09 22:51:23 +00:00
|
|
|
|
/-
|
|
|
|
|
Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
2015-05-08 03:10:17 +00:00
|
|
|
|
Author: Leonardo de Moura, Jeremy Avigad
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
Finite sets.
|
2015-04-09 22:51:23 +00:00
|
|
|
|
-/
|
2015-08-13 01:37:33 +00:00
|
|
|
|
import data.fintype.basic data.nat data.list.perm algebra.binary
|
2015-07-16 00:44:19 +00:00
|
|
|
|
open nat quot list subtype binary function eq.ops
|
2015-04-09 22:51:23 +00:00
|
|
|
|
open [declarations] perm
|
|
|
|
|
|
|
|
|
|
definition nodup_list (A : Type) := {l : list A | nodup l}
|
|
|
|
|
|
|
|
|
|
variable {A : Type}
|
|
|
|
|
|
|
|
|
|
definition to_nodup_list_of_nodup {l : list A} (n : nodup l) : nodup_list A :=
|
|
|
|
|
tag l n
|
|
|
|
|
|
|
|
|
|
definition to_nodup_list [h : decidable_eq A] (l : list A) : nodup_list A :=
|
|
|
|
|
@to_nodup_list_of_nodup A (erase_dup l) (nodup_erase_dup l)
|
|
|
|
|
|
2015-07-06 19:17:57 +00:00
|
|
|
|
private definition eqv (l₁ l₂ : nodup_list A) :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
perm (elt_of l₁) (elt_of l₂)
|
|
|
|
|
|
2015-07-06 19:17:57 +00:00
|
|
|
|
local infix ~ := eqv
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-07-06 19:17:57 +00:00
|
|
|
|
private definition eqv.refl (l : nodup_list A) : l ~ l :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
!perm.refl
|
|
|
|
|
|
2015-07-06 19:17:57 +00:00
|
|
|
|
private definition eqv.symm {l₁ l₂ : nodup_list A} : l₁ ~ l₂ → l₂ ~ l₁ :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
perm.symm
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-07-06 19:17:57 +00:00
|
|
|
|
private definition eqv.trans {l₁ l₂ l₃ : nodup_list A} : l₁ ~ l₂ → l₂ ~ l₃ → l₁ ~ l₃ :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
perm.trans
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-06-01 01:51:09 +00:00
|
|
|
|
definition finset.nodup_list_setoid [instance] (A : Type) : setoid (nodup_list A) :=
|
2015-07-06 19:17:57 +00:00
|
|
|
|
setoid.mk (@eqv A) (mk_equivalence (@eqv A) (@eqv.refl A) (@eqv.symm A) (@eqv.trans A))
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
|
|
|
|
definition finset (A : Type) : Type :=
|
2015-06-01 01:51:09 +00:00
|
|
|
|
quot (finset.nodup_list_setoid A)
|
|
|
|
|
|
|
|
|
|
namespace finset
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-08-04 20:47:16 +00:00
|
|
|
|
-- give finset notation higher priority than set notation, so that it is tried first
|
|
|
|
|
protected definition prio : num := num.succ std.priority.default
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
definition to_finset_of_nodup (l : list A) (n : nodup l) : finset A :=
|
|
|
|
|
⟦to_nodup_list_of_nodup n⟧
|
|
|
|
|
|
|
|
|
|
definition to_finset [h : decidable_eq A] (l : list A) : finset A :=
|
|
|
|
|
⟦to_nodup_list l⟧
|
|
|
|
|
|
2015-06-10 07:39:50 +00:00
|
|
|
|
lemma to_finset_eq_of_nodup [h : decidable_eq A] {l : list A} (n : nodup l) :
|
|
|
|
|
to_finset_of_nodup l n = to_finset l :=
|
|
|
|
|
assert P : to_nodup_list_of_nodup n = to_nodup_list l, from
|
|
|
|
|
begin
|
|
|
|
|
rewrite [↑to_nodup_list, ↑to_nodup_list_of_nodup],
|
2015-06-11 01:19:16 +00:00
|
|
|
|
congruence,
|
|
|
|
|
rewrite [erase_dup_eq_of_nodup n]
|
2015-06-10 07:39:50 +00:00
|
|
|
|
end,
|
|
|
|
|
quot.sound (eq.subst P !setoid.refl)
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
definition has_decidable_eq [instance] [h : decidable_eq A] : decidable_eq (finset A) :=
|
|
|
|
|
λ s₁ s₂, quot.rec_on_subsingleton₂ s₁ s₂
|
|
|
|
|
(λ l₁ l₂,
|
|
|
|
|
match decidable_perm (elt_of l₁) (elt_of l₂) with
|
|
|
|
|
| decidable.inl e := decidable.inl (quot.sound e)
|
|
|
|
|
| decidable.inr n := decidable.inr (λ e : ⟦l₁⟧ = ⟦l₂⟧, absurd (quot.exact e) n)
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
definition mem (a : A) (s : finset A) : Prop :=
|
|
|
|
|
quot.lift_on s (λ l, a ∈ elt_of l)
|
|
|
|
|
(λ l₁ l₂ (e : l₁ ~ l₂), propext (iff.intro
|
|
|
|
|
(λ ainl₁, mem_perm e ainl₁)
|
|
|
|
|
(λ ainl₂, mem_perm (perm.symm e) ainl₂)))
|
|
|
|
|
|
2015-09-30 15:06:31 +00:00
|
|
|
|
infix [priority finset.prio] ∈ := mem
|
2015-08-04 20:47:16 +00:00
|
|
|
|
notation [priority finset.prio] a ∉ b := ¬ mem a b
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-04-11 16:28:27 +00:00
|
|
|
|
theorem mem_of_mem_list {a : A} {l : nodup_list A} : a ∈ elt_of l → a ∈ ⟦l⟧ :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
λ ainl, ainl
|
|
|
|
|
|
2015-04-11 16:28:27 +00:00
|
|
|
|
theorem mem_list_of_mem {a : A} {l : nodup_list A} : a ∈ ⟦l⟧ → a ∈ elt_of l :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
λ ainl, ainl
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
/- singleton -/
|
|
|
|
|
definition singleton (a : A) : finset A :=
|
|
|
|
|
to_finset_of_nodup [a] !nodup_singleton
|
|
|
|
|
|
2015-07-22 16:01:42 +00:00
|
|
|
|
theorem mem_singleton [simp] (a : A) : a ∈ singleton a :=
|
2015-04-11 16:28:27 +00:00
|
|
|
|
mem_of_mem_list !mem_cons
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem eq_of_mem_singleton {x a : A} : x ∈ singleton a → x = a :=
|
|
|
|
|
list.mem_singleton
|
|
|
|
|
|
|
|
|
|
theorem mem_singleton_eq (x a : A) : (x ∈ singleton a) = (x = a) :=
|
|
|
|
|
propext (iff.intro eq_of_mem_singleton (assume H, eq.subst H !mem_singleton))
|
|
|
|
|
|
2015-07-16 00:44:19 +00:00
|
|
|
|
lemma eq_of_singleton_eq {a b : A} : singleton a = singleton b → a = b :=
|
|
|
|
|
assume Pseq, eq_of_mem_singleton (Pseq ▸ mem_singleton a)
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
definition decidable_mem [instance] [h : decidable_eq A] : ∀ (a : A) (s : finset A), decidable (a ∈ s) :=
|
|
|
|
|
λ a s, quot.rec_on_subsingleton s
|
|
|
|
|
(λ l, match list.decidable_mem a (elt_of l) with
|
|
|
|
|
| decidable.inl p := decidable.inl (mem_of_mem_list p)
|
|
|
|
|
| decidable.inr n := decidable.inr (λ p, absurd (mem_list_of_mem p) n)
|
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
theorem mem_to_finset [h : decidable_eq A] {a : A} {l : list A} : a ∈ l → a ∈ to_finset l :=
|
|
|
|
|
λ ainl, mem_erase_dup ainl
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_to_finset_of_nodup {a : A} {l : list A} (n : nodup l) : a ∈ l → a ∈ to_finset_of_nodup l n :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
λ ainl, ainl
|
|
|
|
|
|
|
|
|
|
/- extensionality -/
|
|
|
|
|
theorem ext {s₁ s₂ : finset A} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ e, quot.sound (perm_ext (has_property l₁) (has_property l₂) e))
|
|
|
|
|
|
|
|
|
|
/- empty -/
|
|
|
|
|
definition empty : finset A :=
|
|
|
|
|
to_finset_of_nodup [] nodup_nil
|
|
|
|
|
|
2015-08-04 20:47:16 +00:00
|
|
|
|
notation [priority finset.prio] `∅` := !empty
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-07-22 16:01:42 +00:00
|
|
|
|
theorem not_mem_empty [simp] (a : A) : a ∉ ∅ :=
|
2015-04-09 22:51:23 +00:00
|
|
|
|
λ aine : a ∈ ∅, aine
|
|
|
|
|
|
2015-07-22 16:01:42 +00:00
|
|
|
|
theorem mem_empty_iff [simp] (x : A) : x ∈ ∅ ↔ false :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
iff_false_intro !not_mem_empty
|
2015-05-17 09:06:10 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_empty_eq (x : A) : x ∈ ∅ = false :=
|
2015-05-17 09:06:10 +00:00
|
|
|
|
propext !mem_empty_iff
|
|
|
|
|
|
|
|
|
|
theorem eq_empty_of_forall_not_mem {s : finset A} (H : ∀x, ¬ x ∈ s) : s = ∅ :=
|
|
|
|
|
ext (take x, iff_false_intro (H x))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
2015-04-11 21:55:41 +00:00
|
|
|
|
/- universe -/
|
|
|
|
|
definition univ [h : fintype A] : finset A :=
|
|
|
|
|
to_finset_of_nodup (@fintype.elems A h) (@fintype.unique A h)
|
|
|
|
|
|
|
|
|
|
theorem mem_univ [h : fintype A] (x : A) : x ∈ univ :=
|
|
|
|
|
fintype.complete x
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_univ_eq [h : fintype A] (x : A) : x ∈ univ = true := propext (iff_true_intro !mem_univ)
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
/- card -/
|
|
|
|
|
definition card (s : finset A) : nat :=
|
|
|
|
|
quot.lift_on s
|
|
|
|
|
(λ l, length (elt_of l))
|
|
|
|
|
(λ l₁ l₂ p, length_eq_length_of_perm p)
|
|
|
|
|
|
|
|
|
|
theorem card_empty : card (@empty A) = 0 :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2015-04-11 16:28:27 +00:00
|
|
|
|
theorem card_singleton (a : A) : card (singleton a) = 1 :=
|
|
|
|
|
rfl
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
lemma ne_empty_of_card_eq_succ {s : finset A} {n : nat} : card s = succ n → s ≠ ∅ :=
|
|
|
|
|
by intros; substvars; contradiction
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
/- insert -/
|
|
|
|
|
section insert
|
|
|
|
|
variable [h : decidable_eq A]
|
|
|
|
|
include h
|
|
|
|
|
|
|
|
|
|
definition insert (a : A) (s : finset A) : finset A :=
|
|
|
|
|
quot.lift_on s
|
|
|
|
|
(λ l, to_finset_of_nodup (insert a (elt_of l)) (nodup_insert a (has_property l)))
|
|
|
|
|
(λ (l₁ l₂ : nodup_list A) (p : l₁ ~ l₂), quot.sound (perm_insert a p))
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
-- set builder notation
|
2015-09-30 15:06:31 +00:00
|
|
|
|
notation [priority finset.prio] `'{`:max a:(foldr `, ` (x b, insert x b) ∅) `}`:0 := a
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
theorem mem_insert (a : A) (s : finset A) : a ∈ insert a s :=
|
|
|
|
|
quot.induction_on s
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ l : nodup_list A, mem_to_finset_of_nodup _ !list.mem_insert)
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
|
|
|
|
theorem mem_insert_of_mem {a : A} {s : finset A} (b : A) : a ∈ s → a ∈ insert b s :=
|
|
|
|
|
quot.induction_on s
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ (l : nodup_list A) (ainl : a ∈ ⟦l⟧), mem_to_finset_of_nodup _ (list.mem_insert_of_mem _ ainl))
|
|
|
|
|
|
|
|
|
|
theorem eq_or_mem_of_mem_insert {x a : A} {s : finset A} : x ∈ insert a s → x = a ∨ x ∈ s :=
|
|
|
|
|
quot.induction_on s (λ l : nodup_list A, λ H, list.eq_or_mem_of_mem_insert H)
|
|
|
|
|
|
2015-07-24 15:56:18 +00:00
|
|
|
|
theorem mem_of_mem_insert_of_ne {x a : A} {s : finset A} (xin : x ∈ insert a s) : x ≠ a → x ∈ s :=
|
|
|
|
|
or_resolve_right (eq_or_mem_of_mem_insert xin)
|
2015-06-20 02:07:05 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_insert_eq (x a : A) (s : finset A) : x ∈ insert a s = (x = a ∨ x ∈ s) :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
propext (iff.intro !eq_or_mem_of_mem_insert
|
|
|
|
|
(or.rec (λH', (eq.substr H' !mem_insert)) !mem_insert_of_mem))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
2015-07-11 02:01:48 +00:00
|
|
|
|
theorem insert_empty_eq (a : A) : '{a} = singleton a := rfl
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem insert_eq_of_mem {a : A} {s : finset A} (H : a ∈ s) : insert a s = s :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
ext (λ x, eq.substr (mem_insert_eq x a s)
|
|
|
|
|
(or_iff_right_of_imp (λH1, eq.substr H1 H)))
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
2015-07-27 17:51:11 +00:00
|
|
|
|
-- useful in proofs by induction
|
|
|
|
|
theorem forall_of_forall_insert {P : A → Prop} {a : A} {s : finset A}
|
|
|
|
|
(H : ∀ x, x ∈ insert a s → P x) :
|
|
|
|
|
∀ x, x ∈ s → P x :=
|
|
|
|
|
λ x xs, H x (!mem_insert_of_mem xs)
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem insert.comm (x y : A) (s : finset A) : insert x (insert y s) = insert y (insert x s) :=
|
|
|
|
|
ext (take a, by rewrite [*mem_insert_eq, propext !or.left_comm])
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
theorem card_insert_of_mem {a : A} {s : finset A} : a ∈ s → card (insert a s) = card s :=
|
|
|
|
|
quot.induction_on s
|
|
|
|
|
(λ (l : nodup_list A) (ainl : a ∈ ⟦l⟧), list.length_insert_of_mem ainl)
|
|
|
|
|
|
|
|
|
|
theorem card_insert_of_not_mem {a : A} {s : finset A} : a ∉ s → card (insert a s) = card s + 1 :=
|
|
|
|
|
quot.induction_on s
|
|
|
|
|
(λ (l : nodup_list A) (nainl : a ∉ ⟦l⟧), list.length_insert_of_not_mem nainl)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
2015-06-10 07:39:50 +00:00
|
|
|
|
theorem card_insert_le (a : A) (s : finset A) :
|
|
|
|
|
card (insert a s) ≤ card s + 1 :=
|
2015-07-24 15:56:18 +00:00
|
|
|
|
if H : a ∈ s then by rewrite [card_insert_of_mem H]; apply le_succ
|
|
|
|
|
else by rewrite [card_insert_of_not_mem H]
|
2015-06-10 07:39:50 +00:00
|
|
|
|
|
2015-05-19 21:18:36 +00:00
|
|
|
|
protected theorem induction [recursor 6] {P : finset A → Prop}
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(H1 : P empty)
|
2015-05-25 08:37:07 +00:00
|
|
|
|
(H2 : ∀ ⦃a : A⦄, ∀{s : finset A}, a ∉ s → P s → P (insert a s)) :
|
2015-05-08 03:10:17 +00:00
|
|
|
|
∀s, P s :=
|
|
|
|
|
take s,
|
|
|
|
|
quot.induction_on s
|
2015-05-08 18:02:06 +00:00
|
|
|
|
(take u,
|
2015-05-08 03:10:17 +00:00
|
|
|
|
subtype.destruct u
|
2015-05-08 18:02:06 +00:00
|
|
|
|
(take l,
|
2015-05-08 03:10:17 +00:00
|
|
|
|
list.induction_on l
|
|
|
|
|
(assume nodup_l, H1)
|
|
|
|
|
(take a l',
|
|
|
|
|
assume IH nodup_al',
|
2015-07-21 15:26:44 +00:00
|
|
|
|
have a ∉ l', from not_mem_of_nodup_cons nodup_al',
|
|
|
|
|
assert e : list.insert a l' = a :: l', from insert_eq_of_not_mem this,
|
|
|
|
|
assert nodup l', from nodup_of_nodup_cons nodup_al',
|
|
|
|
|
assert P (quot.mk (subtype.tag l' this)), from IH this,
|
|
|
|
|
assert P (insert a (quot.mk (subtype.tag l' _))), from H2 `a ∉ l'` this,
|
2015-05-08 18:02:06 +00:00
|
|
|
|
begin
|
|
|
|
|
revert nodup_al',
|
2015-07-21 15:26:44 +00:00
|
|
|
|
rewrite [-e],
|
2015-05-08 18:02:06 +00:00
|
|
|
|
intros,
|
2015-07-21 15:26:44 +00:00
|
|
|
|
apply this
|
2015-05-08 18:02:06 +00:00
|
|
|
|
end)))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
protected theorem induction_on {P : finset A → Prop} (s : finset A)
|
|
|
|
|
(H1 : P empty)
|
2015-05-25 08:37:07 +00:00
|
|
|
|
(H2 : ∀ ⦃a : A⦄, ∀ {s : finset A}, a ∉ s → P s → P (insert a s)) :
|
2015-05-08 03:10:17 +00:00
|
|
|
|
P s :=
|
2015-05-19 05:35:18 +00:00
|
|
|
|
finset.induction H1 H2 s
|
2015-06-20 02:07:05 +00:00
|
|
|
|
|
|
|
|
|
theorem exists_of_not_empty {s : finset A} : s ≠ ∅ → ∃ a : A, a ∈ s :=
|
|
|
|
|
begin
|
|
|
|
|
induction s with a s nin ih,
|
|
|
|
|
{intro h, exact absurd rfl h},
|
|
|
|
|
{intro h, existsi a, apply mem_insert}
|
|
|
|
|
end
|
2015-06-20 02:23:14 +00:00
|
|
|
|
|
2015-06-20 11:37:37 +00:00
|
|
|
|
theorem eq_empty_of_card_eq_zero {s : finset A} (H : card s = 0) : s = ∅ :=
|
|
|
|
|
begin
|
|
|
|
|
induction s with a s' H1 IH,
|
|
|
|
|
{ reflexivity },
|
|
|
|
|
{ rewrite (card_insert_of_not_mem H1) at H, apply nat.no_confusion H}
|
|
|
|
|
end
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
end insert
|
|
|
|
|
|
|
|
|
|
/- erase -/
|
|
|
|
|
section erase
|
|
|
|
|
variable [h : decidable_eq A]
|
|
|
|
|
include h
|
|
|
|
|
|
|
|
|
|
definition erase (a : A) (s : finset A) : finset A :=
|
|
|
|
|
quot.lift_on s
|
|
|
|
|
(λ l, to_finset_of_nodup (erase a (elt_of l)) (nodup_erase_of_nodup a (has_property l)))
|
|
|
|
|
(λ (l₁ l₂ : nodup_list A) (p : l₁ ~ l₂), quot.sound (erase_perm_erase_of_perm a p))
|
|
|
|
|
|
|
|
|
|
theorem mem_erase (a : A) (s : finset A) : a ∉ erase a s :=
|
|
|
|
|
quot.induction_on s
|
|
|
|
|
(λ l, list.mem_erase_of_nodup _ (has_property l))
|
|
|
|
|
|
|
|
|
|
theorem card_erase_of_mem {a : A} {s : finset A} : a ∈ s → card (erase a s) = pred (card s) :=
|
|
|
|
|
quot.induction_on s (λ l ainl, list.length_erase_of_mem ainl)
|
|
|
|
|
|
|
|
|
|
theorem card_erase_of_not_mem {a : A} {s : finset A} : a ∉ s → card (erase a s) = card s :=
|
|
|
|
|
quot.induction_on s (λ l nainl, list.length_erase_of_not_mem nainl)
|
2015-06-20 02:07:05 +00:00
|
|
|
|
|
|
|
|
|
theorem erase_empty (a : A) : erase a ∅ = ∅ :=
|
|
|
|
|
rfl
|
|
|
|
|
|
|
|
|
|
theorem ne_of_mem_erase {a b : A} {s : finset A} : b ∈ erase a s → b ≠ a :=
|
|
|
|
|
by intro h beqa; subst b; exact absurd h !mem_erase
|
|
|
|
|
|
|
|
|
|
theorem mem_of_mem_erase {a b : A} {s : finset A} : b ∈ erase a s → b ∈ s :=
|
|
|
|
|
quot.induction_on s (λ l bin, mem_of_mem_erase bin)
|
|
|
|
|
|
|
|
|
|
theorem mem_erase_of_ne_of_mem {a b : A} {s : finset A} : a ≠ b → a ∈ s → a ∈ erase b s :=
|
|
|
|
|
quot.induction_on s (λ l n ain, list.mem_erase_of_ne_of_mem n ain)
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem mem_erase_iff (a b : A) (s : finset A) : a ∈ erase b s ↔ a ∈ s ∧ a ≠ b :=
|
|
|
|
|
iff.intro
|
|
|
|
|
(assume H, and.intro (mem_of_mem_erase H) (ne_of_mem_erase H))
|
|
|
|
|
(assume H, mem_erase_of_ne_of_mem (and.right H) (and.left H))
|
|
|
|
|
|
|
|
|
|
theorem mem_erase_eq (a b : A) (s : finset A) : a ∈ erase b s = (a ∈ s ∧ a ≠ b) :=
|
|
|
|
|
propext !mem_erase_iff
|
|
|
|
|
|
2015-06-20 02:07:05 +00:00
|
|
|
|
open decidable
|
2015-07-27 03:22:05 +00:00
|
|
|
|
theorem erase_insert {a : A} {s : finset A} : a ∉ s → erase a (insert a s) = s :=
|
2015-06-20 02:07:05 +00:00
|
|
|
|
λ anins, finset.ext (λ b, by_cases
|
|
|
|
|
(λ beqa : b = a, iff.intro
|
|
|
|
|
(λ bin, by subst b; exact absurd bin !mem_erase)
|
|
|
|
|
(λ bin, by subst b; contradiction))
|
|
|
|
|
(λ bnea : b ≠ a, iff.intro
|
|
|
|
|
(λ bin,
|
2015-07-21 15:26:44 +00:00
|
|
|
|
assert b ∈ insert a s, from mem_of_mem_erase bin,
|
|
|
|
|
mem_of_mem_insert_of_ne this bnea)
|
2015-06-20 02:07:05 +00:00
|
|
|
|
(λ bin,
|
2015-07-21 15:26:44 +00:00
|
|
|
|
have b ∈ insert a s, from mem_insert_of_mem _ bin,
|
|
|
|
|
mem_erase_of_ne_of_mem bnea this)))
|
2015-06-20 02:07:05 +00:00
|
|
|
|
|
|
|
|
|
theorem insert_erase {a : A} {s : finset A} : a ∈ s → insert a (erase a s) = s :=
|
|
|
|
|
λ ains, finset.ext (λ b, by_cases
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose b = a, iff.intro
|
2015-06-20 02:07:05 +00:00
|
|
|
|
(λ bin, by subst b; assumption)
|
|
|
|
|
(λ bin, by subst b; apply mem_insert))
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose b ≠ a, iff.intro
|
|
|
|
|
(λ bin, mem_of_mem_erase (mem_of_mem_insert_of_ne bin `b ≠ a`))
|
|
|
|
|
(λ bin, mem_insert_of_mem _ (mem_erase_of_ne_of_mem `b ≠ a` bin))))
|
2015-04-09 22:51:23 +00:00
|
|
|
|
end erase
|
|
|
|
|
|
|
|
|
|
/- union -/
|
|
|
|
|
section union
|
|
|
|
|
variable [h : decidable_eq A]
|
|
|
|
|
include h
|
|
|
|
|
|
|
|
|
|
definition union (s₁ s₂ : finset A) : finset A :=
|
|
|
|
|
quot.lift_on₂ s₁ s₂
|
|
|
|
|
(λ l₁ l₂,
|
|
|
|
|
to_finset_of_nodup (list.union (elt_of l₁) (elt_of l₂))
|
|
|
|
|
(nodup_union_of_nodup_of_nodup (has_property l₁) (has_property l₂)))
|
|
|
|
|
(λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_union p₁ p₂))
|
|
|
|
|
|
2015-08-04 20:47:16 +00:00
|
|
|
|
infix [priority finset.prio] ∪ := union
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
|
|
|
|
theorem mem_union_left {a : A} {s₁ : finset A} (s₂ : finset A) : a ∈ s₁ → a ∈ s₁ ∪ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁, list.mem_union_left _ ainl₁)
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_union_l {a : A} {s₁ : finset A} {s₂ : finset A} : a ∈ s₁ → a ∈ s₁ ∪ s₂ :=
|
|
|
|
|
mem_union_left s₂
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
theorem mem_union_right {a : A} {s₂ : finset A} (s₁ : finset A) : a ∈ s₂ → a ∈ s₁ ∪ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₂, list.mem_union_right _ ainl₂)
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_union_r {a : A} {s₂ : finset A} {s₁ : finset A} : a ∈ s₂ → a ∈ s₁ ∪ s₂ :=
|
|
|
|
|
mem_union_right s₁
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
theorem mem_or_mem_of_mem_union {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∪ s₂ → a ∈ s₁ ∨ a ∈ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_or_mem_of_mem_union ainl₁l₂)
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_union_iff (a : A) (s₁ s₂ : finset A) : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ :=
|
|
|
|
|
iff.intro
|
2015-04-09 22:51:23 +00:00
|
|
|
|
(λ h, mem_or_mem_of_mem_union h)
|
|
|
|
|
(λ d, or.elim d
|
|
|
|
|
(λ i, mem_union_left _ i)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ i, mem_union_right _ i))
|
|
|
|
|
|
|
|
|
|
theorem mem_union_eq (a : A) (s₁ s₂ : finset A) : (a ∈ s₁ ∪ s₂) = (a ∈ s₁ ∨ a ∈ s₂) :=
|
|
|
|
|
propext !mem_union_iff
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
|
|
|
|
theorem union.comm (s₁ s₂ : finset A) : s₁ ∪ s₂ = s₂ ∪ s₁ :=
|
|
|
|
|
ext (λ a, by rewrite [*mem_union_eq]; exact or.comm)
|
|
|
|
|
|
|
|
|
|
theorem union.assoc (s₁ s₂ s₃ : finset A) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) :=
|
|
|
|
|
ext (λ a, by rewrite [*mem_union_eq]; exact or.assoc)
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem union.left_comm (s₁ s₂ s₃ : finset A) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) :=
|
|
|
|
|
!left_comm union.comm union.assoc s₁ s₂ s₃
|
|
|
|
|
|
|
|
|
|
theorem union.right_comm (s₁ s₂ s₃ : finset A) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ :=
|
|
|
|
|
!right_comm union.comm union.assoc s₁ s₂ s₃
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
theorem union_self (s : finset A) : s ∪ s = s :=
|
|
|
|
|
ext (λ a, iff.intro
|
|
|
|
|
(λ ain, or.elim (mem_or_mem_of_mem_union ain) (λ i, i) (λ i, i))
|
|
|
|
|
(λ i, mem_union_left _ i))
|
|
|
|
|
|
|
|
|
|
theorem union_empty (s : finset A) : s ∪ ∅ = s :=
|
|
|
|
|
ext (λ a, iff.intro
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose a ∈ s ∪ ∅, or.elim (mem_or_mem_of_mem_union this) (λ i, i) (λ i, absurd i !not_mem_empty))
|
|
|
|
|
(suppose a ∈ s, mem_union_left _ this))
|
2015-04-09 22:51:23 +00:00
|
|
|
|
|
|
|
|
|
theorem empty_union (s : finset A) : ∅ ∪ s = s :=
|
|
|
|
|
calc ∅ ∪ s = s ∪ ∅ : union.comm
|
|
|
|
|
... = s : union_empty
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem insert_eq (a : A) (s : finset A) : insert a s = singleton a ∪ s :=
|
|
|
|
|
ext (take x,
|
|
|
|
|
calc
|
|
|
|
|
x ∈ insert a s ↔ x ∈ insert a s : iff.refl
|
|
|
|
|
... = (x = a ∨ x ∈ s) : mem_insert_eq
|
|
|
|
|
... = (x ∈ singleton a ∨ x ∈ s) : mem_singleton_eq
|
2015-07-11 02:01:48 +00:00
|
|
|
|
... = (x ∈ '{a} ∪ s) : mem_union_eq)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem insert_union (a : A) (s t : finset A) : insert a (s ∪ t) = insert a s ∪ t :=
|
|
|
|
|
by rewrite [*insert_eq, union.assoc]
|
2015-04-09 22:51:23 +00:00
|
|
|
|
end union
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
/- inter -/
|
|
|
|
|
section inter
|
2015-04-10 23:45:25 +00:00
|
|
|
|
variable [h : decidable_eq A]
|
|
|
|
|
include h
|
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
definition inter (s₁ s₂ : finset A) : finset A :=
|
2015-04-10 23:45:25 +00:00
|
|
|
|
quot.lift_on₂ s₁ s₂
|
|
|
|
|
(λ l₁ l₂,
|
2015-05-05 15:53:31 +00:00
|
|
|
|
to_finset_of_nodup (list.inter (elt_of l₁) (elt_of l₂))
|
|
|
|
|
(nodup_inter_of_nodup _ (has_property l₁)))
|
|
|
|
|
(λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_inter p₁ p₂))
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-08-04 20:47:16 +00:00
|
|
|
|
infix [priority finset.prio] ∩ := inter
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem mem_of_mem_inter_left {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∩ s₂ → a ∈ s₁ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_of_mem_inter_left ainl₁l₂)
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem mem_of_mem_inter_right {a : A} {s₁ s₂ : finset A} : a ∈ s₁ ∩ s₂ → a ∈ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁l₂, list.mem_of_mem_inter_right ainl₁l₂)
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_inter {a : A} {s₁ s₂ : finset A} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ :=
|
2015-05-05 15:53:31 +00:00
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ ainl₁ ainl₂, list.mem_inter_of_mem_of_mem ainl₁ ainl₂)
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_inter_iff (a : A) (s₁ s₂ : finset A) : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ :=
|
|
|
|
|
iff.intro
|
2015-05-05 15:53:31 +00:00
|
|
|
|
(λ h, and.intro (mem_of_mem_inter_left h) (mem_of_mem_inter_right h))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ h, mem_inter (and.elim_left h) (and.elim_right h))
|
|
|
|
|
|
|
|
|
|
theorem mem_inter_eq (a : A) (s₁ s₂ : finset A) : (a ∈ s₁ ∩ s₂) = (a ∈ s₁ ∧ a ∈ s₂) :=
|
|
|
|
|
propext !mem_inter_iff
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem inter.comm (s₁ s₂ : finset A) : s₁ ∩ s₂ = s₂ ∩ s₁ :=
|
|
|
|
|
ext (λ a, by rewrite [*mem_inter_eq]; exact and.comm)
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem inter.assoc (s₁ s₂ s₃ : finset A) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) :=
|
|
|
|
|
ext (λ a, by rewrite [*mem_inter_eq]; exact and.assoc)
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem inter.left_comm (s₁ s₂ s₃ : finset A) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) :=
|
|
|
|
|
!left_comm inter.comm inter.assoc s₁ s₂ s₃
|
|
|
|
|
|
|
|
|
|
theorem inter.right_comm (s₁ s₂ s₃ : finset A) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ :=
|
|
|
|
|
!right_comm inter.comm inter.assoc s₁ s₂ s₃
|
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem inter_self (s : finset A) : s ∩ s = s :=
|
2015-04-10 23:45:25 +00:00
|
|
|
|
ext (λ a, iff.intro
|
2015-05-05 15:53:31 +00:00
|
|
|
|
(λ h, mem_of_mem_inter_right h)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ h, mem_inter h h))
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem inter_empty (s : finset A) : s ∩ ∅ = ∅ :=
|
2015-04-10 23:45:25 +00:00
|
|
|
|
ext (λ a, iff.intro
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose a ∈ s ∩ ∅, absurd (mem_of_mem_inter_right this) !not_mem_empty)
|
|
|
|
|
(suppose a ∈ ∅, absurd this !not_mem_empty))
|
2015-04-10 23:45:25 +00:00
|
|
|
|
|
2015-05-05 15:53:31 +00:00
|
|
|
|
theorem empty_inter (s : finset A) : ∅ ∩ s = ∅ :=
|
|
|
|
|
calc ∅ ∩ s = s ∩ ∅ : inter.comm
|
|
|
|
|
... = ∅ : inter_empty
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem singleton_inter_of_mem {a : A} {s : finset A} (H : a ∈ s) :
|
|
|
|
|
singleton a ∩ s = singleton a :=
|
|
|
|
|
ext (take x,
|
|
|
|
|
begin
|
|
|
|
|
rewrite [mem_inter_eq, !mem_singleton_eq],
|
|
|
|
|
exact iff.intro
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose x = a ∧ x ∈ s, and.left this)
|
|
|
|
|
(suppose x = a, and.intro this (eq.subst (eq.symm this) H))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
theorem singleton_inter_of_not_mem {a : A} {s : finset A} (H : a ∉ s) :
|
|
|
|
|
singleton a ∩ s = ∅ :=
|
|
|
|
|
ext (take x,
|
|
|
|
|
begin
|
|
|
|
|
rewrite [mem_inter_eq, !mem_singleton_eq, mem_empty_eq],
|
|
|
|
|
exact iff.intro
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose x = a ∧ x ∈ s, H (eq.subst (and.left this) (and.right this)))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(false.elim)
|
|
|
|
|
end)
|
2015-05-05 15:53:31 +00:00
|
|
|
|
end inter
|
2015-04-11 16:28:27 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
/- distributivity laws -/
|
|
|
|
|
section inter
|
|
|
|
|
variable [h : decidable_eq A]
|
|
|
|
|
include h
|
|
|
|
|
|
|
|
|
|
theorem inter.distrib_left (s t u : finset A) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) :=
|
2015-07-27 03:22:05 +00:00
|
|
|
|
ext (take x, by rewrite [mem_inter_eq, *mem_union_eq, *mem_inter_eq]; apply and.left_distrib)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem inter.distrib_right (s t u : finset A) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) :=
|
2015-07-27 03:22:05 +00:00
|
|
|
|
ext (take x, by rewrite [mem_inter_eq, *mem_union_eq, *mem_inter_eq]; apply and.right_distrib)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem union.distrib_left (s t u : finset A) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) :=
|
2015-07-27 03:22:05 +00:00
|
|
|
|
ext (take x, by rewrite [mem_union_eq, *mem_inter_eq, *mem_union_eq]; apply or.left_distrib)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem union.distrib_right (s t u : finset A) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) :=
|
2015-07-27 03:22:05 +00:00
|
|
|
|
ext (take x, by rewrite [mem_union_eq, *mem_inter_eq, *mem_union_eq]; apply or.right_distrib)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
end inter
|
|
|
|
|
|
|
|
|
|
/- disjoint -/
|
|
|
|
|
-- Mainly for internal use; library will use s₁ ∩ s₂ = ∅. Note that it does not require decidable equality.
|
|
|
|
|
definition disjoint (s₁ s₂ : finset A) : Prop :=
|
|
|
|
|
quot.lift_on₂ s₁ s₂ (λ l₁ l₂, disjoint (elt_of l₁) (elt_of l₂))
|
|
|
|
|
(λ v₁ v₂ w₁ w₂ p₁ p₂, propext (iff.intro
|
|
|
|
|
(λ d₁ a (ainw₁ : a ∈ elt_of w₁),
|
2015-07-21 15:26:44 +00:00
|
|
|
|
have a ∈ elt_of v₁, from mem_perm (perm.symm p₁) ainw₁,
|
|
|
|
|
have a ∉ elt_of v₂, from disjoint_left d₁ this,
|
|
|
|
|
not_mem_perm p₂ this)
|
2015-05-08 03:10:17 +00:00
|
|
|
|
(λ d₂ a (ainv₁ : a ∈ elt_of v₁),
|
2015-07-21 15:26:44 +00:00
|
|
|
|
have a ∈ elt_of w₁, from mem_perm p₁ ainv₁,
|
|
|
|
|
have a ∉ elt_of w₂, from disjoint_left d₂ this,
|
|
|
|
|
not_mem_perm (perm.symm p₂) this)))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem disjoint.elim {s₁ s₂ : finset A} {x : A} :
|
|
|
|
|
disjoint s₁ s₂ → x ∈ s₁ → x ∈ s₂ → false :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (take u₁ u₂, assume H H1 H2, H x H1 H2)
|
|
|
|
|
|
|
|
|
|
theorem disjoint.intro {s₁ s₂ : finset A} : (∀{x : A}, x ∈ s₁ → x ∈ s₂ → false) → disjoint s₁ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (take u₁ u₂, assume H, H)
|
|
|
|
|
|
2015-05-17 09:06:10 +00:00
|
|
|
|
theorem inter_eq_empty_of_disjoint [h : decidable_eq A] {s₁ s₂ : finset A} (H : disjoint s₁ s₂) : s₁ ∩ s₂ = ∅ :=
|
2015-05-08 03:10:17 +00:00
|
|
|
|
ext (take x, iff_false_intro (assume H1,
|
|
|
|
|
disjoint.elim H (mem_of_mem_inter_left H1) (mem_of_mem_inter_right H1)))
|
|
|
|
|
|
2015-05-17 09:06:10 +00:00
|
|
|
|
theorem disjoint_of_inter_eq_empty [h : decidable_eq A] {s₁ s₂ : finset A} (H : s₁ ∩ s₂ = ∅) : disjoint s₁ s₂ :=
|
2015-05-08 03:10:17 +00:00
|
|
|
|
disjoint.intro (take x H1 H2,
|
2015-07-21 15:26:44 +00:00
|
|
|
|
have x ∈ s₁ ∩ s₂, from mem_inter H1 H2,
|
|
|
|
|
!not_mem_empty (eq.subst H this))
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem disjoint.comm {s₁ s₂ : finset A} : disjoint s₁ s₂ → disjoint s₂ s₁ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ d, list.disjoint.comm d)
|
|
|
|
|
|
2015-05-17 09:06:10 +00:00
|
|
|
|
theorem inter_eq_empty [h : decidable_eq A] {s₁ s₂ : finset A}
|
|
|
|
|
(H : ∀x : A, x ∈ s₁ → x ∈ s₂ → false) : s₁ ∩ s₂ = ∅ :=
|
|
|
|
|
inter_eq_empty_of_disjoint (disjoint.intro H)
|
|
|
|
|
|
2015-04-11 22:29:12 +00:00
|
|
|
|
/- subset -/
|
|
|
|
|
definition subset (s₁ s₂ : finset A) : Prop :=
|
|
|
|
|
quot.lift_on₂ s₁ s₂
|
|
|
|
|
(λ l₁ l₂, sublist (elt_of l₁) (elt_of l₂))
|
|
|
|
|
(λ v₁ v₂ w₁ w₂ p₁ p₂, propext (iff.intro
|
|
|
|
|
(λ s₁ a i, mem_perm p₂ (s₁ a (mem_perm (perm.symm p₁) i)))
|
|
|
|
|
(λ s₂ a i, mem_perm (perm.symm p₂) (s₂ a (mem_perm p₁ i)))))
|
|
|
|
|
|
2015-09-30 15:06:31 +00:00
|
|
|
|
infix [priority finset.prio] ⊆ := subset
|
2015-04-11 22:29:12 +00:00
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem empty_subset (s : finset A) : ∅ ⊆ s :=
|
2015-04-11 22:29:12 +00:00
|
|
|
|
quot.induction_on s (λ l, list.nil_sub (elt_of l))
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem subset_univ [h : fintype A] (s : finset A) : s ⊆ univ :=
|
2015-04-11 22:29:12 +00:00
|
|
|
|
quot.induction_on s (λ l a i, fintype.complete a)
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem subset.refl (s : finset A) : s ⊆ s :=
|
2015-04-11 22:29:12 +00:00
|
|
|
|
quot.induction_on s (λ l, list.sub.refl (elt_of l))
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem subset.trans {s₁ s₂ s₃ : finset A} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ :=
|
2015-04-11 22:29:12 +00:00
|
|
|
|
quot.induction_on₃ s₁ s₂ s₃ (λ l₁ l₂ l₃ h₁ h₂, list.sub.trans h₁ h₂)
|
|
|
|
|
|
2015-05-08 03:10:17 +00:00
|
|
|
|
theorem mem_of_subset_of_mem {s₁ s₂ : finset A} {a : A} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ :=
|
2015-04-11 22:29:12 +00:00
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h₁ h₂, h₁ a h₂)
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem subset.antisymm {s₁ s₂ : finset A} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
|
|
|
|
|
ext (take x, iff.intro (assume H, mem_of_subset_of_mem H₁ H) (assume H, mem_of_subset_of_mem H₂ H))
|
|
|
|
|
|
|
|
|
|
-- alternative name
|
|
|
|
|
theorem eq_of_subset_of_subset {s₁ s₂ : finset A} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ :=
|
|
|
|
|
subset.antisymm H₁ H₂
|
|
|
|
|
|
2015-05-10 07:44:08 +00:00
|
|
|
|
theorem subset_of_forall {s₁ s₂ : finset A} : (∀x, x ∈ s₁ → x ∈ s₂) → s₁ ⊆ s₂ :=
|
|
|
|
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ H, H)
|
|
|
|
|
|
2015-05-10 10:07:03 +00:00
|
|
|
|
theorem subset_insert [h : decidable_eq A] (s : finset A) (a : A) : s ⊆ insert a s :=
|
2015-07-21 15:26:44 +00:00
|
|
|
|
subset_of_forall (take x, suppose x ∈ s, mem_insert_of_mem _ this)
|
2015-05-10 10:07:03 +00:00
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem eq_empty_of_subset_empty {x : finset A} (H : x ⊆ ∅) : x = ∅ :=
|
|
|
|
|
subset.antisymm H (empty_subset x)
|
|
|
|
|
|
|
|
|
|
theorem subset_empty_iff (x : finset A) : x ⊆ ∅ ↔ x = ∅ :=
|
|
|
|
|
iff.intro eq_empty_of_subset_empty (take xeq, by rewrite xeq; apply subset.refl ∅)
|
2015-05-10 10:07:03 +00:00
|
|
|
|
|
2015-06-20 02:35:13 +00:00
|
|
|
|
section
|
|
|
|
|
variable [decA : decidable_eq A]
|
|
|
|
|
include decA
|
|
|
|
|
|
2015-07-25 17:38:24 +00:00
|
|
|
|
theorem erase_subset_erase (a : A) {s t : finset A} (H : s ⊆ t) : erase a s ⊆ erase a t :=
|
|
|
|
|
begin
|
|
|
|
|
apply subset_of_forall,
|
|
|
|
|
intro x,
|
|
|
|
|
rewrite *mem_erase_eq,
|
|
|
|
|
intro H',
|
|
|
|
|
show x ∈ t ∧ x ≠ a, from and.intro (mem_of_subset_of_mem H (and.left H')) (and.right H')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
theorem erase_subset (a : A) (s : finset A) : erase a s ⊆ s :=
|
|
|
|
|
begin
|
|
|
|
|
apply subset_of_forall,
|
|
|
|
|
intro x,
|
|
|
|
|
rewrite mem_erase_eq,
|
|
|
|
|
intro H,
|
|
|
|
|
apply and.left H
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
theorem erase_eq_of_not_mem {a : A} {s : finset A} (anins : a ∉ s) : erase a s = s :=
|
|
|
|
|
eq_of_subset_of_subset !erase_subset
|
|
|
|
|
(subset_of_forall (take x, assume xs : x ∈ s,
|
|
|
|
|
have x ≠ a, from assume H', anins (eq.subst H' xs),
|
|
|
|
|
mem_erase_of_ne_of_mem this xs))
|
|
|
|
|
|
|
|
|
|
theorem erase_insert_subset (a : A) (s : finset A) : erase a (insert a s) ⊆ s :=
|
|
|
|
|
decidable.by_cases
|
|
|
|
|
(assume ains : a ∈ s, by rewrite [insert_eq_of_mem ains]; apply erase_subset)
|
|
|
|
|
(assume nains : a ∉ s, by rewrite [!erase_insert nains]; apply subset.refl)
|
|
|
|
|
|
|
|
|
|
theorem erase_subset_of_subset_insert {a : A} {s t : finset A} (H : s ⊆ insert a t) :
|
|
|
|
|
erase a s ⊆ t :=
|
|
|
|
|
subset.trans (!erase_subset_erase H) !erase_insert_subset
|
|
|
|
|
|
|
|
|
|
theorem insert_erase_subset (a : A) (s : finset A) : s ⊆ insert a (erase a s) :=
|
|
|
|
|
decidable.by_cases
|
|
|
|
|
(assume ains : a ∈ s, by rewrite [!insert_erase ains]; apply subset.refl)
|
|
|
|
|
(assume nains : a ∉ s, by rewrite[erase_eq_of_not_mem nains]; apply subset_insert)
|
|
|
|
|
|
|
|
|
|
theorem insert_subset_insert (a : A) {s t : finset A} (H : s ⊆ t) : insert a s ⊆ insert a t :=
|
|
|
|
|
begin
|
|
|
|
|
apply subset_of_forall,
|
|
|
|
|
intro x,
|
|
|
|
|
rewrite *mem_insert_eq,
|
|
|
|
|
intro H',
|
|
|
|
|
cases H' with [xeqa, xins],
|
|
|
|
|
exact (or.inl xeqa),
|
|
|
|
|
exact (or.inr (mem_of_subset_of_mem H xins))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
theorem subset_insert_of_erase_subset {s t : finset A} {a : A} (H : erase a s ⊆ t) :
|
|
|
|
|
s ⊆ insert a t :=
|
|
|
|
|
subset.trans (insert_erase_subset a s) (!insert_subset_insert H)
|
|
|
|
|
|
|
|
|
|
theorem subset_insert_iff (s t : finset A) (a : A) : s ⊆ insert a t ↔ erase a s ⊆ t :=
|
|
|
|
|
iff.intro !erase_subset_of_subset_insert !subset_insert_of_erase_subset
|
|
|
|
|
|
2015-06-20 02:35:13 +00:00
|
|
|
|
end
|
|
|
|
|
|
2015-04-11 16:28:27 +00:00
|
|
|
|
/- upto -/
|
|
|
|
|
section upto
|
|
|
|
|
definition upto (n : nat) : finset nat :=
|
|
|
|
|
to_finset_of_nodup (list.upto n) (nodup_upto n)
|
|
|
|
|
|
|
|
|
|
theorem card_upto : ∀ n, card (upto n) = n :=
|
|
|
|
|
list.length_upto
|
|
|
|
|
|
|
|
|
|
theorem lt_of_mem_upto {n a : nat} : a ∈ upto n → a < n :=
|
|
|
|
|
list.lt_of_mem_upto
|
|
|
|
|
|
|
|
|
|
theorem mem_upto_succ_of_mem_upto {n a : nat} : a ∈ upto n → a ∈ upto (succ n) :=
|
|
|
|
|
list.mem_upto_succ_of_mem_upto
|
|
|
|
|
|
|
|
|
|
theorem mem_upto_of_lt {n a : nat} : a < n → a ∈ upto n :=
|
|
|
|
|
list.mem_upto_of_lt
|
2015-05-08 03:10:17 +00:00
|
|
|
|
|
|
|
|
|
theorem mem_upto_iff (a n : nat) : a ∈ upto n ↔ a < n :=
|
|
|
|
|
iff.intro lt_of_mem_upto mem_upto_of_lt
|
|
|
|
|
|
|
|
|
|
theorem mem_upto_eq (a n : nat) : a ∈ upto n = (a < n) :=
|
|
|
|
|
propext !mem_upto_iff
|
2015-04-11 16:28:27 +00:00
|
|
|
|
end upto
|
2015-05-17 07:49:02 +00:00
|
|
|
|
|
|
|
|
|
/- useful rules for calculations with quantifiers -/
|
|
|
|
|
theorem exists_mem_empty_iff {A : Type} (P : A → Prop) : (∃ x, x ∈ ∅ ∧ P x) ↔ false :=
|
|
|
|
|
iff.intro
|
|
|
|
|
(assume H,
|
|
|
|
|
obtain x (H1 : x ∈ ∅ ∧ P x), from H,
|
|
|
|
|
!not_mem_empty (and.left H1))
|
|
|
|
|
(assume H, false.elim H)
|
|
|
|
|
|
|
|
|
|
theorem exists_mem_empty_eq {A : Type} (P : A → Prop) : (∃ x, x ∈ ∅ ∧ P x) = false :=
|
|
|
|
|
propext !exists_mem_empty_iff
|
|
|
|
|
|
|
|
|
|
theorem exists_mem_insert_iff {A : Type} [d : decidable_eq A]
|
|
|
|
|
(a : A) (s : finset A) (P : A → Prop) :
|
|
|
|
|
(∃ x, x ∈ insert a s ∧ P x) ↔ P a ∨ (∃ x, x ∈ s ∧ P x) :=
|
|
|
|
|
iff.intro
|
|
|
|
|
(assume H,
|
|
|
|
|
obtain x [H1 H2], from H,
|
|
|
|
|
or.elim (eq_or_mem_of_mem_insert H1)
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose x = a, or.inl (eq.subst this H2))
|
|
|
|
|
(suppose x ∈ s, or.inr (exists.intro x (and.intro this H2))))
|
2015-05-17 07:49:02 +00:00
|
|
|
|
(assume H,
|
|
|
|
|
or.elim H
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose P a, exists.intro a (and.intro !mem_insert this))
|
|
|
|
|
(suppose ∃ x, x ∈ s ∧ P x,
|
|
|
|
|
obtain x [H2 H3], from this,
|
2015-05-17 07:49:02 +00:00
|
|
|
|
exists.intro x (and.intro (!mem_insert_of_mem H2) H3)))
|
|
|
|
|
|
|
|
|
|
theorem exists_mem_insert_eq {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) :
|
|
|
|
|
(∃ x, x ∈ insert a s ∧ P x) = (P a ∨ (∃ x, x ∈ s ∧ P x)) :=
|
|
|
|
|
propext !exists_mem_insert_iff
|
|
|
|
|
|
|
|
|
|
theorem forall_mem_empty_iff {A : Type} (P : A → Prop) : (∀ x, x ∈ ∅ → P x) ↔ true :=
|
|
|
|
|
iff.intro
|
|
|
|
|
(assume H, trivial)
|
|
|
|
|
(assume H, take x, assume H', absurd H' !not_mem_empty)
|
|
|
|
|
|
|
|
|
|
theorem forall_mem_empty_eq {A : Type} (P : A → Prop) : (∀ x, x ∈ ∅ → P x) = true :=
|
|
|
|
|
propext !forall_mem_empty_iff
|
|
|
|
|
|
|
|
|
|
theorem forall_mem_insert_iff {A : Type} [d : decidable_eq A]
|
|
|
|
|
(a : A) (s : finset A) (P : A → Prop) :
|
|
|
|
|
(∀ x, x ∈ insert a s → P x) ↔ P a ∧ (∀ x, x ∈ s → P x) :=
|
|
|
|
|
iff.intro
|
|
|
|
|
(assume H, and.intro (H _ !mem_insert) (take x, assume H', H _ (!mem_insert_of_mem H')))
|
|
|
|
|
(assume H, take x, assume H' : x ∈ insert a s,
|
|
|
|
|
or.elim (eq_or_mem_of_mem_insert H')
|
2015-07-21 15:26:44 +00:00
|
|
|
|
(suppose x = a, eq.subst (eq.symm this) (and.left H))
|
|
|
|
|
(suppose x ∈ s, and.right H _ this))
|
2015-05-17 07:49:02 +00:00
|
|
|
|
|
|
|
|
|
theorem forall_mem_insert_eq {A : Type} [d : decidable_eq A] (a : A) (s : finset A) (P : A → Prop) :
|
|
|
|
|
(∀ x, x ∈ insert a s → P x) = (P a ∧ (∀ x, x ∈ s → P x)) :=
|
|
|
|
|
propext !forall_mem_insert_iff
|
|
|
|
|
|
2015-04-09 22:51:23 +00:00
|
|
|
|
end finset
|