feat(library/data/{finset,list}/comb.lean): add 'any' for finsets
This commit is contained in:
parent
c5fb3ec6d0
commit
9d73aa657b
2 changed files with 65 additions and 11 deletions
|
@ -110,16 +110,18 @@ quot.lift_on s
|
||||||
(λ l, all (elt_of l) p)
|
(λ l, all (elt_of l) p)
|
||||||
(λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !and.left_comm) p true)
|
(λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !and.left_comm) p true)
|
||||||
|
|
||||||
-- notation for bounded quantifiers
|
|
||||||
notation `forallb` binders `∈` a `,` r:(scoped:1 P, P) := all a r
|
|
||||||
notation `∀₀` binders `∈` a `,` r:(scoped:1 P, P) := all a r
|
|
||||||
|
|
||||||
theorem all_empty (p : A → Prop) : all ∅ p = true :=
|
theorem all_empty (p : A → Prop) : all ∅ p = true :=
|
||||||
rfl
|
rfl
|
||||||
|
|
||||||
theorem of_mem_of_all {p : A → Prop} {a : A} {s : finset A} : a ∈ s → all s p → p a :=
|
theorem of_mem_of_all {p : A → Prop} {a : A} {s : finset A} : a ∈ s → all s p → p a :=
|
||||||
quot.induction_on s (λ l i h, list.of_mem_of_all i h)
|
quot.induction_on s (λ l i h, list.of_mem_of_all i h)
|
||||||
|
|
||||||
|
theorem forall_of_all {p : A → Prop} {s : finset A} (H : all s p) : ∀{a}, a ∈ s → p a :=
|
||||||
|
λ a H', of_mem_of_all H' H
|
||||||
|
|
||||||
|
definition decidable_all (p : A → Prop) [h : decidable_pred p] (s : finset A) : decidable (all s p) :=
|
||||||
|
quot.rec_on_subsingleton s (λ l, list.decidable_all p (elt_of l))
|
||||||
|
|
||||||
theorem all_implies {p q : A → Prop} {s : finset A} : all s p → (∀ x, p x → q x) → all s q :=
|
theorem all_implies {p q : A → Prop} {s : finset A} : all s p → (∀ x, p x → q x) → all s q :=
|
||||||
quot.induction_on s (λ l h₁ h₂, list.all_implies h₁ h₂)
|
quot.induction_on s (λ l h₁ h₂, list.all_implies h₁ h₂)
|
||||||
|
|
||||||
|
@ -148,6 +150,40 @@ theorem all_inter_of_all_right {p : A → Prop} {s₁ : finset A} (s₂ : finset
|
||||||
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_right _ h)
|
quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_right _ h)
|
||||||
end all
|
end all
|
||||||
|
|
||||||
|
/- any -/
|
||||||
|
section any
|
||||||
|
variables {A : Type}
|
||||||
|
definition any (s : finset A) (p : A → Prop) : Prop :=
|
||||||
|
quot.lift_on s
|
||||||
|
(λ l, any (elt_of l) p)
|
||||||
|
(λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !or.left_comm) p false)
|
||||||
|
|
||||||
|
theorem any_empty (p : A → Prop) : any ∅ p = false := rfl
|
||||||
|
|
||||||
|
theorem exists_of_any {p : A → Prop} {s : finset A} : any s p → ∃a, a ∈ s ∧ p a :=
|
||||||
|
quot.induction_on s (λ l H, list.exists_of_any H)
|
||||||
|
|
||||||
|
theorem any_of_mem {p : A → Prop} {s : finset A} {a : A} : a ∈ s → p a → any s p :=
|
||||||
|
quot.induction_on s (λ l H1 H2, list.any_of_mem H1 H2)
|
||||||
|
|
||||||
|
theorem any_of_insert [h : decidable_eq A] {p : A → Prop} (s : finset A) {a : A} (H : p a) :
|
||||||
|
any (insert a s) p :=
|
||||||
|
any_of_mem (mem_insert a s) H
|
||||||
|
|
||||||
|
theorem any_of_insert_right [h : decidable_eq A] {p : A → Prop} {s : finset A} (a : A) (H : any s p) :
|
||||||
|
any (insert a s) p :=
|
||||||
|
obtain b (H' : b ∈ s ∧ p b), from exists_of_any H,
|
||||||
|
any_of_mem (mem_insert_of_mem a (and.left H')) (and.right H')
|
||||||
|
|
||||||
|
theorem any_of_exists {p : A → Prop} {s : finset A} (H : ∃a, a ∈ s ∧ p a) : any s p :=
|
||||||
|
obtain a H', from H,
|
||||||
|
any_of_mem (and.left H') (and.right H')
|
||||||
|
|
||||||
|
definition decidable_any (p : A → Prop) [h : decidable_pred p] (s : finset A) : decidable (any s p) :=
|
||||||
|
quot.rec_on_subsingleton s (λ l, list.decidable_any p (elt_of l))
|
||||||
|
|
||||||
|
end any
|
||||||
|
|
||||||
section product
|
section product
|
||||||
variables {A B : Type}
|
variables {A B : Type}
|
||||||
definition product (s₁ : finset A) (s₂ : finset B) : finset (A × B) :=
|
definition product (s₁ : finset A) (s₂ : finset B) : finset (A × B) :=
|
||||||
|
|
|
@ -168,15 +168,20 @@ foldr (λ a r, p a ∧ r) true l
|
||||||
definition any (l : list A) (p : A → Prop) : Prop :=
|
definition any (l : list A) (p : A → Prop) : Prop :=
|
||||||
foldr (λ a r, p a ∨ r) false l
|
foldr (λ a r, p a ∨ r) false l
|
||||||
|
|
||||||
theorem all_nil (p : A → Prop) : all [] p = true
|
theorem all_nil_eq (p : A → Prop) : all [] p = true
|
||||||
|
|
||||||
theorem all_cons (p : A → Prop) (a : A) (l : list A) : all (a::l) p = (p a ∧ all l p)
|
theorem all_nil (p : A → Prop) : all [] p := trivial
|
||||||
|
|
||||||
|
theorem all_cons_eq (p : A → Prop) (a : A) (l : list A) : all (a::l) p = (p a ∧ all l p)
|
||||||
|
|
||||||
|
theorem all_cons {p : A → Prop} {a : A} {l : list A} (H1 : p a) (H2 : all l p) : all (a::l) p :=
|
||||||
|
and.intro H1 H2
|
||||||
|
|
||||||
theorem all_of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → all l p :=
|
theorem all_of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → all l p :=
|
||||||
assume h, by rewrite [all_cons at h]; exact (and.elim_right h)
|
assume h, by rewrite [all_cons_eq at h]; exact (and.elim_right h)
|
||||||
|
|
||||||
theorem of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → p a :=
|
theorem of_all_cons {p : A → Prop} {a : A} {l : list A} : all (a::l) p → p a :=
|
||||||
assume h, by rewrite [all_cons at h]; exact (and.elim_left h)
|
assume h, by rewrite [all_cons_eq at h]; exact (and.elim_left h)
|
||||||
|
|
||||||
theorem all_cons_of_all {p : A → Prop} {a : A} {l : list A} : p a → all l p → all (a::l) p :=
|
theorem all_cons_of_all {p : A → Prop} {a : A} {l : list A} : p a → all l p → all (a::l) p :=
|
||||||
assume pa alllp, and.intro pa alllp
|
assume pa alllp, and.intro pa alllp
|
||||||
|
@ -193,16 +198,21 @@ theorem of_mem_of_all {p : A → Prop} {a : A} : ∀ {l}, a ∈ l → all l p
|
||||||
| (b::l) h₁ h₂ :=
|
| (b::l) h₁ h₂ :=
|
||||||
or.elim (eq_or_mem_of_mem_cons h₁)
|
or.elim (eq_or_mem_of_mem_cons h₁)
|
||||||
(λ aeqb : a = b,
|
(λ aeqb : a = b,
|
||||||
by rewrite [all_cons at h₂, -aeqb at h₂]; exact (and.elim_left h₂))
|
by rewrite [all_cons_eq at h₂, -aeqb at h₂]; exact (and.elim_left h₂))
|
||||||
(λ ainl : a ∈ l,
|
(λ ainl : a ∈ l,
|
||||||
have allp : all l p, by rewrite [all_cons at h₂]; exact (and.elim_right h₂),
|
have allp : all l p, by rewrite [all_cons_eq at h₂]; exact (and.elim_right h₂),
|
||||||
of_mem_of_all ainl allp)
|
of_mem_of_all ainl allp)
|
||||||
|
|
||||||
|
theorem all_of_forall {p : A → Prop} : ∀ {l}, (∀a, a ∈ l → p a) → all l p
|
||||||
|
| [] H := !all_nil
|
||||||
|
| (a::l) H := all_cons (H a !mem_cons)
|
||||||
|
(all_of_forall (λ a' H', H a' (mem_cons_of_mem _ H')))
|
||||||
|
|
||||||
theorem any_nil (p : A → Prop) : any [] p = false
|
theorem any_nil (p : A → Prop) : any [] p = false
|
||||||
|
|
||||||
theorem any_cons (p : A → Prop) (a : A) (l : list A) : any (a::l) p = (p a ∨ any l p)
|
theorem any_cons (p : A → Prop) (a : A) (l : list A) : any (a::l) p = (p a ∨ any l p)
|
||||||
|
|
||||||
theorem any_of_mem (p : A → Prop) {a : A} : ∀ {l}, a ∈ l → p a → any l p
|
theorem any_of_mem {p : A → Prop} {a : A} : ∀ {l}, a ∈ l → p a → any l p
|
||||||
| [] i h := absurd i !not_mem_nil
|
| [] i h := absurd i !not_mem_nil
|
||||||
| (b::l) i h :=
|
| (b::l) i h :=
|
||||||
or.elim (eq_or_mem_of_mem_cons i)
|
or.elim (eq_or_mem_of_mem_cons i)
|
||||||
|
@ -211,6 +221,14 @@ theorem any_of_mem (p : A → Prop) {a : A} : ∀ {l}, a ∈ l → p a → any l
|
||||||
have anyl : any l p, from any_of_mem ainl h,
|
have anyl : any l p, from any_of_mem ainl h,
|
||||||
or.inr anyl)
|
or.inr anyl)
|
||||||
|
|
||||||
|
theorem exists_of_any {p : A → Prop} : ∀{l : list A}, any l p → ∃a, a ∈ l ∧ p a
|
||||||
|
| [] H := false.elim H
|
||||||
|
| (b::l) H := or.elim H
|
||||||
|
(assume H1 : p b, exists.intro b (and.intro !mem_cons H1))
|
||||||
|
(assume H1 : any l p,
|
||||||
|
obtain a (H2 : a ∈ l ∧ p a), from exists_of_any H1,
|
||||||
|
exists.intro a (and.intro (mem_cons_of_mem b (and.left H2)) (and.right H2)))
|
||||||
|
|
||||||
definition decidable_all (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (all l p)
|
definition decidable_all (p : A → Prop) [H : decidable_pred p] : ∀ l, decidable (all l p)
|
||||||
| [] := decidable_true
|
| [] := decidable_true
|
||||||
| (a :: l) :=
|
| (a :: l) :=
|
||||||
|
|
Loading…
Reference in a new issue