blob_id
stringlengths
40
40
directory_id
stringlengths
40
40
path
stringlengths
7
139
content_id
stringlengths
40
40
detected_licenses
listlengths
0
16
license_type
stringclasses
2 values
repo_name
stringlengths
7
55
snapshot_id
stringlengths
40
40
revision_id
stringlengths
40
40
branch_name
stringclasses
6 values
visit_date
int64
1,471B
1,694B
revision_date
int64
1,378B
1,694B
committer_date
int64
1,378B
1,694B
github_id
float64
1.33M
604M
star_events_count
int64
0
43.5k
fork_events_count
int64
0
1.5k
gha_license_id
stringclasses
6 values
gha_event_created_at
int64
1,402B
1,695B
gha_created_at
int64
1,359B
1,637B
gha_language
stringclasses
19 values
src_encoding
stringclasses
2 values
language
stringclasses
1 value
is_vendor
bool
1 class
is_generated
bool
1 class
length_bytes
int64
3
6.4M
extension
stringclasses
4 values
content
stringlengths
3
6.12M
9d3c51046603d0298c533e9370a820389081518f
f57749ca63d6416f807b770f67559503fdb21001
/library/data/finset/comb.lean
bab4ed30c7d8c1592a8ba407c76c7314a0303e97
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
17,546
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura, Jeremy Avigad Combinators for finite sets. -/ import data.finset.basic logic.identities open list quot subtype decidable perm function namespace finset /- image (corresponds to map on list) -/ section image variables {A B : Type} variable [h : decidable_eq B] include h definition image (f : A → B) (s : finset A) : finset B := quot.lift_on s (λ l, to_finset (list.map f (elt_of l))) (λ l₁ l₂ p, quot.sound (perm_erase_dup_of_perm (perm_map _ p))) notation f `'[`:max a `]` := image f a theorem image_empty (f : A → B) : image f ∅ = ∅ := rfl theorem mem_image_of_mem (f : A → B) {s : finset A} {a : A} : a ∈ s → f a ∈ image f s := quot.induction_on s (take l, assume H : a ∈ elt_of l, mem_to_finset (mem_map f H)) theorem mem_image {f : A → B} {s : finset A} {a : A} {b : B} (H1 : a ∈ s) (H2 : f a = b) : b ∈ image f s := eq.subst H2 (mem_image_of_mem f H1) theorem exists_of_mem_image {f : A → B} {s : finset A} {b : B} : b ∈ image f s → ∃a, a ∈ s ∧ f a = b := quot.induction_on s (take l, assume H : b ∈ erase_dup (list.map f (elt_of l)), exists_of_mem_map (mem_of_mem_erase_dup H)) theorem mem_image_iff (f : A → B) {s : finset A} {y : B} : y ∈ image f s ↔ ∃x, x ∈ s ∧ f x = y := iff.intro exists_of_mem_image (assume H, obtain x (H₁ : x ∈ s) (H₂ : f x = y), from H, mem_image H₁ H₂) theorem mem_image_eq (f : A → B) {s : finset A} {y : B} : y ∈ image f s = ∃x, x ∈ s ∧ f x = y := propext (mem_image_iff f) theorem mem_image_of_mem_image_of_subset {f : A → B} {s t : finset A} {y : B} (H1 : y ∈ image f s) (H2 : s ⊆ t) : y ∈ image f t := obtain x (H3: x ∈ s) (H4 : f x = y), from exists_of_mem_image H1, have H5 : x ∈ t, from mem_of_subset_of_mem H2 H3, show y ∈ image f t, from mem_image H5 H4 theorem image_insert [h' : decidable_eq A] (f : A → B) (s : finset A) (a : A) : image f (insert a s) = insert (f a) (image f s) := ext (take y, iff.intro (assume H : y ∈ image f (insert a s), obtain x (H1l : x ∈ insert a s) (H1r :f x = y), from exists_of_mem_image H, have x = a ∨ x ∈ s, from eq_or_mem_of_mem_insert H1l, or.elim this (suppose x = a, have f a = y, from eq.subst this H1r, show y ∈ insert (f a) (image f s), from eq.subst this !mem_insert) (suppose x ∈ s, have f x ∈ image f s, from mem_image_of_mem f this, show y ∈ insert (f a) (image f s), from eq.subst H1r (mem_insert_of_mem _ this))) (suppose y ∈ insert (f a) (image f s), have y = f a ∨ y ∈ image f s, from eq_or_mem_of_mem_insert this, or.elim this (suppose y = f a, have f a ∈ image f (insert a s), from mem_image_of_mem f !mem_insert, show y ∈ image f (insert a s), from eq.subst (eq.symm `y = f a`) this) (suppose y ∈ image f s, show y ∈ image f (insert a s), from mem_image_of_mem_image_of_subset this !subset_insert))) lemma image_compose {C : Type} [deceqC : decidable_eq C] {f : B → C} {g : A → B} {s : finset A} : image (f∘g) s = image f (image g s) := ext (take z, iff.intro (suppose z ∈ image (f∘g) s, obtain x (Hx : x ∈ s) (Hgfx : f (g x) = z), from exists_of_mem_image this, by rewrite -Hgfx; apply mem_image_of_mem _ (mem_image_of_mem _ Hx)) (suppose z ∈ image f (image g s), obtain y (Hy : y ∈ image g s) (Hfy : f y = z), from exists_of_mem_image this, obtain x (Hx : x ∈ s) (Hgx : g x = y), from exists_of_mem_image Hy, mem_image Hx (by esimp; rewrite [Hgx, Hfy]))) lemma image_subset {a b : finset A} (f : A → B) (H : a ⊆ b) : f '[a] ⊆ f '[b] := subset_of_forall (take y, assume Hy : y ∈ f '[a], obtain x (Hx₁ : x ∈ a) (Hx₂ : f x = y), from exists_of_mem_image Hy, mem_image (mem_of_subset_of_mem H Hx₁) Hx₂) theorem image_union [h' : decidable_eq A] (f : A → B) (s t : finset A) : image f (s ∪ t) = image f s ∪ image f t := ext (take y, iff.intro (assume H : y ∈ image f (s ∪ t), obtain x [(xst : x ∈ s ∪ t) (fxy : f x = y)], from exists_of_mem_image H, or.elim (mem_or_mem_of_mem_union xst) (assume xs, mem_union_l (mem_image xs fxy)) (assume xt, mem_union_r (mem_image xt fxy))) (assume H : y ∈ image f s ∪ image f t, or.elim (mem_or_mem_of_mem_union H) (assume yifs : y ∈ image f s, obtain x [(xs : x ∈ s) (fxy : f x = y)], from exists_of_mem_image yifs, mem_image (mem_union_l xs) fxy) (assume yift : y ∈ image f t, obtain x [(xt : x ∈ t) (fxy : f x = y)], from exists_of_mem_image yift, mem_image (mem_union_r xt) fxy))) end image /- filter and set-builder notation -/ section filter variables {A : Type} [deceq : decidable_eq A] include deceq variables (p : A → Prop) [decp : decidable_pred p] (s : finset A) {x : A} include decp definition filter : finset A := quot.lift_on s (λl, to_finset_of_nodup (list.filter p (subtype.elt_of l)) (list.nodup_filter p (subtype.has_property l))) (λ l₁ l₂ u, quot.sound (perm.perm_filter u)) notation `{` binder ∈ s `|` r:(scoped:1 p, filter p s) `}` := r theorem filter_empty : filter p ∅ = ∅ := rfl variables {p s} theorem of_mem_filter : x ∈ filter p s → p x := quot.induction_on s (take l, list.of_mem_filter) theorem mem_of_mem_filter : x ∈ filter p s → x ∈ s := quot.induction_on s (take l, list.mem_of_mem_filter) theorem mem_filter_of_mem {x : A} : x ∈ s → p x → x ∈ filter p s := quot.induction_on s (take l, list.mem_filter_of_mem) variables (p s) theorem mem_filter_iff : x ∈ filter p s ↔ x ∈ s ∧ p x := iff.intro (assume H, and.intro (mem_of_mem_filter H) (of_mem_filter H)) (assume H, mem_filter_of_mem (and.left H) (and.right H)) theorem mem_filter_eq : x ∈ filter p s = (x ∈ s ∧ p x) := propext !mem_filter_iff variable t : finset A theorem mem_filter_union_iff : x ∈ filter p (s ∪ t) ↔ x ∈ filter p s ∨ x ∈ filter p t := by rewrite [*mem_filter_iff, mem_union_iff, and.right_distrib] end filter theorem mem_singleton_eq' {A : Type} [deceq : decidable_eq A] (x a : A) : x ∈ '{a} = (x = a) := by rewrite [mem_insert_eq, mem_empty_eq, or_false] /- set difference -/ section diff variables {A : Type} [deceq : decidable_eq A] include deceq definition diff (s t : finset A) : finset A := {x ∈ s | x ∉ t} infix `\`:70 := diff theorem mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∈ s := mem_of_mem_filter H theorem not_mem_of_mem_diff {s t : finset A} {x : A} (H : x ∈ s \ t) : x ∉ t := of_mem_filter H theorem mem_diff {s t : finset A} {x : A} (H1 : x ∈ s) (H2 : x ∉ t) : x ∈ s \ t := mem_filter_of_mem H1 H2 theorem mem_diff_iff (s t : finset A) (x : A) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.intro (assume H, and.intro (mem_of_mem_diff H) (not_mem_of_mem_diff H)) (assume H, mem_diff (and.left H) (and.right H)) theorem mem_diff_eq (s t : finset A) (x : A) : x ∈ s \ t = (x ∈ s ∧ x ∉ t) := propext !mem_diff_iff theorem union_diff_cancel {s t : finset A} (H : s ⊆ t) : s ∪ (t \ s) = t := ext (take x, iff.intro (suppose x ∈ s ∪ (t \ s), or.elim (mem_or_mem_of_mem_union this) (suppose x ∈ s, mem_of_subset_of_mem H this) (suppose x ∈ t \ s, mem_of_mem_diff this)) (suppose x ∈ t, decidable.by_cases (suppose x ∈ s, mem_union_left _ this) (suppose x ∉ s, mem_union_right _ (mem_diff `x ∈ t` this)))) theorem diff_union_cancel {s t : finset A} (H : s ⊆ t) : (t \ s) ∪ s = t := eq.subst !union.comm (!union_diff_cancel H) end diff /- all -/ section all variables {A : Type} definition all (s : finset A) (p : A → Prop) : Prop := quot.lift_on s (λ l, all (elt_of l) p) (λ l₁ l₂ p, foldr_eq_of_perm (λ a₁ a₂ q, propext !and.left_comm) p true) theorem all_empty (p : A → Prop) : all ∅ p = true := rfl 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) 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 theorem all_of_forall {p : A → Prop} {s : finset A} : (∀a, a ∈ s → p a) → all s p := quot.induction_on s (λ l H, list.all_of_forall H) theorem all_iff_forall (p : A → Prop) (s : finset A) : all s p ↔ (∀a, a ∈ s → p a) := iff.intro forall_of_all all_of_forall definition decidable_all [instance] (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 := quot.induction_on s (λ l h₁ h₂, list.all_implies h₁ h₂) variable [h : decidable_eq A] include h theorem all_union {p : A → Prop} {s₁ s₂ : finset A} : all s₁ p → all s₂ p → all (s₁ ∪ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a₁ a₂, all_union a₁ a₂) theorem all_of_all_union_left {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₁ p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_left a) theorem all_of_all_union_right {p : A → Prop} {s₁ s₂ : finset A} : all (s₁ ∪ s₂) p → all s₂ p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ a, list.all_of_all_union_right a) theorem all_insert_of_all {p : A → Prop} {a : A} {s : finset A} : p a → all s p → all (insert a s) p := quot.induction_on s (λ l h₁ h₂, list.all_insert_of_all h₁ h₂) theorem all_erase_of_all {p : A → Prop} (a : A) {s : finset A}: all s p → all (erase a s) p := quot.induction_on s (λ l h, list.all_erase_of_all a h) theorem all_inter_of_all_left {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₁ p → all (s₁ ∩ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_left _ h) theorem all_inter_of_all_right {p : A → Prop} {s₁ : finset A} (s₂ : finset A) : all s₂ p → all (s₁ ∩ s₂) p := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ h, list.all_inter_of_all_right _ h) theorem subset_iff_all (s t : finset A) : s ⊆ t ↔ all s (λ x, x ∈ t) := iff.intro (suppose s ⊆ t, all_of_forall (take x, suppose x ∈ s, mem_of_subset_of_mem `s ⊆ t` `x ∈ s`)) (suppose all s (λ x, x ∈ t), subset_of_forall (take x, suppose x ∈ s, of_mem_of_all `x ∈ s` `all s (λ x, x ∈ t)`)) definition decidable_subset [instance] (s t : finset A) : decidable (s ⊆ t) := decidable_of_decidable_of_iff _ (iff.symm !subset_iff_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_exists {p : A → Prop} {s : finset A} (H : ∃a, a ∈ s ∧ p a) : any s p := obtain a H₁ H₂, from H, any_of_mem H₁ H₂ theorem any_iff_exists (p : A → Prop) (s : finset A) : any s p ↔ (∃a, a ∈ s ∧ p a) := iff.intro exists_of_any any_of_exists 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) (H₂ : p b), from exists_of_any H, any_of_mem (mem_insert_of_mem a H₁) H₂ definition decidable_any [instance] (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 variables {A B : Type} definition product (s₁ : finset A) (s₂ : finset B) : finset (A × B) := quot.lift_on₂ s₁ s₂ (λ l₁ l₂, to_finset_of_nodup (product (elt_of l₁) (elt_of l₂)) (nodup_product (has_property l₁) (has_property l₂))) (λ v₁ v₂ w₁ w₂ p₁ p₂, quot.sound (perm_product p₁ p₂)) infix * := product theorem empty_product (s : finset B) : @empty A * s = ∅ := quot.induction_on s (λ l, rfl) theorem mem_product {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : a ∈ s₁ → b ∈ s₂ → (a, b) ∈ s₁ * s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i₁ i₂, list.mem_product i₁ i₂) theorem mem_of_mem_product_left {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : (a, b) ∈ s₁ * s₂ → a ∈ s₁ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_left i) theorem mem_of_mem_product_right {a : A} {b : B} {s₁ : finset A} {s₂ : finset B} : (a, b) ∈ s₁ * s₂ → b ∈ s₂ := quot.induction_on₂ s₁ s₂ (λ l₁ l₂ i, list.mem_of_mem_product_right i) theorem product_empty (s : finset A) : s * @empty B = ∅ := ext (λ p, match p with | (a, b) := iff.intro (λ i, absurd (mem_of_mem_product_right i) !not_mem_empty) (λ i, absurd i !not_mem_empty) end) end product /- powerset -/ section powerset variables {A : Type} [deceqA : decidable_eq A] include deceqA section list_powerset open list definition list_powerset : list A → finset (finset A) | [] := '{∅} | (a :: l) := list_powerset l ∪ image (insert a) (list_powerset l) end list_powerset private theorem image_insert_comm (a b : A) (s : finset (finset A)) : image (insert a) (image (insert b) s) = image (insert b) (image (insert a) s) := have aux' : ∀ a b : A, ∀ x : finset A, x ∈ image (insert a) (image (insert b) s) → x ∈ image (insert b) (image (insert a) s), from begin intros [a, b, x, H], cases (exists_of_mem_image H) with [y, Hy], cases Hy with [Hy1, Hy2], cases (exists_of_mem_image Hy1) with [z, Hz], cases Hz with [Hz1, Hz2], substvars, rewrite insert.comm, repeat (apply mem_image_of_mem), assumption end, ext (take x, iff.intro (aux' a b x) (aux' b a x)) theorem list_powerset_eq_list_powerset_of_perm {l₁ l₂ : list A} (p : l₁ ~ l₂) : list_powerset l₁ = list_powerset l₂ := perm.induction_on p rfl (λ x l₁ l₂ p ih, by rewrite [↑list_powerset, ih]) (λ x y l, by rewrite [↑list_powerset, ↑list_powerset, *image_union, image_insert_comm, *union.assoc, union.left_comm (finset.image (finset.insert x) _)]) (λ l₁ l₂ l₃ p₁ p₂ r₁ r₂, eq.trans r₁ r₂) definition powerset (s : finset A) : finset (finset A) := quot.lift_on s (λ l, list_powerset (elt_of l)) (λ l₁ l₂ p, list_powerset_eq_list_powerset_of_perm p) theorem powerset_empty : powerset (∅ : finset A) = '{∅} := rfl theorem powerset_insert {a : A} {s : finset A} : a ∉ s → powerset (insert a s) = powerset s ∪ image (insert a) (powerset s) := quot.induction_on s (λ l, assume H : a ∉ quot.mk l, calc powerset (insert a (quot.mk l)) = list_powerset (list.insert a (elt_of l)) : rfl ... = list_powerset (#list a :: elt_of l) : by rewrite [list.insert_eq_of_not_mem H] ... = powerset (quot.mk l) ∪ image (insert a) (powerset (quot.mk l)) : rfl) theorem mem_powerset_iff_subset (s : finset A) : ∀ x, x ∈ powerset s ↔ x ⊆ s := begin induction s with a s nains ih, intro x, rewrite powerset_empty, show x ∈ '{∅} ↔ x ⊆ ∅, by rewrite [mem_singleton_eq', subset_empty_iff], intro x, rewrite [powerset_insert nains, mem_union_iff, ih, mem_image_iff], exact (iff.intro (assume H, or.elim H (suppose x ⊆ s, subset.trans this !subset_insert) (suppose ∃ y, y ∈ powerset s ∧ insert a y = x, obtain y [yps iay], from this, show x ⊆ insert a s, begin rewrite [-iay], apply insert_subset_insert, rewrite -ih, apply yps end)) (assume H : x ⊆ insert a s, assert H' : erase a x ⊆ s, from erase_subset_of_subset_insert H, decidable.by_cases (suppose a ∈ x, or.inr (exists.intro (erase a x) (and.intro (show erase a x ∈ powerset s, by rewrite ih; apply H') (show insert a (erase a x) = x, from insert_erase this)))) (suppose a ∉ x, or.inl (show x ⊆ s, by rewrite [(erase_eq_of_not_mem this) at H']; apply H')))) end theorem subset_of_mem_powerset {s t : finset A} (H : s ∈ powerset t) : s ⊆ t := by rewrite mem_powerset_iff_subset at H; exact H theorem mem_powerset_of_subset {s t : finset A} (H : s ⊆ t) : s ∈ powerset t := by rewrite -mem_powerset_iff_subset at H; exact H theorem empty_mem_powerset (s : finset A) : ∅ ∈ powerset s := by rewrite mem_powerset_iff_subset; apply empty_subset end powerset end finset
bc7fb923dffc78e01bf0cee0c90c8f44cdabed4e
f20db13587f4dd28a4b1fbd31953afd491691fa0
/library/init/data/set.lean
56efe48d47955f8499fa013b55eac287067669c9
[ "Apache-2.0" ]
permissive
AHartNtkn/lean
9a971edfc6857c63edcbf96bea6841b9a84cf916
0d83a74b26541421fc1aa33044c35b03759710ed
refs/heads/master
1,620,592,591,236
1,516,749,881,000
1,516,749,881,000
118,697,288
1
0
null
1,516,759,470,000
1,516,759,470,000
null
UTF-8
Lean
false
false
2,281
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.interactive universes u v def set (α : Type u) := α → Prop def set_of {α : Type u} (p : α → Prop) : set α := p namespace set variables {α : Type u} {β : Type v} protected def mem (a : α) (s : set α) := s a instance : has_mem α (set α) := ⟨set.mem⟩ protected def subset (s₁ s₂ : set α) := ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂ instance : has_subset (set α) := ⟨set.subset⟩ protected def sep (p : α → Prop) (s : set α) : set α := {a | a ∈ s ∧ p a} instance : has_sep α (set α) := ⟨set.sep⟩ instance : has_emptyc (set α) := ⟨λ a, false⟩ def univ : set α := λ a, true protected def insert (a : α) (s : set α) : set α := {b | b = a ∨ b ∈ s} instance : has_insert α (set α) := ⟨set.insert⟩ protected def union (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∨ a ∈ s₂} instance : has_union (set α) := ⟨set.union⟩ protected def inter (s₁ s₂ : set α) : set α := {a | a ∈ s₁ ∧ a ∈ s₂} instance : has_inter (set α) := ⟨set.inter⟩ def compl (s : set α) : set α := {a | a ∉ s} instance : has_neg (set α) := ⟨compl⟩ protected def diff (s t : set α) : set α := {a ∈ s | a ∉ t} instance : has_sdiff (set α) := ⟨set.diff⟩ def powerset (s : set α) : set (set α) := {t | t ⊆ s} prefix `𝒫`:100 := powerset @[reducible] def sUnion (s : set (set α)) : set α := {t | ∃ a ∈ s, t ∈ a} prefix `⋃₀`:110 := sUnion def image (f : α → β) (s : set α) : set β := {b | ∃ a, a ∈ s ∧ f a = b} instance : functor set := {map := @set.image, id_map := begin intros _ s, funext b, dsimp [image, set_of], exact propext ⟨λ ⟨b', ⟨_, _⟩⟩, ‹b' = b› ▸ ‹s b'›, λ _, ⟨b, ⟨‹s b›, rfl⟩⟩⟩, end, map_comp := begin intros, funext c, dsimp [image, set_of, function.comp], exact propext ⟨λ ⟨a, ⟨h₁, h₂⟩⟩, ⟨g a, ⟨⟨a, ⟨h₁, rfl⟩⟩, h₂⟩⟩, λ ⟨b, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩, ⟨a, ⟨h₁, h₂.symm ▸ h₃⟩⟩⟩ end} end set
60cba27f058f3d34f390d4a6529e61402c3158ec
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/data/set/accumulate_auto.lean
efd709c2ede06120a2f6cb08235ec55f984ad589
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,168
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.set.lattice import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Accumulate The function `accumulate` takes a set `s` and returns `⋃ y ≤ x, s y`. -/ namespace set /-- `accumulate s` is the union of `s y` for `y ≤ x`. -/ def accumulate {α : Type u_1} {β : Type u_2} [HasLessEq α] (s : α → set β) (x : α) : set β := Union fun (y : α) => Union fun (H : y ≤ x) => s y theorem accumulate_def {α : Type u_1} {β : Type u_2} {s : α → set β} [HasLessEq α] {x : α} : accumulate s x = Union fun (y : α) => Union fun (H : y ≤ x) => s y := rfl @[simp] theorem mem_accumulate {α : Type u_1} {β : Type u_2} {s : α → set β} [HasLessEq α] {x : α} {z : β} : z ∈ accumulate s x ↔ ∃ (y : α), ∃ (H : y ≤ x), z ∈ s y := mem_bUnion_iff theorem subset_accumulate {α : Type u_1} {β : Type u_2} {s : α → set β} [preorder α] {x : α} : s x ⊆ accumulate s x := fun (z : β) => mem_bUnion le_rfl theorem monotone_accumulate {α : Type u_1} {β : Type u_2} {s : α → set β} [preorder α] : monotone (accumulate s) := fun (x y : α) (hxy : x ≤ y) => bUnion_subset_bUnion_left fun (z : α) (hz : z ∈ fun (y : α) => preorder.le y x) => le_trans hz hxy theorem bUnion_accumulate {α : Type u_1} {β : Type u_2} {s : α → set β} [preorder α] (x : α) : (Union fun (y : α) => Union fun (H : y ≤ x) => accumulate s y) = Union fun (y : α) => Union fun (H : y ≤ x) => s y := subset.antisymm (bUnion_subset fun (x_1 : α) (hx : x_1 ∈ fun (y : α) => preorder.le y x) => monotone_accumulate hx) (bUnion_subset_bUnion_right fun (x_1 : α) (hx : x_1 ∈ fun (y : α) => preorder.le y x) => subset_accumulate) theorem Union_accumulate {α : Type u_1} {β : Type u_2} {s : α → set β} [preorder α] : (Union fun (x : α) => accumulate s x) = Union fun (x : α) => s x := sorry end Mathlib
2177f8a1a55ecfc5dae6a15ca56e6a6f5c449dd8
7a76361040c55ae1eba5856c1a637593117a6556
/src/exercises/love03_forward_proofs_exercise_solution.lean
b956e938f516c24035538cfff76d4c302f52d7cd
[]
no_license
rgreenblatt/fpv2021
c2cbe7b664b648cef7d240a654d6bdf97a559272
c65d72e48c8fa827d2040ed6ea86c2be62db36fa
refs/heads/main
1,692,245,693,819
1,633,364,621,000
1,633,364,621,000
407,231,487
0
0
null
1,631,808,608,000
1,631,808,608,000
null
UTF-8
Lean
false
false
5,484
lean
import ..lovelib /-! # LoVe Exercise 3: Forward Proofs -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /-! ## Question 1: Connectives and Quantifiers 1.1. Supply structured proofs of the following lemmas. -/ lemma I (a : Prop) : a → a := assume ha : a, show a, from ha lemma K (a b : Prop) : a → b → b := assume ha : a, assume hb : b, show b, from hb lemma C (a b c : Prop) : (a → b → c) → b → a → c := assume hg : a → b → c, assume hb : b, assume ha : a, show c, from hg ha hb lemma proj_1st (a : Prop) : a → a → a := assume ha ha' : a, show a, from ha /-! Please give a different answer than for `proj_1st`. -/ lemma proj_2nd (a : Prop) : a → a → a := assume ha ha' : a, show a, from ha' lemma some_nonsense (a b c : Prop) : (a → b → c) → a → (a → c) → b → c := assume hg : a → b → c, assume ha : a, assume hf : a → c, assume hb : b, have hc : c := hf ha, show c, from hc /-! 1.2. Supply a structured proof of the contraposition rule. -/ lemma contrapositive (a b : Prop) : (a → b) → ¬ b → ¬ a := assume hab : a → b, assume hnb : ¬ b, assume ha : a, have hb : b := hab ha, show false, from hnb hb /-! 1.3. Supply a structured proof of the distributivity of `∀` over `∧`. -/ lemma forall_and {α : Type} (p q : α → Prop) : (∀x, p x ∧ q x) ↔ (∀x, p x) ∧ (∀x, q x) := iff.intro (assume hpq : ∀x, p x ∧ q x, have hp : ∀x, p x := fix x, and.elim_left (hpq x), have hq : ∀x, q x := fix x, and.elim_right (hpq x), show (∀x, p x) ∧ (∀x, q x), from and.intro hp hq) (assume hpq : (∀x, p x) ∧ (∀x, q x), have hp : ∀x, p x := and.elim_left hpq, have hq : ∀x, q x := and.elim_right hpq, assume x : α, show p x ∧ q x, from and.intro (hp x) (hq x)) /-! 1.4 (**optional**). Reuse, if possible, the lemma `forall_and` from question 1.3 to prove the following instance of the lemma. -/ lemma forall_and_inst {α : Type} (r s : α → α → Prop) : (∀x, r x x ∧ s x x) ↔ (∀x, r x x) ∧ (∀x, s x x) := forall_and (λx, r x x) (λx, s x x) /-! 1.5. Supply a structured proof of the following property, which can be used pull a `∀`-quantifier past an `∃`-quantifier. -/ lemma forall_exists_of_exists_forall {α : Type} (p : α → α → Prop) : (∃x, ∀y, p x y) → (∀y, ∃x, p x y) := assume hpxy : ∃x, ∀y, p x y, fix b, show ∃x, p x b, from exists.elim hpxy (fix a, assume hpay : ∀y, p a y, show ∃x, p x b, from exists.intro a (hpay b)) /-! ## Question 2: Chain of Equalities 2.1. Write the following proof using `calc`. (a + b) * (a + b) = a * (a + b) + b * (a + b) = a * a + a * b + b * a + b * b = a * a + a * b + a * b + b * b = a * a + 2 * a * b + b * b Hint: You might need the tactics `simp` and `cc` and the lemmas `mul_add`, `add_mul`, and `two_mul`. -/ lemma binomial_square (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := calc (a + b) * (a + b) = a * (a + b) + b * (a + b) : begin simp [add_mul, mul_add], cc end ... = a * a + a * b + b * a + b * b : begin simp [add_mul, mul_add], cc end ... = a * a + a * b + a * b + b * b : by cc ... = a * a + 2 * a * b + b * b : begin simp [two_mul, mul_add, add_mul], cc end /-! 2.2 (**optional**). Prove the same argument again, this time as a structured proof, with `have` steps corresponding to the `calc` equations. Try to reuse as much of the above proof idea as possible, proceeding mechanically. -/ lemma binomial_square₂ (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := have h1 : (a + b) * (a + b) = a * (a + b) + b * (a + b) := begin simp [add_mul, mul_add], cc end, have h2 : a * (a + b) + b * (a + b) = a * a + a * b + b * a + b * b := begin simp [add_mul, mul_add], cc end, have h3 : a * a + a * b + b * a + b * b = a * a + a * b + a * b + b * b := begin simp, cc end, have h4 : a * a + a * b + a * b + b * b = a * a + 2 * a * b + b * b := begin simp [two_mul, add_mul, mul_add], cc end, show _, from begin rw h1, rw h2, rw h3, rw h4 end /-! 2.3. Prove the same lemma again, this time using tactics. -/ lemma binomial_square₃ (a b : ℕ) : (a + b) * (a + b) = a * a + 2 * a * b + b * b := begin simp [two_mul, add_mul, mul_add], cc end /-! ## Question 3 (**optional**): One-Point Rules 3.1 (**optional**). Prove that the following wrong formulation of the one-point rule for `∀` is inconsistent, using a structured proof. -/ axiom forall.one_point_wrong {α : Type} {t : α} {p : α → Prop} : (∀x : α, x = t ∧ p x) ↔ p t lemma proof_of_false : false := begin have wrong : (∀x, x = 0 ∧ true) ↔ true := @forall.one_point_wrong ℕ 0 (λ_, true), simp at wrong, have one_eq_zero : 1 = 0 := wrong 1, cc end /-! 3.2 (**optional**). Prove that the following wrong formulation of the one-point rule for `∃` is inconsistent, using a tactical or structured proof. -/ axiom exists.one_point_wrong {α : Type} {t : α} {p : α → Prop} : (∃x : α, x = t → p x) ↔ p t lemma proof_of_false₂ : false := begin have wrong : (∃x, x ≠ 0) ↔ false := @exists.one_point_wrong ℕ 0 (λ_, false), simp at wrong, have one_eq_zero : 1 = 0 := wrong 1, cc end end LoVe
fbfda437d0c1c9e6d03b9fe6c549c08efe0afc0c
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/968.lean
ff13eae6eeed8ec2eee085d05fcd3f2ec7a5f291
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
221
lean
variables (A : Type) [inhabited A] definition f (a : A) : A := a check @f nat nat.is_inhabited structure foo := (a : A) check @foo nat nat.is_inhabited inductive bla | mk : A → bla check @bla nat nat.is_inhabited
e0a6c7b60644a8406eddb55eccb9ddeb4ea0ec66
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/deprecated/subfield.lean
05646a2ab68b92029a5f858dfeee2d8f94a4b2fa
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,883
lean
/- Copyright (c) 2018 Andreas Swerdlow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andreas Swerdlow -/ import deprecated.subring variables {F : Type*} [field F] (S : set F) class is_subfield extends is_subring S : Prop := (inv_mem : ∀ {x : F}, x ∈ S → x⁻¹ ∈ S) instance is_subfield.field [is_subfield S] : field S := { inv := λ x, ⟨x⁻¹, is_subfield.inv_mem x.2⟩, exists_pair_ne := ⟨0, 1, λ h, zero_ne_one (subtype.ext_iff_val.1 h)⟩, mul_inv_cancel := λ a ha, subtype.ext_iff_val.2 (mul_inv_cancel (λ h, ha $ subtype.ext_iff_val.2 h)), inv_zero := subtype.ext_iff_val.2 inv_zero, ..show comm_ring S, by apply_instance } instance univ.is_subfield : is_subfield (@set.univ F) := { inv_mem := by intros; trivial } /- note: in the next two declarations, if we let type-class inference figure out the instance `ring_hom.is_subring_preimage` then that instance only applies when particular instances of `is_add_subgroup _` and `is_submonoid _` are chosen (which are not the default ones). If we specify it explicitly, then it doesn't complain. -/ instance preimage.is_subfield {K : Type*} [field K] (f : F →+* K) (s : set K) [is_subfield s] : is_subfield (f ⁻¹' s) := { inv_mem := λ a (ha : f a ∈ s), show f a⁻¹ ∈ s, by { rw [f.map_inv], exact is_subfield.inv_mem ha }, ..f.is_subring_preimage s } instance image.is_subfield {K : Type*} [field K] (f : F →+* K) (s : set F) [is_subfield s] : is_subfield (f '' s) := { inv_mem := λ a ⟨x, xmem, ha⟩, ⟨x⁻¹, is_subfield.inv_mem xmem, ha ▸ f.map_inv _⟩, ..f.is_subring_image s } instance range.is_subfield {K : Type*} [field K] (f : F →+* K) : is_subfield (set.range f) := by { rw ← set.image_univ, apply_instance } namespace field /-- `field.closure s` is the minimal subfield that includes `s`. -/ def closure : set F := { x | ∃ y ∈ ring.closure S, ∃ z ∈ ring.closure S, y / z = x } variables {S} theorem ring_closure_subset : ring.closure S ⊆ closure S := λ x hx, ⟨x, hx, 1, is_submonoid.one_mem, div_one x⟩ instance closure.is_submonoid : is_submonoid (closure S) := { mul_mem := by rintros _ _ ⟨p, hp, q, hq, hq0, rfl⟩ ⟨r, hr, s, hs, hs0, rfl⟩; exact ⟨p * r, is_submonoid.mul_mem hp hr, q * s, is_submonoid.mul_mem hq hs, (div_mul_div _ _ _ _).symm⟩, one_mem := ring_closure_subset $ is_submonoid.one_mem } instance closure.is_subfield : is_subfield (closure S) := have h0 : (0:F) ∈ closure S, from ring_closure_subset $ is_add_submonoid.zero_mem, { add_mem := begin intros a b ha hb, rcases (id ha) with ⟨p, hp, q, hq, rfl⟩, rcases (id hb) with ⟨r, hr, s, hs, rfl⟩, classical, by_cases hq0 : q = 0, by simp [hb, hq0], by_cases hs0 : s = 0, by simp [ha, hs0], exact ⟨p * s + q * r, is_add_submonoid.add_mem (is_submonoid.mul_mem hp hs) (is_submonoid.mul_mem hq hr), q * s, is_submonoid.mul_mem hq hs, (div_add_div p r hq0 hs0).symm⟩ end, zero_mem := h0, neg_mem := begin rintros _ ⟨p, hp, q, hq, rfl⟩, exact ⟨-p, is_add_subgroup.neg_mem hp, q, hq, neg_div q p⟩ end, inv_mem := begin rintros _ ⟨p, hp, q, hq, rfl⟩, classical, by_cases hp0 : p = 0, by simp [hp0, h0], exact ⟨q, hq, p, hp, inv_div.symm⟩ end } theorem mem_closure {a : F} (ha : a ∈ S) : a ∈ closure S := ring_closure_subset $ ring.mem_closure ha theorem subset_closure : S ⊆ closure S := λ _, mem_closure theorem closure_subset {T : set F} [is_subfield T] (H : S ⊆ T) : closure S ⊆ T := by rintros _ ⟨p, hp, q, hq, hq0, rfl⟩; exact is_submonoid.mul_mem (ring.closure_subset H hp) (is_subfield.inv_mem $ ring.closure_subset H hq) theorem closure_subset_iff (s t : set F) [is_subfield t] : closure s ⊆ t ↔ s ⊆ t := ⟨set.subset.trans subset_closure, closure_subset⟩ theorem closure_mono {s t : set F} (H : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans H subset_closure end field lemma is_subfield_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set F) [∀ i, is_subfield (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subfield (⋃i, s i) := { inv_mem := λ x hx, let ⟨i, hi⟩ := set.mem_Union.1 hx in set.mem_Union.2 ⟨i, is_subfield.inv_mem hi⟩, to_is_subring := is_subring_Union_of_directed s directed } instance is_subfield.inter (S₁ S₂ : set F) [is_subfield S₁] [is_subfield S₂] : is_subfield (S₁ ∩ S₂) := { inv_mem := λ x hx, ⟨is_subfield.inv_mem hx.1, is_subfield.inv_mem hx.2⟩ } instance is_subfield.Inter {ι : Sort*} (S : ι → set F) [h : ∀ y : ι, is_subfield (S y)] : is_subfield (set.Inter S) := { inv_mem := λ x hx, set.mem_Inter.2 $ λ y, is_subfield.inv_mem $ set.mem_Inter.1 hx y }
3137cc6db2543800b2cd0aa97d4af1f6731775da
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/tactic/default.lean
8860f72beddad5c285f123fe5995f5794a5562a6
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
817
lean
/- This file imports many useful tactics ("the kitchen sink"). You can use `import tactic` at the beginning of your file to get everything. (Although you may want to strip things down when you're polishing.) Because this file imports some complicated tactics, it has many transitive dependencies (which of course may not use `import tactic`, and must import selectively). As (non-exhaustive) examples, these includes things like: * algebra.group_power * algebra.ordered_ring * data.rat * data.nat.prime * data.list.perm * data.set.lattice * data.equiv.encodable * order.complete_lattice -/ import tactic.basic tactic.monotonicity.interactive tactic.finish tactic.tauto tactic.tidy tactic.abel tactic.ring tactic.linarith tactic.omega tactic.wlog tactic.tfae tactic.apply_fun tactic.localized
d5d4bc5c027526da257e67c74dcbb6356580ccf8
fa02ed5a3c9c0adee3c26887a16855e7841c668b
/src/tactic/choose.lean
5897267887d202375ba660e8212f176e8d27ac85
[ "Apache-2.0" ]
permissive
jjgarzella/mathlib
96a345378c4e0bf26cf604aed84f90329e4896a2
395d8716c3ad03747059d482090e2bb97db612c8
refs/heads/master
1,686,480,124,379
1,625,163,323,000
1,625,163,323,000
281,190,421
2
0
Apache-2.0
1,595,268,170,000
1,595,268,169,000
null
UTF-8
Lean
false
false
7,288
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Floris van Doorn, Mario Carneiro -/ import tactic.core /-! # `choose` tactic Performs Skolemization, that is, given `h : ∀ a:α, ∃ b:β, p a b |- G` produces `f : α → β, hf: ∀ a, p a (f a) |- G`. -/ namespace tactic /-- Given `α : Sort u`, `nonemp : nonempty α`, `p : α → Prop`, a context of local variables `ctxt`, and a pair of an element `val : α` and `spec : p val`, `mk_sometimes u α nonemp p ctx (val, spec)` produces another pair `val', spec'` such that `val'` does not have any free variables from elements of `ctxt` whose types are propositions. This is done by applying `function.sometimes` to abstract over all the propositional arguments. -/ meta def mk_sometimes (u : level) (α nonemp p : expr) : list expr → expr × expr → tactic (expr × expr) | [] (val, spec) := pure (val, spec) | (e :: ctxt) (val, spec) := do (val, spec) ← mk_sometimes ctxt (val, spec), t ← infer_type e, b ← is_prop t, pure $ if b then let val' := expr.bind_lambda val e in (expr.const ``function.sometimes [level.zero, u] t α nonemp val', expr.const ``function.sometimes_spec [u] t α nonemp p val' e spec) else (val, spec) /-- Changes `(h : ∀xs, ∃a:α, p a) ⊢ g` to `(d : ∀xs, a) (s : ∀xs, p (d xs)) ⊢ g` and `(h : ∀xs, p xs ∧ q xs) ⊢ g` to `(d : ∀xs, p xs) (s : ∀xs, q xs) ⊢ g`. `choose1` returns a pair of the second local constant it introduces, and the error result (see below). If `nondep` is true and `α` is inhabited, then it will remove the dependency of `d` on all propositional assumptions in `xs`. For example if `ys` are propositions then `(h : ∀xs ys, ∃a:α, p a) ⊢ g` becomes `(d : ∀xs, a) (s : ∀xs ys, p (d xs)) ⊢ g`. The second value returned by `choose1` is the result of nondep elimination: * `none`: nondep elimination was not attempted or was not applicable * `some none`: nondep elimination was successful * ``some (some `(nonempty α))``: nondep elimination was unsuccessful because we could not find a `nonempty α` instance -/ meta def choose1 (nondep : bool) (h : expr) (data : name) (spec : name) : tactic (expr × option (option expr)) := do t ← infer_type h, (ctxt, t) ← whnf t >>= open_pis, t ← whnf t transparency.all, match t with | `(@Exists %%α %%p) := do α_t ← infer_type α, expr.sort u ← whnf α_t transparency.all, (ne_fail, nonemp) ← if nondep then do let ne := expr.const ``nonempty [u] α, nonemp ← try_core (mk_instance ne <|> retrieve' (do m ← mk_meta_var ne, set_goals [m], ctxt.mmap' (λ e, do b ← is_proof e, monad.unlessb b $ (mk_app ``nonempty.intro [e] >>= note_anon none) $> ()), unfreeze_local_instances >> apply_instance, instantiate_mvars m)), pure (some (option.guard (λ _, nonemp.is_none) ne), nonemp) else pure (none, none), ctxt' ← if nonemp.is_some then ctxt.mfilter (λ e, bnot <$> is_proof e) else pure ctxt, value ← mk_local_def data (α.pis ctxt'), t' ← head_beta (p.app (value.mk_app ctxt')), spec ← mk_local_def spec (t'.pis ctxt), (value_proof, spec_proof) ← nonemp.elim pure (λ nonemp, mk_sometimes u α nonemp p ctxt) (expr.const ``classical.some [u] α p (h.mk_app ctxt), expr.const ``classical.some_spec [u] α p (h.mk_app ctxt)), dependent_pose_core [(value, value_proof.lambdas ctxt'), (spec, spec_proof.lambdas ctxt)], try (tactic.clear h), intro1, e ← intro1, pure (e, ne_fail) | `(%%p ∧ %%q) := do mk_app ``and.elim_left [h.mk_app ctxt] >>= lambdas ctxt >>= note data none, hq ← mk_app ``and.elim_right [h.mk_app ctxt] >>= lambdas ctxt >>= note spec none, try (tactic.clear h), pure (hq, none) | _ := fail "expected a term of the shape `∀xs, ∃a, p xs a` or `∀xs, p xs ∧ q xs`" end /-- Changes `(h : ∀xs, ∃as, p as ∧ q as) ⊢ g` to a list of functions `as`, and a final hypothesis on `p as` and `q as`. If `nondep` is true then the functions will be made to not depend on propositional arguments, when possible. The last argument is an internal recursion variable, indicating whether nondep elimination has been useful so far. The tactic fails if `nondep` is true, and nondep elimination is attempted at least once, and it fails every time it is attempted, in which case it returns an error complaining about the first attempt. -/ meta def choose (nondep : bool) : expr → list name → opt_param (option (option expr)) none → tactic unit | h [] _ := fail "expect list of variables" | h [n] (some (some ne)) := do g ← mk_meta_var ne, set_goals [g], -- make a reasonable error state fail "choose: failed to synthesize nonempty instance" | h [n] _ := do cnt ← revert h, intro n, intron (cnt - 1), return () | h (n::ns) ne_fail₁ := do (v, ne_fail₂) ← get_unused_name >>= choose1 nondep h n, choose v ns $ match ne_fail₁, ne_fail₂ with | none, _ := ne_fail₂ | some none, _ := some none | _, some none := some none | _, _ := ne_fail₁ end namespace interactive setup_tactic_parser /-- `choose a b h h' using hyp` takes an hypothesis `hyp` of the form `∀ (x : X) (y : Y), ∃ (a : A) (b : B), P x y a b ∧ Q x y a b` for some `P Q : X → Y → A → B → Prop` and outputs into context a function `a : X → Y → A`, `b : X → Y → B` and two assumptions: `h : ∀ (x : X) (y : Y), P x y (a x y) (b x y)` and `h' : ∀ (x : X) (y : Y), Q x y (a x y) (b x y)`. It also works with dependent versions. `choose! a b h h' using hyp` does the same, except that it will remove dependency of the functions on propositional arguments if possible. For example if `Y` is a proposition and `A` and `B` are nonempty in the above example then we will instead get `a : X → A`, `b : X → B`, and the assumptions `h : ∀ (x : X) (y : Y), P x y (a x) (b x)` and `h' : ∀ (x : X) (y : Y), Q x y (a x) (b x)`. Examples: ```lean example (h : ∀n m : ℕ, ∃i j, m = n + i ∨ m + j = n) : true := begin choose i j h using h, guard_hyp i : ℕ → ℕ → ℕ, guard_hyp j : ℕ → ℕ → ℕ, guard_hyp h : ∀ (n m : ℕ), m = n + i n m ∨ m + j n m = n, trivial end ``` ```lean example (h : ∀ i : ℕ, i < 7 → ∃ j, i < j ∧ j < i+i) : true := begin choose! f h h' using h, guard_hyp f : ℕ → ℕ, guard_hyp h : ∀ (i : ℕ), i < 7 → i < f i, guard_hyp h' : ∀ (i : ℕ), i < 7 → f i < i + i, trivial, end ``` -/ meta def choose (nondep : parse (tk "!")?) (first : parse ident) (names : parse ident*) (tgt : parse (tk "using" *> texpr)?) : tactic unit := do tgt ← match tgt with | none := get_local `this | some e := tactic.i_to_expr_strict e end, tactic.choose nondep.is_some tgt (first :: names), try (interactive.simp none none tt [simp_arg_type.expr ``(exists_prop)] [] (loc.ns $ some <$> names)), try (tactic.clear tgt) add_tactic_doc { name := "choose", category := doc_category.tactic, decl_names := [`tactic.interactive.choose], tags := ["classical logic"] } end interactive end tactic
3081b4f15d99661b5f73a19e1001daacb735ee82
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/language_ln2_prf.lean
a3c52d6b2f6313e235556853128b7c2238364078
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
3,320
lean
/- Proof system for the locally-nameless representation of first-order logic following the notes at /flypitch-notes Thanks to Andrew who wrote the prototypical examples in language_term_n* ---Jesse 2018-10-16T00:00:50 -/ import .language_term_ln2 section parameter L : Language def arity (α β : Type) : nat → Type -- thanks Seul!! | 0 := β | (n+1) := α → arity n -- inductive closed_preterm :Π n : ℕ, preterm L n → Prop -- | bvar : Π k : ℕ, closed_preterm 0 (preterm.bvar L k) -- | func : Π n : ℕ, Π f : L.functions n, closed_preterm n (preterm.func f) -- | apply : Π n : ℕ, Π t1 : (preterm L (n+1)), Π t2 : (preterm L 0), Π h1 : (closed_preterm (n+1) t1), Π h2 : (closed_preterm 0 t2), closed_preterm n (preterm.apply t1 t2) -- inductive presentence : Π(n : ℕ), preformula L n → Prop -- | true : presentence 0 (preformula.true L) -- | false : presentence 0 (preformula.false L) -- | equal : Π t1 t2 : term L, Π h1 : closed_preterm 0 t1, Π h2 : closed_preterm 0 t2, presentence 0 (preformula.equal t1 t2) -- | rel : Π {n : ℕ}, Π R : L.relations n, presentence n (preformula.rel R) -- | apprel : Π {n : ℕ}, Π ψ : preformula L (n+1), Π t : term L, Π h1 : presentence (n+1) ψ, Π h2 : closed_preterm 0 t, presentence n (preformula.apprel ψ t) -- | imp : Π ϕ : preformula L 0, Π ψ : preformula L 0, Π h1 : presentence 0 ϕ, Π h2 : presentence 0 ψ, presentence 0 (preformula.imp ϕ ψ) -- | all : Π ψ : preformula L 0, Π h : presentence 0 ψ, presentence 0 (preformula.all ψ) -- this is wrong -- -- def sentence : formula L → Prop := presentence 0 /-- A sentence is a well-formed L-formula with no free variables --/ def is_sentence : formula L → Prop := λ ψ, (free_vars_formula L ψ = []) ∧ well_formed_formula L ψ def sentences := {ψ : formula L // is_sentence ψ} lemma true_is_sentence : is_sentence (preformula.true L) := begin split, {split,}, {trivial} end lemma false_is_sentence : is_sentence (preformula.false L) := begin split, {split}, {split} end lemma imp_is_sentence (ϕ : sentences) (ψ : sentences) : is_sentence (preformula.imp (ϕ.val) (ψ.val)) := begin split, {cases ϕ, cases ψ, simp[free_vars_formula], cases ϕ_property, cases ψ_property, simp[free_vars_preformula], have h1 : free_vars_preformula L 0 ϕ_val = free_vars_formula L ϕ_val, {refl}, have h2 : free_vars_preformula L 0 ψ_val = free_vars_formula L ψ_val, {refl}, simp [h1, h2], rw [ϕ_property_left, ψ_property_left], refl}, {split, {exact and.right (ϕ.property)}, {exact and.right (ψ.property)}, } end #reduce true_is_sentence -- For now, I am deciding against a proof system with nonempty contexts. -- reflecting Prop-tautologies into sentences L inductive propositional_combination : ((list (sentences)) → sentences) → Prop | true : propositional_combination (λ xs, ⟨preformula.true L, true_is_sentence⟩) | false : propositional_combination (λ xs, ⟨preformula.false L, false_is_sentence⟩) | imp : Π f g : ((list (sentences)) → sentences), Π hf : propositional_combination f, Π hg : propositional_combination g, propositional_combination (λ xs, ⟨preformula.imp (f xs).val (g xs).val, imp_is_sentence⟩) -- TODO: get all projections, get all propositional combinations thereof. end
15e0a5c28af79eb76da3c8075b6c2dceeea83745
d29d82a0af640c937e499f6be79fc552eae0aa13
/src/deprecated/subgroup.lean
9cbba3b45b0cfcc5b87b209d804a274091b1efc1
[ "Apache-2.0" ]
permissive
AbdulMajeedkhurasani/mathlib
835f8a5c5cf3075b250b3737172043ab4fa1edf6
79bc7323b164aebd000524ebafd198eb0e17f956
refs/heads/master
1,688,003,895,660
1,627,788,521,000
1,627,788,521,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
25,034
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mitchell Rowett, Scott Morrison, Johan Commelin, Mario Carneiro, Michael Howes -/ import group_theory.subgroup import deprecated.submonoid open set function variables {G : Type*} {H : Type*} {A : Type*} {a a₁ a₂ b c: G} section group variables [group G] [add_group A] /-- `s` is an additive subgroup: a set containing 0 and closed under addition and negation. -/ class is_add_subgroup (s : set A) extends is_add_submonoid s : Prop := (neg_mem {a} : a ∈ s → -a ∈ s) /-- `s` is a subgroup: a set containing 1 and closed under multiplication and inverse. -/ @[to_additive] class is_subgroup (s : set G) extends is_submonoid s : Prop := (inv_mem {a} : a ∈ s → a⁻¹ ∈ s) @[to_additive] lemma is_subgroup.div_mem {s : set G} [is_subgroup s] {x y : G} (hx : x ∈ s) (hy : y ∈ s) : x / y ∈ s := by simpa only [div_eq_mul_inv] using is_submonoid.mul_mem hx (is_subgroup.inv_mem hy) lemma additive.is_add_subgroup (s : set G) [is_subgroup s] : @is_add_subgroup (additive G) _ s := @is_add_subgroup.mk (additive G) _ _ (additive.is_add_submonoid _) (@is_subgroup.inv_mem _ _ _ _) theorem additive.is_add_subgroup_iff {s : set G} : @is_add_subgroup (additive G) _ s ↔ is_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_subgroup.mk G _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by exactI additive.is_add_subgroup _⟩ lemma multiplicative.is_subgroup (s : set A) [is_add_subgroup s] : @is_subgroup (multiplicative A) _ s := @is_subgroup.mk (multiplicative A) _ _ (multiplicative.is_submonoid _) (@is_add_subgroup.neg_mem _ _ _ _) theorem multiplicative.is_subgroup_iff {s : set A} : @is_subgroup (multiplicative A) _ s ↔ is_add_subgroup s := ⟨by rintro ⟨⟨h₁, h₂⟩, h₃⟩; exact @is_add_subgroup.mk A _ _ ⟨h₁, @h₂⟩ @h₃, λ h, by exactI multiplicative.is_subgroup _⟩ /-- The group structure on a subgroup coerced to a type. -/ @[to_additive "The additive group structure on an additive subgroup coerced to a type."] def subtype.group {s : set G} [is_subgroup s] : group s := { inv := λ x, ⟨(x:G)⁻¹, is_subgroup.inv_mem x.2⟩, mul_left_inv := λ x, subtype.eq $ mul_left_inv x.1, div := λ x y, ⟨(x / y : G), is_subgroup.div_mem x.2 y.2⟩, div_eq_mul_inv := λ x y, subtype.ext $ div_eq_mul_inv x.1 y.1, .. subtype.monoid } /-- The commutative group structure on a commutative subgroup coerced to a type. -/ @[to_additive "The additive commutative group structure on a additive commutative subgroup coerced to a type."] def subtype.comm_group {G : Type*} [comm_group G] {s : set G} [is_subgroup s] : comm_group s := { .. subtype.group, .. subtype.comm_monoid } section local attribute [instance] subtype.group subtype.add_group @[simp, norm_cast, to_additive] lemma is_subgroup.coe_inv {s : set G} [is_subgroup s] (a : s) : ((a⁻¹ : s) : G) = a⁻¹ := rfl attribute [norm_cast] is_add_subgroup.coe_neg @[simp, norm_cast] lemma is_subgroup.coe_gpow {s : set G} [is_subgroup s] (a : s) (n : ℤ) : ((a ^ n : s) : G) = a ^ n := by induction n; simp [is_submonoid.coe_pow a] @[simp, norm_cast] lemma is_add_subgroup.gsmul_coe {s : set A} [is_add_subgroup s] (a : s) (n : ℤ) : ((n • a : s) : A) = n • a := by induction n; simp [is_add_submonoid.smul_coe a] attribute [to_additive gsmul_coe] is_subgroup.coe_gpow end @[to_additive of_add_neg] theorem is_subgroup.of_div (s : set G) (one_mem : (1:G) ∈ s) (div_mem : ∀{a b:G}, a ∈ s → b ∈ s → a * b⁻¹ ∈ s) : is_subgroup s := have inv_mem : ∀a, a ∈ s → a⁻¹ ∈ s, from assume a ha, have 1 * a⁻¹ ∈ s, from div_mem one_mem ha, by simpa, { inv_mem := inv_mem, mul_mem := assume a b ha hb, have a * b⁻¹⁻¹ ∈ s, from div_mem ha (inv_mem b hb), by simpa, one_mem := one_mem } theorem is_add_subgroup.of_sub (s : set A) (zero_mem : (0:A) ∈ s) (sub_mem : ∀{a b:A}, a ∈ s → b ∈ s → a - b ∈ s) : is_add_subgroup s := is_add_subgroup.of_add_neg s zero_mem (λ x y hx hy, by simpa only [sub_eq_add_neg] using sub_mem hx hy) @[to_additive] instance is_subgroup.inter (s₁ s₂ : set G) [is_subgroup s₁] [is_subgroup s₂] : is_subgroup (s₁ ∩ s₂) := { inv_mem := λ x hx, ⟨is_subgroup.inv_mem hx.1, is_subgroup.inv_mem hx.2⟩ } @[to_additive] instance is_subgroup.Inter {ι : Sort*} (s : ι → set G) [h : ∀ y : ι, is_subgroup (s y)] : is_subgroup (set.Inter s) := { inv_mem := λ x h, set.mem_Inter.2 $ λ y, is_subgroup.inv_mem (set.mem_Inter.1 h y) } @[to_additive] lemma is_subgroup_Union_of_directed {ι : Type*} [hι : nonempty ι] (s : ι → set G) [∀ i, is_subgroup (s i)] (directed : ∀ i j, ∃ k, s i ⊆ s k ∧ s j ⊆ s k) : is_subgroup (⋃i, s i) := { inv_mem := λ a ha, let ⟨i, hi⟩ := set.mem_Union.1 ha in set.mem_Union.2 ⟨i, is_subgroup.inv_mem hi⟩, to_is_submonoid := is_submonoid_Union_of_directed s directed } end group namespace is_subgroup open is_submonoid variables [group G] (s : set G) [is_subgroup s] @[to_additive] lemma inv_mem_iff : a⁻¹ ∈ s ↔ a ∈ s := ⟨λ h, by simpa using inv_mem h, inv_mem⟩ @[to_additive] lemma mul_mem_cancel_right (h : a ∈ s) : b * a ∈ s ↔ b ∈ s := ⟨λ hba, by simpa using mul_mem hba (inv_mem h), λ hb, mul_mem hb h⟩ @[to_additive] lemma mul_mem_cancel_left (h : a ∈ s) : a * b ∈ s ↔ b ∈ s := ⟨λ hab, by simpa using mul_mem (inv_mem h) hab, mul_mem h⟩ end is_subgroup class normal_add_subgroup [add_group A] (s : set A) extends is_add_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : A, g + n + -g ∈ s) @[to_additive] class normal_subgroup [group G] (s : set G) extends is_subgroup s : Prop := (normal : ∀ n ∈ s, ∀ g : G, g * n * g⁻¹ ∈ s) @[to_additive] lemma normal_subgroup_of_comm_group [comm_group G] (s : set G) [hs : is_subgroup s] : normal_subgroup s := { normal := λ n hn g, by rwa [mul_right_comm, mul_right_inv, one_mul], ..hs } lemma additive.normal_add_subgroup [group G] (s : set G) [normal_subgroup s] : @normal_add_subgroup (additive G) _ s := @normal_add_subgroup.mk (additive G) _ _ (@additive.is_add_subgroup G _ _ _) (@normal_subgroup.normal _ _ _ _) theorem additive.normal_add_subgroup_iff [group G] {s : set G} : @normal_add_subgroup (additive G) _ s ↔ normal_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_subgroup.mk G _ _ (additive.is_add_subgroup_iff.1 h₁) @h₂, λ h, by exactI additive.normal_add_subgroup _⟩ lemma multiplicative.normal_subgroup [add_group A] (s : set A) [normal_add_subgroup s] : @normal_subgroup (multiplicative A) _ s := @normal_subgroup.mk (multiplicative A) _ _ (@multiplicative.is_subgroup A _ _ _) (@normal_add_subgroup.normal _ _ _ _) theorem multiplicative.normal_subgroup_iff [add_group A] {s : set A} : @normal_subgroup (multiplicative A) _ s ↔ normal_add_subgroup s := ⟨by rintro ⟨h₁, h₂⟩; exact @normal_add_subgroup.mk A _ _ (multiplicative.is_subgroup_iff.1 h₁) @h₂, λ h, by exactI multiplicative.normal_subgroup _⟩ namespace is_subgroup variable [group G] -- Normal subgroup properties @[to_additive] lemma mem_norm_comm {s : set G} [normal_subgroup s] {a b : G} (hab : a * b ∈ s) : b * a ∈ s := have h : a⁻¹ * (a * b) * a⁻¹⁻¹ ∈ s, from normal_subgroup.normal (a * b) hab a⁻¹, by simp at h; exact h @[to_additive] lemma mem_norm_comm_iff {s : set G} [normal_subgroup s] {a b : G} : a * b ∈ s ↔ b * a ∈ s := ⟨mem_norm_comm, mem_norm_comm⟩ /-- The trivial subgroup -/ @[to_additive] def trivial (G : Type*) [group G] : set G := {1} @[simp, to_additive] lemma mem_trivial {g : G} : g ∈ trivial G ↔ g = 1 := mem_singleton_iff @[to_additive] instance trivial_normal : normal_subgroup (trivial G) := by refine {..}; simp [trivial] {contextual := tt} @[to_additive] lemma eq_trivial_iff {s : set G} [is_subgroup s] : s = trivial G ↔ (∀ x ∈ s, x = (1 : G)) := by simp only [set.ext_iff, is_subgroup.mem_trivial]; exact ⟨λ h x, (h x).1, λ h x, ⟨h x, λ hx, hx.symm ▸ is_submonoid.one_mem⟩⟩ @[to_additive] instance univ_subgroup : normal_subgroup (@univ G) := by refine {..}; simp @[to_additive add_center] def center (G : Type*) [group G] : set G := {z | ∀ g, g * z = z * g} @[to_additive mem_add_center] lemma mem_center {a : G} : a ∈ center G ↔ ∀g, g * a = a * g := iff.rfl @[to_additive add_center_normal] instance center_normal : normal_subgroup (center G) := { one_mem := by simp [center], mul_mem := assume a b ha hb g, by rw [←mul_assoc, mem_center.2 ha g, mul_assoc, mem_center.2 hb g, ←mul_assoc], inv_mem := assume a ha g, calc g * a⁻¹ = a⁻¹ * (g * a) * a⁻¹ : by simp [ha g] ... = a⁻¹ * g : by rw [←mul_assoc, mul_assoc]; simp, normal := assume n ha g h, calc h * (g * n * g⁻¹) = h * n : by simp [ha g, mul_assoc] ... = g * g⁻¹ * n * h : by rw ha h; simp ... = g * n * g⁻¹ * h : by rw [mul_assoc g, ha g⁻¹, ←mul_assoc] } @[to_additive add_normalizer] def normalizer (s : set G) : set G := {g : G | ∀ n, n ∈ s ↔ g * n * g⁻¹ ∈ s} @[to_additive] instance normalizer_is_subgroup (s : set G) : is_subgroup (normalizer s) := { one_mem := by simp [normalizer], mul_mem := λ a b (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) (hb : ∀ n, n ∈ s ↔ b * n * b⁻¹ ∈ s) n, by rw [mul_inv_rev, ← mul_assoc, mul_assoc a, mul_assoc a, ← ha, ← hb], inv_mem := λ a (ha : ∀ n, n ∈ s ↔ a * n * a⁻¹ ∈ s) n, by rw [ha (a⁻¹ * n * a⁻¹⁻¹)]; simp [mul_assoc] } @[to_additive subset_add_normalizer] lemma subset_normalizer (s : set G) [is_subgroup s] : s ⊆ normalizer s := λ g hg n, by rw [is_subgroup.mul_mem_cancel_right _ ((is_subgroup.inv_mem_iff _).2 hg), is_subgroup.mul_mem_cancel_left _ hg] local attribute [instance] subtype.group /-- Every subgroup is a normal subgroup of its normalizer -/ @[to_additive add_normal_in_add_normalizer] instance normal_in_normalizer (s : set G) [is_subgroup s] : normal_subgroup (subtype.val ⁻¹' s : set (normalizer s)) := { one_mem := show (1 : G) ∈ s, from is_submonoid.one_mem, mul_mem := λ a b ha hb, show (a * b : G) ∈ s, from is_submonoid.mul_mem ha hb, inv_mem := λ a ha, show (a⁻¹ : G) ∈ s, from is_subgroup.inv_mem ha, normal := λ a ha ⟨m, hm⟩, (hm a).1 ha } end is_subgroup -- Homomorphism subgroups namespace is_group_hom open is_submonoid is_subgroup open is_mul_hom (map_mul) @[to_additive] def ker [group H] (f : G → H) : set G := preimage f (trivial H) @[to_additive] lemma mem_ker [group H] (f : G → H) {x : G} : x ∈ ker f ↔ f x = 1 := mem_trivial variables [group G] [group H] @[to_additive] lemma one_ker_inv (f : G → H) [is_group_hom f] {a b : G} (h : f (a * b⁻¹) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, rw [←inv_inv (f b), eq_inv_of_mul_eq_one h] end @[to_additive] lemma one_ker_inv' (f : G → H) [is_group_hom f] {a b : G} (h : f (a⁻¹ * b) = 1) : f a = f b := begin rw [map_mul f, map_inv f] at h, apply inv_injective, rw eq_inv_of_mul_eq_one h end @[to_additive] lemma inv_ker_one (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a * b⁻¹) = 1 := have f a * (f b)⁻¹ = 1, by rw [h, mul_right_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma inv_ker_one' (f : G → H) [is_group_hom f] {a b : G} (h : f a = f b) : f (a⁻¹ * b) = 1 := have (f a)⁻¹ * f b = 1, by rw [h, mul_left_inv], by rwa [←map_inv f, ←map_mul f] at this @[to_additive] lemma one_iff_ker_inv (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a * b⁻¹) = 1 := ⟨inv_ker_one f, one_ker_inv f⟩ @[to_additive] lemma one_iff_ker_inv' (f : G → H) [is_group_hom f] (a b : G) : f a = f b ↔ f (a⁻¹ * b) = 1 := ⟨inv_ker_one' f, one_ker_inv' f⟩ @[to_additive] lemma inv_iff_ker (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a * b⁻¹ ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv _ _ _ @[to_additive] lemma inv_iff_ker' (f : G → H) [w : is_group_hom f] (a b : G) : f a = f b ↔ a⁻¹ * b ∈ ker f := by rw [mem_ker]; exact one_iff_ker_inv' _ _ _ @[to_additive] instance image_subgroup (f : G → H) [is_group_hom f] (s : set G) [is_subgroup s] : is_subgroup (f '' s) := { mul_mem := assume a₁ a₂ ⟨b₁, hb₁, eq₁⟩ ⟨b₂, hb₂, eq₂⟩, ⟨b₁ * b₂, mul_mem hb₁ hb₂, by simp [eq₁, eq₂, map_mul f]⟩, one_mem := ⟨1, one_mem, map_one f⟩, inv_mem := assume a ⟨b, hb, eq⟩, ⟨b⁻¹, inv_mem hb, by rw map_inv f; simp *⟩ } @[to_additive] instance range_subgroup (f : G → H) [is_group_hom f] : is_subgroup (set.range f) := @set.image_univ _ _ f ▸ is_group_hom.image_subgroup f set.univ local attribute [simp] one_mem inv_mem mul_mem normal_subgroup.normal @[to_additive] instance preimage (f : G → H) [is_group_hom f] (s : set H) [is_subgroup s] : is_subgroup (f ⁻¹' s) := by refine {..}; simp [map_mul f, map_one f, map_inv f, @inv_mem H _ s] {contextual:=tt} @[to_additive] instance preimage_normal (f : G → H) [is_group_hom f] (s : set H) [normal_subgroup s] : normal_subgroup (f ⁻¹' s) := ⟨by simp [map_mul f, map_inv f] {contextual:=tt}⟩ @[to_additive] instance normal_subgroup_ker (f : G → H) [is_group_hom f] : normal_subgroup (ker f) := is_group_hom.preimage_normal f (trivial H) @[to_additive] lemma injective_of_trivial_ker (f : G → H) [is_group_hom f] (h : ker f = trivial G) : function.injective f := begin intros a₁ a₂ hfa, simp [ext_iff, ker, is_subgroup.trivial] at h, have ha : a₁ * a₂⁻¹ = 1, by rw ←h; exact inv_ker_one f hfa, rw [eq_inv_of_mul_eq_one ha, inv_inv a₂] end @[to_additive] lemma trivial_ker_of_injective (f : G → H) [is_group_hom f] (h : function.injective f) : ker f = trivial G := set.ext $ assume x, iff.intro (assume hx, suffices f x = f 1, by simpa using h this, by simp [map_one f]; rwa [mem_ker] at hx) (by simp [mem_ker, is_group_hom.map_one f] {contextual := tt}) @[to_additive] lemma injective_iff_trivial_ker (f : G → H) [is_group_hom f] : function.injective f ↔ ker f = trivial G := ⟨trivial_ker_of_injective f, injective_of_trivial_ker f⟩ @[to_additive] lemma trivial_ker_iff_eq_one (f : G → H) [is_group_hom f] : ker f = trivial G ↔ ∀ x, f x = 1 → x = 1 := by rw set.ext_iff; simp [ker]; exact ⟨λ h x hx, (h x).1 hx, λ h x, ⟨h x, λ hx, by rw [hx, map_one f]⟩⟩ end is_group_hom section local attribute [instance] subtype.group @[to_additive] instance subtype_val.is_group_hom [group G] {s : set G} [is_subgroup s] : is_group_hom (subtype.val : s → G) := { ..subtype_val.is_monoid_hom } @[to_additive] instance coe.is_group_hom [group G] {s : set G} [is_subgroup s] : is_group_hom (coe : s → G) := { ..subtype_val.is_monoid_hom } @[to_additive] instance subtype_mk.is_group_hom [group G] [group H] {s : set G} [is_subgroup s] (f : H → G) [is_group_hom f] (h : ∀ x, f x ∈ s) : is_group_hom (λ x, (⟨f x, h x⟩ : s)) := { ..subtype_mk.is_monoid_hom f h } @[to_additive] instance set_inclusion.is_group_hom [group G] {s t : set G} [is_subgroup s] [is_subgroup t] (h : s ⊆ t) : is_group_hom (set.inclusion h) := subtype_mk.is_group_hom _ _ end section local attribute [instance] subtype.monoid /-- `subtype.val : set.range f → H` as a monoid homomorphism, when `f` is a monoid homomorphism. -/ @[to_additive "`subtype.val : set.range f → H` as an additive monoid homomorphism, when `f` is an additive monoid homomorphism."] def monoid_hom.range_subtype_val [monoid G] [monoid H] (f : G →* H) : (set.range f) →* H := monoid_hom.of subtype.val /-- `set.range_factorization f : G → set.range f` as a monoid homomorphism, when `f` is a monoid homomorphism. -/ @[to_additive "`set.range_factorization f : G → set.range f` as an additive monoid homomorphism, when `f` is an additive monoid homomorphism."] def monoid_hom.range_factorization [monoid G] [monoid H] (f : G →* H) : G →* (set.range f) := { to_fun := set.range_factorization f, map_one' := by { dsimp [set.range_factorization], simp, refl, }, map_mul' := by { intros, dsimp [set.range_factorization], simp, refl, } } end namespace add_group variables [add_group A] inductive in_closure (s : set A) : A → Prop | basic {a : A} : a ∈ s → in_closure a | zero : in_closure 0 | neg {a : A} : in_closure a → in_closure (-a) | add {a b : A} : in_closure a → in_closure b → in_closure (a + b) end add_group namespace group open is_submonoid is_subgroup variables [group G] {s : set G} @[to_additive] inductive in_closure (s : set G) : G → Prop | basic {a : G} : a ∈ s → in_closure a | one : in_closure 1 | inv {a : G} : in_closure a → in_closure a⁻¹ | mul {a b : G} : in_closure a → in_closure b → in_closure (a * b) /-- `group.closure s` is the subgroup closed over `s`, i.e. the smallest subgroup containg s. -/ @[to_additive] def closure (s : set G) : set G := {a | in_closure s a } @[to_additive] lemma mem_closure {a : G} : a ∈ s → a ∈ closure s := in_closure.basic @[to_additive] instance closure.is_subgroup (s : set G) : is_subgroup (closure s) := { one_mem := in_closure.one, mul_mem := assume a b, in_closure.mul, inv_mem := assume a, in_closure.inv } @[to_additive] theorem subset_closure {s : set G} : s ⊆ closure s := λ a, mem_closure @[to_additive] theorem closure_subset {s t : set G} [is_subgroup t] (h : s ⊆ t) : closure s ⊆ t := assume a ha, by induction ha; simp [h _, *, one_mem, mul_mem, inv_mem_iff] @[to_additive] lemma closure_subset_iff (s t : set G) [is_subgroup t] : closure s ⊆ t ↔ s ⊆ t := ⟨assume h b ha, h (mem_closure ha), assume h b ha, closure_subset h ha⟩ @[to_additive] theorem closure_mono {s t : set G} (h : s ⊆ t) : closure s ⊆ closure t := closure_subset $ set.subset.trans h subset_closure @[simp, to_additive] lemma closure_subgroup (s : set G) [is_subgroup s] : closure s = s := set.subset.antisymm (closure_subset $ set.subset.refl s) subset_closure @[to_additive] theorem exists_list_of_mem_closure {s : set G} {a : G} (h : a ∈ closure s) : (∃l:list G, (∀x∈l, x ∈ s ∨ x⁻¹ ∈ s) ∧ l.prod = a) := in_closure.rec_on h (λ x hxs, ⟨[x], list.forall_mem_singleton.2 $ or.inl hxs, one_mul _⟩) ⟨[], list.forall_mem_nil _, rfl⟩ (λ x _ ⟨L, HL1, HL2⟩, ⟨L.reverse.map has_inv.inv, λ x hx, let ⟨y, hy1, hy2⟩ := list.exists_of_mem_map hx in hy2 ▸ or.imp id (by rw [inv_inv]; exact id) (HL1 _ $ list.mem_reverse.1 hy1).symm, HL2 ▸ list.rec_on L one_inv.symm (λ hd tl ih, by rw [list.reverse_cons, list.map_append, list.prod_append, ih, list.map_singleton, list.prod_cons, list.prod_nil, mul_one, list.prod_cons, mul_inv_rev])⟩) (λ x y hx hy ⟨L1, HL1, HL2⟩ ⟨L2, HL3, HL4⟩, ⟨L1 ++ L2, list.forall_mem_append.2 ⟨HL1, HL3⟩, by rw [list.prod_append, HL2, HL4]⟩) @[to_additive] lemma image_closure [group H] (f : G → H) [is_group_hom f] (s : set G) : f '' closure s = closure (f '' s) := le_antisymm begin rintros _ ⟨x, hx, rfl⟩, apply in_closure.rec_on hx; intros, { solve_by_elim [subset_closure, set.mem_image_of_mem] }, { rw [is_monoid_hom.map_one f], apply is_submonoid.one_mem }, { rw [is_group_hom.map_inv f], apply is_subgroup.inv_mem, assumption }, { rw [is_monoid_hom.map_mul f], solve_by_elim [is_submonoid.mul_mem] } end (closure_subset $ set.image_subset _ subset_closure) @[to_additive] theorem mclosure_subset {s : set G} : monoid.closure s ⊆ closure s := monoid.closure_subset $ subset_closure @[to_additive] theorem mclosure_inv_subset {s : set G} : monoid.closure (has_inv.inv ⁻¹' s) ⊆ closure s := monoid.closure_subset $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx) @[to_additive] theorem closure_eq_mclosure {s : set G} : closure s = monoid.closure (s ∪ has_inv.inv ⁻¹' s) := set.subset.antisymm (@closure_subset _ _ _ (monoid.closure (s ∪ has_inv.inv ⁻¹' s)) { inv_mem := λ x hx, monoid.in_closure.rec_on hx (λ x hx, or.cases_on hx (λ hx, monoid.subset_closure $ or.inr $ show x⁻¹⁻¹ ∈ s, from (inv_inv x).symm ▸ hx) (λ hx, monoid.subset_closure $ or.inl hx)) ((@one_inv G _).symm ▸ is_submonoid.one_mem) (λ x y hx hy ihx ihy, (mul_inv_rev x y).symm ▸ is_submonoid.mul_mem ihy ihx) } (set.subset.trans (set.subset_union_left _ _) monoid.subset_closure)) (monoid.closure_subset $ set.union_subset subset_closure $ λ x hx, inv_inv x ▸ (is_subgroup.inv_mem $ subset_closure hx)) @[to_additive] theorem mem_closure_union_iff {G : Type*} [comm_group G] {s t : set G} {x : G} : x ∈ closure (s ∪ t) ↔ ∃ y ∈ closure s, ∃ z ∈ closure t, y * z = x := begin simp only [closure_eq_mclosure, monoid.mem_closure_union_iff, exists_prop, preimage_union], split, { rintro ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨_, hys, _, hzs, rfl⟩, _, ⟨_, hyt, _, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm zs] }, { rintro ⟨_, ⟨ys, hys, zs, hzs, rfl⟩, _, ⟨yt, hyt, zt, hzt, rfl⟩, rfl⟩, refine ⟨_, ⟨ys, hys, yt, hyt, rfl⟩, _, ⟨zs, hzs, zt, hzt, rfl⟩, _⟩, rw [mul_assoc, mul_assoc, mul_left_comm yt] } end end group namespace is_subgroup variable [group G] @[to_additive] lemma trivial_eq_closure : trivial G = group.closure ∅ := subset.antisymm (by simp [set.subset_def, is_submonoid.one_mem]) (group.closure_subset $ by simp) end is_subgroup /-The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ namespace group variables {s : set G} [group G] lemma conjugates_of_subset {t : set G} [normal_subgroup t] {a : G} (h : a ∈ t) : conjugates_of a ⊆ t := λ x hc, begin obtain ⟨c, w⟩ := is_conj_iff.1 hc, have H := normal_subgroup.normal a h c, rwa ←w, end theorem conjugates_of_set_subset' {s t : set G} [normal_subgroup t] (h : s ⊆ t) : conjugates_of_set s ⊆ t := set.bUnion_subset (λ x H, conjugates_of_subset (h H)) /-- The normal closure of a set s is the subgroup closure of all the conjugates of elements of s. It is the smallest normal subgroup containing s. -/ def normal_closure (s : set G) : set G := closure (conjugates_of_set s) theorem conjugates_of_set_subset_normal_closure : conjugates_of_set s ⊆ normal_closure s := subset_closure theorem subset_normal_closure : s ⊆ normal_closure s := set.subset.trans subset_conjugates_of_set conjugates_of_set_subset_normal_closure /-- The normal closure of a set is a subgroup. -/ instance normal_closure.is_subgroup (s : set G) : is_subgroup (normal_closure s) := closure.is_subgroup (conjugates_of_set s) /-- The normal closure of s is a normal subgroup. -/ instance normal_closure.is_normal : normal_subgroup (normal_closure s) := ⟨ λ n h g, begin induction h with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset_normal_closure (conj_mem_conjugates_of_set hx))}, {simpa using (normal_closure.is_subgroup s).one_mem}, {rw ←conj_inv, exact (is_subgroup.inv_mem ihx)}, {rw ←conj_mul, exact (is_submonoid.mul_mem ihx ihy)}, end ⟩ /-- The normal closure of s is the smallest normal subgroup containing s. -/ theorem normal_closure_subset {s t : set G} [normal_subgroup t] (h : s ⊆ t) : normal_closure s ⊆ t := λ a w, begin induction w with x hx x hx ihx x y hx hy ihx ihy, {exact (conjugates_of_set_subset' h $ hx)}, {exact is_submonoid.one_mem}, {exact is_subgroup.inv_mem ihx}, {exact is_submonoid.mul_mem ihx ihy} end lemma normal_closure_subset_iff {s t : set G} [normal_subgroup t] : s ⊆ t ↔ normal_closure s ⊆ t := ⟨normal_closure_subset, set.subset.trans (subset_normal_closure)⟩ theorem normal_closure_mono {s t : set G} : s ⊆ t → normal_closure s ⊆ normal_closure t := λ h, normal_closure_subset (set.subset.trans h (subset_normal_closure)) end group /-- Create a bundled subgroup from a set `s` and `[is_subgroup s]`. -/ @[to_additive "Create a bundled additive subgroup from a set `s` and `[is_add_subgroup s]`."] def subgroup.of [group G] (s : set G) [h : is_subgroup s] : subgroup G := { carrier := s, one_mem' := h.1.1, mul_mem' := h.1.2, inv_mem' := h.2 } @[to_additive] instance subgroup.is_subgroup [group G] (K : subgroup G) : is_subgroup (K : set G) := { one_mem := K.one_mem', mul_mem := K.mul_mem', inv_mem := K.inv_mem' } @[to_additive] instance subgroup.of_normal [group G] (s : set G) [h : is_subgroup s] [n : normal_subgroup s] : subgroup.normal (subgroup.of s) := { conj_mem := n.normal, }
10f48a794453362386956eabcf0086e0cddd39b2
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/ind8.lean
d954852ce3621feb4e1de74e1494ef5180c8a882
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
356
lean
import logic inductive Pair1 (A B : Type) := mk () : A → B → Pair1 A B check Pair1.mk check Pair1.mk Prop Prop true false inductive Pair2 {A : Type} (B : A → Type) := mk () : Π (a : A), B a → Pair2 B check @Pair2.mk check Pair2.mk (λx, Prop) true false inductive Pair3 (A B : Type) := mk : A → B → Pair3 A B check Pair3.mk true false
15418f92220f3144744ac1b934930fc9badb7129
900ff83b8a995f83b07c2fa0715d52fa265c4b9e
/tests/lean/run/585c.lean
a5df5637ed8f4511256de3098a7ba2ce302615cd
[ "Apache-2.0" ]
permissive
moritzbuhl/lean
b2ee98197f1c47255c647228c07c830b229ba766
89e98b56713c027afdf97ad96abd2d54b35a43d5
refs/heads/master
1,688,295,006,616
1,627,741,115,000
1,627,741,115,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
716
lean
namespace list inductive perm {α} : list α → list α → Prop | nil : perm [] [] | cons : Π (x : α) {l₁ l₂ : list α}, perm l₁ l₂ → perm (x::l₁) (x::l₂) | swap : Π (x y : α) (l : list α), perm (y::x::l) (x::y::l) | trans : Π {l₁ l₂ l₃ : list α}, perm l₁ l₂ → perm l₂ l₃ → perm l₁ l₃ infix ~ := perm @[refl] protected axiom perm.refl {α} : ∀ (l : list α), l ~ l @[symm] protected axiom perm.symm {α} {l₁ l₂ : list α} (p : l₁ ~ l₂) : l₂ ~ l₁ attribute [trans] perm.trans end list example (α) (l : list α) (a b : α) : a :: b :: l ~ b :: a :: l := begin success_if_fail { simp [← list.perm.cons] }, -- 💣 apply list.perm.swap end
ef11223c683ce9088f2ca006217b358a08875c70
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/fold.lean
efa15d31b89199676a10c8053a42e079ff8d0d61
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
986
lean
prelude definition Prop := Type.{0} inductive true : Prop | intro : true inductive false : Prop constant num : Type inductive prod (A B : Type*) | mk : A → B → prod infixl ` × `:30 := prod variables a b c : num section local notation `(` t:(foldr `, ` (e r, prod.mk e r)) `)` := t check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldr `, ` (e r, prod.mk r e)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldl `, ` (e r, prod.mk r e)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end section local notation `(` t:(foldl `, ` (e r, prod.mk e r)) `)` := t set_option pp.notation true check (a, false, b, true, c) set_option pp.notation false check (a, false, b, true, c) end
a5f88e6ab130ec636c459eaf1d8c1d19bc68d0d8
eb9357a70318e50e095b58730bebfe0cffee457f
/lean/love01_definitions_and_statements_exercise_sheet.lean
73c49ee7d99c64a6e025bc31e407b5a767d179d4
[]
no_license
Vierkantor/logical_verification_2021
7485dd916953131d501760f023d5b30fbb74d36a
9500b9c194e22a9ab4067321cfed7a1f445afcfc
refs/heads/main
1,692,560,845,086
1,624,721,275,000
1,624,721,275,000
416,354,079
0
0
null
null
null
null
UTF-8
Lean
false
false
4,358
lean
import .love01_definitions_and_statements_demo /-! # LoVe Exercise 1: Definitions and Statements Replace the placeholders (e.g., `:= sorry`) with your solutions. -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /-! ## Question 1: Truncated Subtraction 1.1. Define the function `sub` that implements truncated subtraction on natural numbers by recursion. "Truncated" means that results that mathematically would be negative are represented by 0. For example: `sub 7 2 = 5` `sub 2 7 = 0` -/ def sub : ℕ → ℕ → ℕ := sorry /-! 1.2. Check that your function works as expected. -/ #eval sub 0 0 -- expected: 0 #eval sub 0 1 -- expected: 0 #eval sub 0 7 -- expected: 0 #eval sub 1 0 -- expected: 1 #eval sub 1 1 -- expected: 0 #eval sub 3 0 -- expected: 3 #eval sub 2 7 -- expected: 0 #eval sub 3 1 -- expected: 2 #eval sub 3 3 -- expected: 0 #eval sub 3 7 -- expected: 0 #eval sub 7 2 -- expected: 5 /-! ## Question 2: Arithmetic Expressions Consider the type `aexp` from the lecture and the function `eval` that computes the value of an expression. You will find the definitions in the file `love01_definitions_and_statements_demo.lean`. One way to find them quickly is to 1. hold the Control (on Linux and Windows) or Command (on macOS) key pressed; 2. move the cursor to the identifier `aexp` or `eval`; 3. click the identifier. -/ #check aexp #check eval /-! 2.1. Test that `eval` behaves as expected. Make sure to exercise each constructor at least once. You can use the following environment in your tests. What happens if you divide by zero? Make sure to use `#eval`. For technical reasons, `#reduce` does not work well here. Note that `#eval` (Lean's evaluation command) and `eval` (our evaluation function on `aexp`) are unrelated. -/ def some_env : string → ℤ | "x" := 3 | "y" := 17 | _ := 201 #eval eval some_env (aexp.var "x") -- expected: 3 -- invoke `#eval` here /-! 2.2. The following function simplifies arithmetic expressions involving addition. It simplifies `0 + e` and `e + 0` to `e`. Complete the definition so that it also simplifies expressions involving the other three binary operators. -/ def simplify : aexp → aexp | (aexp.add (aexp.num 0) e₂) := simplify e₂ | (aexp.add e₁ (aexp.num 0)) := simplify e₁ -- insert the missing cases here -- catch-all cases below | (aexp.num i) := aexp.num i | (aexp.var x) := aexp.var x | (aexp.add e₁ e₂) := aexp.add (simplify e₁) (simplify e₂) | (aexp.sub e₁ e₂) := aexp.sub (simplify e₁) (simplify e₂) | (aexp.mul e₁ e₂) := aexp.mul (simplify e₁) (simplify e₂) | (aexp.div e₁ e₂) := aexp.div (simplify e₁) (simplify e₂) /-! 2.3. Is the `simplify` function correct? In fact, what would it mean for it to be correct or not? Intuitively, for `simplify` to be correct, it must return an arithmetic expression that yields the same numeric value when evaluated as the original expression. Given an environment `env` and an expression `e`, state (without proving it) the property that the value of `e` after simplification is the same as the value of `e` before. -/ lemma simplify_correct (env : string → ℤ) (e : aexp) : true := -- replace `true` by your lemma statement sorry /-! ## Question 3: λ-Terms 3.1. Complete the following definitions, by replacing the `sorry` markers by terms of the expected type. Hint: A procedure for doing so systematically is described in Section 1.1.4 of the Hitchhiker's Guide. As explained there, you can use `_` as a placeholder while constructing a term. By hovering over `_`, you will see the current logical context. -/ def I : α → α := λa, a def K : α → β → α := λa b, a def C : (α → β → γ) → β → α → γ := sorry def proj_1st : α → α → α := sorry /-! Please give a different answer than for `proj_1st`. -/ def proj_2nd : α → α → α := sorry def some_nonsense : (α → β → γ) → α → (α → γ) → β → γ := sorry /-! 3.2. Show the typing derivation for your definition of `C` above, on paper or using ASCII or Unicode art. You might find the characters `–` (to draw horizontal bars) and `⊢` useful. -/ -- write your solution in a comment here or on paper end LoVe
11f82f05bafa07f3d6deaa7bed8cb4e2d21fe95b
b7f22e51856f4989b970961f794f1c435f9b8f78
/hott/homotopy/wedge.hlean
d159c9ccdcce2e8c572c7330a316f7eb963e7bf1
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
2,938
hlean
/- Copyright (c) 2016 Jakob von Raumer. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jakob von Raumer, Ulrik Buchholtz The Wedge Sum of Two Pointed Types -/ import hit.pointed_pushout .connectedness types.unit open eq pushout pointed unit trunc_index definition pwedge (A B : Type*) : Type* := ppushout (pconst punit A) (pconst punit B) namespace wedge -- TODO maybe find a cleaner proof protected definition unit (A : Type*) : A ≃* pwedge punit A := begin fapply pequiv_of_pmap, { fapply pmap.mk, intro a, apply pinr a, apply respect_pt }, { fapply is_equiv.adjointify, intro x, fapply pushout.elim_on x, exact λ x, Point A, exact id, intro u, reflexivity, intro x, fapply pushout.rec_on x, intro u, cases u, esimp, apply (glue unit.star)⁻¹, intro a, reflexivity, intro u, cases u, esimp, apply eq_pathover, refine _ ⬝hp !ap_id⁻¹, fapply eq_hconcat, apply ap_compose inr, krewrite elim_glue, fapply eq_hconcat, apply ap_idp, apply square_of_eq, apply con.left_inv, intro a, reflexivity}, end end wedge open trunc is_trunc is_conn function namespace wedge_extension section -- The wedge connectivity lemma (Lemma 8.6.2) parameters {A B : Type*} (n m : ℕ) [cA : is_conn n A] [cB : is_conn m B] (P : A → B → Type) [HP : Πa b, is_trunc (m + n) (P a b)] (f : Πa : A, P a pt) (g : Πb : B, P pt b) (p : f pt = g pt) include cA cB HP private definition Q (a : A) : Type := fiber (λs : (Πb : B, P a b), s (Point B)) (f a) private definition is_trunc_Q (a : A) : is_trunc (n.-1) (Q a) := begin refine @is_conn.elim_general (m.-1) _ _ _ (P a) _ (f a), rewrite [-succ_add_succ, of_nat_add_of_nat], intro b, apply HP end local attribute is_trunc_Q [instance] private definition Q_sec : Πa : A, Q a := is_conn.elim (n.-1) Q (fiber.mk g p⁻¹) protected definition ext : Π(a : A)(b : B), P a b := λa, fiber.point (Q_sec a) protected definition β_left (a : A) : ext a (Point B) = f a := fiber.point_eq (Q_sec a) private definition coh_aux : Σq : ext (Point A) = g, β_left (Point A) = ap (λs : (Πb : B, P (Point A) b), s (Point B)) q ⬝ p⁻¹ := equiv.to_fun (fiber.fiber_eq_equiv (Q_sec (Point A)) (fiber.mk g p⁻¹)) (is_conn.elim_β (n.-1) Q (fiber.mk g p⁻¹)) protected definition β_right (b : B) : ext (Point A) b = g b := apd10 (sigma.pr1 coh_aux) b private definition lem : β_left (Point A) = β_right (Point B) ⬝ p⁻¹ := begin unfold β_right, unfold β_left, krewrite (apd10_eq_ap_eval (sigma.pr1 coh_aux) (Point B)), exact sigma.pr2 coh_aux, end protected definition coh : (β_left (Point A))⁻¹ ⬝ β_right (Point B) = p := by rewrite [lem,con_inv,inv_inv,con.assoc,con.left_inv] end end wedge_extension
26fd0413ae48c588c6d66fef71b183b68fb49939
5f83eb0c32f15aeed5993a3ad5ededb6f31fe7aa
/lean/attic/bitvec-vector.lean
e3b96153907d28aff5f1509811dd044444ef0afe
[]
no_license
uw-unsat/jitterbug
45b54979b156c0f5330012313052f8594abd6f14
78d1e75ad506498b585fbac66985ff9d9d05952d
refs/heads/master
1,689,066,921,433
1,687,061,448,000
1,688,415,161,000
244,440,882
46
5
null
null
null
null
UTF-8
Lean
false
false
33,788
lean
-- Theorems for axiomatization: -- bvmul_comm, bvmulhu_comm, bvmul_decomp, bvurem_of_bvsub_bvmul_bvudiv -- -- The theorems prove that both bvmul and bvmulhu are commutative, -- that 64-bit bvmul can be decomposed into 32-bit operations, and -- that bvurem can be computed using bvsub, bvmul, and bvudiv. -- -- They are used by bvaxiom.rkt to replace bitvector primitives with -- uninterpreted functions. -- -- One can check the proofs using the Lean theorem prover 3.x: -- https://leanprover.github.io -- This file uses bitvec in the Lean library, which is a vector of bools. import data.bitvec import data.vector import ..natutil namespace list section ops variable {α : Type} lemma take_nil (i : ℕ) : take i (@nil α) = (@nil α) := by { induction i; simp } lemma take_full {i : ℕ} {l : list α} (h : i ≥ length l) : take i l = l := begin revert i, induction l with hd tl ih; intros, { rw take_nil }, { rw [length_cons] at h, have hj : exists j, i = nat.succ j, { existsi nat.pred i, rw nat.succ_pred_eq_of_pos, apply nat.lt_of_succ_le, apply le_trans (nat.le_add_left _ _) h }, cases hj with j hj, subst i, simp, rw ih, rw nat.add_one at h, apply nat.le_of_succ_le_succ h } end lemma take_append : ∀ (i : ℕ) (l₁ l₂ : list α), take i (l₁ ++ l₂) = (take i l₁) ++ (take (i - length l₁) l₂) := begin intro, induction i with i ih; intros, { simp }, { cases l₁ with hd tl; simp, rw ih, rw add_comm, rw nat.add_one, rw nat.succ_sub_succ_eq_sub } end lemma take_append_left : ∀ {i : ℕ} (l₁ l₂ : list α) (h : i ≤ length l₁), take i (l₁ ++ l₂) = take i l₁ := begin intros, rw take_append, have hsub : i - length l₁ = 0, { apply nat.sub_eq_zero_of_le h }, simp [hsub] end lemma drop_nil (i : ℕ) : drop i (@nil α) = (@nil α) := by { induction i; simp } lemma drop_full {i : ℕ} {l : list α} (h : i ≥ length l) : drop i l = nil := begin apply eq_nil_of_length_eq_zero, rw length_drop, apply nat.sub_eq_zero_of_le h end lemma drop_append : ∀ (i : ℕ) (l₁ l₂ : list α), drop i (l₁ ++ l₂) = (drop i l₁) ++ (drop (i - length l₁) l₂) := begin intro, induction i with i ih; intros, { simp }, { cases l₁ with hd tl; simp, rw ih, rw add_comm, rw nat.add_one, rw nat.succ_sub_succ_eq_sub } end lemma drop_append_left : ∀ {i : ℕ} (l₁ l₂ : list α) (h : i ≤ length l₁), drop i (l₁ ++ l₂) = (drop i l₁) ++ l₂ := begin intros, rw drop_append, have hsub : i - length l₁ = 0, { apply nat.sub_eq_zero_of_le h }, simp [hsub] end lemma take_drop_cancel : ∀ (i : ℕ) (l : list α), take i l ++ drop i l = l := begin intro, induction i with i ih; intros, { simp }, { cases l; simp, apply ih } end end ops section accumr₂ variables {α β γ φ σ : Type} lemma map_accumr₂_append : ∀ (f : α → β → σ → σ × φ) {x₁ x₂ : list α} {y₁ y₂ : list β} {n₁ n₂ : ℕ} (hx₁ : length x₁ = n₁) (hy₁ : length y₁ = n₁) (hx₂ : length x₂ = n₂) (hy₂ : length y₂ = n₂) (c : σ), map_accumr₂ f (x₁ ++ x₂) (y₁ ++ y₂) c = let r := map_accumr₂ f x₂ y₂ c, q := map_accumr₂ f x₁ y₁ r.1 in (q.1, q.2 ++ r.2) := begin intros, simp, revert x₁ x₂ y₁ y₂ n₂ hx₁ hy₁ hx₂ hy₂ c, induction n₁ with n₁ ih; intros, { have : x₁ = [], { apply eq_nil_of_length_eq_zero hx₁ }, subst x₁, have : y₁ = [], { apply eq_nil_of_length_eq_zero hy₁ }, subst y₁, simp [map_accumr₂] }, { cases x₁ with a x₁, { contradiction }, cases y₁ with b y₁, { contradiction }, rw [nat.succ_eq_add_one, length_cons] at *, have hx₁ : length x₁ = n₁, { apply add_right_cancel hx₁ }, have hy₁ : length y₁ = n₁, { apply add_right_cancel hy₁ }, rw cons_append a, rw cons_append b, simp [map_accumr₂], rw (ih hx₁ hy₁ hx₂ hy₂) } end end accumr₂ end list namespace vector section ops variable {α : Type} variable {n : ℕ} lemma eq_of_append {n m : ℕ} {x₁ y₁ : vector α n} {x₂ y₂ : vector α m} (h₁ : x₁ = y₁) (h₂ : x₂ = y₂) : vector.append x₁ x₂ = vector.append y₁ y₂ := by { simp [h₁, h₂] } def cong {a b : ℕ} (h : a = b) : vector α a → vector α b | ⟨x, p⟩ := ⟨x, h ▸ p⟩ lemma append_nil (x : vector α n) (y : vector α 0) : vector.append x y = x := begin cases x with x hx, cases y with y hy, apply vector.eq, have : y = list.nil, apply list.eq_nil_of_length_eq_zero hy, subst y, simp end lemma append_assoc {a b c : ℕ} (x : vector α a) (y : vector α b) (z : vector α c) : vector.append x (vector.append y z) = cong (add_assoc a b c) (vector.append (vector.append x y) z) := begin cases x with x hx, cases y with y hy, cases z with z hz, simp [append, cong] end theorem append_succ (v : vector α (nat.succ n)) : ∃ (xs : vector α n) (x : α), v = vector.append xs (x :: nil) := begin existsi vector.remove_nth (fin.mk n (nat.lt_succ_self n)) v, existsi vector.nth v (fin.mk n (nat.lt_succ_self n)), cases v with v hv, apply vector.eq, simp [vector.nth, vector.remove_nth], revert n, induction v with hd tl ih; intros, { contradiction }, { have htl : list.length tl = n, { rw list.length_cons at hv, rw nat.succ_eq_add_one at hv, apply nat.add_right_cancel hv, }, cases n; simp [list.remove_nth], { have : tl = [], { apply list.eq_nil_of_length_eq_zero htl }, subst tl, simp }, { apply ih htl } } end lemma rev_induction {C : Π {m : ℕ}, vector α m → Prop} (v : vector α n) (hz : ∀ (v : vector α 0), C v) (hi : ∀ {m : ℕ} (v : vector α m) (a : α), C v → C (append v (a :: nil))) : C v := begin induction n with n ih, { apply hz }, { have h : exists xs x, v = append xs (x::nil), by apply append_succ, cases h with xs h, cases h with x h, rw h, apply hi, apply ih } end theorem take_nil (i : ℕ) : take i nil = cong (eq.symm (nat.min_zero i)) (@nil α) := begin apply vector.eq, simp [list.take_nil], refl end theorem take_full {i : ℕ} (h : i ≥ n) (v : vector α n) : take i v = cong (eq.symm (min_eq_right h)) v := begin cases v with l hl, apply vector.eq, simp [take, cong], subst hl, apply list.take_full h end lemma min3 (i n m : ℕ) : min i n + min (i - n) m = min i (n + m) := begin cases (le_or_gt i n) with h1 h1, { rw min_eq_left h1, rw min_eq_left (le_trans h1 (nat.le_add_right _ _)), have hsub : i - n = 0, { rw nat.sub_eq_zero_iff_le, assumption }, simp [hsub] }, { rw min_eq_right_of_lt h1, cases (le_or_gt i (n + m)) with h2 h2, { rw min_eq_left h2, rw min_eq_left (nat.le_add_to_sub_le h2), rw nat.add_sub_of_le (le_of_lt h1) }, { rw min_eq_right_of_lt h2, rw min_eq_right_of_lt (nat.add_lt_to_lt_sub h2) } } end theorem take_append {m : ℕ} (i : ℕ) (x : vector α n) (y : vector α m) : take i (append x y) = cong (min3 i n m) (append (take i x) (take (i - n) y)) := begin apply vector.eq, cases x with x hx, cases y with y hy, simp [append, take, cong], subst n, apply list.take_append end lemma min3_left {i : ℕ} (h : i ≤ n) (m : ℕ) : min i n = min i (n + m) := begin rw min_eq_left h, have hnm : i ≤ n + m, { apply le_trans h (nat.le_add_right _ _) }, rw min_eq_left hnm end theorem take_append_left {i m : ℕ} (h : i ≤ n) (x : vector α n) (y : vector α m) : take i (append x y) = cong (min3_left h m) (take i x) := begin apply vector.eq, cases x, cases y, simp [append, take, cong], subst n, apply list.take_append_left _ _ h end theorem drop_nil (i : ℕ) : drop i nil = cong (eq.symm (nat.zero_sub i)) (@nil α) := begin apply vector.eq, simp [list.drop_nil], refl end theorem drop_full {i : ℕ} (h : i ≥ n) (v : vector α n) : drop i v = cong (eq.symm (nat.sub_eq_zero_of_le h)) nil := begin cases v with l hl, apply vector.eq, simp [drop, cong], subst hl, apply list.drop_full h end lemma drop3 (i n m : ℕ) : n - i + (m - (i - n)) = n + m - i := begin cases (le_or_gt i n) with h1 h1, { simp [nat.sub_eq_zero_of_le h1], rw ← nat.add_sub_assoc h1, simp }, { simp [nat.sub_eq_zero_of_lt h1], rw nat.sub_sub_eq_add_sub (le_of_lt h1), simp } end theorem drop_append {m : ℕ} (i : ℕ) (x : vector α n) (y : vector α m) : drop i (append x y) = cong (drop3 i n m) (append (drop i x) (drop (i - n) y)) := begin apply vector.eq, cases x with x hx, cases y with y hy, simp [append, drop, cong], subst n, apply list.drop_append end theorem drop_append_left {m i : ℕ} (h : i ≤ n) (x : vector α n) (y : vector α m) : drop i (append x y) = cong (eq.symm (nat.sub_add_comm h)) (append (drop i x) y) := begin apply vector.eq, cases x with x hx, cases y with y hy, simp [append, drop, cong], subst n, apply list.drop_append_left _ _ h end end ops section accumr₂ variables {α β γ φ σ : Type} lemma map_accumr₂_zero (f : α → β → σ → σ × φ) (x : vector α 0) (y : vector β 0) (c : σ) : map_accumr₂ f x y c = (c, nil) := begin cases x with x hx, cases y with y hy, have : x = [], { apply list.eq_nil_of_length_eq_zero hx }, subst x, have : y = [], { apply list.eq_nil_of_length_eq_zero hy }, subst y, refl end lemma map_accumr₂_append : ∀ (f : α → β → σ → σ × φ) {n₁ n₂ : ℕ} (x₁ : vector α n₁) (x₂ : vector α n₂) (y₁ : vector β n₁) (y₂ : vector β n₂) (c : σ), map_accumr₂ f (append x₁ x₂) (append y₁ y₂) c = let r := map_accumr₂ f x₂ y₂ c, q := map_accumr₂ f x₁ y₁ r.1 in (q.1, append q.2 r.2) := begin intros, cases x₁ with x₁ hx₁, cases x₂ with x₂ hx₂, cases y₁ with y₁ hy₁, cases y₂ with y₂ hy₂, simp [append, map_accumr₂], have h : forall {α β : Type} (x y : prod α β) (h1 : x.1 = y.1) (h2 : x.2 = y.2), x = y, { intros, cases x, cases y, simp at *, congr; assumption }, apply h; simp; clear h, { rw list.map_accumr₂_append f hx₁ hy₁ hx₂ hy₂ }, { congr, rw list.map_accumr₂_append f hx₁ hy₁ hx₂ hy₂ } end end accumr₂ end vector namespace bitvec open vector section conversion lemma two_pos : 2 > 0 := by { apply nat.succ_pos 1 } lemma to_nat_ff : bitvec.to_nat (ff::nil) = 0 := by simp [bits_to_nat_to_list, bits_to_nat, add_lsb] lemma to_nat_tt : bitvec.to_nat (tt::nil) = 1 := by simp [bits_to_nat_to_list, bits_to_nat, add_lsb] lemma to_nat_lt_two (b : bool) : bitvec.to_nat (b::nil) < 2 := begin cases b; simp [to_nat_ff, to_nat_tt], { apply two_pos }, { apply nat.lt_succ_self } end lemma to_nat_div_two (b : bool) : bitvec.to_nat (b::nil) / 2 = 0 := nat.div_eq_of_lt (to_nat_lt_two b) lemma to_nat_mod_two (b : bool) : bitvec.to_nat (b::nil) % 2 = bitvec.to_nat (b::nil) := nat.mod_eq_of_lt (to_nat_lt_two b) lemma eq_of_to_nat_bool : ∀ (b1 b2 : bool), bitvec.to_nat (b1::nil) = bitvec.to_nat (b2::nil) → b1 = b2 := begin intros, cases b1; cases b2; simp [to_nat_ff, to_nat_tt] at *; assumption end lemma even_ne_odd (n m : ℕ) : n * 2 ≠ 1 + m * 2 := begin intros, rw [add_comm, mul_comm n, mul_comm m], rw [← nat.bit1_val, ← nat.bit0_val], apply nat.bit0_ne_bit1 end lemma odd_ne_even (n m : ℕ) : 1 + n * 2 ≠ m * 2 := begin intros, apply ne.symm, apply even_ne_odd end theorem eq_of_to_nat {n : ℕ} : ∀ (x y : bitvec n), x.to_nat = y.to_nat → x = y := begin induction n with n ih; intros, { simp [vector.eq_nil x, vector.eq_nil y] }, { have hx : exists xs b, x = vector.append xs (b::nil), { apply append_succ }, cases hx with v1 hx, cases hx with b1, subst x, have hy : exists ys b, y = vector.append ys (b::nil), { apply append_succ }, cases hy with v2 hy, cases hy with b2 _, subst y, repeat { rw to_nat_append at * }, have : v1 = v2, { apply ih, cases b1; cases b2; simp [to_nat_ff, to_nat_tt] at *, { apply nat.eq_of_mul_eq_mul_right two_pos; assumption }, { have : false, apply even_ne_odd; assumption, contradiction }, { have : false, apply odd_ne_even; assumption, contradiction }, { apply nat.eq_of_mul_eq_mul_right two_pos, apply add_left_cancel; assumption } }, subst v2, have : b1 = b2, { apply eq_of_to_nat_bool b1 b2, apply add_left_cancel; assumption }, subst b2 } end theorem to_nat_zero (x : bitvec 0) : x.to_nat = 0 := begin cases x with x hx, have : x = [], { apply list.eq_nil_of_length_eq_zero; assumption }, subst x, refl end theorem to_nat_le {n : ℕ} : ∀ (x : bitvec n), x.to_nat ≤ 2^n - 1 := begin induction n with n ih; intros, { simp [to_nat_zero] }, { have hx : exists xs b, x = vector.append xs (b::nil), { apply append_succ }, cases hx with v hx, cases hx with b, subst x, rw to_nat_append, rw nat.pow_succ, cases b; simp [to_nat_ff, to_nat_tt], { calc bitvec.to_nat v * 2 ≤ (2^n - 1) * 2 : by { apply nat.mul_le_mul_right, apply ih } ... = 2^n * 2 - 2 : by { apply nat.mul_sub_right_distrib } ... ≤ 2^n * 2 - 1 : by { apply nat.sub_le_sub_left, apply nat.le_succ } }, { calc 1 + bitvec.to_nat v * 2 ≤ 1 + (2^n - 1) * 2 : by { apply nat.add_le_add_left, apply nat.mul_le_mul_right, apply ih } ... = 2^n * 2 - 2 + 1 : by { rw nat.mul_sub_right_distrib, simp } ... = 2^n * 2 - 1 : by { rw ← nat.sub_sub_eq_sub_add, { apply nat.le_mul_of_pos_left, apply nat.pos_pow_of_pos _ two_pos }, { apply nat.le_succ } } } } end theorem to_nat_lt {n : ℕ} (x : bitvec n) : x.to_nat < 2^n := begin apply lt_of_le_of_lt (to_nat_le x), apply nat.sub_lt _ nat.one_pos, apply nat.pos_pow_of_pos _ two_pos end theorem of_nat_to_nat {n : ℕ} (x : bitvec n) : bitvec.of_nat n (bitvec.to_nat x) = x := begin apply eq_of_to_nat, rw to_nat_of_nat, apply nat.mod_eq_of_lt, apply to_nat_lt end theorem to_nat_cong {n m : ℕ} (h : n = m) (x : bitvec n) : bitvec.to_nat (vector.cong h x) = bitvec.to_nat x := begin cases x with x hx, refl end end conversion lemma repeat_succ {α : Type}: ∀ (a : α) (n : ℕ), vector.repeat a (nat.succ n) = vector.append (repeat a n) (a :: nil) := begin intros, apply vector.eq, simp [vector.repeat], induction n; simp; assumption end lemma zero_of_nat {n : ℕ}: 0 = bitvec.of_nat n 0 := begin unfold has_zero.zero, induction n with n ih, { refl }, { simp [bitvec.zero, repeat_succ, of_nat_succ] at *, rw ih } end lemma one_of_nat {n : ℕ}: 1 = bitvec.of_nat n 1 := begin unfold has_one.one, induction n with n ih, { refl }, { simp [bitvec.one, repeat_succ, of_nat_succ], have h : 1 < 2, { apply nat.lt_succ_self }, rw <- zero_of_nat, refl } end section add variable {n : ℕ} lemma adc_head_tail (x y : bitvec n) (c : bool) : adc x y c = let r := map_accumr₂ (λ x y c, (bitvec.carry x y c, bitvec.xor3 x y c)) x y c in r.1 :: r.2 := begin simp [adc], cases h : map_accumr₂ _ x y c, refl end def adc_zero (x y : bitvec 0) (c : bool) : adc x y c = c :: nil := begin rw adc_head_tail, simp [map_accumr₂_zero] end lemma adc_append : ∀ {n m : ℕ} (x₁ : bitvec n) (x₂ : bitvec m) (y₁ : bitvec n) (y₂ : bitvec m) (c : bool), adc (vector.append x₁ x₂) (vector.append y₁ y₂) c = let r := adc x₂ y₂ c, q := adc x₁ y₁ (head r) in head q :: vector.append (tail q) (tail r) := begin intros, repeat { rw adc_head_tail; simp }, simp [head_cons, tail_cons], rw vector.map_accumr₂_append end lemma adc_to_nat : ∀ (x y : bitvec n) (c : bool), tail (adc x y c) = bitvec.of_nat n (x.to_nat + y.to_nat + cond c 1 0) := begin induction n with n ih; intros, { rw adc_zero, refl }, { have hx : exists xs b, x = vector.append xs (b::nil), { apply append_succ }, cases hx with v1 hx, cases hx with b1, subst x, have hy : exists xs b, y = vector.append xs (b::nil), { apply append_succ }, cases hy with v2 hy, cases hy with b2, subst y, rw adc_append, simp, rw ih, simp, simp [head_cons, tail_cons], simp [of_nat_succ, to_nat_append], have h : ∀ (a b x y : ℕ), a + (x * 2 + (y * 2 + b)) = a + b + (x + y) * 2, { intros, simp [right_distrib] }, rw h, clear h, apply vector.eq_of_append, { rw nat.add_mul_div_right _ _ two_pos, simp, congr, cases b1; cases b2; cases c; refl }, { rw nat.add_mul_mod_self_right, cases b1; cases b2; cases c; refl } } end end add namespace bv section ops variable {n : ℕ} def add (x y : bitvec n) := bitvec.of_nat n (x.to_nat + y.to_nat) instance : has_add (bitvec n) := ⟨bv.add⟩ lemma add_of_nat (x y : bitvec n) : x + y = bitvec.of_nat n (x.to_nat + y.to_nat) := rfl lemma add_of_nat_bits (x y : bitvec n) : x + y = bitvec.add x y := begin intros, simp [add_of_nat], unfold bitvec.add, rw adc_to_nat, refl end protected lemma add_comm (a b : bitvec n) : a + b = b + a := by { simp [add_of_nat] } protected lemma add_zero : ∀ a : bitvec n, a + 0 = a := begin intros, apply eq_of_to_nat, simp [add_of_nat, zero_of_nat], simp [to_nat_of_nat], apply nat.mod_eq_of_lt (to_nat_lt _) end protected lemma zero_add (a : bitvec n) : 0 + a = a := bv.add_comm a 0 ▸ bv.add_zero a protected lemma add_assoc : ∀ a b c : bitvec n, a + b + c = a + (b + c) := begin intros, apply eq_of_to_nat, simp [add_of_nat], simp [to_nat_of_nat], simp [nat.add_mod_self_right] end protected def mul (x y : bitvec n) : bitvec n := bitvec.of_nat n (x.to_nat * y.to_nat) instance : has_mul (bitvec n) := ⟨bv.mul⟩ lemma mul_of_nat (x y : bitvec n) : x * y = bitvec.of_nat n (x.to_nat * y.to_nat) := rfl protected lemma mul_comm (a b : bitvec n) : a * b = b * a := by { simp [mul_of_nat, mul_comm] } protected lemma mul_one : ∀ (a : bitvec n), a * 1 = a := begin intros, apply eq_of_to_nat, simp [mul_of_nat, one_of_nat], simp [to_nat_of_nat], cases n, { simp [to_nat_zero] }, { rw (@nat.mod_eq_of_lt 1), { simp, apply nat.mod_eq_of_lt (to_nat_lt _) }, { calc 1 = 2^0 : by simp ... < 2^(nat.succ n) : by { apply nat.pow_lt_pow_of_lt_right, apply nat.lt_succ_self, apply nat.zero_lt_succ } } } end protected lemma one_mul (a : bitvec n) : 1 * a = a := bv.mul_comm a 1 ▸ bv.mul_one a protected lemma mul_assoc : ∀ a b c : (bitvec n), a * b * c = a * (b * c) := begin intros, apply eq_of_to_nat, simp [mul_of_nat], simp [to_nat_of_nat], simp [nat.mul_mod_self_left, nat.mul_mod_self_right, mul_assoc] end protected lemma distrib_left : ∀ a b c : (bitvec n), a * (b + c) = a * b + a * c := begin intros, apply eq_of_to_nat, simp [add_of_nat, mul_of_nat], simp [to_nat_of_nat], simp [nat.mul_mod_self_right, nat.left_distrib], rw nat.add_mod end protected lemma distrib_right (a b c : bitvec n) : (a + b) * c = a * c + b * c := begin rw [bv.mul_comm, bv.distrib_left], simp [bv.mul_comm] end protected def neg (x : bitvec n) : bitvec n := bitvec.of_nat n (2^n - x.to_nat) instance : has_neg (bitvec n) := ⟨bv.neg⟩ lemma neg_of_nat (x : bitvec n) : -x = bitvec.of_nat n (2^n - x.to_nat) := rfl protected lemma add_left_neg : ∀ a : (bitvec n), -a + a = 0 := begin intros, apply eq_of_to_nat, simp [neg_of_nat, add_of_nat, zero_of_nat], simp [to_nat_of_nat], rw nat.add_mod_self_right, rw ← nat.add_sub_assoc, { rw ← nat.sub_add_eq_add_sub, { simp [nat.sub_self] }, { apply le_refl } }, { apply le_of_lt, apply to_nat_lt } end instance : comm_ring (bitvec n) := { add := bv.add, add_assoc := bv.add_assoc, zero := 0, zero_add := bv.zero_add, add_zero := bv.add_zero, neg := bv.neg, add_left_neg := bv.add_left_neg, add_comm := bv.add_comm, mul := bv.mul, mul_assoc := bv.mul_assoc, one := 1, one_mul := bv.one_mul, mul_one := bv.mul_one, left_distrib := bv.distrib_left, right_distrib := bv.distrib_right, mul_comm := bv.mul_comm } -- Don't use nat.sub, where x - y is defined to be 0 if x < y protected def sub (x y : bitvec n) : bitvec n := bv.add x (bv.neg y) instance : has_sub (bitvec n) := ⟨bv.sub⟩ lemma sub_of_nat (x y : bitvec n) : x - y = bitvec.of_nat n (x.to_nat + (2^n - y.to_nat)) := begin simp [bv.sub], simp [add_of_nat, neg_of_nat], simp [to_nat_of_nat], apply eq_of_to_nat, simp [to_nat_of_nat], rw nat.add_mod_self_right end -- x % 0 = -1 (be careful: nat.div produces 0 in this case) protected def udiv (x y : bitvec n) : bitvec n := bitvec.of_nat n (if y.to_nat = 0 then 2^n - 1 else (x.to_nat / y.to_nat)) -- x % 0 = x (nat.mod has the same behavior) protected def urem (x y : bitvec n) : bitvec n := bitvec.of_nat n (x.to_nat % y.to_nat) lemma to_nat_append_lt {m : ℕ} (x : bitvec n) (y : bitvec m) : y.to_nat + x.to_nat * 2^m < 2^(n + m) := calc y.to_nat + x.to_nat * 2^m ≤ y.to_nat + (2^n - 1) * 2^m : by { apply nat.add_le_add_left, apply nat.mul_le_mul_right, apply to_nat_le } ... < 2^m + (2^n - 1) * 2^m : by { apply nat.add_lt_add_right (to_nat_lt _) } ... = 2^m + (2^n * 2^m - 2^m) : by { simp [nat.mul_sub_right_distrib] } ... = 2^n * 2^m : by { rw nat.add_sub_of_le, apply nat.le_mul_of_pos_left, apply nat.pos_pow_of_pos _ two_pos } ... = 2^(n + m) : by { rw [nat.pow_add_mul] } lemma append_of_nat {m : ℕ} (x : bitvec n) (y : bitvec m) : vector.append x y = bitvec.of_nat (n + m) (x.to_nat * 2^m + y.to_nat) := begin apply eq_of_to_nat, simp [to_nat_of_nat], rw nat.mod_eq_of_lt (to_nat_append_lt _ _), revert n x, induction m with m ih; intros, { simp [to_nat_zero, append_nil] }, { have hy : exists ys b, y = vector.append ys (b::nil), { apply append_succ }, cases hy with v hy, cases hy with b, subst y, rw append_assoc, simp [to_nat_cong, to_nat_append], rw ih, simp [nat.pow_succ], generalize : bitvec.to_nat (b :: nil) = s, generalize : bitvec.to_nat v = t, generalize : bitvec.to_nat x = u, simp [nat.pow_succ], rw [right_distrib, mul_assoc] } end def zext {m : ℕ} (h : n ≤ m) (x : bitvec n) : bitvec m := vector.cong (nat.sub_add_cancel h) (vector.append 0 x) lemma zext_of_nat {m : ℕ} (h : n ≤ m) (x : bitvec n) : zext h x = bitvec.of_nat m x.to_nat := begin unfold zext, apply eq_of_to_nat, simp [append_of_nat, zero_of_nat], simp [to_nat_of_nat, to_nat_cong], rw [nat.add_sub_of_le h] end def concat {m : ℕ} (x : bitvec n) (y : bitvec m) : bitvec (n + m) := vector.append x y lemma concat_of_nat {m : ℕ} (x : bitvec n) (y : bitvec m) : concat x y = bitvec.of_nat (n + m) (x.to_nat * 2^m + y.to_nat) := append_of_nat x y -- specialized version of extract def extract_hi {m : ℕ} (h : m ≤ n) (x : bitvec n) : bitvec m := cong (min_eq_left h) (take m x) lemma extract_hi_of_nat {m : ℕ} (h : m ≤ n) (x : bitvec n) : extract_hi h x = bitvec.of_nat m (x.to_nat / 2^(n - m)) := begin unfold extract_hi, apply eq_of_to_nat, simp [to_nat_of_nat, to_nat_cong], have hrange : x.to_nat / 2^(n - m) < 2^m, { rw nat.div_lt_iff_lt_mul _ _ (nat.pos_pow_of_pos _ two_pos), rw ← nat.pow_add_mul, rw nat.add_sub_of_le h, apply to_nat_lt }, rw nat.mod_eq_of_lt hrange, clear hrange, revert m x, induction n with n ih; intros, { have : m = 0, { apply nat.eq_zero_of_le_zero h }, subst m, simp [to_nat_zero] }, { cases lt_or_eq_of_le h with hlt, { have hx : exists xs b, x = vector.append xs (b::nil), { apply append_succ }, cases hx with v hx, cases hx with b, subst x, have hle : m ≤ n, { apply nat.le_of_lt_succ hlt }, rw take_append_left hle, simp [to_nat_cong, to_nat_append], rw ih _ hle, have heq : nat.succ n - m = n - m + 1, { rw nat.succ_eq_add_one, rw nat.sub_add_eq_add_sub hle }, simp [heq], simp [nat.pow_add_mul], rw ← nat.div_div_eq_div_mul, rw nat.mul_add_div_right _ _ two_pos, simp [to_nat_div_two] }, { subst m, rw take_full h, simp [nat.sub_self, to_nat_cong] } } end def extract_lo {m : ℕ} (h : m ≤ n) (x : bitvec n) : bitvec m := vector.cong (nat.sub_sub_self h) (vector.drop (n - m) x) lemma extract_lo_of_nat {m : ℕ} (h : m ≤ n) (x : bitvec n) : extract_lo h x = bitvec.of_nat m (x.to_nat) := begin unfold extract_lo, apply eq_of_to_nat, simp [to_nat_of_nat, to_nat_cong], revert m x, induction n with n ih; intros, { have : m = 0, { apply nat.eq_zero_of_le_zero h }, subst m, simp [to_nat_zero] }, { cases m, { rw drop_full, { simp [to_nat_cong, to_nat_zero] }, { apply le_refl } }, { rw nat.succ_sub_succ_eq_sub, have hx : exists xs b, x = vector.append xs (b::nil), { apply append_succ }, cases hx with v hx, cases hx with b, subst x, rw drop_append_left (nat.sub_le _ _), simp [to_nat_cong], simp [to_nat_append], have hle : m ≤ n, { apply nat.le_of_succ_le_succ h }, rw ih _ hle, rw nat.mod_pow_succ two_pos, rw nat.mul_add_div_right _ _ two_pos, rw nat.mul_add_mod_left, simp [to_nat_div_two, to_nat_mod_two, mul_comm] } } end theorem concat_extract {n m : ℕ} (x : bitvec (n + m)) : concat (extract_hi (nat.le_add_right n m) x) (extract_lo (nat.le_add_left m n) x) = x := begin cases x, apply vector.eq, simp [concat, extract_hi, extract_lo, take, drop, cong], rw nat.add_sub_cancel, apply list.take_drop_cancel end -- extract and mul lemma extract_lo_mul_of_nat {n m : ℕ} (h : m ≤ n) (x y : bitvec n) : extract_lo h (x * y) = bitvec.of_nat m (x.to_nat * y.to_nat) := begin apply eq_of_to_nat, simp [extract_lo_of_nat, mul_of_nat], simp [to_nat_of_nat], rw nat.dvd_mod_mod, apply nat.dvd_pow_of_le _ h, end lemma extract_hi_mul_of_nat {n m : ℕ} (h : m ≤ n) (x y : bitvec n) : extract_hi h (x * y) = bitvec.of_nat m (x.to_nat * y.to_nat / (2^(n - m))) := begin apply eq_of_to_nat, simp [extract_hi_of_nat, mul_of_nat], simp [to_nat_of_nat], generalize : x.to_nat * y.to_nat = a, calc a % 2^n / 2^(n - m) % 2^m = (a - 2^n * (a / 2^n)) / 2^(n - m) % 2^m : by { simp [nat.mod_sub_div] } ... = (a - 2^(n - m + m) * (a / 2^n)) / 2^(n - m) % 2^m : by { rw nat.sub_add_cancel h } ... = (a - 2^(n - m) * (2^m * (a / 2^n))) / 2^(n - m) % 2^m : by { rw [nat.pow_add_mul], rw mul_assoc } ... = (a / 2^(n - m) - 2^m * (a / 2^n)) % 2^m : by { rw nat.sub_mul_div, rw [← mul_assoc, ← nat.pow_add_mul], rw nat.sub_add_cancel h, rw mul_comm, apply nat.div_mul_le_self } ... = a / 2^(n - m) % 2^m : by { apply nat.sub_mul_mod, rw mul_comm, rw ← nat.le_div_iff_mul_le _ _ (nat.pos_pow_of_pos _ two_pos), rw nat.div_div_eq_div_mul, rw ← nat.pow_add_mul, rw nat.sub_add_cancel h } end end ops end bv end bitvec -- theorems open bitvec open bitvec.bv -- bvmulhu lemma le_add_self (n : ℕ): n ≤ n + n := nat.le_add_right n n def bvmulhu {n : ℕ} (x y : bitvec n) : bitvec n := let h := le_add_self n in extract_hi h ((zext h x) * (zext h y)). theorem bvmulhu_comm {n : ℕ} (x y : bitvec n) : bvmulhu x y = bvmulhu y x := by { simp [bvmulhu, mul_comm] } -- lower bits lemma extract_lo_mul_concat {n m : ℕ} (x₁ : bitvec m) (x₀ : bitvec n) (y₁ : bitvec m) (y₀ : bitvec n) : extract_lo (nat.le_add_left n m) ((concat x₁ x₀) * (concat y₁ y₀)) = x₀ * y₀ := begin apply eq_of_to_nat, simp [concat_of_nat, extract_lo_of_nat, mul_of_nat], simp [to_nat_of_nat], rw ← nat.mul_mod, rw nat.dvd_mod_mod, { rw nat.mul_mod, simp [nat.add_mul_mod_self_right], rw ← nat.mul_mod }, { apply nat.dvd_pow_add_right } end -- upper bits lemma extract_hi_mul_concat {n : ℕ} (x₁ x₀ y₁ y₀ : bitvec n) : extract_hi (le_add_self n) ((concat x₁ x₀) * (concat y₁ y₀)) = bvmulhu x₀ y₀ + (x₀ * y₁ + x₁ * y₀) := begin apply eq_of_to_nat, simp [bvmulhu], -- rewrite extract_hi of mul before other operations simp [extract_hi_mul_of_nat], simp [zext_of_nat, concat_of_nat], simp [to_nat_of_nat, nat.add_sub_cancel], have hlt : forall (x : bitvec n), x.to_nat < 2^(n + n), { intros, apply lt_of_lt_of_le (to_nat_lt _), apply nat.pow_le_pow_of_le_right two_pos, apply le_add_self }, repeat { rw nat.mod_eq_of_lt (hlt _) }, clear hlt, repeat { rw nat.mod_eq_of_lt (to_nat_append_lt _ _) }, simp [left_distrib, right_distrib], have heq : forall (a b c d e f k : ℕ), a * (b * k) + (c * k * d + e * k * f) = k * (a * b + c * d + e * f), { intros, calc a * (b * k) + (c * k * d + e * k * f) = k * (a * b) + c * k * d + e * k * f : by { rw ← mul_assoc a b k, simp [mul_comm] } ... = k * (a * b) + k * c * d + k * e * f : by { simp [mul_comm] } ... = k * (a * b) + k * (c * d) + k * (e * f) : by { simp [mul_assoc] } ... = k * (a * b + c * d + e * f) : by { simp [left_distrib] } }, rw heq, clear heq, rw nat.add_mul_div_left _ _ (nat.pos_pow_of_pos _ two_pos), have heq2 : forall (a b c d e k : ℕ), a + (b + c + d * (e * k)) = a + b + c + d * e * k, { intros, simp [mul_assoc] }, rw heq2, clear heq2, rw nat.add_mul_mod_self_right, simp [add_of_nat, mul_of_nat], simp [to_nat_of_nat], -- rewrite the inner one first rw ← nat.add_mod (x₀.to_nat * y₁.to_nat), -- rwrite the outer one next rw ← nat.add_mod end -- combine upper and lower bits theorem bvmul_decomp_concat {n : ℕ} (x₁ x₀ y₁ y₀ : bitvec n) : (concat x₁ x₀) * (concat y₁ y₀) = concat ((bvmulhu x₀ y₀) + (x₀ * y₁ + x₁ * y₀)) (x₀ * y₀) := begin intros, rw ← extract_lo_mul_concat x₁ x₀ y₁ y₀, rw ← extract_hi_mul_concat x₁ x₀ y₁ y₀, rw concat_extract end -- (bveq (concat (bvadd (bvmulhu (extract 31 0 x) (extract 31 0 y)) -- (bvadd (bvmul (extract 31 0 x) (extract 63 32 y)) -- (bvmul (extract 63 32 x) (extract 31 0 y)))) -- (bvmul (extract 31 0 x) (extract 31 0 y))) -- (bvmul x y)) theorem bvmul_decomp {n : ℕ} (x y : bitvec (n + n)) : let h := le_add_self n, x₁ := extract_hi h x, x₀ := extract_lo h x, y₁ := extract_hi h y, y₀ := extract_lo h y in x * y = concat ((bvmulhu x₀ y₀) + (x₀ * y₁ + x₁ * y₀)) (x₀ * y₀) := begin intros, rw ← bvmul_decomp_concat, simp [concat_extract] end -- x % y = x - (x / y) * y -- One can also prove this using SMT in lemmas.rkt. theorem bvurem_of_bvsub_bvmul_bvudiv {n : ℕ} (x y : bitvec n) : bv.urem x y = x - (bv.udiv x y) * y := begin apply eq_of_to_nat, simp [sub_of_nat, mul_of_nat], unfold bv.urem, unfold bv.udiv, simp [to_nat_of_nat], cases (y.to_nat) with y, { simp }, { rw [if_neg], { generalize : nat.succ y = k, have hmod : x.to_nat % k < 2^n, { apply lt_of_le_of_lt, apply nat.mod_le, apply to_nat_lt }, simp [nat.mod_eq_of_lt hmod], have hdiv : x.to_nat / k < 2^n, { apply lt_of_le_of_lt, apply nat.div_le_self, apply to_nat_lt }, simp [nat.mod_eq_of_lt hdiv], have h : x.to_nat / k * k < 2^n, { apply lt_of_le_of_lt, apply nat.div_mul_le_self, apply to_nat_lt }, simp [nat.mod_eq_of_lt h], calc x.to_nat % k = x.to_nat - x.to_nat / k * k : by { rw mul_comm, apply nat.mod_sub_div } ... = (x.to_nat - x.to_nat / k * k) % 2^n : by { rw nat.mod_eq_of_lt, apply lt_of_le_of_lt, apply nat.sub_le, apply to_nat_lt } ... = (x.to_nat - x.to_nat / k * k + 2^n) % 2^n : by { rw nat.add_mod_right } ... = (x.to_nat + 2^n - x.to_nat / k * k) % 2^n : by { rw nat.sub_add_eq_add_sub, apply nat.div_mul_le_self } ... = (x.to_nat + (2^n - x.to_nat / k * k)) % 2^n : by { rw nat.add_sub_assoc, apply le_of_lt, assumption } }, { apply nat.succ_ne_zero } } end
c446dd89ad1f3556cb598ee2780bce4e4e6e7e88
88fb7558b0636ec6b181f2a548ac11ad3919f8a5
/tests/lean/run/dsimplify2.lean
ed1524559d65718d585cd735cbf3294f1eb3b87f
[ "Apache-2.0" ]
permissive
moritayasuaki/lean
9f666c323cb6fa1f31ac597d777914aed41e3b7a
ae96ebf6ee953088c235ff7ae0e8c95066ba8001
refs/heads/master
1,611,135,440,814
1,493,852,869,000
1,493,852,869,000
90,269,903
0
0
null
1,493,906,291,000
1,493,906,291,000
null
UTF-8
Lean
false
false
416
lean
open tactic def f : nat → nat := λ x, x + 10 @[simp] lemma f_lemma (x : nat) : f x = x + 10 := rfl example (p : nat → Prop) (a : nat) (h : p (a + 10)) : p (f a) := by do t ← target, S ← simp_lemmas.mk_default, new_t ← dsimplify (λ e, failed) (λ e, do new_e ← S^.drewrite e, return (new_e, tt)) t, expected ← to_expr `(p (a + 10)), guard (new_t = expected), change new_t, assumption
5206740512aa8cf514cb144d43d3fdab253f03ce
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/group_theory/perm/cycle_type.lean
d511c35b9f2c7c6e31f15c667446b52694e871db
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
26,356
lean
/- Copyright (c) 2020 Thomas Browning. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Thomas Browning -/ import algebra.gcd_monoid.multiset import combinatorics.partition import group_theory.perm.cycles import ring_theory.int.basic import tactic.linarith /-! # Cycle Types In this file we define the cycle type of a permutation. ## Main definitions - `σ.cycle_type` where `σ` is a permutation of a `fintype` - `σ.partition` where `σ` is a permutation of a `fintype` ## Main results - `sum_cycle_type` : The sum of `σ.cycle_type` equals `σ.support.card` - `lcm_cycle_type` : The lcm of `σ.cycle_type` equals `order_of σ` - `is_conj_iff_cycle_type_eq` : Two permutations are conjugate if and only if they have the same cycle type. * `exists_prime_order_of_dvd_card`: For every prime `p` dividing the order of a finite group `G` there exists an element of order `p` in `G`. This is known as Cauchy`s theorem. -/ namespace equiv.perm open equiv list multiset variables {α : Type*} [fintype α] section cycle_type variables [decidable_eq α] /-- The cycle type of a permutation -/ def cycle_type (σ : perm α) : multiset ℕ := σ.cycle_factors_finset.1.map (finset.card ∘ support) lemma cycle_type_def (σ : perm α) : σ.cycle_type = σ.cycle_factors_finset.1.map (finset.card ∘ support) := rfl lemma cycle_type_eq' {σ : perm α} (s : finset (perm α)) (h1 : ∀ f : perm α, f ∈ s → f.is_cycle) (h2 : ∀ (a ∈ s) (b ∈ s), a ≠ b → disjoint a b) (h0 : s.noncomm_prod id (λ a ha b hb, (em (a = b)).by_cases (λ h, h ▸ commute.refl a) (set.pairwise.mono' (λ _ _, disjoint.commute) h2 ha hb)) = σ) : σ.cycle_type = s.1.map (finset.card ∘ support) := begin rw cycle_type_def, congr, rw cycle_factors_finset_eq_finset, exact ⟨h1, h2, h0⟩ end lemma cycle_type_eq {σ : perm α} (l : list (perm α)) (h0 : l.prod = σ) (h1 : ∀ σ : perm α, σ ∈ l → σ.is_cycle) (h2 : l.pairwise disjoint) : σ.cycle_type = l.map (finset.card ∘ support) := begin have hl : l.nodup := nodup_of_pairwise_disjoint_cycles h1 h2, rw cycle_type_eq' l.to_finset, { simp [list.erase_dup_eq_self.mpr hl] }, { simpa using h1 }, { simpa [hl] using h0 }, { simpa [list.erase_dup_eq_self.mpr hl] using list.forall_of_pairwise disjoint.symmetric h2 } end lemma cycle_type_one : (1 : perm α).cycle_type = 0 := cycle_type_eq [] rfl (λ _, false.elim) pairwise.nil lemma cycle_type_eq_zero {σ : perm α} : σ.cycle_type = 0 ↔ σ = 1 := by simp [cycle_type_def, cycle_factors_finset_eq_empty_iff] lemma card_cycle_type_eq_zero {σ : perm α} : σ.cycle_type.card = 0 ↔ σ = 1 := by rw [card_eq_zero, cycle_type_eq_zero] lemma two_le_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 2 ≤ n := begin simp only [cycle_type_def, ←finset.mem_def, function.comp_app, multiset.mem_map, mem_cycle_factors_finset_iff] at h, obtain ⟨_, ⟨hc, -⟩, rfl⟩ := h, exact hc.two_le_card_support end lemma one_lt_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : 1 < n := two_le_of_mem_cycle_type h lemma is_cycle.cycle_type {σ : perm α} (hσ : is_cycle σ) : σ.cycle_type = [σ.support.card] := cycle_type_eq [σ] (mul_one σ) (λ τ hτ, (congr_arg is_cycle (list.mem_singleton.mp hτ)).mpr hσ) (pairwise_singleton disjoint σ) lemma card_cycle_type_eq_one {σ : perm α} : σ.cycle_type.card = 1 ↔ σ.is_cycle := begin rw card_eq_one, simp_rw [cycle_type_def, multiset.map_eq_singleton, ←finset.singleton_val, finset.val_inj, cycle_factors_finset_eq_singleton_iff], split, { rintro ⟨_, _, ⟨h, -⟩, -⟩, exact h }, { intro h, use [σ.support.card, σ], simp [h] } end lemma disjoint.cycle_type {σ τ : perm α} (h : disjoint σ τ) : (σ * τ).cycle_type = σ.cycle_type + τ.cycle_type := begin rw [cycle_type_def, cycle_type_def, cycle_type_def, h.cycle_factors_finset_mul_eq_union, ←multiset.map_add, finset.union_val, multiset.add_eq_union_iff_disjoint.mpr _], rw [←finset.disjoint_val], exact h.disjoint_cycle_factors_finset end lemma cycle_type_inv (σ : perm α) : σ⁻¹.cycle_type = σ.cycle_type := cycle_induction_on (λ τ : perm α, τ⁻¹.cycle_type = τ.cycle_type) σ rfl (λ σ hσ, by rw [hσ.cycle_type, hσ.inv.cycle_type, support_inv]) (λ σ τ hστ hc hσ hτ, by rw [mul_inv_rev, hστ.cycle_type, ←hσ, ←hτ, add_comm, disjoint.cycle_type (λ x, or.imp (λ h : τ x = x, inv_eq_iff_eq.mpr h.symm) (λ h : σ x = x, inv_eq_iff_eq.mpr h.symm) (hστ x).symm)]) lemma cycle_type_conj {σ τ : perm α} : (τ * σ * τ⁻¹).cycle_type = σ.cycle_type := begin revert τ, apply cycle_induction_on _ σ, { intro, simp }, { intros σ hσ τ, rw [hσ.cycle_type, hσ.is_cycle_conj.cycle_type, card_support_conj] }, { intros σ τ hd hc hσ hτ π, rw [← conj_mul, hd.cycle_type, disjoint.cycle_type, hσ, hτ], intro a, apply (hd (π⁻¹ a)).imp _ _; { intro h, rw [perm.mul_apply, perm.mul_apply, h, apply_inv_self] } } end lemma sum_cycle_type (σ : perm α) : σ.cycle_type.sum = σ.support.card := cycle_induction_on (λ τ : perm α, τ.cycle_type.sum = τ.support.card) σ (by rw [cycle_type_one, sum_zero, support_one, finset.card_empty]) (λ σ hσ, by rw [hσ.cycle_type, coe_sum, list.sum_singleton]) (λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, sum_add, hσ, hτ, hστ.card_support_mul]) lemma sign_of_cycle_type (σ : perm α) : sign σ = (σ.cycle_type.map (λ n, -(-1 : ℤˣ) ^ n)).prod := cycle_induction_on (λ τ : perm α, sign τ = (τ.cycle_type.map (λ n, -(-1 : ℤˣ) ^ n)).prod) σ (by rw [sign_one, cycle_type_one, multiset.map_zero, prod_zero]) (λ σ hσ, by rw [hσ.sign, hσ.cycle_type, coe_map, coe_prod, list.map_singleton, list.prod_singleton]) (λ σ τ hστ hc hσ hτ, by rw [sign_mul, hσ, hτ, hστ.cycle_type, multiset.map_add, prod_add]) lemma lcm_cycle_type (σ : perm α) : σ.cycle_type.lcm = order_of σ := cycle_induction_on (λ τ : perm α, τ.cycle_type.lcm = order_of τ) σ (by rw [cycle_type_one, lcm_zero, order_of_one]) (λ σ hσ, by rw [hσ.cycle_type, ←singleton_coe, ←singleton_eq_cons, lcm_singleton, order_of_is_cycle hσ, normalize_eq]) (λ σ τ hστ hc hσ hτ, by rw [hστ.cycle_type, lcm_add, lcm_eq_nat_lcm, hστ.order_of, hσ, hτ]) lemma dvd_of_mem_cycle_type {σ : perm α} {n : ℕ} (h : n ∈ σ.cycle_type) : n ∣ order_of σ := begin rw ← lcm_cycle_type, exact dvd_lcm h, end lemma order_of_cycle_of_dvd_order_of (f : perm α) (x : α) : order_of (cycle_of f x) ∣ order_of f := begin by_cases hx : f x = x, { rw ←cycle_of_eq_one_iff at hx, simp [hx] }, { refine dvd_of_mem_cycle_type _, rw [cycle_type, multiset.mem_map], refine ⟨f.cycle_of x, _, _⟩, { rwa [←finset.mem_def, cycle_of_mem_cycle_factors_finset_iff, mem_support] }, { simp [order_of_is_cycle (is_cycle_cycle_of _ hx)] } } end lemma two_dvd_card_support {σ : perm α} (hσ : σ ^ 2 = 1) : 2 ∣ σ.support.card := (congr_arg (has_dvd.dvd 2) σ.sum_cycle_type).mp (multiset.dvd_sum (λ n hn, by rw le_antisymm (nat.le_of_dvd zero_lt_two $ (dvd_of_mem_cycle_type hn).trans $ order_of_dvd_of_pow_eq_one hσ) (two_le_of_mem_cycle_type hn))) lemma cycle_type_prime_order {σ : perm α} (hσ : (order_of σ).prime) : ∃ n : ℕ, σ.cycle_type = repeat (order_of σ) (n + 1) := begin rw eq_repeat_of_mem (λ n hn, or_iff_not_imp_left.mp (hσ.eq_one_or_self_of_dvd n (dvd_of_mem_cycle_type hn)) (one_lt_of_mem_cycle_type hn).ne'), use σ.cycle_type.card - 1, rw tsub_add_cancel_of_le, rw [nat.succ_le_iff, pos_iff_ne_zero, ne, card_cycle_type_eq_zero], intro H, rw [H, order_of_one] at hσ, exact hσ.ne_one rfl, end lemma is_cycle_of_prime_order {σ : perm α} (h1 : (order_of σ).prime) (h2 : σ.support.card < 2 * (order_of σ)) : σ.is_cycle := begin obtain ⟨n, hn⟩ := cycle_type_prime_order h1, rw [←σ.sum_cycle_type, hn, multiset.sum_repeat, nsmul_eq_mul, nat.cast_id, mul_lt_mul_right (order_of_pos σ), nat.succ_lt_succ_iff, nat.lt_succ_iff, nat.le_zero_iff] at h2, rw [←card_cycle_type_eq_one, hn, card_repeat, h2], end lemma cycle_type_le_of_mem_cycle_factors_finset {f g : perm α} (hf : f ∈ g.cycle_factors_finset) : f.cycle_type ≤ g.cycle_type := begin rw mem_cycle_factors_finset_iff at hf, rw [cycle_type_def, cycle_type_def, hf.left.cycle_factors_finset_eq_singleton], refine map_le_map _, simpa [←finset.mem_def, mem_cycle_factors_finset_iff] using hf end lemma cycle_type_mul_mem_cycle_factors_finset_eq_sub {f g : perm α} (hf : f ∈ g.cycle_factors_finset) : (g * f⁻¹).cycle_type = g.cycle_type - f.cycle_type := begin suffices : (g * f⁻¹).cycle_type + f.cycle_type = g.cycle_type - f.cycle_type + f.cycle_type, { rw tsub_add_cancel_of_le (cycle_type_le_of_mem_cycle_factors_finset hf) at this, simp [←this] }, simp [←(disjoint_mul_inv_of_mem_cycle_factors_finset hf).cycle_type, tsub_add_cancel_of_le (cycle_type_le_of_mem_cycle_factors_finset hf)] end theorem is_conj_of_cycle_type_eq {σ τ : perm α} (h : cycle_type σ = cycle_type τ) : is_conj σ τ := begin revert τ, apply cycle_induction_on _ σ, { intros τ h, rw [cycle_type_one, eq_comm, cycle_type_eq_zero] at h, rw h }, { intros σ hσ τ hστ, have hτ := card_cycle_type_eq_one.2 hσ, rw [hστ, card_cycle_type_eq_one] at hτ, apply hσ.is_conj hτ, rw [hσ.cycle_type, hτ.cycle_type, coe_eq_coe, singleton_perm] at hστ, simp only [and_true, eq_self_iff_true] at hστ, exact hστ }, { intros σ τ hστ hσ h1 h2 π hπ, rw [hστ.cycle_type] at hπ, { have h : σ.support.card ∈ map (finset.card ∘ perm.support) π.cycle_factors_finset.val, { simp [←cycle_type_def, ←hπ, hσ.cycle_type] }, obtain ⟨σ', hσ'l, hσ'⟩ := multiset.mem_map.mp h, have key : is_conj (σ' * (π * σ'⁻¹)) π, { rw is_conj_iff, use σ'⁻¹, simp [mul_assoc] }, refine is_conj.trans _ key, have hs : σ.cycle_type = σ'.cycle_type, { rw [←finset.mem_def, mem_cycle_factors_finset_iff] at hσ'l, rw [hσ.cycle_type, ←hσ', hσ'l.left.cycle_type] }, refine hστ.is_conj_mul (h1 hs) (h2 _) _, { rw [cycle_type_mul_mem_cycle_factors_finset_eq_sub, ←hπ, add_comm, hs, add_tsub_cancel_right], rwa finset.mem_def }, { exact (disjoint_mul_inv_of_mem_cycle_factors_finset hσ'l).symm } } } end theorem is_conj_iff_cycle_type_eq {σ τ : perm α} : is_conj σ τ ↔ σ.cycle_type = τ.cycle_type := ⟨λ h, begin obtain ⟨π, rfl⟩ := is_conj_iff.1 h, rw cycle_type_conj, end, is_conj_of_cycle_type_eq⟩ @[simp] lemma cycle_type_extend_domain {β : Type*} [fintype β] [decidable_eq β] {p : β → Prop} [decidable_pred p] (f : α ≃ subtype p) {g : perm α} : cycle_type (g.extend_domain f) = cycle_type g := begin apply cycle_induction_on _ g, { rw [extend_domain_one, cycle_type_one, cycle_type_one] }, { intros σ hσ, rw [(hσ.extend_domain f).cycle_type, hσ.cycle_type, card_support_extend_domain] }, { intros σ τ hd hc hσ hτ, rw [hd.cycle_type, ← extend_domain_mul, (hd.extend_domain f).cycle_type, hσ, hτ] } end lemma mem_cycle_type_iff {n : ℕ} {σ : perm α} : n ∈ cycle_type σ ↔ ∃ c τ : perm α, σ = c * τ ∧ disjoint c τ ∧ is_cycle c ∧ c.support.card = n := begin split, { intro h, obtain ⟨l, rfl, hlc, hld⟩ := trunc_cycle_factors σ, rw cycle_type_eq _ rfl hlc hld at h, obtain ⟨c, cl, rfl⟩ := list.exists_of_mem_map h, rw (list.perm_cons_erase cl).pairwise_iff (λ _ _ hd, _) at hld, swap, { exact hd.symm }, refine ⟨c, (l.erase c).prod, _, _, hlc _ cl, rfl⟩, { rw [← list.prod_cons, (list.perm_cons_erase cl).symm.prod_eq' (hld.imp (λ _ _, disjoint.commute))] }, { exact disjoint_prod_right _ (λ g, list.rel_of_pairwise_cons hld) } }, { rintros ⟨c, t, rfl, hd, hc, rfl⟩, simp [hd.cycle_type, hc.cycle_type] } end lemma le_card_support_of_mem_cycle_type {n : ℕ} {σ : perm α} (h : n ∈ cycle_type σ) : n ≤ σ.support.card := (le_sum_of_mem h).trans (le_of_eq σ.sum_cycle_type) lemma cycle_type_of_card_le_mem_cycle_type_add_two {n : ℕ} {g : perm α} (hn2 : fintype.card α < n + 2) (hng : n ∈ g.cycle_type) : g.cycle_type = {n} := begin obtain ⟨c, g', rfl, hd, hc, rfl⟩ := mem_cycle_type_iff.1 hng, by_cases g'1 : g' = 1, { rw [hd.cycle_type, hc.cycle_type, multiset.singleton_eq_cons, multiset.singleton_coe, g'1, cycle_type_one, add_zero] }, contrapose! hn2, apply le_trans _ (c * g').support.card_le_univ, rw [hd.card_support_mul], exact add_le_add_left (two_le_card_support_of_ne_one g'1) _, end end cycle_type lemma card_compl_support_modeq [decidable_eq α] {p n : ℕ} [hp : fact p.prime] {σ : perm α} (hσ : σ ^ p ^ n = 1) : σ.supportᶜ.card ≡ fintype.card α [MOD p] := begin rw [nat.modeq_iff_dvd' σ.supportᶜ.card_le_univ, ←finset.card_compl, compl_compl], refine (congr_arg _ σ.sum_cycle_type).mp (multiset.dvd_sum (λ k hk, _)), obtain ⟨m, -, hm⟩ := (nat.dvd_prime_pow hp.out).mp (order_of_dvd_of_pow_eq_one hσ), obtain ⟨l, -, rfl⟩ := (nat.dvd_prime_pow hp.out).mp ((congr_arg _ hm).mp (dvd_of_mem_cycle_type hk)), exact dvd_pow_self _ (λ h, (one_lt_of_mem_cycle_type hk).ne $ by rw [h, pow_zero]), end lemma exists_fixed_point_of_prime {p n : ℕ} [hp : fact p.prime] (hα : ¬ p ∣ fintype.card α) {σ : perm α} (hσ : σ ^ p ^ n = 1) : ∃ a : α, σ a = a := begin classical, contrapose! hα, simp_rw ← mem_support at hα, exact nat.modeq_zero_iff_dvd.mp ((congr_arg _ (finset.card_eq_zero.mpr (compl_eq_bot.mpr (finset.eq_univ_iff_forall.mpr hα)))).mp (card_compl_support_modeq hσ).symm), end lemma exists_fixed_point_of_prime' {p n : ℕ} [hp : fact p.prime] (hα : p ∣ fintype.card α) {σ : perm α} (hσ : σ ^ p ^ n = 1) {a : α} (ha : σ a = a) : ∃ b : α, σ b = b ∧ b ≠ a := begin classical, have h : ∀ b : α, b ∈ σ.supportᶜ ↔ σ b = b := λ b, by rw [finset.mem_compl, mem_support, not_not], obtain ⟨b, hb1, hb2⟩ := finset.exists_ne_of_one_lt_card (lt_of_lt_of_le hp.out.one_lt (nat.le_of_dvd (finset.card_pos.mpr ⟨a, (h a).mpr ha⟩) (nat.modeq_zero_iff_dvd.mp ((card_compl_support_modeq hσ).trans (nat.modeq_zero_iff_dvd.mpr hα))))) a, exact ⟨b, (h b).mp hb1, hb2⟩, end lemma is_cycle_of_prime_order' {σ : perm α} (h1 : (order_of σ).prime) (h2 : fintype.card α < 2 * (order_of σ)) : σ.is_cycle := begin classical, exact is_cycle_of_prime_order h1 (lt_of_le_of_lt σ.support.card_le_univ h2), end lemma is_cycle_of_prime_order'' {σ : perm α} (h1 : (fintype.card α).prime) (h2 : order_of σ = fintype.card α) : σ.is_cycle := is_cycle_of_prime_order' ((congr_arg nat.prime h2).mpr h1) begin classical, rw [←one_mul (fintype.card α), ←h2, mul_lt_mul_right (order_of_pos σ)], exact one_lt_two, end section cauchy variables (G : Type*) [group G] (n : ℕ) /-- The type of vectors with terms from `G`, length `n`, and product equal to `1:G`. -/ def vectors_prod_eq_one : set (vector G n) := {v | v.to_list.prod = 1} namespace vectors_prod_eq_one lemma mem_iff {n : ℕ} (v : vector G n) : v ∈ vectors_prod_eq_one G n ↔ v.to_list.prod = 1 := iff.rfl lemma zero_eq : vectors_prod_eq_one G 0 = {vector.nil} := set.eq_singleton_iff_unique_mem.mpr ⟨eq.refl (1 : G), λ v hv, v.eq_nil⟩ lemma one_eq : vectors_prod_eq_one G 1 = {vector.nil.cons 1} := begin simp_rw [set.eq_singleton_iff_unique_mem, mem_iff, vector.to_list_singleton, list.prod_singleton, vector.head_cons], exact ⟨rfl, λ v hv, v.cons_head_tail.symm.trans (congr_arg2 vector.cons hv v.tail.eq_nil)⟩, end instance zero_unique : unique (vectors_prod_eq_one G 0) := by { rw zero_eq, exact set.unique_singleton vector.nil } instance one_unique : unique (vectors_prod_eq_one G 1) := by { rw one_eq, exact set.unique_singleton (vector.nil.cons 1) } /-- Given a vector `v` of length `n`, make a vector of length `n + 1` whose product is `1`, by appending the inverse of the product of `v`. -/ @[simps] def vector_equiv : vector G n ≃ vectors_prod_eq_one G (n + 1) := { to_fun := λ v, ⟨v.to_list.prod⁻¹ ::ᵥ v, by rw [mem_iff, vector.to_list_cons, list.prod_cons, inv_mul_self]⟩, inv_fun := λ v, v.1.tail, left_inv := λ v, v.tail_cons v.to_list.prod⁻¹, right_inv := λ v, subtype.ext ((congr_arg2 vector.cons (eq_inv_of_mul_eq_one (by { rw [←list.prod_cons, ←vector.to_list_cons, v.1.cons_head_tail], exact v.2 })).symm rfl).trans v.1.cons_head_tail) } /-- Given a vector `v` of length `n` whose product is 1, make a vector of length `n - 1`, by deleting the last entry of `v`. -/ def equiv_vector : vectors_prod_eq_one G n ≃ vector G (n - 1) := ((vector_equiv G (n - 1)).trans (if hn : n = 0 then (show vectors_prod_eq_one G (n - 1 + 1) ≃ vectors_prod_eq_one G n, by { rw hn, exact equiv_of_unique_of_unique }) else by rw tsub_add_cancel_of_le (nat.pos_of_ne_zero hn).nat_succ_le)).symm instance [fintype G] : fintype (vectors_prod_eq_one G n) := fintype.of_equiv (vector G (n - 1)) (equiv_vector G n).symm lemma card [fintype G] : fintype.card (vectors_prod_eq_one G n) = fintype.card G ^ (n - 1) := (fintype.card_congr (equiv_vector G n)).trans (card_vector (n - 1)) variables {G n} {g : G} (v : vectors_prod_eq_one G n) (j k : ℕ) /-- Rotate a vector whose product is 1. -/ def rotate : vectors_prod_eq_one G n := ⟨⟨_, (v.1.1.length_rotate k).trans v.1.2⟩, list.prod_rotate_eq_one_of_prod_eq_one v.2 k⟩ lemma rotate_zero : rotate v 0 = v := subtype.ext (subtype.ext v.1.1.rotate_zero) lemma rotate_rotate : rotate (rotate v j) k = rotate v (j + k) := subtype.ext (subtype.ext (v.1.1.rotate_rotate j k)) lemma rotate_length : rotate v n = v := subtype.ext (subtype.ext ((congr_arg _ v.1.2.symm).trans v.1.1.rotate_length)) end vectors_prod_eq_one lemma exists_prime_order_of_dvd_card {G : Type*} [group G] [fintype G] (p : ℕ) [hp : fact p.prime] (hdvd : p ∣ fintype.card G) : ∃ x : G, order_of x = p := begin have hp' : p - 1 ≠ 0 := mt tsub_eq_zero_iff_le.mp (not_le_of_lt hp.out.one_lt), have Scard := calc p ∣ fintype.card G ^ (p - 1) : hdvd.trans (dvd_pow (dvd_refl _) hp') ... = fintype.card (vectors_prod_eq_one G p) : (vectors_prod_eq_one.card G p).symm, let f : ℕ → vectors_prod_eq_one G p → vectors_prod_eq_one G p := λ k v, vectors_prod_eq_one.rotate v k, have hf1 : ∀ v, f 0 v = v := vectors_prod_eq_one.rotate_zero, have hf2 : ∀ j k v, f k (f j v) = f (j + k) v := λ j k v, vectors_prod_eq_one.rotate_rotate v j k, have hf3 : ∀ v, f p v = v := vectors_prod_eq_one.rotate_length, let σ := equiv.mk (f 1) (f (p - 1)) (λ s, by rw [hf2, add_tsub_cancel_of_le hp.out.one_lt.le, hf3]) (λ s, by rw [hf2, tsub_add_cancel_of_le hp.out.one_lt.le, hf3]), have hσ : ∀ k v, (σ ^ k) v = f k v := λ k v, nat.rec (hf1 v).symm (λ k hk, eq.trans (by exact congr_arg σ hk) (hf2 k 1 v)) k, replace hσ : σ ^ (p ^ 1) = 1 := perm.ext (λ v, by rw [pow_one, hσ, hf3, one_apply]), let v₀ : vectors_prod_eq_one G p := ⟨vector.repeat 1 p, (list.prod_repeat 1 p).trans (one_pow p)⟩, have hv₀ : σ v₀ = v₀ := subtype.ext (subtype.ext (list.rotate_repeat (1 : G) p 1)), obtain ⟨v, hv1, hv2⟩ := exists_fixed_point_of_prime' Scard hσ hv₀, refine exists_imp_exists (λ g hg, order_of_eq_prime _ (λ hg', hv2 _)) (list.rotate_one_eq_self_iff_eq_repeat.mp (subtype.ext_iff.mp (subtype.ext_iff.mp hv1))), { rw [←list.prod_repeat, ←v.1.2, ←hg, (show v.val.val.prod = 1, from v.2)] }, { rw [subtype.ext_iff_val, subtype.ext_iff_val, hg, hg', v.1.2], refl }, end end cauchy lemma subgroup_eq_top_of_swap_mem [decidable_eq α] {H : subgroup (perm α)} [d : decidable_pred (∈ H)] {τ : perm α} (h0 : (fintype.card α).prime) (h1 : fintype.card α ∣ fintype.card H) (h2 : τ ∈ H) (h3 : is_swap τ) : H = ⊤ := begin haveI : fact (fintype.card α).prime := ⟨h0⟩, obtain ⟨σ, hσ⟩ := exists_prime_order_of_dvd_card (fintype.card α) h1, have hσ1 : order_of (σ : perm α) = fintype.card α := (order_of_subgroup σ).trans hσ, have hσ2 : is_cycle ↑σ := is_cycle_of_prime_order'' h0 hσ1, have hσ3 : (σ : perm α).support = ⊤ := finset.eq_univ_of_card (σ : perm α).support ((order_of_is_cycle hσ2).symm.trans hσ1), have hσ4 : subgroup.closure {↑σ, τ} = ⊤ := closure_prime_cycle_swap h0 hσ2 hσ3 h3, rw [eq_top_iff, ←hσ4, subgroup.closure_le, set.insert_subset, set.singleton_subset_iff], exact ⟨subtype.mem σ, h2⟩, end section partition variables [decidable_eq α] /-- The partition corresponding to a permutation -/ def partition (σ : perm α) : (fintype.card α).partition := { parts := σ.cycle_type + repeat 1 (fintype.card α - σ.support.card), parts_pos := λ n hn, begin cases mem_add.mp hn with hn hn, { exact zero_lt_one.trans (one_lt_of_mem_cycle_type hn) }, { exact lt_of_lt_of_le zero_lt_one (ge_of_eq (multiset.eq_of_mem_repeat hn)) }, end, parts_sum := by rw [sum_add, sum_cycle_type, multiset.sum_repeat, nsmul_eq_mul, nat.cast_id, mul_one, add_tsub_cancel_of_le σ.support.card_le_univ] } lemma parts_partition {σ : perm α} : σ.partition.parts = σ.cycle_type + repeat 1 (fintype.card α - σ.support.card) := rfl lemma filter_parts_partition_eq_cycle_type {σ : perm α} : (partition σ).parts.filter (λ n, 2 ≤ n) = σ.cycle_type := begin rw [parts_partition, filter_add, multiset.filter_eq_self.2 (λ _, two_le_of_mem_cycle_type), multiset.filter_eq_nil.2 (λ a h, _), add_zero], rw multiset.eq_of_mem_repeat h, dec_trivial end lemma partition_eq_of_is_conj {σ τ : perm α} : is_conj σ τ ↔ σ.partition = τ.partition := begin rw [is_conj_iff_cycle_type_eq], refine ⟨λ h, _, λ h, _⟩, { rw [nat.partition.ext_iff, parts_partition, parts_partition, ← sum_cycle_type, ← sum_cycle_type, h] }, { rw [← filter_parts_partition_eq_cycle_type, ← filter_parts_partition_eq_cycle_type, h] } end end partition /-! ### 3-cycles -/ /-- A three-cycle is a cycle of length 3. -/ def is_three_cycle [decidable_eq α] (σ : perm α) : Prop := σ.cycle_type = {3} namespace is_three_cycle variables [decidable_eq α] {σ : perm α} lemma cycle_type (h : is_three_cycle σ) : σ.cycle_type = {3} := h lemma card_support (h : is_three_cycle σ) : σ.support.card = 3 := by rw [←sum_cycle_type, h.cycle_type, multiset.sum_singleton] lemma _root_.card_support_eq_three_iff : σ.support.card = 3 ↔ σ.is_three_cycle := begin refine ⟨λ h, _, is_three_cycle.card_support⟩, by_cases h0 : σ.cycle_type = 0, { rw [←sum_cycle_type, h0, sum_zero] at h, exact (ne_of_lt zero_lt_three h).elim }, obtain ⟨n, hn⟩ := exists_mem_of_ne_zero h0, by_cases h1 : σ.cycle_type.erase n = 0, { rw [←sum_cycle_type, ←cons_erase hn, h1, ←singleton_eq_cons, multiset.sum_singleton] at h, rw [is_three_cycle, ←cons_erase hn, h1, h, singleton_eq_cons] }, obtain ⟨m, hm⟩ := exists_mem_of_ne_zero h1, rw [←sum_cycle_type, ←cons_erase hn, ←cons_erase hm, multiset.sum_cons, multiset.sum_cons] at h, linarith [two_le_of_mem_cycle_type hn, two_le_of_mem_cycle_type (mem_of_mem_erase hm)], end lemma is_cycle (h : is_three_cycle σ) : is_cycle σ := by rw [←card_cycle_type_eq_one, h.cycle_type, card_singleton] lemma sign (h : is_three_cycle σ) : sign σ = 1 := begin rw [sign_of_cycle_type, h.cycle_type], refl, end lemma inv {f : perm α} (h : is_three_cycle f) : is_three_cycle (f⁻¹) := by rwa [is_three_cycle, cycle_type_inv] @[simp] lemma inv_iff {f : perm α} : is_three_cycle (f⁻¹) ↔ is_three_cycle f := ⟨by { rw ← inv_inv f, apply inv }, inv⟩ lemma order_of {g : perm α} (ht : is_three_cycle g) : order_of g = 3 := by rw [←lcm_cycle_type, ht.cycle_type, multiset.lcm_singleton, normalize_eq] lemma is_three_cycle_sq {g : perm α} (ht : is_three_cycle g) : is_three_cycle (g * g) := begin rw [←pow_two, ←card_support_eq_three_iff, support_pow_coprime, ht.card_support], rw [ht.order_of, nat.coprime_iff_gcd_eq_one], norm_num, end end is_three_cycle section variable [decidable_eq α] lemma is_three_cycle_swap_mul_swap_same {a b c : α} (ab : a ≠ b) (ac : a ≠ c) (bc : b ≠ c) : is_three_cycle (swap a b * swap a c) := begin suffices h : support (swap a b * swap a c) = {a, b, c}, { rw [←card_support_eq_three_iff, h], simp [ab, ac, bc] }, apply le_antisymm ((support_mul_le _ _).trans (λ x, _)) (λ x hx, _), { simp [ab, ac, bc] }, { simp only [finset.mem_insert, finset.mem_singleton] at hx, rw mem_support, simp only [perm.coe_mul, function.comp_app, ne.def], obtain rfl | rfl | rfl := hx, { rw [swap_apply_left, swap_apply_of_ne_of_ne ac.symm bc.symm], exact ac.symm }, { rw [swap_apply_of_ne_of_ne ab.symm bc, swap_apply_right], exact ab }, { rw [swap_apply_right, swap_apply_left], exact bc } } end open subgroup lemma swap_mul_swap_same_mem_closure_three_cycles {a b c : α} (ab : a ≠ b) (ac : a ≠ c) : (swap a b * swap a c) ∈ closure {σ : perm α | is_three_cycle σ } := begin by_cases bc : b = c, { subst bc, simp [one_mem] }, exact subset_closure (is_three_cycle_swap_mul_swap_same ab ac bc) end lemma is_swap.mul_mem_closure_three_cycles {σ τ : perm α} (hσ : is_swap σ) (hτ : is_swap τ) : σ * τ ∈ closure {σ : perm α | is_three_cycle σ } := begin obtain ⟨a, b, ab, rfl⟩ := hσ, obtain ⟨c, d, cd, rfl⟩ := hτ, by_cases ac : a = c, { subst ac, exact swap_mul_swap_same_mem_closure_three_cycles ab cd }, have h' : swap a b * swap c d = swap a b * swap a c * (swap c a * swap c d), { simp [swap_comm c a, mul_assoc] }, rw h', exact mul_mem _ (swap_mul_swap_same_mem_closure_three_cycles ab ac) (swap_mul_swap_same_mem_closure_three_cycles (ne.symm ac) cd), end end end equiv.perm
aa97db96b658f26782fd01e3f76fccaf06234aef
a047a4718edfa935d17231e9e6ecec8c7b701e05
/src/data/finset.lean
d126919e4b401027b6d7d50b6a9658ec8c185a8c
[ "Apache-2.0" ]
permissive
utensil-contrib/mathlib
bae0c9fafe5e2bdb516efc89d6f8c1502ecc9767
b91909e77e219098a2f8cc031f89d595fe274bd2
refs/heads/master
1,668,048,976,965
1,592,442,701,000
1,592,442,701,000
273,197,855
0
0
null
1,592,472,812,000
1,592,472,811,000
null
UTF-8
Lean
false
false
124,163
lean
/- Copyright (c) 2015 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura, Jeremy Avigad, Minchao Wu, Mario Carneiro -/ import data.multiset import tactic.monotonicity import tactic.apply /-! # Finite sets -/ open multiset subtype nat variables {α : Type*} {β : Type*} {γ : Type*} /-- `finset α` is the type of finite sets of elements of `α`. It is implemented as a multiset (a list up to permutation) which has no duplicate elements. -/ structure finset (α : Type*) := (val : multiset α) (nodup : nodup val) namespace finset theorem eq_of_veq : ∀ {s t : finset α}, s.1 = t.1 → s = t | ⟨s, _⟩ ⟨t, _⟩ rfl := rfl @[simp] theorem val_inj {s t : finset α} : s.1 = t.1 ↔ s = t := ⟨eq_of_veq, congr_arg _⟩ @[simp] theorem erase_dup_eq_self [decidable_eq α] (s : finset α) : erase_dup s.1 = s.1 := erase_dup_eq_self.2 s.2 instance has_decidable_eq [decidable_eq α] : decidable_eq (finset α) | s₁ s₂ := decidable_of_iff _ val_inj /- membership -/ instance : has_mem α (finset α) := ⟨λ a s, a ∈ s.1⟩ theorem mem_def {a : α} {s : finset α} : a ∈ s ↔ a ∈ s.1 := iff.rfl @[simp] theorem mem_mk {a : α} {s nd} : a ∈ @finset.mk α s nd ↔ a ∈ s := iff.rfl instance decidable_mem [h : decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ s) := multiset.decidable_mem _ _ /-! ### set coercion -/ /-- Convert a finset to a set in the natural way. -/ instance : has_lift (finset α) (set α) := ⟨λ s, {x | x ∈ s}⟩ @[simp, norm_cast] lemma mem_coe {a : α} {s : finset α} : a ∈ (↑s : set α) ↔ a ∈ s := iff.rfl @[simp] lemma set_of_mem {α} {s : finset α} : {a | a ∈ s} = ↑s := rfl @[simp] lemma coe_mem {s : finset α} (x : (↑s : set α)) : ↑x ∈ s := x.2 @[simp] lemma mk_coe {s : finset α} (x : (↑s : set α)) {h} : (⟨↑x, h⟩ : (↑s : set α)) = x := by { apply subtype.eq, refl, } instance decidable_mem' [decidable_eq α] (a : α) (s : finset α) : decidable (a ∈ (↑s : set α)) := s.decidable_mem _ /-! ### extensionality -/ theorem ext_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ ∀ a, a ∈ s₁ ↔ a ∈ s₂ := val_inj.symm.trans $ nodup_ext s₁.2 s₂.2 @[ext] theorem ext {s₁ s₂ : finset α} : (∀ a, a ∈ s₁ ↔ a ∈ s₂) → s₁ = s₂ := ext_iff.2 @[simp, norm_cast] theorem coe_inj {s₁ s₂ : finset α} : (↑s₁ : set α) = ↑s₂ ↔ s₁ = s₂ := set.ext_iff.trans ext_iff.symm lemma coe_injective {α} : function.injective (coe : finset α → set α) := λ s t, coe_inj.1 /-! ### subset -/ instance : has_subset (finset α) := ⟨λ s₁ s₂, ∀ ⦃a⦄, a ∈ s₁ → a ∈ s₂⟩ theorem subset_def {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ s₁.1 ⊆ s₂.1 := iff.rfl @[simp] theorem subset.refl (s : finset α) : s ⊆ s := subset.refl _ theorem subset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₂ ⊆ s₃ → s₁ ⊆ s₃ := subset.trans theorem superset.trans {s₁ s₂ s₃ : finset α} : s₁ ⊇ s₂ → s₂ ⊇ s₃ → s₁ ⊇ s₃ := λ h' h, subset.trans h h' -- TODO: these should be global attributes, but this will require fixing other files local attribute [trans] subset.trans superset.trans theorem mem_of_subset {s₁ s₂ : finset α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := mem_of_subset theorem subset.antisymm {s₁ s₂ : finset α} (H₁ : s₁ ⊆ s₂) (H₂ : s₂ ⊆ s₁) : s₁ = s₂ := ext $ λ a, ⟨@H₁ a, @H₂ a⟩ theorem subset_iff {s₁ s₂ : finset α} : s₁ ⊆ s₂ ↔ ∀ ⦃x⦄, x ∈ s₁ → x ∈ s₂ := iff.rfl @[simp] theorem coe_subset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊆ ↑s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem val_le_iff {s₁ s₂ : finset α} : s₁.1 ≤ s₂.1 ↔ s₁ ⊆ s₂ := le_iff_subset s₁.2 instance : has_ssubset (finset α) := ⟨λa b, a ⊆ b ∧ ¬ b ⊆ a⟩ instance : partial_order (finset α) := { le := (⊆), lt := (⊂), le_refl := subset.refl, le_trans := @subset.trans _, le_antisymm := @subset.antisymm _ } theorem subset.antisymm_iff {s₁ s₂ : finset α} : s₁ = s₂ ↔ s₁ ⊆ s₂ ∧ s₂ ⊆ s₁ := le_antisymm_iff @[simp] theorem le_iff_subset {s₁ s₂ : finset α} : s₁ ≤ s₂ ↔ s₁ ⊆ s₂ := iff.rfl @[simp] theorem lt_iff_ssubset {s₁ s₂ : finset α} : s₁ < s₂ ↔ s₁ ⊂ s₂ := iff.rfl @[simp] lemma coe_ssubset {s₁ s₂ : finset α} : (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊂ s₂ := show (↑s₁ : set α) ⊂ ↑s₂ ↔ s₁ ⊆ s₂ ∧ ¬s₂ ⊆ s₁, by simp only [set.ssubset_def, finset.coe_subset] @[simp] theorem val_lt_iff {s₁ s₂ : finset α} : s₁.1 < s₂.1 ↔ s₁ ⊂ s₂ := and_congr val_le_iff $ not_congr val_le_iff theorem ssubset_iff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ⊂ s₂ ↔ ∃ x ∈ s₂, x ∉ s₁ := set.ssubset_iff_of_subset h /-! ### Nonempty -/ /-- The property `s.nonempty` expresses the fact that the finset `s` is not empty. It should be used in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks to the dot notation. -/ protected def nonempty (s : finset α) : Prop := ∃ x:α, x ∈ s @[norm_cast] lemma coe_nonempty {s : finset α} : (↑s:set α).nonempty ↔ s.nonempty := iff.rfl lemma nonempty.bex {s : finset α} (h : s.nonempty) : ∃ x:α, x ∈ s := h lemma nonempty.mono {s t : finset α} (hst : s ⊆ t) (hs : s.nonempty) : t.nonempty := set.nonempty.mono hst hs /-! ### empty -/ /-- The empty finset -/ protected def empty : finset α := ⟨0, nodup_zero⟩ instance : has_emptyc (finset α) := ⟨finset.empty⟩ instance : inhabited (finset α) := ⟨∅⟩ @[simp] theorem empty_val : (∅ : finset α).1 = 0 := rfl @[simp] theorem not_mem_empty (a : α) : a ∉ (∅ : finset α) := id @[simp] theorem ne_empty_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ≠ ∅ := λ e, not_mem_empty a $ e ▸ h theorem nonempty.ne_empty {s : finset α} (h : s.nonempty) : s ≠ ∅ := exists.elim h $ λ a, ne_empty_of_mem @[simp] theorem empty_subset (s : finset α) : ∅ ⊆ s := zero_subset _ theorem eq_empty_of_forall_not_mem {s : finset α} (H : ∀x, x ∉ s) : s = ∅ := eq_of_veq (eq_zero_of_forall_not_mem H) lemma eq_empty_iff_forall_not_mem {s : finset α} : s = ∅ ↔ ∀ x, x ∉ s := ⟨by rintro rfl x; exact id, λ h, eq_empty_of_forall_not_mem h⟩ @[simp] theorem val_eq_zero {s : finset α} : s.1 = 0 ↔ s = ∅ := @val_inj _ s ∅ theorem subset_empty {s : finset α} : s ⊆ ∅ ↔ s = ∅ := subset_zero.trans val_eq_zero theorem nonempty_of_ne_empty {s : finset α} (h : s ≠ ∅) : s.nonempty := exists_mem_of_ne_zero (mt val_eq_zero.1 h) theorem nonempty_iff_ne_empty {s : finset α} : s.nonempty ↔ s ≠ ∅ := ⟨nonempty.ne_empty, nonempty_of_ne_empty⟩ theorem eq_empty_or_nonempty (s : finset α) : s = ∅ ∨ s.nonempty := classical.by_cases or.inl (λ h, or.inr (nonempty_of_ne_empty h)) @[simp] lemma coe_empty : ↑(∅ : finset α) = (∅ : set α) := rfl /-! ### singleton -/ /-- `{a} : finset a` is the set `{a}` containing `a` and nothing else. This differs from `insert a ∅` in that it does not require a `decidable_eq` instance for `α`. -/ instance : has_singleton α (finset α) := ⟨λ a, ⟨{a}, nodup_singleton a⟩⟩ @[simp] theorem singleton_val (a : α) : ({a} : finset α).1 = a :: 0 := rfl @[simp] theorem mem_singleton {a b : α} : b ∈ ({a} : finset α) ↔ b = a := mem_singleton theorem not_mem_singleton {a b : α} : a ∉ ({b} : finset α) ↔ a ≠ b := not_congr mem_singleton theorem mem_singleton_self (a : α) : a ∈ ({a} : finset α) := or.inl rfl theorem singleton_inj {a b : α} : ({a} : finset α) = {b} ↔ a = b := ⟨λ h, mem_singleton.1 (h ▸ mem_singleton_self _), congr_arg _⟩ theorem singleton_nonempty (a : α) : ({a} : finset α).nonempty := ⟨a, mem_singleton_self a⟩ @[simp] theorem singleton_ne_empty (a : α) : ({a} : finset α) ≠ ∅ := (singleton_nonempty a).ne_empty @[simp] lemma coe_singleton (a : α) : ↑({a} : finset α) = ({a} : set α) := by { ext, simp } lemma eq_singleton_iff_unique_mem {s : finset α} {a : α} : s = {a} ↔ a ∈ s ∧ ∀ x ∈ s, x = a := begin split; intro t, rw t, refine ⟨finset.mem_singleton_self _, λ _, finset.mem_singleton.1⟩, ext, rw finset.mem_singleton, refine ⟨t.right _, λ r, r.symm ▸ t.left⟩ end lemma singleton_iff_unique_mem (s : finset α) : (∃ a, s = {a}) ↔ ∃! a, a ∈ s := by simp only [eq_singleton_iff_unique_mem, exists_unique] lemma singleton_subset_set_iff {s : set α} {a : α} : ↑({a} : finset α) ⊆ s ↔ a ∈ s := by rw [coe_singleton, set.singleton_subset_iff] @[simp] lemma singleton_subset_iff {s : finset α} {a : α} : {a} ⊆ s ↔ a ∈ s := singleton_subset_set_iff /-! ### insert -/ section decidable_eq variables [decidable_eq α] /-- `insert a s` is the set `{a} ∪ s` containing `a` and the elements of `s`. -/ instance : has_insert α (finset α) := ⟨λ a s, ⟨_, nodup_ndinsert a s.2⟩⟩ theorem insert_def (a : α) (s : finset α) : insert a s = ⟨_, nodup_ndinsert a s.2⟩ := rfl @[simp] theorem insert_val (a : α) (s : finset α) : (insert a s).1 = ndinsert a s.1 := rfl theorem insert_val' (a : α) (s : finset α) : (insert a s).1 = erase_dup (a :: s.1) := by rw [erase_dup_cons, erase_dup_eq_self]; refl theorem insert_val_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : (insert a s).1 = a :: s.1 := by rw [insert_val, ndinsert_of_not_mem h] @[simp] theorem mem_insert {a b : α} {s : finset α} : a ∈ insert b s ↔ a = b ∨ a ∈ s := mem_ndinsert theorem mem_insert_self (a : α) (s : finset α) : a ∈ insert a s := mem_ndinsert_self a s.1 theorem mem_insert_of_mem {a b : α} {s : finset α} (h : a ∈ s) : a ∈ insert b s := mem_ndinsert_of_mem h theorem mem_of_mem_insert_of_ne {a b : α} {s : finset α} (h : b ∈ insert a s) : b ≠ a → b ∈ s := (mem_insert.1 h).resolve_left @[simp] lemma coe_insert (a : α) (s : finset α) : ↑(insert a s) = (insert a ↑s : set α) := set.ext $ λ x, by simp only [mem_coe, mem_insert, set.mem_insert_iff] instance : is_lawful_singleton α (finset α) := ⟨λ a, by { ext, simp }⟩ @[simp] theorem insert_eq_of_mem {a : α} {s : finset α} (h : a ∈ s) : insert a s = s := eq_of_veq $ ndinsert_of_mem h @[simp] theorem insert_singleton_self_eq (a : α) : ({a, a} : finset α) = {a} := insert_eq_of_mem $ mem_singleton_self _ theorem insert.comm (a b : α) (s : finset α) : insert a (insert b s) = insert b (insert a s) := ext $ λ x, by simp only [finset.mem_insert, or.left_comm] @[simp] theorem insert_idem (a : α) (s : finset α) : insert a (insert a s) = insert a s := ext $ λ x, by simp only [finset.mem_insert, or.assoc.symm, or_self] @[simp] theorem insert_ne_empty (a : α) (s : finset α) : insert a s ≠ ∅ := ne_empty_of_mem (mem_insert_self a s) lemma ne_insert_of_not_mem (s t : finset α) {a : α} (h : a ∉ s) : s ≠ insert a t := by { contrapose! h, simp [h] } theorem insert_subset {a : α} {s t : finset α} : insert a s ⊆ t ↔ a ∈ t ∧ s ⊆ t := by simp only [subset_iff, mem_insert, forall_eq, or_imp_distrib, forall_and_distrib] theorem subset_insert (a : α) (s : finset α) : s ⊆ insert a s := λ b, mem_insert_of_mem theorem insert_subset_insert (a : α) {s t : finset α} (h : s ⊆ t) : insert a s ⊆ insert a t := insert_subset.2 ⟨mem_insert_self _ _, subset.trans h (subset_insert _ _)⟩ lemma ssubset_iff {s t : finset α} : s ⊂ t ↔ (∃a, a ∉ s ∧ insert a s ⊆ t) := iff.intro (assume H, have ∃a ∈ t, a ∉ s := set.exists_of_ssubset H, let ⟨a, hat, has⟩ := this in ⟨a, has, insert_subset.mpr ⟨hat, H.1⟩⟩) (assume ⟨a, hat, has⟩, let ⟨h₁, h₂⟩ := insert_subset.mp has in ⟨h₂, assume h, hat $ h h₁⟩) lemma ssubset_insert {s : finset α} {a : α} (h : a ∉ s) : s ⊂ insert a s := ssubset_iff.mpr ⟨a, h, subset.refl _⟩ @[recursor 6] protected theorem induction {α : Type*} {p : finset α → Prop} [decidable_eq α] (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : ∀ s, p s | ⟨s, nd⟩ := multiset.induction_on s (λ _, h₁) (λ a s IH nd, begin cases nodup_cons.1 nd with m nd', rw [← (eq_of_veq _ : insert a (finset.mk s _) = ⟨a::s, nd⟩)], { exact h₂ (by exact m) (IH nd') }, { rw [insert_val, ndinsert_of_not_mem m] } end) nd /-- To prove a proposition about an arbitrary `finset α`, it suffices to prove it for the empty `finset`, and to show that if it holds for some `finset α`, then it holds for the `finset` obtained by inserting a new element. -/ @[elab_as_eliminator] protected theorem induction_on {α : Type*} {p : finset α → Prop} [decidable_eq α] (s : finset α) (h₁ : p ∅) (h₂ : ∀ ⦃a : α⦄ {s : finset α}, a ∉ s → p s → p (insert a s)) : p s := finset.induction h₁ h₂ s /-! ### union -/ /-- `s ∪ t` is the set such that `a ∈ s ∪ t` iff `a ∈ s` or `a ∈ t`. -/ instance : has_union (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndunion s₁.1 s₂.2⟩⟩ theorem union_val_nd (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = ndunion s₁.1 s₂.1 := rfl @[simp] theorem union_val (s₁ s₂ : finset α) : (s₁ ∪ s₂).1 = s₁.1 ∪ s₂.1 := ndunion_eq_union s₁.2 @[simp] theorem mem_union {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∪ s₂ ↔ a ∈ s₁ ∨ a ∈ s₂ := mem_ndunion theorem mem_union_left {a : α} {s₁ : finset α} (s₂ : finset α) (h : a ∈ s₁) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inl h theorem mem_union_right {a : α} {s₂ : finset α} (s₁ : finset α) (h : a ∈ s₂) : a ∈ s₁ ∪ s₂ := mem_union.2 $ or.inr h theorem not_mem_union {a : α} {s₁ s₂ : finset α} : a ∉ s₁ ∪ s₂ ↔ a ∉ s₁ ∧ a ∉ s₂ := by rw [mem_union, not_or_distrib] @[simp] lemma coe_union (s₁ s₂ : finset α) : ↑(s₁ ∪ s₂) = (↑s₁ ∪ ↑s₂ : set α) := set.ext $ λ x, mem_union theorem union_subset {s₁ s₂ s₃ : finset α} (h₁ : s₁ ⊆ s₃) (h₂ : s₂ ⊆ s₃) : s₁ ∪ s₂ ⊆ s₃ := val_le_iff.1 (ndunion_le.2 ⟨h₁, val_le_iff.2 h₂⟩) theorem subset_union_left (s₁ s₂ : finset α) : s₁ ⊆ s₁ ∪ s₂ := λ x, mem_union_left _ theorem subset_union_right (s₁ s₂ : finset α) : s₂ ⊆ s₁ ∪ s₂ := λ x, mem_union_right _ theorem union_comm (s₁ s₂ : finset α) : s₁ ∪ s₂ = s₂ ∪ s₁ := ext $ λ x, by simp only [mem_union, or_comm] instance : is_commutative (finset α) (∪) := ⟨union_comm⟩ @[simp] theorem union_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = s₁ ∪ (s₂ ∪ s₃) := ext $ λ x, by simp only [mem_union, or_assoc] instance : is_associative (finset α) (∪) := ⟨union_assoc⟩ @[simp] theorem union_idempotent (s : finset α) : s ∪ s = s := ext $ λ _, mem_union.trans $ or_self _ instance : is_idempotent (finset α) (∪) := ⟨union_idempotent⟩ theorem union_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := ext $ λ _, by simp only [mem_union, or.left_comm] theorem union_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := ext $ λ x, by simp only [mem_union, or_assoc, or_comm (x ∈ s₂)] theorem union_self (s : finset α) : s ∪ s = s := union_idempotent s @[simp] theorem union_empty (s : finset α) : s ∪ ∅ = s := ext $ λ x, mem_union.trans $ or_false _ @[simp] theorem empty_union (s : finset α) : ∅ ∪ s = s := ext $ λ x, mem_union.trans $ false_or _ theorem insert_eq (a : α) (s : finset α) : insert a s = {a} ∪ s := rfl @[simp] theorem insert_union (a : α) (s t : finset α) : insert a s ∪ t = insert a (s ∪ t) := by simp only [insert_eq, union_assoc] @[simp] theorem union_insert (a : α) (s t : finset α) : s ∪ insert a t = insert a (s ∪ t) := by simp only [insert_eq, union_left_comm] theorem insert_union_distrib (a : α) (s t : finset α) : insert a (s ∪ t) = insert a s ∪ insert a t := by simp only [insert_union, union_insert, insert_idem] @[simp] lemma union_eq_left_iff_subset {s t : finset α} : s ∪ t = s ↔ t ⊆ s := begin split, { assume h, have : t ⊆ s ∪ t := subset_union_right _ _, rwa h at this }, { assume h, exact subset.antisymm (union_subset (subset.refl _) h) (subset_union_left _ _) } end @[simp] lemma left_eq_union_iff_subset {s t : finset α} : s = s ∪ t ↔ t ⊆ s := by rw [← union_eq_left_iff_subset, eq_comm] @[simp] lemma union_eq_right_iff_subset {s t : finset α} : t ∪ s = s ↔ t ⊆ s := by rw [union_comm, union_eq_left_iff_subset] @[simp] lemma right_eq_union_iff_subset {s t : finset α} : s = t ∪ s ↔ t ⊆ s := by rw [← union_eq_right_iff_subset, eq_comm] /-! ### inter -/ /-- `s ∩ t` is the set such that `a ∈ s ∩ t` iff `a ∈ s` and `a ∈ t`. -/ instance : has_inter (finset α) := ⟨λ s₁ s₂, ⟨_, nodup_ndinter s₂.1 s₁.2⟩⟩ theorem inter_val_nd (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = ndinter s₁.1 s₂.1 := rfl @[simp] theorem inter_val (s₁ s₂ : finset α) : (s₁ ∩ s₂).1 = s₁.1 ∩ s₂.1 := ndinter_eq_inter s₁.2 @[simp] theorem mem_inter {a : α} {s₁ s₂ : finset α} : a ∈ s₁ ∩ s₂ ↔ a ∈ s₁ ∧ a ∈ s₂ := mem_ndinter theorem mem_of_mem_inter_left {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₁ := (mem_inter.1 h).1 theorem mem_of_mem_inter_right {a : α} {s₁ s₂ : finset α} (h : a ∈ s₁ ∩ s₂) : a ∈ s₂ := (mem_inter.1 h).2 theorem mem_inter_of_mem {a : α} {s₁ s₂ : finset α} : a ∈ s₁ → a ∈ s₂ → a ∈ s₁ ∩ s₂ := and_imp.1 mem_inter.2 theorem inter_subset_left (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₁ := λ a, mem_of_mem_inter_left theorem inter_subset_right (s₁ s₂ : finset α) : s₁ ∩ s₂ ⊆ s₂ := λ a, mem_of_mem_inter_right theorem subset_inter {s₁ s₂ s₃ : finset α} : s₁ ⊆ s₂ → s₁ ⊆ s₃ → s₁ ⊆ s₂ ∩ s₃ := by simp only [subset_iff, mem_inter] {contextual:=tt}; intros; split; trivial @[simp] lemma coe_inter (s₁ s₂ : finset α) : ↑(s₁ ∩ s₂) = (↑s₁ ∩ ↑s₂ : set α) := set.ext $ λ _, mem_inter @[simp] theorem union_inter_cancel_left {s t : finset α} : (s ∪ t) ∩ s = s := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_left] @[simp] theorem union_inter_cancel_right {s t : finset α} : (s ∪ t) ∩ t = t := by rw [← coe_inj, coe_inter, coe_union, set.union_inter_cancel_right] theorem inter_comm (s₁ s₂ : finset α) : s₁ ∩ s₂ = s₂ ∩ s₁ := ext $ λ _, by simp only [mem_inter, and_comm] @[simp] theorem inter_assoc (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = s₁ ∩ (s₂ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and_assoc] theorem inter_left_comm (s₁ s₂ s₃ : finset α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := ext $ λ _, by simp only [mem_inter, and.left_comm] theorem inter_right_comm (s₁ s₂ s₃ : finset α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := ext $ λ _, by simp only [mem_inter, and.right_comm] @[simp] theorem inter_self (s : finset α) : s ∩ s = s := ext $ λ _, mem_inter.trans $ and_self _ @[simp] theorem inter_empty (s : finset α) : s ∩ ∅ = ∅ := ext $ λ _, mem_inter.trans $ and_false _ @[simp] theorem empty_inter (s : finset α) : ∅ ∩ s = ∅ := ext $ λ _, mem_inter.trans $ false_and _ @[simp] lemma inter_union_self (s t : finset α) : s ∩ (t ∪ s) = s := by rw [inter_comm, union_inter_cancel_right] @[simp] theorem insert_inter_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₂) : insert a s₁ ∩ s₂ = insert a (s₁ ∩ s₂) := ext $ λ x, have x = a ∨ x ∈ s₂ ↔ x ∈ s₂, from or_iff_right_of_imp $ by rintro rfl; exact h, by simp only [mem_inter, mem_insert, or_and_distrib_left, this] @[simp] theorem inter_insert_of_mem {s₁ s₂ : finset α} {a : α} (h : a ∈ s₁) : s₁ ∩ insert a s₂ = insert a (s₁ ∩ s₂) := by rw [inter_comm, insert_inter_of_mem h, inter_comm] @[simp] theorem insert_inter_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₂) : insert a s₁ ∩ s₂ = s₁ ∩ s₂ := ext $ λ x, have ¬ (x = a ∧ x ∈ s₂), by rintro ⟨rfl, H⟩; exact h H, by simp only [mem_inter, mem_insert, or_and_distrib_right, this, false_or] @[simp] theorem inter_insert_of_not_mem {s₁ s₂ : finset α} {a : α} (h : a ∉ s₁) : s₁ ∩ insert a s₂ = s₁ ∩ s₂ := by rw [inter_comm, insert_inter_of_not_mem h, inter_comm] @[simp] theorem singleton_inter_of_mem {a : α} {s : finset α} (H : a ∈ s) : {a} ∩ s = {a} := show insert a ∅ ∩ s = insert a ∅, by rw [insert_inter_of_mem H, empty_inter] @[simp] theorem singleton_inter_of_not_mem {a : α} {s : finset α} (H : a ∉ s) : {a} ∩ s = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_singleton]; rintro x ⟨rfl, h⟩; exact H h @[simp] theorem inter_singleton_of_mem {a : α} {s : finset α} (h : a ∈ s) : s ∩ {a} = {a} := by rw [inter_comm, singleton_inter_of_mem h] @[simp] theorem inter_singleton_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : s ∩ {a} = ∅ := by rw [inter_comm, singleton_inter_of_not_mem h] @[mono] lemma inter_subset_inter {x y s t : finset α} (h : x ⊆ y) (h' : s ⊆ t) : x ∩ s ⊆ y ∩ t := begin intros a a_in, rw finset.mem_inter at a_in ⊢, exact ⟨h a_in.1, h' a_in.2⟩ end lemma inter_subset_inter_right {x y s : finset α} (h : x ⊆ y) : x ∩ s ⊆ y ∩ s := finset.inter_subset_inter h (finset.subset.refl _) lemma inter_subset_inter_left {x y s : finset α} (h : x ⊆ y) : s ∩ x ⊆ s ∩ y := finset.inter_subset_inter (finset.subset.refl _) h /-! ### lattice laws -/ instance : lattice (finset α) := { sup := (∪), sup_le := assume a b c, union_subset, le_sup_left := subset_union_left, le_sup_right := subset_union_right, inf := (∩), le_inf := assume a b c, subset_inter, inf_le_left := inter_subset_left, inf_le_right := inter_subset_right, ..finset.partial_order } @[simp] theorem sup_eq_union (s t : finset α) : s ⊔ t = s ∪ t := rfl @[simp] theorem inf_eq_inter (s t : finset α) : s ⊓ t = s ∩ t := rfl instance : semilattice_inf_bot (finset α) := { bot := ∅, bot_le := empty_subset, ..finset.lattice } instance {α : Type*} [decidable_eq α] : semilattice_sup_bot (finset α) := { ..finset.semilattice_inf_bot, ..finset.lattice } instance : distrib_lattice (finset α) := { le_sup_inf := assume a b c, show (a ∪ b) ∩ (a ∪ c) ⊆ a ∪ b ∩ c, by simp only [subset_iff, mem_inter, mem_union, and_imp, or_imp_distrib] {contextual:=tt}; simp only [true_or, imp_true_iff, true_and, or_true], ..finset.lattice } theorem inter_distrib_left (s t u : finset α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := inf_sup_left theorem inter_distrib_right (s t u : finset α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := inf_sup_right theorem union_distrib_left (s t u : finset α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := sup_inf_left theorem union_distrib_right (s t u : finset α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := sup_inf_right lemma union_eq_empty_iff (A B : finset α) : A ∪ B = ∅ ↔ A = ∅ ∧ B = ∅ := sup_eq_bot_iff /-! ### erase -/ /-- `erase s a` is the set `s - {a}`, that is, the elements of `s` which are not equal to `a`. -/ def erase (s : finset α) (a : α) : finset α := ⟨_, nodup_erase_of_nodup a s.2⟩ @[simp] theorem erase_val (s : finset α) (a : α) : (erase s a).1 = s.1.erase a := rfl @[simp] theorem mem_erase {a b : α} {s : finset α} : a ∈ erase s b ↔ a ≠ b ∧ a ∈ s := mem_erase_iff_of_nodup s.2 theorem not_mem_erase (a : α) (s : finset α) : a ∉ erase s a := mem_erase_of_nodup s.2 @[simp] theorem erase_empty (a : α) : erase ∅ a = ∅ := rfl theorem ne_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ≠ a := by simp only [mem_erase]; exact and.left theorem mem_of_mem_erase {a b : α} {s : finset α} : b ∈ erase s a → b ∈ s := mem_of_mem_erase theorem mem_erase_of_ne_of_mem {a b : α} {s : finset α} : a ≠ b → a ∈ s → a ∈ erase s b := by simp only [mem_erase]; exact and.intro theorem erase_insert {a : α} {s : finset α} (h : a ∉ s) : erase (insert a s) a = s := ext $ assume x, by simp only [mem_erase, mem_insert, and_or_distrib_left, not_and_self, false_or]; apply and_iff_right_of_imp; rintro H rfl; exact h H theorem insert_erase {a : α} {s : finset α} (h : a ∈ s) : insert a (erase s a) = s := ext $ assume x, by simp only [mem_insert, mem_erase, or_and_distrib_left, dec_em, true_and]; apply or_iff_right_of_imp; rintro rfl; exact h theorem erase_subset_erase (a : α) {s t : finset α} (h : s ⊆ t) : erase s a ⊆ erase t a := val_le_iff.1 $ erase_le_erase _ $ val_le_iff.2 h theorem erase_subset (a : α) (s : finset α) : erase s a ⊆ s := erase_subset _ _ @[simp] lemma coe_erase (a : α) (s : finset α) : ↑(erase s a) = (↑s \ {a} : set α) := set.ext $ λ _, mem_erase.trans $ by rw [and_comm, set.mem_diff, set.mem_singleton_iff]; refl lemma erase_ssubset {a : α} {s : finset α} (h : a ∈ s) : s.erase a ⊂ s := calc s.erase a ⊂ insert a (s.erase a) : ssubset_insert $ not_mem_erase _ _ ... = _ : insert_erase h theorem erase_eq_of_not_mem {a : α} {s : finset α} (h : a ∉ s) : erase s a = s := eq_of_veq $ erase_of_not_mem h theorem subset_insert_iff {a : α} {s t : finset α} : s ⊆ insert a t ↔ erase s a ⊆ t := by simp only [subset_iff, or_iff_not_imp_left, mem_erase, mem_insert, and_imp]; exact forall_congr (λ x, forall_swap) theorem erase_insert_subset (a : α) (s : finset α) : erase (insert a s) a ⊆ s := subset_insert_iff.1 $ subset.refl _ theorem insert_erase_subset (a : α) (s : finset α) : s ⊆ insert a (erase s a) := subset_insert_iff.2 $ subset.refl _ /-! ### sdiff -/ /-- `s \ t` is the set consisting of the elements of `s` that are not in `t`. -/ instance : has_sdiff (finset α) := ⟨λs₁ s₂, ⟨s₁.1 - s₂.1, nodup_of_le (sub_le_self _ _) s₁.2⟩⟩ @[simp] theorem mem_sdiff {a : α} {s₁ s₂ : finset α} : a ∈ s₁ \ s₂ ↔ a ∈ s₁ ∧ a ∉ s₂ := mem_sub_of_nodup s₁.2 lemma not_mem_sdiff_of_mem_right {a : α} {s t : finset α} (h : a ∈ t) : a ∉ s \ t := by simp only [mem_sdiff, h, not_true, not_false_iff, and_false] theorem sdiff_union_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : (s₂ \ s₁) ∪ s₁ = s₂ := ext $ λ a, by simpa only [mem_sdiff, mem_union, or_comm, or_and_distrib_left, dec_em, and_true] using or_iff_right_of_imp (@h a) theorem union_sdiff_of_subset {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁ ∪ (s₂ \ s₁) = s₂ := (union_comm _ _).trans (sdiff_union_of_subset h) theorem inter_sdiff (s t u : finset α) : s ∩ (t \ u) = s ∩ t \ u := by { ext x, simp [and_assoc] } @[simp] theorem inter_sdiff_self (s₁ s₂ : finset α) : s₁ ∩ (s₂ \ s₁) = ∅ := eq_empty_of_forall_not_mem $ by simp only [mem_inter, mem_sdiff]; rintro x ⟨h, _, hn⟩; exact hn h @[simp] theorem sdiff_inter_self (s₁ s₂ : finset α) : (s₂ \ s₁) ∩ s₁ = ∅ := (inter_comm _ _).trans (inter_sdiff_self _ _) @[simp] theorem sdiff_self (s₁ : finset α) : s₁ \ s₁ = ∅ := by ext; simp theorem sdiff_inter_distrib_right (s₁ s₂ s₃ : finset α) : s₁ \ (s₂ ∩ s₃) = (s₁ \ s₂) ∪ (s₁ \ s₃) := by ext; simp only [and_or_distrib_left, mem_union, classical.not_and_distrib, mem_sdiff, mem_inter] @[simp] theorem sdiff_inter_self_left (s₁ s₂ : finset α) : s₁ \ (s₁ ∩ s₂) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, empty_union] @[simp] theorem sdiff_inter_self_right (s₁ s₂ : finset α) : s₁ \ (s₂ ∩ s₁) = s₁ \ s₂ := by simp only [sdiff_inter_distrib_right, sdiff_self, union_empty] @[simp] theorem sdiff_empty {s₁ : finset α} : s₁ \ ∅ = s₁ := ext (by simp) @[mono] theorem sdiff_subset_sdiff {s₁ s₂ t₁ t₂ : finset α} (h₁ : t₁ ⊆ t₂) (h₂ : s₂ ⊆ s₁) : t₁ \ s₁ ⊆ t₂ \ s₂ := by simpa only [subset_iff, mem_sdiff, and_imp] using λ a m₁ m₂, and.intro (h₁ m₁) (mt (@h₂ _) m₂) theorem sdiff_subset_self {s₁ s₂ : finset α} : s₁ \ s₂ ⊆ s₁ := suffices s₁ \ s₂ ⊆ s₁ \ ∅, by simpa [sdiff_empty] using this, sdiff_subset_sdiff (subset.refl _) (empty_subset _) @[simp] lemma coe_sdiff (s₁ s₂ : finset α) : ↑(s₁ \ s₂) = (↑s₁ \ ↑s₂ : set α) := set.ext $ λ _, mem_sdiff @[simp] theorem union_sdiff_self_eq_union {s t : finset α} : s ∪ (t \ s) = s ∪ t := ext $ λ a, by simp only [mem_union, mem_sdiff, or_iff_not_imp_left, imp_and_distrib, and_iff_left id] @[simp] theorem sdiff_union_self_eq_union {s t : finset α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_sdiff_self_eq_union, union_comm] lemma union_sdiff_symm {s t : finset α} : s ∪ (t \ s) = t ∪ (s \ t) := by rw [union_sdiff_self_eq_union, union_sdiff_self_eq_union, union_comm] lemma sdiff_union_inter (s t : finset α) : (s \ t) ∪ (s ∩ t) = s := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } @[simp] lemma sdiff_idem (s t : finset α) : s \ t \ t = s \ t := by { simp only [ext_iff, mem_sdiff], tauto } lemma sdiff_eq_empty_iff_subset {s t : finset α} : s \ t = ∅ ↔ s ⊆ t := by { rw [subset_iff, ext_iff], simp } @[simp] lemma empty_sdiff (s : finset α) : ∅ \ s = ∅ := by { rw sdiff_eq_empty_iff_subset, exact empty_subset _ } lemma insert_sdiff_of_not_mem (s : finset α) {t : finset α} {x : α} (h : x ∉ t) : (insert x s) \ t = insert x (s \ t) := begin rw [← coe_inj, coe_insert, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_not_mem ↑s h end lemma insert_sdiff_of_mem (s : finset α) {t : finset α} {x : α} (h : x ∈ t) : (insert x s) \ t = s \ t := begin rw [← coe_inj, coe_sdiff, coe_sdiff, coe_insert], exact set.insert_diff_of_mem ↑s h end @[simp] lemma sdiff_subset (s t : finset α) : s \ t ⊆ s := by simp [subset_iff, mem_sdiff] {contextual := tt} lemma union_sdiff_distrib (s₁ s₂ t : finset α) : (s₁ ∪ s₂) \ t = s₁ \ t ∪ s₂ \ t := by { simp only [ext_iff, mem_sdiff, mem_union], tauto } lemma sdiff_union_distrib (s t₁ t₂ : finset α) : s \ (t₁ ∪ t₂) = (s \ t₁) ∩ (s \ t₂) := by { simp only [ext_iff, mem_union, mem_sdiff, mem_inter], tauto } lemma union_sdiff_self (s t : finset α) : (s ∪ t) \ t = s \ t := by rw [union_sdiff_distrib, sdiff_self, union_empty] lemma sdiff_singleton_eq_erase (a : α) (s : finset α) : s \ singleton a = erase s a := by { ext, rw [mem_erase, mem_sdiff, mem_singleton], tauto } lemma sdiff_sdiff_self_left (s t : finset α) : s \ (s \ t) = s ∩ t := by { simp only [ext_iff, mem_sdiff, mem_inter], tauto } lemma inter_eq_inter_of_sdiff_eq_sdiff {s t₁ t₂ : finset α} : s \ t₁ = s \ t₂ → s ∩ t₁ = s ∩ t₂ := by { simp only [ext_iff, mem_sdiff, mem_inter], intros b c, replace b := b c, split; tauto } end decidable_eq /-! ### attach -/ /-- `attach s` takes the elements of `s` and forms a new set of elements of the subtype `{x // x ∈ s}`. -/ def attach (s : finset α) : finset {x // x ∈ s} := ⟨attach s.1, nodup_attach.2 s.2⟩ @[simp] theorem attach_val (s : finset α) : s.attach.1 = s.1.attach := rfl @[simp] theorem mem_attach (s : finset α) : ∀ x, x ∈ s.attach := mem_attach _ @[simp] theorem attach_empty : attach (∅ : finset α) = ∅ := rfl /-! ### piecewise -/ section piecewise /-- `s.piecewise f g` is the function equal to `f` on the finset `s`, and to `g` on its complement. -/ def piecewise {α : Type*} {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) [∀j, decidable (j ∈ s)] : Πi, δ i := λi, if i ∈ s then f i else g i variables {δ : α → Sort*} (s : finset α) (f g : Πi, δ i) @[simp] lemma piecewise_insert_self [decidable_eq α] {j : α} [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g j = f j := by simp [piecewise] @[simp] lemma piecewise_empty [∀i : α, decidable (i ∈ (∅ : finset α))] : piecewise ∅ f g = g := by { ext i, simp [piecewise] } variable [∀j, decidable (j ∈ s)] @[norm_cast] lemma piecewise_coe [∀j, decidable (j ∈ (↑s : set α))] : (↑s : set α).piecewise f g = s.piecewise f g := by { ext, congr } @[simp, priority 980] lemma piecewise_eq_of_mem {i : α} (hi : i ∈ s) : s.piecewise f g i = f i := by simp [piecewise, hi] @[simp, priority 980] lemma piecewise_eq_of_not_mem {i : α} (hi : i ∉ s) : s.piecewise f g i = g i := by simp [piecewise, hi] @[simp, priority 990] lemma piecewise_insert_of_ne [decidable_eq α] {i j : α} [∀i, decidable (i ∈ insert j s)] (h : i ≠ j) : (insert j s).piecewise f g i = s.piecewise f g i := by simp [piecewise, h] lemma piecewise_insert [decidable_eq α] (j : α) [∀i, decidable (i ∈ insert j s)] : (insert j s).piecewise f g = function.update (s.piecewise f g) j (f j) := begin classical, rw [← piecewise_coe, ← piecewise_coe, ← set.piecewise_insert, ← coe_insert j s], congr end lemma update_eq_piecewise {β : Type*} [decidable_eq α] (f : α → β) (i : α) (v : β) : function.update f i v = piecewise (singleton i) (λj, v) f := begin ext j, by_cases h : j = i, { rw [h], simp }, { simp [h] } end end piecewise section decidable_pi_exists variables {s : finset α} instance decidable_dforall_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∀a (h : a ∈ s), p a h) := multiset.decidable_dforall_multiset /-- decidable equality for functions whose domain is bounded by finsets -/ instance decidable_eq_pi_finset {β : α → Type*} [h : ∀a, decidable_eq (β a)] : decidable_eq (Πa∈s, β a) := multiset.decidable_eq_pi_multiset instance decidable_dexists_finset {p : Πa∈s, Prop} [hp : ∀a (h : a ∈ s), decidable (p a h)] : decidable (∃a (h : a ∈ s), p a h) := multiset.decidable_dexists_multiset end decidable_pi_exists /-! ### filter -/ section filter variables {p q : α → Prop} [decidable_pred p] [decidable_pred q] /-- `filter p s` is the set of elements of `s` that satisfy `p`. -/ def filter (p : α → Prop) [decidable_pred p] (s : finset α) : finset α := ⟨_, nodup_filter p s.2⟩ @[simp] theorem filter_val (s : finset α) : (filter p s).1 = s.1.filter p := rfl @[simp] theorem mem_filter {s : finset α} {a : α} : a ∈ s.filter p ↔ a ∈ s ∧ p a := mem_filter @[simp] theorem filter_subset (s : finset α) : s.filter p ⊆ s := filter_subset _ theorem filter_ssubset {s : finset α} : s.filter p ⊂ s ↔ ∃ x ∈ s, ¬ p x := ⟨λ h, let ⟨x, hs, hp⟩ := set.exists_of_ssubset h in ⟨x, hs, mt (λ hp, mem_filter.2 ⟨hs, hp⟩) hp⟩, λ ⟨x, hs, hp⟩, ⟨s.filter_subset, λ h, hp (mem_filter.1 (h hs)).2⟩⟩ theorem filter_filter (s : finset α) : (s.filter p).filter q = s.filter (λa, p a ∧ q a) := ext $ assume a, by simp only [mem_filter, and_comm, and.left_comm] @[simp] lemma filter_true {s : finset α} [h : decidable_pred (λ _, true)] : @finset.filter α (λ _, true) h s = s := by ext; simp @[simp] theorem filter_false {h} (s : finset α) : @filter α (λa, false) h s = ∅ := ext $ assume a, by simp only [mem_filter, and_false]; refl lemma filter_congr {s : finset α} (H : ∀ x ∈ s, p x ↔ q x) : filter p s = filter q s := eq_of_veq $ filter_congr H lemma filter_empty : filter p ∅ = ∅ := subset_empty.1 $ filter_subset _ lemma filter_subset_filter {s t : finset α} (h : s ⊆ t) : s.filter p ⊆ t.filter p := assume a ha, mem_filter.2 ⟨h (mem_filter.1 ha).1, (mem_filter.1 ha).2⟩ @[simp] lemma coe_filter (s : finset α) : ↑(s.filter p) = ({x ∈ ↑s | p x} : set α) := set.ext $ λ _, mem_filter theorem filter_singleton (a : α) : filter p (singleton a) = if p a then singleton a else ∅ := by { classical, ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } variable [decidable_eq α] theorem filter_union (s₁ s₂ : finset α) : (s₁ ∪ s₂).filter p = s₁.filter p ∪ s₂.filter p := ext $ λ _, by simp only [mem_filter, mem_union, or_and_distrib_right] theorem filter_union_right (p q : α → Prop) [decidable_pred p] [decidable_pred q] (s : finset α) : s.filter p ∪ s.filter q = s.filter (λx, p x ∨ q x) := ext $ λ x, by simp only [mem_filter, mem_union, and_or_distrib_left.symm] lemma filter_mem_eq_inter {s t : finset α} : s.filter (λ i, i ∈ t) = s ∩ t := ext $ λ i, by rw [mem_filter, mem_inter] theorem filter_inter {s t : finset α} : filter p s ∩ t = filter p (s ∩ t) := by { ext, simp only [mem_inter, mem_filter, and.right_comm] } theorem inter_filter {s t : finset α} : s ∩ filter p t = filter p (s ∩ t) := by rw [inter_comm, filter_inter, inter_comm] theorem filter_insert (a : α) (s : finset α) : filter p (insert a s) = if p a then insert a (filter p s) else (filter p s) := by { ext x, simp, split_ifs with h; by_cases h' : x = a; simp [h, h'] } theorem filter_or [decidable_pred (λ a, p a ∨ q a)] (s : finset α) : s.filter (λ a, p a ∨ q a) = s.filter p ∪ s.filter q := ext $ λ _, by simp only [mem_filter, mem_union, and_or_distrib_left] theorem filter_and [decidable_pred (λ a, p a ∧ q a)] (s : finset α) : s.filter (λ a, p a ∧ q a) = s.filter p ∩ s.filter q := ext $ λ _, by simp only [mem_filter, mem_inter, and_comm, and.left_comm, and_self] theorem filter_not [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter (λ a, ¬ p a) = s \ s.filter p := ext $ by simpa only [mem_filter, mem_sdiff, and_comm, not_and] using λ a, and_congr_right $ λ h : a ∈ s, (imp_iff_right h).symm.trans imp_not_comm theorem sdiff_eq_filter (s₁ s₂ : finset α) : s₁ \ s₂ = filter (∉ s₂) s₁ := ext $ λ _, by simp only [mem_sdiff, mem_filter] theorem sdiff_eq_self (s₁ s₂ : finset α) : s₁ \ s₂ = s₁ ↔ s₁ ∩ s₂ ⊆ ∅ := by { simp [subset.antisymm_iff,sdiff_subset_self], split; intro h, { transitivity' ((s₁ \ s₂) ∩ s₂), mono, simp }, { calc s₁ \ s₂ ⊇ s₁ \ (s₁ ∩ s₂) : by simp [(⊇)] ... ⊇ s₁ \ ∅ : by mono using [(⊇)] ... ⊇ s₁ : by simp [(⊇)] } } theorem filter_union_filter_neg_eq [decidable_pred (λ a, ¬ p a)] (s : finset α) : s.filter p ∪ s.filter (λa, ¬ p a) = s := by simp only [filter_not, union_sdiff_of_subset (filter_subset s)] theorem filter_inter_filter_neg_eq (s : finset α) : s.filter p ∩ s.filter (λa, ¬ p a) = ∅ := by simp only [filter_not, inter_sdiff_self] lemma subset_union_elim {s : finset α} {t₁ t₂ : set α} (h : ↑s ⊆ t₁ ∪ t₂) : ∃s₁ s₂ : finset α, s₁ ∪ s₂ = s ∧ ↑s₁ ⊆ t₁ ∧ ↑s₂ ⊆ t₂ \ t₁ := begin classical, refine ⟨s.filter (∈ t₁), s.filter (∉ t₁), _, _ , _⟩, { simp [filter_union_right, classical.or_not] }, { intro x, simp }, { intro x, simp, intros hx hx₂, refine ⟨or.resolve_left (h hx) hx₂, hx₂⟩ } end /- We can simplify an application of filter where the decidability is inferred in "the wrong way" -/ @[simp] lemma filter_congr_decidable {α} (s : finset α) (p : α → Prop) (h : decidable_pred p) [decidable_pred p] : @filter α p h s = s.filter p := by congr section classical open_locale classical /-- The following instance allows us to write `{ x ∈ s | p x }` for `finset.filter s p`. Since the former notation requires us to define this for all propositions `p`, and `finset.filter` only works for decidable propositions, the notation `{ x ∈ s | p x }` is only compatible with classical logic because it uses `classical.prop_decidable`. We don't want to redo all lemmas of `finset.filter` for `has_sep.sep`, so we make sure that `simp` unfolds the notation `{ x ∈ s | p x }` to `finset.filter s p`. If `p` happens to be decidable, the simp-lemma `filter_congr_decidable` will make sure that `finset.filter` uses the right instance for decidability. -/ noncomputable instance {α : Type*} : has_sep α (finset α) := ⟨λ p x, x.filter p⟩ @[simp] lemma sep_def {α : Type*} (s : finset α) (p : α → Prop) : {x ∈ s | p x} = s.filter p := rfl end classical /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq'` with the equality the other way. -/ -- This is not a good simp lemma, as it would prevent `finset.mem_filter` from firing -- on, e.g. `x ∈ s.filter(eq b)`. lemma filter_eq [decidable_eq β] (s : finset β) (b : β) : s.filter(eq b) = ite (b ∈ s) {b} ∅ := begin split_ifs, { ext, simp only [mem_filter, mem_singleton], exact ⟨λ h, h.2.symm, by { rintro ⟨h⟩, exact ⟨h, rfl⟩, }⟩ }, { ext, simp only [mem_filter, not_and, iff_false, not_mem_empty], rintros m ⟨e⟩, exact h m, } end /-- After filtering out everything that does not equal a given value, at most that value remains. This is equivalent to `filter_eq` with the equality the other way. -/ lemma filter_eq' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a = b) = ite (b ∈ s) {b} ∅ := trans (filter_congr (λ _ _, ⟨eq.symm, eq.symm⟩)) (filter_eq s b) lemma filter_ne [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, b ≠ a) = s.erase b := by { ext, simp only [mem_filter, mem_erase, ne.def], cc, } lemma filter_ne' [decidable_eq β] (s : finset β) (b : β) : s.filter (λ a, a ≠ b) = s.erase b := trans (filter_congr (λ _ _, ⟨ne.symm, ne.symm⟩)) (filter_ne s b) end filter /-! ### range -/ section range variables {n m l : ℕ} /-- `range n` is the set of natural numbers less than `n`. -/ def range (n : ℕ) : finset ℕ := ⟨_, nodup_range n⟩ @[simp] theorem range_val (n : ℕ) : (range n).1 = multiset.range n := rfl @[simp] theorem mem_range : m ∈ range n ↔ m < n := mem_range @[simp] theorem range_zero : range 0 = ∅ := rfl @[simp] theorem range_one : range 1 = {0} := rfl theorem range_succ : range (succ n) = insert n (range n) := eq_of_veq $ (range_succ n).trans $ (ndinsert_of_not_mem not_mem_range_self).symm theorem range_add_one : range (n + 1) = insert n (range n) := range_succ @[simp] theorem not_mem_range_self : n ∉ range n := not_mem_range_self @[simp] theorem self_mem_range_succ (n : ℕ) : n ∈ range (n + 1) := multiset.self_mem_range_succ n @[simp] theorem range_subset {n m} : range n ⊆ range m ↔ n ≤ m := range_subset theorem range_mono : monotone range := λ _ _, range_subset.2 end range /- useful rules for calculations with quantifiers -/ theorem exists_mem_empty_iff (p : α → Prop) : (∃ x, x ∈ (∅ : finset α) ∧ p x) ↔ false := by simp only [not_mem_empty, false_and, exists_false] theorem exists_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∃ x, x ∈ insert a s ∧ p x) ↔ p a ∨ (∃ x, x ∈ s ∧ p x) := by simp only [mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] theorem forall_mem_empty_iff (p : α → Prop) : (∀ x, x ∈ (∅ : finset α) → p x) ↔ true := iff_true_intro $ λ _, false.elim theorem forall_mem_insert [d : decidable_eq α] (a : α) (s : finset α) (p : α → Prop) : (∀ x, x ∈ insert a s → p x) ↔ p a ∧ (∀ x, x ∈ s → p x) := by simp only [mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] end finset /-- Equivalence between the set of natural numbers which are `≥ k` and `ℕ`, given by `n → n - k`. -/ def not_mem_range_equiv (k : ℕ) : {n // n ∉ range k} ≃ ℕ := { to_fun := λ i, i.1 - k, inv_fun := λ j, ⟨j + k, by simp⟩, left_inv := begin assume j, rw subtype.ext, apply nat.sub_add_cancel, simpa using j.2 end, right_inv := λ j, nat.add_sub_cancel _ _ } @[simp] lemma coe_not_mem_range_equiv (k : ℕ) : (not_mem_range_equiv k : {n // n ∉ range k} → ℕ) = (λ i, i - k) := rfl @[simp] lemma coe_not_mem_range_equiv_symm (k : ℕ) : ((not_mem_range_equiv k).symm : ℕ → {n // n ∉ range k}) = λ j, ⟨j + k, by simp⟩ := rfl namespace option /-- Construct an empty or singleton finset from an `option` -/ def to_finset (o : option α) : finset α := match o with | none := ∅ | some a := {a} end @[simp] theorem to_finset_none : none.to_finset = (∅ : finset α) := rfl @[simp] theorem to_finset_some {a : α} : (some a).to_finset = {a} := rfl @[simp] theorem mem_to_finset {a : α} {o : option α} : a ∈ o.to_finset ↔ a ∈ o := by cases o; simp only [to_finset, finset.mem_singleton, option.mem_def, eq_comm]; refl end option /-! ### erase_dup on list and multiset -/ namespace multiset variable [decidable_eq α] /-- `to_finset s` removes duplicates from the multiset `s` to produce a finset. -/ def to_finset (s : multiset α) : finset α := ⟨_, nodup_erase_dup s⟩ @[simp] theorem to_finset_val (s : multiset α) : s.to_finset.1 = s.erase_dup := rfl theorem to_finset_eq {s : multiset α} (n : nodup s) : finset.mk s n = s.to_finset := finset.val_inj.1 (erase_dup_eq_self.2 n).symm @[simp] theorem mem_to_finset {a : α} {s : multiset α} : a ∈ s.to_finset ↔ a ∈ s := mem_erase_dup @[simp] lemma to_finset_zero : to_finset (0 : multiset α) = ∅ := rfl @[simp] lemma to_finset_cons (a : α) (s : multiset α) : to_finset (a :: s) = insert a (to_finset s) := finset.eq_of_veq erase_dup_cons @[simp] lemma to_finset_add (s t : multiset α) : to_finset (s + t) = to_finset s ∪ to_finset t := finset.ext $ by simp @[simp] lemma to_finset_nsmul (s : multiset α) : ∀(n : ℕ) (hn : n ≠ 0), (n •ℕ s).to_finset = s.to_finset | 0 h := by contradiction | (n+1) h := begin by_cases n = 0, { rw [h, zero_add, one_nsmul] }, { rw [add_nsmul, to_finset_add, one_nsmul, to_finset_nsmul n h, finset.union_idempotent] } end @[simp] lemma to_finset_inter (s t : multiset α) : to_finset (s ∩ t) = to_finset s ∩ to_finset t := finset.ext $ by simp theorem to_finset_eq_empty {m : multiset α} : m.to_finset = ∅ ↔ m = 0 := finset.val_inj.symm.trans multiset.erase_dup_eq_zero end multiset namespace list variable [decidable_eq α] /-- `to_finset l` removes duplicates from the list `l` to produce a finset. -/ def to_finset (l : list α) : finset α := multiset.to_finset l @[simp] theorem to_finset_val (l : list α) : l.to_finset.1 = (l.erase_dup : multiset α) := rfl theorem to_finset_eq {l : list α} (n : nodup l) : @finset.mk α l n = l.to_finset := multiset.to_finset_eq n @[simp] theorem mem_to_finset {a : α} {l : list α} : a ∈ l.to_finset ↔ a ∈ l := mem_erase_dup @[simp] theorem to_finset_nil : to_finset (@nil α) = ∅ := rfl @[simp] theorem to_finset_cons {a : α} {l : list α} : to_finset (a :: l) = insert a (to_finset l) := finset.eq_of_veq $ by by_cases h : a ∈ l; simp [finset.insert_val', multiset.erase_dup_cons, h] end list namespace finset /-! ### map -/ section map open function /-- When `f` is an embedding of `α` in `β` and `s` is a finset in `α`, then `s.map f` is the image finset in `β`. The embedding condition guarantees that there are no duplicates in the image. -/ def map (f : α ↪ β) (s : finset α) : finset β := ⟨s.1.map f, nodup_map f.2 s.2⟩ @[simp] theorem map_val (f : α ↪ β) (s : finset α) : (map f s).1 = s.1.map f := rfl @[simp] theorem map_empty (f : α ↪ β) : (∅ : finset α).map f = ∅ := rfl variables {f : α ↪ β} {s : finset α} @[simp] theorem mem_map {b : β} : b ∈ s.map f ↔ ∃ a ∈ s, f a = b := mem_map.trans $ by simp only [exists_prop]; refl theorem mem_map' (f : α ↪ β) {a} {s : finset α} : f a ∈ s.map f ↔ a ∈ s := mem_map_of_inj f.2 theorem mem_map_of_mem (f : α ↪ β) {a} {s : finset α} : a ∈ s → f a ∈ s.map f := (mem_map' _).2 @[simp] theorem coe_map (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) = f '' ↑s := set.ext $ λ x, mem_map.trans set.mem_image_iff_bex.symm theorem coe_map_subset_range (f : α ↪ β) (s : finset α) : (↑(s.map f) : set β) ⊆ set.range f := calc ↑(s.map f) = f '' ↑s : coe_map f s ... ⊆ set.range f : set.image_subset_range f ↑s theorem map_to_finset [decidable_eq α] [decidable_eq β] {s : multiset α} : s.to_finset.map f = (s.map f).to_finset := ext $ λ _, by simp only [mem_map, multiset.mem_map, exists_prop, multiset.mem_to_finset] @[simp] theorem map_refl : s.map (embedding.refl _) = s := ext $ λ _, by simpa only [mem_map, exists_prop] using exists_eq_right theorem map_map {g : β ↪ γ} : (s.map f).map g = s.map (f.trans g) := eq_of_veq $ by simp only [map_val, multiset.map_map]; refl theorem map_subset_map {s₁ s₂ : finset α} : s₁.map f ⊆ s₂.map f ↔ s₁ ⊆ s₂ := ⟨λ h x xs, (mem_map' _).1 $ h $ (mem_map' f).2 xs, λ h, by simp [subset_def, map_subset_map h]⟩ theorem map_inj {s₁ s₂ : finset α} : s₁.map f = s₂.map f ↔ s₁ = s₂ := by simp only [subset.antisymm_iff, map_subset_map] /-- Associate to an embedding `f` from `α` to `β` the embedding that maps a finset to its image under `f`. -/ def map_embedding (f : α ↪ β) : finset α ↪ finset β := ⟨map f, λ s₁ s₂, map_inj.1⟩ @[simp] theorem map_embedding_apply : map_embedding f s = map f s := rfl theorem map_filter {p : β → Prop} [decidable_pred p] : (s.map f).filter p = (s.filter (p ∘ f)).map f := ext $ λ b, by simp only [mem_filter, mem_map, exists_prop, and_assoc]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, h1, h2, rfl⟩, by rintro ⟨x, h1, h2, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem map_union [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).map f = s₁.map f ∪ s₂.map f := ext $ λ _, by simp only [mem_map, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem map_inter [decidable_eq α] [decidable_eq β] {f : α ↪ β} (s₁ s₂ : finset α) : (s₁ ∩ s₂).map f = s₁.map f ∩ s₂.map f := ext $ λ b, by simp only [mem_map, mem_inter, exists_prop]; exact ⟨by rintro ⟨a, ⟨m₁, m₂⟩, rfl⟩; exact ⟨⟨a, m₁, rfl⟩, ⟨a, m₂, rfl⟩⟩, by rintro ⟨⟨a, m₁, e⟩, ⟨a', m₂, rfl⟩⟩; cases f.2 e; exact ⟨_, ⟨m₁, m₂⟩, rfl⟩⟩ @[simp] theorem map_singleton (f : α ↪ β) (a : α) : map f {a} = {f a} := ext $ λ _, by simp only [mem_map, mem_singleton, exists_prop, exists_eq_left]; exact eq_comm @[simp] theorem map_insert [decidable_eq α] [decidable_eq β] (f : α ↪ β) (a : α) (s : finset α) : (insert a s).map f = insert (f a) (s.map f) := by simp only [insert_eq, map_union, map_singleton] @[simp] theorem map_eq_empty : s.map f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_map_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_map_val {s : finset α} : s.attach.map (embedding.subtype _) = s := eq_of_veq $ by rw [map_val, attach_val]; exact attach_map_val _ end map lemma range_add_one' (n : ℕ) : range (n + 1) = insert 0 ((range n).map ⟨λi, i + 1, assume i j, nat.succ_inj⟩) := by ext (⟨⟩ | ⟨n⟩); simp [nat.succ_eq_add_one, nat.zero_lt_succ n] /-! ### image -/ section image variables [decidable_eq β] /-- `image f s` is the forward image of `s` under `f`. -/ def image (f : α → β) (s : finset α) : finset β := (s.1.map f).to_finset @[simp] theorem image_val (f : α → β) (s : finset α) : (image f s).1 = (s.1.map f).erase_dup := rfl @[simp] theorem image_empty (f : α → β) : (∅ : finset α).image f = ∅ := rfl variables {f : α → β} {s : finset α} @[simp] theorem mem_image {b : β} : b ∈ s.image f ↔ ∃ a ∈ s, f a = b := by simp only [mem_def, image_val, mem_erase_dup, multiset.mem_map, exists_prop] theorem mem_image_of_mem (f : α → β) {a} {s : finset α} (h : a ∈ s) : f a ∈ s.image f := mem_image.2 ⟨_, h, rfl⟩ @[simp] lemma coe_image {f : α → β} : ↑(s.image f) = f '' ↑s := set.ext $ λ _, mem_image.trans set.mem_image_iff_bex.symm lemma nonempty.image (h : s.nonempty) (f : α → β) : (s.image f).nonempty := let ⟨a, ha⟩ := h in ⟨f a, mem_image_of_mem f ha⟩ theorem image_to_finset [decidable_eq α] {s : multiset α} : s.to_finset.image f = (s.map f).to_finset := ext $ λ _, by simp only [mem_image, multiset.mem_to_finset, exists_prop, multiset.mem_map] theorem image_val_of_inj_on (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : (image f s).1 = s.1.map f := multiset.erase_dup_eq_self.2 (nodup_map_on H s.2) theorem image_id [decidable_eq α] : s.image id = s := ext $ λ _, by simp only [mem_image, exists_prop, id, exists_eq_right] theorem image_image [decidable_eq γ] {g : β → γ} : (s.image f).image g = s.image (g ∘ f) := eq_of_veq $ by simp only [image_val, erase_dup_map_erase_dup_eq, multiset.map_map] theorem image_subset_image {s₁ s₂ : finset α} (h : s₁ ⊆ s₂) : s₁.image f ⊆ s₂.image f := by simp only [subset_def, image_val, subset_erase_dup', erase_dup_subset', multiset.map_subset_map h] theorem image_mono (f : α → β) : monotone (finset.image f) := λ _ _, image_subset_image theorem coe_image_subset_range : ↑(s.image f) ⊆ set.range f := calc ↑(s.image f) = f '' ↑s : coe_image ... ⊆ set.range f : set.image_subset_range f ↑s theorem image_filter {p : β → Prop} [decidable_pred p] : (s.image f).filter p = (s.filter (p ∘ f)).image f := ext $ λ b, by simp only [mem_filter, mem_image, exists_prop]; exact ⟨by rintro ⟨⟨x, h1, rfl⟩, h2⟩; exact ⟨x, ⟨h1, h2⟩, rfl⟩, by rintro ⟨x, ⟨h1, h2⟩, rfl⟩; exact ⟨⟨x, h1, rfl⟩, h2⟩⟩ theorem image_union [decidable_eq α] {f : α → β} (s₁ s₂ : finset α) : (s₁ ∪ s₂).image f = s₁.image f ∪ s₂.image f := ext $ λ _, by simp only [mem_image, mem_union, exists_prop, or_and_distrib_right, exists_or_distrib] theorem image_inter [decidable_eq α] (s₁ s₂ : finset α) (hf : ∀x y, f x = f y → x = y) : (s₁ ∩ s₂).image f = s₁.image f ∩ s₂.image f := ext $ by simp only [mem_image, exists_prop, mem_inter]; exact λ b, ⟨λ ⟨a, ⟨m₁, m₂⟩, e⟩, ⟨⟨a, m₁, e⟩, ⟨a, m₂, e⟩⟩, λ ⟨⟨a, m₁, e₁⟩, ⟨a', m₂, e₂⟩⟩, ⟨a, ⟨m₁, hf _ _ (e₂.trans e₁.symm) ▸ m₂⟩, e₁⟩⟩. @[simp] theorem image_singleton (f : α → β) (a : α) : image f {a} = {f a} := ext $ λ x, by simpa only [mem_image, exists_prop, mem_singleton, exists_eq_left] using eq_comm @[simp] theorem image_insert [decidable_eq α] (f : α → β) (a : α) (s : finset α) : (insert a s).image f = insert (f a) (s.image f) := by simp only [insert_eq, image_singleton, image_union] @[simp] theorem image_eq_empty : s.image f = ∅ ↔ s = ∅ := ⟨λ h, eq_empty_of_forall_not_mem $ λ a m, ne_empty_of_mem (mem_image_of_mem _ m) h, λ e, e.symm ▸ rfl⟩ lemma attach_image_val [decidable_eq α] {s : finset α} : s.attach.image subtype.val = s := eq_of_veq $ by rw [image_val, attach_val, multiset.attach_map_val, erase_dup_eq_self] @[simp] lemma attach_insert [decidable_eq α] {a : α} {s : finset α} : attach (insert a s) = insert (⟨a, mem_insert_self a s⟩ : {x // x ∈ insert a s}) ((attach s).image (λx, ⟨x.1, mem_insert_of_mem x.2⟩)) := ext $ λ ⟨x, hx⟩, ⟨or.cases_on (mem_insert.1 hx) (λ h : x = a, λ _, mem_insert.2 $ or.inl $ subtype.eq h) (λ h : x ∈ s, λ _, mem_insert_of_mem $ mem_image.2 $ ⟨⟨x, h⟩, mem_attach _ _, subtype.eq rfl⟩), λ _, finset.mem_attach _ _⟩ theorem map_eq_image (f : α ↪ β) (s : finset α) : s.map f = s.image f := eq_of_veq $ (multiset.erase_dup_eq_self.2 (s.map f).2).symm lemma image_const {s : finset α} (h : s.nonempty) (b : β) : s.image (λa, b) = singleton b := ext $ assume b', by simp only [mem_image, exists_prop, exists_and_distrib_right, h.bex, true_and, mem_singleton, eq_comm] /-- Because `finset.image` requires a `decidable_eq` instances for the target type, we can only construct a `functor finset` when working classically. -/ instance [Π P, decidable P] : functor finset := { map := λ α β f s, s.image f, } instance [Π P, decidable P] : is_lawful_functor finset := { id_map := λ α x, image_id, comp_map := λ α β γ f g s, image_image.symm, } /-- Given a finset `s` and a predicate `p`, `s.subtype p` is the finset of `subtype p` whose elements belong to `s`. -/ protected def subtype {α} (p : α → Prop) [decidable_pred p] (s : finset α) : finset (subtype p) := (s.filter p).attach.map ⟨λ x, ⟨x.1, (finset.mem_filter.1 x.2).2⟩, λ x y H, subtype.eq $ subtype.mk.inj H⟩ @[simp] lemma mem_subtype {p : α → Prop} [decidable_pred p] {s : finset α} : ∀{a : subtype p}, a ∈ s.subtype p ↔ a.val ∈ s | ⟨a, ha⟩ := by simp [finset.subtype, ha] lemma subset_image_iff {f : α → β} {s : finset β} {t : set α} : ↑s ⊆ f '' t ↔ ∃s' : finset α, ↑s' ⊆ t ∧ s'.image f = s := begin classical, split, swap, { rintro ⟨s, hs, rfl⟩, rw [coe_image], exact set.image_subset f hs }, intro h, induction s using finset.induction with a s has ih h, { refine ⟨∅, set.empty_subset _, _⟩, convert finset.image_empty _ }, rw [finset.coe_insert, set.insert_subset] at h, rcases ih h.2 with ⟨s', hst, hsi⟩, rcases h.1 with ⟨x, hxt, rfl⟩, refine ⟨insert x s', _, _⟩, { rw [finset.coe_insert, set.insert_subset], exact ⟨hxt, hst⟩ }, rw [finset.image_insert, hsi], congr end end image /-! ### card -/ section card /-- `card s` is the cardinality (number of elements) of `s`. -/ def card (s : finset α) : nat := s.1.card theorem card_def (s : finset α) : s.card = s.1.card := rfl @[simp] theorem card_empty : card (∅ : finset α) = 0 := rfl @[simp] theorem card_eq_zero {s : finset α} : card s = 0 ↔ s = ∅ := card_eq_zero.trans val_eq_zero theorem card_pos {s : finset α} : 0 < card s ↔ s.nonempty := pos_iff_ne_zero.trans $ (not_congr card_eq_zero).trans nonempty_iff_ne_empty.symm theorem card_ne_zero_of_mem {s : finset α} {a : α} (h : a ∈ s) : card s ≠ 0 := (not_congr card_eq_zero).2 (ne_empty_of_mem h) theorem card_eq_one {s : finset α} : s.card = 1 ↔ ∃ a, s = {a} := by cases s; simp only [multiset.card_eq_one, finset.card, ← val_inj, singleton_val] @[simp] theorem card_insert_of_not_mem [decidable_eq α] {a : α} {s : finset α} (h : a ∉ s) : card (insert a s) = card s + 1 := by simpa only [card_cons, card, insert_val] using congr_arg multiset.card (ndinsert_of_not_mem h) theorem card_insert_le [decidable_eq α] (a : α) (s : finset α) : card (insert a s) ≤ card s + 1 := by by_cases a ∈ s; [{rw [insert_eq_of_mem h], apply nat.le_add_right}, rw [card_insert_of_not_mem h]] @[simp] theorem card_singleton (a : α) : card ({a} : finset α) = 1 := card_singleton _ theorem card_erase_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) = pred (card s) := card_erase_of_mem theorem card_erase_lt_of_mem [decidable_eq α] {a : α} {s : finset α} : a ∈ s → card (erase s a) < card s := card_erase_lt_of_mem theorem card_erase_le [decidable_eq α] {a : α} {s : finset α} : card (erase s a) ≤ card s := card_erase_le @[simp] theorem card_range (n : ℕ) : card (range n) = n := card_range n @[simp] theorem card_attach {s : finset α} : card (attach s) = card s := multiset.card_attach end card end finset theorem multiset.to_finset_card_le [decidable_eq α] (m : multiset α) : m.to_finset.card ≤ m.card := card_le_of_le (erase_dup_le _) theorem list.to_finset_card_le [decidable_eq α] (l : list α) : l.to_finset.card ≤ l.length := multiset.to_finset_card_le ⟦l⟧ namespace finset section card theorem card_image_le [decidable_eq β] {f : α → β} {s : finset α} : card (image f s) ≤ card s := by simpa only [card_map] using (s.1.map f).to_finset_card_le theorem card_image_of_inj_on [decidable_eq β] {f : α → β} {s : finset α} (H : ∀x∈s, ∀y∈s, f x = f y → x = y) : card (image f s) = card s := by simp only [card, image_val_of_inj_on H, card_map] theorem card_image_of_injective [decidable_eq β] {f : α → β} (s : finset α) (H : function.injective f) : card (image f s) = card s := card_image_of_inj_on $ λ x _ y _ h, H h @[simp] lemma card_map {α β} (f : α ↪ β) {s : finset α} : (s.map f).card = s.card := multiset.card_map _ _ lemma card_eq_of_bijective {s : finset α} {n : ℕ} (f : ∀i, i < n → α) (hf : ∀a∈s, ∃i, ∃h:i<n, f i h = a) (hf' : ∀i (h : i < n), f i h ∈ s) (f_inj : ∀i j (hi : i < n) (hj : j < n), f i hi = f j hj → i = j) : card s = n := begin classical, have : ∀ (a : α), a ∈ s ↔ ∃i (hi : i ∈ range n), f i (mem_range.1 hi) = a, from assume a, ⟨assume ha, let ⟨i, hi, eq⟩ := hf a ha in ⟨i, mem_range.2 hi, eq⟩, assume ⟨i, hi, eq⟩, eq ▸ hf' i (mem_range.1 hi)⟩, have : s = ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)), by simpa only [ext_iff, mem_image, exists_prop, subtype.exists, mem_attach, true_and], calc card s = card ((range n).attach.image $ λi, f i.1 (mem_range.1 i.2)) : by rw [this] ... = card ((range n).attach) : card_image_of_injective _ $ assume ⟨i, hi⟩ ⟨j, hj⟩ eq, subtype.eq $ f_inj i j (mem_range.1 hi) (mem_range.1 hj) eq ... = card (range n) : card_attach ... = n : card_range n end lemma card_eq_succ [decidable_eq α] {s : finset α} {n : ℕ} : s.card = n + 1 ↔ (∃a t, a ∉ t ∧ insert a t = s ∧ card t = n) := iff.intro (assume eq, have 0 < card s, from eq.symm ▸ nat.zero_lt_succ _, let ⟨a, has⟩ := card_pos.mp this in ⟨a, s.erase a, s.not_mem_erase a, insert_erase has, by simp only [eq, card_erase_of_mem has, pred_succ]⟩) (assume ⟨a, t, hat, s_eq, n_eq⟩, s_eq ▸ n_eq ▸ card_insert_of_not_mem hat) theorem card_le_of_subset {s t : finset α} : s ⊆ t → card s ≤ card t := multiset.card_le_of_le ∘ val_le_iff.mpr theorem eq_of_subset_of_card_le {s t : finset α} (h : s ⊆ t) (h₂ : card t ≤ card s) : s = t := eq_of_veq $ multiset.eq_of_le_of_card_le (val_le_iff.mpr h) h₂ lemma card_lt_card {s t : finset α} (h : s ⊂ t) : s.card < t.card := card_lt_of_lt (val_lt_iff.2 h) lemma card_le_card_of_inj_on {s : finset α} {t : finset β} (f : α → β) (hf : ∀a∈s, f a ∈ t) (f_inj : ∀a₁∈s, ∀a₂∈s, f a₁ = f a₂ → a₁ = a₂) : card s ≤ card t := begin classical, calc card s = card (s.image f) : by rw [card_image_of_inj_on f_inj] ... ≤ card t : card_le_of_subset $ assume x hx, match x, finset.mem_image.1 hx with _, ⟨a, ha, rfl⟩ := hf a ha end end lemma card_le_of_inj_on {n} {s : finset α} (f : ℕ → α) (hf : ∀i<n, f i ∈ s) (f_inj : ∀i j, i<n → j<n → f i = f j → i = j) : n ≤ card s := calc n = card (range n) : (card_range n).symm ... ≤ card s : card_le_card_of_inj_on f (by simpa only [mem_range]) (by simp only [mem_range]; exact assume a₁ h₁ a₂ h₂, f_inj a₁ a₂ h₁ h₂) /-- Suppose that, given objects defined on all strict subsets of any finset `s`, one knows how to define an object on `s`. Then one can inductively define an object on all finsets, starting from the empty set and iterating. This can be used either to define data, or to prove properties. -/ @[elab_as_eliminator] def strong_induction_on {p : finset α → Sort*} : ∀ (s : finset α), (∀s, (∀t ⊂ s, p t) → p s) → p s | ⟨s, nd⟩ ih := multiset.strong_induction_on s (λ s IH nd, ih ⟨s, nd⟩ (λ ⟨t, nd'⟩ ss, IH t (val_lt_iff.2 ss) nd')) nd @[elab_as_eliminator] lemma case_strong_induction_on [decidable_eq α] {p : finset α → Prop} (s : finset α) (h₀ : p ∅) (h₁ : ∀ a s, a ∉ s → (∀t ⊆ s, p t) → p (insert a s)) : p s := finset.strong_induction_on s $ λ s, finset.induction_on s (λ _, h₀) $ λ a s n _ ih, h₁ a s n $ λ t ss, ih _ (lt_of_le_of_lt ss (ssubset_insert n) : t < _) lemma card_congr {s : finset α} {t : finset β} (f : Π a ∈ s, β) (h₁ : ∀ a ha, f a ha ∈ t) (h₂ : ∀ a b ha hb, f a ha = f b hb → a = b) (h₃ : ∀ b ∈ t, ∃ a ha, f a ha = b) : s.card = t.card := by haveI := classical.prop_decidable; exact calc s.card = s.attach.card : card_attach.symm ... = (s.attach.image (λ (a : {a // a ∈ s}), f a.1 a.2)).card : eq.symm (card_image_of_injective _ (λ a b h, subtype.eq (h₂ _ _ _ _ h))) ... = t.card : congr_arg card (finset.ext $ λ b, ⟨λ h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ h₁ _ _, λ h, let ⟨a, ha₁, ha₂⟩ := h₃ b h in mem_image.2 ⟨⟨a, ha₁⟩, by simp [ha₂]⟩⟩) lemma card_union_add_card_inter [decidable_eq α] (s t : finset α) : (s ∪ t).card + (s ∩ t).card = s.card + t.card := finset.induction_on t (by simp) $ λ a r har, by by_cases a ∈ s; simp *; cc lemma card_union_le [decidable_eq α] (s t : finset α) : (s ∪ t).card ≤ s.card + t.card := card_union_add_card_inter s t ▸ le_add_right _ _ lemma surj_on_of_inj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hinj : ∀ a₁ a₂ ha₁ ha₂, f a₁ ha₁ = f a₂ ha₂ → a₁ = a₂) (hst : card t ≤ card s) : (∀ b ∈ t, ∃ a ha, b = f a ha) := by haveI := classical.dec_eq β; exact λ b hb, have h : card (image (λ (a : {a // a ∈ s}), f (a.val) a.2) (attach s)) = card s, from @card_attach _ s ▸ card_image_of_injective _ (λ ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ h, subtype.eq $ hinj _ _ _ _ h), have h₁ : image (λ a : {a // a ∈ s}, f a.1 a.2) s.attach = t := eq_of_subset_of_card_le (λ b h, let ⟨a, ha₁, ha₂⟩ := mem_image.1 h in ha₂ ▸ hf _ _) (by simp [hst, h]), begin rw ← h₁ at hb, rcases mem_image.1 hb with ⟨a, ha₁, ha₂⟩, exact ⟨a, a.2, ha₂.symm⟩, end open function lemma inj_on_of_surj_on_of_card_le {s : finset α} {t : finset β} (f : Π a ∈ s, β) (hf : ∀ a ha, f a ha ∈ t) (hsurj : ∀ b ∈ t, ∃ a ha, b = f a ha) (hst : card s ≤ card t) ⦃a₁ a₂⦄ (ha₁ : a₁ ∈ s) (ha₂ : a₂ ∈ s) (ha₁a₂: f a₁ ha₁ = f a₂ ha₂) : a₁ = a₂ := by haveI : inhabited {x // x ∈ s} := ⟨⟨a₁, ha₁⟩⟩; exact let f' : {x // x ∈ s} → {x // x ∈ t} := λ x, ⟨f x.1 x.2, hf x.1 x.2⟩ in let g : {x // x ∈ t} → {x // x ∈ s} := @surj_inv _ _ f' (λ x, let ⟨y, hy₁, hy₂⟩ := hsurj x.1 x.2 in ⟨⟨y, hy₁⟩, subtype.eq hy₂.symm⟩) in have hg : injective g, from function.injective_surj_inv _, have hsg : surjective g, from λ x, let ⟨y, hy⟩ := surj_on_of_inj_on_of_card_le (λ (x : {x // x ∈ t}) (hx : x ∈ t.attach), g x) (λ x _, show (g x) ∈ s.attach, from mem_attach _ _) (λ x y _ _ hxy, hg hxy) (by simpa) x (mem_attach _ _) in ⟨y, hy.snd.symm⟩, have hif : injective f', from (left_inverse_of_surjective_of_right_inverse hsg (right_inverse_surj_inv _)).injective, subtype.ext.1 (@hif ⟨a₁, ha₁⟩ ⟨a₂, ha₂⟩ (subtype.eq ha₁a₂)) end card /-! ### bind -/ section bind variables [decidable_eq β] {s : finset α} {t : α → finset β} /-- `bind s t` is the union of `t x` over `x ∈ s` -/ protected def bind (s : finset α) (t : α → finset β) : finset β := (s.1.bind (λ a, (t a).1)).to_finset @[simp] theorem bind_val (s : finset α) (t : α → finset β) : (s.bind t).1 = (s.1.bind (λ a, (t a).1)).erase_dup := rfl @[simp] theorem bind_empty : finset.bind ∅ t = ∅ := rfl @[simp] theorem mem_bind {b : β} : b ∈ s.bind t ↔ ∃a∈s, b ∈ t a := by simp only [mem_def, bind_val, mem_erase_dup, mem_bind, exists_prop] @[simp] theorem bind_insert [decidable_eq α] {a : α} : (insert a s).bind t = t a ∪ s.bind t := ext $ λ x, by simp only [mem_bind, exists_prop, mem_union, mem_insert, or_and_distrib_right, exists_or_distrib, exists_eq_left] -- ext $ λ x, by simp [or_and_distrib_right, exists_or_distrib] @[simp] lemma singleton_bind {a : α} : finset.bind {a} t = t a := begin classical, rw [← insert_emptyc_eq, bind_insert, bind_empty, union_empty] end theorem bind_inter (s : finset α) (f : α → finset β) (t : finset β) : s.bind f ∩ t = s.bind (λ x, f x ∩ t) := begin ext x, simp only [mem_bind, mem_inter], tauto end theorem inter_bind (t : finset β) (s : finset α) (f : α → finset β) : t ∩ s.bind f = s.bind (λ x, t ∩ f x) := by rw [inter_comm, bind_inter]; simp [inter_comm] theorem image_bind [decidable_eq γ] {f : α → β} {s : finset α} {t : β → finset γ} : (s.image f).bind t = s.bind (λa, t (f a)) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [image_insert, bind_insert, ih]) theorem bind_image [decidable_eq γ] {s : finset α} {t : α → finset β} {f : β → γ} : (s.bind t).image f = s.bind (λa, (t a).image f) := by haveI := classical.dec_eq α; exact finset.induction_on s rfl (λ a s has ih, by simp only [bind_insert, image_union, ih]) theorem bind_to_finset [decidable_eq α] (s : multiset α) (t : α → multiset β) : (s.bind t).to_finset = s.to_finset.bind (λa, (t a).to_finset) := ext $ λ x, by simp only [multiset.mem_to_finset, mem_bind, multiset.mem_bind, exists_prop] lemma bind_mono {t₁ t₂ : α → finset β} (h : ∀a∈s, t₁ a ⊆ t₂ a) : s.bind t₁ ⊆ s.bind t₂ := have ∀b a, a ∈ s → b ∈ t₁ a → (∃ (a : α), a ∈ s ∧ b ∈ t₂ a), from assume b a ha hb, ⟨a, ha, finset.mem_of_subset (h a ha) hb⟩, by simpa only [subset_iff, mem_bind, exists_imp_distrib, and_imp, exists_prop] lemma bind_subset_bind_of_subset_left {α : Type*} {s₁ s₂ : finset α} (t : α → finset β) (h : s₁ ⊆ s₂) : s₁.bind t ⊆ s₂.bind t := begin intro x, simp only [and_imp, mem_bind, exists_prop], exact Exists.imp (λ a ha, ⟨h ha.1, ha.2⟩) end lemma bind_singleton {f : α → β} : s.bind (λa, {f a}) = s.image f := ext $ λ x, by simp only [mem_bind, mem_image, mem_singleton, eq_comm] lemma image_bind_filter_eq [decidable_eq α] (s : finset β) (g : β → α) : (s.image g).bind (λa, s.filter $ (λc, g c = a)) = s := begin ext b, suffices : (∃ a, a ∈ s ∧ b ∈ s ∧ g b = g a) ↔ b ∈ s, by simpa, exact ⟨λ ⟨a, ha, hb, hab⟩, hb, λ hb, ⟨b, hb, hb, rfl⟩⟩ end end bind /-! ### prod-/ section prod variables {s : finset α} {t : finset β} /-- `product s t` is the set of pairs `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def product (s : finset α) (t : finset β) : finset (α × β) := ⟨_, nodup_product s.2 t.2⟩ @[simp] theorem product_val : (s.product t).1 = s.1.product t.1 := rfl @[simp] theorem mem_product {p : α × β} : p ∈ s.product t ↔ p.1 ∈ s ∧ p.2 ∈ t := mem_product theorem product_eq_bind [decidable_eq α] [decidable_eq β] (s : finset α) (t : finset β) : s.product t = s.bind (λa, t.image $ λb, (a, b)) := ext $ λ ⟨x, y⟩, by simp only [mem_product, mem_bind, mem_image, exists_prop, prod.mk.inj_iff, and.left_comm, exists_and_distrib_left, exists_eq_right, exists_eq_left] @[simp] theorem card_product (s : finset α) (t : finset β) : card (s.product t) = card s * card t := multiset.card_product _ _ end prod /-! ### sigma -/ section sigma variables {σ : α → Type*} {s : finset α} {t : Πa, finset (σ a)} /-- `sigma s t` is the set of dependent pairs `⟨a, b⟩` such that `a ∈ s` and `b ∈ t a`. -/ protected def sigma (s : finset α) (t : Πa, finset (σ a)) : finset (Σa, σ a) := ⟨_, nodup_sigma s.2 (λ a, (t a).2)⟩ @[simp] theorem mem_sigma {p : sigma σ} : p ∈ s.sigma t ↔ p.1 ∈ s ∧ p.2 ∈ t (p.1) := mem_sigma theorem sigma_mono {s₁ s₂ : finset α} {t₁ t₂ : Πa, finset (σ a)} (H1 : s₁ ⊆ s₂) (H2 : ∀a, t₁ a ⊆ t₂ a) : s₁.sigma t₁ ⊆ s₂.sigma t₂ := λ ⟨x, sx⟩ H, let ⟨H3, H4⟩ := mem_sigma.1 H in mem_sigma.2 ⟨H1 H3, H2 x H4⟩ theorem sigma_eq_bind [decidable_eq α] [∀a, decidable_eq (σ a)] (s : finset α) (t : Πa, finset (σ a)) : s.sigma t = s.bind (λa, (t a).image $ λb, ⟨a, b⟩) := ext $ λ ⟨x, y⟩, by simp only [mem_sigma, mem_bind, mem_image, exists_prop, and.left_comm, exists_and_distrib_left, exists_eq_left, heq_iff_eq, exists_eq_right] end sigma /-! ### pi -/ section pi variables {δ : α → Type*} [decidable_eq α] /-- Given a finset `s` of `α` and for all `a : α` a finset `t a` of `δ a`, then one can define the finset `s.pi t` of all functions defined on elements of `s` taking values in `t a` for `a ∈ s`. Note that the elements of `s.pi t` are only partially defined, on `s`. -/ def pi (s : finset α) (t : Πa, finset (δ a)) : finset (Πa∈s, δ a) := ⟨s.1.pi (λ a, (t a).1), nodup_pi s.2 (λ a _, (t a).2)⟩ @[simp] lemma pi_val (s : finset α) (t : Πa, finset (δ a)) : (s.pi t).1 = s.1.pi (λ a, (t a).1) := rfl @[simp] lemma mem_pi {s : finset α} {t : Πa, finset (δ a)} {f : Πa∈s, δ a} : f ∈ s.pi t ↔ (∀a (h : a ∈ s), f a h ∈ t a) := mem_pi _ _ _ /-- The empty dependent product function, defined on the emptyset. The assumption `a ∈ ∅` is never satisfied. -/ def pi.empty (β : α → Sort*) (a : α) (h : a ∈ (∅ : finset α)) : β a := multiset.pi.empty β a h /-- Given a function `f` defined on a finset `s`, define a new function on the finset `s ∪ {a}`, equal to `f` on `s` and sending `a` to a given value `b`. This function is denoted `s.pi.cons a b f`. If `a` already belongs to `s`, the new function takes the value `b` at `a` anyway. -/ def pi.cons (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (a' : α) (h : a' ∈ insert a s) : δ a' := multiset.pi.cons s.1 a b f _ (multiset.mem_cons.2 $ mem_insert.symm.2 h) @[simp] lemma pi.cons_same (s : finset α) (a : α) (b : δ a) (f : Πa, a ∈ s → δ a) (h : a ∈ insert a s) : pi.cons s a b f a h = b := multiset.pi.cons_same _ lemma pi.cons_ne {s : finset α} {a a' : α} {b : δ a} {f : Πa, a ∈ s → δ a} {h : a' ∈ insert a s} (ha : a ≠ a') : pi.cons s a b f a' h = f a' ((mem_insert.1 h).resolve_left ha.symm) := multiset.pi.cons_ne _ _ lemma injective_pi_cons {a : α} {b : δ a} {s : finset α} (hs : a ∉ s) : function.injective (pi.cons s a b) := assume e₁ e₂ eq, @multiset.injective_pi_cons α _ δ a b s.1 hs _ _ $ funext $ assume e, funext $ assume h, have pi.cons s a b e₁ e (by simpa only [mem_cons, mem_insert] using h) = pi.cons s a b e₂ e (by simpa only [mem_cons, mem_insert] using h), { rw [eq] }, this @[simp] lemma pi_empty {t : Πa:α, finset (δ a)} : pi (∅ : finset α) t = singleton (pi.empty δ) := rfl @[simp] lemma pi_insert [∀a, decidable_eq (δ a)] {s : finset α} {t : Πa:α, finset (δ a)} {a : α} (ha : a ∉ s) : pi (insert a s) t = (t a).bind (λb, (pi s t).image (pi.cons s a b)) := begin apply eq_of_veq, rw ← multiset.erase_dup_eq_self.2 (pi (insert a s) t).2, refine (λ s' (h : s' = a :: s.1), (_ : erase_dup (multiset.pi s' (λ a, (t a).1)) = erase_dup ((t a).1.bind $ λ b, erase_dup $ (multiset.pi s.1 (λ (a : α), (t a).val)).map $ λ f a' h', multiset.pi.cons s.1 a b f a' (h ▸ h')))) _ (insert_val_of_not_mem ha), subst s', rw pi_cons, congr, funext b, rw multiset.erase_dup_eq_self.2, exact multiset.nodup_map (multiset.injective_pi_cons ha) (pi s t).2, end lemma pi_subset {s : finset α} (t₁ t₂ : Πa, finset (δ a)) (h : ∀ a ∈ s, t₁ a ⊆ t₂ a) : s.pi t₁ ⊆ s.pi t₂ := λ g hg, mem_pi.2 $ λ a ha, h a ha (mem_pi.mp hg a ha) end pi /-! ### powerset -/ section powerset /-- When `s` is a finset, `s.powerset` is the finset of all subsets of `s` (seen as finsets). -/ def powerset (s : finset α) : finset (finset α) := ⟨s.1.powerset.pmap finset.mk (λ t h, nodup_of_le (mem_powerset.1 h) s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset.2 s.2)⟩ @[simp] theorem mem_powerset {s t : finset α} : s ∈ powerset t ↔ s ⊆ t := by cases s; simp only [powerset, mem_mk, mem_pmap, mem_powerset, exists_prop, exists_eq_right]; rw ← val_le_iff @[simp] theorem empty_mem_powerset (s : finset α) : ∅ ∈ powerset s := mem_powerset.2 (empty_subset _) @[simp] theorem mem_powerset_self (s : finset α) : s ∈ powerset s := mem_powerset.2 (subset.refl _) @[simp] lemma powerset_empty : finset.powerset (∅ : finset α) = {∅} := rfl @[simp] theorem powerset_mono {s t : finset α} : powerset s ⊆ powerset t ↔ s ⊆ t := ⟨λ h, (mem_powerset.1 $ h $ mem_powerset_self _), λ st u h, mem_powerset.2 $ subset.trans (mem_powerset.1 h) st⟩ @[simp] theorem card_powerset (s : finset α) : card (powerset s) = 2 ^ card s := (card_pmap _ _ _).trans (card_powerset s.1) lemma not_mem_of_mem_powerset_of_not_mem {s t : finset α} {a : α} (ht : t ∈ s.powerset) (h : a ∉ s) : a ∉ t := by { apply mt _ h, apply mem_powerset.1 ht } lemma powerset_insert [decidable_eq α] (s : finset α) (a : α) : powerset (insert a s) = s.powerset ∪ s.powerset.image (insert a) := begin ext t, simp only [exists_prop, mem_powerset, mem_image, mem_union, subset_insert_iff], by_cases h : a ∈ t, { split, { exact λH, or.inr ⟨_, H, insert_erase h⟩ }, { intros H, cases H, { exact subset.trans (erase_subset a t) H }, { rcases H with ⟨u, hu⟩, rw ← hu.2, exact subset.trans (erase_insert_subset a u) hu.1 } } }, { have : ¬ ∃ (u : finset α), u ⊆ s ∧ insert a u = t, by simp [ne.symm (ne_insert_of_not_mem _ _ h)], simp [finset.erase_eq_of_not_mem h, this] } end end powerset section powerset_len /-- Given an integer `n` and a finset `s`, then `powerset_len n s` is the finset of subsets of `s` of cardinality `n`.-/ def powerset_len (n : ℕ) (s : finset α) : finset (finset α) := ⟨(s.1.powerset_len n).pmap finset.mk (λ t h, nodup_of_le (mem_powerset_len.1 h).1 s.2), nodup_pmap (λ a ha b hb, congr_arg finset.val) (nodup_powerset_len s.2)⟩ theorem mem_powerset_len {n} {s t : finset α} : s ∈ powerset_len n t ↔ s ⊆ t ∧ card s = n := by cases s; simp [powerset_len, val_le_iff.symm]; refl @[simp] theorem powerset_len_mono {n} {s t : finset α} (h : s ⊆ t) : powerset_len n s ⊆ powerset_len n t := λ u h', mem_powerset_len.2 $ and.imp (λ h₂, subset.trans h₂ h) id (mem_powerset_len.1 h') @[simp] theorem card_powerset_len (n : ℕ) (s : finset α) : card (powerset_len n s) = nat.choose (card s) n := (card_pmap _ _ _).trans (card_powerset_len n s.1) end powerset_len /-! ### fold -/ section fold variables (op : β → β → β) [hc : is_commutative β op] [ha : is_associative β op] local notation a * b := op a b include hc ha /-- `fold op b f s` folds the commutative associative operation `op` over the `f`-image of `s`, i.e. `fold (+) b f {1,2,3} = `f 1 + f 2 + f 3 + b`. -/ def fold (b : β) (f : α → β) (s : finset α) : β := (s.1.map f).fold op b variables {op} {f : α → β} {b : β} {s : finset α} {a : α} @[simp] theorem fold_empty : (∅ : finset α).fold op b f = b := rfl @[simp] theorem fold_insert [decidable_eq α] (h : a ∉ s) : (insert a s).fold op b f = f a * s.fold op b f := by unfold fold; rw [insert_val, ndinsert_of_not_mem h, map_cons, fold_cons_left] @[simp] theorem fold_singleton : ({a} : finset α).fold op b f = f a * b := rfl @[simp] theorem fold_map {g : γ ↪ α} {s : finset γ} : (s.map g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, map, multiset.map_map] @[simp] theorem fold_image [decidable_eq α] {g : γ → α} {s : finset γ} (H : ∀ (x ∈ s) (y ∈ s), g x = g y → x = y) : (s.image g).fold op b f = s.fold op b (f ∘ g) := by simp only [fold, image_val_of_inj_on H, multiset.map_map] @[congr] theorem fold_congr {g : α → β} (H : ∀ x ∈ s, f x = g x) : s.fold op b f = s.fold op b g := by rw [fold, fold, map_congr H] theorem fold_op_distrib {f g : α → β} {b₁ b₂ : β} : s.fold op (b₁ * b₂) (λx, f x * g x) = s.fold op b₁ f * s.fold op b₂ g := by simp only [fold, fold_distrib] theorem fold_hom {op' : γ → γ → γ} [is_commutative γ op'] [is_associative γ op'] {m : β → γ} (hm : ∀x y, m (op x y) = op' (m x) (m y)) : s.fold op' (m b) (λx, m (f x)) = m (s.fold op b f) := by rw [fold, fold, ← fold_hom op hm, multiset.map_map] theorem fold_union_inter [decidable_eq α] {s₁ s₂ : finset α} {b₁ b₂ : β} : (s₁ ∪ s₂).fold op b₁ f * (s₁ ∩ s₂).fold op b₂ f = s₁.fold op b₂ f * s₂.fold op b₁ f := by unfold fold; rw [← fold_add op, ← map_add, union_val, inter_val, union_add_inter, map_add, hc.comm, fold_add] @[simp] theorem fold_insert_idem [decidable_eq α] [hi : is_idempotent β op] : (insert a s).fold op b f = f a * s.fold op b f := begin by_cases (a ∈ s), { rw [← insert_erase h], simp [← ha.assoc, hi.idempotent] }, { apply fold_insert h }, end lemma fold_op_rel_iff_and {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∧ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∧ ∀ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← and_assoc, and_comm (r c (f a)), and_assoc], apply and_congr iff.rfl, split, { rintro ⟨h₁, h₂⟩, intros b hb, rw finset.mem_insert at hb, rcases hb with rfl|hb; solve_by_elim }, { intro h, split, { exact h a (finset.mem_insert_self _ _), }, { intros b hb, apply h b, rw finset.mem_insert, right, exact hb } } end lemma fold_op_rel_iff_or {r : β → β → Prop} (hr : ∀ {x y z}, r x (op y z) ↔ (r x y ∨ r x z)) {c : β} : r c (s.fold op b f) ↔ (r c b ∨ ∃ x∈s, r c (f x)) := begin classical, apply finset.induction_on s, { simp }, clear s, intros a s ha IH, rw [finset.fold_insert ha, hr, IH, ← or_assoc, or_comm (r c (f a)), or_assoc], apply or_congr iff.rfl, split, { rintro (h₁|⟨x, hx, h₂⟩), { use a, simp [h₁] }, { refine ⟨x, by simp [hx], h₂⟩ } }, { rintro ⟨x, hx, h⟩, rw mem_insert at hx, cases hx, { left, rwa hx at h }, { right, exact ⟨x, hx, h⟩ } } end omit hc ha section order variables [decidable_linear_order β] (c : β) lemma le_fold_min : c ≤ s.fold min b f ↔ (c ≤ b ∧ ∀ x∈s, c ≤ f x) := fold_op_rel_iff_and $ λ x y z, le_min_iff lemma fold_min_le : s.fold min b f ≤ c ↔ (b ≤ c ∨ ∃ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ ≤ _ ↔ _, exact min_le_iff end lemma lt_fold_min : c < s.fold min b f ↔ (c < b ∧ ∀ x∈s, c < f x) := fold_op_rel_iff_and $ λ x y z, lt_min_iff lemma fold_min_lt : s.fold min b f < c ↔ (b < c ∨ ∃ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_or, intros x y z, show _ < _ ↔ _, exact min_lt_iff end lemma fold_max_le : s.fold max b f ≤ c ↔ (b ≤ c ∧ ∀ x∈s, f x ≤ c) := begin show _ ≥ _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ ≤ _ ↔ _, exact max_le_iff end lemma le_fold_max : c ≤ s.fold max b f ↔ (c ≤ b ∨ ∃ x∈s, c ≤ f x) := fold_op_rel_iff_or $ λ x y z, le_max_iff lemma fold_max_lt : s.fold max b f < c ↔ (b < c ∧ ∀ x∈s, f x < c) := begin show _ > _ ↔ _, apply fold_op_rel_iff_and, intros x y z, show _ < _ ↔ _, exact max_lt_iff end lemma lt_fold_max : c < s.fold max b f ↔ (c < b ∨ ∃ x∈s, c < f x) := fold_op_rel_iff_or $ λ x y z, lt_max_iff end order end fold /-! ### sup -/ section sup variables [semilattice_sup_bot α] /-- Supremum of a finite set: `sup {a, b, c} f = f a ⊔ f b ⊔ f c` -/ def sup (s : finset β) (f : β → α) : α := s.fold (⊔) ⊥ f variables {s s₁ s₂ : finset β} {f : β → α} lemma sup_val : s.sup f = (s.1.map f).sup := rfl @[simp] lemma sup_empty : (∅ : finset β).sup f = ⊥ := fold_empty @[simp] lemma sup_insert [decidable_eq β] {b : β} : (insert b s : finset β).sup f = f b ⊔ s.sup f := fold_insert_idem @[simp] lemma sup_singleton {b : β} : ({b} : finset β).sup f = f b := sup_singleton lemma sup_union [decidable_eq β] : (s₁ ∪ s₂).sup f = s₁.sup f ⊔ s₂.sup f := finset.induction_on s₁ (by rw [empty_union, sup_empty, bot_sup_eq]) $ λ a s has ih, by rw [insert_union, sup_insert, sup_insert, ih, sup_assoc] theorem sup_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.sup f = s₂.sup g := by subst hs; exact finset.fold_congr hfg @[simp] lemma sup_le_iff {a : α} : s.sup f ≤ a ↔ (∀b ∈ s, f b ≤ a) := begin apply iff.trans multiset.sup_le, simp only [multiset.mem_map, and_imp, exists_imp_distrib], exact ⟨λ k b hb, k _ _ hb rfl, λ k a' b hb h, h ▸ k _ hb⟩, end lemma sup_le {a : α} : (∀b ∈ s, f b ≤ a) → s.sup f ≤ a := sup_le_iff.2 lemma le_sup {b : β} (hb : b ∈ s) : f b ≤ s.sup f := sup_le_iff.1 (le_refl _) _ hb lemma sup_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.sup f ≤ s.sup g := sup_le (λ b hb, le_trans (h b hb) (le_sup hb)) lemma sup_mono (h : s₁ ⊆ s₂) : s₁.sup f ≤ s₂.sup f := sup_le $ assume b hb, le_sup (h hb) @[simp] lemma sup_lt_iff [is_total α (≤)] {a : α} (ha : ⊥ < a) : s.sup f < a ↔ (∀b ∈ s, f b < a) := by letI := classical.dec_eq β; from ⟨ λh b hb, lt_of_le_of_lt (le_sup hb) h, finset.induction_on s (by simp [ha]) (by simp {contextual := tt}) ⟩ lemma comp_sup_eq_sup_comp [is_total α (≤)] {γ : Type} [semilattice_sup_bot γ] (g : α → γ) (mono_g : monotone g) (bot : g ⊥ = ⊥) : g (s.sup f) = s.sup (g ∘ f) := have A : ∀x y, g (x ⊔ y) = g x ⊔ g y := begin assume x y, cases (@is_total.total _ (≤) _ x y) with h, { simp [sup_of_le_right h, sup_of_le_right (mono_g h)] }, { simp [sup_of_le_left h, sup_of_le_left (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [bot]) (by simp [A] {contextual := tt}) theorem subset_range_sup_succ (s : finset ℕ) : s ⊆ range (s.sup id).succ := λ n hn, mem_range.2 $ nat.lt_succ_of_le $ le_sup hn theorem exists_nat_subset_range (s : finset ℕ) : ∃n : ℕ, s ⊆ range n := ⟨_, s.subset_range_sup_succ⟩ end sup lemma sup_eq_supr [complete_lattice β] (s : finset α) (f : α → β) : s.sup f = (⨆a∈s, f a) := le_antisymm (finset.sup_le $ assume a ha, le_supr_of_le a $ le_supr _ ha) (supr_le $ assume a, supr_le $ assume ha, le_sup ha) /-! ### inf -/ section inf variables [semilattice_inf_top α] /-- Infimum of a finite set: `inf {a, b, c} f = f a ⊓ f b ⊓ f c` -/ def inf (s : finset β) (f : β → α) : α := s.fold (⊓) ⊤ f variables {s s₁ s₂ : finset β} {f : β → α} lemma inf_val : s.inf f = (s.1.map f).inf := rfl @[simp] lemma inf_empty : (∅ : finset β).inf f = ⊤ := fold_empty lemma le_inf_iff {a : α} : a ≤ s.inf f ↔ ∀b ∈ s, a ≤ f b := begin apply iff.trans multiset.le_inf, refine ⟨λ k b hb, k (f b) (multiset.mem_map_of_mem _ hb), λ k b hb, _⟩, rw multiset.mem_map at hb, rcases hb with ⟨a', ha', rfl⟩, apply k a' ha' end @[simp] lemma inf_insert [decidable_eq β] {b : β} : (insert b s : finset β).inf f = f b ⊓ s.inf f := fold_insert_idem @[simp] lemma inf_singleton {b : β} : ({b} : finset β).inf f = f b := inf_singleton lemma inf_union [decidable_eq β] : (s₁ ∪ s₂).inf f = s₁.inf f ⊓ s₂.inf f := finset.induction_on s₁ (by rw [empty_union, inf_empty, top_inf_eq]) $ λ a s has ih, by rw [insert_union, inf_insert, inf_insert, ih, inf_assoc] theorem inf_congr {f g : β → α} (hs : s₁ = s₂) (hfg : ∀a∈s₂, f a = g a) : s₁.inf f = s₂.inf g := by subst hs; exact finset.fold_congr hfg lemma inf_le {b : β} (hb : b ∈ s) : s.inf f ≤ f b := le_inf_iff.1 (le_refl _) _ hb lemma le_inf {a : α} : (∀b ∈ s, a ≤ f b) → a ≤ s.inf f := le_inf_iff.2 lemma inf_mono_fun {g : β → α} (h : ∀b∈s, f b ≤ g b) : s.inf f ≤ s.inf g := le_inf (λ b hb, le_trans (inf_le hb) (h b hb)) lemma inf_mono (h : s₁ ⊆ s₂) : s₂.inf f ≤ s₁.inf f := le_inf $ assume b hb, inf_le (h hb) lemma lt_inf [is_total α (≤)] {a : α} : (a < ⊤) → (∀b ∈ s, a < f b) → a < s.inf f := by letI := classical.dec_eq β; from finset.induction_on s (by simp) (by simp {contextual := tt}) lemma comp_inf_eq_inf_comp [is_total α (≤)] {γ : Type} [semilattice_inf_top γ] (g : α → γ) (mono_g : monotone g) (top : g ⊤ = ⊤) : g (s.inf f) = s.inf (g ∘ f) := have A : ∀x y, g (x ⊓ y) = g x ⊓ g y := begin assume x y, cases (@is_total.total _ (≤) _ x y) with h, { simp [inf_of_le_left h, inf_of_le_left (mono_g h)] }, { simp [inf_of_le_right h, inf_of_le_right (mono_g h)] } end, by letI := classical.dec_eq β; from finset.induction_on s (by simp [top]) (by simp [A] {contextual := tt}) end inf lemma inf_eq_infi [complete_lattice β] (s : finset α) (f : α → β) : s.inf f = (⨅a∈s, f a) := le_antisymm (le_infi $ assume a, le_infi $ assume ha, inf_le ha) (finset.le_inf $ assume a ha, infi_le_of_le a $ infi_le _ ha) /-! ### max and min of finite sets -/ section max_min variables [decidable_linear_order α] /-- Let `s` be a finset in a linear order. Then `s.max` is the maximum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.max'`. -/ protected def max : finset α → option α := fold (option.lift_or_get max) none some theorem max_eq_sup_with_bot (s : finset α) : s.max = @sup (with_bot α) α _ s some := rfl @[simp] theorem max_empty : (∅ : finset α).max = none := rfl @[simp] theorem max_insert {a : α} {s : finset α} : (insert a s).max = option.lift_or_get max (some a) s.max := fold_insert_idem @[simp] theorem max_singleton {a : α} : finset.max {a} = some a := by { rw [← insert_emptyc_eq], exact max_insert } theorem max_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.max := (@le_sup (with_bot α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem max_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.max := let ⟨a, ha⟩ := h in max_of_mem ha theorem max_eq_none {s : finset α} : s.max = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := max_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ max_empty⟩ theorem mem_of_max {s : finset α} : ∀ {a : α}, a ∈ s.max → a ∈ s := finset.induction_on s (λ _ H, by cases H) (λ b s _ (ih : ∀ {a}, a ∈ s.max → a ∈ s) a (h : a ∈ (insert b s).max), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice max_choice (some b) s.max with q q; rw [max_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end) theorem le_max_of_mem {s : finset α} {a b : α} (h₁ : a ∈ s) (h₂ : b ∈ s.max) : a ≤ b := by rcases @le_sup (with_bot α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Let `s` be a finset in a linear order. Then `s.min` is the minimum of `s` if `s` is not empty, and `none` otherwise. It belongs to `option α`. If you want to get an element of `α`, see `s.min'`. -/ protected def min : finset α → option α := fold (option.lift_or_get min) none some theorem min_eq_inf_with_top (s : finset α) : s.min = @inf (with_top α) α _ s some := rfl @[simp] theorem min_empty : (∅ : finset α).min = none := rfl @[simp] theorem min_insert {a : α} {s : finset α} : (insert a s).min = option.lift_or_get min (some a) s.min := fold_insert_idem @[simp] theorem min_singleton {a : α} : finset.min {a} = some a := by { rw ← insert_emptyc_eq, exact min_insert } theorem min_of_mem {s : finset α} {a : α} (h : a ∈ s) : ∃ b, b ∈ s.min := (@inf_le (with_top α) _ _ _ _ _ h _ rfl).imp $ λ b, Exists.fst theorem min_of_nonempty {s : finset α} (h : s.nonempty) : ∃ a, a ∈ s.min := let ⟨a, ha⟩ := h in min_of_mem ha theorem min_eq_none {s : finset α} : s.min = none ↔ s = ∅ := ⟨λ h, s.eq_empty_or_nonempty.elim id (λ H, let ⟨a, ha⟩ := min_of_nonempty H in by rw h at ha; cases ha), λ h, h.symm ▸ min_empty⟩ theorem mem_of_min {s : finset α} : ∀ {a : α}, a ∈ s.min → a ∈ s := finset.induction_on s (λ _ H, by cases H) $ λ b s _ (ih : ∀ {a}, a ∈ s.min → a ∈ s) a (h : a ∈ (insert b s).min), begin by_cases p : b = a, { induction p, exact mem_insert_self b s }, { cases option.lift_or_get_choice min_choice (some b) s.min with q q; rw [min_insert, q] at h, { cases h, cases p rfl }, { exact mem_insert_of_mem (ih h) } } end theorem min_le_of_mem {s : finset α} {a b : α} (h₁ : b ∈ s) (h₂ : a ∈ s.min) : a ≤ b := by rcases @inf_le (with_top α) _ _ _ _ _ h₁ _ rfl with ⟨b', hb, ab⟩; cases h₂.symm.trans hb; assumption /-- Given a nonempty finset `s` in a linear order `α `, then `s.min' h` is its minimum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.min`, taking values in `option α`. -/ def min' (s : finset α) (H : s.nonempty) : α := @option.get _ s.min $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := min_of_mem hk in by simp at hb; simp [hb] /-- Given a nonempty finset `s` in a linear order `α `, then `s.max' h` is its maximum, as an element of `α`, where `h` is a proof of nonemptiness. Without this assumption, use instead `s.max`, taking values in `option α`. -/ def max' (s : finset α) (H : s.nonempty) : α := @option.get _ s.max $ let ⟨k, hk⟩ := H in let ⟨b, hb⟩ := max_of_mem hk in by simp at hb; simp [hb] variables (s : finset α) (H : s.nonempty) theorem min'_mem : s.min' H ∈ s := mem_of_min $ by simp [min'] theorem min'_le (x) (H2 : x ∈ s) : s.min' H ≤ x := min_le_of_mem H2 $ option.get_mem _ theorem le_min' (x) (H2 : ∀ y ∈ s, x ≤ y) : x ≤ s.min' H := H2 _ $ min'_mem _ _ theorem max'_mem : s.max' H ∈ s := mem_of_max $ by simp [max'] theorem le_max' (x) (H2 : x ∈ s) : x ≤ s.max' H := le_max_of_mem H2 $ option.get_mem _ theorem max'_le (x) (H2 : ∀ y ∈ s, y ≤ x) : s.max' H ≤ x := H2 _ $ max'_mem _ _ theorem min'_lt_max' {i j} (H1 : i ∈ s) (H2 : j ∈ s) (H3 : i ≠ j) : s.min' H < s.max' H := begin rcases lt_trichotomy i j with H4 | H4 | H4, { have H5 := min'_le s H i H1, have H6 := le_max' s H j H2, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 }, { cc }, { have H5 := min'_le s H j H2, have H6 := le_max' s H i H1, apply lt_of_le_of_lt H5, apply lt_of_lt_of_le H4 H6 } end /-- If there's more than 1 element, the min' is less than the max'. An alternate version of `min'_lt_max'` which is sometimes more convenient. -/ lemma min'_lt_max'_of_card (h₂ : 1 < card s) : s.min' H < s.max' H := begin apply lt_of_not_ge, intro a, apply not_le_of_lt h₂ (le_of_eq _), rw card_eq_one, use max' s H, rw eq_singleton_iff_unique_mem, refine ⟨max'_mem _ _, λ t Ht, le_antisymm (le_max' s H t Ht) (le_trans a (min'_le s H t Ht))⟩, end end max_min section exists_max_min variables [linear_order α] lemma exists_max_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x' ≤ f x := begin letI := classical.DLO α, cases max_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_max hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', le_max_of_mem (mem_image_of_mem f hx') hy⟩, end lemma exists_min_image (s : finset β) (f : β → α) (h : s.nonempty) : ∃ x ∈ s, ∀ x' ∈ s, f x ≤ f x' := begin letI := classical.DLO α, cases min_of_nonempty (h.image f) with y hy, rcases mem_image.mp (mem_of_min hy) with ⟨x, hx, rfl⟩, exact ⟨x, hx, λ x' hx', min_le_of_mem (mem_image_of_mem f hx') hy⟩ end end exists_max_min /-! ### sort -/ section sort variables (r : α → α → Prop) [decidable_rel r] [is_trans α r] [is_antisymm α r] [is_total α r] /-- `sort s` constructs a sorted list from the unordered set `s`. (Uses merge sort algorithm.) -/ def sort (s : finset α) : list α := sort r s.1 @[simp] theorem sort_sorted (s : finset α) : list.sorted r (sort r s) := sort_sorted _ _ @[simp] theorem sort_eq (s : finset α) : ↑(sort r s) = s.1 := sort_eq _ _ @[simp] theorem sort_nodup (s : finset α) : (sort r s).nodup := (by rw sort_eq; exact s.2 : @multiset.nodup α (sort r s)) @[simp] theorem sort_to_finset [decidable_eq α] (s : finset α) : (sort r s).to_finset = s := list.to_finset_eq (sort_nodup r s) ▸ eq_of_veq (sort_eq r s) @[simp] theorem mem_sort {s : finset α} {a : α} : a ∈ sort r s ↔ a ∈ s := multiset.mem_sort _ @[simp] theorem length_sort {s : finset α} : (sort r s).length = s.card := multiset.length_sort _ end sort section sort_linear_order variables [decidable_linear_order α] theorem sort_sorted_lt (s : finset α) : list.sorted (<) (sort (≤) s) := (sort_sorted _ _).imp₂ (@lt_of_le_of_ne _ _) (sort_nodup _ _) lemma sorted_zero_eq_min' (s : finset α) (h : 0 < (s.sort (≤)).length) (H : s.nonempty) : (s.sort (≤)).nth_le 0 h = s.min' H := begin let l := s.sort (≤), apply le_antisymm, { have : s.min' H ∈ l := (finset.mem_sort (≤)).mpr (s.min'_mem H), obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.min' H := list.mem_iff_nth_le.1 this, rw ← hi, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≤)) (nat.zero_le i) }, { have : l.nth_le 0 h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l 0 h), exact s.min'_le H _ this } end lemma sorted_last_eq_max' (s : finset α) (h : (s.sort (≤)).length - 1 < (s.sort (≤)).length) (H : s.nonempty) : (s.sort (≤)).nth_le ((s.sort (≤)).length - 1) h = s.max' H := begin let l := s.sort (≤), apply le_antisymm, { have : l.nth_le ((s.sort (≤)).length - 1) h ∈ s := (finset.mem_sort (≤)).1 (list.nth_le_mem l _ h), exact s.le_max' H _ this }, { have : s.max' H ∈ l := (finset.mem_sort (≤)).mpr (s.max'_mem H), obtain ⟨i, i_lt, hi⟩ : ∃ i (hi : i < l.length), l.nth_le i hi = s.max' H := list.mem_iff_nth_le.1 this, rw ← hi, have : i ≤ l.length - 1 := nat.le_pred_of_lt i_lt, exact list.nth_le_of_sorted_of_le (s.sort_sorted (≤)) (nat.le_pred_of_lt i_lt) }, end /-- Given a finset `s` of cardinal `k` in a linear order `α`, the map `mono_of_fin s h` is the increasing bijection between `fin k` and `s` as an `α`-valued map. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ def mono_of_fin (s : finset α) {k : ℕ} (h : s.card = k) (i : fin k) : α := have A : (i : ℕ) < (s.sort (≤)).length, by simpa [h] using i.2, (s.sort (≤)).nth_le i A lemma mono_of_fin_strict_mono (s : finset α) {k : ℕ} (h : s.card = k) : strict_mono (s.mono_of_fin h) := begin assume i j hij, exact list.pairwise_iff_nth_le.1 s.sort_sorted_lt _ _ _ hij end lemma mono_of_fin_bij_on (s : finset α) {k : ℕ} (h : s.card = k) : set.bij_on (s.mono_of_fin h) set.univ ↑s := begin have A : ∀ j, j ∈ s ↔ j ∈ (s.sort (≤)) := λ j, by simp, apply set.bij_on.mk, { assume i hi, simp only [mono_of_fin, set.mem_preimage, mem_coe, list.nth_le, A], exact list.nth_le_mem _ _ _ }, { exact ((mono_of_fin_strict_mono s h).injective).inj_on _ }, { assume x hx, simp only [mem_coe, A] at hx, obtain ⟨i, il, hi⟩ : ∃ (i : ℕ) (h : i < (s.sort (≤)).length), (s.sort (≤)).nth_le i h = x := list.nth_le_of_mem hx, simp [h] at il, exact ⟨⟨i, il⟩, set.mem_univ _, hi⟩ } end lemma mono_of_fin_injective (s : finset α) {k : ℕ} (h : s.card = k) : function.injective (s.mono_of_fin h) := set.injective_iff_inj_on_univ.mpr (s.mono_of_fin_bij_on h).inj_on /-- The bijection `mono_of_fin s h` sends `0` to the minimum of `s`. -/ lemma mono_of_fin_zero {s : finset α} {k : ℕ} (h : s.card = k) (hs : s.nonempty) (hz : 0 < k) : mono_of_fin s h ⟨0, hz⟩ = s.min' hs := begin apply le_antisymm, { have : min' s hs ∈ s := min'_mem s hs, rcases (mono_of_fin_bij_on s h).surj_on this with ⟨a, _, ha⟩, rw ← ha, apply (mono_of_fin_strict_mono s h).monotone, exact zero_le a.val }, { have : mono_of_fin s h ⟨0, hz⟩ ∈ s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), exact min'_le s hs _ this } end /-- The bijection `mono_of_fin s h` sends `k-1` to the maximum of `s`. -/ lemma mono_of_fin_last {s : finset α} {k : ℕ} (h : s.card = k) (hs : s.nonempty) (hz : 0 < k) : mono_of_fin s h ⟨k-1, buffer.lt_aux_2 hz⟩ = s.max' hs := begin have h'' : k - 1 < k := buffer.lt_aux_2 hz, apply le_antisymm, { have : mono_of_fin s h ⟨k-1, h''⟩ ∈ s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), exact le_max' s hs _ this }, { have : max' s hs ∈ s := max'_mem s hs, rcases (mono_of_fin_bij_on s h).surj_on this with ⟨a, _, ha⟩, rw ← ha, apply (mono_of_fin_strict_mono s h).monotone, exact le_pred_of_lt a.2}, end /-- Any increasing bijection between `fin k` and a finset of cardinality `k` has to coincide with the increasing bijection `mono_of_fin s h`. For a statement assuming only that `f` maps `univ` to `s`, see `mono_of_fin_unique'`.-/ lemma mono_of_fin_unique {s : finset α} {k : ℕ} (h : s.card = k) {f : fin k → α} (hbij : set.bij_on f set.univ ↑s) (hmono : strict_mono f) : f = s.mono_of_fin h := begin ext ⟨i, hi⟩, induction i using nat.strong_induction_on with i IH, rcases lt_trichotomy (f ⟨i, hi⟩) (mono_of_fin s h ⟨i, hi⟩) with H|H|H, { have A : f ⟨i, hi⟩ ∈ ↑s := hbij.maps_to (set.mem_univ _), rcases (mono_of_fin_bij_on s h).surj_on A with ⟨j, _, hj⟩, rw ← hj at H, have ji : j < ⟨i, hi⟩ := (mono_of_fin_strict_mono s h).lt_iff_lt.1 H, have : f j = mono_of_fin s h j, by { convert IH j.1 ji (lt_trans ji hi), rw fin.ext_iff }, rw ← this at hj, exact (ne_of_lt (hmono ji) hj).elim }, { exact H }, { have A : mono_of_fin s h ⟨i, hi⟩ ∈ ↑s := (mono_of_fin_bij_on s h).maps_to (set.mem_univ _), rcases hbij.surj_on A with ⟨j, _, hj⟩, rw ← hj at H, have ji : j < ⟨i, hi⟩ := hmono.lt_iff_lt.1 H, have : f j = mono_of_fin s h j, by { convert IH j.1 ji (lt_trans ji hi), rw fin.ext_iff }, rw this at hj, exact (ne_of_lt (mono_of_fin_strict_mono s h ji) hj).elim } end /-- Two parametrizations `mono_of_fin` of the same set take the same value on `i` and `j` if and only if `i = j`. Since they can be defined on a priori not defeq types `fin k` and `fin l` (although necessarily `k = l`), the conclusion is rather written `i.val = j.val`. -/ @[simp] lemma mono_of_fin_eq_mono_of_fin_iff {k l : ℕ} {s : finset α} {i : fin k} {j : fin l} {h : s.card = k} {h' : s.card = l} : s.mono_of_fin h i = s.mono_of_fin h' j ↔ i.val = j.val := begin have A : k = l, by rw [← h', ← h], have : s.mono_of_fin h = (s.mono_of_fin h') ∘ (λ j : (fin k), ⟨j.1, A ▸ j.2⟩) := rfl, rw [this, function.comp_app, (s.mono_of_fin_injective h').eq_iff, fin.ext_iff] end /-- Given a finset `s` of cardinal `k` in a linear order `α`, the equiv `mono_equiv_of_fin s h` is the increasing bijection between `fin k` and `s` as an `s`-valued map. Here, `h` is a proof that the cardinality of `s` is `k`. We use this instead of a map `fin s.card → α` to avoid casting issues in further uses of this function. -/ noncomputable def mono_equiv_of_fin (s : finset α) {k : ℕ} (h : s.card = k) : fin k ≃ {x // x ∈ s} := (s.mono_of_fin_bij_on h).equiv _ end sort_linear_order /-! ### disjoint -/ section disjoint variable [decidable_eq α] theorem disjoint_left {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ s → a ∉ t := by simp only [_root_.disjoint, inf_eq_inter, le_iff_subset, subset_iff, mem_inter, not_and, and_imp]; refl theorem disjoint_val {s t : finset α} : disjoint s t ↔ s.1.disjoint t.1 := disjoint_left theorem disjoint_iff_inter_eq_empty {s t : finset α} : disjoint s t ↔ s ∩ t = ∅ := disjoint_iff instance decidable_disjoint (U V : finset α) : decidable (disjoint U V) := decidable_of_decidable_of_iff (by apply_instance) eq_bot_iff theorem disjoint_right {s t : finset α} : disjoint s t ↔ ∀ {a}, a ∈ t → a ∉ s := by rw [disjoint.comm, disjoint_left] theorem disjoint_iff_ne {s t : finset α} : disjoint s t ↔ ∀ a ∈ s, ∀ b ∈ t, a ≠ b := by simp only [disjoint_left, imp_not_comm, forall_eq'] theorem disjoint_of_subset_left {s t u : finset α} (h : s ⊆ u) (d : disjoint u t) : disjoint s t := disjoint_left.2 (λ x m₁, (disjoint_left.1 d) (h m₁)) theorem disjoint_of_subset_right {s t u : finset α} (h : t ⊆ u) (d : disjoint s u) : disjoint s t := disjoint_right.2 (λ x m₁, (disjoint_right.1 d) (h m₁)) @[simp] theorem disjoint_empty_left (s : finset α) : disjoint ∅ s := disjoint_bot_left @[simp] theorem disjoint_empty_right (s : finset α) : disjoint s ∅ := disjoint_bot_right @[simp] theorem singleton_disjoint {s : finset α} {a : α} : disjoint (singleton a) s ↔ a ∉ s := by simp only [disjoint_left, mem_singleton, forall_eq] @[simp] theorem disjoint_singleton {s : finset α} {a : α} : disjoint s (singleton a) ↔ a ∉ s := disjoint.comm.trans singleton_disjoint @[simp] theorem disjoint_insert_left {a : α} {s t : finset α} : disjoint (insert a s) t ↔ a ∉ t ∧ disjoint s t := by simp only [disjoint_left, mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] @[simp] theorem disjoint_insert_right {a : α} {s t : finset α} : disjoint s (insert a t) ↔ a ∉ s ∧ disjoint s t := disjoint.comm.trans $ by rw [disjoint_insert_left, disjoint.comm] @[simp] theorem disjoint_union_left {s t u : finset α} : disjoint (s ∪ t) u ↔ disjoint s u ∧ disjoint t u := by simp only [disjoint_left, mem_union, or_imp_distrib, forall_and_distrib] @[simp] theorem disjoint_union_right {s t u : finset α} : disjoint s (t ∪ u) ↔ disjoint s t ∧ disjoint s u := by simp only [disjoint_right, mem_union, or_imp_distrib, forall_and_distrib] lemma sdiff_disjoint {s t : finset α} : disjoint (t \ s) s := disjoint_left.2 $ assume a ha, (mem_sdiff.1 ha).2 lemma disjoint_sdiff {s t : finset α} : disjoint s (t \ s) := sdiff_disjoint.symm lemma disjoint_sdiff_inter (s t : finset α) : disjoint (s \ t) (s ∩ t) := disjoint_of_subset_right (inter_subset_right _ _) sdiff_disjoint lemma sdiff_eq_self_iff_disjoint {s t : finset α} : s \ t = s ↔ disjoint s t := by rw [sdiff_eq_self, subset_empty, disjoint_iff_inter_eq_empty] lemma sdiff_eq_self_of_disjoint {s t : finset α} (h : disjoint s t) : s \ t = s := sdiff_eq_self_iff_disjoint.2 h lemma disjoint_self_iff_empty (s : finset α) : disjoint s s ↔ s = ∅ := disjoint_self lemma disjoint_bind_left {ι : Type*} (s : finset ι) (f : ι → finset α) (t : finset α) : disjoint (s.bind f) t ↔ (∀i∈s, disjoint (f i) t) := begin classical, refine s.induction _ _, { simp only [forall_mem_empty_iff, bind_empty, disjoint_empty_left] }, { assume i s his ih, simp only [disjoint_union_left, bind_insert, his, forall_mem_insert, ih] } end lemma disjoint_bind_right {ι : Type*} (s : finset α) (t : finset ι) (f : ι → finset α) : disjoint s (t.bind f) ↔ (∀i∈t, disjoint s (f i)) := by simpa only [disjoint.comm] using disjoint_bind_left t f s @[simp] theorem card_disjoint_union {s t : finset α} (h : disjoint s t) : card (s ∪ t) = card s + card t := by rw [← card_union_add_card_inter, disjoint_iff_inter_eq_empty.1 h, card_empty, add_zero] theorem card_sdiff {s t : finset α} (h : s ⊆ t) : card (t \ s) = card t - card s := suffices card (t \ s) = card ((t \ s) ∪ s) - card s, by rwa sdiff_union_of_subset h at this, by rw [card_disjoint_union sdiff_disjoint, nat.add_sub_cancel] lemma disjoint_filter {s : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : disjoint (s.filter p) (s.filter q) ↔ (∀ x ∈ s, p x → ¬ q x) := by split; simp [disjoint_left] {contextual := tt} lemma disjoint_filter_filter {s t : finset α} {p q : α → Prop} [decidable_pred p] [decidable_pred q] : (disjoint s t) → disjoint (s.filter p) (t.filter q) := disjoint.mono (filter_subset _) (filter_subset _) lemma pi_disjoint_of_disjoint {δ : α → Type*} [∀a, decidable_eq (δ a)] {s : finset α} [decidable_eq (Πa∈s, δ a)] (t₁ t₂ : Πa, finset (δ a)) {a : α} (ha : a ∈ s) (h : disjoint (t₁ a) (t₂ a)) : disjoint (s.pi t₁) (s.pi t₂) := disjoint_iff_ne.2 $ λ f₁ hf₁ f₂ hf₂ eq₁₂, disjoint_iff_ne.1 h (f₁ a ha) (mem_pi.mp hf₁ a ha) (f₂ a ha) (mem_pi.mp hf₂ a ha) $ congr_fun (congr_fun eq₁₂ a) ha lemma disjoint_iff_disjoint_coe {α : Type*} {a b : finset α} [decidable_eq α] : disjoint a b ↔ disjoint (↑a : set α) (↑b : set α) := by { rw [finset.disjoint_left, set.disjoint_left], refl } end disjoint /-- Given a set A and a set B inside it, we can shrink A to any appropriate size, and keep B inside it. -/ lemma exists_intermediate_set {A B : finset α} (i : ℕ) (h₁ : i + card B ≤ card A) (h₂ : B ⊆ A) : ∃ (C : finset α), B ⊆ C ∧ C ⊆ A ∧ card C = i + card B := begin classical, rcases nat.le.dest h₁ with ⟨k, _⟩, clear h₁, induction k with k ih generalizing A, { exact ⟨A, h₂, subset.refl _, h.symm⟩ }, { have : (A \ B).nonempty, { rw [← card_pos, card_sdiff h₂, ← h, nat.add_right_comm, nat.add_sub_cancel, nat.add_succ], apply nat.succ_pos }, rcases this with ⟨a, ha⟩, have z : i + card B + k = card (erase A a), { rw [card_erase_of_mem, ← h, nat.add_succ, nat.pred_succ], rw mem_sdiff at ha, exact ha.1 }, rcases ih _ z with ⟨B', hB', B'subA', cards⟩, { exact ⟨B', hB', trans B'subA' (erase_subset _ _), cards⟩ }, { rintros t th, apply mem_erase_of_ne_of_mem _ (h₂ th), rintro rfl, exact not_mem_sdiff_of_mem_right th ha } } end /-- We can shrink A to any smaller size. -/ lemma exists_smaller_set (A : finset α) (i : ℕ) (h₁ : i ≤ card A) : ∃ (B : finset α), B ⊆ A ∧ card B = i := let ⟨B, _, x₁, x₂⟩ := exists_intermediate_set i (by simpa) (empty_subset A) in ⟨B, x₁, x₂⟩ instance [has_repr α] : has_repr (finset α) := ⟨λ s, repr s.1⟩ /-- Given a finset `s` of `ℕ` contained in `{0,..., n-1}`, the corresponding finset in `fin n` is `s.attach_fin h` where `h` is a proof that all elements of `s` are less than `n`. -/ def attach_fin (s : finset ℕ) {n : ℕ} (h : ∀ m ∈ s, m < n) : finset (fin n) := ⟨s.1.pmap (λ a ha, ⟨a, ha⟩) h, multiset.nodup_pmap (λ _ _ _ _, fin.mk.inj) s.2⟩ @[simp] lemma mem_attach_fin {n : ℕ} {s : finset ℕ} (h : ∀ m ∈ s, m < n) {a : fin n} : a ∈ s.attach_fin h ↔ a.1 ∈ s := ⟨λ h, let ⟨b, hb₁, hb₂⟩ := multiset.mem_pmap.1 h in hb₂ ▸ hb₁, λ h, multiset.mem_pmap.2 ⟨a.1, h, fin.eta _ _⟩⟩ @[simp] lemma card_attach_fin {n : ℕ} (s : finset ℕ) (h : ∀ m ∈ s, m < n) : (s.attach_fin h).card = s.card := multiset.card_pmap _ _ _ /-! ### choose -/ section choose variables (p : α → Prop) [decidable_pred p] (l : finset α) /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the corresponding subtype. -/ def choose_x (hp : (∃! a, a ∈ l ∧ p a)) : { a // a ∈ l ∧ p a } := multiset.choose_x p l.val hp /-- Given a finset `l` and a predicate `p`, associate to a proof that there is a unique element of `l` satisfying `p` this unique element, as an element of the ambient type. -/ def choose (hp : ∃! a, a ∈ l ∧ p a) : α := choose_x p l hp lemma choose_spec (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l ∧ p (choose p l hp) := (choose_x p l hp).property lemma choose_mem (hp : ∃! a, a ∈ l ∧ p a) : choose p l hp ∈ l := (choose_spec _ _ _).1 lemma choose_property (hp : ∃! a, a ∈ l ∧ p a) : p (choose p l hp) := (choose_spec _ _ _).2 end choose theorem lt_wf {α} : well_founded (@has_lt.lt (finset α) _) := have H : subrelation (@has_lt.lt (finset α) _) (inv_image (<) card), from λ x y hxy, card_lt_card hxy, subrelation.wf H $ inv_image.wf _ $ nat.lt_wf /-! ### intervals -/ /- Ico (a closed open interval) -/ variables {n m l : ℕ} /-- `Ico n m` is the set of natural numbers `n ≤ k < m`. -/ def Ico (n m : ℕ) : finset ℕ := ⟨_, Ico.nodup n m⟩ namespace Ico @[simp] theorem val (n m : ℕ) : (Ico n m).1 = multiset.Ico n m := rfl @[simp] theorem to_finset (n m : ℕ) : (multiset.Ico n m).to_finset = Ico n m := (multiset.to_finset_eq _).symm theorem image_add (n m k : ℕ) : (Ico n m).image ((+) k) = Ico (n + k) (m + k) := by simp [image, multiset.Ico.map_add] theorem image_sub (n m k : ℕ) (h : k ≤ n) : (Ico n m).image (λ x, x - k) = Ico (n - k) (m - k) := begin dsimp [image], rw [multiset.Ico.map_sub _ _ _ h, ←multiset.to_finset_eq], refl, end theorem zero_bot (n : ℕ) : Ico 0 n = range n := eq_of_veq $ multiset.Ico.zero_bot _ @[simp] theorem card (n m : ℕ) : (Ico n m).card = m - n := multiset.Ico.card _ _ @[simp] theorem mem {n m l : ℕ} : l ∈ Ico n m ↔ n ≤ l ∧ l < m := multiset.Ico.mem theorem eq_empty_of_le {n m : ℕ} (h : m ≤ n) : Ico n m = ∅ := eq_of_veq $ multiset.Ico.eq_zero_of_le h @[simp] theorem self_eq_empty (n : ℕ) : Ico n n = ∅ := eq_empty_of_le $ le_refl n @[simp] theorem eq_empty_iff {n m : ℕ} : Ico n m = ∅ ↔ m ≤ n := iff.trans val_eq_zero.symm multiset.Ico.eq_zero_iff theorem subset_iff {m₁ n₁ m₂ n₂ : ℕ} (hmn : m₁ < n₁) : Ico m₁ n₁ ⊆ Ico m₂ n₂ ↔ (m₂ ≤ m₁ ∧ n₁ ≤ n₂) := begin simp only [subset_iff, mem], refine ⟨λ h, ⟨_, _⟩, _⟩, { exact (h ⟨le_refl _, hmn⟩).1 }, { refine le_of_pred_lt (@h (pred n₁) ⟨le_pred_of_lt hmn, pred_lt _⟩).2, exact ne_of_gt (lt_of_le_of_lt (nat.zero_le m₁) hmn) }, { rintros ⟨hm, hn⟩ k ⟨hmk, hkn⟩, exact ⟨le_trans hm hmk, lt_of_lt_of_le hkn hn⟩ } end protected theorem subset {m₁ n₁ m₂ n₂ : ℕ} (hmm : m₂ ≤ m₁) (hnn : n₁ ≤ n₂) : Ico m₁ n₁ ⊆ Ico m₂ n₂ := begin simp only [finset.subset_iff, Ico.mem], assume x hx, exact ⟨le_trans hmm hx.1, lt_of_lt_of_le hx.2 hnn⟩ end lemma union_consecutive {n m l : ℕ} (hnm : n ≤ m) (hml : m ≤ l) : Ico n m ∪ Ico m l = Ico n l := by rw [← to_finset, ← to_finset, ← multiset.to_finset_add, multiset.Ico.add_consecutive hnm hml, to_finset] @[simp] lemma inter_consecutive (n m l : ℕ) : Ico n m ∩ Ico m l = ∅ := begin rw [← to_finset, ← to_finset, ← multiset.to_finset_inter, multiset.Ico.inter_consecutive], simp, end lemma disjoint_consecutive (n m l : ℕ) : disjoint (Ico n m) (Ico m l) := le_of_eq $ inter_consecutive n m l @[simp] theorem succ_singleton (n : ℕ) : Ico n (n+1) = {n} := eq_of_veq $ multiset.Ico.succ_singleton theorem succ_top {n m : ℕ} (h : n ≤ m) : Ico n (m + 1) = insert m (Ico n m) := by rw [← to_finset, multiset.Ico.succ_top h, multiset.to_finset_cons, to_finset] theorem succ_top' {n m : ℕ} (h : n < m) : Ico n m = insert (m - 1) (Ico n (m - 1)) := begin have w : m = m - 1 + 1 := (nat.sub_add_cancel (nat.one_le_of_lt h)).symm, conv { to_lhs, rw w }, rw succ_top, exact nat.le_pred_of_lt h end theorem insert_succ_bot {n m : ℕ} (h : n < m) : insert n (Ico (n + 1) m) = Ico n m := by rw [eq_comm, ← to_finset, multiset.Ico.eq_cons h, multiset.to_finset_cons, to_finset] @[simp] theorem pred_singleton {m : ℕ} (h : 0 < m) : Ico (m - 1) m = {m - 1} := eq_of_veq $ multiset.Ico.pred_singleton h @[simp] theorem not_mem_top {n m : ℕ} : m ∉ Ico n m := multiset.Ico.not_mem_top lemma filter_lt_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, x < l) = Ico n m := eq_of_veq $ multiset.Ico.filter_lt_of_top_le hml lemma filter_lt_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, x < l) = ∅ := eq_of_veq $ multiset.Ico.filter_lt_of_le_bot hln lemma filter_lt_of_ge {n m l : ℕ} (hlm : l ≤ m) : (Ico n m).filter (λ x, x < l) = Ico n l := eq_of_veq $ multiset.Ico.filter_lt_of_ge hlm @[simp] lemma filter_lt (n m l : ℕ) : (Ico n m).filter (λ x, x < l) = Ico n (min m l) := eq_of_veq $ multiset.Ico.filter_lt n m l lemma filter_le_of_le_bot {n m l : ℕ} (hln : l ≤ n) : (Ico n m).filter (λ x, l ≤ x) = Ico n m := eq_of_veq $ multiset.Ico.filter_le_of_le_bot hln lemma filter_le_of_top_le {n m l : ℕ} (hml : m ≤ l) : (Ico n m).filter (λ x, l ≤ x) = ∅ := eq_of_veq $ multiset.Ico.filter_le_of_top_le hml lemma filter_le_of_le {n m l : ℕ} (hnl : n ≤ l) : (Ico n m).filter (λ x, l ≤ x) = Ico l m := eq_of_veq $ multiset.Ico.filter_le_of_le hnl @[simp] lemma filter_le (n m l : ℕ) : (Ico n m).filter (λ x, l ≤ x) = Ico (max n l) m := eq_of_veq $ multiset.Ico.filter_le n m l @[simp] lemma diff_left (l n m : ℕ) : (Ico n m) \ (Ico n l) = Ico (max n l) m := by ext k; by_cases n ≤ k; simp [h, and_comm] @[simp] lemma diff_right (l n m : ℕ) : (Ico n m) \ (Ico l m) = Ico n (min m l) := have ∀k, (k < m ∧ (l ≤ k → m ≤ k)) ↔ (k < m ∧ k < l) := assume k, and_congr_right $ assume hk, by rw [← not_imp_not]; simp [hk], by ext k; by_cases n ≤ k; simp [h, this] lemma image_const_sub {k m n : ℕ} (hkn : k ≤ n) : (Ico k m).image (λ j, n - j) = Ico (n + 1 - m) (n + 1 - k) := begin rw [nat.sub_add_comm hkn], ext j, simp only [mem, mem_image, exists_prop, nat.lt_iff_add_one_le, add_le_add_iff_right], split, { rintros ⟨j, ⟨hjk, hjm⟩, rfl⟩, split, { simp only [← nat.add_sub_add_right n 1 j, nat.sub_le_sub_left, hjm] }, { exact nat.sub_le_sub_left _ hjk } }, { rintros ⟨hm, hk⟩, have hj : j ≤ n := le_trans hk (nat.sub_le_self _ _), refine ⟨n - j, ⟨_, _⟩, _⟩, { apply nat.le_sub_right_of_add_le, rwa nat.le_sub_left_iff_add_le hkn at hk }, { rwa [← nat.sub_add_comm hj, nat.sub_le_iff] }, { exact nat.sub_sub_self hj } } end end Ico lemma range_eq_Ico (n : ℕ) : finset.range n = finset.Ico 0 n := by { ext i, simp } lemma range_image_pred_top_sub (n : ℕ) : (finset.range n).image (λ j, n - 1 - j) = finset.range n := begin cases n, { simp }, { simp [range_eq_Ico, Ico.image_const_sub] } end -- TODO We don't yet attempt to reproduce the entire interface for `Ico` for `Ico_ℤ`. /-- `Ico_ℤ l u` is the set of integers `l ≤ k < u`. -/ def Ico_ℤ (l u : ℤ) : finset ℤ := (finset.range (u - l).to_nat).map { to_fun := λ n, n + l, inj' := λ n m h, by simpa using h } @[simp] lemma Ico_ℤ.mem {n m l : ℤ} : l ∈ Ico_ℤ n m ↔ n ≤ l ∧ l < m := begin dsimp [Ico_ℤ], simp only [int.lt_to_nat, exists_prop, mem_range, add_comm, function.embedding.coe_fn_mk, mem_map], split, { rintro ⟨a, ⟨h, rfl⟩⟩, exact ⟨int.le.intro rfl, lt_sub_iff_add_lt'.mp h⟩ }, { rintro ⟨h₁, h₂⟩, use (l - n).to_nat, split; simp [h₁, h₂], } end @[simp] lemma Ico_ℤ.card (l u : ℤ) : (Ico_ℤ l u).card = (u - l).to_nat := by simp [Ico_ℤ] lemma supr_coe [has_Sup β] (f : α → β) (s : finset α) : (⨆ x ∈ (↑s : set α), f x) = ⨆ x ∈ s, f x := rfl lemma infi_coe [has_Inf β] (f : α → β) (s : finset α) : (⨅ x ∈ (↑s : set α), f x) = ⨅ x ∈ s, f x := rfl end finset namespace multiset lemma count_sup [decidable_eq β] (s : finset α) (f : α → multiset β) (b : β) : count b (s.sup f) = s.sup (λa, count b (f a)) := begin letI := classical.dec_eq α, refine s.induction _ _, { exact count_zero _ }, { assume i s his ih, rw [finset.sup_insert, sup_eq_union, count_union, finset.sup_insert, ih], refl } end end multiset namespace list variable [decidable_eq α] theorem to_finset_card_of_nodup {l : list α} (h : l.nodup) : l.to_finset.card = l.length := congr_arg card $ (@multiset.erase_dup_eq_self α _ l).2 h end list section lattice variables {ι : Sort*} [complete_lattice α] lemma supr_eq_supr_finset (s : ι → α) : (⨆i, s i) = (⨆t:finset (plift ι), ⨆i∈t, s (plift.down i)) := begin classical, exact le_antisymm (supr_le $ assume b, le_supr_of_le {plift.up b} $ le_supr_of_le (plift.up b) $ le_supr_of_le (by simp) $ le_refl _) (supr_le $ assume t, supr_le $ assume b, supr_le $ assume hb, le_supr _ _) end lemma infi_eq_infi_finset (s : ι → α) : (⨅i, s i) = (⨅t:finset (plift ι), ⨅i∈t, s (plift.down i)) := begin classical, exact le_antisymm (le_infi $ assume t, le_infi $ assume b, le_infi $ assume hb, infi_le _ _) (le_infi $ assume b, infi_le_of_le {plift.up b} $ infi_le_of_le (plift.up b) $ infi_le_of_le (by simp) $ le_refl _) end end lattice namespace set variables {ι : Sort*} lemma Union_eq_Union_finset (s : ι → set α) : (⋃i, s i) = (⋃t:finset (plift ι), ⋃i∈t, s (plift.down i)) := supr_eq_supr_finset s lemma Inter_eq_Inter_finset (s : ι → set α) : (⋂i, s i) = (⋂t:finset (plift ι), ⋂i∈t, s (plift.down i)) := infi_eq_infi_finset s end set namespace finset namespace nat /-- The antidiagonal of a natural number `n` is the finset of pairs `(i,j)` such that `i+j = n`. -/ def antidiagonal (n : ℕ) : finset (ℕ × ℕ) := (multiset.nat.antidiagonal n).to_finset /-- A pair (i,j) is contained in the antidiagonal of `n` if and only if `i+j=n`. -/ @[simp] lemma mem_antidiagonal {n : ℕ} {x : ℕ × ℕ} : x ∈ antidiagonal n ↔ x.1 + x.2 = n := by rw [antidiagonal, multiset.mem_to_finset, multiset.nat.mem_antidiagonal] /-- The cardinality of the antidiagonal of `n` is `n+1`. -/ @[simp] lemma card_antidiagonal (n : ℕ) : (antidiagonal n).card = n+1 := by simpa using list.to_finset_card_of_nodup (list.nat.nodup_antidiagonal n) /-- The antidiagonal of `0` is the list `[(0,0)]` -/ @[simp] lemma antidiagonal_zero : antidiagonal 0 = {(0, 0)} := by { rw [antidiagonal, multiset.nat.antidiagonal_zero], refl } end nat end finset namespace finset /-! ### bUnion -/ @[simp] theorem bUnion_singleton (a : α) (s : α → set β) : (⋃ x ∈ ({a} : finset α), s x) = s a := by { simp only [set.Union, ← supr_coe], rw coe_singleton, exact supr_singleton } variables [decidable_eq α] theorem supr_union {α} [complete_lattice α] {β} [decidable_eq β] {f : β → α} {s t : finset β} : (⨆ x ∈ s ∪ t, f x) = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) := calc (⨆ x ∈ s ∪ t, f x) = (⨆ x, (⨆h : x∈s, f x) ⊔ (⨆h : x∈t, f x)) : congr_arg _ $ funext $ λ x, by { convert supr_or, rw finset.mem_union, rw finset.mem_union, refl, refl } ... = (⨆x∈s, f x) ⊔ (⨆x∈t, f x) : supr_sup_eq lemma bUnion_union (s t : finset α) (u : α → set β) : (⋃ x ∈ s ∪ t, u x) = (⋃ x ∈ s, u x) ∪ (⋃ x ∈ t, u x) := supr_union @[simp] lemma bUnion_insert (a : α) (s : finset α) (t : α → set β) : (⋃ x ∈ insert a s, t x) = t a ∪ (⋃ x ∈ s, t x) := begin rw insert_eq, simp only [bUnion_union, finset.bUnion_singleton] end end finset
566bdfd24928b65552f170ba7be06013ba51278d
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/library/theories/group_theory/finsubg.lean
f8ff6d382f9bef2b3a93a9499c0d275529a68a02
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
20,597
lean
/- Copyright (c) 2015 Haitao Zhang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author : Haitao Zhang -/ -- develop the concept of finite subgroups based on finsets so that the properties -- can be used directly without translating from the set based theory first import data algebra.group .subgroup open function finset -- ⁻¹ in eq.ops conflicts with group ⁻¹ open eq.ops namespace group_theory open ops section subg -- we should be able to prove properties using finsets directly variables {G : Type} [group G] variable [decidable_eq G] attribute [reducible] definition finset_mul_closed_on (H : finset G) : Prop := ∀ x y : G, x ∈ H → y ∈ H → x * y ∈ H definition finset_has_inv (H : finset G) : Prop := ∀ a : G, a ∈ H → a⁻¹ ∈ H structure is_finsubg [class] (H : finset G) : Type := (has_one : 1 ∈ H) (mul_closed : finset_mul_closed_on H) (has_inv : finset_has_inv H) attribute [instance] definition univ_is_finsubg [finG : fintype G] : is_finsubg (@finset.univ G _) := is_finsubg.mk !mem_univ (λ x y Px Py, !mem_univ) (λ a Pa, !mem_univ) attribute [instance] definition one_is_finsubg : is_finsubg ('{(1:G)}) := is_finsubg.mk !mem_singleton (λ x y Px Py, by rewrite [eq_of_mem_singleton Px, eq_of_mem_singleton Py, one_mul]; apply mem_singleton) (λ x Px, by rewrite [eq_of_mem_singleton Px, one_inv]; apply mem_singleton) lemma finsubg_has_one (H : finset G) [h : is_finsubg H] : 1 ∈ H := @is_finsubg.has_one G _ _ H h lemma finsubg_mul_closed (H : finset G) [h : is_finsubg H] {x y : G} : x ∈ H → y ∈ H → x * y ∈ H := @is_finsubg.mul_closed G _ _ H h x y lemma finsubg_has_inv (H : finset G) [h : is_finsubg H] {a : G} : a ∈ H → a⁻¹ ∈ H := @is_finsubg.has_inv G _ _ H h a attribute [instance] definition finsubg_to_subg {H : finset G} [h : is_finsubg H] : is_subgroup (ts H) := is_subgroup.mk (mem_eq_mem_to_set H 1 ▸ finsubg_has_one H) (take x y, begin repeat rewrite -mem_eq_mem_to_set, apply finsubg_mul_closed H end) (take a, begin repeat rewrite -mem_eq_mem_to_set, apply finsubg_has_inv H end) open nat lemma finsubg_eq_singleton_one_of_card_one {H : finset G} [h : is_finsubg H] : card H = 1 → H = '{1} := assume Pcard, eq.symm (eq_of_card_eq_of_subset (by rewrite [Pcard]) (subset_of_forall take g, by rewrite [mem_singleton_iff]; intro Pg; rewrite Pg; exact finsubg_has_one H)) end subg section fin_lcoset open set variables {A : Type} [decidable_eq A] [group A] definition fin_lcoset (H : finset A) (a : A) := finset.image (lmul_by a) H definition fin_rcoset (H : finset A) (a : A) : finset A := image (rmul_by a) H definition fin_lcosets (H G : finset A) := image (fin_lcoset H) G definition fin_inv : finset A → finset A := image inv variable {H : finset A} lemma lmul_rmul {a b : A} : (lmul_by a) ∘ (rmul_by b) = (rmul_by b) ∘ (lmul_by a) := funext take c, calc a*(c*b) = (a*c)*b : mul.assoc lemma fin_lrcoset_comm {a b : A} : fin_lcoset (fin_rcoset H b) a = fin_rcoset (fin_lcoset H a) b := by esimp [fin_lcoset, fin_rcoset]; rewrite [-*image_comp, lmul_rmul] lemma inv_mem_fin_inv {a : A} : a ∈ H → a⁻¹ ∈ fin_inv H := assume Pin, mem_image Pin rfl lemma fin_lcoset_eq (a : A) : ts (fin_lcoset H a) = a ∘> (ts H) := calc ts (fin_lcoset H a) = coset.l a (ts H) : to_set_image ... = a ∘> (ts H) : glcoset_eq_lcoset lemma fin_lcoset_id : fin_lcoset H 1 = H := by rewrite [eq_eq_to_set_eq, fin_lcoset_eq, glcoset_id] lemma fin_lcoset_compose (a b : A) : fin_lcoset (fin_lcoset H b) a = fin_lcoset H (a*b) := to_set.inj (by rewrite [*fin_lcoset_eq, glcoset_compose]) lemma fin_lcoset_inv (a : A) : fin_lcoset (fin_lcoset H a) a⁻¹ = H := to_set.inj (by rewrite [*fin_lcoset_eq, glcoset_inv]) lemma fin_lcoset_card (a : A) : card (fin_lcoset H a) = card H := card_image_eq_of_inj_on (lmul_inj_on a (ts H)) lemma fin_lcosets_card_eq {G : finset A} : ∀ gH, gH ∈ fin_lcosets H G → card gH = card H := take gH, assume Pcosets, obtain g Pg, from exists_of_mem_image Pcosets, and.right Pg ▸ fin_lcoset_card g variable [is_finsubgH : is_finsubg H] include is_finsubgH lemma fin_lcoset_same (x a : A) : x ∈ (fin_lcoset H a) = (fin_lcoset H x = fin_lcoset H a) := begin rewrite mem_eq_mem_to_set, rewrite [eq_eq_to_set_eq, *(fin_lcoset_eq x), fin_lcoset_eq a], exact (subg_lcoset_same x a) end lemma fin_mem_lcoset (g : A) : g ∈ fin_lcoset H g := have P : g ∈ g ∘> ts H, from and.left (subg_in_coset_refl g), have P1 : g ∈ ts (fin_lcoset H g), from eq.symm (fin_lcoset_eq g) ▸ P, eq.symm (mem_eq_mem_to_set _ g) ▸ P1 lemma fin_lcoset_subset {S : finset A} (Psub : S ⊆ H) : ∀ x, x ∈ H → fin_lcoset S x ⊆ H := have Psubs : set.subset (ts S) (ts H), from subset_eq_to_set_subset S H ▸ Psub, take x, assume Pxs : x ∈ ts H, have Pcoset : set.subset (x ∘> ts S) (ts H), from subg_lcoset_subset_subg Psubs x Pxs, by rewrite [subset_eq_to_set_subset, fin_lcoset_eq x]; exact Pcoset lemma finsubg_lcoset_id {a : A} : a ∈ H → fin_lcoset H a = H := by rewrite [eq_eq_to_set_eq, fin_lcoset_eq, mem_eq_mem_to_set]; apply subgroup_lcoset_id lemma finsubg_inv_lcoset_eq_rcoset {a : A} : fin_inv (fin_lcoset H a) = fin_rcoset H a⁻¹ := begin esimp [fin_inv, fin_lcoset, fin_rcoset], rewrite [-image_comp], apply ext, intro b, rewrite [*mem_image_iff, ↑comp, ↑lmul_by, ↑rmul_by], apply iff.intro, intro Pl, cases Pl with h Ph, cases Ph with Pin Peq, existsi h⁻¹, apply and.intro, exact finsubg_has_inv H Pin, rewrite [-mul_inv, Peq], intro Pr, cases Pr with h Ph, cases Ph with Pin Peq, existsi h⁻¹, apply and.intro, exact finsubg_has_inv H Pin, rewrite [mul_inv, inv_inv, Peq], end lemma finsubg_conj_closed {g h : A} : g ∈ H → h ∈ H → g ∘c h ∈ H := assume Pgin Phin, finsubg_mul_closed H (finsubg_mul_closed H Pgin Phin) (finsubg_has_inv H Pgin) variable {G : finset A} variable [is_finsubgG : is_finsubg G] include is_finsubgG open finset.partition definition fin_lcoset_partition_subg (Psub : H ⊆ G) := partition.mk G (fin_lcoset H) fin_lcoset_same (restriction_imp_union (fin_lcoset H) fin_lcoset_same (fin_lcoset_subset Psub)) open nat theorem lagrange_theorem (Psub : H ⊆ G) : card G = card (fin_lcosets H G) * card H := calc card G = finset.Sum (fin_lcosets H G) card : class_equation (fin_lcoset_partition_subg Psub) ... = finset.Sum (fin_lcosets H G) (λ x, card H) : finset.Sum_ext (take g P, fin_lcosets_card_eq g P) ... = card (fin_lcosets H G) * card H : Sum_const_eq_card_mul end fin_lcoset section open fintype list subtype lemma dinj_tag {A : Type} (P : A → Prop) : dinj P tag := take a₁ a₂ Pa₁ Pa₂ Pteq, subtype.no_confusion Pteq (λ Pe Pqe, Pe) open nat lemma card_pos {A : Type} [ambientA : group A] [finA : fintype A] : 0 < card A := length_pos_of_mem (mem_univ 1) end section lcoset_fintype open fintype list subtype variables {A : Type} [group A] [fintype A] [decidable_eq A] variables G H : finset A attribute [reducible] definition is_fin_lcoset (S : finset A) : Prop := ∃ g, g ∈ G ∧ fin_lcoset H g = S definition to_list : list A := list.filter (λ g, g ∈ G) (elems A) definition list_lcosets : list (finset A) := erase_dup (map (fin_lcoset H) (to_list G)) attribute [reducible] definition lcoset_type : Type := {S : finset A | is_fin_lcoset G H S} definition all_lcosets : list (lcoset_type G H) := dmap (is_fin_lcoset G H) tag (list_lcosets G H) variables {G H} [finsubgG : is_finsubg G] include finsubgG lemma self_is_lcoset : is_fin_lcoset G H H := exists.intro 1 (and.intro !finsubg_has_one fin_lcoset_id) lemma lcoset_subset_of_subset (J : lcoset_type G H) : H ⊆ G → elt_of J ⊆ G := assume Psub, obtain j Pjin Pj, from has_property J, by rewrite [-Pj]; apply fin_lcoset_subset Psub; exact Pjin variables (G H) definition lcoset_one : lcoset_type G H := tag H self_is_lcoset variables {G H} definition lcoset_lmul {g : A} (Pgin : g ∈ G) (S : lcoset_type G H) : lcoset_type G H := tag (fin_lcoset (elt_of S) g) (obtain f Pfin Pf, from has_property S, exists.intro (g*f) (by apply and.intro; exact finsubg_mul_closed G Pgin Pfin; rewrite [-Pf, -fin_lcoset_compose])) definition lcoset_mul (S₁ S₂ : lcoset_type G H): finset A := Union (elt_of S₁) (fin_lcoset (elt_of S₂)) lemma mul_mem_lcoset_mul (J K : lcoset_type G H) {g h} : g ∈ elt_of J → h ∈ elt_of K → g*h ∈ lcoset_mul J K := assume Pg, begin rewrite [↑lcoset_mul, mem_Union_iff, ↑fin_lcoset], intro Ph, existsi g, apply and.intro, exact Pg, rewrite [mem_image_iff, ↑lmul_by], existsi h, exact and.intro Ph rfl end lemma is_lcoset_of_mem_list_lcosets {S : finset A} : S ∈ list_lcosets G H → is_fin_lcoset G H S := assume Pin, obtain g Pgin Pg, from exists_of_mem_map (mem_of_mem_erase_dup Pin), exists.intro g (and.intro (of_mem_filter Pgin) Pg) lemma mem_list_lcosets_of_is_lcoset {S : finset A} : is_fin_lcoset G H S → S ∈ list_lcosets G H := assume Plcoset, obtain g Pgin Pg, from Plcoset, Pg ▸ mem_erase_dup (mem_map _ (mem_filter_of_mem (complete g) Pgin)) lemma fin_lcosets_eq : fin_lcosets H G = to_finset_of_nodup (list_lcosets G H) !nodup_erase_dup := ext (take S, iff.intro (λ Pimg, mem_list_lcosets_of_is_lcoset (exists_of_mem_image Pimg)) (λ Pl, obtain g Pg, from is_lcoset_of_mem_list_lcosets Pl, iff.elim_right !mem_image_iff (is_lcoset_of_mem_list_lcosets Pl))) lemma length_all_lcosets : length (all_lcosets G H) = card (fin_lcosets H G) := eq.trans (show length (all_lcosets G H) = length (list_lcosets G H), from have Pmap : map elt_of (all_lcosets G H) = list_lcosets G H, from map_dmap_of_inv_of_pos (λ S P, rfl) (λ S, is_lcoset_of_mem_list_lcosets), by rewrite[-Pmap, length_map]) (by rewrite fin_lcosets_eq) lemma lcoset_lmul_compose {f g : A} (Pf : f ∈ G) (Pg : g ∈ G) (S : lcoset_type G H) : lcoset_lmul Pf (lcoset_lmul Pg S) = lcoset_lmul (finsubg_mul_closed G Pf Pg) S := subtype.eq !fin_lcoset_compose lemma lcoset_lmul_one (S : lcoset_type G H) : lcoset_lmul !finsubg_has_one S = S := subtype.eq fin_lcoset_id lemma lcoset_lmul_inv {g : A} {Pg : g ∈ G} (S : lcoset_type G H) : lcoset_lmul (finsubg_has_inv G Pg) (lcoset_lmul Pg S) = S := subtype.eq (to_set.inj begin esimp [lcoset_lmul], rewrite [fin_lcoset_compose, mul.left_inv, fin_lcoset_eq, glcoset_id] end) lemma lcoset_lmul_inj {g : A} {Pg : g ∈ G}: @injective (lcoset_type G H) _ (lcoset_lmul Pg) := injective_of_has_left_inverse (exists.intro (lcoset_lmul (finsubg_has_inv G Pg)) lcoset_lmul_inv) lemma card_elt_of_lcoset_type (S : lcoset_type G H) : card (elt_of S) = card H := obtain f Pfin Pf, from has_property S, Pf ▸ fin_lcoset_card f attribute [instance] definition lcoset_fintype : fintype (lcoset_type G H) := fintype.mk (all_lcosets G H) (dmap_nodup_of_dinj (dinj_tag (is_fin_lcoset G H)) !nodup_erase_dup) (take s, subtype.destruct s (take S, assume PS, mem_dmap PS (mem_list_lcosets_of_is_lcoset PS))) lemma card_lcoset_type : card (lcoset_type G H) = card (fin_lcosets H G) := length_all_lcosets open nat variable [finsubgH : is_finsubg H] include finsubgH theorem lagrange_theorem' (Psub : H ⊆ G) : card G = card (lcoset_type G H) * card H := calc card G = card (fin_lcosets H G) * card H : lagrange_theorem Psub ... = card (lcoset_type G H) * card H : card_lcoset_type lemma lcoset_disjoint {S₁ S₂ : lcoset_type G H} : S₁ ≠ S₂ → elt_of S₁ ∩ elt_of S₂ = ∅ := obtain f₁ Pfin₁ Pf₁, from has_property S₁, obtain f₂ Pfin₂ Pf₂, from has_property S₂, assume Pne, inter_eq_empty_of_disjoint (disjoint.intro take g, begin rewrite [-Pf₁, -Pf₂, *fin_lcoset_same], intro Pgf₁, rewrite [Pgf₁, Pf₁, Pf₂], intro Peq, exact absurd (subtype.eq Peq) Pne end ) lemma card_Union_lcosets (lcs : finset (lcoset_type G H)) : card (Union lcs elt_of) = card lcs * card H := calc card (Union lcs elt_of) = ∑ lc ∈ lcs, card (elt_of lc) : card_Union_of_disjoint lcs elt_of (λ (S₁ S₂ : lcoset_type G H) P₁ P₂ Pne, lcoset_disjoint Pne) ... = ∑ lc ∈ lcs, card H : Sum_ext (take lc P, card_elt_of_lcoset_type _) ... = card lcs * card H : Sum_const_eq_card_mul lemma exists_of_lcoset_type (J : lcoset_type G H) : ∃ j, j ∈ elt_of J ∧ fin_lcoset H j = elt_of J := obtain j Pjin Pj, from has_property J, exists.intro j (and.intro (Pj ▸ !fin_mem_lcoset) Pj) lemma lcoset_not_empty (J : lcoset_type G H) : elt_of J ≠ ∅ := obtain j Pjin Pj, from has_property J, assume Pempty, absurd (by rewrite [-Pempty, -Pj]; apply fin_mem_lcoset) (not_mem_empty j) end lcoset_fintype section normalizer open subtype variables {G : Type} [ambientG : group G] [finG : fintype G] [deceqG : decidable_eq G] include ambientG deceqG finG variable H : finset G definition normalizer : finset G := {g ∈ univ | ∀ h, h ∈ H → g ∘c h ∈ H} variable {H} variable [finsubgH : is_finsubg H] include finsubgH lemma subset_normalizer : H ⊆ normalizer H := subset_of_forall take g, assume PginH, mem_sep_of_mem !mem_univ (take h, assume PhinH, finsubg_conj_closed PginH PhinH) lemma normalizer_has_one : 1 ∈ normalizer H := mem_of_subset_of_mem subset_normalizer (finsubg_has_one H) lemma normalizer_mul_closed : finset_mul_closed_on (normalizer H) := take f g, assume Pfin Pgin, mem_sep_of_mem !mem_univ take h, assume Phin, begin rewrite [-conj_compose], apply of_mem_sep Pfin, apply of_mem_sep Pgin, exact Phin end lemma conj_eq_of_mem_normalizer {g : G} : g ∈ normalizer H → image (conj_by g) H = H := assume Pgin, eq_of_card_eq_of_subset (card_image_eq_of_inj_on (take h j, assume P1 P2, !conj_inj)) (subset_of_forall take h, assume Phin, obtain j Pjin Pj, from exists_of_mem_image Phin, begin substvars, apply of_mem_sep Pgin, exact Pjin end) lemma normalizer_has_inv : finset_has_inv (normalizer H) := take g, assume Pgin, mem_sep_of_mem !mem_univ take h, begin rewrite [-(conj_eq_of_mem_normalizer Pgin) at {1}, mem_image_iff], intro Pex, cases Pex with k Pk, rewrite [-(and.right Pk), conj_compose, mul.left_inv, conj_id], exact and.left Pk end attribute [instance] definition normalizer_is_finsubg : is_finsubg (normalizer H) := is_finsubg.mk normalizer_has_one normalizer_mul_closed normalizer_has_inv lemma lcoset_subset_normalizer (J : lcoset_type (normalizer H) H) : elt_of J ⊆ normalizer H := lcoset_subset_of_subset J subset_normalizer lemma lcoset_subset_normalizer_of_mem {g : G} : g ∈ normalizer H → fin_lcoset H g ⊆ normalizer H := assume Pgin, fin_lcoset_subset subset_normalizer g Pgin lemma lrcoset_same_of_mem_normalizer {g : G} : g ∈ normalizer H → fin_lcoset H g = fin_rcoset H g := assume Pg, ext take h, iff.intro (assume Pl, obtain j Pjin Pj, from exists_of_mem_image Pl, mem_image (of_mem_sep Pg j Pjin) (calc g*j*g⁻¹*g = g*j : inv_mul_cancel_right ... = h : Pj)) (assume Pr, obtain j Pjin Pj, from exists_of_mem_image Pr, mem_image (of_mem_sep (finsubg_has_inv (normalizer H) Pg) j Pjin) (calc g*(g⁻¹*j*g⁻¹⁻¹) = g*(g⁻¹*j*g) : inv_inv ... = g*(g⁻¹*(j*g)) : mul.assoc ... = j*g : mul_inv_cancel_left ... = h : Pj)) lemma lcoset_mul_eq_lcoset (J K : lcoset_type (normalizer H) H) {g : G} : g ∈ elt_of J → (lcoset_mul J K) = fin_lcoset (elt_of K) g := assume Pgin, obtain j Pjin Pj, from has_property J, obtain k Pkin Pk, from has_property K, Union_const (lcoset_not_empty J) begin rewrite [-Pk], intro h Phin, have Phinn : h ∈ normalizer H, begin apply mem_of_subset_of_mem (lcoset_subset_normalizer_of_mem Pjin), rewrite Pj, assumption end, revert Phin Pgin, rewrite [-Pj, *fin_lcoset_same], intro Pheq Pgeq, rewrite [*(lrcoset_same_of_mem_normalizer Pkin), *fin_lrcoset_comm, Pheq, Pgeq] end lemma lcoset_mul_is_lcoset (J K : lcoset_type (normalizer H) H) : is_fin_lcoset (normalizer H) H (lcoset_mul J K) := obtain j Pjin Pj, from has_property J, obtain k Pkin Pk, from has_property K, exists.intro (j*k) (and.intro (finsubg_mul_closed _ Pjin Pkin) begin rewrite [lcoset_mul_eq_lcoset J K (Pj ▸ fin_mem_lcoset j), -fin_lcoset_compose, Pk] end) lemma lcoset_inv_is_lcoset (J : lcoset_type (normalizer H) H) : is_fin_lcoset (normalizer H) H (fin_inv (elt_of J)) := obtain j Pjin Pj, from has_property J, exists.intro j⁻¹ begin rewrite [-Pj, finsubg_inv_lcoset_eq_rcoset], apply and.intro, apply normalizer_has_inv, assumption, apply lrcoset_same_of_mem_normalizer, apply normalizer_has_inv, assumption end definition fin_coset_mul (J K : lcoset_type (normalizer H) H) : lcoset_type (normalizer H) H := tag (lcoset_mul J K) (lcoset_mul_is_lcoset J K) definition fin_coset_inv (J : lcoset_type (normalizer H) H) : lcoset_type (normalizer H) H := tag (fin_inv (elt_of J)) (lcoset_inv_is_lcoset J) definition fin_coset_one : lcoset_type (normalizer H) H := tag H self_is_lcoset local infix `^` := fin_coset_mul lemma fin_coset_mul_eq_lcoset (J K : lcoset_type (normalizer H) H) {g : G} : g ∈ (elt_of J) → elt_of (J ^ K) = fin_lcoset (elt_of K) g := assume Pgin, lcoset_mul_eq_lcoset J K Pgin lemma fin_coset_mul_assoc (J K L : lcoset_type (normalizer H) H) : J ^ K ^ L = J ^ (K ^ L) := obtain j Pjin Pj, from exists_of_lcoset_type J, obtain k Pkin Pk, from exists_of_lcoset_type K, have Pjk : j*k ∈ elt_of (J ^ K), from mul_mem_lcoset_mul J K Pjin Pkin, obtain l Plin Pl, from has_property L, subtype.eq (begin rewrite [fin_coset_mul_eq_lcoset (J ^ K) _ Pjk, fin_coset_mul_eq_lcoset J _ Pjin, fin_coset_mul_eq_lcoset K _ Pkin, -Pl, *fin_lcoset_compose] end) lemma fin_coset_mul_one (J : lcoset_type (normalizer H) H) : J ^ fin_coset_one = J := obtain j Pjin Pj, from exists_of_lcoset_type J, subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ Pjin, -Pj] end lemma fin_coset_one_mul (J : lcoset_type (normalizer H) H) : fin_coset_one ^ J = J := subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ (finsubg_has_one H), fin_lcoset_id] end lemma fin_coset_left_inv (J : lcoset_type (normalizer H) H) : (fin_coset_inv J) ^ J = fin_coset_one := obtain j Pjin Pj, from exists_of_lcoset_type J, have Pjinv : j⁻¹ ∈ elt_of (fin_coset_inv J), from inv_mem_fin_inv Pjin, subtype.eq begin rewrite [↑fin_coset_one, fin_coset_mul_eq_lcoset _ _ Pjinv, -Pj, fin_lcoset_inv] end variable (H) attribute [instance] definition fin_coset_group : group (lcoset_type (normalizer H) H) := group.mk fin_coset_mul fin_coset_mul_assoc fin_coset_one fin_coset_one_mul fin_coset_mul_one fin_coset_inv fin_coset_left_inv variables {H} (Hc : finset (lcoset_type (normalizer H) H)) definition fin_coset_Union : finset G := Union Hc elt_of variables {Hc} [finsubgHc : is_finsubg Hc] include finsubgHc lemma mem_normalizer_of_mem_fcU {j : G} : j ∈ fin_coset_Union Hc → j ∈ normalizer H := assume Pjin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, mem_of_subset_of_mem !lcoset_subset_normalizer PjJ lemma fcU_has_one : (1:G) ∈ fin_coset_Union Hc := iff.elim_right (mem_Union_iff Hc elt_of (1:G)) (exists.intro 1 (and.intro (finsubg_has_one Hc) (finsubg_has_one H))) lemma fcU_has_inv : finset_has_inv (fin_coset_Union Hc) := take j, assume Pjin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, have PJinv : J⁻¹ ∈ Hc, from finsubg_has_inv Hc PJ, have Pjinv : j⁻¹ ∈ elt_of J⁻¹, from inv_mem_fin_inv PjJ, iff.elim_right !mem_Union_iff (exists.intro J⁻¹ (and.intro PJinv Pjinv)) lemma fcU_mul_closed : finset_mul_closed_on (fin_coset_Union Hc) := take j k, assume Pjin Pkin, obtain J PJ PjJ, from iff.elim_left !mem_Union_iff Pjin, obtain K PK PkK, from iff.elim_left !mem_Union_iff Pkin, have Pjk : j*k ∈ elt_of (J*K), from mul_mem_lcoset_mul J K PjJ PkK, iff.elim_right !mem_Union_iff (exists.intro (J*K) (and.intro (finsubg_mul_closed Hc PJ PK) Pjk)) attribute [instance] definition fcU_is_finsubg : is_finsubg (fin_coset_Union Hc) := is_finsubg.mk fcU_has_one fcU_mul_closed fcU_has_inv end normalizer end group_theory
48309c0bfc829da3cdc273e4a0c319732aa1eb77
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world1/level1.lean
b80c76c8f47bcca7602b2cc05dc548599d8dbb30
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
5,676
lean
import mynat.definition -- imports the natural numbers {0,1,2,3,4,...}. import mynat.add -- imports definition of addition on the natural numbers. import mynat.mul -- imports definition of multiplication on the natural numbers. namespace mynat -- hide -- World name : Tutorial world /- # The Natural Number Game, version 1.08beta [see "what's new" below] [will be ready to go after some updates to the engine which generates the web pages] ## By Kevin Buzzard and Mohammad Pedramfar. Special thanks to Rob Lewis for tactic hackery, Sian Carey for power world (world 4), and, last but not least, all the people who fed back comments, including the 2019-20 Imperial College 1st year maths beta tester students, Marie-Amélie Lawn, Toby Gee, Joseph Myers, and all the people who have been in touch via the <a href="https://leanprover.zulipchat.com/" target="blank">Lean Zulip chat</a> or the <a href="https://xenaproject.wordpress.com/" target="blank">Xena Project blog</a> or via <a href="https://twitter.com/home" target="blank">Twitter</a>. The natural number game is brought to you by the Xena project, a project based at Imperial College London whose aim is to get mathematics undergraduates using computer theorem provers. Lean is a computer theorem prover being developed at Microsoft Research. ## What's new? What's new: function world (world 5) and proposition world (world 6). Can be attempted straight after world 2 (world 5 before world 6 though). New tactics `exact`, `intro`, `have` and `apply`. # What is this game? Welcome to the natural number game -- a game which shows the power of induction. In this game, you get own version of the natural numbers, called `mynat`, in a programming language called Lean. Your version of the natural numbers satisfies something called the principle of mathematical induction, and a couple of other things too (Peano's axioms). Unfortunately, nobody has proved any theorems about these natural numbers yet. For example, addition will be defined for you, but nobody has proved that `x + y = y + x` yet. This is your job. You're going to prove mathematical theorems using the Lean theorem prover. In other words, you're going to solve levels in a computer game. You're going to prove these theorems using *tactics*. This introductory world, Tutorial World, will take you through some of these tactics. During your proofs, your "goal" (i.e. what you're supposed to be proving) will be displayed with a `⊢` symbol in front of it. If the top right hand box reports "Theorem Proved!", you have closed all the goals in the level and can move on to the next level in the world you're in. # World 1: Tutorial World ## Level 1: the `refl` tactic. Let's start with the `refl` tactic. `refl` stands for "reflexivity", which is a fancy way of saying that it will prove any goal of the form `A = A`. It doesn't matter how complicated `A` is, all that matters is that the left hand side is *exactly equal* to the right hand side (a computer scientist would say "definitionally equal"). I really mean "press the same buttons on your computer in the same order" equal. For example, `x * y + z = x * y + z` can be proved by `refl`, but `x + y = y + x` cannot. Let's see `refl` in action! At the bottom of the text in this box, there's a lemma, which says that if $x$, $y$ and $z$ are natural numbers then $xy + z = xy + z$. Locate this lemma (if you can't see the lemma and these instructions at the same time, make this box wider by dragging the sides). Let's supply the proof. Click on the word `sorry` and then delete it. When the system finishes being busy, you'll be able to see your goal -- the objective of this level -- in the box on the top right. Remember that the goal is the thing with the weird `⊢` thing just before it. The goal in this case is `x * y + z = x * y + z`, where `x`, `y` and `z` are some of your very own natural numbers. That's a pretty easy goal to prove -- you can just prove it with the `refl` tactic. Where it used to say `sorry`, write `refl,` **and don't forget the comma**. Then hit enter to go onto the next line. If all is well, Lean should tell you "Proof complete!" in the top right box, and there should be no errors in the bottom right box. You just did the first level of the tutorial! And you also learnt how to avoid by *far* the most common mistake that beginner users make -- **every line must end with a comma**. If things go weird and you don't understand why the top right box is empty, check for missing commas. Also check you've spelt `refl` correctly: it's REFL for "reflexivity". For each level, the idea is to get Lean into this state: with the top right box saying "Proof complete!" and the bottom right box empty (i.e. with no errors in). If you want to be reminded about the `refl` tactic, you can click on the "Tactics" drop down menu on the left. Resize the window if it's too small. Now click on "next level" in the top right of your browser to go onto the second level of tutorial world, where we'll learn about the `rw` tactic. [NB don't click on "next world", we're not ready for addition world yet] -/ /- Lemma : no-side-bar For all natural numbers $x$, $y$ and $z$, we have $xy + z = xy + z$. -/ lemma example1 (x y z : mynat) : x * y + z = x * y + z := begin [less_leaky] refl end /- Tactic : refl The `refl` tactic will close any goal of the form `A = B` where `A` and `B` are *exactly the same thing*. ### Example: If it looks like this in the top right hand box: ``` a b c d : mynat ⊢ (a + b) * (c + d) = (a + b) * (c + d) ``` then `refl,` will close the goal and solve the level. Don't forget the comma. -/ end mynat -- hide
7ad2e4ba28f5e70bd5888a73ccdceb6acc25ec41
2bafba05c98c1107866b39609d15e849a4ca2bb8
/src/week_6/solutions/Part_A_tendsto.lean
beff65d050643ffd7a9c9b59ccb0e3c43888cd66
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/formalising-mathematics
b54c83c94b5c315024ff09997fcd6b303892a749
7cf1d51c27e2038d2804561d63c74711924044a1
refs/heads/master
1,651,267,046,302
1,638,888,459,000
1,638,888,459,000
331,592,375
284
24
Apache-2.0
1,669,593,705,000
1,611,224,849,000
Lean
UTF-8
Lean
false
false
12,765
lean
import order.filter.basic /- # tendsto If `X` and `Y` are types, `φ : X → Y` is a function, and `F : filter X` and `G : filter Y` are filters, then `filter.tendsto φ F G` is a true-false statement, which is pronounced something like "`F` tends to `G` along `φ`". Of course we will `open filter` in this file, so you can just write `tendsto φ F G`, or if you like the dot notation you can even write `F.tendsto φ G`. ## Geometric meaning of `tendsto`. Let's start by thinking about the easy case where `F` and `G` are actually subsets of `X` and `Y` (that is, principal filters, associated to sets which we will also call `F` and `G`). In this case, `tendsto φ F G` simply means "`φ` restricts to a function from `F` to `G`", or in other words `∀ x ∈ F, φ(x) ∈ G`. There are two other ways of writing this predicate. The first involves pushing a set forward along a map. If `F` is a subset of `X` then let `φ(F)` denote the image of `F` under `φ`, that is, the subset `{y : Y | ∃ x : X, φ x = y}` of `Y`. Then `tendsto φ F G` simply means `φ(F) ⊆ G`. The second involves pulling a set back along a map. If `G` is a subset of `Y` then let `φ⁻¹(G)` denote the preimage of `G` under `φ`, that is, the subset `{x : X | φ x ∈ G}` of `Y`. Then `tendsto φ F G` simply means `F ⊆ φ⁻¹(G)`. This is how it all works in the case of sets. What we need to do today is to figure out how to push forward and pull back filters along a map `φ`. Once we have done this, then we can prove `φ(F) ≤ G ↔ F ≤ φ⁻¹(G)` and use either one of these as our definition of `tendsto φ F G` -- it doesn't matter which. ## Digression : adjoint functors. The discussion below is not needed to be able to do this week's problems, but it might provide some helpful background for some. Also note that anyone who still doens't like the word "type" can literally just change it for the word "set" (and change "term of type" to "element of set"), which is how arguments of the below kind would appear in the traditional mathematical literature. Partially ordered types, such as the type of subsets of a fixed type `X` or the type of filters on `X`, are actually very simple examples of categories. In general if `P` is a partially ordered type and `x,y` are terms of type `P` then the idea is that we can define `Hom(x,y)` to have exactly one element if `x ≤ y` is true, and no elements at all if `x ≤ y` is false. The structure/axioms for a category are that `Hom(x,x)` is supposed to have an identity element, which follows from reflexivity of `≤`, and that one can compose morphisms, which follows from transitivity of `≤`. Antisymmetry states that if two objects are isomorphic (i.e., in this case, if `Hom(x,y)` and `Hom(y,x)` are both nonempty), then they are equal. If `φ : X → Y` is a map of types, then pushing forward subsets and pulling back subsets are both functors from `set X` to `set Y`, because `S ⊆ T → φ(S) ⊆ φ(T)` and `U ⊆ V → φ⁻¹(U) ⊆ φ⁻¹(V)`. The statement that `φ(S) ≤ U ↔ S ≤ φ⁻¹(U)` is simply the statement that these functors are adjoint to each other. Today we will define pushforward and pullback of filters, and show that they are also a pair of adjoint functors, but we will not use this language. In fact there is a special language for adjoint functors in this simple situation: we will say that pushforward and pullback form a Galois connection. -/ /- ## Warm-up: pushing forward and pulling back subsets. Say `X` and `Y` are types, and `f : X → Y`. -/ variables (X Y : Type) (f : X → Y) /- ### images In Lean, the image `f(S)` of a subset `S : set X` cannot be denoted `f S`, because `f` expects an _element_ of `X` as an input, not a subset of `X`, so we need new notation. Notation : `f '' S` is the image of `S` under `f`. Let's check this. -/ example (S : set X) : f '' S = {y : Y | ∃ x : X, x ∈ S ∧ f x = y} := begin -- true by definition refl end /- ### preimages In Lean, the preimage `f⁻¹(T)` of a subset `T : set Y` cannot be denoted `f⁻¹ T` because `⁻¹` is the inverse notation in group theory, so if anything would be a function from `Y` to `X`, not a function on subsets of `Y`. Notation : `f ⁻¹' T` is the preimage of `T` under `f`. Let's check this. Pro shortcut: `\-'` for `⁻¹'` -/ example (T : set Y) : f ⁻¹' T = {x : X | f x ∈ T} := begin -- true by definition refl end /- I claim that the following conditions on `S : set X` and `T : set Y` are equivalent: 1) `f '' S ⊆ T` 2) `S ⊆ f⁻¹' T` Indeed, they both say that `f` restricts to a function from `S` to `T`. Let's check this. You might find `mem_preimage : a ∈ f ⁻¹' s ↔ f a ∈ s` and -/ open set example (S : set X) (T : set Y) : f '' S ⊆ T ↔ S ⊆ f⁻¹' T := begin split, { intros h x hxS, -- rw subset_def at h, -- rw mem_preimage, apply h, use [x, hxS, rfl] }, { rintros h - ⟨x, hxS, rfl⟩, exact h hxS } end /- ## Pushing forward filters. Pushing forward is easy, so let's do that first. It's called `filter.map` in Lean. We define the pushforward filter `map f F` on `Y` to be the obvious thing: a subset of `Y` is in the filter iff `f⁻¹(Y)` is in `F`. Let's check this is a filter. Reminder of some helpful lemmas: In `set`: `mem_set_of_eq : a ∈ {x : α | p x} = p a` -- definitional In `filter`: `univ_mem_sets : univ ∈ F` `mem_sets_of_superset : S ∈ F → S ⊆ T → T ∈ F` `inter_mem_sets : S ∈ F → T ∈ F → S ∩ T ∈ F` -/ open filter -- this is called `F.map f` or `filter.map f F` -- or just `map f F` if `filter` is open. example (F : filter X) : filter Y := { sets := {T : set Y | f ⁻¹' T ∈ F }, univ_sets := begin -- rw mem_set_of_eq, exact univ_mem_sets, end, sets_of_superset := begin intros S T hS hST, --rw mem_set_of_eq at *, refine mem_sets_of_superset hS _, intros x hx, exact hST hx, end, inter_sets := begin intros S T, -- I am abusing definitional equality exact inter_mem_sets, end, } -- this is `filter.mem_map` and it's true by definition. example (F : filter X) (T : set Y) : T ∈ F.map f ↔ f ⁻¹' T ∈ F := begin -- true by definition refl end -- Let's check that map satisfies some basic functorialities. -- Recall that if your goal is to check two filters are -- equal then you can use the `ext` tactic. -- pushing along the identity map id : X → X doesn't change the filter. -- this is `filter.map_id` but see if you can prove it yourself. example (F : filter X) : F.map id = F := begin ext S, refl, end -- pushing along g ∘ f is the same as pushing along f and then g -- for some reason this isn't in mathlib, instead they have `map_map` which -- has the equality the other way. variables (Z : Type) (g : Y → Z) -- this isn't in mathlib, but `filter.map_map` is the equality the other -- way around. See if you can prove it yourself. example (F : filter X) : F.map (g ∘ f) = (F.map f).map g := begin ext S, refl, end open_locale filter -- for 𝓟 notation -- pushing the principal filter `𝓟 S` along `f` gives `𝓟 (f '' S)` -- this is `filter.map_principal` but see if you can prove it yourself. example (S : set X) : (𝓟 S).map f = 𝓟 (f '' S) := begin ext T, -- rw mem_map, -- rw mem_principal_sets, -- rw mem_principal_sets, split, { rintro h y ⟨x, hx, rfl⟩, exact h hx }, { rintro h x hx, apply h, exact ⟨x, hx, rfl⟩ } end /- ## tendsto The definition: if `f : X → Y` and `F : filter X` and `G : filter Y` then `tendsto f F G : Prop := map f F ≤ G`. This is a definition (it has type `Prop`), not the proof of a theorem. It is a true-false statement attached to `f`, `F` and `G`, it's a bit like saying "f is continuous at x" or something like that, it might be true and it might be false. The mental model you might want to have of the definition is that `tendsto f F G` means that the function `f` restricts to a function from the generalized set `F` to the generalized set `G`. -/ -- this is `filter.tendsto_def` example (F : filter X) (G : filter Y) : tendsto f F G ↔ ∀ T : set Y, T ∈ G → f ⁻¹' T ∈ F := begin -- true by definition refl end -- Let's make a basic API for `tendsto` -- this is `tendsto_id` but see if you can prove it yourself. example (F : filter X) : tendsto id F F := begin intro S, exact id, end -- this is `tendsto.comp` but see if you can prove it yourself example (F : filter X) (G : filter Y) (H : filter Z) (f : X → Y) (g : Y → Z) (hf : tendsto f F G) (hg : tendsto g G H) : tendsto (g ∘ f) F H := begin rintro S hS, specialize hg hS, specialize hf hg, exact hf, end -- I would recommend looking at the model answer to this one if -- you get stuck. lemma tendsto_comp_map (g : Y → Z) (F : filter X) (G : filter Z) : tendsto (g ∘ f) F G ↔ tendsto g (F.map f) G := begin refl, -- Both sides are the same, by definition. Think about it on paper! end /- ## Appendix : Pulling back filters We don't use this in the next part. Say `f : X → Y` and `G : filter Y`, and we want a filter on `X`. Let's make a naive definition. We want a collection of subsets of `X` corresponding to the filter obtained by pulling back `G` along `f`. When should `S : set X` be in this filter? Perhaps it is when `f '' S ∈ G`. However, there is no reason that the collection of `S` satisfying this property should be a filter on `X`. For example, there is no reason to espect that `f '' univ ∈ G` if `f` is not surjective. Here's a way of fixing this. Remember that our model of a filter `G` is some kind of generalised notion of a set. If `T : set Y` then `T ∈ G` is supposed to mean that the "set" `G` is a subset of `T`. So this should imply that `f⁻¹(G) ⊆ f⁻¹(T)`. In particular, if `T ∈ G` and `f⁻¹(T) ⊆ S` then this should mean `f⁻¹(G) ⊆ S` and hence `S ∈ f⁻¹(G)`. Let's try this and see if it works. Random useful lemmas (you might be getting to the point where you can guess the names of the lemmas): `subset_univ S : S ⊆ univ` `subset.trans : A ⊆ B → B ⊆ C → A ⊆ C` -/ -- this is called filter.comap example (G : filter Y) : filter X := { sets := {S : set X | ∃ T ∈ G, f ⁻¹' T ⊆ S}, univ_sets := begin use univ, split, { exact univ_mem_sets }, { exact subset_univ _ } end, sets_of_superset := begin rintros S T ⟨U, hUG, hUS⟩ hST, use [U, hUG], exact subset.trans hUS hST end, inter_sets := begin rintro S T ⟨U, hUG, hUS⟩ ⟨V, hVG, hVT⟩, use [U ∩ V, inter_mem_sets hUG hVG], rintro x ⟨hxU, hxV⟩, exact ⟨hUS hxU, hVT hxV⟩, end } -- Let's call this mem_comap lemma mem_comap (f : X → Y) (G : filter Y) (S : set X) : S ∈ comap f G ↔ ∃ T ∈ G, f ⁻¹' T ⊆ S := begin -- true by definition refl end -- If you want to, you can check some preliminary properties of `comap`. -- this is comap_id example (G : filter Y) : comap id G = G := begin ext S, rw mem_comap, split, { rintro ⟨T, hT, h⟩, exact mem_sets_of_superset hT h, }, { intro hS, use [S, hS], refl } end -- this is comap_comap but the other way around lemma comap_comp (H : filter Z) : comap (g ∘ f) H = comap f (comap g H) := begin ext S, simp only [mem_comap], split, { rintro ⟨U, hU, h⟩, use g ⁻¹' U, refine ⟨_, h⟩, rw mem_comap, use [U, hU] }, { rintro ⟨T, ⟨U, hU, h2⟩, h⟩, use [U, hU], refine subset.trans _ h, intros x hx, exact h2 hx } end -- this is comap_principal. Remember `mem_principal_sets`! example (T : set Y) : comap f (𝓟 T) = 𝓟 (f ⁻¹' T) := begin ext S, -- rw mem_comap, -- rw mem_principal_sets, split, { rintro ⟨U, hU, h⟩, refine subset.trans (λ x, _) h, apply hU }, { intro h, exact ⟨T, mem_principal_self T, h⟩ } end -- This is the proof that `map f` and `comap f` are adjoint functors, -- or in other words form a Galois connection. It is the "generalised set" -- analogue of the assertion that if S is a subset of X and T is a subset of Y -- then f(S) ⊆ T ↔ S ⊆ f⁻¹(T), these both being ways to say that `f` restricts -- to a function from `S` to `T`. lemma filter.galois_connection (F : filter X) (G : filter Y) : map f F ≤ G ↔ F ≤ comap f G := begin split, { rintro h S ⟨T, hT, hTS⟩, rw le_def at h, exact mem_sets_of_superset (h T hT) hTS }, { rintro h T hT, rw le_def at h, exact h (f ⁻¹' T) ⟨T, hT, subset.refl _⟩ }, end -- indeed, `map f` and `comap f` form a Galois connection. example : galois_connection (map f) (comap f) := filter.galois_connection X Y f
8f9e57014610098ad167889fe1c7696631250c12
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/padics/hensel.lean
7c0fe6c02d74dca11fdf11949cc247e59ada91aa
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
21,327
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis -/ import data.padics.padic_integers import topology.metric_space.cau_seq_filter import analysis.specific_limits import data.polynomial.identities import topology.algebra.polynomial /-! # Hensel's lemma on ℤ_p This file proves Hensel's lemma on ℤ_p, roughly following Keith Conrad's writeup: <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> Hensel's lemma gives a simple condition for the existence of a root of a polynomial. The proof and motivation are described in the paper [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019]. ## References * <http://www.math.uconn.edu/~kconrad/blurbs/gradnumthy/hensel.pdf> * [R. Y. Lewis, *A formal proof of Hensel's lemma over the p-adic integers*][lewis2019] * <https://en.wikipedia.org/wiki/Hensel%27s_lemma> ## Tags p-adic, p adic, padic, p-adic integer -/ noncomputable theory open_locale classical topological_space -- We begin with some general lemmas that are used below in the computation. lemma padic_polynomial_dist {p : ℕ} [fact p.prime] (F : polynomial ℤ_[p]) (x y : ℤ_[p]) : ∥F.eval x - F.eval y∥ ≤ ∥x - y∥ := let ⟨z, hz⟩ := F.eval_sub_factor x y in calc ∥F.eval x - F.eval y∥ = ∥z∥ * ∥x - y∥ : by simp [hz] ... ≤ 1 * ∥x - y∥ : mul_le_mul_of_nonneg_right (padic_int.norm_le_one _) (norm_nonneg _) ... = ∥x - y∥ : by simp open filter metric private lemma comp_tendsto_lim {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} (ncs : cau_seq ℤ_[p] norm) : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 (F.eval ncs.lim)) := (F.continuous_eval.tendsto _).comp ncs.tendsto_limit section parameters {p : ℕ} [fact p.prime] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} {a : ℤ_[p]} (ncs_der_val : ∀ n, ∥F.derivative.eval (ncs n)∥ = ∥F.derivative.eval a∥) include ncs_der_val private lemma ncs_tendsto_const : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 ∥F.derivative.eval a∥) := by convert tendsto_const_nhds; ext; rw ncs_der_val private lemma ncs_tendsto_lim : tendsto (λ i, ∥F.derivative.eval (ncs i)∥) at_top (𝓝 (∥F.derivative.eval ncs.lim∥)) := tendsto.comp (continuous_iff_continuous_at.1 continuous_norm _) (comp_tendsto_lim _) private lemma norm_deriv_eq : ∥F.derivative.eval ncs.lim∥ = ∥F.derivative.eval a∥ := tendsto_nhds_unique ncs_tendsto_lim ncs_tendsto_const end section parameters {p : ℕ} [fact p.prime] {ncs : cau_seq ℤ_[p] norm} {F : polynomial ℤ_[p]} (hnorm : tendsto (λ i, ∥F.eval (ncs i)∥) at_top (𝓝 0)) include hnorm private lemma tendsto_zero_of_norm_tendsto_zero : tendsto (λ i, F.eval (ncs i)) at_top (𝓝 0) := tendsto_iff_norm_tendsto_zero.2 (by simpa using hnorm) lemma limit_zero_of_norm_tendsto_zero : F.eval ncs.lim = 0 := tendsto_nhds_unique (comp_tendsto_lim _) tendsto_zero_of_norm_tendsto_zero end section hensel open nat parameters {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} {a : ℤ_[p]} (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) (hnsol : F.eval a ≠ 0) include hnorm /-- `T` is an auxiliary value that is used to control the behavior of the polynomial `F`. -/ private def T : ℝ := ∥(F.eval a / (F.derivative.eval a)^2 : ℚ_[p])∥ private lemma deriv_sq_norm_pos : 0 < ∥F.derivative.eval a∥ ^ 2 := lt_of_le_of_lt (norm_nonneg _) hnorm private lemma deriv_sq_norm_ne_zero : ∥F.derivative.eval a∥^2 ≠ 0 := ne_of_gt deriv_sq_norm_pos private lemma deriv_norm_ne_zero : ∥F.derivative.eval a∥ ≠ 0 := λ h, deriv_sq_norm_ne_zero (by simp [*, pow_two]) private lemma deriv_norm_pos : 0 < ∥F.derivative.eval a∥ := lt_of_le_of_ne (norm_nonneg _) (ne.symm deriv_norm_ne_zero) private lemma deriv_ne_zero : F.derivative.eval a ≠ 0 := mt norm_eq_zero.2 deriv_norm_ne_zero private lemma T_def : T = ∥F.eval a∥ / ∥F.derivative.eval a∥^2 := calc T = ∥F.eval a∥ / ∥((F.derivative.eval a)^2 : ℚ_[p])∥ : normed_field.norm_div _ _ ... = ∥F.eval a∥ / ∥(F.derivative.eval a)^2∥ : by simp [norm, padic_int.norm_def] ... = ∥F.eval a∥ / ∥(F.derivative.eval a)∥^2 : by simp [pow, monoid.pow] private lemma T_lt_one : T < 1 := let h := (div_lt_one deriv_sq_norm_pos).2 hnorm in by rw T_def; apply h private lemma T_pow {n : ℕ} (hn : n > 0) : T ^ n < 1 := have T ^ n ≤ T ^ 1, from pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) (succ_le_of_lt hn), lt_of_le_of_lt (by simpa) T_lt_one private lemma T_pow' (n : ℕ) : T ^ (2 ^ n) < 1 := (T_pow (pow_pos (by norm_num) _)) private lemma T_pow_nonneg (n : ℕ) : T ^ n ≥ 0 := pow_nonneg (norm_nonneg _) _ /-- We will construct a sequence of elements of ℤ_p satisfying successive values of `ih`. -/ private def ih (n : ℕ) (z : ℤ_[p]) : Prop := ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∥F.eval z∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) private lemma ih_0 : ih 0 a := ⟨ rfl, by simp [T_def, mul_div_cancel' _ (ne_of_gt (deriv_sq_norm_pos hnorm))] ⟩ private lemma calc_norm_le_one {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1 := calc ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ = ∥(↑(F.eval z) : ℚ_[p])∥ / ∥(↑(F.derivative.eval z) : ℚ_[p])∥ : normed_field.norm_div _ _ ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : by simp [hz.1] ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ ... ≤ 1 : mul_le_one (padic_int.norm_le_one _) (T_pow_nonneg _) (le_of_lt (T_pow' _)) private lemma calc_deriv_dist {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) (hz1 : ∥z1∥ = ∥F.eval z∥ / ∥F.derivative.eval a∥) {n} (hz : ih n z) : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥ := calc ∥F.derivative.eval z' - F.derivative.eval z∥ ≤ ∥z' - z∥ : padic_polynomial_dist _ _ _ ... = ∥z1∥ : by simp only [sub_eq_add_neg, add_assoc, hz', add_add_neg_cancel'_right, norm_neg] ... = ∥F.eval z∥ / ∥F.derivative.eval a∥ : hz1 ... ≤ ∥F.derivative.eval a∥^2 * T^(2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 hz.2 ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel deriv_norm_ne_zero _ ... < ∥F.derivative.eval a∥ : (mul_lt_iff_lt_one_right deriv_norm_pos).2 (T_pow (pow_pos (by norm_num) _)) private def calc_eval_z' {z z' z1 : ℤ_[p]} (hz' : z' = z - z1) {n} (hz : ih n z) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : {q : ℤ_[p] // F.eval z' = q * z1^2} := have hdzne' : (↑(F.derivative.eval z) : ℚ_[p]) ≠ 0, from have hdzne : F.derivative.eval z ≠ 0, from mt norm_eq_zero.2 (by rw hz.1; apply deriv_norm_ne_zero; assumption), λ h, hdzne $ subtype.ext_iff_val.2 h, let ⟨q, hq⟩ := F.binom_expansion z (-z1) in have ∥(↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)) : ℚ_[p])∥ ≤ 1, by {rw padic_norm_e.mul, apply mul_le_one, apply padic_int.norm_le_one, apply norm_nonneg, apply h1}, have F.derivative.eval z * (-z1) = -F.eval z, from calc F.derivative.eval z * (-z1) = (F.derivative.eval z) * -⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩ : by rw [hzeq] ... = -((F.derivative.eval z) * ⟨↑(F.eval z) / ↑(F.derivative.eval z), h1⟩) : by simp [subtype.ext_iff_val] ... = -(⟨↑(F.derivative.eval z) * (↑(F.eval z) / ↑(F.derivative.eval z)), this⟩) : subtype.ext_iff_val.2 $ by simp ... = -(F.eval z) : by simp [mul_div_cancel' _ hdzne'], have heq : F.eval z' = q * z1^2, by simpa [this, hz'] using hq, ⟨q, heq⟩ private def calc_eval_z'_norm {z z' z1 : ℤ_[p]} {n} (hz : ih n z) {q} (heq : F.eval z' = q * z1^2) (h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1) (hzeq : z1 = ⟨_, h1⟩) : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)) := calc ∥F.eval z'∥ = ∥q∥ * ∥z1∥^2 : by simp [heq] ... ≤ 1 * ∥z1∥^2 : mul_le_mul_of_nonneg_right (padic_int.norm_le_one _) (pow_nonneg (norm_nonneg _) _) ... = ∥F.eval z∥^2 / ∥F.derivative.eval a∥^2 : by simp [hzeq, hz.1, div_pow] ... ≤ (∥F.derivative.eval a∥^2 * T^(2^n))^2 / ∥F.derivative.eval a∥^2 : (div_le_div_right deriv_sq_norm_pos).2 (pow_le_pow_of_le_left (norm_nonneg _) hz.2 _) ... = (∥F.derivative.eval a∥^2)^2 * (T^(2^n))^2 / ∥F.derivative.eval a∥^2 : by simp only [mul_pow] ... = ∥F.derivative.eval a∥^2 * (T^(2^n))^2 : div_sq_cancel deriv_sq_norm_ne_zero _ ... = ∥F.derivative.eval a∥^2 * T^(2^(n + 1)) : by rw [←pow_mul, pow_succ' 2] set_option eqn_compiler.zeta true /-- Given `z : ℤ_[p]` satisfying `ih n z`, construct `z' : ℤ_[p]` satisfying `ih (n+1) z'`. We need the hypothesis `ih n z`, since otherwise `z'` is not necessarily an integer. -/ private def ih_n {n : ℕ} {z : ℤ_[p]} (hz : ih n z) : {z' : ℤ_[p] // ih (n+1) z'} := have h1 : ∥(↑(F.eval z) : ℚ_[p]) / ↑(F.derivative.eval z)∥ ≤ 1, from calc_norm_le_one hz, let z1 : ℤ_[p] := ⟨_, h1⟩, z' : ℤ_[p] := z - z1 in ⟨ z', have hdist : ∥F.derivative.eval z' - F.derivative.eval z∥ < ∥F.derivative.eval a∥, from calc_deriv_dist rfl (by simp [z1, hz.1]) hz, have hfeq : ∥F.derivative.eval z'∥ = ∥F.derivative.eval a∥, begin rw [sub_eq_add_neg, ← hz.1, ←norm_neg (F.derivative.eval z)] at hdist, have := padic_int.norm_eq_of_norm_add_lt_right hdist, rwa [norm_neg, hz.1] at this end, let ⟨q, heq⟩ := calc_eval_z' rfl hz h1 rfl in have hnle : ∥F.eval z'∥ ≤ ∥F.derivative.eval a∥^2 * T^(2^(n+1)), from calc_eval_z'_norm hz heq h1 rfl, ⟨hfeq, hnle⟩⟩ set_option eqn_compiler.zeta false -- why doesn't "noncomputable theory" stick here? private noncomputable def newton_seq_aux : Π n : ℕ, {z : ℤ_[p] // ih n z} | 0 := ⟨a, ih_0⟩ | (k+1) := ih_n (newton_seq_aux k).2 private def newton_seq (n : ℕ) : ℤ_[p] := (newton_seq_aux n).1 private lemma newton_seq_deriv_norm (n : ℕ) : ∥F.derivative.eval (newton_seq n)∥ = ∥F.derivative.eval a∥ := (newton_seq_aux n).2.1 private lemma newton_seq_norm_le (n : ℕ) : ∥F.eval (newton_seq n)∥ ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) := (newton_seq_aux n).2.2 private lemma newton_seq_norm_eq (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ := by simp [newton_seq, newton_seq_aux, ih_n, sub_eq_add_neg, add_comm] private lemma newton_seq_succ_dist (n : ℕ) : ∥newton_seq (n+1) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := calc ∥newton_seq (n+1) - newton_seq n∥ = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval (newton_seq n)∥ : newton_seq_norm_eq _ ... = ∥F.eval (newton_seq n)∥ / ∥F.derivative.eval a∥ : by rw newton_seq_deriv_norm ... ≤ ∥F.derivative.eval a∥^2 * T ^ (2^n) / ∥F.derivative.eval a∥ : (div_le_div_right deriv_norm_pos).2 (newton_seq_norm_le _) ... = ∥F.derivative.eval a∥ * T^(2^n) : div_sq_cancel (ne_of_gt deriv_norm_pos) _ include hnsol private lemma T_pos : T > 0 := begin rw T_def, exact div_pos (norm_pos_iff.2 hnsol) (deriv_sq_norm_pos hnorm) end private lemma newton_seq_succ_dist_weak (n : ℕ) : ∥newton_seq (n+2) - newton_seq (n+1)∥ < ∥F.eval a∥ / ∥F.derivative.eval a∥ := have 2 ≤ 2^(n+1), from have _, from pow_le_pow (by norm_num : 1 ≤ 2) (nat.le_add_left _ _ : 1 ≤ n + 1), by simpa using this, calc ∥newton_seq (n+2) - newton_seq (n+1)∥ ≤ ∥F.derivative.eval a∥ * T^(2^(n+1)) : newton_seq_succ_dist _ ... ≤ ∥F.derivative.eval a∥ * T^2 : mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) ... < ∥F.derivative.eval a∥ * T^1 : mul_lt_mul_of_pos_left (pow_lt_pow_of_lt_one T_pos T_lt_one (by norm_num)) deriv_norm_pos ... = ∥F.eval a∥ / ∥F.derivative.eval a∥ : begin rw [T, pow_two, pow_one, normed_field.norm_div, ←mul_div_assoc, padic_norm_e.mul], apply mul_div_mul_left, apply deriv_norm_ne_zero; assumption end private lemma newton_seq_dist_aux (n : ℕ) : ∀ k : ℕ, ∥newton_seq (n + k) - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) | 0 := begin simp, apply mul_nonneg, {apply norm_nonneg}, {apply T_pow_nonneg} end | (k+1) := have 2^n ≤ 2^(n+k), by {apply pow_le_pow, norm_num, apply nat.le_add_right}, calc ∥newton_seq (n + (k + 1)) - newton_seq n∥ = ∥newton_seq ((n + k) + 1) - newton_seq n∥ : by rw add_assoc ... = ∥(newton_seq ((n + k) + 1) - newton_seq (n+k)) + (newton_seq (n+k) - newton_seq n)∥ : by rw ←sub_add_sub_cancel ... ≤ max (∥newton_seq ((n + k) + 1) - newton_seq (n+k)∥) (∥newton_seq (n+k) - newton_seq n∥) : padic_int.nonarchimedean _ _ ... ≤ max (∥F.derivative.eval a∥ * T^(2^((n + k)))) (∥F.derivative.eval a∥ * T^(2^n)) : max_le_max (newton_seq_succ_dist _) (newton_seq_dist_aux _) ... = ∥F.derivative.eval a∥ * T^(2^n) : max_eq_right $ mul_le_mul_of_nonneg_left (pow_le_pow_of_le_one (norm_nonneg _) (le_of_lt T_lt_one) this) (norm_nonneg _) private lemma newton_seq_dist {n k : ℕ} (hnk : n ≤ k) : ∥newton_seq k - newton_seq n∥ ≤ ∥F.derivative.eval a∥ * T^(2^n) := have hex : ∃ m, k = n + m, from exists_eq_add_of_le hnk, let ⟨_, hex'⟩ := hex in by rw hex'; apply newton_seq_dist_aux; assumption private lemma newton_seq_dist_to_a : ∀ n : ℕ, 0 < n → ∥newton_seq n - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ | 1 h := by simp [sub_eq_add_neg, add_assoc, newton_seq, newton_seq_aux, ih_n]; apply normed_field.norm_div | (k+2) h := have hlt : ∥newton_seq (k+2) - newton_seq (k+1)∥ < ∥newton_seq (k+1) - a∥, by rw newton_seq_dist_to_a (k+1) (succ_pos _); apply newton_seq_succ_dist_weak; assumption, have hne' : ∥newton_seq (k + 2) - newton_seq (k+1)∥ ≠ ∥newton_seq (k+1) - a∥, from ne_of_lt hlt, calc ∥newton_seq (k + 2) - a∥ = ∥(newton_seq (k + 2) - newton_seq (k+1)) + (newton_seq (k+1) - a)∥ : by rw ←sub_add_sub_cancel ... = max (∥newton_seq (k + 2) - newton_seq (k+1)∥) (∥newton_seq (k+1) - a∥) : padic_int.norm_add_eq_max_of_ne hne' ... = ∥newton_seq (k+1) - a∥ : max_eq_right_of_lt hlt ... = ∥polynomial.eval a F∥ / ∥polynomial.eval a (polynomial.derivative F)∥ : newton_seq_dist_to_a (k+1) (succ_pos _) private lemma bound' : tendsto (λ n : ℕ, ∥F.derivative.eval a∥ * T^(2^n)) at_top (𝓝 0) := begin rw ←mul_zero (∥F.derivative.eval a∥), exact tendsto_const_nhds.mul (tendsto.comp (tendsto_pow_at_top_nhds_0_of_lt_1 (norm_nonneg _) (T_lt_one hnorm)) (nat.tendsto_pow_at_top_at_top_of_one_lt (by norm_num))) end private lemma bound : ∀ {ε}, ε > 0 → ∃ N : ℕ, ∀ {n}, n ≥ N → ∥F.derivative.eval a∥ * T^(2^n) < ε := have mtn : ∀ n : ℕ, ∥polynomial.eval a (polynomial.derivative F)∥ * T ^ (2 ^ n) ≥ 0, from λ n, mul_nonneg (norm_nonneg _) (T_pow_nonneg _), begin have := bound' hnorm hnsol, simp [tendsto, nhds] at this, intros ε hε, cases this (ball 0 ε) (mem_ball_self hε) (is_open_ball) with N hN, existsi N, intros n hn, simpa [normed_field.norm_mul, real.norm_eq_abs, abs_of_nonneg (mtn n)] using hN _ hn end private lemma bound'_sq : tendsto (λ n : ℕ, ∥F.derivative.eval a∥^2 * T^(2^n)) at_top (𝓝 0) := begin rw [←mul_zero (∥F.derivative.eval a∥), pow_two], simp only [mul_assoc], apply tendsto.mul, { apply tendsto_const_nhds }, { apply bound', assumption } end private theorem newton_seq_is_cauchy : is_cau_seq norm newton_seq := begin intros ε hε, cases bound hnorm hnsol hε with N hN, existsi N, intros j hj, apply lt_of_le_of_lt, { apply newton_seq_dist _ _ hj, assumption }, { apply hN, apply le_refl } end private def newton_cau_seq : cau_seq ℤ_[p] norm := ⟨_, newton_seq_is_cauchy⟩ private def soln : ℤ_[p] := newton_cau_seq.lim private lemma soln_spec {ε : ℝ} (hε : ε > 0) : ∃ (N : ℕ), ∀ {i : ℕ}, i ≥ N → ∥soln - newton_cau_seq i∥ < ε := setoid.symm (cau_seq.equiv_lim newton_cau_seq) _ hε private lemma soln_deriv_norm : ∥F.derivative.eval soln∥ = ∥F.derivative.eval a∥ := norm_deriv_eq newton_seq_deriv_norm private lemma newton_seq_norm_tendsto_zero : tendsto (λ i, ∥F.eval (newton_cau_seq i)∥) at_top (𝓝 0) := squeeze_zero (λ _, norm_nonneg _) newton_seq_norm_le bound'_sq private lemma newton_seq_dist_tendsto : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 (∥F.eval a∥ / ∥F.derivative.eval a∥)) := tendsto_const_nhds.congr' $ eventually_at_top.2 ⟨1, λ _ hx, (newton_seq_dist_to_a _ hx).symm⟩ private lemma newton_seq_dist_tendsto' : tendsto (λ n, ∥newton_cau_seq n - a∥) at_top (𝓝 ∥soln - a∥) := (continuous_norm.tendsto _).comp (newton_cau_seq.tendsto_limit.sub tendsto_const_nhds) private lemma soln_dist_to_a : ∥soln - a∥ = ∥F.eval a∥ / ∥F.derivative.eval a∥ := tendsto_nhds_unique newton_seq_dist_tendsto' newton_seq_dist_tendsto private lemma soln_dist_to_a_lt_deriv : ∥soln - a∥ < ∥F.derivative.eval a∥ := begin rw [soln_dist_to_a, div_lt_iff], { rwa pow_two at hnorm }, { apply deriv_norm_pos, assumption } end private lemma eval_soln : F.eval soln = 0 := limit_zero_of_norm_tendsto_zero newton_seq_norm_tendsto_zero private lemma soln_unique (z : ℤ_[p]) (hev : F.eval z = 0) (hnlt : ∥z - a∥ < ∥F.derivative.eval a∥) : z = soln := have soln_dist : ∥z - soln∥ < ∥F.derivative.eval a∥, from calc ∥z - soln∥ = ∥(z - a) + (a - soln)∥ : by rw sub_add_sub_cancel ... ≤ max (∥z - a∥) (∥a - soln∥) : padic_int.nonarchimedean _ _ ... < ∥F.derivative.eval a∥ : max_lt hnlt (by rw norm_sub_rev; apply soln_dist_to_a_lt_deriv), let h := z - soln, ⟨q, hq⟩ := F.binom_expansion soln h in have (F.derivative.eval soln + q * h) * h = 0, from eq.symm (calc 0 = F.eval (soln + h) : by simp [hev, h] ... = F.derivative.eval soln * h + q * h^2 : by rw [hq, eval_soln, zero_add] ... = (F.derivative.eval soln + q * h) * h : by rw [pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval soln + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval soln = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval soln∥ (calc ∥F.derivative.eval soln∥ = ∥(-q) * h∥ : by rw this ... ≤ 1 * ∥h∥ : by rw [padic_int.norm_mul]; exact mul_le_mul_of_nonneg_right (padic_int.norm_le_one _) (norm_nonneg _) ... = ∥z - soln∥ : by simp [h] ... < ∥F.derivative.eval soln∥ : by rw soln_deriv_norm; apply soln_dist), eq_of_sub_eq_zero (by rw ←this; refl) end hensel variables {p : ℕ} [fact p.prime] {F : polynomial ℤ_[p]} {a : ℤ_[p]} private lemma a_soln_is_unique (ha : F.eval a = 0) (z' : ℤ_[p]) (hz' : F.eval z' = 0) (hnormz' : ∥z' - a∥ < ∥F.derivative.eval a∥) : z' = a := let h := z' - a, ⟨q, hq⟩ := F.binom_expansion a h in have (F.derivative.eval a + q * h) * h = 0, from eq.symm (calc 0 = F.eval (a + h) : show 0 = F.eval (a + (z' - a)), by rw add_comm; simp [hz'] ... = F.derivative.eval a * h + q * h^2 : by rw [hq, ha, zero_add] ... = (F.derivative.eval a + q * h) * h : by rw [pow_two, right_distrib, mul_assoc]), have h = 0, from by_contradiction $ λ hne, have F.derivative.eval a + q * h = 0, from (eq_zero_or_eq_zero_of_mul_eq_zero this).resolve_right hne, have F.derivative.eval a = (-q) * h, by simpa using eq_neg_of_add_eq_zero this, lt_irrefl ∥F.derivative.eval a∥ (calc ∥F.derivative.eval a∥ = ∥q∥*∥h∥ : by simp [this] ... ≤ 1*∥h∥ : mul_le_mul_of_nonneg_right (padic_int.norm_le_one _) (norm_nonneg _) ... < ∥F.derivative.eval a∥ : by simpa [h]), eq_of_sub_eq_zero (by rw ←this; refl) variable (hnorm : ∥F.eval a∥ < ∥F.derivative.eval a∥^2) include hnorm private lemma a_is_soln (ha : F.eval a = 0) : F.eval a = 0 ∧ ∥a - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval a∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = a := ⟨ha, by simp; apply deriv_norm_pos; apply hnorm, rfl, a_soln_is_unique ha⟩ lemma hensels_lemma : ∃ z : ℤ_[p], F.eval z = 0 ∧ ∥z - a∥ < ∥F.derivative.eval a∥ ∧ ∥F.derivative.eval z∥ = ∥F.derivative.eval a∥ ∧ ∀ z', F.eval z' = 0 → ∥z' - a∥ < ∥F.derivative.eval a∥ → z' = z := if ha : F.eval a = 0 then ⟨a, a_is_soln hnorm ha⟩ else by refine ⟨soln _ _, eval_soln _ _, soln_dist_to_a_lt_deriv _ _, soln_deriv_norm _ _, soln_unique _ _⟩; assumption
f549d6266dc987cb6a4c9bd040a76485079f7f66
69d4931b605e11ca61881fc4f66db50a0a875e39
/src/data/zsqrtd/gaussian_int.lean
ba593e1cac4f350a1ecda690a9f7c290d5c53d64
[ "Apache-2.0" ]
permissive
abentkamp/mathlib
d9a75d291ec09f4637b0f30cc3880ffb07549ee5
5360e476391508e092b5a1e5210bd0ed22dc0755
refs/heads/master
1,682,382,954,948
1,622,106,077,000
1,622,106,077,000
149,285,665
0
0
null
null
null
null
UTF-8
Lean
false
false
12,586
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import data.zsqrtd.basic import data.complex.basic import ring_theory.principal_ideal_domain import number_theory.quadratic_reciprocity /-! # Gaussian integers The Gaussian integers are complex integer, complex numbers whose real and imaginary parts are both integers. ## Main definitions The Euclidean domain structure on `ℤ[i]` is defined in this file. The homomorphism `to_complex` into the complex numbers is also defined in this file. ## Main statements `prime_iff_mod_four_eq_three_of_nat_prime` A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` ## Notations This file uses the local notation `ℤ[i]` for `gaussian_int` ## Implementation notes Gaussian integers are implemented using the more general definition `zsqrtd`, the type of integers adjoined a square root of `d`, in this case `-1`. The definition is reducible, so that properties and definitions about `zsqrtd` can easily be used. -/ open zsqrtd complex @[reducible] def gaussian_int : Type := zsqrtd (-1) local notation `ℤ[i]` := gaussian_int namespace gaussian_int instance : has_repr ℤ[i] := ⟨λ x, "⟨" ++ repr x.re ++ ", " ++ repr x.im ++ "⟩"⟩ instance : comm_ring ℤ[i] := zsqrtd.comm_ring section local attribute [-instance] complex.field -- Avoid making things noncomputable unnecessarily. /-- The embedding of the Gaussian integers into the complex numbers, as a ring homomorphism. -/ def to_complex : ℤ[i] →+* ℂ := zsqrtd.lift ⟨I, by simp⟩ end instance : has_coe (ℤ[i]) ℂ := ⟨to_complex⟩ lemma to_complex_def (x : ℤ[i]) : (x : ℂ) = x.re + x.im * I := rfl lemma to_complex_def' (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ) = x + y * I := by simp [to_complex_def] lemma to_complex_def₂ (x : ℤ[i]) : (x : ℂ) = ⟨x.re, x.im⟩ := by apply complex.ext; simp [to_complex_def] @[simp] lemma to_real_re (x : ℤ[i]) : ((x.re : ℤ) : ℝ) = (x : ℂ).re := by simp [to_complex_def] @[simp] lemma to_real_im (x : ℤ[i]) : ((x.im : ℤ) : ℝ) = (x : ℂ).im := by simp [to_complex_def] @[simp] lemma to_complex_re (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).re = x := by simp [to_complex_def] @[simp] lemma to_complex_im (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).im = y := by simp [to_complex_def] @[simp] lemma to_complex_add (x y : ℤ[i]) : ((x + y : ℤ[i]) : ℂ) = x + y := to_complex.map_add _ _ @[simp] lemma to_complex_mul (x y : ℤ[i]) : ((x * y : ℤ[i]) : ℂ) = x * y := to_complex.map_mul _ _ @[simp] lemma to_complex_one : ((1 : ℤ[i]) : ℂ) = 1 := to_complex.map_one @[simp] lemma to_complex_zero : ((0 : ℤ[i]) : ℂ) = 0 := to_complex.map_zero @[simp] lemma to_complex_neg (x : ℤ[i]) : ((-x : ℤ[i]) : ℂ) = -x := to_complex.map_neg _ @[simp] lemma to_complex_sub (x y : ℤ[i]) : ((x - y : ℤ[i]) : ℂ) = x - y := to_complex.map_sub _ _ @[simp] lemma to_complex_inj {x y : ℤ[i]} : (x : ℂ) = y ↔ x = y := by cases x; cases y; simp [to_complex_def₂] @[simp] lemma to_complex_eq_zero {x : ℤ[i]} : (x : ℂ) = 0 ↔ x = 0 := by rw [← to_complex_zero, to_complex_inj] @[simp] lemma nat_cast_real_norm (x : ℤ[i]) : (x.norm : ℝ) = (x : ℂ).norm_sq := by rw [norm, norm_sq]; simp @[simp] lemma nat_cast_complex_norm (x : ℤ[i]) : (x.norm : ℂ) = (x : ℂ).norm_sq := by cases x; rw [norm, norm_sq]; simp lemma norm_nonneg (x : ℤ[i]) : 0 ≤ norm x := norm_nonneg (by norm_num) _ @[simp] lemma norm_eq_zero {x : ℤ[i]} : norm x = 0 ↔ x = 0 := by rw [← @int.cast_inj ℝ _ _ _]; simp lemma norm_pos {x : ℤ[i]} : 0 < norm x ↔ x ≠ 0 := by rw [lt_iff_le_and_ne, ne.def, eq_comm, norm_eq_zero]; simp [norm_nonneg] @[simp] lemma coe_nat_abs_norm (x : ℤ[i]) : (x.norm.nat_abs : ℤ) = x.norm := int.nat_abs_of_nonneg (norm_nonneg _) @[simp] lemma nat_cast_nat_abs_norm {α : Type*} [ring α] (x : ℤ[i]) : (x.norm.nat_abs : α) = x.norm := by rw [← int.cast_coe_nat, coe_nat_abs_norm] lemma nat_abs_norm_eq (x : ℤ[i]) : x.norm.nat_abs = x.re.nat_abs * x.re.nat_abs + x.im.nat_abs * x.im.nat_abs := int.coe_nat_inj $ begin simp, simp [norm] end protected def div (x y : ℤ[i]) : ℤ[i] := let n := (rat.of_int (norm y))⁻¹ in let c := y.conj in ⟨round (rat.of_int (x * c).re * n : ℚ), round (rat.of_int (x * c).im * n : ℚ)⟩ instance : has_div ℤ[i] := ⟨gaussian_int.div⟩ lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * conj y).re / norm y : ℚ), round ((x * conj y).im / norm y : ℚ)⟩ := show zsqrtd.mk _ _ = _, by simp [rat.of_int_eq_mk, rat.mk_eq_div, div_eq_mul_inv] lemma to_complex_div_re (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).re = round ((x / y : ℂ).re) := by rw [div_def, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma to_complex_div_im (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).im = round ((x / y : ℂ).im) := by rw [div_def, ← @rat.cast_round ℝ _ _, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] local notation `abs'` := _root_.abs lemma norm_sq_le_norm_sq_of_re_le_of_im_le {x y : ℂ} (hre : abs' x.re ≤ abs' y.re) (him : abs' x.im ≤ abs' y.im) : x.norm_sq ≤ y.norm_sq := by rw [norm_sq_apply, norm_sq_apply, ← _root_.abs_mul_self, _root_.abs_mul, ← _root_.abs_mul_self y.re, _root_.abs_mul y.re, ← _root_.abs_mul_self x.im, _root_.abs_mul x.im, ← _root_.abs_mul_self y.im, _root_.abs_mul y.im]; exact (add_le_add (mul_self_le_mul_self (abs_nonneg _) hre) (mul_self_le_mul_self (abs_nonneg _) him)) lemma norm_sq_div_sub_div_lt_one (x y : ℤ[i]) : ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq < 1 := calc ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq = ((x / y : ℂ).re - ((x / y : ℤ[i]) : ℂ).re + ((x / y : ℂ).im - ((x / y : ℤ[i]) : ℂ).im) * I : ℂ).norm_sq : congr_arg _ $ by apply complex.ext; simp ... ≤ (1 / 2 + 1 / 2 * I).norm_sq : have abs' (2⁻¹ : ℝ) = 2⁻¹, from _root_.abs_of_nonneg (by norm_num), norm_sq_le_norm_sq_of_re_le_of_im_le (by rw [to_complex_div_re]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).re) (by rw [to_complex_div_im]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).im) ... < 1 : by simp [norm_sq]; norm_num protected def mod (x y : ℤ[i]) : ℤ[i] := x - y * (x / y) instance : has_mod ℤ[i] := ⟨gaussian_int.mod⟩ lemma mod_def (x y : ℤ[i]) : x % y = x - y * (x / y) := rfl lemma norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm < y.norm := have (y : ℂ) ≠ 0, by rwa [ne.def, ← to_complex_zero, to_complex_inj], (@int.cast_lt ℝ _ _ _ _).1 $ calc ↑(norm (x % y)) = (x - y * (x / y : ℤ[i]) : ℂ).norm_sq : by simp [mod_def] ... = (y : ℂ).norm_sq * (((x / y) - (x / y : ℤ[i])) : ℂ).norm_sq : by rw [← norm_sq_mul, mul_sub, mul_div_cancel' _ this] ... < (y : ℂ).norm_sq * 1 : mul_lt_mul_of_pos_left (norm_sq_div_sub_div_lt_one _ _) (norm_sq_pos.2 this) ... = norm y : by simp lemma nat_abs_norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm.nat_abs < y.norm.nat_abs := int.coe_nat_lt.1 (by simp [-int.coe_nat_lt, norm_mod_lt x hy]) lemma norm_le_norm_mul_left (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (norm x).nat_abs ≤ (norm (x * y)).nat_abs := by rw [norm_mul, int.nat_abs_mul]; exact le_mul_of_one_le_right (nat.zero_le _) (int.coe_nat_le.1 (by rw [coe_nat_abs_norm]; exact int.add_one_le_of_lt (norm_pos.2 hy))) instance : nontrivial ℤ[i] := ⟨⟨0, 1, dec_trivial⟩⟩ instance : euclidean_domain ℤ[i] := { quotient := (/), remainder := (%), quotient_zero := by { simp [div_def], refl }, quotient_mul_add_remainder_eq := λ _ _, by simp [mod_def], r := _, r_well_founded := measure_wf (int.nat_abs ∘ norm), remainder_lt := nat_abs_norm_mod_lt, mul_left_not_lt := λ a b hb0, not_lt_of_ge $ norm_le_norm_mul_left a hb0, .. gaussian_int.comm_ring, .. gaussian_int.nontrivial } open principal_ideal_ring lemma mod_four_eq_three_of_nat_prime_of_prime (p : ℕ) [hp : fact p.prime] (hpi : prime (p : ℤ[i])) : p % 4 = 3 := hp.1.eq_two_or_odd.elim (λ hp2, absurd hpi (mt irreducible_iff_prime.2 $ λ ⟨hu, h⟩, begin have := h ⟨1, 1⟩ ⟨1, -1⟩ (hp2.symm ▸ rfl), rw [← norm_eq_one_iff, ← norm_eq_one_iff] at this, exact absurd this dec_trivial end)) (λ hp1, by_contradiction $ λ hp3 : p % 4 ≠ 3, have hp41 : p % 4 = 1, begin rw [← nat.mod_mul_left_mod p 2 2, show 2 * 2 = 4, from rfl] at hp1, have := nat.mod_lt p (show 0 < 4, from dec_trivial), revert this hp3 hp1, generalize : p % 4 = m, dec_trivial!, end, let ⟨k, hk⟩ := (zmod.exists_sq_eq_neg_one_iff_mod_four_ne_three p).2 $ by rw hp41; exact dec_trivial in begin obtain ⟨k, k_lt_p, rfl⟩ : ∃ (k' : ℕ) (h : k' < p), (k' : zmod p) = k, { refine ⟨k.val, k.val_lt, zmod.nat_cast_zmod_val k⟩ }, have hpk : p ∣ k ^ 2 + 1, by rw [← char_p.cast_eq_zero_iff (zmod p) p]; simp *, have hkmul : (k ^ 2 + 1 : ℤ[i]) = ⟨k, 1⟩ * ⟨k, -1⟩ := by simp [sq, zsqrtd.ext], have hpne1 : p ≠ 1 := ne_of_gt hp.1.one_lt, have hkltp : 1 + k * k < p * p, from calc 1 + k * k ≤ k + k * k : add_le_add_right (nat.pos_of_ne_zero (λ hk0, by clear_aux_decl; simp [*, pow_succ'] at *)) _ ... = k * (k + 1) : by simp [add_comm, mul_add] ... < p * p : mul_lt_mul k_lt_p k_lt_p (nat.succ_pos _) (nat.zero_le _), have hpk₁ : ¬ (p : ℤ[i]) ∣ ⟨k, -1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k, -1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (-1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpk₂ : ¬ (p : ℤ[i]) ∣ ⟨k, 1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k, 1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [add_comm, norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 (by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1), obtain ⟨y, hy⟩ := hpk, have := hpi.2.2 ⟨k, 1⟩ ⟨k, -1⟩ ⟨y, by rw [← hkmul, ← nat.cast_mul p, ← hy]; simp⟩, clear_aux_decl, tauto end) lemma sq_add_sq_of_nat_prime_of_not_irreducible (p : ℕ) [hp : fact p.prime] (hpi : ¬irreducible (p : ℤ[i])) : ∃ a b, a^2 + b^2 = p := have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $ by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.1.one_lt).symm h.1, have hab : ∃ a b, (p : ℤ[i]) = a * b ∧ ¬ is_unit a ∧ ¬ is_unit b, by simpa [irreducible_iff, hpu, not_forall, not_or_distrib] using hpi, let ⟨a, b, hpab, hau, hbu⟩ := hab in have hnap : (norm a).nat_abs = p, from ((hp.1.mul_eq_prime_sq_iff (mt norm_eq_one_iff.1 hau) (mt norm_eq_one_iff.1 hbu)).1 $ by rw [← int.coe_nat_inj', int.coe_nat_pow, sq, ← @norm_nat_cast (-1), hpab]; simp).1, ⟨a.re.nat_abs, a.im.nat_abs, by simpa [nat_abs_norm_eq, sq] using hnap⟩ lemma prime_of_nat_prime_of_mod_four_eq_three (p : ℕ) [hp : fact p.prime] (hp3 : p % 4 = 3) : prime (p : ℤ[i]) := irreducible_iff_prime.1 $ classical.by_contradiction $ λ hpi, let ⟨a, b, hab⟩ := sq_add_sq_of_nat_prime_of_not_irreducible p hpi in have ∀ a b : zmod 4, a^2 + b^2 ≠ p, by erw [← zmod.nat_cast_mod 4 p, hp3]; exact dec_trivial, this a b (hab ▸ by simp) /-- A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` -/ lemma prime_iff_mod_four_eq_three_of_nat_prime (p : ℕ) [hp : fact p.prime] : prime (p : ℤ[i]) ↔ p % 4 = 3 := ⟨mod_four_eq_three_of_nat_prime_of_prime p, prime_of_nat_prime_of_mod_four_eq_three p⟩ end gaussian_int
5bf62726b32174b7a3a3797cc7b4d5f3e5618b6a
80cc5bf14c8ea85ff340d1d747a127dcadeb966f
/src/data/prod.lean
4fdb81ffd62310f9e6620dad0af2f2806e6fe0b0
[ "Apache-2.0" ]
permissive
lacker/mathlib
f2439c743c4f8eb413ec589430c82d0f73b2d539
ddf7563ac69d42cfa4a1bfe41db1fed521bd795f
refs/heads/master
1,671,948,326,773
1,601,479,268,000
1,601,479,268,000
298,686,743
0
0
Apache-2.0
1,601,070,794,000
1,601,070,794,000
null
UTF-8
Lean
false
false
4,544
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl -/ import tactic.basic /-! # Extra facts about `prod` This file defines `prod.swap : α × β → β × α` and proves various simple lemmas about `prod`. -/ variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} @[simp] theorem prod.forall {p : α × β → Prop} : (∀ x, p x) ↔ (∀ a b, p (a, b)) := ⟨assume h a b, h (a, b), assume h ⟨a, b⟩, h a b⟩ @[simp] theorem prod.exists {p : α × β → Prop} : (∃ x, p x) ↔ (∃ a b, p (a, b)) := ⟨assume ⟨⟨a, b⟩, h⟩, ⟨a, b, h⟩, assume ⟨a, b, h⟩, ⟨⟨a, b⟩, h⟩⟩ @[simp] lemma prod_map (f : α → γ) (g : β → δ) (p : α × β) : prod.map f g p = (f p.1, g p.2) := rfl namespace prod @[simp] lemma map_mk (f : α → γ) (g : β → δ) (a : α) (b : β) : map f g (a, b) = (f a, g b) := rfl lemma map_fst (f : α → γ) (g : β → δ) (p : α × β) : (map f g p).1 = f (p.1) := rfl lemma map_snd (f : α → γ) (g : β → δ) (p : α × β) : (map f g p).2 = g (p.2) := rfl lemma map_fst' (f : α → γ) (g : β → δ) : (prod.fst ∘ map f g) = f ∘ prod.fst := funext $ map_fst f g lemma map_snd' (f : α → γ) (g : β → δ) : (prod.snd ∘ map f g) = g ∘ prod.snd := funext $ map_snd f g @[simp] theorem mk.inj_iff {a₁ a₂ : α} {b₁ b₂ : β} : (a₁, b₁) = (a₂, b₂) ↔ (a₁ = a₂ ∧ b₁ = b₂) := ⟨prod.mk.inj, by cc⟩ lemma ext_iff {p q : α × β} : p = q ↔ p.1 = q.1 ∧ p.2 = q.2 := by rw [← @mk.eta _ _ p, ← @mk.eta _ _ q, mk.inj_iff] @[ext] lemma ext {α β} {p q : α × β} (h₁ : p.1 = q.1) (h₂ : p.2 = q.2) : p = q := ext_iff.2 ⟨h₁, h₂⟩ lemma map_def {f : α → γ} {g : β → δ} : prod.map f g = λ (p : α × β), (f p.1, g p.2) := funext (λ p, ext (map_fst f g p) (map_snd f g p)) lemma id_prod : (λ (p : α × α), (p.1, p.2)) = id := funext $ λ ⟨a, b⟩, rfl lemma fst_surjective [h : nonempty β] : function.surjective (@fst α β) := λ x, h.elim $ λ y, ⟨⟨x, y⟩, rfl⟩ lemma snd_surjective [h : nonempty α] : function.surjective (@snd α β) := λ y, h.elim $ λ x, ⟨⟨x, y⟩, rfl⟩ lemma fst_injective [subsingleton β] : function.injective (@fst α β) := λ x y h, ext h (subsingleton.elim _ _) lemma snd_injective [subsingleton α] : function.injective (@snd α β) := λ x y h, ext (subsingleton.elim _ _) h /-- Swap the factors of a product. `swap (a, b) = (b, a)` -/ def swap : α × β → β × α := λp, (p.2, p.1) @[simp] lemma swap_swap : ∀ x : α × β, swap (swap x) = x | ⟨a, b⟩ := rfl @[simp] lemma fst_swap {p : α × β} : (swap p).1 = p.2 := rfl @[simp] lemma snd_swap {p : α × β} : (swap p).2 = p.1 := rfl @[simp] lemma swap_prod_mk {a : α} {b : β} : swap (a, b) = (b, a) := rfl @[simp] lemma swap_swap_eq : swap ∘ swap = @id (α × β) := funext swap_swap @[simp] lemma swap_left_inverse : function.left_inverse (@swap α β) swap := swap_swap @[simp] lemma swap_right_inverse : function.right_inverse (@swap α β) swap := swap_swap lemma eq_iff_fst_eq_snd_eq : ∀{p q : α × β}, p = q ↔ (p.1 = q.1 ∧ p.2 = q.2) | ⟨p₁, p₂⟩ ⟨q₁, q₂⟩ := by simp lemma fst_eq_iff : ∀ {p : α × β} {x : α}, p.1 = x ↔ p = (x, p.2) | ⟨a, b⟩ x := by simp lemma snd_eq_iff : ∀ {p : α × β} {x : β}, p.2 = x ↔ p = (p.1, x) | ⟨a, b⟩ x := by simp theorem lex_def (r : α → α → Prop) (s : β → β → Prop) {p q : α × β} : prod.lex r s p q ↔ r p.1 q.1 ∨ p.1 = q.1 ∧ s p.2 q.2 := ⟨λ h, by cases h; simp *, λ h, match p, q, h with | (a, b), (c, d), or.inl h := lex.left _ _ h | (a, b), (c, d), or.inr ⟨e, h⟩ := by change a = c at e; subst e; exact lex.right _ h end⟩ instance lex.decidable [decidable_eq α] (r : α → α → Prop) (s : β → β → Prop) [decidable_rel r] [decidable_rel s] : decidable_rel (prod.lex r s) := λ p q, decidable_of_decidable_of_iff (by apply_instance) (lex_def r s).symm end prod open function lemma function.injective.prod_map {f : α → γ} {g : β → δ} (hf : injective f) (hg : injective g) : injective (prod.map f g) := λ x y h, prod.ext (hf (prod.ext_iff.1 h).1) (hg $ (prod.ext_iff.1 h).2) lemma function.surjective.prod_map {f : α → γ} {g : β → δ} (hf : surjective f) (hg : surjective g) : surjective (prod.map f g) := λ p, let ⟨x, hx⟩ := hf p.1 in let ⟨y, hy⟩ := hg p.2 in ⟨(x, y), prod.ext hx hy⟩
134c053812c54866c113f95f057115b0e3ba3d12
0c1546a496eccfb56620165cad015f88d56190c5
/library/init/data/list/lemmas.lean
f07d4a9c432be2248508f9a096a4c563c1324e8d
[ "Apache-2.0" ]
permissive
Solertis/lean
491e0939957486f664498fbfb02546e042699958
84188c5aa1673fdf37a082b2de8562dddf53df3f
refs/heads/master
1,610,174,257,606
1,486,263,620,000
1,486,263,620,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
8,002
lean
/- Copyright (c) 2014 Parikshit Khanna. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Parikshit Khanna, Jeremy Avigad, Leonardo de Moura, Floris van Doorn -/ prelude import init.data.list.basic init.function init.meta universe variables u v variables {α : Type u} {β : Type v} namespace list @[simp] lemma nil_append (s : list α) : [] ++ s = s := rfl lemma cons_append (x : α) (s t : list α) : (x::s) ++ t = x::(s ++ t) := rfl @[simp] lemma map_nil (f : α → β) : map f [] = [] := rfl lemma map_cons (f : α → β) (a : α) (l : list α) : map f (a :: l) = f a :: map f l := rfl @[simp] lemma length_nil : length (@nil α) = 0 := rfl @[simp] lemma length_cons (x : α) (t : list α) : length (x::t) = length t + 1 := rfl @[simp] lemma nth_zero (a : α) (l : list α) : nth (a :: l) 0 = some a := rfl @[simp] lemma nth_succ (a : α) (l : list α) (n : nat) : nth (a::l) (nat.succ n) = nth l n := rfl /- list membership -/ @[simp] lemma mem_nil_iff (a : α) : a ∈ ([] : list α) ↔ false := iff.rfl @[simp] lemma not_mem_nil (a : α) : a ∉ ([] : list α) := iff.mp $ mem_nil_iff a @[simp] lemma mem_cons_self (a : α) (l : list α) : a ∈ a :: l := or.inl rfl lemma eq_nil_of_forall_not_mem : ∀ {l : list α}, (∀ a, a ∉ l) → l = nil | [] := assume h, rfl | (b :: l') := assume h, absurd (mem_cons_self b l') (h b) lemma mem_cons_of_mem (y : α) {a : α} {l : list α} : a ∈ l → a ∈ y :: l := assume H, or.inr H lemma eq_or_mem_of_mem_cons {a y : α} {l : list α} : a ∈ y::l → a = y ∨ a ∈ l := assume h, h lemma mem_singleton {a b : α} : a ∈ [b] → a = b := suppose a ∈ [b], or.elim (eq_or_mem_of_mem_cons this) (suppose a = b, this) (suppose a ∈ [], absurd this (not_mem_nil a)) @[simp] lemma mem_singleton_iff (a b : α) : a ∈ [b] ↔ a = b := iff.intro mem_singleton (begin intro h, simp [h] end) lemma mem_of_mem_cons_of_mem {a b : α} {l : list α} : a ∈ b::l → b ∈ l → a ∈ l := assume ainbl binl, or.elim (eq_or_mem_of_mem_cons ainbl) (suppose a = b, begin subst a, exact binl end) (suppose a ∈ l, this) lemma mem_or_mem_of_mem_append {a : α} : ∀ {s t : list α}, a ∈ s ++ t → a ∈ s ∨ a ∈ t | [] t h := or.inr h | (b::s) t h := have a = b ∨ a ∈ s ++ t, from h, match this with | or.inl h₁ := or.inl (h₁ ▸ mem_cons_self _ _) | or.inr h₁ := have a ∈ s ∨ a ∈ t, from mem_or_mem_of_mem_append h₁, match this with | or.inl h₂ := or.inl (mem_cons_of_mem _ h₂) | or.inr h₂ := or.inr h₂ end end lemma mem_append_of_mem_or_mem {a : α} {s t : list α} : a ∈ s ∨ a ∈ t → a ∈ s ++ t := list.induction_on s (take h, or.elim h false.elim id) (take b s, assume ih : a ∈ s ∨ a ∈ t → a ∈ s ++ t, suppose a ∈ b::s ∨ a ∈ t, or.elim this (suppose a ∈ b::s, or.elim (eq_or_mem_of_mem_cons this) (suppose a = b, or.inl this) (suppose a ∈ s, or.inr (ih (or.inl this)))) (suppose a ∈ t, or.inr (ih (or.inr this)))) @[simp] lemma mem_append_iff (a : α) (s t : list α) : a ∈ s ++ t ↔ a ∈ s ∨ a ∈ t := iff.intro mem_or_mem_of_mem_append mem_append_of_mem_or_mem lemma not_mem_of_not_mem_append_left {a : α} {s t : list α} : a ∉ s++t → a ∉ s := λ nainst ains, absurd (mem_append_of_mem_or_mem (or.inl ains)) nainst lemma not_mem_of_not_mem_append_right {a : α} {s t : list α} : a ∉ s++t → a ∉ t := λ nainst aint, absurd (mem_append_of_mem_or_mem (or.inr aint)) nainst lemma not_mem_append {a : α} {s t : list α} : a ∉ s → a ∉ t → a ∉ s++t := λ nains naint ainst, or.elim (mem_or_mem_of_mem_append ainst) (λ ains, by contradiction) (λ aint, by contradiction) lemma length_pos_of_mem {a : α} : ∀ {l : list α}, a ∈ l → 0 < length l | [] := suppose a ∈ [], begin simp at this, contradiction end | (b::l) := suppose a ∈ b::l, nat.zero_lt_succ _ lemma mem_split {a : α} {l : list α} : a ∈ l → ∃ s t : list α, l = s ++ (a::t) := list.induction_on l (suppose a ∈ [], begin simp at this, contradiction end) (take b l, assume ih : a ∈ l → ∃ s t : list α, l = s ++ (a::t), suppose a ∈ b::l, or.elim (eq_or_mem_of_mem_cons this) (suppose a = b, ⟨[], l, begin rw this, reflexivity end⟩) (suppose a ∈ l, match (ih this) with | ⟨s, t, (h : l = s ++ (a::t))⟩ := ⟨b::s, t, begin rw h, reflexivity end⟩ end)) lemma mem_append_left {a : α} {l₁ : list α} (l₂ : list α) : a ∈ l₁ → a ∈ l₁ ++ l₂ := assume ainl₁, mem_append_of_mem_or_mem (or.inl ainl₁) lemma mem_append_right {a : α} (l₁ : list α) {l₂ : list α} : a ∈ l₂ → a ∈ l₁ ++ l₂ := assume ainl₂, mem_append_of_mem_or_mem (or.inr ainl₂) lemma mem_of_ne_of_mem {a y : α} {l : list α} (h₁ : a ≠ y) (h₂ : a ∈ y :: l) : a ∈ l := or.elim (eq_or_mem_of_mem_cons h₂) (λe, absurd e h₁) (λr, r) lemma ne_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ≠ b := assume nin aeqb, absurd (or.inl aeqb) nin lemma not_mem_of_not_mem_cons {a b : α} {l : list α} : a ∉ b::l → a ∉ l := assume nin nainl, absurd (or.inr nainl) nin lemma not_mem_cons_of_ne_of_not_mem {a y : α} {l : list α} : a ≠ y → a ∉ l → a ∉ y::l := assume P1 P2, not.intro (assume Pain, absurd (eq_or_mem_of_mem_cons Pain) (not_or P1 P2)) lemma ne_and_not_mem_of_not_mem_cons {a y : α} {l : list α} : a ∉ y::l → a ≠ y ∧ a ∉ l := assume P, and.intro (ne_of_not_mem_cons P) (not_mem_of_not_mem_cons P) -- TODO(Jeremy): move this to data/list/set.lean definition sublist (l₁ l₂ : list α) := ∀ ⦃a : α⦄, a ∈ l₁ → a ∈ l₂ instance : has_subset (list α) := ⟨sublist⟩ @[simp] lemma nil_subset (l : list α) : [] ⊆ l := λ b i, false.elim (iff.mp (mem_nil_iff b) i) @[simp] lemma subset.refl (l : list α) : l ⊆ l := λ b i, i lemma subset.trans {l₁ l₂ l₃ : list α} (h₁ : l₁ ⊆ l₂) (h₂ : l₂ ⊆ l₃) : l₁ ⊆ l₃ := λ b i, h₂ (h₁ i) @[simp] lemma subset_cons (a : α) (l : list α) : l ⊆ a::l := λ b i, or.inr i lemma subset_of_cons_subset {a : α} {l₁ l₂ : list α} : a::l₁ ⊆ l₂ → l₁ ⊆ l₂ := λ s b i, s (mem_cons_of_mem _ i) lemma cons_subset_cons {l₁ l₂ : list α} (a : α) (s : l₁ ⊆ l₂) : (a::l₁) ⊆ (a::l₂) := λ b hin, or.elim (eq_or_mem_of_mem_cons hin) (λ e : b = a, or.inl e) (λ i : b ∈ l₁, or.inr (s i)) @[simp] lemma subset_append_left (l₁ l₂ : list α) : l₁ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inl i) @[simp] lemma subset_append_right (l₁ l₂ : list α) : l₂ ⊆ l₁++l₂ := λ b i, iff.mpr (mem_append_iff b l₁ l₂) (or.inr i) lemma subset_cons_of_subset (a : α) {l₁ l₂ : list α} : l₁ ⊆ l₂ → l₁ ⊆ (a::l₂) := λ (s : l₁ ⊆ l₂) (a : α) (i : a ∈ l₁), or.inr (s i) lemma subset_app_of_subset_left (l l₁ l₂ : list α) : l ⊆ l₁ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₁) (a : α) (ainl : a ∈ l), have a ∈ l₁, from s ainl, mem_append_of_mem_or_mem (or.inl this) lemma subset_app_of_subset_right (l l₁ l₂ : list α) : l ⊆ l₂ → l ⊆ l₁++l₂ := λ (s : l ⊆ l₂) (a : α) (ainl : a ∈ l), have a ∈ l₂, from s ainl, mem_append_of_mem_or_mem (or.inr this) lemma cons_subset_of_subset_of_mem {a : α} {l m : list α} (ainm : a ∈ m) (lsubm : l ⊆ m) : a::l ⊆ m := take b, suppose b ∈ a::l, or.elim (eq_or_mem_of_mem_cons this) (suppose b = a, begin subst b, exact ainm end) (suppose b ∈ l, lsubm this) lemma app_subset_of_subset_of_subset {l₁ l₂ l : list α} (l₁subl : l₁ ⊆ l) (l₂subl : l₂ ⊆ l) : l₁ ++ l₂ ⊆ l := take a, suppose a ∈ l₁ ++ l₂, or.elim (mem_or_mem_of_mem_append this) (suppose a ∈ l₁, l₁subl this) (suppose a ∈ l₂, l₂subl this) end list
01ab2759bc3dd93bc1ce432841954d9f086aee01
618003631150032a5676f229d13a079ac875ff77
/src/algebra/group/units.lean
bcf73ca38794da7bd24c8a9db4498c021d5254ae
[ "Apache-2.0" ]
permissive
awainverse/mathlib
939b68c8486df66cfda64d327ad3d9165248c777
ea76bd8f3ca0a8bf0a166a06a475b10663dec44a
refs/heads/master
1,659,592,962,036
1,590,987,592,000
1,590,987,592,000
268,436,019
1
0
Apache-2.0
1,590,990,500,000
1,590,990,500,000
null
UTF-8
Lean
false
false
10,441
lean
/- Copyright (c) 2017 Kenny Lau. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kenny Lau, Mario Carneiro, Johannes Hölzl, Chris Hughes, Jens Wagemaker -/ import algebra.group.basic import tactic.ext import tactic.norm_cast /-! # Units (i.e., invertible elements) of a multiplicative monoid -/ universe u variable {α : Type u} /-- Units of a monoid, bundled version. An element of a `monoid` is a unit if it has a two-sided inverse. This version bundles the inverse element so that it can be computed. For a predicate see `is_unit`. -/ structure units (α : Type u) [monoid α] := (val : α) (inv : α) (val_inv : val * inv = 1) (inv_val : inv * val = 1) /-- Units of an add_monoid, bundled version. An element of an add_monoid is a unit if it has a two-sided additive inverse. This version bundles the inverse element so that it can be computed. For a predicate see `is_add_unit`. -/ structure add_units (α : Type u) [add_monoid α] := (val : α) (neg : α) (val_neg : val + neg = 0) (neg_val : neg + val = 0) attribute [to_additive add_units] units namespace units variables [monoid α] @[to_additive] instance : has_coe (units α) α := ⟨val⟩ @[simp, to_additive] lemma coe_mk (a : α) (b h₁ h₂) : ↑(units.mk a b h₁ h₂) = a := rfl @[ext, to_additive] theorem ext : function.injective (coe : units α → α) | ⟨v, i₁, vi₁, iv₁⟩ ⟨v', i₂, vi₂, iv₂⟩ e := by change v = v' at e; subst v'; congr; simpa only [iv₂, vi₁, one_mul, mul_one] using mul_assoc i₂ v i₁ @[to_additive] theorem ext_iff {a b : units α} : a = b ↔ (a : α) = b := ext.eq_iff.symm @[to_additive] instance [decidable_eq α] : decidable_eq (units α) := λ a b, decidable_of_iff' _ ext_iff /-- Units of a monoid form a group. -/ @[to_additive] instance : group (units α) := { mul := λ u₁ u₂, ⟨u₁.val * u₂.val, u₂.inv * u₁.inv, by rw [mul_assoc, ← mul_assoc u₂.val, val_inv, one_mul, val_inv], by rw [mul_assoc, ← mul_assoc u₁.inv, inv_val, one_mul, inv_val]⟩, one := ⟨1, 1, one_mul 1, one_mul 1⟩, mul_one := λ u, ext $ mul_one u, one_mul := λ u, ext $ one_mul u, mul_assoc := λ u₁ u₂ u₃, ext $ mul_assoc u₁ u₂ u₃, inv := λ u, ⟨u.2, u.1, u.4, u.3⟩, mul_left_inv := λ u, ext u.inv_val } variables (a b : units α) {c : units α} @[simp, norm_cast, to_additive] lemma coe_mul : (↑(a * b) : α) = a * b := rfl attribute [norm_cast] add_units.coe_add @[simp, norm_cast, to_additive] lemma coe_one : ((1 : units α) : α) = 1 := rfl attribute [norm_cast] add_units.coe_zero @[to_additive] lemma val_coe : (↑a : α) = a.val := rfl @[norm_cast, to_additive] lemma coe_inv : ((a⁻¹ : units α) : α) = a.inv := rfl attribute [norm_cast] add_units.coe_neg @[simp, to_additive] lemma inv_mul : (↑a⁻¹ * a : α) = 1 := inv_val _ @[simp, to_additive] lemma mul_inv : (a * ↑a⁻¹ : α) = 1 := val_inv _ @[simp, to_additive] lemma mul_inv_cancel_left (a : units α) (b : α) : (a:α) * (↑a⁻¹ * b) = b := by rw [← mul_assoc, mul_inv, one_mul] @[simp, to_additive] lemma inv_mul_cancel_left (a : units α) (b : α) : (↑a⁻¹:α) * (a * b) = b := by rw [← mul_assoc, inv_mul, one_mul] @[simp, to_additive] lemma mul_inv_cancel_right (a : α) (b : units α) : a * b * ↑b⁻¹ = a := by rw [mul_assoc, mul_inv, mul_one] @[simp, to_additive] lemma inv_mul_cancel_right (a : α) (b : units α) : a * ↑b⁻¹ * b = a := by rw [mul_assoc, inv_mul, mul_one] @[to_additive] instance : inhabited (units α) := ⟨1⟩ @[to_additive] instance {α} [comm_monoid α] : comm_group (units α) := { mul_comm := λ u₁ u₂, ext $ mul_comm _ _, ..units.group } @[to_additive] instance [has_repr α] : has_repr (units α) := ⟨repr ∘ val⟩ @[simp, to_additive] theorem mul_right_inj (a : units α) {b c : α} : (a:α) * b = a * c ↔ b = c := ⟨λ h, by simpa only [inv_mul_cancel_left] using congr_arg ((*) ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[simp, to_additive] theorem mul_left_inj (a : units α) {b c : α} : b * a = c * a ↔ b = c := ⟨λ h, by simpa only [mul_inv_cancel_right] using congr_arg (* ↑(a⁻¹ : units α)) h, congr_arg _⟩ @[to_additive] theorem eq_mul_inv_iff_mul_eq {a b : α} : a = b * ↑c⁻¹ ↔ a * c = b := ⟨λ h, by rw [h, inv_mul_cancel_right], λ h, by rw [← h, mul_inv_cancel_right]⟩ @[to_additive] theorem eq_inv_mul_iff_mul_eq {a c : α} : a = ↑b⁻¹ * c ↔ ↑b * a = c := ⟨λ h, by rw [h, mul_inv_cancel_left], λ h, by rw [← h, inv_mul_cancel_left]⟩ @[to_additive] theorem inv_mul_eq_iff_eq_mul {b c : α} : ↑a⁻¹ * b = c ↔ b = a * c := ⟨λ h, by rw [← h, mul_inv_cancel_left], λ h, by rw [h, inv_mul_cancel_left]⟩ @[to_additive] theorem mul_inv_eq_iff_eq_mul {a c : α} : a * ↑b⁻¹ = c ↔ a = c * b := ⟨λ h, by rw [← h, inv_mul_cancel_right], λ h, by rw [h, mul_inv_cancel_right]⟩ end units /-- For `a, b` in a `comm_monoid` such that `a * b = 1`, makes a unit out of `a`. -/ @[to_additive "For `a, b` in an `add_comm_monoid` such that `a + b = 0`, makes an add_unit out of `a`."] def units.mk_of_mul_eq_one [comm_monoid α] (a b : α) (hab : a * b = 1) : units α := ⟨a, b, hab, (mul_comm b a).trans hab⟩ @[simp, to_additive] lemma units.coe_mk_of_mul_eq_one [comm_monoid α] {a b : α} (h : a * b = 1) : (units.mk_of_mul_eq_one a b h : α) = a := rfl section monoid variables [monoid α] {a b c : α} /-- Partial division. It is defined when the second argument is invertible, and unlike the division operator in `division_ring` it is not totalized at zero. -/ def divp (a : α) (u) : α := a * (u⁻¹ : units α) infix ` /ₚ `:70 := divp @[simp] theorem divp_self (u : units α) : (u : α) /ₚ u = 1 := units.mul_inv _ @[simp] theorem divp_one (a : α) : a /ₚ 1 = a := mul_one _ theorem divp_assoc (a b : α) (u : units α) : a * b /ₚ u = a * (b /ₚ u) := mul_assoc _ _ _ @[simp] theorem divp_inv (u : units α) : a /ₚ u⁻¹ = a * u := rfl @[simp] theorem divp_mul_cancel (a : α) (u : units α) : a /ₚ u * u = a := (mul_assoc _ _ _).trans $ by rw [units.inv_mul, mul_one] @[simp] theorem mul_divp_cancel (a : α) (u : units α) : (a * u) /ₚ u = a := (mul_assoc _ _ _).trans $ by rw [units.mul_inv, mul_one] @[simp] theorem divp_left_inj (u : units α) {a b : α} : a /ₚ u = b /ₚ u ↔ a = b := units.mul_left_inj _ theorem divp_divp_eq_divp_mul (x : α) (u₁ u₂ : units α) : (x /ₚ u₁) /ₚ u₂ = x /ₚ (u₂ * u₁) := by simp only [divp, mul_inv_rev, units.coe_mul, mul_assoc] theorem divp_eq_iff_mul_eq {x : α} {u : units α} {y : α} : x /ₚ u = y ↔ y * u = x := u.mul_left_inj.symm.trans $ by rw [divp_mul_cancel]; exact ⟨eq.symm, eq.symm⟩ theorem divp_eq_one_iff_eq {a : α} {u : units α} : a /ₚ u = 1 ↔ a = u := (units.mul_left_inj u).symm.trans $ by rw [divp_mul_cancel, one_mul] @[simp] theorem one_divp (u : units α) : 1 /ₚ u = ↑u⁻¹ := one_mul _ end monoid section comm_monoid variables [comm_monoid α] theorem divp_eq_divp_iff {x y : α} {ux uy : units α} : x /ₚ ux = y /ₚ uy ↔ x * uy = y * ux := by rw [divp_eq_iff_mul_eq, mul_comm, ← divp_assoc, divp_eq_iff_mul_eq, mul_comm y ux] theorem divp_mul_divp (x y : α) (ux uy : units α) : (x /ₚ ux) * (y /ₚ uy) = (x * y) /ₚ (ux * uy) := by rw [← divp_divp_eq_divp_mul, divp_assoc, mul_comm x, divp_assoc, mul_comm] end comm_monoid /-! # `is_unit` predicate In this file we define the `is_unit` predicate on a `monoid`, and prove a few basic properties. For the bundled version see `units`. See also `prime`, `associated`, and `irreducible` in `algebra/associated`. -/ section is_unit variables {M : Type*} {N : Type*} /-- An element `a : M` of a monoid is a unit if it has a two-sided inverse. The actual definition says that `a` is equal to some `u : units M`, where `units M` is a bundled version of `is_unit`. -/ @[to_additive is_add_unit "An element `a : M` of an add_monoid is an `add_unit` if it has a two-sided additive inverse. The actual definition says that `a` is equal to some `u : add_units M`, where `add_units M` is a bundled version of `is_add_unit`."] def is_unit [monoid M] (a : M) : Prop := ∃ u : units M, (u : M) = a @[simp, to_additive is_add_unit_add_unit] lemma is_unit_unit [monoid M] (u : units M) : is_unit (u : M) := ⟨u, rfl⟩ @[simp, to_additive is_add_unit_zero] theorem is_unit_one [monoid M] : is_unit (1:M) := ⟨1, rfl⟩ @[to_additive is_add_unit_of_add_eq_zero] theorem is_unit_of_mul_eq_one [comm_monoid M] (a b : M) (h : a * b = 1) : is_unit a := ⟨units.mk_of_mul_eq_one a b h, rfl⟩ @[to_additive is_add_unit_iff_exists_neg] theorem is_unit_iff_exists_inv [comm_monoid M] {a : M} : is_unit a ↔ ∃ b, a * b = 1 := ⟨by rintro ⟨⟨a, b, hab, _⟩, rfl⟩; exact ⟨b, hab⟩, λ ⟨b, hab⟩, is_unit_of_mul_eq_one _ b hab⟩ @[to_additive is_add_unit_iff_exists_neg'] theorem is_unit_iff_exists_inv' [comm_monoid M] {a : M} : is_unit a ↔ ∃ b, b * a = 1 := by simp [is_unit_iff_exists_inv, mul_comm] /-- Multiplication by a `u : units M` doesn't affect `is_unit`. -/ @[simp, to_additive is_add_unit_add_add_units "Addition of a `u : add_units M` doesn't affect `is_add_unit`."] theorem units.is_unit_mul_units [monoid M] (a : M) (u : units M) : is_unit (a * u) ↔ is_unit a := iff.intro (assume ⟨v, hv⟩, have is_unit (a * ↑u * ↑u⁻¹), by existsi v * u⁻¹; rw [←hv, units.coe_mul], by rwa [mul_assoc, units.mul_inv, mul_one] at this) (assume ⟨v, hv⟩, hv ▸ ⟨v * u, (units.coe_mul v u).symm⟩) @[to_additive is_add_unit_of_add_is_add_unit_left] theorem is_unit_of_mul_is_unit_left [comm_monoid M] {x y : M} (hu : is_unit (x * y)) : is_unit x := let ⟨z, hz⟩ := is_unit_iff_exists_inv.1 hu in is_unit_iff_exists_inv.2 ⟨y * z, by rwa ← mul_assoc⟩ @[to_additive] theorem is_unit_of_mul_is_unit_right [comm_monoid M] {x y : M} (hu : is_unit (x * y)) : is_unit y := @is_unit_of_mul_is_unit_left _ _ y x $ by rwa mul_comm @[to_additive] theorem is_unit.mul_right_inj [monoid M] {a b c : M} (ha : is_unit a) : a * b = a * c ↔ b = c := by cases ha with a ha; rw [←ha, units.mul_right_inj] @[to_additive] theorem is_unit.mul_left_inj [monoid M] {a b c : M} (ha : is_unit a) : b * a = c * a ↔ b = c := by cases ha with a ha; rw [←ha, units.mul_left_inj] end is_unit
1f0cb11099ec0903b0c3877c362f8481847b3b0f
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/Lean3Lib/data/rbtree/insert_auto.lean
821d1a30279057486f76f907056153ad268cd739
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
24,664
lean
/- Copyright (c) 2017 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.Lean3Lib.data.rbtree.find universes u v namespace Mathlib namespace rbnode @[simp] theorem balance1_eq₁ {α : Type u} (l : rbnode α) (x : α) (r₁ : rbnode α) (y : α) (r₂ : rbnode α) (v : α) (t : rbnode α) : balance1 (red_node l x r₁) y r₂ v t = red_node (black_node l x r₁) y (black_node r₂ v t) := sorry @[simp] theorem balance1_eq₂ {α : Type u} (l₁ : rbnode α) (y : α) (l₂ : rbnode α) (x : α) (r : rbnode α) (v : α) (t : rbnode α) : get_color l₁ ≠ color.red → balance1 l₁ y (red_node l₂ x r) v t = red_node (black_node l₁ y l₂) x (black_node r v t) := sorry @[simp] theorem balance1_eq₃ {α : Type u} (l : rbnode α) (y : α) (r : rbnode α) (v : α) (t : rbnode α) : get_color l ≠ color.red → get_color r ≠ color.red → balance1 l y r v t = black_node (red_node l y r) v t := sorry @[simp] theorem balance2_eq₁ {α : Type u} (l : rbnode α) (x₁ : α) (r₁ : rbnode α) (y : α) (r₂ : rbnode α) (v : α) (t : rbnode α) : balance2 (red_node l x₁ r₁) y r₂ v t = red_node (black_node t v l) x₁ (black_node r₁ y r₂) := sorry @[simp] theorem balance2_eq₂ {α : Type u} (l₁ : rbnode α) (y : α) (l₂ : rbnode α) (x₂ : α) (r₂ : rbnode α) (v : α) (t : rbnode α) : get_color l₁ ≠ color.red → balance2 l₁ y (red_node l₂ x₂ r₂) v t = red_node (black_node t v l₁) y (black_node l₂ x₂ r₂) := sorry @[simp] theorem balance2_eq₃ {α : Type u} (l : rbnode α) (y : α) (r : rbnode α) (v : α) (t : rbnode α) : get_color l ≠ color.red → get_color r ≠ color.red → balance2 l y r v t = black_node t v (red_node l y r) := sorry /- We can use the same induction principle for balance1 and balance2 -/ theorem balance.cases {α : Type u} {p : rbnode α → α → rbnode α → Prop} (l : rbnode α) (y : α) (r : rbnode α) (red_left : ∀ (l : rbnode α) (x : α) (r₁ : rbnode α) (y : α) (r₂ : rbnode α), p (red_node l x r₁) y r₂) (red_right : ∀ (l₁ : rbnode α) (y : α) (l₂ : rbnode α) (x : α) (r : rbnode α), get_color l₁ ≠ color.red → p l₁ y (red_node l₂ x r)) (other : ∀ (l : rbnode α) (y : α) (r : rbnode α), get_color l ≠ color.red → get_color r ≠ color.red → p l y r) : p l y r := sorry theorem balance1_ne_leaf {α : Type u} (l : rbnode α) (x : α) (r : rbnode α) (v : α) (t : rbnode α) : balance1 l x r v t ≠ leaf := sorry theorem balance1_node_ne_leaf {α : Type u} {s : rbnode α} (a : α) (t : rbnode α) : s ≠ leaf → balance1_node s a t ≠ leaf := sorry theorem balance2_ne_leaf {α : Type u} (l : rbnode α) (x : α) (r : rbnode α) (v : α) (t : rbnode α) : balance2 l x r v t ≠ leaf := sorry theorem balance2_node_ne_leaf {α : Type u} {s : rbnode α} (a : α) (t : rbnode α) : s ≠ leaf → balance2_node s a t ≠ leaf := sorry theorem ins.induction {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {p : rbnode α → Prop} (t : rbnode α) (x : α) (is_leaf : p leaf) (is_red_lt : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.lt → p a → p (red_node a y b)) (is_red_eq : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.eq → p (red_node a y b)) (is_red_gt : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.gt → p b → p (red_node a y b)) (is_black_lt_red : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.lt → get_color a = color.red → p a → p (black_node a y b)) (is_black_lt_not_red : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.lt → get_color a ≠ color.red → p a → p (black_node a y b)) (is_black_eq : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.eq → p (black_node a y b)) (is_black_gt_red : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.gt → get_color b = color.red → p b → p (black_node a y b)) (is_black_gt_not_red : ∀ (a : rbnode α) (y : α) (b : rbnode α), cmp_using lt x y = ordering.gt → get_color b ≠ color.red → p b → p (black_node a y b)) : p t := sorry theorem is_searchable_balance1 {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {l : rbnode α} {y : α} {r : rbnode α} {v : α} {t : rbnode α} {lo : Option α} {hi : Option α} : is_searchable lt l lo (some y) → is_searchable lt r (some y) (some v) → is_searchable lt t (some v) hi → is_searchable lt (balance1 l y r v t) lo hi := sorry theorem is_searchable_balance1_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} [is_trans α lt] {y : α} {s : rbnode α} {lo : Option α} {hi : Option α} : is_searchable lt t lo (some y) → is_searchable lt s (some y) hi → is_searchable lt (balance1_node t y s) lo hi := sorry theorem is_searchable_balance2 {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {l : rbnode α} {y : α} {r : rbnode α} {v : α} {t : rbnode α} {lo : Option α} {hi : Option α} : is_searchable lt t lo (some v) → is_searchable lt l (some v) (some y) → is_searchable lt r (some y) hi → is_searchable lt (balance2 l y r v t) lo hi := sorry theorem is_searchable_balance2_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} [is_trans α lt] {y : α} {s : rbnode α} {lo : Option α} {hi : Option α} : is_searchable lt s lo (some y) → is_searchable lt t (some y) hi → is_searchable lt (balance2_node t y s) lo hi := sorry theorem is_searchable_ins {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} {x : α} [is_strict_weak_order α lt] {lo : Option α} {hi : Option α} (h : is_searchable lt t lo hi) : lift lt lo (some x) → lift lt (some x) hi → is_searchable lt (ins lt t x) lo hi := sorry theorem is_searchable_mk_insert_result {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {c : color} {t : rbnode α} : is_searchable lt t none none → is_searchable lt (mk_insert_result c t) none none := sorry theorem is_searchable_insert {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} {x : α} [is_strict_weak_order α lt] : is_searchable lt t none none → is_searchable lt (insert lt t x) none none := sorry end rbnode namespace rbnode theorem mem_balance1_node_of_mem_left {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {s : rbnode α} (v : α) (t : rbnode α) : mem lt x s → mem lt x (balance1_node s v t) := sorry theorem mem_balance2_node_of_mem_left {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {s : rbnode α} (v : α) (t : rbnode α) : mem lt x s → mem lt x (balance2_node s v t) := sorry theorem mem_balance1_node_of_mem_right {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {t : rbnode α} (v : α) (s : rbnode α) : mem lt x t → mem lt x (balance1_node s v t) := sorry theorem mem_balance2_node_of_mem_right {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {t : rbnode α} (v : α) (s : rbnode α) : mem lt x t → mem lt x (balance2_node s v t) := sorry theorem mem_balance1_node_of_incomp {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {v : α} (s : rbnode α) (t : rbnode α) : ¬lt x v ∧ ¬lt v x → s ≠ leaf → mem lt x (balance1_node s v t) := sorry theorem mem_balance2_node_of_incomp {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {x : α} {v : α} (s : rbnode α) (t : rbnode α) : ¬lt v x ∧ ¬lt x v → s ≠ leaf → mem lt x (balance2_node s v t) := sorry theorem ins_ne_leaf {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (t : rbnode α) (x : α) : ins lt t x ≠ leaf := sorry theorem insert_ne_leaf {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (t : rbnode α) (x : α) : insert lt t x ≠ leaf := sorry theorem mem_ins_of_incomp {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (t : rbnode α) {x : α} {y : α} (h : ¬lt x y ∧ ¬lt y x) : mem lt x (ins lt t y) := sorry theorem mem_ins_of_mem {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} (z : α) {x : α} (h : mem lt x t) : mem lt x (ins lt t z) := sorry theorem mem_mk_insert_result {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {a : α} {t : rbnode α} (c : color) : mem lt a t → mem lt a (mk_insert_result c t) := sorry theorem mem_of_mem_mk_insert_result {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {a : α} {t : rbnode α} {c : color} : mem lt a (mk_insert_result c t) → mem lt a t := sorry theorem mem_insert_of_incomp {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (t : rbnode α) {x : α} {y : α} (h : ¬lt x y ∧ ¬lt y x) : mem lt x (insert lt t y) := sorry theorem mem_insert_of_mem {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {x : α} (z : α) : mem lt x t → mem lt x (insert lt t z) := fun (ᾰ : mem lt x t) => mem_mk_insert_result lt (get_color t) (mem_ins_of_mem lt z ᾰ) theorem of_mem_balance1_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {s : rbnode α} {v : α} {t : rbnode α} : mem lt x (balance1_node s v t) → mem lt x s ∨ ¬lt x v ∧ ¬lt v x ∨ mem lt x t := sorry theorem of_mem_balance2_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {s : rbnode α} {v : α} {t : rbnode α} : mem lt x (balance2_node s v t) → mem lt x s ∨ ¬lt x v ∧ ¬lt v x ∨ mem lt x t := sorry theorem equiv_or_mem_of_mem_ins {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {x : α} {z : α} (h : mem lt x (ins lt t z)) : strict_weak_order.equiv x z ∨ mem lt x t := sorry theorem equiv_or_mem_of_mem_insert {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {x : α} {z : α} (h : mem lt x (insert lt t z)) : strict_weak_order.equiv x z ∨ mem lt x t := sorry theorem mem_exact_balance1_node_of_mem_exact {α : Type u} {x : α} {s : rbnode α} (v : α) (t : rbnode α) : mem_exact x s → mem_exact x (balance1_node s v t) := sorry theorem mem_exact_balance2_node_of_mem_exact {α : Type u} {x : α} {s : rbnode α} (v : α) (t : rbnode α) : mem_exact x s → mem_exact x (balance2_node s v t) := sorry theorem find_balance1_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {z : α} {t : rbnode α} {s : rbnode α} {lo : Option α} {hi : Option α} : is_searchable lt t lo (some z) → is_searchable lt s (some z) hi → find lt t y = some x → strict_weak_order.equiv y x → find lt (balance1_node t z s) y = some x := sorry theorem find_balance2_node {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {z : α} {s : rbnode α} {t : rbnode α} [is_trans α lt] {lo : Option α} {hi : Option α} : is_searchable lt s lo (some z) → is_searchable lt t (some z) hi → find lt t y = some x → strict_weak_order.equiv y x → find lt (balance2_node t z s) y = some x := sorry /- Auxiliary lemma -/ theorem ite_eq_of_not_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_order α lt] {a : α} {b : α} {β : Type v} (t : β) (s : β) (h : lt b a) : ite (lt a b) t s = s := sorry theorem find_ins_of_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {t : rbnode α} (he : strict_weak_order.equiv x y) {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) (hlt₁ : lift lt lo (some x)) (hlt₂ : lift lt (some x) hi) : find lt (ins lt t x) y = some x := sorry theorem find_mk_insert_result {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (c : color) (t : rbnode α) (x : α) : find lt (mk_insert_result c t) x = find lt t x := sorry theorem find_insert_of_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {t : rbnode α} (he : strict_weak_order.equiv x y) : is_searchable lt t none none → find lt (insert lt t x) y = some x := sorry theorem weak_trichotomous {α : Type u} (lt : α → α → Prop) [DecidableRel lt] (x : α) (y : α) {p : Prop} (is_lt : lt x y → p) (is_eqv : ¬lt x y ∧ ¬lt y x → p) (is_gt : lt y x → p) : p := dite (lt x y) (fun (h : lt x y) => dite (lt y x) (fun (h_1 : lt y x) => is_lt h) fun (h_1 : ¬lt y x) => is_lt h) fun (h : ¬lt x y) => dite (lt y x) (fun (h : lt y x) => is_gt h) fun (h_1 : ¬lt y x) => is_eqv { left := h, right := h_1 } theorem find_black_eq_find_red {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {l : rbnode α} {y : α} {r : rbnode α} {x : α} : find lt (black_node l y r) x = find lt (red_node l y r) x := sorry theorem find_red_of_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {l : rbnode α} {y : α} {r : rbnode α} {x : α} (h : lt x y) : find lt (red_node l y r) x = find lt l x := sorry theorem find_red_of_gt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_order α lt] {l : rbnode α} {y : α} {r : rbnode α} {x : α} (h : lt y x) : find lt (red_node l y r) x = find lt r x := sorry theorem find_red_of_incomp {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {l : rbnode α} {y : α} {r : rbnode α} {x : α} (h : ¬lt x y ∧ ¬lt y x) : find lt (red_node l y r) x = some y := sorry theorem find_balance1_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {r : rbnode α} {t : rbnode α} {v : α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt x y) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = find lt (red_node l v r) x := sorry theorem find_balance1_node_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {s : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (hlt : lt y x) (ht : is_searchable lt t lo (some x)) (hs : is_searchable lt s (some x) hi) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance1_node t x s) y = find lt t y := sorry theorem find_balance1_gt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {r : rbnode α} {t : rbnode α} {v : α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt y x) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = find lt t x := sorry theorem find_balance1_node_gt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {s : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt x y) (ht : is_searchable lt t lo (some x)) (hs : is_searchable lt s (some x) hi) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance1_node t x s) y = find lt s y := sorry theorem find_balance1_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {r : rbnode α} {t : rbnode α} {v : α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : ¬lt x y ∧ ¬lt y x) (hl : is_searchable lt l lo (some v)) (hr : is_searchable lt r (some v) (some y)) (ht : is_searchable lt t (some y) hi) : find lt (balance1 l v r y t) x = some y := sorry theorem find_balance1_node_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {s : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : ¬lt x y ∧ ¬lt y x) (ht : is_searchable lt t lo (some y)) (hs : is_searchable lt s (some y) hi) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance1_node t y s) x = some y := sorry theorem find_balance2_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {v : α} {r : rbnode α} {t : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt x y) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = find lt t x := sorry theorem find_balance2_node_lt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {s : rbnode α} {t : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt x y) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance2_node t y s) x = find lt s x := sorry theorem find_balance2_gt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {v : α} {r : rbnode α} {t : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt y x) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = find lt (red_node l v r) x := sorry theorem find_balance2_node_gt {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {s : rbnode α} {t : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : lt y x) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance2_node t y s) x = find lt t x := sorry theorem find_balance2_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {l : rbnode α} {v : α} {r : rbnode α} {t : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : ¬lt x y ∧ ¬lt y x) (hl : is_searchable lt l (some y) (some v)) (hr : is_searchable lt r (some v) hi) (ht : is_searchable lt t lo (some y)) : find lt (balance2 l v r y t) x = some y := sorry theorem find_balance2_node_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {t : rbnode α} {s : rbnode α} {x : α} {y : α} {lo : Option α} {hi : Option α} (h : ¬lt x y ∧ ¬lt y x) (ht : is_searchable lt t (some y) hi) (hs : is_searchable lt s lo (some y)) (hne : autoParam (t ≠ leaf) (Lean.Syntax.ident Lean.SourceInfo.none (String.toSubstring "Mathlib.rbnode.ins_ne_leaf_tac") (Lean.Name.mkStr (Lean.Name.mkStr (Lean.Name.mkStr Lean.Name.anonymous "Mathlib") "rbnode") "ins_ne_leaf_tac") [])) : find lt (balance2_node t y s) x = some y := sorry theorem find_ins_of_disj {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {t : rbnode α} (hn : lt x y ∨ lt y x) {lo : Option α} {hi : Option α} (hs : is_searchable lt t lo hi) (hlt₁ : lift lt lo (some x)) (hlt₂ : lift lt (some x) hi) : find lt (ins lt t x) y = find lt t y := sorry theorem find_insert_of_disj {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {t : rbnode α} (hd : lt x y ∨ lt y x) : is_searchable lt t none none → find lt (insert lt t x) y = find lt t y := sorry theorem find_insert_of_not_eqv {α : Type u} (lt : α → α → Prop) [DecidableRel lt] [is_strict_weak_order α lt] {x : α} {y : α} {t : rbnode α} (hn : ¬strict_weak_order.equiv x y) : is_searchable lt t none none → find lt (insert lt t x) y = find lt t y := sorry inductive is_bad_red_black {α : Type u} : rbnode α → ℕ → Prop where | bad_red : ∀ {c₁ c₂ : color} {n : ℕ} {l r : rbnode α} {v : α}, is_red_black l c₁ n → is_red_black r c₂ n → is_bad_red_black (red_node l v r) n theorem balance1_rb {α : Type u} {l : rbnode α} {r : rbnode α} {t : rbnode α} {y : α} {v : α} {c_l : color} {c_r : color} {c_t : color} {n : ℕ} : is_red_black l c_l n → is_red_black r c_r n → is_red_black t c_t n → ∃ (c : color), is_red_black (balance1 l y r v t) c (Nat.succ n) := sorry theorem balance2_rb {α : Type u} {l : rbnode α} {r : rbnode α} {t : rbnode α} {y : α} {v : α} {c_l : color} {c_r : color} {c_t : color} {n : ℕ} : is_red_black l c_l n → is_red_black r c_r n → is_red_black t c_t n → ∃ (c : color), is_red_black (balance2 l y r v t) c (Nat.succ n) := sorry theorem balance1_node_rb {α : Type u} {t : rbnode α} {s : rbnode α} {y : α} {c : color} {n : ℕ} : is_bad_red_black t n → is_red_black s c n → ∃ (c : color), is_red_black (balance1_node t y s) c (Nat.succ n) := sorry theorem balance2_node_rb {α : Type u} {t : rbnode α} {s : rbnode α} {y : α} {c : color} {n : ℕ} : is_bad_red_black t n → is_red_black s c n → ∃ (c : color), is_red_black (balance2_node t y s) c (Nat.succ n) := sorry def ins_rb_result {α : Type u} : rbnode α → color → ℕ → Prop := sorry theorem of_get_color_eq_red {α : Type u} {t : rbnode α} {c : color} {n : ℕ} : get_color t = color.red → is_red_black t c n → c = color.red := sorry theorem of_get_color_ne_red {α : Type u} {t : rbnode α} {c : color} {n : ℕ} : get_color t ≠ color.red → is_red_black t c n → c = color.black := sorry theorem ins_rb {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} (x : α) {c : color} {n : ℕ} (h : is_red_black t c n) : ins_rb_result (ins lt t x) c n := sorry def insert_rb_result {α : Type u} : rbnode α → color → ℕ → Prop := sorry theorem insert_rb {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} (x : α) {c : color} {n : ℕ} (h : is_red_black t c n) : insert_rb_result (insert lt t x) c n := sorry theorem insert_is_red_black {α : Type u} (lt : α → α → Prop) [DecidableRel lt] {t : rbnode α} {c : color} {n : ℕ} (x : α) : is_red_black t c n → ∃ (c : color), ∃ (n : ℕ), is_red_black (insert lt t x) c n := sorry end Mathlib
12f9fac2c14826ca65566bd2b1fdb0494d5a68bc
63abd62053d479eae5abf4951554e1064a4c45b4
/src/logic/unique.lean
ab9c2a51b5c42aa9d3ad12cc907d6480403dd01b
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
4,370
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import tactic.basic /-! # Types with a unique term In this file we define a typeclass `unique`, which expresses that a type has a unique term. In other words, a type that is `inhabited` and a `subsingleton`. ## Main declaration * `unique`: a typeclass that expresses that a type has a unique term. ## Main statements * `unique.mk'`: an inhabited subsingleton type is `unique`. This can not be an instance because it would lead to loops in typeclass inference. * `function.surjective.unique`: if the domain of a surjective function is `unique`, then its codomain is `unique` as well. * `function.injective.subsingleton`: if the codomain of an injective function is `subsingleton`, then its domain is `subsingleton` as well. * `function.injective.unique`: if the codomain of an injective function is `subsingleton` and its domain is `inhabited`, then its domain is `unique`. ## Implementation details The typeclass `unique α` is implemented as a type, rather than a `Prop`-valued predicate, for good definitional properties of the default term. -/ universes u v w variables {α : Sort u} {β : Sort v} {γ : Sort w} /-- `unique α` expresses that `α` is a type with a unique term `default α`. This is implemented as a type, rather than a `Prop`-valued predicate, for good definitional properties of the default term. -/ @[ext] structure unique (α : Sort u) extends inhabited α := (uniq : ∀ a:α, a = default) attribute [class] unique instance punit.unique : unique punit.{u} := { default := punit.star, uniq := λ x, punit_eq x _ } instance fin.unique : unique (fin 1) := { default := 0, uniq := λ ⟨n, hn⟩, fin.eq_of_veq (nat.eq_zero_of_le_zero (nat.le_of_lt_succ hn)) } namespace unique open function section variables [unique α] @[priority 100] -- see Note [lower instance priority] instance : inhabited α := to_inhabited ‹unique α› lemma eq_default (a : α) : a = default α := uniq _ a lemma default_eq (a : α) : default α = a := (uniq _ a).symm @[priority 100] -- see Note [lower instance priority] instance : subsingleton α := ⟨λ a b, by rw [eq_default a, eq_default b]⟩ lemma forall_iff {p : α → Prop} : (∀ a, p a) ↔ p (default α) := ⟨λ h, h _, λ h x, by rwa [unique.eq_default x]⟩ lemma exists_iff {p : α → Prop} : Exists p ↔ p (default α) := ⟨λ ⟨a, ha⟩, eq_default a ▸ ha, exists.intro (default α)⟩ end @[ext] protected lemma subsingleton_unique' : ∀ (h₁ h₂ : unique α), h₁ = h₂ | ⟨⟨x⟩, h⟩ ⟨⟨y⟩, _⟩ := by congr; rw [h x, h y] instance subsingleton_unique : subsingleton (unique α) := ⟨unique.subsingleton_unique'⟩ /-- Construct `unique` from `inhabited` and `subsingleton`. Making this an instance would create a loop in the class inheritance graph. -/ def mk' (α : Sort u) [h₁ : inhabited α] [subsingleton α] : unique α := { uniq := λ x, subsingleton.elim _ _, .. h₁ } end unique @[simp] lemma pi.default_def {β : Π a : α, Sort v} [Π a, inhabited (β a)] : default (Π a, β a) = λ a, default (β a) := rfl lemma pi.default_apply {β : Π a : α, Sort v} [Π a, inhabited (β a)] (a : α) : default (Π a, β a) a = default (β a) := rfl instance pi.unique {β : Π a : α, Sort v} [Π a, unique (β a)] : unique (Π a, β a) := { uniq := λ f, funext $ λ x, unique.eq_default _, .. pi.inhabited α } namespace function variable {f : α → β} /-- If the domain of a surjective function is a singleton, then the codomain is a singleton as well. -/ protected def surjective.unique (hf : surjective f) [unique α] : unique β := { default := f (default _), uniq := λ b, let ⟨a, ha⟩ := hf b in ha ▸ congr_arg f (unique.eq_default _) } /-- If the codomain of an injective function is a subsingleton, then the domain is a subsingleton as well. -/ protected lemma injective.subsingleton (hf : injective f) [subsingleton β] : subsingleton α := ⟨λ x y, hf $ subsingleton.elim _ _⟩ /-- If `α` is inhabited and admits an injective map to a subsingleton type, then `α` is `unique`. -/ protected def injective.unique [inhabited α] [subsingleton β] (hf : injective f) : unique α := @unique.mk' _ _ hf.subsingleton end function
bdf385b325b86511932f436bfff2c951885fb433
9dc8cecdf3c4634764a18254e94d43da07142918
/src/topology/sober.lean
a4ac489f6758e3c9498d13054eed411cc8f6f5ad
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
9,226
lean
/- Copyright (c) 2021 Andrew Yang. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Andrew Yang -/ import topology.separation import topology.continuous_function.basic /-! # Sober spaces A quasi-sober space is a topological space where every irreducible closed subset has a generic point. A sober space is a quasi-sober space where every irreducible closed subset has a *unique* generic point. This is if and only if the space is T0, and thus sober spaces can be stated via `[quasi_sober α] [t0_space α]`. ## Main definition * `is_generic_point` : `x` is the generic point of `S` if `S` is the closure of `x`. * `quasi_sober` : A space is quasi-sober if every irreducible closed subset has a generic point. -/ open set variables {α β : Type*} [topological_space α] [topological_space β] section generic_point /-- `x` is a generic point of `S` if `S` is the closure of `x`. -/ def is_generic_point (x : α) (S : set α) : Prop := closure ({x} : set α) = S lemma is_generic_point_def {x : α} {S : set α} : is_generic_point x S ↔ closure ({x} : set α) = S := iff.rfl lemma is_generic_point.def {x : α} {S : set α} (h : is_generic_point x S) : closure ({x} : set α) = S := h lemma is_generic_point_closure {x : α} : is_generic_point x (closure ({x} : set α)) := refl _ variables {x y : α} {S U Z : set α} lemma is_generic_point_iff_specializes : is_generic_point x S ↔ ∀ y, x ⤳ y ↔ y ∈ S := by simp only [specializes_iff_mem_closure, is_generic_point, set.ext_iff] namespace is_generic_point lemma specializes_iff_mem (h : is_generic_point x S) : x ⤳ y ↔ y ∈ S := is_generic_point_iff_specializes.1 h y lemma specializes (h : is_generic_point x S) (h' : y ∈ S) : x ⤳ y := h.specializes_iff_mem.2 h' lemma mem (h : is_generic_point x S) : x ∈ S := h.specializes_iff_mem.1 specializes_rfl protected lemma is_closed (h : is_generic_point x S) : is_closed S := h.def ▸ is_closed_closure protected lemma is_irreducible (h : is_generic_point x S) : is_irreducible S := h.def ▸ is_irreducible_singleton.closure /-- In a T₀ space, each set has at most one generic point. -/ protected lemma eq [t0_space α] (h : is_generic_point x S) (h' : is_generic_point y S) : x = y := ((h.specializes h'.mem).antisymm (h'.specializes h.mem)).eq lemma mem_open_set_iff (h : is_generic_point x S) (hU : is_open U) : x ∈ U ↔ (S ∩ U).nonempty := ⟨λ h', ⟨x, h.mem, h'⟩, λ ⟨y, hyS, hyU⟩, (h.specializes hyS).mem_open hU hyU⟩ lemma disjoint_iff (h : is_generic_point x S) (hU : is_open U) : disjoint S U ↔ x ∉ U := by rw [h.mem_open_set_iff hU, ← not_disjoint_iff_nonempty_inter, not_not] lemma mem_closed_set_iff (h : is_generic_point x S) (hZ : is_closed Z) : x ∈ Z ↔ S ⊆ Z := by rw [← h.def, hZ.closure_subset_iff, singleton_subset_iff] protected lemma image (h : is_generic_point x S) {f : α → β} (hf : continuous f) : is_generic_point (f x) (closure (f '' S)) := begin rw [is_generic_point_def, ← h.def, ← image_singleton], exact subset.antisymm (closure_mono (image_subset _ subset_closure)) (closure_minimal (image_closure_subset_closure_image hf) is_closed_closure) end end is_generic_point lemma is_generic_point_iff_forall_closed (hS : is_closed S) (hxS : x ∈ S) : is_generic_point x S ↔ ∀ Z : set α, is_closed Z → x ∈ Z → S ⊆ Z := have closure {x} ⊆ S, from closure_minimal (singleton_subset_iff.2 hxS) hS, by simp_rw [is_generic_point, subset_antisymm_iff, this, true_and, closure, subset_sInter_iff, mem_set_of_eq, and_imp, singleton_subset_iff] end generic_point section sober /-- A space is sober if every irreducible closed subset has a generic point. -/ @[mk_iff] class quasi_sober (α : Type*) [topological_space α] : Prop := (sober : ∀ {S : set α} (hS₁ : is_irreducible S) (hS₂ : is_closed S), ∃ x, is_generic_point x S) /-- A generic point of the closure of an irreducible space. -/ noncomputable def is_irreducible.generic_point [quasi_sober α] {S : set α} (hS : is_irreducible S) : α := (quasi_sober.sober hS.closure is_closed_closure).some lemma is_irreducible.generic_point_spec [quasi_sober α] {S : set α} (hS : is_irreducible S) : is_generic_point hS.generic_point (closure S) := (quasi_sober.sober hS.closure is_closed_closure).some_spec @[simp] lemma is_irreducible.generic_point_closure_eq [quasi_sober α] {S : set α} (hS : is_irreducible S) : closure ({hS.generic_point} : set α) = closure S := hS.generic_point_spec variable (α) /-- A generic point of a sober irreducible space. -/ noncomputable def generic_point [quasi_sober α] [irreducible_space α] : α := (irreducible_space.is_irreducible_univ α).generic_point lemma generic_point_spec [quasi_sober α] [irreducible_space α] : is_generic_point (generic_point α) ⊤ := by simpa using (irreducible_space.is_irreducible_univ α).generic_point_spec @[simp] lemma generic_point_closure [quasi_sober α] [irreducible_space α] : closure ({generic_point α} : set α) = ⊤ := generic_point_spec α variable {α} lemma generic_point_specializes [quasi_sober α] [irreducible_space α] (x : α) : generic_point α ⤳ x := (is_irreducible.generic_point_spec _).specializes (by simp) local attribute [instance, priority 10] specialization_order /-- The closed irreducible subsets of a sober space bijects with the points of the space. -/ noncomputable def irreducible_set_equiv_points [quasi_sober α] [t0_space α] : { s : set α | is_irreducible s ∧ is_closed s } ≃o α := { to_fun := λ s, s.prop.1.generic_point, inv_fun := λ x, ⟨closure ({x} : set α), is_irreducible_singleton.closure, is_closed_closure⟩, left_inv := λ s, subtype.eq $ eq.trans (s.prop.1.generic_point_spec) $ closure_eq_iff_is_closed.mpr s.2.2, right_inv := λ x, is_irreducible_singleton.closure.generic_point_spec.eq (by { convert is_generic_point_closure using 1, rw closure_closure }), map_rel_iff' := λ s t, by { change _ ⤳ _ ↔ _, rw specializes_iff_closure_subset, simp [s.prop.2.closure_eq, t.prop.2.closure_eq, ← subtype.coe_le_coe] } } lemma closed_embedding.quasi_sober {f : α → β} (hf : closed_embedding f) [quasi_sober β] : quasi_sober α := begin constructor, intros S hS hS', have hS'' := hS.image f hf.continuous.continuous_on, obtain ⟨x, hx⟩ := quasi_sober.sober hS'' (hf.is_closed_map _ hS'), obtain ⟨y, hy, rfl⟩ := hx.mem, use y, change _ = _ at hx, apply set.image_injective.mpr hf.inj, rw [← hx, ← hf.closure_image_eq, set.image_singleton] end lemma open_embedding.quasi_sober {f : α → β} (hf : open_embedding f) [quasi_sober β] : quasi_sober α := begin constructor, intros S hS hS', have hS'' := hS.image f hf.continuous.continuous_on, obtain ⟨x, hx⟩ := quasi_sober.sober hS''.closure is_closed_closure, obtain ⟨T, hT, rfl⟩ := hf.to_inducing.is_closed_iff.mp hS', rw set.image_preimage_eq_inter_range at hx hS'', have hxT : x ∈ T, { rw ← hT.closure_eq, exact closure_mono (set.inter_subset_left _ _) hx.mem }, have hxU : x ∈ set.range f, { rw hx.mem_open_set_iff hf.open_range, refine set.nonempty.mono _ hS''.1, simpa using subset_closure }, rcases hxU with ⟨y, rfl⟩, use y, change _ = _, rw [hf.to_embedding.closure_eq_preimage_closure_image, set.image_singleton, (show _ = _, from hx)], apply set.image_injective.mpr hf.inj, ext z, simp only [set.image_preimage_eq_inter_range, set.mem_inter_eq, and.congr_left_iff], exact λ hy, ⟨λ h, hT.closure_eq ▸ closure_mono (set.inter_subset_left _ _) h, λ h, subset_closure ⟨h, hy⟩⟩ end /-- A space is quasi sober if it can be covered by open quasi sober subsets. -/ lemma quasi_sober_of_open_cover (S : set (set α)) (hS : ∀ s : S, is_open (s : set α)) [hS' : ∀ s : S, quasi_sober s] (hS'' : ⋃₀ S = ⊤) : quasi_sober α := begin rw quasi_sober_iff, intros t h h', obtain ⟨x, hx⟩ := h.1, obtain ⟨U, hU, hU'⟩ : x ∈ ⋃₀S := by { rw hS'', trivial }, haveI : quasi_sober U := hS' ⟨U, hU⟩, have H : is_preirreducible (coe ⁻¹' t : set U) := h.2.preimage (hS ⟨U, hU⟩).open_embedding_subtype_coe, replace H : is_irreducible (coe ⁻¹' t : set U) := ⟨⟨⟨x, hU'⟩, by simpa using hx⟩, H⟩, use H.generic_point, have := continuous_subtype_coe.closure_preimage_subset _ H.generic_point_spec.mem, rw h'.closure_eq at this, apply le_antisymm, { apply h'.closure_subset_iff.mpr, simpa using this }, rw [← set.image_singleton, ← closure_closure], have := closure_mono (image_closure_subset_closure_image (@continuous_subtype_coe α _ U)), refine set.subset.trans _ this, rw H.generic_point_spec.def, refine (subset_closure_inter_of_is_preirreducible_of_is_open h.2 (hS ⟨U, hU⟩) ⟨x, hx, hU'⟩).trans (closure_mono _), rw ← subtype.image_preimage_coe, exact set.image_subset _ subset_closure, end @[priority 100] instance t2_space.quasi_sober [t2_space α] : quasi_sober α := begin constructor, rintro S h -, obtain ⟨x, rfl⟩ := (is_irreducible_iff_singleton S).mp h, exact ⟨x, closure_singleton⟩ end end sober
623f1f4894518d5f507fbb57a81b856e48c3f94b
6fca17f8d5025f89be1b2d9d15c9e0c4b4900cbf
/src/game/world5/level5.lean
264101e038a8c73a2eb4ed16a6e0442aa113daaa
[ "Apache-2.0" ]
permissive
arolihas/natural_number_game
4f0c93feefec93b8824b2b96adff8b702b8b43ce
8e4f7b4b42888a3b77429f90cce16292bd288138
refs/heads/master
1,621,872,426,808
1,586,270,467,000
1,586,270,467,000
253,648,466
0
0
null
1,586,219,694,000
1,586,219,694,000
null
UTF-8
Lean
false
false
2,347
lean
/- # Function world. ## Level 5: `P → (Q → P)`. In this level, our goal is to construct a function, like in level 2. ``` ⊢ P → (Q → P) ``` So $P$ and $Q$ are sets, and our goal is to construct a function which takes an element of $P$ and outputs a function from $Q$ to $P$. We don't know anything at all about the sets $P$ and $Q$, so initially this seems like a bit of a tall order. But let's give it a go. Delete the `sorry` and let's think about how to proceed. Our goal is `P → X` for some set $X=\operatorname{Hom}(Q,P)$, and if our goal is to construct a function then we almost always want to use the `intro` tactic from level 2, Lean's version of "let $p\in P$ be arbitrary." So let's start with `intro p,` and we then find ourselves in this state: ``` P Q : Type, p : P ⊢ Q → P ``` We now have an arbitrary element $p\in P$ and we are supposed to be constructing a function $Q\to P$. Well, how about the constant function, which sends everything to $p$? This will work. So let $q\in Q$ be arbitrary: `intro q,` and then let's output `p`. `exact p,` -/ /- Definition We define an element of $\operatorname{Hom}(P,\operatorname{Hom}(Q,P))$. -/ example (P Q : Type) : P → (Q → P) := begin end /- A mathematician would treat the set `P → (Q → P)` as the same as the set `P × Q → P`, because to give an element of either function space is just to give a rule which takes an element of $P$ and an element of $Q$, and returns an element of $P$. Thinking of the goal as a function from `P × Q` to `P` we realise that it's just projection onto the first factor. ## Did you notice? I wrote `P → (Q → P)` but Lean just writes `P → Q → P`. This is because computer scientists adopt the convention that `→` is *right associative*, which is a fancy way of saying "when we write `P → Q → R`, we mean `P → (Q → R)`." Mathematicians use right associativity as a convention for powers: if a mathematician says $10^{10^{10}}$ they don't mean $(10^{10})^{10}=10^{100}$, they mean $10^{(10^{10})}$. So `10 ^ 10 ^ 10` in Lean means `10 ^ (10 ^ 10)` and not `(10 ^ 10) ^ 10`. However they use left associativity as a convention for subtraction: if a mathematician writes $6 - 2 - 1$ they mean $(6 - 2) - 1$ and not $6 - (2 - 1)$. ## Pro tip `intros p q,` is the same as `intro p, intro q,`. -/
afb1b5f9c4f62391f5c0b58fcec2ab1871a72034
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebra/order.lean
d9d8b433bc22055a9fde020fe543c44d91cadafc
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
10,053
lean
/- Copyright (c) 2017 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro -/ universe u variables {α : Type u} @[simp] lemma ge_iff_le [preorder α] {a b : α} : a ≥ b ↔ b ≤ a := iff.rfl @[simp] lemma gt_iff_lt [preorder α] {a b : α} : a > b ↔ b < a := iff.rfl lemma not_le_of_lt [preorder α] {a b : α} (h : a < b) : ¬ b ≤ a := (le_not_le_of_lt h).right lemma not_lt_of_le [preorder α] {a b : α} (h : a ≤ b) : ¬ b < a | hab := not_le_of_gt hab h lemma le_iff_eq_or_lt [partial_order α] {a b : α} : a ≤ b ↔ a = b ∨ a < b := le_iff_lt_or_eq.trans or.comm lemma lt_of_le_of_ne' [partial_order α] {a b : α} (h₁ : a ≤ b) (h₂ : a ≠ b) : a < b := lt_of_le_not_le h₁ $ mt (le_antisymm h₁) h₂ lemma lt_iff_le_and_ne [partial_order α] {a b : α} : a < b ↔ a ≤ b ∧ a ≠ b := ⟨λ h, ⟨le_of_lt h, ne_of_lt h⟩, λ ⟨h1, h2⟩, lt_of_le_of_ne h1 h2⟩ lemma eq_iff_le_not_lt [partial_order α] {a b : α} : a = b ↔ a ≤ b ∧ ¬ a < b := ⟨λ h, ⟨le_of_eq h, h ▸ lt_irrefl _⟩, λ ⟨h₁, h₂⟩, le_antisymm h₁ $ classical.by_contradiction $ λ h₃, h₂ (lt_of_le_not_le h₁ h₃)⟩ lemma eq_or_lt_of_le [partial_order α] {a b : α} (h : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le h).symm lemma lt_of_not_ge' [linear_order α] {a b : α} (h : ¬ b ≤ a) : a < b := lt_of_le_not_le ((le_total _ _).resolve_right h) h lemma lt_iff_not_ge' [linear_order α] {x y : α} : x < y ↔ ¬ y ≤ x := ⟨not_le_of_gt, lt_of_not_ge'⟩ @[simp] lemma not_lt [linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_gt, not_lt_of_ge⟩ lemma le_of_not_lt [linear_order α] {a b : α} : ¬ a < b → b ≤ a := not_lt.1 @[simp] lemma not_le [linear_order α] {a b : α} : ¬ a ≤ b ↔ b < a := lt_iff_not_ge'.symm lemma lt_or_le [linear_order α] : ∀ a b : α, a < b ∨ b ≤ a := lt_or_ge lemma le_or_lt [linear_order α] : ∀ a b : α, a ≤ b ∨ b < a := le_or_gt lemma not_lt_iff_eq_or_lt [linear_order α] {a b : α} : ¬ a < b ↔ a = b ∨ b < a := not_lt.trans $ le_iff_eq_or_lt.trans $ or_congr eq_comm iff.rfl lemma exists_ge_of_linear [linear_order α] (a b : α) : ∃ c, a ≤ c ∧ b ≤ c := match le_total a b with | or.inl h := ⟨_, h, le_refl _⟩ | or.inr h := ⟨_, le_refl _, h⟩ end lemma lt_imp_lt_of_le_imp_le {β} [linear_order α] [preorder β] {a b : α} {c d : β} (H : a ≤ b → c ≤ d) (h : d < c) : b < a := lt_of_not_ge' $ λ h', not_lt_of_ge (H h') h lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_gt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma lt_iff_lt_of_le_iff_le' {β} [preorder α] [preorder β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) (H' : b ≤ a ↔ d ≤ c) : b < a ↔ d < c := lt_iff_le_not_le.trans $ (and_congr H' (not_congr H)).trans lt_iff_le_not_le.symm lemma lt_iff_lt_of_le_iff_le {β} [linear_order α] [linear_order β] {a b : α} {c d : β} (H : a ≤ b ↔ c ≤ d) : b < a ↔ d < c := not_le.symm.trans $ iff.trans (not_congr H) $ not_le lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ lemma eq_of_forall_le_iff [partial_order α] {a b : α} (H : ∀ c, c ≤ a ↔ c ≤ b) : a = b := le_antisymm ((H _).1 (le_refl _)) ((H _).2 (le_refl _)) lemma le_of_forall_le [preorder α] {a b : α} (H : ∀ c, c ≤ a → c ≤ b) : a ≤ b := H _ (le_refl _) lemma le_of_forall_le' [preorder α] {a b : α} (H : ∀ c, a ≤ c → b ≤ c) : b ≤ a := H _ (le_refl _) lemma le_of_forall_lt [linear_order α] {a b : α} (H : ∀ c, c < a → c < b) : a ≤ b := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le [linear_order α] {a b : α} : (∀ ⦃c⦄, c < a → c < b) ↔ a ≤ b := ⟨le_of_forall_lt, λ h c hca, lt_of_lt_of_le hca h⟩ lemma le_of_forall_lt' [linear_order α] {a b : α} (H : ∀ c, a < c → b < c) : b ≤ a := le_of_not_lt $ λ h, lt_irrefl _ (H _ h) lemma forall_lt_iff_le' [linear_order α] {a b : α} : (∀ ⦃c⦄, a < c → b < c) ↔ b ≤ a := ⟨le_of_forall_lt', λ h c hac, lt_of_le_of_lt h hac⟩ lemma eq_of_forall_ge_iff [partial_order α] {a b : α} (H : ∀ c, a ≤ c ↔ b ≤ c) : a = b := le_antisymm ((H _).2 (le_refl _)) ((H _).1 (le_refl _)) /-- monotonicity of `≤` with respect to `→` -/ lemma le_implies_le_of_le_of_le {a b c d : α} [preorder α] (h₀ : c ≤ a) (h₁ : b ≤ d) : a ≤ b → c ≤ d := assume h₂ : a ≤ b, calc c ≤ a : h₀ ... ≤ b : h₂ ... ≤ d : h₁ namespace decidable local attribute [instance, priority 10] classical.prop_decidable lemma lt_or_eq_of_le [partial_order α] {a b : α} (hab : a ≤ b) : a < b ∨ a = b := if hba : b ≤ a then or.inr (le_antisymm hab hba) else or.inl (lt_of_le_not_le hab hba) lemma eq_or_lt_of_le [partial_order α] {a b : α} (hab : a ≤ b) : a = b ∨ a < b := (lt_or_eq_of_le hab).swap lemma le_iff_lt_or_eq [partial_order α] {a b : α} : a ≤ b ↔ a < b ∨ a = b := ⟨lt_or_eq_of_le, le_of_lt_or_eq⟩ lemma le_of_not_lt [linear_order α] {a b : α} (h : ¬ b < a) : a ≤ b := decidable.by_contradiction $ λ h', h $ lt_of_le_not_le ((le_total _ _).resolve_right h') h' lemma not_lt [linear_order α] {a b : α} : ¬ a < b ↔ b ≤ a := ⟨le_of_not_lt, not_lt_of_ge⟩ lemma lt_or_le [linear_order α] (a b : α) : a < b ∨ b ≤ a := if hba : b ≤ a then or.inr hba else or.inl $ not_le.1 hba lemma le_or_lt [linear_order α] (a b : α) : a ≤ b ∨ b < a := (lt_or_le b a).swap lemma lt_trichotomy [linear_order α] (a b : α) : a < b ∨ a = b ∨ b < a := (lt_or_le _ _).imp_right $ λ h, (eq_or_lt_of_le h).imp_left eq.symm lemma lt_or_gt_of_ne [linear_order α] {a b : α} (h : a ≠ b) : a < b ∨ b < a := (lt_trichotomy a b).imp_right $ λ h', h'.resolve_left h def lt_by_cases [decidable_linear_order α] (x y : α) {P : Sort*} (h₁ : x < y → P) (h₂ : x = y → P) (h₃ : y < x → P) : P := begin by_cases h : x < y, { exact h₁ h }, by_cases h' : y < x, { exact h₃ h' }, apply h₂, apply le_antisymm; apply le_of_not_gt; assumption end lemma ne_iff_lt_or_gt [linear_order α] {a b : α} : a ≠ b ↔ a < b ∨ b < a := ⟨lt_or_gt_of_ne, λo, o.elim ne_of_lt ne_of_gt⟩ lemma le_imp_le_of_lt_imp_lt {β} [preorder α] [linear_order β] {a b : α} {c d : β} (H : d < c → b < a) (h : a ≤ b) : c ≤ d := le_of_not_lt $ λ h', not_le_of_gt (H h') h lemma le_imp_le_iff_lt_imp_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b → c ≤ d) ↔ (d < c → b < a) := ⟨lt_imp_lt_of_le_imp_le, le_imp_le_of_lt_imp_lt⟩ lemma le_iff_le_iff_lt_iff_lt {β} [linear_order α] [linear_order β] {a b : α} {c d : β} : (a ≤ b ↔ c ≤ d) ↔ (b < a ↔ d < c) := ⟨lt_iff_lt_of_le_iff_le, λ H, not_lt.symm.trans $ iff.trans (not_congr H) $ not_lt⟩ end decidable namespace ordering /-- `compares o a b` means that `a` and `b` have the ordering relation `o` between them, assuming that the relation `a < b` is defined -/ @[simp] def compares [has_lt α] : ordering → α → α → Prop | lt a b := a < b | eq a b := a = b | gt a b := a > b theorem compares.eq_lt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = lt ↔ a < b) | lt a b h := ⟨λ _, h, λ _, rfl⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h' h).elim⟩ | gt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ theorem compares.eq_eq [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = eq ↔ a = b) | lt a b h := ⟨λ h, by injection h, λ h', (ne_of_lt h h').elim⟩ | eq a b h := ⟨λ _, h, λ _, rfl⟩ | gt a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h h').elim⟩ theorem compares.eq_gt [preorder α] : ∀ {o} {a b : α}, compares o a b → (o = gt ↔ b < a) | lt a b h := ⟨λ h, by injection h, λ h', (lt_asymm h h').elim⟩ | eq a b h := ⟨λ h, by injection h, λ h', (ne_of_gt h' h).elim⟩ | gt a b h := ⟨λ _, h, λ _, rfl⟩ theorem compares.inj [preorder α] {o₁} : ∀ {o₂} {a b : α}, compares o₁ a b → compares o₂ a b → o₁ = o₂ | lt a b h₁ h₂ := h₁.eq_lt.2 h₂ | eq a b h₁ h₂ := h₁.eq_eq.2 h₂ | gt a b h₁ h₂ := h₁.eq_gt.2 h₂ theorem compares_iff_of_compares_impl {β : Type*} [linear_order α] [preorder β] {a b : α} {a' b' : β} (h : ∀ {o}, compares o a b → compares o a' b') (o) : compares o a b ↔ compares o a' b' := begin refine ⟨h, λ ho, _⟩, cases lt_trichotomy a b with hab hab, { change compares ordering.lt a b at hab, rwa [ho.inj (h hab)] }, { cases hab with hab hab, { change compares ordering.eq a b at hab, rwa [ho.inj (h hab)] }, { change compares ordering.gt a b at hab, rwa [ho.inj (h hab)] } } end theorem swap_or_else (o₁ o₂) : (or_else o₁ o₂).swap = or_else o₁.swap o₂.swap := by cases o₁; try {refl}; cases o₂; refl theorem or_else_eq_lt (o₁ o₂) : or_else o₁ o₂ = lt ↔ o₁ = lt ∨ (o₁ = eq ∧ o₂ = lt) := by cases o₁; cases o₂; exact dec_trivial end ordering theorem cmp_compares [decidable_linear_order α] (a b : α) : (cmp a b).compares a b := begin unfold cmp cmp_using, by_cases a < b; simp [h], by_cases h₂ : b < a; simp [h₂, gt], exact (lt_or_eq_of_le (le_of_not_gt h₂)).resolve_left h end theorem cmp_swap [preorder α] [@decidable_rel α (<)] (a b : α) : (cmp a b).swap = cmp b a := begin unfold cmp cmp_using, by_cases a < b; by_cases h₂ : b < a; simp [h, h₂, gt, ordering.swap], exact lt_asymm h h₂ end
0b84d441d4b99b31e90b5c8fd5fa89f12d9717f3
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/data/set/basic.lean
f5234e04233e5e408b3d5c28ebe714eab6808840
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
48,441
lean
/- Copyright (c) 2014 Jeremy Avigad. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Jeremy Avigad, Leonardo de Moura -/ import tactic.ext tactic.finish data.subtype tactic.interactive open function /- set coercion to a type -/ namespace set instance {α : Type*} : has_coe_to_sort (set α) := ⟨_, λ s, {x // x ∈ s}⟩ end set section set_coe universe u variables {α : Type u} theorem set.set_coe_eq_subtype (s : set α) : coe_sort.{(u+1) (u+2)} s = {x // x ∈ s} := rfl @[simp] theorem set_coe.forall {s : set α} {p : s → Prop} : (∀ x : s, p x) ↔ (∀ x (h : x ∈ s), p ⟨x, h⟩) := subtype.forall @[simp] theorem set_coe.exists {s : set α} {p : s → Prop} : (∃ x : s, p x) ↔ (∃ x (h : x ∈ s), p ⟨x, h⟩) := subtype.exists @[simp] theorem set_coe_cast : ∀ {s t : set α} (H' : s = t) (H : @eq (Type u) s t) (x : s), cast H x = ⟨x.1, H' ▸ x.2⟩ | s _ rfl _ ⟨x, h⟩ := rfl theorem set_coe.ext {s : set α} {a b : s} : (↑a : α) = ↑b → a = b := subtype.eq theorem set_coe.ext_iff {s : set α} {a b : s} : (↑a : α) = ↑b ↔ a = b := iff.intro set_coe.ext (assume h, h ▸ rfl) end set_coe lemma subtype.mem {α : Type*} {s : set α} (p : s) : (p : α) ∈ s := p.property namespace set universes u v w x variables {α : Type u} {β : Type v} {γ : Type w} {ι : Sort x} {a : α} {s t : set α} instance : inhabited (set α) := ⟨∅⟩ @[extensionality] theorem ext {a b : set α} (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := funext (assume x, propext (h x)) theorem ext_iff (s t : set α) : s = t ↔ ∀ x, x ∈ s ↔ x ∈ t := ⟨λ h x, by rw h, ext⟩ @[trans] theorem mem_of_mem_of_subset {α : Type u} {x : α} {s t : set α} (hx : x ∈ s) (h : s ⊆ t) : x ∈ t := h hx /- mem and set_of -/ @[simp] theorem mem_set_of_eq {a : α} {p : α → Prop} : a ∈ {a | p a} = p a := rfl @[simp] theorem nmem_set_of_eq {a : α} {P : α → Prop} : a ∉ {a : α | P a} = ¬ P a := rfl @[simp] theorem set_of_mem_eq {s : set α} : {x | x ∈ s} = s := rfl theorem mem_def {a : α} {s : set α} : a ∈ s ↔ s a := iff.rfl instance decidable_mem (s : set α) [H : decidable_pred s] : ∀ a, decidable (a ∈ s) := H instance decidable_set_of (p : α → Prop) [H : decidable_pred p] : decidable_pred {a | p a} := H @[simp] theorem set_of_subset_set_of {p q : α → Prop} : {a | p a} ⊆ {a | q a} ↔ (∀a, p a → q a) := iff.rfl @[simp] lemma sep_set_of {α} {p q : α → Prop} : {a ∈ {a | p a } | q a} = {a | p a ∧ q a} := rfl @[simp] lemma set_of_mem {α} {s : set α} : {a | a ∈ s} = s := rfl /- subset -/ -- TODO(Jeremy): write a tactic to unfold specific instances of generic notation? theorem subset_def {s t : set α} : (s ⊆ t) = ∀ x, x ∈ s → x ∈ t := rfl @[refl] theorem subset.refl (a : set α) : a ⊆ a := assume x, id @[trans] theorem subset.trans {a b c : set α} (ab : a ⊆ b) (bc : b ⊆ c) : a ⊆ c := assume x h, bc (ab h) @[trans] theorem mem_of_eq_of_mem {α : Type u} {x y : α} {s : set α} (hx : x = y) (h : y ∈ s) : x ∈ s := hx.symm ▸ h theorem subset.antisymm {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := ext (λ x, iff.intro (λ ina, h₁ ina) (λ inb, h₂ inb)) theorem subset.antisymm_iff {a b : set α} : a = b ↔ a ⊆ b ∧ b ⊆ a := ⟨λ e, e ▸ ⟨subset.refl _, subset.refl _⟩, λ ⟨h₁, h₂⟩, subset.antisymm h₁ h₂⟩ -- an alterantive name theorem eq_of_subset_of_subset {a b : set α} (h₁ : a ⊆ b) (h₂ : b ⊆ a) : a = b := subset.antisymm h₁ h₂ theorem mem_of_subset_of_mem {s₁ s₂ : set α} {a : α} : s₁ ⊆ s₂ → a ∈ s₁ → a ∈ s₂ := assume h₁ h₂, h₁ h₂ theorem not_subset : (¬ s ⊆ t) ↔ ∃a, a ∈ s ∧ a ∉ t := by simp [subset_def, classical.not_forall] /- strict subset -/ /-- `s ⊂ t` means that `s` is a strict subset of `t`, that is, `s ⊆ t` but `s ≠ t`. -/ def strict_subset (s t : set α) := s ⊆ t ∧ s ≠ t instance : has_ssubset (set α) := ⟨strict_subset⟩ theorem ssubset_def : (s ⊂ t) = (s ⊆ t ∧ s ≠ t) := rfl lemma exists_of_ssubset {α : Type u} {s t : set α} (h : s ⊂ t) : (∃x∈t, x ∉ s) := classical.by_contradiction $ assume hn, have t ⊆ s, from assume a hat, classical.by_contradiction $ assume has, hn ⟨a, hat, has⟩, h.2 $ subset.antisymm h.1 this lemma ssubset_iff_subset_not_subset {s t : set α} : s ⊂ t ↔ s ⊆ t ∧ ¬ t ⊆ s := by split; simp [set.ssubset_def, ne.def, set.subset.antisymm_iff] {contextual := tt} theorem not_mem_empty (x : α) : ¬ (x ∈ (∅ : set α)) := assume h : x ∈ ∅, h @[simp] theorem not_not_mem [decidable (a ∈ s)] : ¬ (a ∉ s) ↔ a ∈ s := not_not /- empty set -/ theorem empty_def : (∅ : set α) = {x | false} := rfl @[simp] theorem mem_empty_eq (x : α) : x ∈ (∅ : set α) = false := rfl @[simp] theorem set_of_false : {a : α | false} = ∅ := rfl theorem eq_empty_iff_forall_not_mem {s : set α} : s = ∅ ↔ ∀ x, x ∉ s := by simp [ext_iff] theorem ne_empty_of_mem {s : set α} {x : α} (h : x ∈ s) : s ≠ ∅ := by { intro hs, rw hs at h, apply not_mem_empty _ h } @[simp] theorem empty_subset (s : set α) : ∅ ⊆ s := assume x, assume h, false.elim h theorem subset_empty_iff {s : set α} : s ⊆ ∅ ↔ s = ∅ := by simp [subset.antisymm_iff] theorem eq_empty_of_subset_empty {s : set α} : s ⊆ ∅ → s = ∅ := subset_empty_iff.1 theorem ne_empty_iff_exists_mem {s : set α} : s ≠ ∅ ↔ ∃ x, x ∈ s := by haveI := classical.prop_decidable; simp [eq_empty_iff_forall_not_mem] theorem exists_mem_of_ne_empty {s : set α} : s ≠ ∅ → ∃ x, x ∈ s := ne_empty_iff_exists_mem.1 theorem coe_nonempty_iff_ne_empty {s : set α} : nonempty s ↔ s ≠ ∅ := nonempty_subtype.trans ne_empty_iff_exists_mem.symm -- TODO: remove when simplifier stops rewriting `a ≠ b` to `¬ a = b` theorem not_eq_empty_iff_exists {s : set α} : ¬ (s = ∅) ↔ ∃ x, x ∈ s := ne_empty_iff_exists_mem theorem subset_eq_empty {s t : set α} (h : t ⊆ s) (e : s = ∅) : t = ∅ := subset_empty_iff.1 $ e ▸ h theorem subset_ne_empty {s t : set α} (h : t ⊆ s) : t ≠ ∅ → s ≠ ∅ := mt (subset_eq_empty h) theorem ball_empty_iff {p : α → Prop} : (∀ x ∈ (∅ : set α), p x) ↔ true := by simp [iff_def] /- universal set -/ theorem univ_def : @univ α = {x | true} := rfl @[simp] theorem mem_univ (x : α) : x ∈ @univ α := trivial theorem empty_ne_univ [h : inhabited α] : (∅ : set α) ≠ univ := by simp [ext_iff] @[simp] theorem subset_univ (s : set α) : s ⊆ univ := λ x H, trivial theorem univ_subset_iff {s : set α} : univ ⊆ s ↔ s = univ := by simp [subset.antisymm_iff] theorem eq_univ_of_univ_subset {s : set α} : univ ⊆ s → s = univ := univ_subset_iff.1 theorem eq_univ_iff_forall {s : set α} : s = univ ↔ ∀ x, x ∈ s := by simp [ext_iff] theorem eq_univ_of_forall {s : set α} : (∀ x, x ∈ s) → s = univ := eq_univ_iff_forall.2 @[simp] lemma univ_eq_empty_iff {α : Type*} : (univ : set α) = ∅ ↔ ¬ nonempty α := eq_empty_iff_forall_not_mem.trans ⟨λ H ⟨x⟩, H x trivial, λ H x _, H ⟨x⟩⟩ lemma nonempty_iff_univ_ne_empty {α : Type*} : nonempty α ↔ (univ : set α) ≠ ∅ := by classical; exact iff_not_comm.1 univ_eq_empty_iff lemma exists_mem_of_nonempty (α) : ∀ [nonempty α], ∃x:α, x ∈ (univ : set α) | ⟨x⟩ := ⟨x, trivial⟩ @[simp] lemma univ_ne_empty {α} [h : nonempty α] : (univ : set α) ≠ ∅ := λ e, univ_eq_empty_iff.1 e h instance univ_decidable : decidable_pred (@set.univ α) := λ x, is_true trivial /- union -/ theorem union_def {s₁ s₂ : set α} : s₁ ∪ s₂ = {a | a ∈ s₁ ∨ a ∈ s₂} := rfl theorem mem_union_left {x : α} {a : set α} (b : set α) : x ∈ a → x ∈ a ∪ b := or.inl theorem mem_union_right {x : α} {b : set α} (a : set α) : x ∈ b → x ∈ a ∪ b := or.inr theorem mem_or_mem_of_mem_union {x : α} {a b : set α} (H : x ∈ a ∪ b) : x ∈ a ∨ x ∈ b := H theorem mem_union.elim {x : α} {a b : set α} {P : Prop} (H₁ : x ∈ a ∪ b) (H₂ : x ∈ a → P) (H₃ : x ∈ b → P) : P := or.elim H₁ H₂ H₃ theorem mem_union (x : α) (a b : set α) : x ∈ a ∪ b ↔ x ∈ a ∨ x ∈ b := iff.rfl @[simp] theorem mem_union_eq (x : α) (a b : set α) : x ∈ a ∪ b = (x ∈ a ∨ x ∈ b) := rfl @[simp] theorem union_self (a : set α) : a ∪ a = a := ext (assume x, or_self _) @[simp] theorem union_empty (a : set α) : a ∪ ∅ = a := ext (assume x, or_false _) @[simp] theorem empty_union (a : set α) : ∅ ∪ a = a := ext (assume x, false_or _) theorem union_comm (a b : set α) : a ∪ b = b ∪ a := ext (assume x, or.comm) theorem union_assoc (a b c : set α) : (a ∪ b) ∪ c = a ∪ (b ∪ c) := ext (assume x, or.assoc) instance union_is_assoc : is_associative (set α) (∪) := ⟨union_assoc⟩ instance union_is_comm : is_commutative (set α) (∪) := ⟨union_comm⟩ theorem union_left_comm (s₁ s₂ s₃ : set α) : s₁ ∪ (s₂ ∪ s₃) = s₂ ∪ (s₁ ∪ s₃) := by finish theorem union_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∪ s₂) ∪ s₃ = (s₁ ∪ s₃) ∪ s₂ := by finish theorem union_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∪ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∪ t = s := by finish [subset_def, ext_iff, iff_def] @[simp] theorem subset_union_left (s t : set α) : s ⊆ s ∪ t := λ x, or.inl @[simp] theorem subset_union_right (s t : set α) : t ⊆ s ∪ t := λ x, or.inr theorem union_subset {s t r : set α} (sr : s ⊆ r) (tr : t ⊆ r) : s ∪ t ⊆ r := by finish [subset_def, union_def] @[simp] theorem union_subset_iff {s t u : set α} : s ∪ t ⊆ u ↔ s ⊆ u ∧ t ⊆ u := by finish [iff_def, subset_def] theorem union_subset_union {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ s₂) (h₂ : t₁ ⊆ t₂) : s₁ ∪ t₁ ⊆ s₂ ∪ t₂ := by finish [subset_def] theorem union_subset_union_left {s₁ s₂ : set α} (t) (h : s₁ ⊆ s₂) : s₁ ∪ t ⊆ s₂ ∪ t := union_subset_union h (by refl) theorem union_subset_union_right (s) {t₁ t₂ : set α} (h : t₁ ⊆ t₂) : s ∪ t₁ ⊆ s ∪ t₂ := union_subset_union (by refl) h @[simp] theorem union_empty_iff {s t : set α} : s ∪ t = ∅ ↔ s = ∅ ∧ t = ∅ := ⟨by finish [ext_iff], by finish [ext_iff]⟩ /- intersection -/ theorem inter_def {s₁ s₂ : set α} : s₁ ∩ s₂ = {a | a ∈ s₁ ∧ a ∈ s₂} := rfl theorem mem_inter_iff (x : α) (a b : set α) : x ∈ a ∩ b ↔ x ∈ a ∧ x ∈ b := iff.rfl @[simp] theorem mem_inter_eq (x : α) (a b : set α) : x ∈ a ∩ b = (x ∈ a ∧ x ∈ b) := rfl theorem mem_inter {x : α} {a b : set α} (ha : x ∈ a) (hb : x ∈ b) : x ∈ a ∩ b := ⟨ha, hb⟩ theorem mem_of_mem_inter_left {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ a := h.left theorem mem_of_mem_inter_right {x : α} {a b : set α} (h : x ∈ a ∩ b) : x ∈ b := h.right @[simp] theorem inter_self (a : set α) : a ∩ a = a := ext (assume x, and_self _) @[simp] theorem inter_empty (a : set α) : a ∩ ∅ = ∅ := ext (assume x, and_false _) @[simp] theorem empty_inter (a : set α) : ∅ ∩ a = ∅ := ext (assume x, false_and _) theorem inter_comm (a b : set α) : a ∩ b = b ∩ a := ext (assume x, and.comm) theorem inter_assoc (a b c : set α) : (a ∩ b) ∩ c = a ∩ (b ∩ c) := ext (assume x, and.assoc) instance inter_is_assoc : is_associative (set α) (∩) := ⟨inter_assoc⟩ instance inter_is_comm : is_commutative (set α) (∩) := ⟨inter_comm⟩ theorem inter_left_comm (s₁ s₂ s₃ : set α) : s₁ ∩ (s₂ ∩ s₃) = s₂ ∩ (s₁ ∩ s₃) := by finish theorem inter_right_comm (s₁ s₂ s₃ : set α) : (s₁ ∩ s₂) ∩ s₃ = (s₁ ∩ s₃) ∩ s₂ := by finish @[simp] theorem inter_subset_left (s t : set α) : s ∩ t ⊆ s := λ x H, and.left H @[simp] theorem inter_subset_right (s t : set α) : s ∩ t ⊆ t := λ x H, and.right H theorem subset_inter {s t r : set α} (rs : r ⊆ s) (rt : r ⊆ t) : r ⊆ s ∩ t := by finish [subset_def, inter_def] @[simp] theorem subset_inter_iff {s t r : set α} : r ⊆ s ∩ t ↔ r ⊆ s ∧ r ⊆ t := ⟨λ h, ⟨subset.trans h (inter_subset_left _ _), subset.trans h (inter_subset_right _ _)⟩, λ ⟨h₁, h₂⟩, subset_inter h₁ h₂⟩ @[simp] theorem inter_univ (a : set α) : a ∩ univ = a := ext (assume x, and_true _) @[simp] theorem univ_inter (a : set α) : univ ∩ a = a := ext (assume x, true_and _) theorem inter_subset_inter_left {s t : set α} (u : set α) (H : s ⊆ t) : s ∩ u ⊆ t ∩ u := by finish [subset_def] theorem inter_subset_inter_right {s t : set α} (u : set α) (H : s ⊆ t) : u ∩ s ⊆ u ∩ t := by finish [subset_def] theorem inter_subset_inter {s₁ s₂ t₁ t₂ : set α} (h₁ : s₁ ⊆ t₁) (h₂ : s₂ ⊆ t₂) : s₁ ∩ s₂ ⊆ t₁ ∩ t₂ := by finish [subset_def] theorem inter_eq_self_of_subset_left {s t : set α} (h : s ⊆ t) : s ∩ t = s := by finish [subset_def, ext_iff, iff_def] theorem inter_eq_self_of_subset_right {s t : set α} (h : t ⊆ s) : s ∩ t = t := by finish [subset_def, ext_iff, iff_def] theorem union_inter_cancel_left {s t : set α} : (s ∪ t) ∩ s = s := by finish [ext_iff, iff_def] theorem union_inter_cancel_right {s t : set α} : (s ∪ t) ∩ t = t := by finish [ext_iff, iff_def] -- TODO(Mario): remove? theorem nonempty_of_inter_nonempty_right {s t : set α} (h : s ∩ t ≠ ∅) : t ≠ ∅ := by finish [ext_iff, iff_def] theorem nonempty_of_inter_nonempty_left {s t : set α} (h : s ∩ t ≠ ∅) : s ≠ ∅ := by finish [ext_iff, iff_def] /- distributivity laws -/ theorem inter_distrib_left (s t u : set α) : s ∩ (t ∪ u) = (s ∩ t) ∪ (s ∩ u) := ext (assume x, and_or_distrib_left) theorem inter_distrib_right (s t u : set α) : (s ∪ t) ∩ u = (s ∩ u) ∪ (t ∩ u) := ext (assume x, or_and_distrib_right) theorem union_distrib_left (s t u : set α) : s ∪ (t ∩ u) = (s ∪ t) ∩ (s ∪ u) := ext (assume x, or_and_distrib_left) theorem union_distrib_right (s t u : set α) : (s ∩ t) ∪ u = (s ∪ u) ∩ (t ∪ u) := ext (assume x, and_or_distrib_right) /- insert -/ theorem insert_def (x : α) (s : set α) : insert x s = { y | y = x ∨ y ∈ s } := rfl @[simp] theorem insert_of_has_insert (x : α) (s : set α) : has_insert.insert x s = insert x s := rfl @[simp] theorem subset_insert (x : α) (s : set α) : s ⊆ insert x s := assume y ys, or.inr ys theorem mem_insert (x : α) (s : set α) : x ∈ insert x s := or.inl rfl theorem mem_insert_of_mem {x : α} {s : set α} (y : α) : x ∈ s → x ∈ insert y s := or.inr theorem eq_or_mem_of_mem_insert {x a : α} {s : set α} : x ∈ insert a s → x = a ∨ x ∈ s := id theorem mem_of_mem_insert_of_ne {x a : α} {s : set α} (xin : x ∈ insert a s) : x ≠ a → x ∈ s := by finish [insert_def] @[simp] theorem mem_insert_iff {x a : α} {s : set α} : x ∈ insert a s ↔ (x = a ∨ x ∈ s) := iff.rfl @[simp] theorem insert_eq_of_mem {a : α} {s : set α} (h : a ∈ s) : insert a s = s := by finish [ext_iff, iff_def] theorem insert_subset : insert a s ⊆ t ↔ (a ∈ t ∧ s ⊆ t) := by simp [subset_def, or_imp_distrib, forall_and_distrib] theorem insert_subset_insert (h : s ⊆ t) : insert a s ⊆ insert a t := assume a', or.imp_right (@h a') theorem ssubset_insert {s : set α} {a : α} (h : a ∉ s) : s ⊂ insert a s := by finish [ssubset_def, ext_iff] theorem insert_comm (a b : α) (s : set α) : insert a (insert b s) = insert b (insert a s) := ext $ by simp [or.left_comm] theorem insert_union : insert a s ∪ t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] @[simp] theorem union_insert : s ∪ insert a t = insert a (s ∪ t) := ext $ assume a, by simp [or.comm, or.left_comm] -- TODO(Jeremy): make this automatic theorem insert_ne_empty (a : α) (s : set α) : insert a s ≠ ∅ := by safe [ext_iff, iff_def]; have h' := a_1 a; finish -- useful in proofs by induction theorem forall_of_forall_insert {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ insert a s → P x) : ∀ x, x ∈ s → P x := by finish theorem forall_insert_of_forall {P : α → Prop} {a : α} {s : set α} (h : ∀ x, x ∈ s → P x) (ha : P a) : ∀ x, x ∈ insert a s → P x := by finish theorem ball_insert_iff {P : α → Prop} {a : α} {s : set α} : (∀ x ∈ insert a s, P x) ↔ P a ∧ (∀x ∈ s, P x) := by finish [iff_def] /- singletons -/ theorem singleton_def (a : α) : ({a} : set α) = insert a ∅ := rfl @[simp] theorem mem_singleton_iff {a b : α} : a ∈ ({b} : set α) ↔ a = b := by finish [singleton_def] lemma set_of_eq_eq_singleton {a : α} : {n | n = a} = {a} := set.ext $ λ n, (set.mem_singleton_iff).symm -- TODO: again, annotation needed @[simp] theorem mem_singleton (a : α) : a ∈ ({a} : set α) := by finish theorem eq_of_mem_singleton {x y : α} (h : x ∈ ({y} : set α)) : x = y := by finish @[simp] theorem singleton_eq_singleton_iff {x y : α} : {x} = ({y} : set α) ↔ x = y := by finish [ext_iff, iff_def] theorem mem_singleton_of_eq {x y : α} (H : x = y) : x ∈ ({y} : set α) := by finish theorem insert_eq (x : α) (s : set α) : insert x s = ({x} : set α) ∪ s := by finish [ext_iff, or_comm] @[simp] theorem pair_eq_singleton (a : α) : ({a, a} : set α) = {a} := by finish @[simp] theorem singleton_ne_empty (a : α) : ({a} : set α) ≠ ∅ := insert_ne_empty _ _ @[simp] theorem singleton_subset_iff {a : α} {s : set α} : {a} ⊆ s ↔ a ∈ s := ⟨λh, h (by simp), λh b e, by simp at e; simp [*]⟩ theorem set_compr_eq_eq_singleton {a : α} : {b | b = a} = {a} := ext $ by simp @[simp] theorem union_singleton : s ∪ {a} = insert a s := by simp [singleton_def] @[simp] theorem singleton_union : {a} ∪ s = insert a s := by rw [union_comm, union_singleton] theorem singleton_inter_eq_empty : {a} ∩ s = ∅ ↔ a ∉ s := by simp [eq_empty_iff_forall_not_mem] theorem inter_singleton_eq_empty : s ∩ {a} = ∅ ↔ a ∉ s := by rw [inter_comm, singleton_inter_eq_empty] /- separation -/ theorem mem_sep {s : set α} {p : α → Prop} {x : α} (xs : x ∈ s) (px : p x) : x ∈ {x ∈ s | p x} := ⟨xs, px⟩ @[simp] theorem mem_sep_eq {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} = (x ∈ s ∧ p x) := rfl theorem mem_sep_iff {s : set α} {p : α → Prop} {x : α} : x ∈ {x ∈ s | p x} ↔ x ∈ s ∧ p x := iff.rfl theorem eq_sep_of_subset {s t : set α} (ssubt : s ⊆ t) : s = {x ∈ t | x ∈ s} := by finish [ext_iff, iff_def, subset_def] theorem sep_subset (s : set α) (p : α → Prop) : {x ∈ s | p x} ⊆ s := assume x, and.left theorem forall_not_of_sep_empty {s : set α} {p : α → Prop} (h : {x ∈ s | p x} = ∅) : ∀ x ∈ s, ¬ p x := by finish [ext_iff] @[simp] lemma sep_univ {α} {p : α → Prop} : {a ∈ (univ : set α) | p a} = {a | p a} := set.ext $ by simp /- complement -/ theorem mem_compl {s : set α} {x : α} (h : x ∉ s) : x ∈ -s := h lemma compl_set_of {α} (p : α → Prop) : - {a | p a} = { a | ¬ p a } := rfl theorem not_mem_of_mem_compl {s : set α} {x : α} (h : x ∈ -s) : x ∉ s := h @[simp] theorem mem_compl_eq (s : set α) (x : α) : x ∈ -s = (x ∉ s) := rfl theorem mem_compl_iff (s : set α) (x : α) : x ∈ -s ↔ x ∉ s := iff.rfl @[simp] theorem inter_compl_self (s : set α) : s ∩ -s = ∅ := by finish [ext_iff] @[simp] theorem compl_inter_self (s : set α) : -s ∩ s = ∅ := by finish [ext_iff] @[simp] theorem compl_empty : -(∅ : set α) = univ := by finish [ext_iff] @[simp] theorem compl_union (s t : set α) : -(s ∪ t) = -s ∩ -t := by finish [ext_iff] @[simp] theorem compl_compl (s : set α) : -(-s) = s := by finish [ext_iff] -- ditto theorem compl_inter (s t : set α) : -(s ∩ t) = -s ∪ -t := by finish [ext_iff] @[simp] theorem compl_univ : -(univ : set α) = ∅ := by finish [ext_iff] theorem union_eq_compl_compl_inter_compl (s t : set α) : s ∪ t = -(-s ∩ -t) := by simp [compl_inter, compl_compl] theorem inter_eq_compl_compl_union_compl (s t : set α) : s ∩ t = -(-s ∪ -t) := by simp [compl_compl] @[simp] theorem union_compl_self (s : set α) : s ∪ -s = univ := by finish [ext_iff] @[simp] theorem compl_union_self (s : set α) : -s ∪ s = univ := by finish [ext_iff] theorem compl_comp_compl : compl ∘ compl = @id (set α) := funext compl_compl theorem compl_subset_comm {s t : set α} : -s ⊆ t ↔ -t ⊆ s := by haveI := classical.prop_decidable; exact forall_congr (λ a, not_imp_comm) lemma compl_subset_compl {s t : set α} : -s ⊆ -t ↔ t ⊆ s := by rw [compl_subset_comm, compl_compl] theorem compl_subset_iff_union {s t : set α} : -s ⊆ t ↔ s ∪ t = univ := iff.symm $ eq_univ_iff_forall.trans $ forall_congr $ λ a, by haveI := classical.prop_decidable; exact or_iff_not_imp_left theorem subset_compl_comm {s t : set α} : s ⊆ -t ↔ t ⊆ -s := forall_congr $ λ a, imp_not_comm theorem subset_compl_iff_disjoint {s t : set α} : s ⊆ -t ↔ s ∩ t = ∅ := iff.trans (forall_congr $ λ a, and_imp.symm) subset_empty_iff theorem inter_subset (a b c : set α) : a ∩ b ⊆ c ↔ a ⊆ -b ∪ c := begin haveI := classical.prop_decidable, split, { intros h x xa, by_cases h' : x ∈ b, simp [h ⟨xa, h'⟩], simp [h'] }, intros h x, rintro ⟨xa, xb⟩, cases h xa, contradiction, assumption end /- set difference -/ theorem diff_eq (s t : set α) : s \ t = s ∩ -t := rfl @[simp] theorem mem_diff {s t : set α} (x : α) : x ∈ s \ t ↔ x ∈ s ∧ x ∉ t := iff.rfl theorem mem_diff_of_mem {s t : set α} {x : α} (h1 : x ∈ s) (h2 : x ∉ t) : x ∈ s \ t := ⟨h1, h2⟩ theorem mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∈ s := h.left theorem not_mem_of_mem_diff {s t : set α} {x : α} (h : x ∈ s \ t) : x ∉ t := h.right theorem union_diff_cancel {s t : set α} (h : s ⊆ t) : s ∪ (t \ s) = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_left {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ s = t := by finish [ext_iff, iff_def, subset_def] theorem union_diff_cancel_right {s t : set α} (h : s ∩ t ⊆ ∅) : (s ∪ t) \ t = s := by finish [ext_iff, iff_def, subset_def] theorem union_diff_left {s t : set α} : (s ∪ t) \ s = t \ s := by finish [ext_iff, iff_def] theorem union_diff_right {s t : set α} : (s ∪ t) \ t = s \ t := by finish [ext_iff, iff_def] theorem union_diff_distrib {s t u : set α} : (s ∪ t) \ u = s \ u ∪ t \ u := inter_distrib_right _ _ _ theorem inter_diff_assoc (a b c : set α) : (a ∩ b) \ c = a ∩ (b \ c) := inter_assoc _ _ _ theorem inter_diff_self (a b : set α) : a ∩ (b \ a) = ∅ := by finish [ext_iff] theorem inter_union_diff (s t : set α) : (s ∩ t) ∪ (s \ t) = s := by finish [ext_iff, iff_def] theorem diff_subset (s t : set α) : s \ t ⊆ s := by finish [subset_def] theorem diff_subset_diff {s₁ s₂ t₁ t₂ : set α} : s₁ ⊆ s₂ → t₂ ⊆ t₁ → s₁ \ t₁ ⊆ s₂ \ t₂ := by finish [subset_def] theorem diff_subset_diff_left {s₁ s₂ t : set α} (h : s₁ ⊆ s₂) : s₁ \ t ⊆ s₂ \ t := diff_subset_diff h (by refl) theorem diff_subset_diff_right {s t u : set α} (h : t ⊆ u) : s \ u ⊆ s \ t := diff_subset_diff (subset.refl s) h theorem compl_eq_univ_diff (s : set α) : -s = univ \ s := by finish [ext_iff] @[simp] lemma empty_diff {α : Type*} (s : set α) : (∅ \ s : set α) = ∅ := eq_empty_of_subset_empty $ assume x ⟨hx, _⟩, hx theorem diff_eq_empty {s t : set α} : s \ t = ∅ ↔ s ⊆ t := ⟨assume h x hx, classical.by_contradiction $ assume : x ∉ t, show x ∈ (∅ : set α), from h ▸ ⟨hx, this⟩, assume h, eq_empty_of_subset_empty $ assume x ⟨hx, hnx⟩, hnx $ h hx⟩ @[simp] theorem diff_empty {s : set α} : s \ ∅ = s := ext $ assume x, ⟨assume ⟨hx, _⟩, hx, assume h, ⟨h, not_false⟩⟩ theorem diff_diff {u : set α} : s \ t \ u = s \ (t ∪ u) := ext $ by simp [not_or_distrib, and.comm, and.left_comm] lemma diff_subset_iff {s t u : set α} : s \ t ⊆ u ↔ s ⊆ t ∪ u := ⟨assume h x xs, classical.by_cases or.inl (assume nxt, or.inr (h ⟨xs, nxt⟩)), assume h x ⟨xs, nxt⟩, or.resolve_left (h xs) nxt⟩ lemma diff_subset_comm {s t u : set α} : s \ t ⊆ u ↔ s \ u ⊆ t := by rw [diff_subset_iff, diff_subset_iff, union_comm] @[simp] theorem insert_diff (h : a ∈ t) : insert a s \ t = s \ t := ext $ by intro; constructor; simp [or_imp_distrib, h] {contextual := tt} theorem union_diff_self {s t : set α} : s ∪ (t \ s) = s ∪ t := by finish [ext_iff, iff_def] theorem diff_union_self {s t : set α} : (s \ t) ∪ t = s ∪ t := by rw [union_comm, union_diff_self, union_comm] theorem diff_inter_self {a b : set α} : (b \ a) ∩ a = ∅ := ext $ by simp [iff_def] {contextual:=tt} theorem diff_eq_self {s t : set α} : s \ t = s ↔ t ∩ s ⊆ ∅ := by finish [ext_iff, iff_def, subset_def] @[simp] theorem diff_singleton_eq_self {a : α} {s : set α} (h : a ∉ s) : s \ {a} = s := diff_eq_self.2 $ by simp [singleton_inter_eq_empty.2 h] @[simp] theorem insert_diff_singleton {a : α} {s : set α} : insert a (s \ {a}) = insert a s := by simp [insert_eq, union_diff_self, -union_singleton, -singleton_union] @[simp] lemma diff_self {s : set α} : s \ s = ∅ := ext $ by simp /- powerset -/ theorem mem_powerset {x s : set α} (h : x ⊆ s) : x ∈ powerset s := h theorem subset_of_mem_powerset {x s : set α} (h : x ∈ powerset s) : x ⊆ s := h theorem mem_powerset_iff (x s : set α) : x ∈ powerset s ↔ x ⊆ s := iff.rfl /- inverse image -/ /-- The preimage of `s : set β` by `f : α → β`, written `f ⁻¹' s`, is the set of `x : α` such that `f x ∈ s`. -/ def preimage {α : Type u} {β : Type v} (f : α → β) (s : set β) : set α := {x | f x ∈ s} infix ` ⁻¹' `:80 := preimage section preimage variables {f : α → β} {g : β → γ} @[simp] theorem preimage_empty : f ⁻¹' ∅ = ∅ := rfl @[simp] theorem mem_preimage_eq {s : set β} {a : α} : (a ∈ f ⁻¹' s) = (f a ∈ s) := rfl theorem preimage_mono {s t : set β} (h : s ⊆ t) : f ⁻¹' s ⊆ f ⁻¹' t := assume x hx, h hx @[simp] theorem preimage_univ : f ⁻¹' univ = univ := rfl @[simp] theorem preimage_inter {s t : set β} : f ⁻¹' (s ∩ t) = f ⁻¹' s ∩ f ⁻¹' t := rfl @[simp] theorem preimage_union {s t : set β} : f ⁻¹' (s ∪ t) = f ⁻¹' s ∪ f ⁻¹' t := rfl @[simp] theorem preimage_compl {s : set β} : f ⁻¹' (- s) = - (f ⁻¹' s) := rfl @[simp] theorem preimage_diff (f : α → β) (s t : set β) : f ⁻¹' (s \ t) = f ⁻¹' s \ f ⁻¹' t := rfl @[simp] theorem preimage_set_of_eq {p : α → Prop} {f : β → α} : f ⁻¹' {a | p a} = {a | p (f a)} := rfl theorem preimage_id {s : set α} : id ⁻¹' s = s := rfl theorem preimage_comp {s : set γ} : (g ∘ f) ⁻¹' s = f ⁻¹' (g ⁻¹' s) := rfl theorem eq_preimage_subtype_val_iff {p : α → Prop} {s : set (subtype p)} {t : set α} : s = subtype.val ⁻¹' t ↔ (∀x (h : p x), (⟨x, h⟩ : subtype p) ∈ s ↔ x ∈ t) := ⟨assume s_eq x h, by rw [s_eq]; simp, assume h, ext $ assume ⟨x, hx⟩, by simp [h]⟩ end preimage /- function image -/ section image infix ` '' `:80 := image /-- Two functions `f₁ f₂ : α → β` are equal on `s` if `f₁ x = f₂ x` for all `x ∈ a`. -/ @[reducible] def eq_on (f1 f2 : α → β) (a : set α) : Prop := ∀ x ∈ a, f1 x = f2 x -- TODO(Jeremy): use bounded exists in image theorem mem_image_iff_bex {f : α → β} {s : set α} {y : β} : y ∈ f '' s ↔ ∃ x (_ : x ∈ s), f x = y := bex_def.symm theorem mem_image_eq (f : α → β) (s : set α) (y: β) : y ∈ f '' s = ∃ x, x ∈ s ∧ f x = y := rfl @[simp] theorem mem_image (f : α → β) (s : set α) (y : β) : y ∈ f '' s ↔ ∃ x, x ∈ s ∧ f x = y := iff.rfl theorem mem_image_of_mem (f : α → β) {x : α} {a : set α} (h : x ∈ a) : f x ∈ f '' a := ⟨_, h, rfl⟩ theorem mem_image_of_injective {f : α → β} {a : α} {s : set α} (hf : injective f) : f a ∈ f '' s ↔ a ∈ s := iff.intro (assume ⟨b, hb, eq⟩, (hf eq) ▸ hb) (assume h, mem_image_of_mem _ h) theorem ball_image_of_ball {f : α → β} {s : set α} {p : β → Prop} (h : ∀ x ∈ s, p (f x)) : ∀ y ∈ f '' s, p y := by finish [mem_image_eq] @[simp] theorem ball_image_iff {f : α → β} {s : set α} {p : β → Prop} : (∀ y ∈ f '' s, p y) ↔ (∀ x ∈ s, p (f x)) := iff.intro (assume h a ha, h _ $ mem_image_of_mem _ ha) (assume h b ⟨a, ha, eq⟩, eq ▸ h a ha) theorem mono_image {f : α → β} {s t : set α} (h : s ⊆ t) : f '' s ⊆ f '' t := assume x ⟨y, hy, y_eq⟩, y_eq ▸ mem_image_of_mem _ $ h hy theorem mem_image_elim {f : α → β} {s : set α} {C : β → Prop} (h : ∀ (x : α), x ∈ s → C (f x)) : ∀{y : β}, y ∈ f '' s → C y | ._ ⟨a, a_in, rfl⟩ := h a a_in theorem mem_image_elim_on {f : α → β} {s : set α} {C : β → Prop} {y : β} (h_y : y ∈ f '' s) (h : ∀ (x : α), x ∈ s → C (f x)) : C y := mem_image_elim h h_y @[congr] lemma image_congr {f g : α → β} {s : set α} (h : ∀a∈s, f a = g a) : f '' s = g '' s := by safe [ext_iff, iff_def] theorem image_eq_image_of_eq_on {f₁ f₂ : α → β} {s : set α} (heq : eq_on f₁ f₂ s) : f₁ '' s = f₂ '' s := image_congr heq theorem image_comp (f : β → γ) (g : α → β) (a : set α) : (f ∘ g) '' a = f '' (g '' a) := subset.antisymm (ball_image_of_ball $ assume a ha, mem_image_of_mem _ $ mem_image_of_mem _ ha) (ball_image_of_ball $ ball_image_of_ball $ assume a ha, mem_image_of_mem _ ha) /- Proof is removed as it uses generated names TODO(Jeremy): make automatic, begin safe [ext_iff, iff_def, mem_image, (∘)], have h' := h_2 (g a_2), finish end -/ theorem image_subset {a b : set α} (f : α → β) (h : a ⊆ b) : f '' a ⊆ f '' b := by finish [subset_def, mem_image_eq] theorem image_union (f : α → β) (s t : set α) : f '' (s ∪ t) = f '' s ∪ f '' t := by finish [ext_iff, iff_def, mem_image_eq] @[simp] theorem image_empty (f : α → β) : f '' ∅ = ∅ := ext $ by simp theorem image_inter_on {f : α → β} {s t : set α} (h : ∀x∈t, ∀y∈s, f x = f y → x = y) : f '' s ∩ f '' t = f '' (s ∩ t) := subset.antisymm (assume b ⟨⟨a₁, ha₁, h₁⟩, ⟨a₂, ha₂, h₂⟩⟩, have a₂ = a₁, from h _ ha₂ _ ha₁ (by simp *), ⟨a₁, ⟨ha₁, this ▸ ha₂⟩, h₁⟩) (subset_inter (mono_image $ inter_subset_left _ _) (mono_image $ inter_subset_right _ _)) theorem image_inter {f : α → β} {s t : set α} (H : injective f) : f '' s ∩ f '' t = f '' (s ∩ t) := image_inter_on (assume x _ y _ h, H h) theorem image_univ_of_surjective {ι : Type*} {f : ι → β} (H : surjective f) : f '' univ = univ := eq_univ_of_forall $ by simp [image]; exact H @[simp] theorem image_singleton {f : α → β} {a : α} : f '' {a} = {f a} := ext $ λ x, by simp [image]; rw eq_comm @[simp] lemma image_eq_empty {α β} {f : α → β} {s : set α} : f '' s = ∅ ↔ s = ∅ := by simp only [eq_empty_iff_forall_not_mem]; exact ⟨λ H a ha, H _ ⟨_, ha, rfl⟩, λ H b ⟨_, ha, _⟩, H _ ha⟩ lemma inter_singleton_ne_empty {α : Type*} {s : set α} {a : α} : s ∩ {a} ≠ ∅ ↔ a ∈ s := by finish [set.inter_singleton_eq_empty] theorem fix_set_compl (t : set α) : compl t = - t := rfl -- TODO(Jeremy): there is an issue with - t unfolding to compl t theorem mem_compl_image (t : set α) (S : set (set α)) : t ∈ compl '' S ↔ -t ∈ S := begin suffices : ∀ x, -x = t ↔ -t = x, {simp [fix_set_compl, this]}, intro x, split; { intro e, subst e, simp } end @[simp] theorem image_id (s : set α) : id '' s = s := ext $ by simp theorem compl_compl_image (S : set (set α)) : compl '' (compl '' S) = S := by rw [← image_comp, compl_comp_compl, image_id] theorem image_insert_eq {f : α → β} {a : α} {s : set α} : f '' (insert a s) = insert (f a) (f '' s) := ext $ by simp [and_or_distrib_left, exists_or_distrib, eq_comm, or_comm, and_comm] theorem image_subset_preimage_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set α) : f '' s ⊆ g ⁻¹' s := λ b ⟨a, h, e⟩, e ▸ ((I a).symm ▸ h : g (f a) ∈ s) theorem preimage_subset_image_of_inverse {f : α → β} {g : β → α} (I : left_inverse g f) (s : set β) : f ⁻¹' s ⊆ g '' s := λ b h, ⟨f b, h, I b⟩ theorem image_eq_preimage_of_inverse {f : α → β} {g : β → α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : image f = preimage g := funext $ λ s, subset.antisymm (image_subset_preimage_of_inverse h₁ s) (preimage_subset_image_of_inverse h₂ s) theorem mem_image_iff_of_inverse {f : α → β} {g : β → α} {b : β} {s : set α} (h₁ : left_inverse g f) (h₂ : right_inverse g f) : b ∈ f '' s ↔ g b ∈ s := by rw image_eq_preimage_of_inverse h₁ h₂; refl theorem image_compl_subset {f : α → β} {s : set α} (H : injective f) : f '' -s ⊆ -(f '' s) := subset_compl_iff_disjoint.2 $ by simp [image_inter H] theorem subset_image_compl {f : α → β} {s : set α} (H : surjective f) : -(f '' s) ⊆ f '' -s := compl_subset_iff_union.2 $ by rw ← image_union; simp [image_univ_of_surjective H] theorem image_compl_eq {f : α → β} {s : set α} (H : bijective f) : f '' -s = -(f '' s) := subset.antisymm (image_compl_subset H.1) (subset_image_compl H.2) /- image and preimage are a Galois connection -/ theorem image_subset_iff {s : set α} {t : set β} {f : α → β} : f '' s ⊆ t ↔ s ⊆ f ⁻¹' t := ball_image_iff theorem image_preimage_subset (f : α → β) (s : set β) : f '' (f ⁻¹' s) ⊆ s := image_subset_iff.2 (subset.refl _) theorem subset_preimage_image (f : α → β) (s : set α) : s ⊆ f ⁻¹' (f '' s) := λ x, mem_image_of_mem f theorem preimage_image_eq {f : α → β} (s : set α) (h : injective f) : f ⁻¹' (f '' s) = s := subset.antisymm (λ x ⟨y, hy, e⟩, h e ▸ hy) (subset_preimage_image f s) theorem image_preimage_eq {f : α → β} {s : set β} (h : surjective f) : f '' (f ⁻¹' s) = s := subset.antisymm (image_preimage_subset f s) (λ x hx, let ⟨y, e⟩ := h x in ⟨y, (e.symm ▸ hx : f y ∈ s), e⟩) lemma preimage_eq_preimage {f : β → α} (hf : surjective f) : f ⁻¹' s = preimage f t ↔ s = t := iff.intro (assume eq, by rw [← @image_preimage_eq β α f s hf, ← @image_preimage_eq β α f t hf, eq]) (assume eq, eq ▸ rfl) lemma surjective_preimage {f : β → α} (hf : surjective f) : injective (preimage f) := assume s t, (preimage_eq_preimage hf).1 theorem compl_image : image (@compl α) = preimage compl := image_eq_preimage_of_inverse compl_compl compl_compl theorem compl_image_set_of {α : Type u} {p : set α → Prop} : compl '' {x | p x} = {x | p (- x)} := congr_fun compl_image p theorem inter_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∩ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∩ t) := λ x h, ⟨mem_image_of_mem _ h.left, h.right⟩ theorem union_preimage_subset (s : set α) (t : set β) (f : α → β) : s ∪ f ⁻¹' t ⊆ f ⁻¹' (f '' s ∪ t) := λ x h, or.elim h (λ l, or.inl $ mem_image_of_mem _ l) (λ r, or.inr r) theorem subset_image_union (f : α → β) (s : set α) (t : set β) : f '' (s ∪ f ⁻¹' t) ⊆ f '' s ∪ t := image_subset_iff.2 (union_preimage_subset _ _ _) lemma preimage_subset_iff {A : set α} {B : set β} {f : α → β} : f⁻¹' B ⊆ A ↔ (∀ a : α, f a ∈ B → a ∈ A) := iff.rfl lemma image_eq_image {f : α → β} (hf : injective f) : f '' s = f '' t ↔ s = t := iff.symm $ iff.intro (assume eq, eq ▸ rfl) $ assume eq, by rw [← preimage_image_eq s hf, ← preimage_image_eq t hf, eq] lemma image_subset_image_iff {f : α → β} (hf : injective f) : f '' s ⊆ f '' t ↔ s ⊆ t := begin refine (iff.symm $ iff.intro (image_subset f) $ assume h, _), rw [← preimage_image_eq s hf, ← preimage_image_eq t hf], exact preimage_mono h end lemma injective_image {f : α → β} (hf : injective f) : injective (('') f) := assume s t, (image_eq_image hf).1 lemma prod_quotient_preimage_eq_image [s : setoid α] (g : quotient s → β) {h : α → β} (Hh : h = g ∘ quotient.mk) (r : set (β × β)) : {x : quotient s × quotient s | (g x.1, g x.2) ∈ r} = (λ a : α × α, (⟦a.1⟧, ⟦a.2⟧)) '' ((λ a : α × α, (h a.1, h a.2)) ⁻¹' r) := Hh.symm ▸ set.ext (λ ⟨a₁, a₂⟩, ⟨quotient.induction_on₂ a₁ a₂ (λ a₁ a₂ h, ⟨(a₁, a₂), h, rfl⟩), λ ⟨⟨b₁, b₂⟩, h₁, h₂⟩, show (g a₁, g a₂) ∈ r, from have h₃ : ⟦b₁⟧ = a₁ ∧ ⟦b₂⟧ = a₂ := prod.ext_iff.1 h₂, h₃.1 ▸ h₃.2 ▸ h₁⟩) def image_factorization (f : α → β) (s : set α) : s → f '' s := λ p, ⟨f p.1, mem_image_of_mem f p.2⟩ lemma image_factorization_eq {f : α → β} {s : set α} : subtype.val ∘ image_factorization f s = f ∘ subtype.val := funext $ λ p, rfl lemma surjective_onto_image {f : α → β} {s : set α} : surjective (image_factorization f s) := λ ⟨_, ⟨a, ha, rfl⟩⟩, ⟨⟨a, ha⟩, rfl⟩ end image theorem univ_eq_true_false : univ = ({true, false} : set Prop) := eq.symm $ eq_univ_of_forall $ classical.cases (by simp) (by simp) section range variables {f : ι → α} open function /-- Range of a function. This function is more flexible than `f '' univ`, as the image requires that the domain is in Type and not an arbitrary Sort. -/ def range (f : ι → α) : set α := {x | ∃y, f y = x} @[simp] theorem mem_range {x : α} : x ∈ range f ↔ ∃ y, f y = x := iff.rfl theorem mem_range_self (i : ι) : f i ∈ range f := ⟨i, rfl⟩ theorem forall_range_iff {p : α → Prop} : (∀ a ∈ range f, p a) ↔ (∀ i, p (f i)) := ⟨assume h i, h (f i) (mem_range_self _), assume h a ⟨i, (hi : f i = a)⟩, hi ▸ h i⟩ theorem exists_range_iff {p : α → Prop} : (∃ a ∈ range f, p a) ↔ (∃ i, p (f i)) := ⟨assume ⟨a, ⟨i, eq⟩, h⟩, ⟨i, eq.symm ▸ h⟩, assume ⟨i, h⟩, ⟨f i, mem_range_self _, h⟩⟩ theorem range_iff_surjective : range f = univ ↔ surjective f := eq_univ_iff_forall @[simp] theorem range_id : range (@id α) = univ := range_iff_surjective.2 surjective_id @[simp] theorem image_univ {ι : Type*} {f : ι → β} : f '' univ = range f := ext $ by simp [image, range] theorem image_subset_range {ι : Type*} (f : ι → β) (s : set ι) : f '' s ⊆ range f := by rw ← image_univ; exact image_subset _ (subset_univ _) theorem range_comp {g : α → β} : range (g ∘ f) = g '' range f := subset.antisymm (forall_range_iff.mpr $ assume i, mem_image_of_mem g (mem_range_self _)) (ball_image_iff.mpr $ forall_range_iff.mpr mem_range_self) theorem range_subset_iff {ι : Type*} {f : ι → β} {s : set β} : range f ⊆ s ↔ ∀ y, f y ∈ s := forall_range_iff lemma nonempty_of_nonempty_range {α : Type*} {β : Type*} {f : α → β} (H : ¬range f = ∅) : nonempty α := begin cases exists_mem_of_ne_empty H with x h, cases mem_range.1 h with y _, exact ⟨y⟩ end @[simp] lemma range_eq_empty {α : Type u} {β : Type v} {f : α → β} : range f = ∅ ↔ ¬ nonempty α := by rw ← set.image_univ; simp [-set.image_univ] theorem image_preimage_eq_inter_range {f : α → β} {t : set β} : f '' (f ⁻¹' t) = t ∩ range f := ext $ assume x, ⟨assume ⟨x, hx, heq⟩, heq ▸ ⟨hx, mem_range_self _⟩, assume ⟨hx, ⟨y, h_eq⟩⟩, h_eq ▸ mem_image_of_mem f $ show y ∈ f ⁻¹' t, by simp [preimage, h_eq, hx]⟩ lemma image_preimage_eq_of_subset {f : α → β} {s : set β} (hs : s ⊆ range f) : f '' (f ⁻¹' s) = s := by rw [image_preimage_eq_inter_range, inter_eq_self_of_subset_left hs] theorem preimage_inter_range {f : α → β} {s : set β} : f ⁻¹' (s ∩ range f) = f ⁻¹' s := set.ext $ λ x, and_iff_left ⟨x, rfl⟩ theorem preimage_image_preimage {f : α → β} {s : set β} : f ⁻¹' (f '' (f ⁻¹' s)) = f ⁻¹' s := by rw [image_preimage_eq_inter_range, preimage_inter_range] @[simp] theorem quot_mk_range_eq [setoid α] : range (λx : α, ⟦x⟧) = univ := range_iff_surjective.2 quot.exists_rep lemma range_const_subset {c : β} : range (λx:α, c) ⊆ {c} := range_subset_iff.2 $ λ x, or.inl rfl @[simp] lemma range_const [h : nonempty α] {c : β} : range (λx:α, c) = {c} := begin refine subset.antisymm range_const_subset (λy hy, _), rw set.mem_singleton_iff.1 hy, rcases exists_mem_of_nonempty α with ⟨x, _⟩, exact mem_range_self x end def range_factorization (f : ι → β) : ι → range f := λ i, ⟨f i, mem_range_self i⟩ lemma range_factorization_eq {f : ι → β} : subtype.val ∘ range_factorization f = f := funext $ λ i, rfl lemma surjective_onto_range : surjective (range_factorization f) := λ ⟨_, ⟨i, rfl⟩⟩, ⟨i, rfl⟩ end range /-- The set `s` is pairwise `r` if `r x y` for all *distinct* `x y ∈ s`. -/ def pairwise_on (s : set α) (r : α → α → Prop) := ∀ x ∈ s, ∀ y ∈ s, x ≠ y → r x y theorem pairwise_on.mono {s t : set α} {r} (h : t ⊆ s) (hp : pairwise_on s r) : pairwise_on t r := λ x xt y yt, hp x (h xt) y (h yt) theorem pairwise_on.mono' {s : set α} {r r' : α → α → Prop} (H : ∀ a b, r a b → r' a b) (hp : pairwise_on s r) : pairwise_on s r' := λ x xs y ys h, H _ _ (hp x xs y ys h) end set /- image and preimage on subtypes -/ namespace subtype variable {α : Type*} lemma val_image {p : α → Prop} {s : set (subtype p)} : subtype.val '' s = {x | ∃h : p x, (⟨x, h⟩ : subtype p) ∈ s} := set.ext $ assume a, ⟨assume ⟨⟨a', ha'⟩, in_s, h_eq⟩, h_eq ▸ ⟨ha', in_s⟩, assume ⟨ha, in_s⟩, ⟨⟨a, ha⟩, in_s, rfl⟩⟩ @[simp] lemma val_range {p : α → Prop} : set.range (@subtype.val _ p) = {x | p x} := by rw ← set.image_univ; simp [-set.image_univ, val_image] theorem val_image_subset (s : set α) (t : set (subtype s)) : t.image val ⊆ s := λ x ⟨y, yt, yvaleq⟩, by rw ←yvaleq; exact y.property theorem val_image_univ (s : set α) : @val _ s '' set.univ = s := set.eq_of_subset_of_subset (val_image_subset _ _) (λ x xs, ⟨⟨x, xs⟩, ⟨set.mem_univ _, rfl⟩⟩) theorem image_preimage_val (s t : set α) : (@subtype.val _ s) '' ((@subtype.val _ s) ⁻¹' t) = t ∩ s := begin ext x, simp, split, { rintros ⟨y, ys, yt, yx⟩, rw ←yx, exact ⟨yt, ys⟩ }, rintros ⟨xt, xs⟩, exact ⟨x, xs, xt, rfl⟩ end theorem preimage_val_eq_preimage_val_iff (s t u : set α) : ((@subtype.val _ s) ⁻¹' t = (@subtype.val _ s) ⁻¹' u) ↔ (t ∩ s = u ∩ s) := begin rw [←image_preimage_val, ←image_preimage_val], split, { intro h, rw h }, intro h, exact set.injective_image (val_injective) h end end subtype namespace set section range variable {α : Type*} @[simp] lemma subtype.val_range {p : α → Prop} : range (@subtype.val _ p) = {x | p x} := by rw ← image_univ; simp [-image_univ, subtype.val_image] @[simp] lemma range_coe_subtype (s : set α): range (coe : s → α) = s := subtype.val_range end range section prod variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*} variables {s s₁ s₂ : set α} {t t₁ t₂ : set β} /-- The cartesian product `prod s t` is the set of `(a, b)` such that `a ∈ s` and `b ∈ t`. -/ protected def prod (s : set α) (t : set β) : set (α × β) := {p | p.1 ∈ s ∧ p.2 ∈ t} lemma prod_eq (s : set α) (t : set β) : set.prod s t = prod.fst ⁻¹' s ∩ prod.snd ⁻¹' t := rfl theorem mem_prod_eq {p : α × β} : p ∈ set.prod s t = (p.1 ∈ s ∧ p.2 ∈ t) := rfl @[simp] theorem mem_prod {p : α × β} : p ∈ set.prod s t ↔ p.1 ∈ s ∧ p.2 ∈ t := iff.rfl lemma mk_mem_prod {a : α} {b : β} (a_in : a ∈ s) (b_in : b ∈ t) : (a, b) ∈ set.prod s t := ⟨a_in, b_in⟩ @[simp] theorem prod_empty {s : set α} : set.prod s ∅ = (∅ : set (α × β)) := ext $ by simp [set.prod] @[simp] theorem empty_prod {t : set β} : set.prod ∅ t = (∅ : set (α × β)) := ext $ by simp [set.prod] theorem insert_prod {a : α} {s : set α} {t : set β} : set.prod (insert a s) t = (prod.mk a '' t) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_insert {b : β} {s : set α} {t : set β} : set.prod s (insert b t) = ((λa, (a, b)) '' s) ∪ set.prod s t := ext begin simp [set.prod, image, iff_def, or_imp_distrib] {contextual := tt}; cc end theorem prod_preimage_eq {f : γ → α} {g : δ → β} : set.prod (preimage f s) (preimage g t) = preimage (λp, (f p.1, g p.2)) (set.prod s t) := rfl theorem prod_mono {s₁ s₂ : set α} {t₁ t₂ : set β} (hs : s₁ ⊆ s₂) (ht : t₁ ⊆ t₂) : set.prod s₁ t₁ ⊆ set.prod s₂ t₂ := assume x ⟨h₁, h₂⟩, ⟨hs h₁, ht h₂⟩ theorem prod_inter_prod : set.prod s₁ t₁ ∩ set.prod s₂ t₂ = set.prod (s₁ ∩ s₂) (t₁ ∩ t₂) := subset.antisymm (assume ⟨a, b⟩ ⟨⟨ha₁, hb₁⟩, ⟨ha₂, hb₂⟩⟩, ⟨⟨ha₁, ha₂⟩, ⟨hb₁, hb₂⟩⟩) (subset_inter (prod_mono (inter_subset_left _ _) (inter_subset_left _ _)) (prod_mono (inter_subset_right _ _) (inter_subset_right _ _))) theorem image_swap_prod : (λp:β×α, (p.2, p.1)) '' set.prod t s = set.prod s t := ext $ assume ⟨a, b⟩, by simp [mem_image_eq, set.prod, and_comm]; exact ⟨ assume ⟨b', a', ⟨h_a, h_b⟩, h⟩, by subst a'; subst b'; assumption, assume h, ⟨b, a, ⟨rfl, rfl⟩, h⟩⟩ theorem image_swap_eq_preimage_swap : image (@prod.swap α β) = preimage prod.swap := image_eq_preimage_of_inverse prod.swap_left_inverse prod.swap_right_inverse theorem prod_image_image_eq {m₁ : α → γ} {m₂ : β → δ} : set.prod (image m₁ s) (image m₂ t) = image (λp:α×β, (m₁ p.1, m₂ p.2)) (set.prod s t) := ext $ by simp [-exists_and_distrib_right, exists_and_distrib_right.symm, and.left_comm, and.assoc, and.comm] theorem prod_range_range_eq {α β γ δ} {m₁ : α → γ} {m₂ : β → δ} : set.prod (range m₁) (range m₂) = range (λp:α×β, (m₁ p.1, m₂ p.2)) := ext $ by simp [range] @[simp] theorem prod_singleton_singleton {a : α} {b : β} : set.prod {a} {b} = ({(a, b)} : set (α×β)) := ext $ by simp [set.prod] theorem prod_neq_empty_iff {s : set α} {t : set β} : set.prod s t ≠ ∅ ↔ (s ≠ ∅ ∧ t ≠ ∅) := by simp [not_eq_empty_iff_exists] theorem prod_eq_empty_iff {s : set α} {t : set β} : set.prod s t = ∅ ↔ (s = ∅ ∨ t = ∅) := suffices (¬ set.prod s t ≠ ∅) ↔ (¬ s ≠ ∅ ∨ ¬ t ≠ ∅), by simpa only [(≠), classical.not_not], by classical; rw [prod_neq_empty_iff, not_and_distrib] @[simp] theorem prod_mk_mem_set_prod_eq {a : α} {b : β} {s : set α} {t : set β} : (a, b) ∈ set.prod s t = (a ∈ s ∧ b ∈ t) := rfl @[simp] theorem univ_prod_univ : set.prod (@univ α) (@univ β) = univ := ext $ assume ⟨a, b⟩, by simp lemma prod_sub_preimage_iff {W : set γ} {f : α × β → γ} : set.prod s t ⊆ f ⁻¹' W ↔ ∀ a b, a ∈ s → b ∈ t → f (a, b) ∈ W := by simp [subset_def] end prod section pi variables {α : Type*} {π : α → Type*} def pi (i : set α) (s : Πa, set (π a)) : set (Πa, π a) := { f | ∀a∈i, f a ∈ s a } @[simp] lemma pi_empty_index (s : Πa, set (π a)) : pi ∅ s = univ := by ext; simp [pi] @[simp] lemma pi_insert_index (a : α) (i : set α) (s : Πa, set (π a)) : pi (insert a i) s = ((λf, f a) ⁻¹' s a) ∩ pi i s := by ext; simp [pi, or_imp_distrib, forall_and_distrib] @[simp] lemma pi_singleton_index (a : α) (s : Πa, set (π a)) : pi {a} s = ((λf:(Πa, π a), f a) ⁻¹' s a) := by ext; simp [pi] lemma pi_if {p : α → Prop} [h : decidable_pred p] (i : set α) (s t : Πa, set (π a)) : pi i (λa, if p a then s a else t a) = pi {a ∈ i | p a} s ∩ pi {a ∈ i | ¬ p a} t := begin ext f, split, { assume h, split; { rintros a ⟨hai, hpa⟩, simpa [*] using h a } }, { rintros ⟨hs, ht⟩ a hai, by_cases p a; simp [*, pi] at * } end end pi end set
3b7a550e6f63fbb99ad3c6a580820b2924fce96f
4727251e0cd73359b15b664c3170e5d754078599
/src/set_theory/ordinal/fixed_point.lean
d5e81eb1092fb447b5678228245765bcad5aa964
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
23,287
lean
/- Copyright (c) 2018 Violeta Hernández Palacios, Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Violeta Hernández Palacios, Mario Carneiro -/ import set_theory.ordinal.arithmetic /-! # Fixed points of normal functions We prove various statements about the fixed points of normal ordinal functions. We state them in three forms: as statements about type-indexed families of normal functions, as statements about ordinal-indexed families of normal functions, and as statements about a single normal function. For the most part, the first case encompasses the others. Moreover, we prove some lemmas about the fixed points of specific normal functions. ## Main definitions and results * `nfp_family`, `nfp_bfamily`, `nfp`: the next fixed point of a (family of) normal function(s). * `fp_family_unbounded`, `fp_bfamily_unbounded`, `fp_unbounded`: the (common) fixed points of a (family of) normal function(s) are unbounded in the ordinals. * `deriv_add_eq_mul_omega_add`: a characterization of the derivative of addition. * `deriv_mul_eq_opow_omega_mul`: a characterization of the derivative of multiplication. -/ noncomputable theory universes u v open function namespace ordinal /-! ### Fixed points of type-indexed families of ordinals -/ section variables {ι : Type u} {f : ι → ordinal.{max u v} → ordinal.{max u v}} /-- The next common fixed point, at least `a`, for a family of normal functions. `ordinal.nfp_family_fp` shows this is a fixed point, `ordinal.le_nfp_family` shows it's at least `a`, and `ordinal.nfp_family_le_fp` shows this is the least ordinal with these properties. -/ def nfp_family (f : ι → ordinal → ordinal) (a) : ordinal := sup (list.foldr f a) theorem nfp_family_eq_sup (f : ι → ordinal → ordinal) (a) : nfp_family f a = sup (list.foldr f a) := rfl theorem foldr_le_nfp_family (f : ι → ordinal → ordinal) (a l) : list.foldr f a l ≤ nfp_family f a := le_sup _ _ theorem le_nfp_family (f : ι → ordinal → ordinal) (a) : a ≤ nfp_family f a := le_sup _ [] theorem lt_nfp_family {a b} : a < nfp_family f b ↔ ∃ l, a < list.foldr f b l := lt_sup theorem nfp_family_le_iff {a b} : nfp_family f a ≤ b ↔ ∀ l, list.foldr f a l ≤ b := sup_le_iff theorem nfp_family_le {a b} : (∀ l, list.foldr f a l ≤ b) → nfp_family f a ≤ b := sup_le theorem nfp_family_monotone (hf : ∀ i, monotone (f i)) : monotone (nfp_family f) := λ a b h, sup_le $ λ l, (list.foldr_monotone hf l h).trans (le_sup _ l) theorem apply_lt_nfp_family (H : ∀ i, is_normal (f i)) {a b} (hb : b < nfp_family f a) (i) : f i b < nfp_family f a := let ⟨l, hl⟩ := lt_nfp_family.1 hb in lt_sup.2 ⟨i :: l, (H i).strict_mono hl⟩ theorem apply_lt_nfp_family_iff [nonempty ι] (H : ∀ i, is_normal (f i)) {a b} : (∀ i, f i b < nfp_family f a) ↔ b < nfp_family f a := ⟨λ h, lt_nfp_family.2 $ let ⟨l, hl⟩ := lt_sup.1 (h (classical.arbitrary ι)) in ⟨l, ((H _).self_le b).trans_lt hl⟩, apply_lt_nfp_family H⟩ theorem nfp_family_le_apply [nonempty ι] (H : ∀ i, is_normal (f i)) {a b} : (∃ i, nfp_family f a ≤ f i b) ↔ nfp_family f a ≤ b := by { rw ←not_iff_not, push_neg, exact apply_lt_nfp_family_iff H } theorem nfp_family_le_fp (H : ∀ i, monotone (f i)) {a b} (ab : a ≤ b) (h : ∀ i, f i b ≤ b) : nfp_family f a ≤ b := sup_le $ λ l, begin by_cases hι : is_empty ι, { rwa @unique.eq_default _ (@list.unique_of_is_empty ι hι) l }, { haveI := not_is_empty_iff.1 hι, induction l with i l IH generalizing a, {exact ab}, exact (H i (IH ab)).trans (h i) } end theorem nfp_family_fp {i} (H : is_normal (f i)) (a) : f i (nfp_family f a) = nfp_family f a := begin unfold nfp_family, rw @is_normal.sup _ H _ _ ⟨[]⟩, apply le_antisymm; refine ordinal.sup_le (λ l, _), { exact le_sup _ (i :: l) }, { exact (H.self_le _).trans (le_sup _ _) } end theorem apply_le_nfp_family [hι : nonempty ι] {f : ι → ordinal → ordinal} (H : ∀ i, is_normal (f i)) {a b} : (∀ i, f i b ≤ nfp_family f a) ↔ b ≤ nfp_family f a := begin refine ⟨λ h, _, λ h i, _⟩, { unfreezingI { cases hι with i }, exact ((H i).self_le b).trans (h i) }, rw ←nfp_family_fp (H i), exact (H i).monotone h end theorem nfp_family_eq_self {f : ι → ordinal → ordinal} {a} (h : ∀ i, f i a = a) : nfp_family f a = a := le_antisymm (sup_le (λ l, (by rw list.foldr_fixed' h l))) (le_nfp_family f a) /-- A generalization of the fixed point lemma for normal functions: any family of normal functions has an unbounded set of common fixed points. -/ theorem fp_family_unbounded (H : ∀ i, is_normal (f i)) : (⋂ i, function.fixed_points (f i)).unbounded (<) := λ a, ⟨_, λ s ⟨i, hi⟩, begin rw ←hi, exact nfp_family_fp (H i) a end, (le_nfp_family f a).not_lt⟩ /-- The derivative of a family of normal functions is the sequence of their common fixed points. -/ def deriv_family (f : ι → ordinal → ordinal) (o : ordinal) : ordinal := limit_rec_on o (nfp_family f 0) (λ a IH, nfp_family f (succ IH)) (λ a l, bsup.{(max u v) u} a) @[simp] theorem deriv_family_zero (f : ι → ordinal → ordinal) : deriv_family f 0 = nfp_family f 0 := limit_rec_on_zero _ _ _ @[simp] theorem deriv_family_succ (f : ι → ordinal → ordinal) (o) : deriv_family f (succ o) = nfp_family f (succ (deriv_family f o)) := limit_rec_on_succ _ _ _ _ theorem deriv_family_limit (f : ι → ordinal → ordinal) {o} : is_limit o → deriv_family f o = bsup.{(max u v) u} o (λ a _, deriv_family f a) := limit_rec_on_limit _ _ _ _ theorem deriv_family_is_normal (f : ι → ordinal → ordinal) : is_normal (deriv_family f) := ⟨λ o, by rw [deriv_family_succ, ← succ_le]; apply le_nfp_family, λ o l a, by rw [deriv_family_limit _ l, bsup_le_iff]⟩ theorem deriv_family_fp {i} (H : is_normal (f i)) (o : ordinal.{max u v}) : f i (deriv_family f o) = deriv_family f o := begin refine limit_rec_on o _ (λ o IH, _) _, { rw [deriv_family_zero], exact nfp_family_fp H 0 }, { rw [deriv_family_succ], exact nfp_family_fp H _ }, { intros o l IH, rw [deriv_family_limit _ l, is_normal.bsup.{(max u v) u (max u v)} H (λ a _, deriv_family f a) l.1], refine eq_of_forall_ge_iff (λ c, _), simp only [bsup_le_iff, IH] {contextual:=tt} } end theorem le_iff_deriv_family (H : ∀ i, is_normal (f i)) {a} : (∀ i, f i a ≤ a) ↔ ∃ o, deriv_family f o = a := ⟨λ ha, begin suffices : ∀ o (_ : a ≤ deriv_family f o), ∃ o, deriv_family f o = a, from this a ((deriv_family_is_normal _).self_le _), refine λ o, limit_rec_on o (λ h₁, ⟨0, le_antisymm _ h₁⟩) (λ o IH h₁, _) (λ o l IH h₁, _), { rw deriv_family_zero, exact nfp_family_le_fp (λ i, (H i).monotone) (ordinal.zero_le _) ha }, { cases le_or_lt a (deriv_family f o), {exact IH h}, refine ⟨succ o, le_antisymm _ h₁⟩, rw deriv_family_succ, exact nfp_family_le_fp (λ i, (H i).monotone) (succ_le.2 h) ha }, { cases eq_or_lt_of_le h₁, {exact ⟨_, h.symm⟩}, rw [deriv_family_limit _ l, ← not_le, bsup_le_iff, not_ball] at h, exact let ⟨o', h, hl⟩ := h in IH o' h (le_of_not_le hl) } end, λ ⟨o, e⟩ i, e ▸ le_of_eq (deriv_family_fp (H i) _)⟩ theorem fp_iff_deriv_family (H : ∀ i, is_normal (f i)) {a} : (∀ i, f i a = a) ↔ ∃ o, deriv_family f o = a := iff.trans ⟨λ h i, le_of_eq (h i), λ h i, (H i).le_iff_eq.1 (h i)⟩ (le_iff_deriv_family H) theorem deriv_family_eq_enum_ord (H : ∀ i, is_normal (f i)) : deriv_family f = enum_ord (⋂ i, function.fixed_points (f i)) := begin rw ←eq_enum_ord _ (fp_family_unbounded H), use (deriv_family_is_normal f).strict_mono, rw set.range_eq_iff, refine ⟨_, λ a ha, _⟩, { rintros a S ⟨i, hi⟩, rw ←hi, exact deriv_family_fp (H i) a }, rw set.mem_Inter at ha, rwa ←fp_iff_deriv_family H end end /-! ### Fixed points of ordinal-indexed families of ordinals -/ section variables {o : ordinal.{u}} {f : Π b < o, ordinal.{max u v} → ordinal.{max u v}} /-- The next common fixed point, at least `a`, for a family of normal functions indexed by ordinals. -/ def nfp_bfamily (o : ordinal) (f : Π b < o, ordinal → ordinal) : ordinal → ordinal := nfp_family (family_of_bfamily o f) theorem nfp_bfamily_eq_nfp_family {o : ordinal} (f : Π b < o, ordinal → ordinal) : nfp_bfamily o f = nfp_family (family_of_bfamily o f) := rfl theorem foldr_le_nfp_bfamily {o : ordinal} (f : Π b < o, ordinal → ordinal) (a l) : list.foldr (family_of_bfamily o f) a l ≤ nfp_bfamily o f a := le_sup _ _ theorem le_nfp_bfamily {o : ordinal} (f : Π b < o, ordinal → ordinal) (a) : a ≤ nfp_bfamily o f a := le_sup _ [] theorem lt_nfp_bfamily {a b} : a < nfp_bfamily o f b ↔ ∃ l, a < list.foldr (family_of_bfamily o f) b l := lt_sup theorem nfp_bfamily_le_iff {o : ordinal} {f : Π b < o, ordinal → ordinal} {a b} : nfp_bfamily o f a ≤ b ↔ ∀ l, list.foldr (family_of_bfamily o f) a l ≤ b := sup_le_iff theorem nfp_bfamily_le {o : ordinal} {f : Π b < o, ordinal → ordinal} {a b} : (∀ l, list.foldr (family_of_bfamily o f) a l ≤ b) → nfp_bfamily o f a ≤ b := sup_le theorem nfp_bfamily_monotone (hf : ∀ i hi, monotone (f i hi)) : monotone (nfp_bfamily o f) := nfp_family_monotone (λ i, hf _ _) theorem apply_lt_nfp_bfamily (ho : o ≠ 0) (H : ∀ i hi, is_normal (f i hi)) {a b} : (∀ i hi, f i hi b < nfp_bfamily o f a) ↔ b < nfp_bfamily o f a := begin unfold nfp_bfamily, rw ←@apply_lt_nfp_family_iff _ (family_of_bfamily o f) (out_nonempty_iff_ne_zero.2 ho) (λ i, H _ _), refine ⟨λ h i, h _ (typein_lt_self i), λ h i hio, _⟩, rw ←family_of_bfamily_enum o f, apply h end theorem nfp_bfamily_le_apply (ho : o ≠ 0) (H : ∀ i hi, is_normal (f i hi)) {a b} : (∃ i hi, nfp_bfamily o f a ≤ f i hi b) ↔ nfp_bfamily o f a ≤ b := by { rw ←not_iff_not, push_neg, convert apply_lt_nfp_bfamily ho H, simp only [not_le] } theorem nfp_bfamily_le_fp (H : ∀ i hi, monotone (f i hi)) {a b} (ab : a ≤ b) (h : ∀ i hi, f i hi b ≤ b) : nfp_bfamily o f a ≤ b := nfp_family_le_fp (λ _, H _ _) ab (λ i, h _ _) theorem nfp_bfamily_fp {i hi} (H : is_normal (f i hi)) (a) : f i hi (nfp_bfamily o f a) = nfp_bfamily o f a := by { rw ←family_of_bfamily_enum o f, apply nfp_family_fp, rw family_of_bfamily_enum, exact H } theorem apply_le_nfp_bfamily (ho : o ≠ 0) (H : ∀ i hi, is_normal (f i hi)) {a b} : (∀ i hi, f i hi b ≤ nfp_bfamily o f a) ↔ b ≤ nfp_bfamily o f a := begin refine ⟨λ h, _, λ h i hi, _⟩, { have ho' : 0 < o := ordinal.pos_iff_ne_zero.2 ho, exact ((H 0 ho').self_le b).trans (h 0 ho') }, rw ←nfp_bfamily_fp (H i hi), exact (H i hi).monotone h end theorem nfp_bfamily_eq_self {a} (h : ∀ i hi, f i hi a = a) : nfp_bfamily o f a = a := nfp_family_eq_self (λ _, h _ _) /-- A generalization of the fixed point lemma for normal functions: any family of normal functions has an unbounded set of common fixed points. -/ theorem fp_bfamily_unbounded (H : ∀ i hi, is_normal (f i hi)) : (⋂ i hi, function.fixed_points (f i hi)).unbounded (<) := λ a, ⟨_, by { rw set.mem_Inter₂, exact λ i hi, nfp_bfamily_fp (H i hi) _ }, (le_nfp_bfamily f a).not_lt⟩ /-- The derivative of a family of normal functions is the sequence of their common fixed points. -/ def deriv_bfamily (o : ordinal) (f : Π b < o, ordinal → ordinal) : ordinal → ordinal := deriv_family (family_of_bfamily o f) theorem deriv_bfamily_eq_deriv_family {o : ordinal} (f : Π b < o, ordinal → ordinal) : deriv_bfamily o f = deriv_family (family_of_bfamily o f) := rfl theorem deriv_bfamily_is_normal {o : ordinal} (f : Π b < o, ordinal → ordinal) : is_normal (deriv_bfamily o f) := deriv_family_is_normal _ theorem deriv_bfamily_fp {i hi} (H : is_normal (f i hi)) (a : ordinal) : f i hi (deriv_bfamily o f a) = deriv_bfamily o f a := by { rw ←family_of_bfamily_enum o f, apply deriv_family_fp, rw family_of_bfamily_enum, exact H } theorem le_iff_deriv_bfamily (H : ∀ i hi, is_normal (f i hi)) {a} : (∀ i hi, f i hi a ≤ a) ↔ ∃ b, deriv_bfamily o f b = a := begin unfold deriv_bfamily, rw ←le_iff_deriv_family, { refine ⟨λ h i, h _ _, λ h i hi, _⟩, rw ←family_of_bfamily_enum o f, apply h }, exact λ _, H _ _ end theorem fp_iff_deriv_bfamily (H : ∀ i hi, is_normal (f i hi)) {a} : (∀ i hi, f i hi a = a) ↔ ∃ b, deriv_bfamily o f b = a := begin rw ←le_iff_deriv_bfamily H, refine ⟨λ h i hi, le_of_eq (h i hi), λ h i hi, _⟩, rw ←(H i hi).le_iff_eq, exact h i hi end theorem deriv_bfamily_eq_enum_ord (H : ∀ i hi, is_normal (f i hi)) : deriv_bfamily o f = enum_ord (⋂ i hi, function.fixed_points (f i hi)) := begin rw ←eq_enum_ord _ (fp_bfamily_unbounded H), use (deriv_bfamily_is_normal f).strict_mono, rw set.range_eq_iff, refine ⟨λ a, set.mem_Inter₂.2 (λ i hi, deriv_bfamily_fp (H i hi) a), λ a ha, _⟩, rw set.mem_Inter₂ at ha, rwa ←fp_iff_deriv_bfamily H end end /-! ### Fixed points of a single function -/ section variable {f : ordinal.{u} → ordinal.{u}} /-- The next fixed point function, the least fixed point of the normal function `f`, at least `a`. -/ def nfp (f : ordinal → ordinal) : ordinal → ordinal := nfp_family (λ _ : unit, f) theorem nfp_eq_nfp_family (f : ordinal → ordinal) : nfp f = nfp_family (λ _ : unit, f) := rfl @[simp] theorem sup_iterate_eq_nfp (f : ordinal.{u} → ordinal.{u}) : (λ a, sup (λ n : ℕ, f^[n] a)) = nfp f := begin refine funext (λ a, le_antisymm _ (sup_le (λ l, _))), { rw sup_le_iff, intro n, rw [←list.length_repeat unit.star n, ←list.foldr_const f a], apply le_sup }, { rw list.foldr_const f a l, exact le_sup _ _ }, end theorem iterate_le_nfp (f a n) : f^[n] a ≤ nfp f a := by { rw ←sup_iterate_eq_nfp, exact le_sup _ n } theorem le_nfp (f a) : a ≤ nfp f a := iterate_le_nfp f a 0 theorem lt_nfp {a b} : a < nfp f b ↔ ∃ n, a < (f^[n]) b := by { rw ←sup_iterate_eq_nfp, exact lt_sup } theorem nfp_le_iff {a b} : nfp f a ≤ b ↔ ∀ n, (f^[n]) a ≤ b := by { rw ←sup_iterate_eq_nfp, exact sup_le_iff } theorem nfp_le {a b} : (∀ n, (f^[n]) a ≤ b) → nfp f a ≤ b := nfp_le_iff.2 @[simp] theorem nfp_id : nfp id = id := funext (λ a, begin simp_rw [←sup_iterate_eq_nfp, iterate_id], exact sup_const a end) theorem nfp_monotone (hf : monotone f) : monotone (nfp f) := nfp_family_monotone (λ i, hf) theorem is_normal.apply_lt_nfp {f} (H : is_normal f) {a b} : f b < nfp f a ↔ b < nfp f a := begin unfold nfp, rw ←@apply_lt_nfp_family_iff unit (λ _, f) _ (λ _, H) a b, exact ⟨λ h _, h, λ h, h unit.star⟩ end theorem is_normal.nfp_le_apply {f} (H : is_normal f) {a b} : nfp f a ≤ f b ↔ nfp f a ≤ b := le_iff_le_iff_lt_iff_lt.2 H.apply_lt_nfp theorem nfp_le_fp {f} (H : monotone f) {a b} (ab : a ≤ b) (h : f b ≤ b) : nfp f a ≤ b := nfp_family_le_fp (λ _, H) ab (λ _, h) theorem is_normal.nfp_fp {f} (H : is_normal f) : ∀ a, f (nfp f a) = nfp f a := @nfp_family_fp unit (λ _, f) unit.star H theorem is_normal.apply_le_nfp {f} (H : is_normal f) {a b} : f b ≤ nfp f a ↔ b ≤ nfp f a := ⟨le_trans (H.self_le _), λ h, by simpa only [H.nfp_fp] using H.le_iff.2 h⟩ theorem nfp_eq_self {f : ordinal → ordinal} {a} (h : f a = a) : nfp f a = a := nfp_family_eq_self (λ _, h) /-- The fixed point lemma for normal functions: any normal function has an unbounded set of fixed points. -/ theorem fp_unbounded (H : is_normal f) : (function.fixed_points f).unbounded (<) := by { convert fp_family_unbounded (λ _ : unit, H), exact (set.Inter_const _).symm } /-- The derivative of a normal function `f` is the sequence of fixed points of `f`. -/ def deriv (f : ordinal → ordinal) : ordinal → ordinal := deriv_family (λ _ : unit, f) theorem deriv_eq_deriv_family (f : ordinal → ordinal) : deriv f = deriv_family (λ _ : unit, f) := rfl @[simp] theorem deriv_zero (f) : deriv f 0 = nfp f 0 := deriv_family_zero _ @[simp] theorem deriv_succ (f o) : deriv f (succ o) = nfp f (succ (deriv f o)) := deriv_family_succ _ _ theorem deriv_limit (f) {o} : is_limit o → deriv f o = bsup.{u 0} o (λ a _, deriv f a) := deriv_family_limit _ theorem deriv_is_normal (f) : is_normal (deriv f) := deriv_family_is_normal _ theorem deriv_id_of_nfp_id {f : ordinal → ordinal} (h : nfp f = id) : deriv f = id := ((deriv_is_normal _).eq_iff_zero_and_succ is_normal.refl).2 (by simp [h, succ_inj]) theorem is_normal.deriv_fp {f} (H : is_normal f) : ∀ o, f (deriv f o) = deriv f o := @deriv_family_fp unit (λ _, f) unit.star H theorem is_normal.le_iff_deriv {f} (H : is_normal f) {a} : f a ≤ a ↔ ∃ o, deriv f o = a := begin unfold deriv, rw ←le_iff_deriv_family (λ _ : unit, H), exact ⟨λ h _, h, λ h, h unit.star⟩ end theorem is_normal.fp_iff_deriv {f} (H : is_normal f) {a} : f a = a ↔ ∃ o, deriv f o = a := by rw [←H.le_iff_eq, H.le_iff_deriv] theorem deriv_eq_enum_ord (H : is_normal f) : deriv f = enum_ord (function.fixed_points f) := by { convert deriv_family_eq_enum_ord (λ _ : unit, H), exact (set.Inter_const _).symm } theorem deriv_eq_id_of_nfp_eq_id {f : ordinal → ordinal} (h : nfp f = id) : deriv f = id := (is_normal.eq_iff_zero_and_succ (deriv_is_normal _) is_normal.refl).2 (by simp [h, succ_inj]) end /-! ### Fixed points of addition -/ @[simp] theorem nfp_add_zero (a) : nfp ((+) a) 0 = a * omega := begin simp_rw [←sup_iterate_eq_nfp, ←sup_mul_nat], congr, funext, induction n with n hn, { rw [nat.cast_zero, mul_zero, iterate_zero_apply] }, { nth_rewrite 1 nat.succ_eq_one_add, rw [nat.cast_add, nat.cast_one, mul_one_add, iterate_succ_apply', hn] } end theorem nfp_add_eq_mul_omega {a b} (hba : b ≤ a * omega) : nfp ((+) a) b = a * omega := begin apply le_antisymm (nfp_le_fp (add_is_normal a).monotone hba _), { rw ←nfp_add_zero, exact nfp_monotone (add_is_normal a).monotone (ordinal.zero_le b) }, { rw [←mul_one_add, one_add_omega] } end theorem add_eq_right_iff_mul_omega_le {a b : ordinal} : a + b = b ↔ a * omega ≤ b := begin refine ⟨λ h, _, λ h, _⟩, { rw [←nfp_add_zero a, ←deriv_zero], cases (add_is_normal a).fp_iff_deriv.1 h with c hc, rw ←hc, exact (deriv_is_normal _).monotone (ordinal.zero_le _) }, { have := ordinal.add_sub_cancel_of_le h, nth_rewrite 0 ←this, rwa [←add_assoc, ←mul_one_add, one_add_omega] } end theorem add_le_right_iff_mul_omega_le {a b : ordinal} : a + b ≤ b ↔ a * omega ≤ b := by { rw ←add_eq_right_iff_mul_omega_le, exact (add_is_normal a).le_iff_eq } theorem deriv_add_eq_mul_omega_add (a b : ordinal.{u}) : deriv ((+) a) b = a * omega + b := begin revert b, rw [←funext_iff, is_normal.eq_iff_zero_and_succ (deriv_is_normal _) (add_is_normal _)], refine ⟨_, λ a h, _⟩, { rw [deriv_zero, add_zero], exact nfp_add_zero a }, { rw [deriv_succ, h, add_succ], exact nfp_eq_self (add_eq_right_iff_mul_omega_le.2 ((le_add_right _ _).trans (lt_succ_self _).le)) } end /-! ### Fixed points of multiplication -/ local infixr ^ := @pow ordinal ordinal ordinal.has_pow @[simp] theorem nfp_mul_one {a : ordinal} (ha : 0 < a) : nfp ((*) a) 1 = a ^ omega := begin rw [←sup_iterate_eq_nfp, ←sup_opow_nat], { dsimp, congr, funext, induction n with n hn, { rw [nat.cast_zero, opow_zero, iterate_zero_apply] }, nth_rewrite 1 nat.succ_eq_one_add, rw [nat.cast_add, nat.cast_one, opow_add, opow_one, iterate_succ_apply', hn] }, { exact ha } end @[simp] theorem nfp_mul_zero (a : ordinal) : nfp ((*) a) 0 = 0 := begin rw [←ordinal.le_zero, nfp_le_iff], intro n, induction n with n hn, { refl }, rwa [iterate_succ_apply, mul_zero] end @[simp] theorem nfp_zero_mul : nfp ((*) 0) = id := begin rw ←sup_iterate_eq_nfp, refine funext (λ a, (sup_le (λ n, _)).antisymm (le_sup (λ n, ((*) 0)^[n] a) 0)), induction n with n hn, { refl }, rw function.iterate_succ', change 0 * _ ≤ a, rw zero_mul, exact ordinal.zero_le a end @[simp] theorem deriv_mul_zero : deriv ((*) 0) = id := deriv_eq_id_of_nfp_eq_id nfp_zero_mul theorem nfp_mul_eq_opow_omega {a b : ordinal} (hb : 0 < b) (hba : b ≤ a ^ omega) : nfp ((*) a) b = a ^ omega.{u} := begin cases eq_zero_or_pos a with ha ha, { rw [ha, zero_opow omega_ne_zero] at *, rw [ordinal.le_zero.1 hba, nfp_zero_mul], refl }, apply le_antisymm, { apply nfp_le_fp (mul_is_normal ha).monotone hba, rw [←opow_one_add, one_add_omega] }, rw ←nfp_mul_one ha, exact nfp_monotone (mul_is_normal ha).monotone (one_le_iff_pos.2 hb) end theorem eq_zero_or_opow_omega_le_of_mul_eq_right {a b : ordinal} (hab : a * b = b) : b = 0 ∨ a ^ omega.{u} ≤ b := begin cases eq_zero_or_pos a with ha ha, { rw [ha, zero_opow omega_ne_zero], exact or.inr (ordinal.zero_le b) }, rw or_iff_not_imp_left, intro hb, change b ≠ 0 at hb, rw ←nfp_mul_one ha, rw ←one_le_iff_ne_zero at hb, exact nfp_le_fp (mul_is_normal ha).monotone hb (le_of_eq hab) end theorem mul_eq_right_iff_opow_omega_dvd {a b : ordinal} : a * b = b ↔ a ^ omega ∣ b := begin cases eq_zero_or_pos a with ha ha, { rw [ha, zero_mul, zero_opow omega_ne_zero, zero_dvd], exact eq_comm }, refine ⟨λ hab, _, λ h, _⟩, { rw dvd_iff_mod_eq_zero, rw [←div_add_mod b (a ^ omega), mul_add, ←mul_assoc, ←opow_one_add, one_add_omega, add_left_cancel] at hab, cases eq_zero_or_opow_omega_le_of_mul_eq_right hab with hab hab, { exact hab }, refine (not_lt_of_le hab (mod_lt b (opow_ne_zero omega _))).elim, rwa ←ordinal.pos_iff_ne_zero }, cases h with c hc, rw [hc, ←mul_assoc, ←opow_one_add, one_add_omega] end theorem mul_le_right_iff_opow_omega_dvd {a b : ordinal} (ha : 0 < a) : a * b ≤ b ↔ a ^ omega ∣ b := by { rw ←mul_eq_right_iff_opow_omega_dvd, exact (mul_is_normal ha).le_iff_eq } theorem nfp_mul_opow_omega_add {a c : ordinal} (b) (ha : 0 < a) (hc : 0 < c) (hca : c ≤ a ^ omega) : nfp ((*) a) (a ^ omega * b + c) = a ^ omega.{u} * b.succ := begin apply le_antisymm, { apply nfp_le_fp (mul_is_normal ha).monotone, { rw mul_succ, apply add_le_add_left hca }, { rw [←mul_assoc, ←opow_one_add, one_add_omega] } }, { cases mul_eq_right_iff_opow_omega_dvd.1 ((mul_is_normal ha).nfp_fp (a ^ omega * b + c)) with d hd, rw hd, apply mul_le_mul_left', have := le_nfp (has_mul.mul a) (a ^ omega * b + c), rw hd at this, have := (add_lt_add_left hc (a ^ omega * b)).trans_le this, rw [add_zero, mul_lt_mul_iff_left (opow_pos omega ha)] at this, rwa succ_le } end theorem deriv_mul_eq_opow_omega_mul {a : ordinal.{u}} (ha : 0 < a) (b) : deriv ((*) a) b = a ^ omega * b := begin revert b, rw [←funext_iff, is_normal.eq_iff_zero_and_succ (deriv_is_normal _) (mul_is_normal (opow_pos omega ha))], refine ⟨_, λ c h, _⟩, { rw [deriv_zero, nfp_mul_zero, mul_zero] }, { rw [deriv_succ, h], exact nfp_mul_opow_omega_add c ha zero_lt_one (one_le_iff_pos.2 (opow_pos _ ha)) }, end end ordinal
9af6aa303b77bb65e87fe330d3550a18f9faa7ef
94e33a31faa76775069b071adea97e86e218a8ee
/src/order/filter/germ.lean
2ccd6960c3068a1fa050da25d87956737ee21e4c
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
22,598
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury G. Kudryashov, Abhimanyu Pallavi Sudhir -/ import order.filter.basic import algebra.module.pi /-! # Germ of a function at a filter The germ of a function `f : α → β` at a filter `l : filter α` is the equivalence class of `f` with respect to the equivalence relation `eventually_eq l`: `f ≈ g` means `∀ᶠ x in l, f x = g x`. ## Main definitions We define * `germ l β` to be the space of germs of functions `α → β` at a filter `l : filter α`; * coercion from `α → β` to `germ l β`: `(f : germ l β)` is the germ of `f : α → β` at `l : filter α`; this coercion is declared as `has_coe_t`, so it does not require an explicit up arrow `↑`; * coercion from `β` to `germ l β`: `(↑c : germ l β)` is the germ of the constant function `λ x:α, c` at a filter `l`; this coercion is declared as `has_lift_t`, so it requires an explicit up arrow `↑`, see [TPiL][TPiL_coe] for details. * `map (F : β → γ) (f : germ l β)` to be the composition of a function `F` and a germ `f`; * `map₂ (F : β → γ → δ) (f : germ l β) (g : germ l γ)` to be the germ of `λ x, F (f x) (g x)` at `l`; * `f.tendsto lb`: we say that a germ `f : germ l β` tends to a filter `lb` if its representatives tend to `lb` along `l`; * `f.comp_tendsto g hg` and `f.comp_tendsto' g hg`: given `f : germ l β` and a function `g : γ → α` (resp., a germ `g : germ lc α`), if `g` tends to `l` along `lc`, then the composition `f ∘ g` is a well-defined germ at `lc`; * `germ.lift_pred`, `germ.lift_rel`: lift a predicate or a relation to the space of germs: `(f : germ l β).lift_pred p` means `∀ᶠ x in l, p (f x)`, and similarly for a relation. [TPiL_coe]: https://leanprover.github.io/theorem_proving_in_lean/type_classes.html#coercions-using-type-classes We also define `map (F : β → γ) : germ l β → germ l γ` sending each germ `f` to `F ∘ f`. For each of the following structures we prove that if `β` has this structure, then so does `germ l β`: * one-operation algebraic structures up to `comm_group`; * `mul_zero_class`, `distrib`, `semiring`, `comm_semiring`, `ring`, `comm_ring`; * `mul_action`, `distrib_mul_action`, `module`; * `preorder`, `partial_order`, and `lattice` structures, as well as `bounded_order`; * `ordered_cancel_comm_monoid` and `ordered_cancel_add_comm_monoid`. ## Tags filter, germ -/ namespace filter variables {α β γ δ : Type*} {l : filter α} {f g h : α → β} lemma const_eventually_eq' [ne_bot l] {a b : β} : (∀ᶠ x in l, a = b) ↔ a = b := eventually_const lemma const_eventually_eq [ne_bot l] {a b : β} : ((λ _, a) =ᶠ[l] (λ _, b)) ↔ a = b := @const_eventually_eq' _ _ _ _ a b lemma eventually_eq.comp_tendsto {f' : α → β} (H : f =ᶠ[l] f') {g : γ → α} {lc : filter γ} (hg : tendsto g lc l) : f ∘ g =ᶠ[lc] f' ∘ g := hg.eventually H /-- Setoid used to define the space of germs. -/ def germ_setoid (l : filter α) (β : Type*) : setoid (α → β) := { r := eventually_eq l, iseqv := ⟨eventually_eq.refl _, λ _ _, eventually_eq.symm, λ _ _ _, eventually_eq.trans⟩ } /-- The space of germs of functions `α → β` at a filter `l`. -/ def germ (l : filter α) (β : Type*) : Type* := quotient (germ_setoid l β) /-- Setoid used to define the filter product. This is a dependent version of `filter.germ_setoid`. -/ def product_setoid (l : filter α) (ε : α → Type*) : setoid (Π a, ε a) := { r := λ f g, ∀ᶠ a in l, f a = g a, iseqv := ⟨λ _, eventually_of_forall (λ _, rfl), λ _ _ h, h.mono (λ _, eq.symm), λ x y z h1 h2, h1.congr (h2.mono (λ x hx, hx ▸ iff.rfl))⟩ } /-- The filter product `Π (a : α), ε a` at a filter `l`. This is a dependent version of `filter.germ`. -/ @[protected] def product (l : filter α) (ε : α → Type*) : Type* := quotient (product_setoid l ε) namespace product variables {ε : α → Type*} instance : has_coe_t (Π a, ε a) (l.product ε) := ⟨quotient.mk'⟩ instance [Π a, inhabited (ε a)] : inhabited (l.product ε) := ⟨(↑(λ a, (default : ε a)) : l.product ε)⟩ end product namespace germ instance : has_coe_t (α → β) (germ l β) := ⟨quotient.mk'⟩ instance : has_lift_t β (germ l β) := ⟨λ c, ↑(λ (x : α), c)⟩ @[simp] lemma quot_mk_eq_coe (l : filter α) (f : α → β) : quot.mk _ f = (f : germ l β) := rfl @[simp] lemma mk'_eq_coe (l : filter α) (f : α → β) : quotient.mk' f = (f : germ l β) := rfl @[elab_as_eliminator] lemma induction_on (f : germ l β) {p : germ l β → Prop} (h : ∀ f : α → β, p f) : p f := quotient.induction_on' f h @[elab_as_eliminator] lemma induction_on₂ (f : germ l β) (g : germ l γ) {p : germ l β → germ l γ → Prop} (h : ∀ (f : α → β) (g : α → γ), p f g) : p f g := quotient.induction_on₂' f g h @[elab_as_eliminator] lemma induction_on₃ (f : germ l β) (g : germ l γ) (h : germ l δ) {p : germ l β → germ l γ → germ l δ → Prop} (H : ∀ (f : α → β) (g : α → γ) (h : α → δ), p f g h) : p f g h := quotient.induction_on₃' f g h H /-- Given a map `F : (α → β) → (γ → δ)` that sends functions eventually equal at `l` to functions eventually equal at `lc`, returns a map from `germ l β` to `germ lc δ`. -/ def map' {lc : filter γ} (F : (α → β) → (γ → δ)) (hF : (l.eventually_eq ⇒ lc.eventually_eq) F F) : germ l β → germ lc δ := quotient.map' F hF /-- Given a germ `f : germ l β` and a function `F : (α → β) → γ` sending eventually equal functions to the same value, returns the value `F` takes on functions having germ `f` at `l`. -/ def lift_on {γ : Sort*} (f : germ l β) (F : (α → β) → γ) (hF : (l.eventually_eq ⇒ (=)) F F) : γ := quotient.lift_on' f F hF @[simp] lemma map'_coe {lc : filter γ} (F : (α → β) → (γ → δ)) (hF : (l.eventually_eq ⇒ lc.eventually_eq) F F) (f : α → β) : map' F hF f = F f := rfl @[simp, norm_cast] lemma coe_eq : (f : germ l β) = g ↔ (f =ᶠ[l] g) := quotient.eq' alias coe_eq ↔ _ _root_.filter.eventually_eq.germ_eq /-- Lift a function `β → γ` to a function `germ l β → germ l γ`. -/ def map (op : β → γ) : germ l β → germ l γ := map' ((∘) op) $ λ f g H, H.mono $ λ x H, congr_arg op H @[simp] lemma map_coe (op : β → γ) (f : α → β) : map op (f : germ l β) = op ∘ f := rfl @[simp] lemma map_id : map id = (id : germ l β → germ l β) := by { ext ⟨f⟩, refl } lemma map_map (op₁ : γ → δ) (op₂ : β → γ) (f : germ l β) : map op₁ (map op₂ f) = map (op₁ ∘ op₂) f := induction_on f $ λ f, rfl /-- Lift a binary function `β → γ → δ` to a function `germ l β → germ l γ → germ l δ`. -/ def map₂ (op : β → γ → δ) : germ l β → germ l γ → germ l δ := quotient.map₂' (λ f g x, op (f x) (g x)) $ λ f f' Hf g g' Hg, Hg.mp $ Hf.mono $ λ x Hf Hg, by simp only [Hf, Hg] @[simp] lemma map₂_coe (op : β → γ → δ) (f : α → β) (g : α → γ) : map₂ op (f : germ l β) g = λ x, op (f x) (g x) := rfl /-- A germ at `l` of maps from `α` to `β` tends to `lb : filter β` if it is represented by a map which tends to `lb` along `l`. -/ protected def tendsto (f : germ l β) (lb : filter β) : Prop := lift_on f (λ f, tendsto f l lb) $ λ f g H, propext (tendsto_congr' H) @[simp, norm_cast] lemma coe_tendsto {f : α → β} {lb : filter β} : (f : germ l β).tendsto lb ↔ tendsto f l lb := iff.rfl alias coe_tendsto ↔ _ _root_.filter.tendsto.germ_tendsto /-- Given two germs `f : germ l β`, and `g : germ lc α`, where `l : filter α`, if `g` tends to `l`, then the composition `f ∘ g` is well-defined as a germ at `lc`. -/ def comp_tendsto' (f : germ l β) {lc : filter γ} (g : germ lc α) (hg : g.tendsto l) : germ lc β := lift_on f (λ f, g.map f) $ λ f₁ f₂ hF, (induction_on g $ λ g hg, coe_eq.2 $ hg.eventually hF) hg @[simp] lemma coe_comp_tendsto' (f : α → β) {lc : filter γ} {g : germ lc α} (hg : g.tendsto l) : (f : germ l β).comp_tendsto' g hg = g.map f := rfl /-- Given a germ `f : germ l β` and a function `g : γ → α`, where `l : filter α`, if `g` tends to `l` along `lc : filter γ`, then the composition `f ∘ g` is well-defined as a germ at `lc`. -/ def comp_tendsto (f : germ l β) {lc : filter γ} (g : γ → α) (hg : tendsto g lc l) : germ lc β := f.comp_tendsto' _ hg.germ_tendsto @[simp] lemma coe_comp_tendsto (f : α → β) {lc : filter γ} {g : γ → α} (hg : tendsto g lc l) : (f : germ l β).comp_tendsto g hg = f ∘ g := rfl @[simp] lemma comp_tendsto'_coe (f : germ l β) {lc : filter γ} {g : γ → α} (hg : tendsto g lc l) : f.comp_tendsto' _ hg.germ_tendsto = f.comp_tendsto g hg := rfl @[simp, norm_cast] lemma const_inj [ne_bot l] {a b : β} : (↑a : germ l β) = ↑b ↔ a = b := coe_eq.trans $ const_eventually_eq @[simp] lemma map_const (l : filter α) (a : β) (f : β → γ) : (↑a : germ l β).map f = ↑(f a) := rfl @[simp] lemma map₂_const (l : filter α) (b : β) (c : γ) (f : β → γ → δ) : map₂ f (↑b : germ l β) ↑c = ↑(f b c) := rfl @[simp] lemma const_comp_tendsto {l : filter α} (b : β) {lc : filter γ} {g : γ → α} (hg : tendsto g lc l) : (↑b : germ l β).comp_tendsto g hg = ↑b := rfl @[simp] lemma const_comp_tendsto' {l : filter α} (b : β) {lc : filter γ} {g : germ lc α} (hg : g.tendsto l) : (↑b : germ l β).comp_tendsto' g hg = ↑b := induction_on g (λ _ _, rfl) hg /-- Lift a predicate on `β` to `germ l β`. -/ def lift_pred (p : β → Prop) (f : germ l β) : Prop := lift_on f (λ f, ∀ᶠ x in l, p (f x)) $ λ f g H, propext $ eventually_congr $ H.mono $ λ x hx, hx ▸ iff.rfl @[simp] lemma lift_pred_coe {p : β → Prop} {f : α → β} : lift_pred p (f : germ l β) ↔ ∀ᶠ x in l, p (f x) := iff.rfl lemma lift_pred_const {p : β → Prop} {x : β} (hx : p x) : lift_pred p (↑x : germ l β) := eventually_of_forall $ λ y, hx @[simp] lemma lift_pred_const_iff [ne_bot l] {p : β → Prop} {x : β} : lift_pred p (↑x : germ l β) ↔ p x := @eventually_const _ _ _ (p x) /-- Lift a relation `r : β → γ → Prop` to `germ l β → germ l γ → Prop`. -/ def lift_rel (r : β → γ → Prop) (f : germ l β) (g : germ l γ) : Prop := quotient.lift_on₂' f g (λ f g, ∀ᶠ x in l, r (f x) (g x)) $ λ f g f' g' Hf Hg, propext $ eventually_congr $ Hg.mp $ Hf.mono $ λ x hf hg, hf ▸ hg ▸ iff.rfl @[simp] lemma lift_rel_coe {r : β → γ → Prop} {f : α → β} {g : α → γ} : lift_rel r (f : germ l β) g ↔ ∀ᶠ x in l, r (f x) (g x) := iff.rfl lemma lift_rel_const {r : β → γ → Prop} {x : β} {y : γ} (h : r x y) : lift_rel r (↑x : germ l β) ↑y := eventually_of_forall $ λ _, h @[simp] lemma lift_rel_const_iff [ne_bot l] {r : β → γ → Prop} {x : β} {y : γ} : lift_rel r (↑x : germ l β) ↑y ↔ r x y := @eventually_const _ _ _ (r x y) instance [inhabited β] : inhabited (germ l β) := ⟨↑(default : β)⟩ section monoid variables {M : Type*} {G : Type*} @[to_additive] instance [has_mul M] : has_mul (germ l M) := ⟨map₂ (*)⟩ @[simp, norm_cast, to_additive] lemma coe_mul [has_mul M] (f g : α → M) : ↑(f * g) = (f * g : germ l M) := rfl @[to_additive] instance [has_one M] : has_one (germ l M) := ⟨↑(1:M)⟩ @[simp, norm_cast, to_additive] lemma coe_one [has_one M] : ↑(1 : α → M) = (1 : germ l M) := rfl @[to_additive] instance [semigroup M] : semigroup (germ l M) := function.surjective.semigroup coe (surjective_quot_mk _) (λ a b, coe_mul a b) @[to_additive] instance [comm_semigroup M] : comm_semigroup (germ l M) := function.surjective.comm_semigroup coe (surjective_quot_mk _) (λ a b, coe_mul a b) @[to_additive add_left_cancel_semigroup] instance [left_cancel_semigroup M] : left_cancel_semigroup (germ l M) := { mul := (*), mul_left_cancel := λ f₁ f₂ f₃, induction_on₃ f₁ f₂ f₃ $ λ f₁ f₂ f₃ H, coe_eq.2 ((coe_eq.1 H).mono $ λ x, mul_left_cancel), .. germ.semigroup } @[to_additive add_right_cancel_semigroup] instance [right_cancel_semigroup M] : right_cancel_semigroup (germ l M) := { mul := (*), mul_right_cancel := λ f₁ f₂ f₃, induction_on₃ f₁ f₂ f₃ $ λ f₁ f₂ f₃ H, coe_eq.2 $ (coe_eq.1 H).mono $ λ x, mul_right_cancel, .. germ.semigroup } instance has_nat_pow [monoid G] : has_pow (germ l G) ℕ := ⟨λ f n, map (^ n) f⟩ @[simp] lemma coe_pow [monoid G] (f : α → G) (n : ℕ) : ↑(f ^ n) = (f ^ n : germ l G) := rfl instance has_int_pow [div_inv_monoid G] : has_pow (germ l G) ℤ := ⟨λ f z, map (^ z) f⟩ @[simp] lemma coe_zpow [div_inv_monoid G] (f : α → G) (z : ℤ) : ↑(f ^ z) = (f ^ z : germ l G) := rfl instance [has_smul M β] : has_smul M (germ l β) := ⟨λ c, map ((•) c)⟩ @[simp, norm_cast] lemma coe_smul [has_smul M β] (c : M) (f : α → β) : ↑(c • f) = (c • f : germ l β) := rfl instance [add_monoid M] : add_monoid (germ l M) := function.surjective.add_monoid coe (surjective_quot_mk _) rfl (λ a b, coe_add a b) (λ _ _, rfl) @[to_additive] instance [monoid M] : monoid (germ l M) := function.surjective.monoid coe (surjective_quot_mk _) rfl (λ a b, coe_mul a b) coe_pow /-- coercion from functions to germs as a monoid homomorphism. -/ @[to_additive] def coe_mul_hom [monoid M] (l : filter α) : (α → M) →* germ l M := ⟨coe, rfl, λ f g, rfl⟩ /-- coercion from functions to germs as an additive monoid homomorphism. -/ add_decl_doc coe_add_hom @[simp, to_additive] lemma coe_coe_mul_hom [monoid M] : (coe_mul_hom l : (α → M) → germ l M) = coe := rfl @[to_additive] instance [comm_monoid M] : comm_monoid (germ l M) := { mul := (*), one := 1, .. germ.comm_semigroup, .. germ.monoid } instance [add_monoid_with_one M] : add_monoid_with_one (germ l M) := { nat_cast := λ n, ↑(n : M), nat_cast_zero := congr_arg coe nat.cast_zero, nat_cast_succ := λ n, congr_arg coe (nat.cast_succ _), .. germ.has_one, .. germ.add_monoid } @[to_additive] instance [has_inv G] : has_inv (germ l G) := ⟨map has_inv.inv⟩ @[simp, norm_cast, to_additive] lemma coe_inv [has_inv G] (f : α → G) : ↑f⁻¹ = (f⁻¹ : germ l G) := rfl @[to_additive] instance [has_div M] : has_div (germ l M) := ⟨map₂ (/)⟩ @[simp, norm_cast, to_additive] lemma coe_div [has_div M] (f g : α → M) : ↑(f / g) = (f / g : germ l M) := rfl instance [sub_neg_monoid G] : sub_neg_monoid (germ l G) := function.surjective.sub_neg_monoid coe (surjective_quot_mk _) rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive sub_neg_monoid] instance [div_inv_monoid G] : div_inv_monoid (germ l G) := function.surjective.div_inv_monoid coe (surjective_quot_mk _) rfl (λ _ _, rfl) (λ _, rfl) (λ _ _, rfl) (λ _ _, rfl) (λ _ _, rfl) @[to_additive] instance [group G] : group (germ l G) := { mul := (*), one := 1, mul_left_inv := by { rintros ⟨f⟩, exact congr_arg (quot.mk _) (mul_left_inv f) }, .. germ.div_inv_monoid } @[to_additive] instance [comm_group G] : comm_group (germ l G) := { mul := (*), one := 1, inv := has_inv.inv, .. germ.group, .. germ.comm_monoid } end monoid section ring variables {R : Type*} instance nontrivial [nontrivial R] [ne_bot l] : nontrivial (germ l R) := let ⟨x, y, h⟩ := exists_pair_ne R in ⟨⟨↑x, ↑y, mt const_inj.1 h⟩⟩ instance [mul_zero_class R] : mul_zero_class (germ l R) := { zero := 0, mul := (*), mul_zero := λ f, induction_on f $ λ f, by { norm_cast, rw [mul_zero] }, zero_mul := λ f, induction_on f $ λ f, by { norm_cast, rw [zero_mul] } } instance [distrib R] : distrib (germ l R) := { mul := (*), add := (+), left_distrib := λ f g h, induction_on₃ f g h $ λ f g h, by { norm_cast, rw [left_distrib] }, right_distrib := λ f g h, induction_on₃ f g h $ λ f g h, by { norm_cast, rw [right_distrib] } } instance [semiring R] : semiring (germ l R) := { .. germ.add_comm_monoid, .. germ.monoid, .. germ.distrib, .. germ.mul_zero_class, .. germ.add_monoid_with_one } /-- Coercion `(α → R) → germ l R` as a `ring_hom`. -/ def coe_ring_hom [semiring R] (l : filter α) : (α → R) →+* germ l R := { to_fun := coe, .. (coe_mul_hom l : _ →* germ l R), .. (coe_add_hom l : _ →+ germ l R) } @[simp] lemma coe_coe_ring_hom [semiring R] : (coe_ring_hom l : (α → R) → germ l R) = coe := rfl instance [ring R] : ring (germ l R) := { .. germ.add_comm_group, .. germ.semiring } instance [comm_semiring R] : comm_semiring (germ l R) := { .. germ.semiring, .. germ.comm_monoid } instance [comm_ring R] : comm_ring (germ l R) := { .. germ.ring, .. germ.comm_monoid } end ring section module variables {M N R : Type*} instance has_smul' [has_smul M β] : has_smul (germ l M) (germ l β) := ⟨map₂ (•)⟩ @[simp, norm_cast] lemma coe_smul' [has_smul M β] (c : α → M) (f : α → β) : ↑(c • f) = (c : germ l M) • (f : germ l β) := rfl instance [monoid M] [mul_action M β] : mul_action M (germ l β) := { one_smul := λ f, induction_on f $ λ f, by { norm_cast, simp only [one_smul] }, mul_smul := λ c₁ c₂ f, induction_on f $ λ f, by { norm_cast, simp only [mul_smul] } } instance mul_action' [monoid M] [mul_action M β] : mul_action (germ l M) (germ l β) := { one_smul := λ f, induction_on f $ λ f, by simp only [← coe_one, ← coe_smul', one_smul], mul_smul := λ c₁ c₂ f, induction_on₃ c₁ c₂ f $ λ c₁ c₂ f, by { norm_cast, simp only [mul_smul] } } instance [monoid M] [add_monoid N] [distrib_mul_action M N] : distrib_mul_action M (germ l N) := { smul_add := λ c f g, induction_on₂ f g $ λ f g, by { norm_cast, simp only [smul_add] }, smul_zero := λ c, by simp only [← coe_zero, ← coe_smul, smul_zero] } instance distrib_mul_action' [monoid M] [add_monoid N] [distrib_mul_action M N] : distrib_mul_action (germ l M) (germ l N) := { smul_add := λ c f g, induction_on₃ c f g $ λ c f g, by { norm_cast, simp only [smul_add] }, smul_zero := λ c, induction_on c $ λ c, by simp only [← coe_zero, ← coe_smul', smul_zero] } instance [semiring R] [add_comm_monoid M] [module R M] : module R (germ l M) := { add_smul := λ c₁ c₂ f, induction_on f $ λ f, by { norm_cast, simp only [add_smul] }, zero_smul := λ f, induction_on f $ λ f, by { norm_cast, simp only [zero_smul, coe_zero] } } instance module' [semiring R] [add_comm_monoid M] [module R M] : module (germ l R) (germ l M) := { add_smul := λ c₁ c₂ f, induction_on₃ c₁ c₂ f $ λ c₁ c₂ f, by { norm_cast, simp only [add_smul] }, zero_smul := λ f, induction_on f $ λ f, by simp only [← coe_zero, ← coe_smul', zero_smul] } end module instance [has_le β] : has_le (germ l β) := ⟨lift_rel (≤)⟩ @[simp] lemma coe_le [has_le β] : (f : germ l β) ≤ g ↔ (f ≤ᶠ[l] g) := iff.rfl lemma le_def [has_le β] : ((≤) : germ l β → germ l β → Prop) = lift_rel (≤) := rfl lemma const_le [has_le β] {x y : β} (h : x ≤ y) : (↑x : germ l β) ≤ ↑y := lift_rel_const h @[simp, norm_cast] lemma const_le_iff [has_le β] [ne_bot l] {x y : β} : (↑x : germ l β) ≤ ↑y ↔ x ≤ y := lift_rel_const_iff instance [preorder β] : preorder (germ l β) := { le := (≤), le_refl := λ f, induction_on f $ eventually_le.refl l, le_trans := λ f₁ f₂ f₃, induction_on₃ f₁ f₂ f₃ $ λ f₁ f₂ f₃, eventually_le.trans } instance [partial_order β] : partial_order (germ l β) := { le := (≤), le_antisymm := λ f g, induction_on₂ f g $ λ f g h₁ h₂, (eventually_le.antisymm h₁ h₂).germ_eq, .. germ.preorder } instance [has_bot β] : has_bot (germ l β) := ⟨↑(⊥:β)⟩ @[simp, norm_cast] lemma const_bot [has_bot β] : (↑(⊥:β) : germ l β) = ⊥ := rfl instance [has_le β] [order_bot β] : order_bot (germ l β) := { bot := ⊥, bot_le := λ f, induction_on f $ λ f, eventually_of_forall $ λ x, bot_le } instance [has_top β] : has_top (germ l β) := ⟨↑(⊤:β)⟩ @[simp, norm_cast] lemma const_top [has_top β] : (↑(⊤:β) : germ l β) = ⊤ := rfl instance [has_le β] [order_top β] : order_top (germ l β) := { top := ⊤, le_top := λ f, induction_on f $ λ f, eventually_of_forall $ λ x, le_top } instance [has_sup β] : has_sup (germ l β) := ⟨map₂ (⊔)⟩ @[simp, norm_cast] lemma const_sup [has_sup β] (a b : β) : ↑(a ⊔ b) = (↑a ⊔ ↑b : germ l β) := rfl instance [has_inf β] : has_inf (germ l β) := ⟨map₂ (⊓)⟩ @[simp, norm_cast] lemma const_inf [has_inf β] (a b : β) : ↑(a ⊓ b) = (↑a ⊓ ↑b : germ l β) := rfl instance [semilattice_sup β] : semilattice_sup (germ l β) := { sup := (⊔), le_sup_left := λ f g, induction_on₂ f g $ λ f g, eventually_of_forall $ λ x, le_sup_left, le_sup_right := λ f g, induction_on₂ f g $ λ f g, eventually_of_forall $ λ x, le_sup_right, sup_le := λ f₁ f₂ g, induction_on₃ f₁ f₂ g $ λ f₁ f₂ g h₁ h₂, h₂.mp $ h₁.mono $ λ x, sup_le, .. germ.partial_order } instance [semilattice_inf β] : semilattice_inf (germ l β) := { inf := (⊓), inf_le_left := λ f g, induction_on₂ f g $ λ f g, eventually_of_forall $ λ x, inf_le_left, inf_le_right := λ f g, induction_on₂ f g $ λ f g, eventually_of_forall $ λ x, inf_le_right, le_inf := λ f₁ f₂ g, induction_on₃ f₁ f₂ g $ λ f₁ f₂ g h₁ h₂, h₂.mp $ h₁.mono $ λ x, le_inf, .. germ.partial_order } instance [lattice β] : lattice (germ l β) := { .. germ.semilattice_sup, .. germ.semilattice_inf } instance [has_le β] [bounded_order β] : bounded_order (germ l β) := { .. germ.order_bot, .. germ.order_top } @[to_additive] instance [ordered_cancel_comm_monoid β] : ordered_cancel_comm_monoid (germ l β) := { mul_le_mul_left := λ f g, induction_on₂ f g $ λ f g H h, induction_on h $ λ h, H.mono $ λ x H, mul_le_mul_left' H _, le_of_mul_le_mul_left := λ f g h, induction_on₃ f g h $ λ f g h H, H.mono $ λ x, le_of_mul_le_mul_left', .. germ.partial_order, .. germ.comm_monoid, .. germ.left_cancel_semigroup } @[to_additive] instance ordered_comm_group [ordered_comm_group β] : ordered_comm_group (germ l β) := { mul_le_mul_left := λ f g, induction_on₂ f g $ λ f g H h, induction_on h $ λ h, H.mono $ λ x H, mul_le_mul_left' H _, .. germ.partial_order, .. germ.comm_group } end germ end filter
1b04f8e6114f624a4e05dc5814c5f6fcb0c183f2
ebf7140a9ea507409ff4c994124fa36e79b4ae35
/src/solutions/tuesday/logic.lean
f8e25f442fdd71033fa1b13da42ad1d328145206
[]
no_license
fundou/lftcm2020
3e88d58a92755ea5dd49f19c36239c35286ecf5e
99d11bf3bcd71ffeaef0250caa08ecc46e69b55b
refs/heads/master
1,685,610,799,304
1,624,070,416,000
1,624,070,416,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
6,431
lean
/- Lean for the Curious Mathematician Tuesday, July 14, 2020 13:00 session Happy Bastille Day! -/ import data.real.basic /-! ## Logic -/ /-! ### Agenda - a quick overview of Lean's tactics for dealing with logical connectives - continue with Patrick Massot's tutorial After the overview, I will ask you to tell us how far you have gotten in the tutorial (by file number), so we can group you in rooms accordingly. (Tutors, please self identify as well!) An apology: In this presentation, I won't tell you anything you don't already know. For example, I will say: "To prove 'A and B', is suffices to prove A and then to prove B." "If you know 'A and B', you can use A and you can use B." The point is, to communicate these intentions to Lean, we have to give names to these actions. Analogies: - Learning grammar. - Learning Latex. Other resources: - Everything I go over here is in the tutorial. - It is also in *Mathematics in Lean*. - *Theorem Proving in Lean* has a more foundational, logician's perspective. - See also the cheat sheet: https://leanprover-community.github.io//img/lean-tactics.pdf You can find links to all of these (including the cheat sheet) on the mathlib web pages, under "Learning resources." This file is in the LFTCM repository. Most of the examples here are from *Mathematics in Lean*. -/ /-! ### Overview The list: → \to, \r if ... then implication ∀ \all for all universal quantifier ∃ \ex there exists existential quantifier ¬ \not, \n not negation ∧ \and and conjunction ↔ \iff, \lr if and only if bi-implication ∨ \or or disjunction false contradiction falsity true this is trivial! truth Remember that a goal in Lean is of the form 1 goal x y : ℕ, h₁ : prime x, h₂ : ¬even x, h₃ : y > x ⊢ y ≥ 4 The stuff before the `⊢` is called the *context*, or *local context*. The facts there are called *hypotheses* or *local hypotheses*. The stuff after the `⊢` is also called the *goal*, or the *target*. A common theme: - Some tactics tell us how to *prove* a goal involving the connective. (Logician's terminology: "introduce" the connective.) - Some tactics tell us how to *use* a hypothesis involving the connective. (Logician's terminology: "eliminate" the connective.) Summary: → if ... then `intro`, `intros` `apply`, `have h₃ := h₁ h₂` ∀ for all `intro`, `intros` `apply`, `specialize`, `have h₂ := h₁ t` ∃ there exists `use` `cases` ¬ not `intro`, `intros` `apply`, `contradiction` ∧ and `split` `cases`, `h.1` / `h.2`, `h.left` / `h.right` ↔ if and only if `split` `cases`, `h.1` / `h.2`, `h.mp` / `h.mpr`, `rw` ∨ or `left` / `right` `cases` false contradiction `contradiction`, `ex_falso` true this is trivial! `trivial` Also, for proof by contradiction: Use `open_locale classical` (but not right after the `import`s.) Use the `by_contradiction` tactic. There are lots of other tactics and methods. But these are all you need to deal with the logical connectives. Another theme: sometimes the logical structure is hidden under a definition. For example: `x ∣ y` is existential `s ⊆ t` is universal Lean will unfold definitions as needed. -/ /-! ### Implication and the universal quantifier -/ section variables a b c d : ℝ variable h₁ : a ≤ b variables h₂ : c ≤ d #check @add_le_add #check @add_le_add _ a b #check @add_le_add _ a b c d _ _ _ _ h₁ #check add_le_add h₁ include h₁ h₂ -- demonstrate variations on `apply` and `have` example : a + c ≤ b + d := -- sorry begin have h := add_le_add h₁, apply h, apply h₂ end -- sorry end section def fn_ub (f : ℝ → ℝ) (a : ℝ) : Prop := ∀ x, f x ≤ a variables {f g : ℝ → ℝ} {a b : ℝ} -- demonstrate variations on `apply`, `have`, and `specialize` -- `dsimp` helps clarify the goal theorem fn_ub_add (hfa : fn_ub f a) (hgb : fn_ub g b) : fn_ub (λ x, f x + g x) (a + b) := -- sorry begin intro x, dsimp, specialize hfa x, have hgb' := hgb x, apply add_le_add hfa hgb' end -- sorry end /-! ### The existential quantifier -/ -- demonstrate `use` and `norm_num` example : ∃ x : ℝ, 2 < x ∧ x < 3 := -- sorry begin use 5 / 2, norm_num end -- sorry -- demonstrate `cases` and `use`, and use `fn_ub_add` section def fn_has_ub (f : ℝ → ℝ) := ∃ a, fn_ub f a variables {f g : ℝ → ℝ} example (ubf : fn_has_ub f) (ubg : fn_has_ub g) : fn_has_ub (λ x, f x + g x) := -- sorry begin cases ubf with a ha, cases ubg with b hb, use a + b, apply fn_ub_add ha hb end -- sorry end /-! ### Negation -/ section variable {f : ℝ → ℝ} example (h : ∀ a, ∃ x, f x > a) : ¬ fn_has_ub f := -- sorry begin intro h', cases h' with a ha, specialize h a, cases h with x hx, specialize ha x, linarith end -- sorry end /-! ### Conjunction -/ section variables {x y : ℝ} -- demonstrate `split` example (h₀ : x ≤ y) (h₁ : ¬ y ≤ x) : x ≤ y ∧ x ≠ y := -- sorry begin split, { assumption }, intro h, apply h₁, rw h end -- sorry -- demonstrate `cases`, `h.1`, `h.2` example (h : x ≤ y ∧ x ≠ y) : ¬ y ≤ x := -- sorry begin cases h with h₀ h₁, intro h₃, apply h₁, apply le_antisymm h₀ h₃ end -- sorry end /-! ### Disjunction -/ section variables x y : ℝ #check le_or_gt 0 y #check @abs_of_nonneg #check @abs_of_neg example : x < abs y → x < y ∨ x < -y := -- sorry begin cases le_or_gt 0 y with ynonneg yneg, { rw abs_of_nonneg ynonneg, intro h, left, apply h }, rw abs_of_neg yneg, intro h, right, apply h end -- sorry end /-! ### Final note I haven't done everything! In particular, I skipped bi-implication, `true`, `false`, proof by contradiction, and more. But this should get you started, and you can consult the resources for more. -/
ec9233799092ed60e3d6a98aeeea1c5c61d65788
9dc8cecdf3c4634764a18254e94d43da07142918
/src/ring_theory/polynomial/pochhammer.lean
7f36bd5948bbc6f8052485087b3c07bfb28b92cd
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
6,781
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import tactic.abel import data.polynomial.eval /-! # The Pochhammer polynomials We define and prove some basic relations about `pochhammer S n : S[X] := X * (X + 1) * ... * (X + n - 1)` which is also known as the rising factorial. A version of this definition that is focused on `nat` can be found in `data.nat.factorial` as `nat.asc_factorial`. ## Implementation As with many other families of polynomials, even though the coefficients are always in `ℕ`, we define the polynomial with coefficients in any `[semiring S]`. ## TODO There is lots more in this direction: * q-factorials, q-binomials, q-Pochhammer. -/ universes u v open polynomial open_locale polynomial section semiring variables (S : Type u) [semiring S] /-- `pochhammer S n` is the polynomial `X * (X+1) * ... * (X + n - 1)`, with coefficients in the semiring `S`. -/ noncomputable def pochhammer : ℕ → S[X] | 0 := 1 | (n+1) := X * (pochhammer n).comp (X + 1) @[simp] lemma pochhammer_zero : pochhammer S 0 = 1 := rfl @[simp] lemma pochhammer_one : pochhammer S 1 = X := by simp [pochhammer] lemma pochhammer_succ_left (n : ℕ) : pochhammer S (n+1) = X * (pochhammer S n).comp (X+1) := by rw pochhammer section variables {S} {T : Type v} [semiring T] @[simp] lemma pochhammer_map (f : S →+* T) (n : ℕ) : (pochhammer S n).map f = pochhammer T n := begin induction n with n ih, { simp, }, { simp [ih, pochhammer_succ_left, map_comp], }, end end @[simp, norm_cast] lemma pochhammer_eval_cast (n k : ℕ) : ((pochhammer ℕ n).eval k : S) = (pochhammer S n).eval k := begin rw [←pochhammer_map (algebra_map ℕ S), eval_map, ←eq_nat_cast (algebra_map ℕ S), eval₂_at_nat_cast, nat.cast_id, eq_nat_cast], end lemma pochhammer_eval_zero {n : ℕ} : (pochhammer S n).eval 0 = if n = 0 then 1 else 0 := begin cases n, { simp, }, { simp [X_mul, nat.succ_ne_zero, pochhammer_succ_left], } end lemma pochhammer_zero_eval_zero : (pochhammer S 0).eval 0 = 1 := by simp @[simp] lemma pochhammer_ne_zero_eval_zero {n : ℕ} (h : n ≠ 0) : (pochhammer S n).eval 0 = 0 := by simp [pochhammer_eval_zero, h] lemma pochhammer_succ_right (n : ℕ) : pochhammer S (n+1) = pochhammer S n * (X + n) := begin suffices h : pochhammer ℕ (n+1) = pochhammer ℕ n * (X + n), { apply_fun polynomial.map (algebra_map ℕ S) at h, simpa only [pochhammer_map, polynomial.map_mul, polynomial.map_add, map_X, polynomial.map_nat_cast] using h }, induction n with n ih, { simp, }, { conv_lhs { rw [pochhammer_succ_left, ih, mul_comp, ←mul_assoc, ←pochhammer_succ_left, add_comp, X_comp, nat_cast_comp, add_assoc, add_comm (1 : ℕ[X]), ← nat.cast_succ] } }, end lemma pochhammer_succ_eval {S : Type*} [semiring S] (n : ℕ) (k : S) : (pochhammer S (n + 1)).eval k = (pochhammer S n).eval k * (k + n) := by rw [pochhammer_succ_right, mul_add, eval_add, eval_mul_X, ← nat.cast_comm, ← C_eq_nat_cast, eval_C_mul, nat.cast_comm, ← mul_add] lemma pochhammer_succ_comp_X_add_one (n : ℕ) : (pochhammer S (n + 1)).comp (X + 1) = pochhammer S (n + 1) + (n + 1) • (pochhammer S n).comp (X + 1) := begin suffices : (pochhammer ℕ (n + 1)).comp (X + 1) = pochhammer ℕ (n + 1) + (n + 1) * (pochhammer ℕ n).comp (X + 1), { simpa [map_comp] using congr_arg (polynomial.map (nat.cast_ring_hom S)) this }, nth_rewrite 1 pochhammer_succ_left, rw [← add_mul, pochhammer_succ_right ℕ n, mul_comp, mul_comm, add_comp, X_comp, nat_cast_comp, add_comm ↑n, ← add_assoc] end lemma polynomial.mul_X_add_nat_cast_comp {p q : S[X]} {n : ℕ} : (p * (X + n)).comp q = (p.comp q) * (q + n) := by rw [mul_add, add_comp, mul_X_comp, ←nat.cast_comm, nat_cast_mul_comp, nat.cast_comm, mul_add] lemma pochhammer_mul (n m : ℕ) : pochhammer S n * (pochhammer S m).comp (X + n) = pochhammer S (n + m) := begin induction m with m ih, { simp, }, { rw [pochhammer_succ_right, polynomial.mul_X_add_nat_cast_comp, ←mul_assoc, ih, nat.succ_eq_add_one, ←add_assoc, pochhammer_succ_right, nat.cast_add, add_assoc], } end lemma pochhammer_nat_eq_asc_factorial (n : ℕ) : ∀ k, (pochhammer ℕ k).eval (n + 1) = n.asc_factorial k | 0 := by erw [eval_one]; refl | (t + 1) := begin rw [pochhammer_succ_right, eval_mul, pochhammer_nat_eq_asc_factorial t], suffices : n.asc_factorial t * (n + 1 + t) = n.asc_factorial (t + 1), by simpa, rw [nat.asc_factorial_succ, add_right_comm, mul_comm] end lemma pochhammer_nat_eq_desc_factorial (a b : ℕ) : (pochhammer ℕ b).eval a = (a + b - 1).desc_factorial b := begin cases b, { rw [nat.desc_factorial_zero, pochhammer_zero, polynomial.eval_one] }, rw [nat.add_succ, nat.succ_sub_succ, tsub_zero], cases a, { rw [pochhammer_ne_zero_eval_zero _ b.succ_ne_zero, zero_add, nat.desc_factorial_of_lt b.lt_succ_self] }, { rw [nat.succ_add, ←nat.add_succ, nat.add_desc_factorial_eq_asc_factorial, pochhammer_nat_eq_asc_factorial] } end end semiring section ordered_semiring variables {S : Type*} [ordered_semiring S] [nontrivial S] lemma pochhammer_pos (n : ℕ) (s : S) (h : 0 < s) : 0 < (pochhammer S n).eval s := begin induction n with n ih, { simp only [nat.nat_zero_eq_zero, pochhammer_zero, eval_one], exact zero_lt_one, }, { rw [pochhammer_succ_right, mul_add, eval_add, ←nat.cast_comm, eval_nat_cast_mul, eval_mul_X, nat.cast_comm, ←mul_add], exact mul_pos ih (lt_of_lt_of_le h ((le_add_iff_nonneg_right _).mpr (nat.cast_nonneg n))), } end end ordered_semiring section factorial open_locale nat variables (S : Type*) [semiring S] (r n : ℕ) @[simp] lemma pochhammer_eval_one (S : Type*) [semiring S] (n : ℕ) : (pochhammer S n).eval (1 : S) = (n! : S) := by rw_mod_cast [pochhammer_nat_eq_asc_factorial, nat.zero_asc_factorial] lemma factorial_mul_pochhammer (S : Type*) [semiring S] (r n : ℕ) : (r! : S) * (pochhammer S n).eval (r + 1) = (r + n)! := by rw_mod_cast [pochhammer_nat_eq_asc_factorial, nat.factorial_mul_asc_factorial] lemma pochhammer_nat_eval_succ (r : ℕ) : ∀ n : ℕ, n * (pochhammer ℕ r).eval (n + 1) = (n + r) * (pochhammer ℕ r).eval n | 0 := begin by_cases h : r = 0, { simp only [h, zero_mul, zero_add], }, { simp only [pochhammer_eval_zero, zero_mul, if_neg h, mul_zero], } end | (k + 1) := by simp only [pochhammer_nat_eq_asc_factorial, nat.succ_asc_factorial, add_right_comm] lemma pochhammer_eval_succ (r n : ℕ) : (n : S) * (pochhammer S r).eval (n + 1 : S) = (n + r) * (pochhammer S r).eval n := by exact_mod_cast congr_arg nat.cast (pochhammer_nat_eval_succ r n) end factorial
04648fd07d2058525f6643727c40ef2b0304d60b
ce6917c5bacabee346655160b74a307b4a5ab620
/src/ch4/ex0406.lean
0b913b3d5ce741973456f09779f8606e21de684e
[]
no_license
Ailrun/Theorem_Proving_in_Lean
ae6a23f3c54d62d401314d6a771e8ff8b4132db2
2eb1b5caf93c6a5a555c79e9097cf2ba5a66cf68
refs/heads/master
1,609,838,270,467
1,586,846,743,000
1,586,846,743,000
240,967,761
1
0
null
null
null
null
UTF-8
Lean
false
false
164
lean
variables (α : Type) (p q : α → Prop) example (h : ∃ x, p x ∧ q x) : ∃ x, q x ∧ p x := match h with ⟨w, hpw, hqw⟩ := ⟨w, hqw, hpw⟩ end
e8799b170b60741ffb14c7c9f995158fae9a1b0a
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/playground/task_test3.lean
b23d6875732efc4ef7253f851124fa6c033bbf9b
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
455
lean
def run1 (i : Nat) (n : Nat) (xs : List Nat) : Nat := n.repeat (fun r => -- dbgTrace (">> [" ++ toString i ++ "] " ++ toString r) $ λ _, xs.foldl (fun a b => a + b) (r+i)) 0 def main (xs : List String) : IO UInt32 := let ys := (List.replicate xs.head.toNat 1); let ts : List (Task Nat) := (List.iota 10).map (fun i => Task.mk $ fun _ => run1 (i+1) xs.head.toNat ys); let ns : List Nat := ts.map Task.get; IO.println (">> " ++ toString ns) *> pure 0
f03a20367f9d156a296f06ab6d03695a7bdfb2ee
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/tests/compiler/t4.lean
db57612d9df47fed5d8e7bb436655266700fba32
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
3,474
lean
/- Benchmark for new code generator -/ inductive Expr | Val : Int → Expr | Var : String → Expr | Add : Expr → Expr → Expr | Mul : Expr → Expr → Expr | Pow : Expr → Expr → Expr | Ln : Expr → Expr namespace Expr protected def Expr.toString : Expr → String | Val n => toString n | Var x => x | Add f g => "(" ++ Expr.toString f ++ " + " ++ Expr.toString g ++ ")" | Mul f g => "(" ++ Expr.toString f ++ " * " ++ Expr.toString g ++ ")" | Pow f g => "(" ++ Expr.toString f ++ " ^ " ++ Expr.toString g ++ ")" | Ln f => "ln(" ++ Expr.toString f ++ ")" instance : ToString Expr := ⟨Expr.toString⟩ partial def pown : Int → Int → Int | a, 0 => 1 | a, 1 => a | a, n => let b := pown a (n / 2); b * b * (if n % 2 = 0 then 1 else a) partial def addAux : Expr → Expr → Expr | Val n, Val m => Val (n + m) | Val 0, f => f | f, Val 0 => f | f, Val n => addAux (Val n) f | Val n, Add (Val m) f => addAux (Val (n+m)) f | f, Add (Val n) g => addAux (Val n) (addAux f g) | Add f g, h => addAux f (addAux g h) | f, g => Add f g def add (a b : Expr) : Expr := -- dbgTrace (">> add (" ++ toString a ++ ", " ++ toString b ++ ")") $ fun _ => addAux a b -- set_option trace.compiler.borrowed_inference true partial def mulAux : Expr → Expr → Expr | Val n, Val m => Val (n*m) | Val 0, _ => Val 0 | _, Val 0 => Val 0 | Val 1, f => f | f, Val 1 => f | f, Val n => mulAux (Val n) f | Val n, Mul (Val m) f => mulAux (Val (n*m)) f | f, Mul (Val n) g => mulAux (Val n) (mulAux f g) | Mul f g, h => mulAux f (mulAux g h) | f, g => Mul f g def mul (a b : Expr) : Expr := -- dbgTrace (">> mul (" ++ toString a ++ ", " ++ toString b ++ ")") $ fun _ => mulAux a b def pow : Expr → Expr → Expr | Val m, Val n => Val (pown m n) | _, Val 0 => Val 1 | f, Val 1 => f | Val 0, _ => Val 0 | f, g => Pow f g def ln : Expr → Expr | Val 1 => Val 0 | f => Ln f def d (x : String) : Expr → Expr | Val _ => Val 0 | Var y => if x = y then Val 1 else Val 0 | Add f g => add (d x f) (d x g) | Mul f g => -- dbgTrace (">> d (" ++ toString f ++ ", " ++ toString g ++ ")") $ fun _ => add (mul f (d x g)) (mul g (d x f)) | Pow f g => mul (pow f g) (add (mul (mul g (d x f)) (pow f (Val (-1)))) (mul (ln f) (d x g))) | Ln f => mul (d x f) (pow f (Val (-1))) def count : Expr → Nat | Val _ => 1 | Var _ => 1 | Add f g => count f + count g | Mul f g => count f + count g | Pow f g => count f + count g | Ln f => count f def nestAux (s : Nat) (f : Nat → Expr → IO Expr) : Nat → Expr → IO Expr | 0, x => pure x | m@(n+1), x => f (s - m) x >>= nestAux s f n def nest (f : Nat → Expr → IO Expr) (n : Nat) (e : Expr) : IO Expr := nestAux n f n e def deriv (i : Nat) (f : Expr) : IO Expr := do let d := d "x" f; IO.println (toString (i+1) ++ " count: " ++ (toString $ count d)); IO.println (toString d); pure d end Expr def main (xs : List String) : IO UInt32 := do let x := Expr.Var "x"; let f := Expr.add x (Expr.mul x (Expr.mul x (Expr.add x x))); IO.println f; discard <| Expr.nest Expr.deriv 3 f; pure 0 -- setOption profiler True -- #eval main []
d556cd23787a555ef7edfce132353b3afec0b29a
9dc8cecdf3c4634764a18254e94d43da07142918
/src/group_theory/group_action/group.lean
e09d12082325592410b45b30cd3d1c60f5704eee
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
10,906
lean
/- Copyright (c) 2018 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Chris Hughes -/ import algebra.hom.aut import group_theory.group_action.units /-! # Group actions applied to various types of group This file contains lemmas about `smul` on `group_with_zero`, and `group`. -/ universes u v w variables {α : Type u} {β : Type v} {γ : Type w} section mul_action /-- `monoid.to_mul_action` is faithful on cancellative monoids. -/ @[to_additive /-" `add_monoid.to_add_action` is faithful on additive cancellative monoids. "-/] instance right_cancel_monoid.to_has_faithful_smul [right_cancel_monoid α] : has_faithful_smul α α := ⟨λ x y h, mul_right_cancel (h 1)⟩ section group variables [group α] [mul_action α β] @[simp, to_additive] lemma inv_smul_smul (c : α) (x : β) : c⁻¹ • c • x = x := by rw [smul_smul, mul_left_inv, one_smul] @[simp, to_additive] lemma smul_inv_smul (c : α) (x : β) : c • c⁻¹ • x = x := by rw [smul_smul, mul_right_inv, one_smul] /-- Given an action of a group `α` on `β`, each `g : α` defines a permutation of `β`. -/ @[to_additive, simps] def mul_action.to_perm (a : α) : equiv.perm β := ⟨λ x, a • x, λ x, a⁻¹ • x, inv_smul_smul a, smul_inv_smul a⟩ /-- Given an action of an additive group `α` on `β`, each `g : α` defines a permutation of `β`. -/ add_decl_doc add_action.to_perm /-- `mul_action.to_perm` is injective on faithful actions. -/ @[to_additive "`add_action.to_perm` is injective on faithful actions."] lemma mul_action.to_perm_injective [has_faithful_smul α β] : function.injective (mul_action.to_perm : α → equiv.perm β) := (show function.injective (equiv.to_fun ∘ mul_action.to_perm), from smul_left_injective').of_comp variables (α) (β) /-- Given an action of a group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def mul_action.to_perm_hom : α →* equiv.perm β := { to_fun := mul_action.to_perm, map_one' := equiv.ext $ one_smul α, map_mul' := λ u₁ u₂, equiv.ext $ mul_smul (u₁:α) u₂ } /-- Given an action of a additive group `α` on a set `β`, each `g : α` defines a permutation of `β`. -/ @[simps] def add_action.to_perm_hom (α : Type*) [add_group α] [add_action α β] : α →+ additive (equiv.perm β) := { to_fun := λ a, additive.of_mul $ add_action.to_perm a, map_zero' := equiv.ext $ zero_vadd α, map_add' := λ a₁ a₂, equiv.ext $ add_vadd a₁ a₂ } /-- The tautological action by `equiv.perm α` on `α`. This generalizes `function.End.apply_mul_action`.-/ instance equiv.perm.apply_mul_action (α : Type*) : mul_action (equiv.perm α) α := { smul := λ f a, f a, one_smul := λ _, rfl, mul_smul := λ _ _ _, rfl } @[simp] protected lemma equiv.perm.smul_def {α : Type*} (f : equiv.perm α) (a : α) : f • a = f a := rfl /-- `equiv.perm.apply_mul_action` is faithful. -/ instance equiv.perm.apply_has_faithful_smul (α : Type*) : has_faithful_smul (equiv.perm α) α := ⟨λ x y, equiv.ext⟩ variables {α} {β} @[to_additive] lemma inv_smul_eq_iff {a : α} {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm a).symm_apply_eq @[to_additive] lemma eq_inv_smul_iff {a : α} {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm a).eq_symm_apply lemma smul_inv [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) : (c • x)⁻¹ = c⁻¹ • x⁻¹ := by rw [inv_eq_iff_mul_eq_one, smul_mul_smul, mul_right_inv, mul_right_inv, one_smul] lemma smul_zpow [group β] [smul_comm_class α β β] [is_scalar_tower α β β] (c : α) (x : β) (p : ℤ) : (c • x) ^ p = c ^ p • x ^ p := by { cases p; simp [smul_pow, smul_inv] } @[simp] lemma commute.smul_right_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute a (r • b) ↔ commute a b := ⟨λ h, inv_smul_smul r b ▸ h.smul_right r⁻¹, λ h, h.smul_right r⟩ @[simp] lemma commute.smul_left_iff [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} (r : α) : commute (r • a) b ↔ commute a b := by rw [commute.symm_iff, commute.smul_right_iff, commute.symm_iff] @[to_additive] protected lemma mul_action.bijective (g : α) : function.bijective (λ b : β, g • b) := (mul_action.to_perm g).bijective @[to_additive] protected lemma mul_action.injective (g : α) : function.injective (λ b : β, g • b) := (mul_action.bijective g).injective @[to_additive] lemma smul_left_cancel (g : α) {x y : β} (h : g • x = g • y) : x = y := mul_action.injective g h @[simp, to_additive] lemma smul_left_cancel_iff (g : α) {x y : β} : g • x = g • y ↔ x = y := (mul_action.injective g).eq_iff @[to_additive] lemma smul_eq_iff_eq_inv_smul (g : α) {x y : β} : g • x = y ↔ x = g⁻¹ • y := (mul_action.to_perm g).apply_eq_iff_eq_symm_apply end group /-- `monoid.to_mul_action` is faithful on nontrivial cancellative monoids with zero. -/ instance cancel_monoid_with_zero.to_has_faithful_smul [cancel_monoid_with_zero α] [nontrivial α] : has_faithful_smul α α := ⟨λ x y h, mul_left_injective₀ one_ne_zero (h 1)⟩ section gwz variables [group_with_zero α] [mul_action α β] @[simp] lemma inv_smul_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c⁻¹ • c • x = x := inv_smul_smul (units.mk0 c hc) x @[simp] lemma smul_inv_smul₀ {c : α} (hc : c ≠ 0) (x : β) : c • c⁻¹ • x = x := smul_inv_smul (units.mk0 c hc) x lemma inv_smul_eq_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : a⁻¹ • x = y ↔ x = a • y := (mul_action.to_perm (units.mk0 a ha)).symm_apply_eq lemma eq_inv_smul_iff₀ {a : α} (ha : a ≠ 0) {x y : β} : x = a⁻¹ • y ↔ a • x = y := (mul_action.to_perm (units.mk0 a ha)).eq_symm_apply @[simp] lemma commute.smul_right_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute a (c • b) ↔ commute a b := commute.smul_right_iff (units.mk0 c hc) @[simp] lemma commute.smul_left_iff₀ [has_mul β] [smul_comm_class α β β] [is_scalar_tower α β β] {a b : β} {c : α} (hc : c ≠ 0) : commute (c • a) b ↔ commute a b := commute.smul_left_iff (units.mk0 c hc) end gwz end mul_action section distrib_mul_action section group variables [group α] [add_monoid β] [distrib_mul_action α β] variables (β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def distrib_mul_action.to_add_equiv (x : α) : β ≃+ β := { .. distrib_mul_action.to_add_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an additive monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def distrib_mul_action.to_add_aut : α →* add_aut β := { to_fun := distrib_mul_action.to_add_equiv β, map_one' := add_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, add_equiv.ext (mul_smul _ _) } variables {α β} theorem smul_eq_zero_iff_eq (a : α) {x : β} : a • x = 0 ↔ x = 0 := ⟨λ h, by rw [← inv_smul_smul a x, h, smul_zero], λ h, h.symm ▸ smul_zero _⟩ theorem smul_ne_zero_iff_ne (a : α) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := not_congr $ smul_eq_zero_iff_eq a end group section gwz variables [group_with_zero α] [add_monoid β] [distrib_mul_action α β] theorem smul_eq_zero_iff_eq' {a : α} (ha : a ≠ 0) {x : β} : a • x = 0 ↔ x = 0 := smul_eq_zero_iff_eq (units.mk0 a ha) theorem smul_ne_zero_iff_ne' {a : α} (ha : a ≠ 0) {x : β} : a • x ≠ 0 ↔ x ≠ 0 := smul_ne_zero_iff_ne (units.mk0 a ha) end gwz end distrib_mul_action section mul_distrib_mul_action variables [group α] [monoid β] [mul_distrib_mul_action α β] variables (β) /-- Each element of the group defines a multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm`. -/ @[simps {simp_rhs := tt}] def mul_distrib_mul_action.to_mul_equiv (x : α) : β ≃* β := { .. mul_distrib_mul_action.to_monoid_hom β x, .. mul_action.to_perm_hom α β x } variables (α β) /-- Each element of the group defines an multiplicative monoid isomorphism. This is a stronger version of `mul_action.to_perm_hom`. -/ @[simps] def mul_distrib_mul_action.to_mul_aut : α →* mul_aut β := { to_fun := mul_distrib_mul_action.to_mul_equiv β, map_one' := mul_equiv.ext (one_smul _), map_mul' := λ a₁ a₂, mul_equiv.ext (mul_smul _ _) } variables {α β} end mul_distrib_mul_action section arrow /-- If `G` acts on `A`, then it acts also on `A → B`, by `(g • F) a = F (g⁻¹ • a)`. -/ @[to_additive arrow_add_action "If `G` acts on `A`, then it acts also on `A → B`, by `(g +ᵥ F) a = F (g⁻¹ +ᵥ a)`", simps] def arrow_action {G A B : Type*} [division_monoid G] [mul_action G A] : mul_action G (A → B) := { smul := λ g F a, F (g⁻¹ • a), one_smul := by { intro, simp only [inv_one, one_smul] }, mul_smul := by { intros, simp only [mul_smul, mul_inv_rev] } } local attribute [instance] arrow_action /-- When `B` is a monoid, `arrow_action` is additionally a `mul_distrib_mul_action`. -/ def arrow_mul_distrib_mul_action {G A B : Type*} [group G] [mul_action G A] [monoid B] : mul_distrib_mul_action G (A → B) := { smul_one := λ g, rfl, smul_mul := λ g f₁ f₂, rfl } local attribute [instance] arrow_mul_distrib_mul_action /-- Given groups `G H` with `G` acting on `A`, `G` acts by multiplicative automorphisms on `A → H`. -/ @[simps] def mul_aut_arrow {G A H} [group G] [mul_action G A] [monoid H] : G →* mul_aut (A → H) := mul_distrib_mul_action.to_mul_aut _ _ end arrow namespace is_unit section mul_action variables [monoid α] [mul_action α β] @[to_additive] lemma smul_left_cancel {a : α} (ha : is_unit a) {x y : β} : a • x = a • y ↔ x = y := let ⟨u, hu⟩ := ha in hu ▸ smul_left_cancel_iff u end mul_action section distrib_mul_action variables [monoid α] [add_monoid β] [distrib_mul_action α β] @[simp] theorem smul_eq_zero {u : α} (hu : is_unit u) {x : β} : u • x = 0 ↔ x = 0 := exists.elim hu $ λ u hu, hu ▸ smul_eq_zero_iff_eq u end distrib_mul_action end is_unit section smul variables [group α] [monoid β] @[simp] lemma is_unit_smul_iff [mul_action α β] [smul_comm_class α β β] [is_scalar_tower α β β] (g : α) (m : β) : is_unit (g • m) ↔ is_unit m := ⟨λ h, inv_smul_smul g m ▸ h.smul g⁻¹, is_unit.smul g⟩ lemma is_unit.smul_sub_iff_sub_inv_smul [add_group β] [distrib_mul_action α β] [is_scalar_tower α β β] [smul_comm_class α β β] (r : α) (a : β) : is_unit (r • 1 - a) ↔ is_unit (1 - r⁻¹ • a) := by rw [←is_unit_smul_iff r (1 - r⁻¹ • a), smul_sub, smul_inv_smul] end smul
1f2e31e8bf246b19110aab4133234958e0898664
4d2583807a5ac6caaffd3d7a5f646d61ca85d532
/src/algebra/module/ulift.lean
44299a8a962963dcacc40dde29e77092058644ce
[ "Apache-2.0" ]
permissive
AntoineChambert-Loir/mathlib
64aabb896129885f12296a799818061bc90da1ff
07be904260ab6e36a5769680b6012f03a4727134
refs/heads/master
1,693,187,631,771
1,636,719,886,000
1,636,719,886,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,762
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison -/ import linear_algebra.basic import algebra.ring.ulift /-! # `ulift` instances for module and multiplicative actions This file defines instances for module, mul_action and related structures on `ulift` types. (Recall `ulift α` is just a "copy" of a type `α` in a higher universe.) We also provide `ulift.module_equiv : ulift M ≃ₗ[R] M`. -/ namespace ulift universes u v w variable {R : Type u} variable {M : Type v} variable {N : Type w} instance has_scalar_left [has_scalar R M] : has_scalar (ulift R) M := ⟨λ s x, s.down • x⟩ @[simp] lemma smul_down [has_scalar R M] (s : ulift R) (x : M) : (s • x) = s.down • x := rfl @[simp] lemma smul_down' [has_scalar R M] (s : R) (x : ulift M) : (s • x).down = s • x.down := rfl instance is_scalar_tower [has_scalar R M] [has_scalar M N] [has_scalar R N] [is_scalar_tower R M N] : is_scalar_tower (ulift R) M N := ⟨λ x y z, show (x.down • y) • z = x.down • y • z, from smul_assoc _ _ _⟩ instance is_scalar_tower' [has_scalar R M] [has_scalar M N] [has_scalar R N] [is_scalar_tower R M N] : is_scalar_tower R (ulift M) N := ⟨λ x y z, show (x • y.down) • z = x • y.down • z, from smul_assoc _ _ _⟩ instance is_scalar_tower'' [has_scalar R M] [has_scalar M N] [has_scalar R N] [is_scalar_tower R M N] : is_scalar_tower R M (ulift N) := ⟨λ x y z, show up ((x • y) • z.down) = ⟨x • y • z.down⟩, by rw smul_assoc⟩ instance mul_action [monoid R] [mul_action R M] : mul_action (ulift R) M := { smul := (•), mul_smul := λ r s f, by { cases r, cases s, simp [mul_smul], }, one_smul := λ f, by { simp [one_smul], } } instance mul_action' [monoid R] [mul_action R M] : mul_action R (ulift M) := { smul := (•), mul_smul := λ r s f, by { cases f, ext, simp [mul_smul], }, one_smul := λ f, by { ext, simp [one_smul], } } instance distrib_mul_action [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action (ulift R) M := { smul_zero := λ c, by { cases c, simp [smul_zero], }, smul_add := λ c f g, by { cases c, simp [smul_add], }, ..ulift.mul_action } instance distrib_mul_action' [monoid R] [add_monoid M] [distrib_mul_action R M] : distrib_mul_action R (ulift M) := { smul_zero := λ c, by { ext, simp [smul_zero], }, smul_add := λ c f g, by { ext, simp [smul_add], }, ..ulift.mul_action' } instance mul_distrib_mul_action [monoid R] [monoid M] [mul_distrib_mul_action R M] : mul_distrib_mul_action (ulift R) M := { smul_one := λ c, by { cases c, simp [smul_one], }, smul_mul := λ c f g, by { cases c, simp [smul_mul'], }, ..ulift.mul_action } instance mul_distrib_mul_action' [monoid R] [monoid M] [mul_distrib_mul_action R M] : mul_distrib_mul_action R (ulift M) := { smul_one := λ c, by { ext, simp [smul_one], }, smul_mul := λ c f g, by { ext, simp [smul_mul'], }, ..ulift.mul_action' } instance module [semiring R] [add_comm_monoid M] [module R M] : module (ulift R) M := { add_smul := λ c f g, by { cases c, simp [add_smul], }, zero_smul := λ f, by { simp [zero_smul], }, ..ulift.distrib_mul_action } instance module' [semiring R] [add_comm_monoid M] [module R M] : module R (ulift M) := { add_smul := by { intros, ext1, apply add_smul }, zero_smul := by { intros, ext1, apply zero_smul } } /-- The `R`-linear equivalence between `ulift M` and `M`. -/ def module_equiv [semiring R] [add_comm_monoid M] [module R M] : ulift M ≃ₗ[R] M := { to_fun := ulift.down, inv_fun := ulift.up, map_smul' := λ r x, rfl, map_add' := λ x y, rfl, left_inv := by tidy, right_inv := by tidy, } end ulift
a4fa3d2abdfac12605f8c24fa6834e5e567e2d62
63abd62053d479eae5abf4951554e1064a4c45b4
/src/order/filter/interval.lean
23098c7f249f6b3a1d47216aa8779d2878e4578a
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
10,402
lean
/- Copyright (c) 2020 Yury G. Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Yury G. Kudryashov -/ import data.set.intervals.ord_connected import order.filter.lift import order.filter.at_top_bot /-! # Convergence of intervals If both `a` and `b` tend to some filter `l₁`, sometimes this implies that `Ixx a b` tends to `l₂.lift' powerset`, i.e., for any `s ∈ l₂` eventually `Ixx a b` becomes a subset of `s`. Here and below `Ixx` is one of `Icc`, `Ico`, `Ioc`, and `Ioo`. We define `filter.tendsto_Ixx_class Ixx l₁ l₂` to be a typeclass representing this property. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. The next table shows “output” filters `l₂` for different values of `Ixx` and `l₁`. The instances that need topology are defined in `topology/algebra/ordered`. | Input filter | `Ixx = Icc` | `Ixx = Ico` | `Ixx = Ioc` | `Ixx = Ioo` | | -----------: | :-----------: | :-----------: | :-----------: | :-----------: | | `at_top` | `at_top` | `at_top` | `at_top` | `at_top` | | `at_bot` | `at_bot` | `at_bot` | `at_bot` | `at_bot` | | `pure a` | `pure a` | `⊥` | `⊥` | `⊥` | | `𝓟 (Iic a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | `𝓟 (Iic a)` | `𝓟 (Iio a)` | | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ici a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | `𝓟 (Ioi a)` | | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | `𝓟 (Iio a)` | | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | `𝓝 a` | | `𝓝[Iic a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | `𝓝[Iic a] b` | `𝓝[Iio a] b` | | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ici a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | `𝓝[Ioi a] b` | | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | `𝓝[Iio a] b` | -/ variables {α β : Type*} open_locale classical filter open set function variables [preorder α] namespace filter /-- A pair of filters `l₁`, `l₂` has `tendsto_Ixx_class Ixx` property if `Ixx a b` tends to `l₂.lift' powerset` as `a` and `b` tend to `l₁`. In all instances `Ixx` is one of `Icc`, `Ico`, `Ioc`, or `Ioo`. The instances provide the best `l₂` for a given `l₁`. In many cases `l₁ = l₂` but sometimes we can drop an endpoint from an interval: e.g., we prove `tendsto_Ixx_class Ico (𝓟 $ Iic a) (𝓟 $ Iio a)`, i.e., if `u₁ n` and `u₂ n` belong eventually to `Iic a`, then the interval `Ico (u₁ n) (u₂ n)` is eventually included in `Iio a`. We mark `l₂` as an `out_param` so that Lean can automatically find an appropriate `l₂` based on `Ixx` and `l₁`. This way, e.g., `tendsto.Ico h₁ h₂` works without specifying explicitly `l₂`. -/ class tendsto_Ixx_class (Ixx : α → α → set α) (l₁ : filter α) (l₂ : out_param $ filter α) : Prop := (tendsto_Ixx : tendsto (λ p : α × α, Ixx p.1 p.2) (l₁ ×ᶠ l₁) (l₂.lift' powerset)) lemma tendsto.Icc {l₁ l₂ : filter α} [tendsto_Ixx_class Icc l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Icc (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ioc {l₁ l₂ : filter α} [tendsto_Ixx_class Ioc l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ioc (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ico {l₁ l₂ : filter α} [tendsto_Ixx_class Ico l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ico (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto.Ioo {l₁ l₂ : filter α} [tendsto_Ixx_class Ioo l₁ l₂] {lb : filter β} {u₁ u₂ : β → α} (h₁ : tendsto u₁ lb l₁) (h₂ : tendsto u₂ lb l₁) : tendsto (λ x, Ioo (u₁ x) (u₂ x)) lb (l₂.lift' powerset) := tendsto_Ixx_class.tendsto_Ixx.comp $ h₁.prod_mk h₂ lemma tendsto_Ixx_class_principal {s t : set α} {Ixx : α → α → set α} : tendsto_Ixx_class Ixx (𝓟 s) (𝓟 t) ↔ ∀ (x ∈ s) (y ∈ s), Ixx x y ⊆ t := begin refine iff.trans ⟨λ h, h.1, λ h, ⟨h⟩⟩ _, simp [lift'_principal monotone_powerset, -mem_prod, -prod.forall, forall_prod_set] end lemma tendsto_Ixx_class_inf {l₁ l₁' l₂ l₂' : filter α} {Ixx} [h : tendsto_Ixx_class Ixx l₁ l₂] [h' : tendsto_Ixx_class Ixx l₁' l₂'] : tendsto_Ixx_class Ixx (l₁ ⊓ l₁') (l₂ ⊓ l₂') := ⟨by simpa only [prod_inf_prod, lift'_inf_powerset] using h.1.inf h'.1⟩ lemma tendsto_Ixx_class_of_subset {l₁ l₂ : filter α} {Ixx Ixx' : α → α → set α} (h : ∀ a b, Ixx a b ⊆ Ixx' a b) [h' : tendsto_Ixx_class Ixx' l₁ l₂] : tendsto_Ixx_class Ixx l₁ l₂ := ⟨tendsto_lift'_powerset_mono h'.1 $ eventually_of_forall $ prod.forall.2 h⟩ lemma has_basis.tendsto_Ixx_class {ι : Type*} {p : ι → Prop} {s} {l : filter α} (hl : l.has_basis p s) {Ixx : α → α → set α} (H : ∀ i, p i → ∀ (x ∈ s i) (y ∈ s i), Ixx x y ⊆ s i) : tendsto_Ixx_class Ixx l l := ⟨(hl.prod_self.tendsto_iff (hl.lift' monotone_powerset)).2 $ λ i hi, ⟨i, hi, λ x hx, H i hi _ hx.1 _ hx.2⟩⟩ instance tendsto_Icc_at_top_at_top : tendsto_Ixx_class Icc (at_top : filter α) at_top := (has_basis_infi_principal_finite _).tendsto_Ixx_class $ λ s hs, ord_connected_bInter $ λ i hi, ord_connected_Ici instance tendsto_Ico_at_top_at_top : tendsto_Ixx_class Ico (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_at_top_at_top : tendsto_Ixx_class Ioc (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_at_top_at_top : tendsto_Ixx_class Ioo (at_top : filter α) at_top := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self) instance tendsto_Icc_at_bot_at_bot : tendsto_Ixx_class Icc (at_bot : filter α) at_bot := (has_basis_infi_principal_finite _).tendsto_Ixx_class $ λ s hs, ord_connected_bInter $ λ i hi, ord_connected_Iic instance tendsto_Ico_at_bot_at_bot : tendsto_Ixx_class Ico (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_at_bot_at_bot : tendsto_Ixx_class Ioc (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_at_bot_at_bot : tendsto_Ixx_class Ioo (at_bot : filter α) at_bot := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Icc_self) instance tendsto_Icc_Ici_Ici {a : α} : tendsto_Ixx_class Icc (𝓟 (Ici a)) (𝓟 (Ici a)) := tendsto_Ixx_class_principal.2 ord_connected_Ici instance tendsto_Icc_Iic_Iic {a : α} : tendsto_Ixx_class Icc (𝓟 (Iic a)) (𝓟 (Iic a)) := tendsto_Ixx_class_principal.2 ord_connected_Iic instance tendsto_Icc_Ioi_Ioi {a : α} : tendsto_Ixx_class Icc (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_principal.2 ord_connected_Ioi instance tendsto_Icc_Iio_Iio {a : α} : tendsto_Ixx_class Icc (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_principal.2 ord_connected_Iio instance tendsto_Ico_Ici_Ici {a : α} : tendsto_Ixx_class Ico (𝓟 (Ici a)) (𝓟 (Ici a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ico_Ioi_Ioi {a : α} : tendsto_Ixx_class Ico (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ico_Iic_Iio {a : α} : tendsto_Ixx_class Ico (𝓟 (Iic a)) (𝓟 (Iio a)) := tendsto_Ixx_class_principal.2 $ λ a ha b hb x hx, lt_of_lt_of_le hx.2 hb instance tendsto_Ico_Iio_Iio {a : α} : tendsto_Ixx_class Ico (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ico_subset_Icc_self) instance tendsto_Ioc_Ici_Ioi {a : α} : tendsto_Ixx_class Ioc (𝓟 (Ici a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_principal.2 $ λ x hx y hy t ht, lt_of_le_of_lt hx ht.1 instance tendsto_Ioc_Iic_Iic {a : α} : tendsto_Ixx_class Ioc (𝓟 (Iic a)) (𝓟 (Iic a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioc_Iio_Iio {a : α} : tendsto_Ixx_class Ioc (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioc_Ioi_Ioi {a : α} : tendsto_Ixx_class Ioc (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioc_subset_Icc_self) instance tendsto_Ioo_Ici_Ioi {a : α} : tendsto_Ixx_class Ioo (𝓟 (Ici a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) instance tendsto_Ioo_Iic_Iio {a : α} : tendsto_Ixx_class Ioo (𝓟 (Iic a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ico_self) instance tendsto_Ioo_Ioi_Ioi {a : α} : tendsto_Ixx_class Ioo (𝓟 (Ioi a)) (𝓟 (Ioi a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) instance tendsto_Ioo_Iio_Iio {a : α} : tendsto_Ixx_class Ioo (𝓟 (Iio a)) (𝓟 (Iio a)) := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) variable [partial_order β] instance tendsto_Icc_pure_pure {a : β} : tendsto_Ixx_class Icc (pure a) (pure a : filter β) := by { rw ← principal_singleton, exact tendsto_Ixx_class_principal.2 ord_connected_singleton } instance tendsto_Ico_pure_bot {a : β} : tendsto_Ixx_class Ico (pure a) ⊥ := ⟨by simp [lift'_bot monotone_powerset]⟩ instance tendsto_Ioc_pure_bot {a : β} : tendsto_Ixx_class Ioc (pure a) ⊥ := ⟨by simp [lift'_bot monotone_powerset]⟩ instance tendsto_Ioo_pure_bot {a : β} : tendsto_Ixx_class Ioo (pure a) ⊥ := tendsto_Ixx_class_of_subset (λ _ _, Ioo_subset_Ioc_self) end filter
c48d406f21919f1e16c70c3e0426501aee76bc4f
05f637fa14ac28031cb1ea92086a0f4eb23ff2b1
/tests/lean/parser1.lean
ed702a89220705376504403a669ca272c29fcac4
[ "Apache-2.0" ]
permissive
codyroux/lean0.1
1ce92751d664aacff0529e139083304a7bbc8a71
0dc6fb974aa85ed6f305a2f4b10a53a44ee5f0ef
refs/heads/master
1,610,830,535,062
1,402,150,480,000
1,402,150,480,000
19,588,851
2
0
null
null
null
null
UTF-8
Lean
false
false
116
lean
(* foo(10) *) print "checkpoint" (* parse_lean_cmds("(" .. "*" .. [[ foo(20) ]] .. "*" .. ")") *)
c6e07d4e8b8e3a740131c78e8f3202025a7f5328
316671bfe98dfae2abac483407a5ae2daca54b1b
/02_True_False/00_intro.lean
14e254246ed69875bad4c0fcbd8fac56f37850c4
[]
no_license
chasedawson/cs-dm
9b38bd7b0bd411a9f7713f5d613b8a0b2fdcd28b
ac3cca3eadc56a6355c8e9a56a39ed7eb1db55cb
refs/heads/master
1,585,360,471,246
1,536,097,455,000
1,536,097,455,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
5,228
lean
/- So far, all of the propositions that we've seen are in the form of assertions about equalities: 0 = 0, 1 = 1, 2 + 3 = 4, and so on. And we've seen how to prove propositions of this kind (that are actually true) using eq.refl and rfl as a shorthand. We are now about to set out to explore the different forms of propositions that arise in predicate logic. In this unit, we meet the simplest of all propositions, even simpler than equality statements, namely the propositions that in lean are called "true" and "false". First, true is the proposition that is always trivially provable. -/ /- The "true introduction" inference rule -/ /- Here's the inference rule for true. Note that it doesn't require an inputs/premises at all. It is truly an axiom. Makes sense: You can alway assume that the proposition true is true. -------- (true.intro) pf: true The true.intro inference rule is called true.intro because it is an introduction rule in the sense that it introduces a true in the conclusion that wasn't in the premises (of which there are none here). Here then is a formal (mathematically precise) and mechanically checked proof of the proposition, true, in Lean. -/ theorem t : true := true.intro #check t #reduce t /- That's it! Super easy. -/ /- There is no introduction rule for false! -/ /- Whereas true is trivially provable, the proposition, false, has not proof and can never be proved. Viewed as a type, it has no values at all. It's what we call an uninhabited type. Therefore there can be no inference rule or sequence of rules that derive a proof of false, because such a thing simply does not exist! That is after all the meaning of false: it is not true, so there must be no proof of it, otherwise it would be true, and that would be a fatal contradiction. -/ /- The difference between tt/ff and the propositions, true and false. -/ /- To clarify one major potential point of confusion it's imperative to see that the propositions, true and false, are not the same as the boolean truth values, which in Lean are called tt and ff. In some other languages, they're called "true" and "false", which really is a source of possible confusion. -/ /- First, let's confirm that the types of tt and ff are bool. -/ #check tt #check ff /- You will recall that we can assign these values to variables of type bool -/ def boolean_false := ff #check boolean_false #reduce boolean_false /- By contrast, we cannot assign the value tt as a proof of true. It's not even of the right type. Uncomment the following line to see that this is the case. Read the error message carefully. EXERCISE: Read and explain the error message to a colleague in your class. -/ -- theorem bad : true := tt /- * EX FALSO QUOD LIBET * -/ /- Now we come to a very fundamental concept in logic: from a contradiction, you can derive a proof of any proposition whatsoever. To put it in English terms, if the impossible has happened, then anything goes! The inference rule for this says that if you're given a proof of false, let's call it f, and any proposition, P, whatsoever (any value, P, of type Prop!), then you can derive a proof of P, and the false disappears from the conclusion (which is why we call this inference rule false elimination). Here's the rule: f : false, { P : Prop } ----------------------- false.elim pf : P Note that the proposition argument, P, is not to be given explicitly as an argument to false.elim, but is to be inferred from context, instead. -/ /- Let's see how this works in Lean. Let's start by simply asserting as an axiom, without proof, that f is a proof of false. -/ axiom f : false /- Well, it was probably a bad idea. It just says, "trust me and accept that the impossible just occurred." The problem is that our logic is now inconsistent and anything at all can be proved. We just feed our proof of false to false.elim to prove any proposition at all. Let's try to prove 0 = 1. -/ theorem zeqo : 0 = 1 := false.elim f /- It will very occasionally be useful to add an axiom to Lean, but one must take extraordinary care to ensure that it is consistent with the underlying logic of lean. The axiom that there is a proof of false immediately makes the whole logic useful because it collapses the distinction between true and false entirely, and in this case, any claim can be proven true, and that would put us logically in a post-truth work, which would be a bad and useless place to be. -/ /- EXERCISE: Prove true = false (given that we've assume there is a proof of false). -/ /- As a final observation, we note that there are some propositions that one might think of as being false, and thus provable from a contradiction, but that we cannot even state in Lean. For example, we can't even state the claim that 1 = tt because Lean requires that the types of the arguments on each side of the = be the same. EXERCISE: Try it. The error message that appears is a little bit complicate but in a nutshell it's saying "I can't find a way to coerce/convert 1 into a bool, and so I can't do anything with this expression." In simple terms, the expression 1 = tt has a type error. It's not even a well formed expression. -/
c333e9c2ce23207ae665c27ddd407a55efefcc9f
4727251e0cd73359b15b664c3170e5d754078599
/src/algebra/free_monoid.lean
9141a95efce5cbb147eac9d26a8277f6f2a13066
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
4,494
lean
/- Copyright (c) 2019 Simon Hudon. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Simon Hudon, Yury Kudryashov -/ import data.list.big_operators /-! # Free monoid over a given alphabet ## Main definitions * `free_monoid α`: free monoid over alphabet `α`; defined as a synonym for `list α` with multiplication given by `(++)`. * `free_monoid.of`: embedding `α → free_monoid α` sending each element `x` to `[x]`; * `free_monoid.lift`: natural equivalence between `α → M` and `free_monoid α →* M` * `free_monoid.map`: embedding of `α → β` into `free_monoid α →* free_monoid β` given by `list.map`. -/ variables {α : Type*} {β : Type*} {γ : Type*} {M : Type*} [monoid M] {N : Type*} [monoid N] /-- Free monoid over a given alphabet. -/ @[to_additive "Free nonabelian additive monoid over a given alphabet"] def free_monoid (α) := list α namespace free_monoid @[to_additive] instance : monoid (free_monoid α) := { one := [], mul := λ x y, (x ++ y : list α), mul_one := by intros; apply list.append_nil, one_mul := by intros; refl, mul_assoc := by intros; apply list.append_assoc } @[to_additive] instance : inhabited (free_monoid α) := ⟨1⟩ @[to_additive] lemma one_def : (1 : free_monoid α) = [] := rfl @[to_additive] lemma mul_def (xs ys : list α) : (xs * ys : free_monoid α) = (xs ++ ys : list α) := rfl /-- Embeds an element of `α` into `free_monoid α` as a singleton list. -/ @[to_additive "Embeds an element of `α` into `free_add_monoid α` as a singleton list." ] def of (x : α) : free_monoid α := [x] @[to_additive] lemma of_def (x : α) : of x = [x] := rfl @[to_additive] lemma of_injective : function.injective (@of α) := λ a b, list.head_eq_of_cons_eq /-- Recursor for `free_monoid` using `1` and `of x * xs` instead of `[]` and `x :: xs`. -/ @[elab_as_eliminator, to_additive "Recursor for `free_add_monoid` using `0` and `of x + xs` instead of `[]` and `x :: xs`."] def rec_on {C : free_monoid α → Sort*} (xs : free_monoid α) (h0 : C 1) (ih : Π x xs, C xs → C (of x * xs)) : C xs := list.rec_on xs h0 ih @[ext, to_additive] lemma hom_eq ⦃f g : free_monoid α →* M⦄ (h : ∀ x, f (of x) = g (of x)) : f = g := monoid_hom.ext $ λ l, rec_on l (f.map_one.trans g.map_one.symm) $ λ x xs hxs, by simp only [h, hxs, monoid_hom.map_mul] /-- Equivalence between maps `α → M` and monoid homomorphisms `free_monoid α →* M`. -/ @[to_additive "Equivalence between maps `α → A` and additive monoid homomorphisms `free_add_monoid α →+ A`."] def lift : (α → M) ≃ (free_monoid α →* M) := { to_fun := λ f, ⟨λ l, (l.map f).prod, rfl, λ l₁ l₂, by simp only [mul_def, list.map_append, list.prod_append]⟩, inv_fun := λ f x, f (of x), left_inv := λ f, funext $ λ x, one_mul (f x), right_inv := λ f, hom_eq $ λ x, one_mul (f (of x)) } @[simp, to_additive] lemma lift_symm_apply (f : free_monoid α →* M) : lift.symm f = f ∘ of := rfl @[to_additive] lemma lift_apply (f : α → M) (l : free_monoid α) : lift f l = (l.map f).prod := rfl @[to_additive] lemma lift_comp_of (f : α → M) : (lift f) ∘ of = f := lift.symm_apply_apply f @[simp, to_additive] lemma lift_eval_of (f : α → M) (x : α) : lift f (of x) = f x := congr_fun (lift_comp_of f) x @[simp, to_additive] lemma lift_restrict (f : free_monoid α →* M) : lift (f ∘ of) = f := lift.apply_symm_apply f @[to_additive] lemma comp_lift (g : M →* N) (f : α → M) : g.comp (lift f) = lift (g ∘ f) := by { ext, simp } @[to_additive] lemma hom_map_lift (g : M →* N) (f : α → M) (x : free_monoid α) : g (lift f x) = lift (g ∘ f) x := monoid_hom.ext_iff.1 (comp_lift g f) x /-- The unique monoid homomorphism `free_monoid α →* free_monoid β` that sends each `of x` to `of (f x)`. -/ @[to_additive "The unique additive monoid homomorphism `free_add_monoid α →+ free_add_monoid β` that sends each `of x` to `of (f x)`."] def map (f : α → β) : free_monoid α →* free_monoid β := { to_fun := list.map f, map_one' := rfl, map_mul' := λ l₁ l₂, list.map_append _ _ _ } @[simp, to_additive] lemma map_of (f : α → β) (x : α) : map f (of x) = of (f x) := rfl @[to_additive] lemma lift_of_comp_eq_map (f : α → β) : lift (λ x, of (f x)) = map f := hom_eq $ λ x, rfl @[to_additive] lemma map_comp (g : β → γ) (f : α → β) : map (g ∘ f) = (map g).comp (map f) := hom_eq $ λ x, rfl end free_monoid
f19871e4803af02071580dbb92e1279989ad0c67
fcf3ffa92a3847189ca669cb18b34ef6b2ec2859
/src/world6/level5.lean
ed1b9c87a12a469f9c2ff0ea1ca526b7ccb38640
[ "Apache-2.0" ]
permissive
nomoid/lean-proofs
4a80a97888699dee42b092b7b959b22d9aa0c066
b9f03a24623d1a1d111d6c2bbf53c617e2596d6a
refs/heads/master
1,674,955,317,080
1,607,475,706,000
1,607,475,706,000
314,104,281
0
0
null
null
null
null
UTF-8
Lean
false
false
91
lean
example (P Q : Prop) : P → (Q → P) := begin intro p, intro q, exact p, end
e206bc49e5a62a4442301c60ea9fe44f23af5df1
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/ind8.lean
796817a303238b4166609019280152a629dfc145
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
331
lean
inductive Pair1 (A B : Type) | mk ( ) : A → B → Pair1 check Pair1.mk check Pair1.mk Prop Prop true false inductive Pair2 {A : Type} (B : A → Type) | mk ( ) : Π (a : A), B a → Pair2 check @Pair2.mk check Pair2.mk (λx, Prop) true false inductive Pair3 (A B : Type) | mk : A → B → Pair3 check Pair3.mk true false
0aeac4f858e5e86eb18a4fbf7a179ff6de17efeb
9dc8cecdf3c4634764a18254e94d43da07142918
/src/data/finset/locally_finite.lean
84879db5c71a892bd6452dfcb9c842bd2708f121
[ "Apache-2.0" ]
permissive
jcommelin/mathlib
d8456447c36c176e14d96d9e76f39841f69d2d9b
ee8279351a2e434c2852345c51b728d22af5a156
refs/heads/master
1,664,782,136,488
1,663,638,983,000
1,663,638,983,000
132,563,656
0
0
Apache-2.0
1,663,599,929,000
1,525,760,539,000
Lean
UTF-8
Lean
false
false
24,751
lean
/- Copyright (c) 2019 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Yaël Dillies -/ import order.locally_finite /-! # Intervals as finsets This file provides basic results about all the `finset.Ixx`, which are defined in `order.locally_finite`. ## TODO This file was originally only about `finset.Ico a b` where `a b : ℕ`. No care has yet been taken to generalize these lemmas properly and many lemmas about `Icc`, `Ioc`, `Ioo` are missing. In general, what's to do is taking the lemmas in `data.x.intervals` and abstract away the concrete structure. Complete the API. See https://github.com/leanprover-community/mathlib/pull/14448#discussion_r906109235 for some ideas. -/ open_locale big_operators variables {ι α : Type*} namespace finset section preorder variables [preorder α] section locally_finite_order variables [locally_finite_order α] {a a₁ a₂ b b₁ b₂ c x : α} @[simp] lemma nonempty_Icc : (Icc a b).nonempty ↔ a ≤ b := by rw [←coe_nonempty, coe_Icc, set.nonempty_Icc] @[simp] lemma nonempty_Ico : (Ico a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ico, set.nonempty_Ico] @[simp] lemma nonempty_Ioc : (Ioc a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ioc, set.nonempty_Ioc] @[simp] lemma nonempty_Ioo [densely_ordered α] : (Ioo a b).nonempty ↔ a < b := by rw [←coe_nonempty, coe_Ioo, set.nonempty_Ioo] @[simp] lemma Icc_eq_empty_iff : Icc a b = ∅ ↔ ¬a ≤ b := by rw [←coe_eq_empty, coe_Icc, set.Icc_eq_empty_iff] @[simp] lemma Ico_eq_empty_iff : Ico a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ico, set.Ico_eq_empty_iff] @[simp] lemma Ioc_eq_empty_iff : Ioc a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ioc, set.Ioc_eq_empty_iff] @[simp] lemma Ioo_eq_empty_iff [densely_ordered α] : Ioo a b = ∅ ↔ ¬a < b := by rw [←coe_eq_empty, coe_Ioo, set.Ioo_eq_empty_iff] alias Icc_eq_empty_iff ↔ _ Icc_eq_empty alias Ico_eq_empty_iff ↔ _ Ico_eq_empty alias Ioc_eq_empty_iff ↔ _ Ioc_eq_empty @[simp] lemma Ioo_eq_empty (h : ¬a < b) : Ioo a b = ∅ := eq_empty_iff_forall_not_mem.2 $ λ x hx, h ((mem_Ioo.1 hx).1.trans (mem_Ioo.1 hx).2) @[simp] lemma Icc_eq_empty_of_lt (h : b < a) : Icc a b = ∅ := Icc_eq_empty h.not_le @[simp] lemma Ico_eq_empty_of_le (h : b ≤ a) : Ico a b = ∅ := Ico_eq_empty h.not_lt @[simp] lemma Ioc_eq_empty_of_le (h : b ≤ a) : Ioc a b = ∅ := Ioc_eq_empty h.not_lt @[simp] lemma Ioo_eq_empty_of_le (h : b ≤ a) : Ioo a b = ∅ := Ioo_eq_empty h.not_lt @[simp] lemma left_mem_Icc : a ∈ Icc a b ↔ a ≤ b := by simp only [mem_Icc, true_and, le_rfl] @[simp] lemma left_mem_Ico : a ∈ Ico a b ↔ a < b := by simp only [mem_Ico, true_and, le_refl] @[simp] lemma right_mem_Icc : b ∈ Icc a b ↔ a ≤ b := by simp only [mem_Icc, and_true, le_rfl] @[simp] lemma right_mem_Ioc : b ∈ Ioc a b ↔ a < b := by simp only [mem_Ioc, and_true, le_rfl] @[simp] lemma left_not_mem_Ioc : a ∉ Ioc a b := λ h, lt_irrefl _ (mem_Ioc.1 h).1 @[simp] lemma left_not_mem_Ioo : a ∉ Ioo a b := λ h, lt_irrefl _ (mem_Ioo.1 h).1 @[simp] lemma right_not_mem_Ico : b ∉ Ico a b := λ h, lt_irrefl _ (mem_Ico.1 h).2 @[simp] lemma right_not_mem_Ioo : b ∉ Ioo a b := λ h, lt_irrefl _ (mem_Ioo.1 h).2 lemma Icc_subset_Icc (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊆ Icc a₂ b₂ := by simpa [←coe_subset] using set.Icc_subset_Icc ha hb lemma Ico_subset_Ico (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ico a₁ b₁ ⊆ Ico a₂ b₂ := by simpa [←coe_subset] using set.Ico_subset_Ico ha hb lemma Ioc_subset_Ioc (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ioc a₁ b₁ ⊆ Ioc a₂ b₂ := by simpa [←coe_subset] using set.Ioc_subset_Ioc ha hb lemma Ioo_subset_Ioo (ha : a₂ ≤ a₁) (hb : b₁ ≤ b₂) : Ioo a₁ b₁ ⊆ Ioo a₂ b₂ := by simpa [←coe_subset] using set.Ioo_subset_Ioo ha hb lemma Icc_subset_Icc_left (h : a₁ ≤ a₂) : Icc a₂ b ⊆ Icc a₁ b := Icc_subset_Icc h le_rfl lemma Ico_subset_Ico_left (h : a₁ ≤ a₂) : Ico a₂ b ⊆ Ico a₁ b := Ico_subset_Ico h le_rfl lemma Ioc_subset_Ioc_left (h : a₁ ≤ a₂) : Ioc a₂ b ⊆ Ioc a₁ b := Ioc_subset_Ioc h le_rfl lemma Ioo_subset_Ioo_left (h : a₁ ≤ a₂) : Ioo a₂ b ⊆ Ioo a₁ b := Ioo_subset_Ioo h le_rfl lemma Icc_subset_Icc_right (h : b₁ ≤ b₂) : Icc a b₁ ⊆ Icc a b₂ := Icc_subset_Icc le_rfl h lemma Ico_subset_Ico_right (h : b₁ ≤ b₂) : Ico a b₁ ⊆ Ico a b₂ := Ico_subset_Ico le_rfl h lemma Ioc_subset_Ioc_right (h : b₁ ≤ b₂) : Ioc a b₁ ⊆ Ioc a b₂ := Ioc_subset_Ioc le_rfl h lemma Ioo_subset_Ioo_right (h : b₁ ≤ b₂) : Ioo a b₁ ⊆ Ioo a b₂ := Ioo_subset_Ioo le_rfl h lemma Ico_subset_Ioo_left (h : a₁ < a₂) : Ico a₂ b ⊆ Ioo a₁ b := by { rw [←coe_subset, coe_Ico, coe_Ioo], exact set.Ico_subset_Ioo_left h } lemma Ioc_subset_Ioo_right (h : b₁ < b₂) : Ioc a b₁ ⊆ Ioo a b₂ := by { rw [←coe_subset, coe_Ioc, coe_Ioo], exact set.Ioc_subset_Ioo_right h } lemma Icc_subset_Ico_right (h : b₁ < b₂) : Icc a b₁ ⊆ Ico a b₂ := by { rw [←coe_subset, coe_Icc, coe_Ico], exact set.Icc_subset_Ico_right h } lemma Ioo_subset_Ico_self : Ioo a b ⊆ Ico a b := by { rw [←coe_subset, coe_Ioo, coe_Ico], exact set.Ioo_subset_Ico_self } lemma Ioo_subset_Ioc_self : Ioo a b ⊆ Ioc a b := by { rw [←coe_subset, coe_Ioo, coe_Ioc], exact set.Ioo_subset_Ioc_self } lemma Ico_subset_Icc_self : Ico a b ⊆ Icc a b := by { rw [←coe_subset, coe_Ico, coe_Icc], exact set.Ico_subset_Icc_self } lemma Ioc_subset_Icc_self : Ioc a b ⊆ Icc a b := by { rw [←coe_subset, coe_Ioc, coe_Icc], exact set.Ioc_subset_Icc_self } lemma Ioo_subset_Icc_self : Ioo a b ⊆ Icc a b := Ioo_subset_Ico_self.trans Ico_subset_Icc_self lemma Icc_subset_Icc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Icc a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := by rw [←coe_subset, coe_Icc, coe_Icc, set.Icc_subset_Icc_iff h₁] lemma Icc_subset_Ioo_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioo a₂ b₂ ↔ a₂ < a₁ ∧ b₁ < b₂ := by rw [←coe_subset, coe_Icc, coe_Ioo, set.Icc_subset_Ioo_iff h₁] lemma Icc_subset_Ico_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ < b₂ := by rw [←coe_subset, coe_Icc, coe_Ico, set.Icc_subset_Ico_iff h₁] lemma Icc_subset_Ioc_iff (h₁ : a₁ ≤ b₁) : Icc a₁ b₁ ⊆ Ioc a₂ b₂ ↔ a₂ < a₁ ∧ b₁ ≤ b₂ := (Icc_subset_Ico_iff h₁.dual).trans and.comm --TODO: `Ico_subset_Ioo_iff`, `Ioc_subset_Ioo_iff` lemma Icc_ssubset_Icc_left (hI : a₂ ≤ b₂) (ha : a₂ < a₁) (hb : b₁ ≤ b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := by { rw [←coe_ssubset, coe_Icc, coe_Icc], exact set.Icc_ssubset_Icc_left hI ha hb } lemma Icc_ssubset_Icc_right (hI : a₂ ≤ b₂) (ha : a₂ ≤ a₁) (hb : b₁ < b₂) : Icc a₁ b₁ ⊂ Icc a₂ b₂ := by { rw [←coe_ssubset, coe_Icc, coe_Icc], exact set.Icc_ssubset_Icc_right hI ha hb } variables (a) @[simp] lemma Ico_self : Ico a a = ∅ := Ico_eq_empty $ lt_irrefl _ @[simp] lemma Ioc_self : Ioc a a = ∅ := Ioc_eq_empty $ lt_irrefl _ @[simp] lemma Ioo_self : Ioo a a = ∅ := Ioo_eq_empty $ lt_irrefl _ variables {a} /-- A set with upper and lower bounds in a locally finite order is a fintype -/ def _root_.set.fintype_of_mem_bounds {s : set α} [decidable_pred (∈ s)] (ha : a ∈ lower_bounds s) (hb : b ∈ upper_bounds s) : fintype s := set.fintype_subset (set.Icc a b) $ λ x hx, ⟨ha hx, hb hx⟩ lemma _root_.bdd_below.finite_of_bdd_above {s : set α} (h₀ : bdd_below s) (h₁ : bdd_above s) : s.finite := let ⟨a, ha⟩ := h₀, ⟨b, hb⟩ := h₁ in by { classical, exact ⟨set.fintype_of_mem_bounds ha hb⟩ } section filter lemma Ico_filter_lt_of_le_left [decidable_pred (< c)] (hca : c ≤ a) : (Ico a b).filter (< c) = ∅ := filter_false_of_mem (λ x hx, (hca.trans (mem_Ico.1 hx).1).not_lt) lemma Ico_filter_lt_of_right_le [decidable_pred (< c)] (hbc : b ≤ c) : (Ico a b).filter (< c) = Ico a b := filter_true_of_mem (λ x hx, (mem_Ico.1 hx).2.trans_le hbc) lemma Ico_filter_lt_of_le_right [decidable_pred (< c)] (hcb : c ≤ b) : (Ico a b).filter (< c) = Ico a c := begin ext x, rw [mem_filter, mem_Ico, mem_Ico, and.right_comm], exact and_iff_left_of_imp (λ h, h.2.trans_le hcb), end lemma Ico_filter_le_of_le_left {a b c : α} [decidable_pred ((≤) c)] (hca : c ≤ a) : (Ico a b).filter ((≤) c) = Ico a b := filter_true_of_mem (λ x hx, hca.trans (mem_Ico.1 hx).1) lemma Ico_filter_le_of_right_le {a b : α} [decidable_pred ((≤) b)] : (Ico a b).filter ((≤) b) = ∅ := filter_false_of_mem (λ x hx, (mem_Ico.1 hx).2.not_le) lemma Ico_filter_le_of_left_le {a b c : α} [decidable_pred ((≤) c)] (hac : a ≤ c) : (Ico a b).filter ((≤) c) = Ico c b := begin ext x, rw [mem_filter, mem_Ico, mem_Ico, and_comm, and.left_comm], exact and_iff_right_of_imp (λ h, hac.trans h.1), end lemma Icc_filter_lt_of_lt_right {a b c : α} [decidable_pred (< c)] (h : b < c) : (Icc a b).filter (< c) = Icc a b := (finset.filter_eq_self _).2 (λ x hx, lt_of_le_of_lt (mem_Icc.1 hx).2 h) lemma Ioc_filter_lt_of_lt_right {a b c : α} [decidable_pred (< c)] (h : b < c) : (Ioc a b).filter (< c) = Ioc a b := (finset.filter_eq_self _).2 (λ x hx, lt_of_le_of_lt (mem_Ioc.1 hx).2 h) lemma Iic_filter_lt_of_lt_right {α} [preorder α] [locally_finite_order_bot α] {a c : α} [decidable_pred (< c)] (h : a < c) : (Iic a).filter (< c) = Iic a := (finset.filter_eq_self _).2 (λ x hx, lt_of_le_of_lt (mem_Iic.1 hx) h) variables (a b) [fintype α] lemma filter_lt_lt_eq_Ioo [decidable_pred (λ j, a < j ∧ j < b)] : univ.filter (λ j, a < j ∧ j < b) = Ioo a b := by { ext, simp } lemma filter_lt_le_eq_Ioc [decidable_pred (λ j, a < j ∧ j ≤ b)] : univ.filter (λ j, a < j ∧ j ≤ b) = Ioc a b := by { ext, simp } lemma filter_le_lt_eq_Ico [decidable_pred (λ j, a ≤ j ∧ j < b)] : univ.filter (λ j, a ≤ j ∧ j < b) = Ico a b := by { ext, simp } lemma filter_le_le_eq_Icc [decidable_pred (λ j, a ≤ j ∧ j ≤ b)] : univ.filter (λ j, a ≤ j ∧ j ≤ b) = Icc a b := by { ext, simp } end filter section locally_finite_order_top variables [locally_finite_order_top α] lemma Icc_subset_Ici_self : Icc a b ⊆ Ici a := by simpa [←coe_subset] using set.Icc_subset_Ici_self lemma Ico_subset_Ici_self : Ico a b ⊆ Ici a := by simpa [←coe_subset] using set.Ico_subset_Ici_self lemma Ioc_subset_Ioi_self : Ioc a b ⊆ Ioi a := by simpa [←coe_subset] using set.Ioc_subset_Ioi_self lemma Ioo_subset_Ioi_self : Ioo a b ⊆ Ioi a := by simpa [←coe_subset] using set.Ioo_subset_Ioi_self lemma Ioc_subset_Ici_self : Ioc a b ⊆ Ici a := Ioc_subset_Icc_self.trans Icc_subset_Ici_self lemma Ioo_subset_Ici_self : Ioo a b ⊆ Ici a := Ioo_subset_Ico_self.trans Ico_subset_Ici_self end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] lemma Icc_subset_Iic_self : Icc a b ⊆ Iic b := by simpa [←coe_subset] using set.Icc_subset_Iic_self lemma Ioc_subset_Iic_self : Ioc a b ⊆ Iic b := by simpa [←coe_subset] using set.Ioc_subset_Iic_self lemma Ico_subset_Iio_self : Ico a b ⊆ Iio b := by simpa [←coe_subset] using set.Ico_subset_Iio_self lemma Ioo_subset_Iio_self : Ioo a b ⊆ Iio b := by simpa [←coe_subset] using set.Ioo_subset_Iio_self lemma Ico_subset_Iic_self : Ico a b ⊆ Iic b := Ico_subset_Icc_self.trans Icc_subset_Iic_self lemma Ioo_subset_Iic_self : Ioo a b ⊆ Iic b := Ioo_subset_Ioc_self.trans Ioc_subset_Iic_self end locally_finite_order_bot end locally_finite_order section locally_finite_order_top variables [locally_finite_order_top α] {a : α} lemma Ioi_subset_Ici_self : Ioi a ⊆ Ici a := by simpa [←coe_subset] using set.Ioi_subset_Ici_self lemma _root_.bdd_below.finite {s : set α} (hs : bdd_below s) : s.finite := let ⟨a, ha⟩ := hs in (Ici a).finite_to_set.subset $ λ x hx, mem_Ici.2 $ ha hx variables [fintype α] lemma filter_lt_eq_Ioi [decidable_pred ((<) a)] : univ.filter ((<) a) = Ioi a := by { ext, simp } lemma filter_le_eq_Ici [decidable_pred ((≤) a)] : univ.filter ((≤) a) = Ici a := by { ext, simp } end locally_finite_order_top section locally_finite_order_bot variables [locally_finite_order_bot α] {a : α} lemma Iio_subset_Iic_self : Iio a ⊆ Iic a := by simpa [←coe_subset] using set.Iio_subset_Iic_self lemma _root_.bdd_above.finite {s : set α} (hs : bdd_above s) : s.finite := hs.dual.finite variables [fintype α] lemma filter_gt_eq_Iio [decidable_pred (< a)] : univ.filter (< a) = Iio a := by { ext, simp } lemma filter_ge_eq_Iic [decidable_pred (≤ a)] : univ.filter (≤ a) = Iic a := by { ext, simp } end locally_finite_order_bot variables [decidable_eq α] [locally_finite_order_top α] [locally_finite_order_bot α] lemma disjoint_Ioi_Iio (a : α) : disjoint (Ioi a) (Iio a) := disjoint_left.2 $ λ b hab hba, (mem_Ioi.1 hab).not_lt $ mem_Iio.1 hba end preorder section partial_order variables [partial_order α] [locally_finite_order α] {a b c : α} @[simp] lemma Icc_self (a : α) : Icc a a = {a} := by rw [←coe_eq_singleton, coe_Icc, set.Icc_self] @[simp] lemma Icc_eq_singleton_iff : Icc a b = {c} ↔ a = c ∧ b = c := by rw [←coe_eq_singleton, coe_Icc, set.Icc_eq_singleton_iff] section decidable_eq variables [decidable_eq α] @[simp] lemma Icc_erase_left (a b : α) : (Icc a b).erase a = Ioc a b := by simp [←coe_inj] @[simp] lemma Icc_erase_right (a b : α) : (Icc a b).erase b = Ico a b := by simp [←coe_inj] @[simp] lemma Ico_erase_left (a b : α) : (Ico a b).erase a = Ioo a b := by simp [←coe_inj] @[simp] lemma Ioc_erase_right (a b : α) : (Ioc a b).erase b = Ioo a b := by simp [←coe_inj] @[simp] lemma Icc_diff_both (a b : α) : Icc a b \ {a, b} = Ioo a b := by simp [←coe_inj] @[simp] lemma Ico_insert_right (h : a ≤ b) : insert b (Ico a b) = Icc a b := by rw [←coe_inj, coe_insert, coe_Icc, coe_Ico, set.insert_eq, set.union_comm, set.Ico_union_right h] @[simp] lemma Ioc_insert_left (h : a ≤ b) : insert a (Ioc a b) = Icc a b := by rw [←coe_inj, coe_insert, coe_Ioc, coe_Icc, set.insert_eq, set.union_comm, set.Ioc_union_left h] @[simp] lemma Ioo_insert_left (h : a < b) : insert a (Ioo a b) = Ico a b := by rw [←coe_inj, coe_insert, coe_Ioo, coe_Ico, set.insert_eq, set.union_comm, set.Ioo_union_left h] @[simp] lemma Ioo_insert_right (h : a < b) : insert b (Ioo a b) = Ioc a b := by rw [←coe_inj, coe_insert, coe_Ioo, coe_Ioc, set.insert_eq, set.union_comm, set.Ioo_union_right h] @[simp] lemma Icc_diff_Ico_self (h : a ≤ b) : Icc a b \ Ico a b = {b} := by simp [←coe_inj, h] @[simp] lemma Icc_diff_Ioc_self (h : a ≤ b) : Icc a b \ Ioc a b = {a} := by simp [←coe_inj, h] @[simp] lemma Icc_diff_Ioo_self (h : a ≤ b) : Icc a b \ Ioo a b = {a, b} := by simp [←coe_inj, h] @[simp] lemma Ico_diff_Ioo_self (h : a < b) : Ico a b \ Ioo a b = {a} := by simp [←coe_inj, h] @[simp] lemma Ioc_diff_Ioo_self (h : a < b) : Ioc a b \ Ioo a b = {b} := by simp [←coe_inj, h] @[simp] lemma Ico_inter_Ico_consecutive (a b c : α) : Ico a b ∩ Ico b c = ∅ := begin refine eq_empty_of_forall_not_mem (λ x hx, _), rw [mem_inter, mem_Ico, mem_Ico] at hx, exact hx.1.2.not_le hx.2.1, end lemma Ico_disjoint_Ico_consecutive (a b c : α) : disjoint (Ico a b) (Ico b c) := le_of_eq $ Ico_inter_Ico_consecutive a b c end decidable_eq -- Those lemmas are purposefully the other way around lemma Icc_eq_cons_Ico (h : a ≤ b) : Icc a b = (Ico a b).cons b right_not_mem_Ico := by { classical, rw [cons_eq_insert, Ico_insert_right h] } lemma Icc_eq_cons_Ioc (h : a ≤ b) : Icc a b = (Ioc a b).cons a left_not_mem_Ioc := by { classical, rw [cons_eq_insert, Ioc_insert_left h] } lemma Ico_filter_le_left {a b : α} [decidable_pred (≤ a)] (hab : a < b) : (Ico a b).filter (λ x, x ≤ a) = {a} := begin ext x, rw [mem_filter, mem_Ico, mem_singleton, and.right_comm, ←le_antisymm_iff, eq_comm], exact and_iff_left_of_imp (λ h, h.le.trans_lt hab), end lemma card_Ico_eq_card_Icc_sub_one (a b : α) : (Ico a b).card = (Icc a b).card - 1 := begin classical, by_cases h : a ≤ b, { rw [←Ico_insert_right h, card_insert_of_not_mem right_not_mem_Ico], exact (nat.add_sub_cancel _ _).symm }, { rw [Ico_eq_empty (λ h', h h'.le), Icc_eq_empty h, card_empty, zero_tsub] } end lemma card_Ioc_eq_card_Icc_sub_one (a b : α) : (Ioc a b).card = (Icc a b).card - 1 := @card_Ico_eq_card_Icc_sub_one αᵒᵈ _ _ _ _ lemma card_Ioo_eq_card_Ico_sub_one (a b : α) : (Ioo a b).card = (Ico a b).card - 1 := begin classical, by_cases h : a ≤ b, { obtain rfl | h' := h.eq_or_lt, { rw [Ioo_self, Ico_self, card_empty] }, rw [←Ioo_insert_left h', card_insert_of_not_mem left_not_mem_Ioo], exact (nat.add_sub_cancel _ _).symm }, { rw [Ioo_eq_empty (λ h', h h'.le), Ico_eq_empty (λ h', h h'.le), card_empty, zero_tsub] } end lemma card_Ioo_eq_card_Ioc_sub_one (a b : α) : (Ioo a b).card = (Ioc a b).card - 1 := @card_Ioo_eq_card_Ico_sub_one αᵒᵈ _ _ _ _ lemma card_Ioo_eq_card_Icc_sub_two (a b : α) : (Ioo a b).card = (Icc a b).card - 2 := by { rw [card_Ioo_eq_card_Ico_sub_one, card_Ico_eq_card_Icc_sub_one], refl } section order_top variables [order_top α] @[simp] lemma Ici_erase [decidable_eq α] (a : α) : (Ici a).erase a = Ioi a := Icc_erase_left _ _ @[simp] lemma Ioi_insert [decidable_eq α] (a : α) : insert a (Ioi a) = Ici a := Ioc_insert_left le_top -- Purposefully written the other way around lemma Ici_eq_cons_Ioi (a : α) : Ici a = (Ioi a).cons a left_not_mem_Ioc := by { classical, rw [cons_eq_insert, Ioi_insert] } end order_top section order_bot variables [order_bot α] @[simp] lemma Iic_erase [decidable_eq α] (b : α) : (Iic b).erase b = Iio b := Icc_erase_right _ _ @[simp] lemma Iio_insert [decidable_eq α] (b : α) : insert b (Iio b) = Iic b := Ico_insert_right bot_le -- Purposefully written the other way around lemma Iic_eq_cons_Iio (b : α) : Iic b = (Iio b).cons b right_not_mem_Ico := by { classical, rw [cons_eq_insert, Iio_insert] } end order_bot end partial_order section linear_order variables [linear_order α] section locally_finite_order variables [locally_finite_order α] {a b : α} lemma Ico_subset_Ico_iff {a₁ b₁ a₂ b₂ : α} (h : a₁ < b₁) : Ico a₁ b₁ ⊆ Ico a₂ b₂ ↔ a₂ ≤ a₁ ∧ b₁ ≤ b₂ := by rw [←coe_subset, coe_Ico, coe_Ico, set.Ico_subset_Ico_iff h] lemma Ico_union_Ico_eq_Ico {a b c : α} (hab : a ≤ b) (hbc : b ≤ c) : Ico a b ∪ Ico b c = Ico a c := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico_eq_Ico hab hbc] @[simp] lemma Ioc_union_Ioc_eq_Ioc {a b c : α} (h₁ : a ≤ b) (h₂ : b ≤ c) : Ioc a b ∪ Ioc b c = Ioc a c := by rw [←coe_inj, coe_union, coe_Ioc, coe_Ioc, coe_Ioc, set.Ioc_union_Ioc_eq_Ioc h₁ h₂] lemma Ico_subset_Ico_union_Ico {a b c : α} : Ico a c ⊆ Ico a b ∪ Ico b c := by { rw [←coe_subset, coe_union, coe_Ico, coe_Ico, coe_Ico], exact set.Ico_subset_Ico_union_Ico } lemma Ico_union_Ico' {a b c d : α} (hcb : c ≤ b) (had : a ≤ d) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico' hcb had] lemma Ico_union_Ico {a b c d : α} (h₁ : min a b ≤ max c d) (h₂ : min c d ≤ max a b) : Ico a b ∪ Ico c d = Ico (min a c) (max b d) := by rw [←coe_inj, coe_union, coe_Ico, coe_Ico, coe_Ico, set.Ico_union_Ico h₁ h₂] lemma Ico_inter_Ico {a b c d : α} : Ico a b ∩ Ico c d = Ico (max a c) (min b d) := by rw [←coe_inj, coe_inter, coe_Ico, coe_Ico, coe_Ico, ←inf_eq_min, ←sup_eq_max, set.Ico_inter_Ico] @[simp] lemma Ico_filter_lt (a b c : α) : (Ico a b).filter (λ x, x < c) = Ico a (min b c) := begin cases le_total b c, { rw [Ico_filter_lt_of_right_le h, min_eq_left h] }, { rw [Ico_filter_lt_of_le_right h, min_eq_right h] } end @[simp] lemma Ico_filter_le (a b c : α) : (Ico a b).filter (λ x, c ≤ x) = Ico (max a c) b := begin cases le_total a c, { rw [Ico_filter_le_of_left_le h, max_eq_right h] }, { rw [Ico_filter_le_of_le_left h, max_eq_left h] } end @[simp] lemma Ioo_filter_lt (a b c : α) : (Ioo a b).filter (< c) = Ioo a (min b c) := by { ext, simp [and_assoc] } @[simp] lemma Iio_filter_lt {α} [linear_order α] [locally_finite_order_bot α] (a b : α) : (Iio a).filter (< b) = Iio (min a b) := by { ext, simp [and_assoc] } @[simp] lemma Ico_diff_Ico_left (a b c : α) : (Ico a b) \ (Ico a c) = Ico (max a c) b := begin cases le_total a c, { ext x, rw [mem_sdiff, mem_Ico, mem_Ico, mem_Ico, max_eq_right h, and.right_comm, not_and, not_lt], exact and_congr_left' ⟨λ hx, hx.2 hx.1, λ hx, ⟨h.trans hx, λ _, hx⟩⟩ }, { rw [Ico_eq_empty_of_le h, sdiff_empty, max_eq_left h] } end @[simp] lemma Ico_diff_Ico_right (a b c : α) : (Ico a b) \ (Ico c b) = Ico a (min b c) := begin cases le_total b c, { rw [Ico_eq_empty_of_le h, sdiff_empty, min_eq_left h] }, { ext x, rw [mem_sdiff, mem_Ico, mem_Ico, mem_Ico, min_eq_right h, and_assoc, not_and', not_le], exact and_congr_right' ⟨λ hx, hx.2 hx.1, λ hx, ⟨hx.trans_le h, λ _, hx⟩⟩ } end end locally_finite_order variables [fintype α] [locally_finite_order_top α] [locally_finite_order_bot α] lemma Ioi_disj_union_Iio (a : α) : (Ioi a).disj_union (Iio a) (disjoint_left.1 $ disjoint_Ioi_Iio a) = ({a} : finset α)ᶜ := by { ext, simp [eq_comm] } end linear_order section ordered_cancel_add_comm_monoid variables [ordered_cancel_add_comm_monoid α] [has_exists_add_of_le α] [decidable_eq α] [locally_finite_order α] lemma image_add_left_Icc (a b c : α) : (Icc a b).image ((+) c) = Icc (c + a) (c + b) := begin ext x, rw [mem_image, mem_Icc], split, { rintro ⟨y, hy, rfl⟩, rw mem_Icc at hy, exact ⟨add_le_add_left hy.1 c, add_le_add_left hy.2 c⟩ }, { intro hx, obtain ⟨y, hy⟩ := exists_add_of_le hx.1, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Icc.2 ⟨le_of_add_le_add_left hx.1, le_of_add_le_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ico (a b c : α) : (Ico a b).image ((+) c) = Ico (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ico], split, { rintro ⟨y, hy, rfl⟩, rw mem_Ico at hy, exact ⟨add_le_add_left hy.1 c, add_lt_add_left hy.2 c⟩ }, { intro hx, obtain ⟨y, hy⟩ := exists_add_of_le hx.1, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ico.2 ⟨le_of_add_le_add_left hx.1, lt_of_add_lt_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ioc (a b c : α) : (Ioc a b).image ((+) c) = Ioc (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ioc], refine ⟨_, λ hx, _⟩, { rintro ⟨y, hy, rfl⟩, rw mem_Ioc at hy, exact ⟨add_lt_add_left hy.1 c, add_le_add_left hy.2 c⟩ }, { obtain ⟨y, hy⟩ := exists_add_of_le hx.1.le, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ioc.2 ⟨lt_of_add_lt_add_left hx.1, le_of_add_le_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_left_Ioo (a b c : α) : (Ioo a b).image ((+) c) = Ioo (c + a) (c + b) := begin ext x, rw [mem_image, mem_Ioo], refine ⟨_, λ hx, _⟩, { rintro ⟨y, hy, rfl⟩, rw mem_Ioo at hy, exact ⟨add_lt_add_left hy.1 c, add_lt_add_left hy.2 c⟩ }, { obtain ⟨y, hy⟩ := exists_add_of_le hx.1.le, rw add_assoc at hy, rw hy at hx, exact ⟨a + y, mem_Ioo.2 ⟨lt_of_add_lt_add_left hx.1, lt_of_add_lt_add_left hx.2⟩, hy.symm⟩ } end lemma image_add_right_Icc (a b c : α) : (Icc a b).image (+ c) = Icc (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Icc a b c } lemma image_add_right_Ico (a b c : α) : (Ico a b).image (+ c) = Ico (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ico a b c } lemma image_add_right_Ioc (a b c : α) : (Ioc a b).image (+ c) = Ioc (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ioc a b c } lemma image_add_right_Ioo (a b c : α) : (Ioo a b).image (+ c) = Ioo (a + c) (b + c) := by { simp_rw add_comm _ c, exact image_add_left_Ioo a b c } end ordered_cancel_add_comm_monoid @[to_additive] lemma prod_prod_Ioi_mul_eq_prod_prod_off_diag [fintype ι] [linear_order ι] [locally_finite_order_top ι] [locally_finite_order_bot ι] [comm_monoid α] (f : ι → ι → α) : ∏ i, ∏ j in Ioi i, f j i * f i j = ∏ i, ∏ j in {i}ᶜ, f j i := begin simp_rw [←Ioi_disj_union_Iio, prod_disj_union, prod_mul_distrib], congr' 1, rw [prod_sigma', prod_sigma'], refine prod_bij' (λ i hi, ⟨i.2, i.1⟩) _ _ (λ i hi, ⟨i.2, i.1⟩) _ _ _; simp, end end finset
004c1ee6281afa4430006e5f9888a06a35367ca1
ff5230333a701471f46c57e8c115a073ebaaa448
/library/init/algebra/group.lean
44d387d83935c35cf46342ed3c86f957c347bec6
[ "Apache-2.0" ]
permissive
stanford-cs242/lean
f81721d2b5d00bc175f2e58c57b710d465e6c858
7bd861261f4a37326dcf8d7a17f1f1f330e4548c
refs/heads/master
1,600,957,431,849
1,576,465,093,000
1,576,465,093,000
225,779,423
0
3
Apache-2.0
1,575,433,936,000
1,575,433,935,000
null
UTF-8
Lean
false
false
16,403
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jeremy Avigad, Leonardo de Moura -/ prelude import init.logic init.algebra.classes init.meta init.meta.decl_cmds init.meta.smt.rsimp /- Make sure instances defined in this file have lower priority than the ones defined for concrete structures -/ set_option default_priority 100 set_option old_structure_cmd true universe u variables {α : Type u} class semigroup (α : Type u) extends has_mul α := (mul_assoc : ∀ a b c : α, a * b * c = a * (b * c)) class comm_semigroup (α : Type u) extends semigroup α := (mul_comm : ∀ a b : α, a * b = b * a) class left_cancel_semigroup (α : Type u) extends semigroup α := (mul_left_cancel : ∀ a b c : α, a * b = a * c → b = c) class right_cancel_semigroup (α : Type u) extends semigroup α := (mul_right_cancel : ∀ a b c : α, a * b = c * b → a = c) class monoid (α : Type u) extends semigroup α, has_one α := (one_mul : ∀ a : α, 1 * a = a) (mul_one : ∀ a : α, a * 1 = a) class comm_monoid (α : Type u) extends monoid α, comm_semigroup α class group (α : Type u) extends monoid α, has_inv α := (mul_left_inv : ∀ a : α, a⁻¹ * a = 1) class comm_group (α : Type u) extends group α, comm_monoid α lemma mul_assoc [semigroup α] : ∀ a b c : α, a * b * c = a * (b * c) := semigroup.mul_assoc instance semigroup_to_is_associative [semigroup α] : is_associative α (*) := ⟨mul_assoc⟩ lemma mul_comm [comm_semigroup α] : ∀ a b : α, a * b = b * a := comm_semigroup.mul_comm instance comm_semigroup_to_is_commutative [comm_semigroup α] : is_commutative α (*) := ⟨mul_comm⟩ lemma mul_left_comm [comm_semigroup α] : ∀ a b c : α, a * (b * c) = b * (a * c) := left_comm has_mul.mul mul_comm mul_assoc local attribute [simp] mul_comm mul_assoc mul_left_comm lemma mul_right_comm [comm_semigroup α] : ∀ a b c : α, a * b * c = a * c * b := right_comm has_mul.mul mul_comm mul_assoc lemma mul_left_cancel [left_cancel_semigroup α] {a b c : α} : a * b = a * c → b = c := left_cancel_semigroup.mul_left_cancel a b c lemma mul_right_cancel [right_cancel_semigroup α] {a b c : α} : a * b = c * b → a = c := right_cancel_semigroup.mul_right_cancel a b c lemma mul_left_cancel_iff [left_cancel_semigroup α] {a b c : α} : a * b = a * c ↔ b = c := ⟨mul_left_cancel, congr_arg _⟩ lemma mul_right_cancel_iff [right_cancel_semigroup α] {a b c : α} : b * a = c * a ↔ b = c := ⟨mul_right_cancel, congr_arg _⟩ @[simp] lemma one_mul [monoid α] : ∀ a : α, 1 * a = a := monoid.one_mul @[simp] lemma mul_one [monoid α] : ∀ a : α, a * 1 = a := monoid.mul_one @[simp] lemma mul_left_inv [group α] : ∀ a : α, a⁻¹ * a = 1 := group.mul_left_inv def inv_mul_self := @mul_left_inv @[simp] lemma inv_mul_cancel_left [group α] (a b : α) : a⁻¹ * (a * b) = b := by rw [← mul_assoc, mul_left_inv, one_mul] @[simp] lemma inv_mul_cancel_right [group α] (a b : α) : a * b⁻¹ * b = a := by simp @[simp] lemma inv_eq_of_mul_eq_one [group α] {a b : α} (h : a * b = 1) : a⁻¹ = b := by rw [← mul_one a⁻¹, ←h, ←mul_assoc, mul_left_inv, one_mul] @[simp] lemma one_inv [group α] : 1⁻¹ = (1 : α) := inv_eq_of_mul_eq_one (one_mul 1) @[simp] lemma inv_inv [group α] (a : α) : (a⁻¹)⁻¹ = a := inv_eq_of_mul_eq_one (mul_left_inv a) @[simp] lemma mul_right_inv [group α] (a : α) : a * a⁻¹ = 1 := have a⁻¹⁻¹ * a⁻¹ = 1, by rw mul_left_inv, by rwa [inv_inv] at this def mul_inv_self := @mul_right_inv lemma inv_inj [group α] {a b : α} (h : a⁻¹ = b⁻¹) : a = b := have a = a⁻¹⁻¹, by simp, begin rw this, simp [h] end lemma group.mul_left_cancel [group α] {a b c : α} (h : a * b = a * c) : b = c := have a⁻¹ * (a * b) = b, by simp, begin simp [h] at this, rw this end lemma group.mul_right_cancel [group α] {a b c : α} (h : a * b = c * b) : a = c := have a * b * b⁻¹ = a, by simp, begin simp [h] at this, rw this end instance group.to_left_cancel_semigroup [s : group α] : left_cancel_semigroup α := { mul_left_cancel := @group.mul_left_cancel α s, ..s } instance group.to_right_cancel_semigroup [s : group α] : right_cancel_semigroup α := { mul_right_cancel := @group.mul_right_cancel α s, ..s } lemma mul_inv_cancel_left [group α] (a b : α) : a * (a⁻¹ * b) = b := by rw [← mul_assoc, mul_right_inv, one_mul] lemma mul_inv_cancel_right [group α] (a b : α) : a * b * b⁻¹ = a := by rw [mul_assoc, mul_right_inv, mul_one] @[simp] lemma mul_inv_rev [group α] (a b : α) : (a * b)⁻¹ = b⁻¹ * a⁻¹ := inv_eq_of_mul_eq_one begin rw [mul_assoc, ← mul_assoc b, mul_right_inv, one_mul, mul_right_inv] end lemma eq_inv_of_eq_inv [group α] {a b : α} (h : a = b⁻¹) : b = a⁻¹ := by simp [h] lemma eq_inv_of_mul_eq_one [group α] {a b : α} (h : a * b = 1) : a = b⁻¹ := have a⁻¹ = b, from inv_eq_of_mul_eq_one h, by simp [this.symm] lemma eq_mul_inv_of_mul_eq [group α] {a b c : α} (h : a * c = b) : a = b * c⁻¹ := by simp [h.symm] lemma eq_inv_mul_of_mul_eq [group α] {a b c : α} (h : b * a = c) : a = b⁻¹ * c := by simp [h.symm] lemma inv_mul_eq_of_eq_mul [group α] {a b c : α} (h : b = a * c) : a⁻¹ * b = c := by simp [h] lemma mul_inv_eq_of_eq_mul [group α] {a b c : α} (h : a = c * b) : a * b⁻¹ = c := by simp [h] lemma eq_mul_of_mul_inv_eq [group α] {a b c : α} (h : a * c⁻¹ = b) : a = b * c := by simp [h.symm] lemma eq_mul_of_inv_mul_eq [group α] {a b c : α} (h : b⁻¹ * a = c) : a = b * c := by simp [h.symm, mul_inv_cancel_left] lemma mul_eq_of_eq_inv_mul [group α] {a b c : α} (h : b = a⁻¹ * c) : a * b = c := by rw [h, mul_inv_cancel_left] lemma mul_eq_of_eq_mul_inv [group α] {a b c : α} (h : a = c * b⁻¹) : a * b = c := by simp [h] lemma mul_inv [comm_group α] (a b : α) : (a * b)⁻¹ = a⁻¹ * b⁻¹ := by rw [mul_inv_rev, mul_comm] /- αdditive "sister" structures. Example, add_semigroup mirrors semigroup. These structures exist just to help automation. In an alternative design, we could have the binary operation as an extra argument for semigroup, monoid, group, etc. However, the lemmas would be hard to index since they would not contain any constant. For example, mul_assoc would be lemma mul_assoc {α : Type u} {op : α → α → α} [semigroup α op] : ∀ a b c : α, op (op a b) c = op a (op b c) := semigroup.mul_assoc The simplifier cannot effectively use this lemma since the pattern for the left-hand-side would be ?op (?op ?a ?b) ?c Remark: we use a tactic for transporting theorems from the multiplicative fragment to the additive one. -/ class add_semigroup (α : Type u) extends has_add α := (add_assoc : ∀ a b c : α, a + b + c = a + (b + c)) class add_comm_semigroup (α : Type u) extends add_semigroup α := (add_comm : ∀ a b : α, a + b = b + a) class add_left_cancel_semigroup (α : Type u) extends add_semigroup α := (add_left_cancel : ∀ a b c : α, a + b = a + c → b = c) class add_right_cancel_semigroup (α : Type u) extends add_semigroup α := (add_right_cancel : ∀ a b c : α, a + b = c + b → a = c) class add_monoid (α : Type u) extends add_semigroup α, has_zero α := (zero_add : ∀ a : α, 0 + a = a) (add_zero : ∀ a : α, a + 0 = a) class add_comm_monoid (α : Type u) extends add_monoid α, add_comm_semigroup α class add_group (α : Type u) extends add_monoid α, has_neg α := (add_left_neg : ∀ a : α, -a + a = 0) class add_comm_group (α : Type u) extends add_group α, add_comm_monoid α open tactic meta def transport_with_dict (dict : name_map name) (src : name) (tgt : name) : command := copy_decl_using dict src tgt >> copy_attribute `reducible src tt tgt >> copy_attribute `simp src tt tgt >> copy_attribute `instance src tt tgt /- Transport multiplicative to additive -/ meta def transport_multiplicative_to_additive (ls : list (name × name)) : command := let dict := native.rb_map.of_list ls in ls.foldl (λ t ⟨src, tgt⟩, do env ← get_env, if (env.get tgt).to_bool = ff then t >> transport_with_dict dict src tgt else t) skip run_cmd transport_multiplicative_to_additive [/- map operations -/ (`has_mul.mul, `has_add.add), (`has_one.one, `has_zero.zero), (`has_inv.inv, `has_neg.neg), (`has_mul, `has_add), (`has_one, `has_zero), (`has_inv, `has_neg), /- map constructors -/ (`has_mul.mk, `has_add.mk), (`has_one, `has_zero.mk), (`has_inv, `has_neg.mk), /- map structures -/ (`semigroup, `add_semigroup), (`monoid, `add_monoid), (`group, `add_group), (`comm_semigroup, `add_comm_semigroup), (`comm_monoid, `add_comm_monoid), (`comm_group, `add_comm_group), (`left_cancel_semigroup, `add_left_cancel_semigroup), (`right_cancel_semigroup, `add_right_cancel_semigroup), (`left_cancel_semigroup.mk, `add_left_cancel_semigroup.mk), (`right_cancel_semigroup.mk, `add_right_cancel_semigroup.mk), /- map instances -/ (`semigroup.to_has_mul, `add_semigroup.to_has_add), (`monoid.to_has_one, `add_monoid.to_has_zero), (`group.to_has_inv, `add_group.to_has_neg), (`comm_semigroup.to_semigroup, `add_comm_semigroup.to_add_semigroup), (`monoid.to_semigroup, `add_monoid.to_add_semigroup), (`comm_monoid.to_monoid, `add_comm_monoid.to_add_monoid), (`comm_monoid.to_comm_semigroup, `add_comm_monoid.to_add_comm_semigroup), (`group.to_monoid, `add_group.to_add_monoid), (`comm_group.to_group, `add_comm_group.to_add_group), (`comm_group.to_comm_monoid, `add_comm_group.to_add_comm_monoid), (`left_cancel_semigroup.to_semigroup, `add_left_cancel_semigroup.to_add_semigroup), (`right_cancel_semigroup.to_semigroup, `add_right_cancel_semigroup.to_add_semigroup), /- map projections -/ (`semigroup.mul_assoc, `add_semigroup.add_assoc), (`comm_semigroup.mul_comm, `add_comm_semigroup.add_comm), (`left_cancel_semigroup.mul_left_cancel, `add_left_cancel_semigroup.add_left_cancel), (`right_cancel_semigroup.mul_right_cancel, `add_right_cancel_semigroup.add_right_cancel), (`monoid.one_mul, `add_monoid.zero_add), (`monoid.mul_one, `add_monoid.add_zero), (`group.mul_left_inv, `add_group.add_left_neg), (`group.mul, `add_group.add), (`group.mul_assoc, `add_group.add_assoc), /- map lemmas -/ (`mul_assoc, `add_assoc), (`mul_comm, `add_comm), (`mul_left_comm, `add_left_comm), (`mul_right_comm, `add_right_comm), (`one_mul, `zero_add), (`mul_one, `add_zero), (`mul_left_inv, `add_left_neg), (`mul_left_cancel, `add_left_cancel), (`mul_right_cancel, `add_right_cancel), (`mul_left_cancel_iff, `add_left_cancel_iff), (`mul_right_cancel_iff, `add_right_cancel_iff), (`inv_mul_cancel_left, `neg_add_cancel_left), (`inv_mul_cancel_right, `neg_add_cancel_right), (`eq_inv_mul_of_mul_eq, `eq_neg_add_of_add_eq), (`inv_eq_of_mul_eq_one, `neg_eq_of_add_eq_zero), (`inv_inv, `neg_neg), (`mul_right_inv, `add_right_neg), (`mul_inv_cancel_left, `add_neg_cancel_left), (`mul_inv_cancel_right, `add_neg_cancel_right), (`mul_inv_rev, `neg_add_rev), (`mul_inv, `neg_add), (`inv_inj, `neg_inj), (`group.mul_left_cancel, `add_group.add_left_cancel), (`group.mul_right_cancel, `add_group.add_right_cancel), (`group.to_left_cancel_semigroup, `add_group.to_left_cancel_add_semigroup), (`group.to_right_cancel_semigroup, `add_group.to_right_cancel_add_semigroup), (`eq_inv_of_eq_inv, `eq_neg_of_eq_neg), (`eq_inv_of_mul_eq_one, `eq_neg_of_add_eq_zero), (`eq_mul_inv_of_mul_eq, `eq_add_neg_of_add_eq), (`inv_mul_eq_of_eq_mul, `neg_add_eq_of_eq_add), (`mul_inv_eq_of_eq_mul, `add_neg_eq_of_eq_add), (`eq_mul_of_mul_inv_eq, `eq_add_of_add_neg_eq), (`eq_mul_of_inv_mul_eq, `eq_add_of_neg_add_eq), (`mul_eq_of_eq_inv_mul, `add_eq_of_eq_neg_add), (`mul_eq_of_eq_mul_inv, `add_eq_of_eq_add_neg), (`one_inv, `neg_zero) ] instance add_semigroup_to_is_eq_associative [add_semigroup α] : is_associative α (+) := ⟨add_assoc⟩ instance add_comm_semigroup_to_is_eq_commutative [add_comm_semigroup α] : is_commutative α (+) := ⟨add_comm⟩ local attribute [simp] add_assoc add_comm add_left_comm def neg_add_self := @add_left_neg def add_neg_self := @add_right_neg def eq_of_add_eq_add_left := @add_left_cancel def eq_of_add_eq_add_right := @add_right_cancel @[reducible] protected def algebra.sub [add_group α] (a b : α) : α := a + -b instance add_group_has_sub [add_group α] : has_sub α := ⟨algebra.sub⟩ @[simp] lemma sub_eq_add_neg [add_group α] (a b : α) : a - b = a + -b := rfl lemma sub_self [add_group α] (a : α) : a - a = 0 := add_right_neg a lemma sub_add_cancel [add_group α] (a b : α) : a - b + b = a := neg_add_cancel_right a b lemma add_sub_cancel [add_group α] (a b : α) : a + b - b = a := add_neg_cancel_right a b lemma add_sub_assoc [add_group α] (a b c : α) : a + b - c = a + (b - c) := by rw [sub_eq_add_neg, add_assoc, ←sub_eq_add_neg] lemma eq_of_sub_eq_zero [add_group α] {a b : α} (h : a - b = 0) : a = b := have 0 + b = b, by rw zero_add, have (a - b) + b = b, by rwa h, by rwa [sub_eq_add_neg, neg_add_cancel_right] at this lemma sub_eq_zero_of_eq [add_group α] {a b : α} (h : a = b) : a - b = 0 := by rw [h, sub_self] lemma sub_eq_zero_iff_eq [add_group α] {a b : α} : a - b = 0 ↔ a = b := ⟨eq_of_sub_eq_zero, sub_eq_zero_of_eq⟩ lemma zero_sub [add_group α] (a : α) : 0 - a = -a := zero_add (-a) lemma sub_zero [add_group α] (a : α) : a - 0 = a := by rw [sub_eq_add_neg, neg_zero, add_zero] lemma sub_ne_zero_of_ne [add_group α] {a b : α} (h : a ≠ b) : a - b ≠ 0 := begin intro hab, apply h, apply eq_of_sub_eq_zero hab end lemma sub_neg_eq_add [add_group α] (a b : α) : a - (-b) = a + b := by rw [sub_eq_add_neg, neg_neg] lemma neg_sub [add_group α] (a b : α) : -(a - b) = b - a := neg_eq_of_add_eq_zero (by rw [sub_eq_add_neg, sub_eq_add_neg, add_assoc, neg_add_cancel_left, add_right_neg]) lemma add_sub [add_group α] (a b c : α) : a + (b - c) = a + b - c := by simp lemma sub_add_eq_sub_sub_swap [add_group α] (a b c : α) : a - (b + c) = a - c - b := by simp lemma add_sub_add_right_eq_sub [add_group α] (a b c : α) : (a + c) - (b + c) = a - b := by rw [sub_add_eq_sub_sub_swap]; simp lemma eq_sub_of_add_eq [add_group α] {a b c : α} (h : a + c = b) : a = b - c := by simp [h.symm] lemma sub_eq_of_eq_add [add_group α] {a b c : α} (h : a = c + b) : a - b = c := by simp [h] lemma eq_add_of_sub_eq [add_group α] {a b c : α} (h : a - c = b) : a = b + c := by simp [h.symm] lemma add_eq_of_eq_sub [add_group α] {a b c : α} (h : a = c - b) : a + b = c := by simp [h] lemma sub_add_eq_sub_sub [add_comm_group α] (a b c : α) : a - (b + c) = a - b - c := by simp lemma neg_add_eq_sub [add_comm_group α] (a b : α) : -a + b = b - a := by simp lemma sub_add_eq_add_sub [add_comm_group α] (a b c : α) : a - b + c = a + c - b := by simp lemma sub_sub [add_comm_group α] (a b c : α) : a - b - c = a - (b + c) := by simp lemma sub_add [add_comm_group α] (a b c : α) : a - b + c = a - (b - c) := by simp lemma add_sub_add_left_eq_sub [add_comm_group α] (a b c : α) : (c + a) - (c + b) = a - b := by simp lemma eq_sub_of_add_eq' [add_comm_group α] {a b c : α} (h : c + a = b) : a = b - c := by simp [h.symm] lemma sub_eq_of_eq_add' [add_comm_group α] {a b c : α} (h : a = b + c) : a - b = c := begin simp [h], rw [add_left_comm], simp end lemma eq_add_of_sub_eq' [add_comm_group α] {a b c : α} (h : a - b = c) : a = b + c := by simp [h.symm] lemma add_eq_of_eq_sub' [add_comm_group α] {a b c : α} (h : b = c - a) : a + b = c := begin simp [h], rw [add_comm c, add_neg_cancel_left] end lemma sub_sub_self [add_comm_group α] (a b : α) : a - (a - b) = b := begin simp, rw [add_comm b, add_neg_cancel_left] end lemma add_sub_comm [add_comm_group α] (a b c d : α) : a + b - (c + d) = (a - c) + (b - d) := by simp lemma sub_eq_sub_add_sub [add_comm_group α] (a b c : α) : a - b = c - b + (a - c) := begin simp, rw [add_left_comm c], simp end lemma neg_neg_sub_neg [add_comm_group α] (a b : α) : - (-a - -b) = a - b := by simp /- The following lemmas generate too many instances for rsimp -/ attribute [no_rsimp] mul_assoc mul_comm mul_left_comm add_assoc add_comm add_left_comm
212f59515ab153913390bb181963f3d850dcf883
35677d2df3f081738fa6b08138e03ee36bc33cad
/src/topology/metric_space/isometry.lean
798827edf28504b2fdb58d0df7f22b5a72241dc7
[ "Apache-2.0" ]
permissive
gebner/mathlib
eab0150cc4f79ec45d2016a8c21750244a2e7ff0
cc6a6edc397c55118df62831e23bfbd6e6c6b4ab
refs/heads/master
1,625,574,853,976
1,586,712,827,000
1,586,712,827,000
99,101,412
1
0
Apache-2.0
1,586,716,389,000
1,501,667,958,000
Lean
UTF-8
Lean
false
false
14,832
lean
/- Copyright (c) 2018 Sébastien Gouëzel. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Isometries of emetric and metric spaces Authors: Sébastien Gouëzel -/ import topology.metric_space.basic topology.bounded_continuous_function analysis.normed_space.basic topology.opens /-! # Isometries We define isometries, i.e., maps between emetric spaces that preserve the edistance (on metric spaces, these are exactly the maps that preserve distances), and prove their basic properties. We also introduce isometric bijections. -/ noncomputable theory universes u v w variables {α : Type u} {β : Type v} {γ : Type w} open function set /-- An isometry (also known as isometric embedding) is a map preserving the edistance between emetric spaces, or equivalently the distance between metric space. -/ def isometry [emetric_space α] [emetric_space β] (f : α → β) : Prop := ∀x1 x2 : α, edist (f x1) (f x2) = edist x1 x2 /-- On metric spaces, a map is an isometry if and only if it preserves distances. -/ lemma isometry_emetric_iff_metric [metric_space α] [metric_space β] {f : α → β} : isometry f ↔ (∀x y, dist (f x) (f y) = dist x y) := ⟨assume H x y, by simp [dist_edist, H x y], assume H x y, by simp [edist_dist, H x y]⟩ /-- An isometry preserves edistances. -/ theorem isometry.edist_eq [emetric_space α] [emetric_space β] {f : α → β} {x y : α} (hf : isometry f) : edist (f x) (f y) = edist x y := hf x y /-- An isometry preserves distances. -/ theorem isometry.dist_eq [metric_space α] [metric_space β] {f : α → β} {x y : α} (hf : isometry f) : dist (f x) (f y) = dist x y := by rw [dist_edist, dist_edist, hf] section emetric_isometry variables [emetric_space α] [emetric_space β] [emetric_space γ] variables {f : α → β} {x y z : α} {s : set α} lemma isometry.lipschitz (h : isometry f) : lipschitz_with 1 f := lipschitz_with.of_edist_le $ λ x y, le_of_eq (h x y) lemma isometry.antilipschitz (h : isometry f) : antilipschitz_with 1 f := λ x y, by simp only [h x y, ennreal.coe_one, one_mul, le_refl] /-- An isometry is injective -/ lemma isometry.injective (h : isometry f) : injective f := h.antilipschitz.injective /-- Any map on a subsingleton is an isometry -/ theorem isometry_subsingleton [subsingleton α] : isometry f := λx y, by rw subsingleton.elim x y; simp /-- The identity is an isometry -/ lemma isometry_id : isometry (id : α → α) := λx y, rfl /-- The composition of isometries is an isometry -/ theorem isometry.comp {g : β → γ} {f : α → β} (hg : isometry g) (hf : isometry f) : isometry (g ∘ f) := assume x y, calc edist ((g ∘ f) x) ((g ∘ f) y) = edist (f x) (f y) : hg _ _ ... = edist x y : hf _ _ /-- An isometry is an embedding -/ theorem isometry.uniform_embedding (hf : isometry f) : uniform_embedding f := hf.antilipschitz.uniform_embedding hf.lipschitz.uniform_continuous /-- An isometry is continuous. -/ lemma isometry.continuous (hf : isometry f) : continuous f := hf.lipschitz.continuous /-- The inverse of an isometry is an isometry. -/ lemma isometry.inv (e : α ≃ β) (h : isometry e.to_fun) : isometry e.inv_fun := λx y, by rw [← h, e.right_inv _, e.right_inv _] /-- Isometries preserve the diameter in emetric spaces. -/ lemma isometry.ediam_image (hf : isometry f) (s : set α) : emetric.diam (f '' s) = emetric.diam s := eq_of_forall_ge_iff $ λ d, by simp only [emetric.diam_le_iff_forall_edist_le, ball_image_iff, hf.edist_eq] lemma isometry.ediam_range (hf : isometry f) : emetric.diam (range f) = emetric.diam (univ : set α) := by { rw ← image_univ, exact hf.ediam_image univ } /-- The injection from a subtype is an isometry -/ lemma isometry_subtype_val {s : set α} : isometry (subtype.val : s → α) := λx y, rfl end emetric_isometry --section /-- An isometry preserves the diameter in metric spaces. -/ lemma isometry.diam_image [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) (s : set α) : metric.diam (f '' s) = metric.diam s := by rw [metric.diam, metric.diam, hf.ediam_image] lemma isometry.diam_range [metric_space α] [metric_space β] {f : α → β} (hf : isometry f) : metric.diam (range f) = metric.diam (univ : set α) := by { rw ← image_univ, exact hf.diam_image univ } /-- `α` and `β` are isometric if there is an isometric bijection between them. -/ structure isometric (α : Type*) (β : Type*) [emetric_space α] [emetric_space β] extends α ≃ β := (isometry_to_fun : isometry to_fun) infix ` ≃ᵢ `:25 := isometric namespace isometric variables [emetric_space α] [emetric_space β] [emetric_space γ] instance : has_coe_to_fun (α ≃ᵢ β) := ⟨λ_, α → β, λe, e.to_equiv⟩ lemma coe_eq_to_equiv (h : α ≃ᵢ β) (a : α) : h a = h.to_equiv a := rfl lemma isometry_inv_fun (h : α ≃ᵢ β) : isometry h.to_equiv.symm := h.isometry_to_fun.inv h.to_equiv @[ext] lemma ext : ∀ ⦃h₁ h₂ : α ≃ᵢ β⦄, (∀ x, h₁ x = h₂ x) → h₁ = h₂ | ⟨e₁, h₁⟩ ⟨e₂, h₂⟩ H := have e₁ = e₂ := equiv.ext _ _ H, by subst e₁ /-- Alternative constructor for isometric bijections, taking as input an isometry, and a right inverse. -/ def mk' (f : α → β) (g : β → α) (hfg : ∀ x, f (g x) = x) (hf : isometry f) : α ≃ᵢ β := { to_fun := f, inv_fun := g, left_inv := λ x, hf.injective $ hfg _, right_inv := hfg, isometry_to_fun := hf } section normed_group variables {G : Type*} [normed_group G] /-- Addition `y ↦ y + x` as an `isometry`. -/ protected def add_right (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_right _ _ _, .. equiv.add_right x } /-- Addition `y ↦ x + y` as an `isometry`. -/ protected def add_left (x : G) : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ y z, dist_add_left _ _ _, .. equiv.add_left x } variable (G) /-- Negation `x ↦ -x` as an `isometry`. -/ protected def neg : G ≃ᵢ G := { isometry_to_fun := isometry_emetric_iff_metric.2 $ λ x y, dist_neg_neg _ _, .. equiv.neg G } end normed_group /-- The (bundled) homeomorphism associated to an isometric isomorphism. -/ protected def to_homeomorph (h : α ≃ᵢ β) : α ≃ₜ β := { continuous_to_fun := (isometry_to_fun h).continuous, continuous_inv_fun := (isometry_inv_fun h).continuous, .. h.to_equiv } lemma coe_eq_to_homeomorph (h : α ≃ᵢ β) (a : α) : h a = h.to_homeomorph a := rfl lemma to_homeomorph_to_equiv (h : α ≃ᵢ β) : h.to_homeomorph.to_equiv = h.to_equiv := by ext; refl /-- The identity isometry of a space. -/ protected def refl (α : Type*) [emetric_space α] : α ≃ᵢ α := { isometry_to_fun := isometry_id, .. equiv.refl α } /-- The composition of two isometric isomorphisms, as an isometric isomorphism. -/ protected def trans (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) : α ≃ᵢ γ := { isometry_to_fun := h₂.isometry_to_fun.comp h₁.isometry_to_fun, .. equiv.trans h₁.to_equiv h₂.to_equiv } @[simp] lemma trans_apply (h₁ : α ≃ᵢ β) (h₂ : β ≃ᵢ γ) (x : α) : h₁.trans h₂ x = h₂ (h₁ x) := rfl /-- The inverse of an isometric isomorphism, as an isometric isomorphism. -/ protected def symm (h : α ≃ᵢ β) : β ≃ᵢ α := { isometry_to_fun := h.isometry_inv_fun, .. h.to_equiv.symm } protected lemma isometry (h : α ≃ᵢ β) : isometry h := h.isometry_to_fun protected lemma continuous (h : α ≃ᵢ β) : continuous h := h.isometry.continuous @[simp] lemma apply_symm_apply (h : α ≃ᵢ β) (y : β) : h (h.symm y) = y := h.to_equiv.apply_symm_apply y @[simp] lemma symm_apply_apply (h : α ≃ᵢ β) (x : α) : h.symm (h x) = x := h.to_equiv.symm_apply_apply x lemma symm_apply_eq (h : α ≃ᵢ β) {x : α} {y : β} : h.symm y = x ↔ y = h x := h.to_equiv.symm_apply_eq lemma eq_symm_apply (h : α ≃ᵢ β) {x : α} {y : β} : x = h.symm y ↔ h x = y := h.to_equiv.eq_symm_apply lemma symm_comp_self (h : α ≃ᵢ β) : ⇑h.symm ∘ ⇑h = id := funext $ assume a, h.to_equiv.left_inv a lemma self_comp_symm (h : α ≃ᵢ β) : ⇑h ∘ ⇑h.symm = id := funext $ assume a, h.to_equiv.right_inv a lemma range_coe (h : α ≃ᵢ β) : range h = univ := eq_univ_of_forall $ assume b, ⟨h.symm b, congr_fun h.self_comp_symm b⟩ lemma image_symm (h : α ≃ᵢ β) : image h.symm = preimage h := image_eq_preimage_of_inverse h.symm.to_equiv.left_inv h.symm.to_equiv.right_inv lemma preimage_symm (h : α ≃ᵢ β) : preimage h.symm = image h := (image_eq_preimage_of_inverse h.to_equiv.left_inv h.to_equiv.right_inv).symm end isometric /-- An isometry induces an isometric isomorphism between the source space and the range of the isometry. -/ def isometry.isometric_on_range [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) : α ≃ᵢ range f := { isometry_to_fun := λx y, begin change edist ((equiv.set.range f _) x) ((equiv.set.range f _) y) = edist x y, rw [equiv.set.range_apply f h.injective, equiv.set.range_apply f h.injective], exact h x y end, .. equiv.set.range f h.injective } lemma isometry.isometric_on_range_apply [emetric_space α] [emetric_space β] {f : α → β} (h : isometry f) (x : α) : h.isometric_on_range x = ⟨f x, mem_range_self _⟩ := begin dunfold isometry.isometric_on_range, rw ← equiv.set.range_apply f h.injective x, refl end /-- In a normed algebra, the inclusion of the base field in the extended field is an isometry. -/ lemma algebra_map_isometry (𝕜 : Type*) (𝕜' : Type*) [normed_field 𝕜] [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] : isometry (algebra_map 𝕜 𝕜') := begin refine isometry_emetric_iff_metric.2 (λx y, _), rw [dist_eq_norm, dist_eq_norm, ← ring_hom.map_sub, norm_algebra_map_eq], end /-- The space of bounded sequences, with its sup norm -/ @[reducible] def ℓ_infty_ℝ : Type := bounded_continuous_function ℕ ℝ open bounded_continuous_function metric topological_space namespace Kuratowski_embedding /-! ### In this section, we show that any separable metric space can be embedded isometrically in ℓ^∞(ℝ) -/ variables {f g : ℓ_infty_ℝ} {n : ℕ} {C : ℝ} [metric_space α] (x : ℕ → α) (a b : α) /-- A metric space can be embedded in `l^∞(ℝ)` via the distances to points in a fixed countable set, if this set is dense. This map is given in the next definition, without density assumptions. -/ def embedding_of_subset : ℓ_infty_ℝ := of_normed_group_discrete (λn, dist a (x n) - dist (x 0) (x n)) (dist a (x 0)) (λ_, abs_dist_sub_le _ _ _) lemma embedding_of_subset_coe : embedding_of_subset x a n = dist a (x n) - dist (x 0) (x n) := rfl /-- The embedding map is always a semi-contraction. -/ lemma embedding_of_subset_dist_le (a b : α) : dist (embedding_of_subset x a) (embedding_of_subset x b) ≤ dist a b := begin refine (dist_le dist_nonneg).2 (λn, _), have A : dist a (x n) + (dist (x 0) (x n) + (-dist b (x n) + -dist (x 0) (x n))) = dist a (x n) - dist b (x n), by ring, simp only [embedding_of_subset_coe, real.dist_eq, A, add_comm, neg_add_rev, _root_.neg_neg, sub_eq_add_neg, add_left_comm], exact abs_dist_sub_le _ _ _ end /-- When the reference set is dense, the embedding map is an isometry on its image. -/ lemma embedding_of_subset_isometry (H : closure (range x) = univ) : isometry (embedding_of_subset x) := begin refine isometry_emetric_iff_metric.2 (λa b, _), refine le_antisymm (embedding_of_subset_dist_le x a b) (real.le_of_forall_epsilon_le (λe epos, _)), /- First step: find n with dist a (x n) < e -/ have A : a ∈ closure (range x), by { have B := mem_univ a, rwa [← H] at B }, rcases metric.mem_closure_range_iff.1 A (e/2) (half_pos epos) with ⟨n, hn⟩, /- Second step: use the norm control at index n to conclude -/ have C : dist b (x n) - dist a (x n) = embedding_of_subset x b n - embedding_of_subset x a n := by { simp [embedding_of_subset_coe, sub_eq_add_neg] }, have := calc dist a b ≤ dist a (x n) + dist (x n) b : dist_triangle _ _ _ ... = 2 * dist a (x n) + (dist b (x n) - dist a (x n)) : by { simp [dist_comm], ring } ... ≤ 2 * dist a (x n) + abs (dist b (x n) - dist a (x n)) : by apply_rules [add_le_add_left, le_abs_self] ... ≤ 2 * (e/2) + abs (embedding_of_subset x b n - embedding_of_subset x a n) : begin rw [C], apply_rules [add_le_add, mul_le_mul_of_nonneg_left, le_of_lt hn, le_refl], norm_num end ... ≤ 2 * (e/2) + dist (embedding_of_subset x b) (embedding_of_subset x a) : begin rw [← coe_diff], apply add_le_add_left, rw [coe_diff, ←real.dist_eq], apply dist_coe_le_dist end ... = dist (embedding_of_subset x b) (embedding_of_subset x a) + e : by ring, simpa [dist_comm] using this end /-- Every separable metric space embeds isometrically in ℓ_infty_ℝ. -/ theorem exists_isometric_embedding (α : Type u) [metric_space α] [separable_space α] : ∃(f : α → ℓ_infty_ℝ), isometry f := begin cases (univ : set α).eq_empty_or_nonempty with h h, { use (λ_, 0), assume x, exact absurd h (nonempty.ne_empty ⟨x, mem_univ x⟩) }, { /- We construct a map x : ℕ → α with dense image -/ rcases h with basepoint, haveI : inhabited α := ⟨basepoint⟩, have : ∃s:set α, countable s ∧ closure s = univ := separable_space.exists_countable_closure_eq_univ, rcases this with ⟨S, ⟨S_countable, S_dense⟩⟩, rcases countable_iff_exists_surjective.1 S_countable with ⟨x, x_range⟩, have : closure (range x) = univ := univ_subset_iff.1 (by { rw [← S_dense], apply closure_mono, assumption }), /- Use embedding_of_subset to construct the desired isometry -/ exact ⟨embedding_of_subset x, embedding_of_subset_isometry x this⟩ } end end Kuratowski_embedding open topological_space Kuratowski_embedding /-- The Kuratowski embedding is an isometric embedding of a separable metric space in ℓ^∞(ℝ) -/ def Kuratowski_embedding (α : Type u) [metric_space α] [separable_space α] : α → ℓ_infty_ℝ := classical.some (Kuratowski_embedding.exists_isometric_embedding α) /-- The Kuratowski embedding is an isometry -/ protected lemma Kuratowski_embedding.isometry (α : Type u) [metric_space α] [separable_space α] : isometry (Kuratowski_embedding α) := classical.some_spec (exists_isometric_embedding α) /-- Version of the Kuratowski embedding for nonempty compacts -/ def nonempty_compacts.Kuratowski_embedding (α : Type u) [metric_space α] [compact_space α] [nonempty α] : nonempty_compacts ℓ_infty_ℝ := ⟨range (Kuratowski_embedding α), range_nonempty _, compact_range (Kuratowski_embedding.isometry α).continuous⟩
f4349051bb0b7d04b735d11079016d3c9d7b6a1d
95dcf8dea2baf2b4b0a60d438f27c35ae3dd3990
/src/tactic/basic.lean
2dd5944008afd06df6dfabe6e170c700bdbdf72d
[ "Apache-2.0" ]
permissive
uniformity1/mathlib
829341bad9dfa6d6be9adaacb8086a8a492e85a4
dd0e9bd8f2e5ec267f68e72336f6973311909105
refs/heads/master
1,588,592,015,670
1,554,219,842,000
1,554,219,842,000
179,110,702
0
0
Apache-2.0
1,554,220,076,000
1,554,220,076,000
null
UTF-8
Lean
false
false
33,718
lean
/- Copyright (c) 2018 Mario Carneiro. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Mario Carneiro, Simon Hudon, Scott Morrison, Keeley Hoek -/ import data.dlist.basic category.basic meta.expr meta.rb_map namespace expr open tactic attribute [derive has_reflect] binder_info protected meta def of_nat (α : expr) : ℕ → tactic expr := nat.binary_rec (tactic.mk_mapp ``has_zero.zero [some α, none]) (λ b n tac, if n = 0 then mk_mapp ``has_one.one [some α, none] else do e ← tac, tactic.mk_app (cond b ``bit1 ``bit0) [e]) protected meta def of_int (α : expr) : ℤ → tactic expr | (n : ℕ) := expr.of_nat α n | -[1+ n] := do e ← expr.of_nat α (n+1), tactic.mk_app ``has_neg.neg [e] /- only traverses the direct descendents -/ meta def {u} traverse {m : Type → Type u} [applicative m] {elab elab' : bool} (f : expr elab → m (expr elab')) : expr elab → m (expr elab') | (var v) := pure $ var v | (sort l) := pure $ sort l | (const n ls) := pure $ const n ls | (mvar n n' e) := mvar n n' <$> f e | (local_const n n' bi e) := local_const n n' bi <$> f e | (app e₀ e₁) := app <$> f e₀ <*> f e₁ | (lam n bi e₀ e₁) := lam n bi <$> f e₀ <*> f e₁ | (pi n bi e₀ e₁) := pi n bi <$> f e₀ <*> f e₁ | (elet n e₀ e₁ e₂) := elet n <$> f e₀ <*> f e₁ <*> f e₂ | (macro mac es) := macro mac <$> list.traverse f es meta def mfoldl {α : Type} {m} [monad m] (f : α → expr → m α) : α → expr → m α | x e := prod.snd <$> (state_t.run (e.traverse $ λ e', (get >>= monad_lift ∘ flip f e' >>= put) $> e') x : m _) end expr namespace interaction_monad open result meta def get_result {σ α} (tac : interaction_monad σ α) : interaction_monad σ (interaction_monad.result σ α) | s := match tac s with | r@(success _ s') := success r s' | r@(exception _ _ s') := success r s' end end interaction_monad namespace lean.parser open lean interaction_monad.result meta def of_tactic' {α} (tac : tactic α) : parser α := do r ← of_tactic (interaction_monad.get_result tac), match r with | (success a _) := return a | (exception f pos _) := exception f pos end -- Override the builtin `lean.parser.of_tactic` coe, which is broken. -- (See test/tactics.lean for a failure case.) @[priority 2000] meta instance has_coe' {α} : has_coe (tactic α) (parser α) := ⟨of_tactic'⟩ meta def emit_command_here (str : string) : lean.parser string := do (_, left) ← with_input command_like str, return left -- Emit a source code string at the location being parsed. meta def emit_code_here : string → lean.parser unit | str := do left ← emit_command_here str, if left.length = 0 then return () else emit_code_here left end lean.parser namespace name meta def head : name → string | (mk_string s anonymous) := s | (mk_string s p) := head p | (mk_numeral n p) := head p | anonymous := "[anonymous]" meta def is_private (n : name) : bool := n.head = "_private" meta def last : name → string | (mk_string s _) := s | (mk_numeral n _) := repr n | anonymous := "[anonymous]" meta def length : name → ℕ | (mk_string s anonymous) := s.length | (mk_string s p) := s.length + 1 + p.length | (mk_numeral n p) := p.length | anonymous := "[anonymous]".length end name namespace environment meta def decl_filter_map {α : Type} (e : environment) (f : declaration → option α) : list α := e.fold [] $ λ d l, match f d with | some r := r :: l | none := l end meta def decl_map {α : Type} (e : environment) (f : declaration → α) : list α := e.decl_filter_map $ λ d, some (f d) meta def get_decls (e : environment) : list declaration := e.decl_map id meta def get_trusted_decls (e : environment) : list declaration := e.decl_filter_map (λ d, if d.is_trusted then some d else none) meta def get_decl_names (e : environment) : list name := e.decl_map declaration.to_name end environment namespace format meta def intercalate (x : format) : list format → format := format.join ∘ list.intersperse x end format namespace tactic meta def eval_expr' (α : Type*) [_inst_1 : reflected α] (e : expr) : tactic α := mk_app ``id [e] >>= eval_expr α -- `mk_fresh_name` returns identifiers starting with underscores, -- which are not legal when emitted by tactic programs. Turn the -- useful source of random names provided by `mk_fresh_name` into -- names which are usable by tactic programs. -- -- The returned name has four components. meta def mk_user_fresh_name : tactic name := do nm ← mk_fresh_name, return $ `user__ ++ nm.pop_prefix.sanitize_name ++ `user__ meta def is_simp_lemma : name → tactic bool := succeeds ∘ tactic.has_attribute `simp meta def local_decls : tactic (name_map declaration) := do e ← tactic.get_env, let xs := e.fold native.mk_rb_map (λ d s, if environment.in_current_file' e d.to_name then s.insert d.to_name d else s), pure xs meta def simp_lemmas_from_file : tactic name_set := do s ← local_decls, let s := s.map (expr.list_constant ∘ declaration.value), xs ← s.to_list.mmap ((<$>) name_set.of_list ∘ mfilter tactic.is_simp_lemma ∘ name_set.to_list ∘ prod.snd), return $ name_set.filter (λ x, ¬ s.contains x) (xs.foldl name_set.union mk_name_set) meta def file_simp_attribute_decl (attr : name) : tactic unit := do s ← simp_lemmas_from_file, trace format!"run_cmd mk_simp_attr `{attr}", let lmms := format.join $ list.intersperse " " $ s.to_list.map to_fmt, trace format!"local attribute [{attr}] {lmms}" meta def mk_local (n : name) : expr := expr.local_const n n binder_info.default (expr.const n []) meta def local_def_value (e : expr) : tactic expr := do do (v,_) ← solve_aux `(true) (do (expr.elet n t v _) ← (revert e >> target) | fail format!"{e} is not a local definition", return v), return v meta def check_defn (n : name) (e : pexpr) : tactic unit := do (declaration.defn _ _ _ d _ _) ← get_decl n, e' ← to_expr e, guard (d =ₐ e') <|> trace d >> failed -- meta def compile_eqn (n : name) (univ : list name) (args : list expr) (val : expr) (num : ℕ) : tactic unit := -- do let lhs := (expr.const n $ univ.map level.param).mk_app args, -- stmt ← mk_app `eq [lhs,val], -- let vs := stmt.list_local_const, -- let stmt := stmt.pis vs, -- (_,pr) ← solve_aux stmt (tactic.intros >> reflexivity), -- add_decl $ declaration.thm (n <.> "equations" <.> to_string (format!"_eqn_{num}")) univ stmt (pure pr) meta def to_implicit : expr → expr | (expr.local_const uniq n bi t) := expr.local_const uniq n binder_info.implicit t | e := e meta def pis : list expr → expr → tactic expr | (e@(expr.local_const uniq pp info _) :: es) f := do t ← infer_type e, f' ← pis es f, pure $ expr.pi pp info t (expr.abstract_local f' uniq) | _ f := pure f meta def lambdas : list expr → expr → tactic expr | (e@(expr.local_const uniq pp info _) :: es) f := do t ← infer_type e, f' ← lambdas es f, pure $ expr.lam pp info t (expr.abstract_local f' uniq) | _ f := pure f meta def extract_def (n : name) (trusted : bool) (elab_def : tactic unit) : tactic unit := do cxt ← list.map to_implicit <$> local_context, t ← target, (eqns,d) ← solve_aux t elab_def, d ← instantiate_mvars d, t' ← pis cxt t, d' ← lambdas cxt d, let univ := t'.collect_univ_params, add_decl $ declaration.defn n univ t' d' (reducibility_hints.regular 1 tt) trusted, applyc n meta def exact_dec_trivial : tactic unit := `[exact dec_trivial] /-- Runs a tactic for a result, reverting the state after completion -/ meta def retrieve {α} (tac : tactic α) : tactic α := λ s, result.cases_on (tac s) (λ a s', result.success a s) result.exception /-- Repeat a tactic at least once, calling it recursively on all subgoals, until it fails. This tactic fails if the first invocation fails. -/ meta def repeat1 (t : tactic unit) : tactic unit := t; repeat t /-- `iterate_range m n t`: Repeat the given tactic at least `m` times and at most `n` times or until `t` fails. Fails if `t` does not run at least m times. -/ meta def iterate_range : ℕ → ℕ → tactic unit → tactic unit | 0 0 t := skip | 0 (n+1) t := try (t >> iterate_range 0 n t) | (m+1) n t := t >> iterate_range m (n-1) t meta def replace_at (tac : expr → tactic (expr × expr)) (hs : list expr) (tgt : bool) : tactic bool := do to_remove ← hs.mfilter $ λ h, do { h_type ← infer_type h, succeeds $ do (new_h_type, pr) ← tac h_type, assert h.local_pp_name new_h_type, mk_eq_mp pr h >>= tactic.exact }, goal_simplified ← succeeds $ do { guard tgt, (new_t, pr) ← target >>= tac, replace_target new_t pr }, to_remove.mmap' (λ h, try (clear h)), return (¬ to_remove.empty ∨ goal_simplified) meta def simp_bottom_up' (post : expr → tactic (expr × expr)) (e : expr) (cfg : simp_config := {}) : tactic (expr × expr) := prod.snd <$> simplify_bottom_up () (λ _, (<$>) (prod.mk ()) ∘ post) e cfg meta structure instance_cache := (α : expr) (univ : level) (inst : name_map expr) meta def mk_instance_cache (α : expr) : tactic instance_cache := do u ← mk_meta_univ, infer_type α >>= unify (expr.sort (level.succ u)), u ← get_univ_assignment u, return ⟨α, u, mk_name_map⟩ namespace instance_cache meta def get (c : instance_cache) (n : name) : tactic (instance_cache × expr) := match c.inst.find n with | some i := return (c, i) | none := do e ← mk_app n [c.α] >>= mk_instance, return (⟨c.α, c.univ, c.inst.insert n e⟩, e) end open expr meta def append_typeclasses : expr → instance_cache → list expr → tactic (instance_cache × list expr) | (pi _ binder_info.inst_implicit (app (const n _) (var _)) body) c l := do (c, p) ← c.get n, return (c, p :: l) | _ c l := return (c, l) meta def mk_app (c : instance_cache) (n : name) (l : list expr) : tactic (instance_cache × expr) := do d ← get_decl n, (c, l) ← append_typeclasses d.type.binding_body c l, return (c, (expr.const n [c.univ]).mk_app (c.α :: l)) end instance_cache /-- Reset the instance cache for the main goal. -/ meta def reset_instance_cache : tactic unit := unfreeze_local_instances meta def match_head (e : expr) : expr → tactic unit | e' := unify e e' <|> do `(_ → %%e') ← whnf e', v ← mk_mvar, match_head (e'.instantiate_var v) meta def find_matching_head : expr → list expr → tactic (list expr) | e [] := return [] | e (H :: Hs) := do t ← infer_type H, ((::) H <$ match_head e t <|> pure id) <*> find_matching_head e Hs meta def subst_locals (s : list (expr × expr)) (e : expr) : expr := (e.abstract_locals (s.map (expr.local_uniq_name ∘ prod.fst)).reverse).instantiate_vars (s.map prod.snd) meta def set_binder : expr → list binder_info → expr | e [] := e | (expr.pi v _ d b) (bi :: bs) := expr.pi v bi d (set_binder b bs) | e _ := e meta def last_explicit_arg : expr → tactic expr | (expr.app f e) := do t ← infer_type f >>= whnf, if t.binding_info = binder_info.default then pure e else last_explicit_arg f | e := pure e private meta def get_expl_pi_arity_aux : expr → tactic nat | (expr.pi n bi d b) := do m ← mk_fresh_name, let l := expr.local_const m n bi d, new_b ← whnf (expr.instantiate_var b l), r ← get_expl_pi_arity_aux new_b, if bi = binder_info.default then return (r + 1) else return r | e := return 0 /-- Compute the arity of explicit arguments of the given (Pi-)type -/ meta def get_expl_pi_arity (type : expr) : tactic nat := whnf type >>= get_expl_pi_arity_aux /-- Compute the arity of explicit arguments of the given function -/ meta def get_expl_arity (fn : expr) : tactic nat := infer_type fn >>= get_expl_pi_arity /-- variation on `assert` where a (possibly incomplete) proof of the assertion is provided as a parameter. ``(h,gs) ← local_proof `h p tac`` creates a local `h : p` and use `tac` to (partially) construct a proof for it. `gs` is the list of remaining goals in the proof of `h`. The benefits over assert are: - unlike with ``h ← assert `h p, tac`` , `h` cannot be used by `tac`; - when `tac` does not complete the proof of `h`, returning the list of goals allows one to write a tactic using `h` and with the confidence that a proof will not boil over to goals left over from the proof of `h`, unlike what would be the case when using `tactic.swap`. -/ meta def local_proof (h : name) (p : expr) (tac₀ : tactic unit) : tactic (expr × list expr) := focus1 $ do h' ← assert h p, [g₀,g₁] ← get_goals, set_goals [g₀], tac₀, gs ← get_goals, set_goals [g₁], return (h', gs) meta def var_names : expr → list name | (expr.pi n _ _ b) := n :: var_names b | _ := [] meta def drop_binders : expr → tactic expr | (expr.pi n bi t b) := b.instantiate_var <$> mk_local' n bi t >>= drop_binders | e := pure e meta def subobject_names (struct_n : name) : tactic (list name × list name) := do env ← get_env, [c] ← pure $ env.constructors_of struct_n | fail "too many constructors", vs ← var_names <$> (mk_const c >>= infer_type), fields ← env.structure_fields struct_n, return $ fields.partition (λ fn, ↑("_" ++ fn.to_string) ∈ vs) meta def expanded_field_list' : name → tactic (dlist $ name × name) | struct_n := do (so,fs) ← subobject_names struct_n, ts ← so.mmap (λ n, do e ← mk_const (n.update_prefix struct_n) >>= infer_type >>= drop_binders, expanded_field_list' $ e.get_app_fn.const_name), return $ dlist.join ts ++ dlist.of_list (fs.map $ prod.mk struct_n) open functor function meta def expanded_field_list (struct_n : name) : tactic (list $ name × name) := dlist.to_list <$> expanded_field_list' struct_n meta def get_classes (e : expr) : tactic (list name) := attribute.get_instances `class >>= list.mfilter (λ n, succeeds $ mk_app n [e] >>= mk_instance) open nat meta def mk_mvar_list : ℕ → tactic (list expr) | 0 := pure [] | (succ n) := (::) <$> mk_mvar <*> mk_mvar_list n /--`iterate_at_most_on_all_goals n t`: repeat the given tactic at most `n` times on all goals, or until it fails. Always succeeds. -/ meta def iterate_at_most_on_all_goals : nat → tactic unit → tactic unit | 0 tac := trace "maximal iterations reached" | (succ n) tac := tactic.all_goals $ (do tac, iterate_at_most_on_all_goals n tac) <|> skip /--`iterate_at_most_on_subgoals n t`: repeat the tactic `t` at most `n` times on the first goal and on all subgoals thus produced, or until it fails. Fails iff `t` fails on current goal. -/ meta def iterate_at_most_on_subgoals : nat → tactic unit → tactic unit | 0 tac := trace "maximal iterations reached" | (succ n) tac := focus1 (do tac, iterate_at_most_on_all_goals n tac) /--`apply_list l`: try to apply the tactics in the list `l` on the first goal, and fail if none succeeds -/ meta def apply_list_expr : list expr → tactic unit | [] := fail "no matching rule" | (h::t) := do interactive.concat_tags (apply h) <|> apply_list_expr t /-- constructs a list of expressions given a list of p-expressions, as follows: - if the p-expression is the name of a theorem, use `i_to_expr_for_apply` on it - if the p-expression is a user attribute, add all the theorems with this attribute to the list.-/ meta def build_list_expr_for_apply : list pexpr → tactic (list expr) | [] := return [] | (h::t) := do tail ← build_list_expr_for_apply t, a ← i_to_expr_for_apply h, (do l ← attribute.get_instances (expr.const_name a), m ← list.mmap mk_const l, return (m.append tail)) <|> return (a::tail) /--`apply_rules hs n`: apply the list of rules `hs` (given as pexpr) and `assumption` on the first goal and the resulting subgoals, iteratively, at most `n` times -/ meta def apply_rules (hs : list pexpr) (n : nat) : tactic unit := do l ← build_list_expr_for_apply hs, iterate_at_most_on_subgoals n (assumption <|> apply_list_expr l) meta def replace (h : name) (p : pexpr) : tactic unit := do h' ← get_local h, p ← to_expr p, note h none p, clear h' /-- Auxiliary function for `iff_mp` and `iff_mpr`. Takes a name, which should be either `` `iff.mp`` or `` `iff.mpr``. If the passed expression is an iterated function type eventually producing an `iff`, returns an expression with the `iff` converted to either the forwards or backwards implication, as requested. -/ meta def mk_iff_mp_app (iffmp : name) : expr → (nat → expr) → option expr | (expr.pi n bi e t) f := expr.lam n bi e <$> mk_iff_mp_app t (λ n, f (n+1) (expr.var n)) | `(%%a ↔ %%b) f := some $ @expr.const tt iffmp [] a b (f 0) | _ f := none meta def iff_mp_core (e ty: expr) : option expr := mk_iff_mp_app `iff.mp ty (λ_, e) meta def iff_mpr_core (e ty: expr) : option expr := mk_iff_mp_app `iff.mpr ty (λ_, e) /-- Given an expression whose type is (a possibly iterated function producing) an `iff`, create the expression which is the forward implication. -/ meta def iff_mp (e : expr) : tactic expr := do t ← infer_type e, iff_mp_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`" /-- Given an expression whose type is (a possibly iterated function producing) an `iff`, create the expression which is the reverse implication. -/ meta def iff_mpr (e : expr) : tactic expr := do t ← infer_type e, iff_mpr_core e t <|> fail "Target theorem must have the form `Π x y z, a ↔ b`" /-- Attempts to apply `e`, and if that fails, if `e` is an `iff`, try applying both directions separately. -/ meta def apply_iff (e : expr) : tactic (list (name × expr)) := let ap e := tactic.apply e {new_goals := new_goals.non_dep_only} in ap e <|> (iff_mp e >>= ap) <|> (iff_mpr e >>= ap) meta def symm_apply (e : expr) (cfg : apply_cfg := {}) : tactic (list (name × expr)) := tactic.apply e cfg <|> (symmetry >> tactic.apply e cfg) meta def apply_assumption (asms : tactic (list expr) := local_context) (tac : tactic unit := skip) : tactic unit := do { ctx ← asms, ctx.any_of (λ H, symm_apply H >> tac) } <|> do { exfalso, ctx ← asms, ctx.any_of (λ H, symm_apply H >> tac) } <|> fail "assumption tactic failed" meta def change_core (e : expr) : option expr → tactic unit | none := tactic.change e | (some h) := do num_reverted : ℕ ← revert h, expr.pi n bi d b ← target, tactic.change $ expr.pi n bi e b, intron num_reverted /-- assuming olde and newe are defeq when elaborated, replaces occurences of olde with newe at hypothesis h. -/ meta def change_with_at (olde newe : pexpr) (hyp : name) : tactic unit := do h ← get_local hyp, tp ← infer_type h, olde ← to_expr olde, newe ← to_expr newe, let repl_tp := tp.replace (λ a n, if a = olde then some newe else none), change_core repl_tp (some h) open nat meta def solve_by_elim_aux (discharger : tactic unit) (asms : tactic (list expr)) : ℕ → tactic unit | 0 := done | (succ n) := discharger <|> (apply_assumption asms $ solve_by_elim_aux n) meta structure by_elim_opt := (all_goals : bool := ff) (discharger : tactic unit := done) (assumptions : tactic (list expr) := local_context) (max_rep : ℕ := 3) meta def solve_by_elim (opt : by_elim_opt := { }) : tactic unit := do tactic.fail_if_no_goals, (if opt.all_goals then id else focus1) $ solve_by_elim_aux opt.discharger opt.assumptions opt.max_rep meta def metavariables : tactic (list expr) := do r ← result, pure (r.list_meta_vars) /-- Succeeds only if the current goal is a proposition. -/ meta def propositional_goal : tactic unit := do goals ← get_goals, p ← is_proof goals.head, guard p meta def triv' : tactic unit := do c ← mk_const `trivial, exact c reducible variable {α : Type} private meta def iterate_aux (t : tactic α) : list α → tactic (list α) | L := (do r ← t, iterate_aux (r :: L)) <|> return L /-- Apply a tactic as many times as possible, collecting the results in a list. -/ meta def iterate' (t : tactic α) : tactic (list α) := list.reverse <$> iterate_aux t [] /-- Like iterate', but fail if the tactic does not succeed at least once. -/ meta def iterate1 (t : tactic α) : tactic (α × list α) := do r ← decorate_ex "iterate1 failed: tactic did not succeed" t, L ← iterate' t, return (r, L) meta def intros1 : tactic (list expr) := iterate1 intro1 >>= λ p, return (p.1 :: p.2) /-- `successes` invokes each tactic in turn, returning the list of successful results. -/ meta def successes (tactics : list (tactic α)) : tactic (list α) := list.filter_map id <$> monad.sequence (tactics.map (λ t, try_core t)) /-- Return target after instantiating metavars and whnf -/ private meta def target' : tactic expr := target >>= instantiate_mvars >>= whnf /-- Just like `split`, `fsplit` applies the constructor when the type of the target is an inductive data type with one constructor. However it does not reorder goals or invoke `auto_param` tactics. -/ -- FIXME check if we can remove `auto_param := ff` meta def fsplit : tactic unit := do [c] ← target' >>= get_constructors_for | tactic.fail "fsplit tactic failed, target is not an inductive datatype with only one constructor", mk_const c >>= λ e, apply e {new_goals := new_goals.all, auto_param := ff} >> skip run_cmd add_interactive [`fsplit] /-- Calls `injection` on each hypothesis, and then, for each hypothesis on which `injection` succeeds, clears the old hypothesis. -/ meta def injections_and_clear : tactic unit := do l ← local_context, results ← successes $ l.map $ λ e, injection e >> clear e, when (results.empty) (fail "could not use `injection` then `clear` on any hypothesis") run_cmd add_interactive [`injections_and_clear] meta def note_anon (e : expr) : tactic unit := do n ← get_unused_name "lh", note n none e, skip /-- `find_local t` returns a local constant with type t, or fails if none exists. -/ meta def find_local (t : pexpr) : tactic expr := do t' ← to_expr t, prod.snd <$> solve_aux t' assumption /-- `dependent_pose_core l`: introduce dependent hypothesis, where the proofs depend on the values of the previous local constants. `l` is a list of local constants and their values. -/ meta def dependent_pose_core (l : list (expr × expr)) : tactic unit := do let lc := l.map prod.fst, let lm := l.map (λ⟨l, v⟩, (l.local_uniq_name, v)), t ← target, new_goal ← mk_meta_var (t.pis lc), old::other_goals ← get_goals, set_goals (old :: new_goal :: other_goals), exact ((new_goal.mk_app lc).instantiate_locals lm), return () /-- like `mk_local_pis` but translating into weak head normal form before checking if it is a Π. -/ meta def mk_local_pis_whnf : expr → tactic (list expr × expr) | e := do (expr.pi n bi d b) ← whnf e | return ([], e), p ← mk_local' n bi d, (ps, r) ← mk_local_pis (expr.instantiate_var b p), return ((p :: ps), r) /-- Changes `(h : ∀xs, ∃a:α, p a) ⊢ g` to `(d : ∀xs, a) (s : ∀xs, p (d xs) ⊢ g` -/ meta def choose1 (h : expr) (data : name) (spec : name) : tactic expr := do t ← infer_type h, (ctxt, t) ← mk_local_pis_whnf t, `(@Exists %%α %%p) ← whnf t transparency.all | fail "expected a term of the shape ∀xs, ∃a, p xs a", α_t ← infer_type α, expr.sort u ← whnf α_t transparency.all, value ← mk_local_def data (α.pis ctxt), t' ← head_beta (p.app (value.mk_app ctxt)), spec ← mk_local_def spec (t'.pis ctxt), dependent_pose_core [ (value, ((((expr.const `classical.some [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt), (spec, ((((expr.const `classical.some_spec [u]).app α).app p).app (h.mk_app ctxt)).lambdas ctxt)], try (tactic.clear h), intro1, intro1 /-- Changes `(h : ∀xs, ∃as, p as) ⊢ g` to a list of functions `as`, an a final hypothesis on `p as` -/ meta def choose : expr → list name → tactic unit | h [] := fail "expect list of variables" | h [n] := do cnt ← revert h, intro n, intron (cnt - 1), return () | h (n::ns) := do v ← get_unused_name >>= choose1 h n, choose v ns /-- This makes sure that the execution of the tactic does not change the tactic state. This can be helpful while using rewrite, apply, or expr munging. Remember to instantiate your metavariables before you're done! -/ meta def lock_tactic_state {α} (t : tactic α) : tactic α | s := match t s with | result.success a s' := result.success a s | result.exception msg pos s' := result.exception msg pos s end /-- Hole command used to fill in a structure's field when specifying an instance. In the following: ``` instance : monad id := {! !} ``` invoking hole command `Instance Stub` produces: ``` instance : monad id := { map := _, map_const := _, pure := _, seq := _, seq_left := _, seq_right := _, bind := _ } ``` -/ @[hole_command] meta def instance_stub : hole_command := { name := "Instance Stub", descr := "Generate a skeleton for the structure under construction.", action := λ _, do tgt ← target >>= whnf, let cl := tgt.get_app_fn.const_name, env ← get_env, fs ← expanded_field_list cl, let fs := fs.map prod.snd, let fs := format.intercalate (",\n " : format) $ fs.map (λ fn, format!"{fn} := _"), let out := format.to_string format!"{{ {fs} }", return [(out,"")] } meta def strip_prefix' (n : name) : list string → name → tactic name | s name.anonymous := pure $ s.foldl (flip name.mk_string) name.anonymous | s (name.mk_string a p) := do let n' := s.foldl (flip name.mk_string) name.anonymous, do { n'' ← tactic.resolve_constant n', if n'' = n then pure n' else strip_prefix' (a :: s) p } <|> strip_prefix' (a :: s) p | s (name.mk_numeral a p) := interaction_monad.failed meta def strip_prefix : name → tactic name | n@(name.mk_string a a_1) := strip_prefix' n [a] a_1 | _ := interaction_monad.failed meta def is_default_local : expr → bool | (expr.local_const _ _ binder_info.default _) := tt | _ := ff meta def mk_patterns (t : expr) : tactic (list format) := do let cl := t.get_app_fn.const_name, env ← get_env, let fs := env.constructors_of cl, fs.mmap $ λ f, do { (vs,_) ← mk_const f >>= infer_type >>= mk_local_pis, let vs := vs.filter (λ v, is_default_local v), vs ← vs.mmap (λ v, do v' ← get_unused_name v.local_pp_name, pose v' none `(()), pure v' ), vs.mmap' $ λ v, get_local v >>= clear, let args := list.intersperse (" " : format) $ vs.map to_fmt, f ← strip_prefix f, if args.empty then pure $ format!"| {f} := _\n" else pure format!"| ({f} {format.join args}) := _\n" } /-- Hole command used to generate a `match` expression. In the following: ``` meta def foo (e : expr) : tactic unit := {! e !} ``` invoking hole command `Match Stub` produces: ``` meta def foo (e : expr) : tactic unit := match e with | (expr.var a) := _ | (expr.sort a) := _ | (expr.const a a_1) := _ | (expr.mvar a a_1 a_2) := _ | (expr.local_const a a_1 a_2 a_3) := _ | (expr.app a a_1) := _ | (expr.lam a a_1 a_2 a_3) := _ | (expr.pi a a_1 a_2 a_3) := _ | (expr.elet a a_1 a_2 a_3) := _ | (expr.macro a a_1) := _ end ``` -/ @[hole_command] meta def match_stub : hole_command := { name := "Match Stub", descr := "Generate a list of equations for a `match` expression.", action := λ es, do [e] ← pure es | fail "expecting one expression", e ← to_expr e, t ← infer_type e >>= whnf, fs ← mk_patterns t, e ← pp e, let out := format.to_string format!"match {e} with\n{format.join fs}end\n", return [(out,"")] } /-- Hole command used to generate a `match` expression. In the following: ``` meta def foo : {! expr → tactic unit !} -- `:=` is omitted ``` invoking hole command `Equations Stub` produces: ``` meta def foo : expr → tactic unit | (expr.var a) := _ | (expr.sort a) := _ | (expr.const a a_1) := _ | (expr.mvar a a_1 a_2) := _ | (expr.local_const a a_1 a_2 a_3) := _ | (expr.app a a_1) := _ | (expr.lam a a_1 a_2 a_3) := _ | (expr.pi a a_1 a_2 a_3) := _ | (expr.elet a a_1 a_2 a_3) := _ | (expr.macro a a_1) := _ ``` A similar result can be obtained by invoking `Equations Stub` on the following: ``` meta def foo : expr → tactic unit := -- do not forget to write `:=`!! {! !} ``` ``` meta def foo : expr → tactic unit := -- don't forget to erase `:=`!! | (expr.var a) := _ | (expr.sort a) := _ | (expr.const a a_1) := _ | (expr.mvar a a_1 a_2) := _ | (expr.local_const a a_1 a_2 a_3) := _ | (expr.app a a_1) := _ | (expr.lam a a_1 a_2 a_3) := _ | (expr.pi a a_1 a_2 a_3) := _ | (expr.elet a a_1 a_2 a_3) := _ | (expr.macro a a_1) := _ ``` -/ @[hole_command] meta def eqn_stub : hole_command := { name := "Equations Stub", descr := "Generate a list of equations for a recursive definition.", action := λ es, do t ← match es with | [t] := to_expr t | [] := target | _ := fail "expecting one type" end, e ← whnf t, (v :: _,_) ← mk_local_pis e | fail "expecting a Pi-type", t' ← infer_type v, fs ← mk_patterns t', t ← pp t, let out := if es.empty then format.to_string format!"-- do not forget to erase `:=`!!\n{format.join fs}" else format.to_string format!"{t}\n{format.join fs}", return [(out,"")] } /-- This command lists the constructors that can be used to satisfy the expected type. When used in the following hole: ``` def foo : ℤ ⊕ ℕ := {! !} ``` the command will produce: ``` def foo : ℤ ⊕ ℕ := {! sum.inl, sum.inr !} ``` and will display: ``` sum.inl : ℤ → ℤ ⊕ ℕ sum.inr : ℕ → ℤ ⊕ ℕ ``` -/ @[hole_command] meta def list_constructors_hole : hole_command := { name := "List Constructors", descr := "Show the list of constructors of the expected type.", action := λ es, do t ← target >>= whnf, (_,t) ← mk_local_pis t, let cl := t.get_app_fn.const_name, let args := t.get_app_args, env ← get_env, let cs := env.constructors_of cl, ts ← cs.mmap $ λ c, do { e ← mk_const c, t ← infer_type (e.mk_app args) >>= pp, c ← strip_prefix c, pure format!"\n{c} : {t}\n" }, fs ← format.intercalate ", " <$> cs.mmap (strip_prefix >=> pure ∘ to_fmt), let out := format.to_string format!"{{! {fs} !}", trace (format.join ts).to_string, return [(out,"")] } meta def classical : tactic unit := do h ← get_unused_name `_inst, mk_const `classical.prop_decidable >>= note h none, reset_instance_cache open expr meta def add_prime : name → name | (name.mk_string s p) := name.mk_string (s ++ "'") p | n := (name.mk_string "x'" n) meta def mk_comp (v : expr) : expr → tactic expr | (app f e) := if e = v then pure f else do guard (¬ v.occurs f) <|> fail "bad guard", e' ← mk_comp e >>= instantiate_mvars, f ← instantiate_mvars f, mk_mapp ``function.comp [none,none,none,f,e'] | e := do guard (e = v), t ← infer_type e, mk_mapp ``id [t] meta def mk_higher_order_type : expr → tactic expr | (pi n bi d b@(pi _ _ _ _)) := do v ← mk_local_def n d, let b' := (b.instantiate_var v), (pi n bi d ∘ flip abstract_local v.local_uniq_name) <$> mk_higher_order_type b' | (pi n bi d b) := do v ← mk_local_def n d, let b' := (b.instantiate_var v), (l,r) ← match_eq b' <|> fail format!"not an equality {b'}", l' ← mk_comp v l, r' ← mk_comp v r, mk_app ``eq [l',r'] | e := failed open lean.parser interactive.types @[user_attribute] meta def higher_order_attr : user_attribute unit (option name) := { name := `higher_order, parser := optional ident, descr := "From a lemma of the shape `f (g x) = h x` derive an auxiliary lemma of the form `f ∘ g = h` for reasoning about higher-order functions.", after_set := some $ λ lmm _ _, do env ← get_env, decl ← env.get lmm, let num := decl.univ_params.length, let lvls := (list.iota num).map (`l).append_after, let l : expr := expr.const lmm $ lvls.map level.param, t ← infer_type l >>= instantiate_mvars, t' ← mk_higher_order_type t, (_,pr) ← solve_aux t' $ do { intros, applyc ``_root_.funext, intro1, applyc lmm; assumption }, pr ← instantiate_mvars pr, lmm' ← higher_order_attr.get_param lmm, lmm' ← (flip name.update_prefix lmm.get_prefix <$> lmm') <|> pure (add_prime lmm), add_decl $ declaration.thm lmm' lvls t' (pure pr), copy_attribute `simp lmm tt lmm', copy_attribute `functor_norm lmm tt lmm' } attribute [higher_order map_comp_pure] map_pure private meta def tactic.use_aux (h : pexpr) : tactic unit := (focus1 (refine h >> done)) <|> (fconstructor >> tactic.use_aux) meta def tactic.use (l : list pexpr) : tactic unit := focus1 $ l.mmap' $ λ h, tactic.use_aux h <|> fail format!"failed to instantiate goal with {h}" meta def clear_aux_decl_aux : list expr → tactic unit | [] := skip | (e::l) := do cond e.is_aux_decl (tactic.clear e) skip, clear_aux_decl_aux l meta def clear_aux_decl : tactic unit := local_context >>= clear_aux_decl_aux end tactic
66b21a57060deb82094f190be366205a64a54b07
acc85b4be2c618b11fc7cb3005521ae6858a8d07
/analysis/topology/continuity.lean
c65a3ae4cc0eb28caae6ef1b5af25d685b180f65
[ "Apache-2.0" ]
permissive
linpingchuan/mathlib
d49990b236574df2a45d9919ba43c923f693d341
5ad8020f67eb13896a41cc7691d072c9331b1f76
refs/heads/master
1,626,019,377,808
1,508,048,784,000
1,508,048,784,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
36,729
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Continuous functions. Parts of the formalization is based on the books: N. Bourbaki: General Topology I. M. James: Topologies and Uniformities A major difference is that this formalization is heavily based on the filter library. -/ import analysis.topology.topological_space noncomputable theory open set filter lattice classical local attribute [instance] decidable_inhabited prop_decidable universes u v w x y variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ι : Sort x} section variables [topological_space α] [topological_space β] [topological_space γ] def continuous (f : α → β) := ∀s, is_open s → is_open (preimage f s) lemma continuous_id : continuous (id : α → α) := assume s h, h lemma continuous_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g): continuous (g ∘ f) := assume s h, hf _ (hg s h) lemma continuous_iff_tendsto {f : α → β} : continuous f ↔ (∀x, tendsto f (nhds x) (nhds (f x))) := ⟨assume hf : continuous f, assume x s, show s ∈ (nhds (f x)).sets → s ∈ (map f (nhds x)).sets, by simp [nhds_sets]; exact assume t t_open t_subset fx_in_t, ⟨preimage f t, hf t t_open, fx_in_t, preimage_mono t_subset⟩, assume hf : ∀x, tendsto f (nhds x) (nhds (f x)), assume s, assume hs : is_open s, have ∀a, f a ∈ s → s ∈ (nhds (f a)).sets, by simp [nhds_sets]; exact assume a ha, ⟨s, hs, subset.refl s, ha⟩, show is_open (preimage f s), by simp [is_open_iff_nhds]; exact assume a ha, hf a (this a ha)⟩ lemma continuous_const {b : β} : continuous (λa:α, b) := continuous_iff_tendsto.mpr $ assume a, tendsto_const_nhds lemma continuous_iff_is_closed {f : α → β} : continuous f ↔ (∀s, is_closed s → is_closed (preimage f s)) := ⟨assume hf s hs, hf (-s) hs, assume hf s, by rw [←is_closed_compl_iff, ←is_closed_compl_iff]; exact hf _⟩ lemma continuous_if {p : α → Prop} {f g : α → β} {h : ∀a, decidable (p a)} (hp : ∀a∈frontier {a | p a}, f a = g a) (hf : continuous f) (hg : continuous g) : continuous (λa, @ite (p a) (h a) β (f a) (g a)) := continuous_iff_is_closed.mpr $ assume s hs, have (λa, ite (p a) (f a) (g a)) ⁻¹' s = (closure {a | p a} ∩ f ⁻¹' s) ∪ (closure {a | ¬ p a} ∩ g ⁻¹' s), from set.ext $ assume a, by_cases (assume : a ∈ frontier {a | p a}, have hac : a ∈ closure {a | p a}, from this.left, have hai : a ∈ closure {a | ¬ p a}, from have a ∈ - interior {a | p a}, from this.right, by rwa [←closure_compl] at this, by by_cases p a; simp [h, hp a this, hac, hai, iff_def] {contextual := tt}) (assume hf : a ∈ - frontier {a | p a}, by_cases (assume : p a, have hc : a ∈ closure {a | p a}, from subset_closure this, have hnc : a ∉ closure {a | ¬ p a}, by show a ∉ closure (- {a | p a}); rw [closure_compl]; simpa [frontier, hc] using hf, by simp [this, hc, hnc]) (assume : ¬ p a, have hc : a ∈ closure {a | ¬ p a}, from subset_closure this, have hnc : a ∉ closure {a | p a}, begin have hc : a ∈ closure (- {a | p a}), from hc, simp [closure_compl] at hc, simpa [frontier, hc] using hf end, by simp [this, hc, hnc])), by rw [this]; exact is_closed_union (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hf s hs) (is_closed_inter is_closed_closure $ continuous_iff_is_closed.mp hg s hs) lemma image_closure_subset_closure_image {f : α → β} {s : set α} (h : continuous f) : f '' closure s ⊆ closure (f '' s) := have ∀ (a : α), nhds a ⊓ principal s ≠ ⊥ → nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from assume a ha, have h₁ : ¬ map f (nhds a ⊓ principal s) = ⊥, by rwa[map_eq_bot_iff], have h₂ : map f (nhds a ⊓ principal s) ≤ nhds (f a) ⊓ principal (f '' s), from le_inf (le_trans (map_mono inf_le_left) $ by rw [continuous_iff_tendsto] at h; exact h a) (le_trans (map_mono inf_le_right) $ by simp; exact subset.refl _), neq_bot_of_le_neq_bot h₁ h₂, by simp [image_subset_iff, closure_eq_nhds]; assumption lemma compact_image {s : set α} {f : α → β} (hs : compact s) (hf : continuous f) : compact (f '' s) := compact_of_finite_subcover $ assume c hco hcs, have hdo : ∀t∈c, is_open (preimage f t), from assume t' ht, hf _ $ hco _ ht, have hds : s ⊆ ⋃i∈c, preimage f i, by simpa [subset_def, -mem_image] using hcs, let ⟨d', hcd', hfd', hd'⟩ := compact_elim_finite_subcover_image hs hdo hds in ⟨d', hcd', hfd', by simpa [subset_def, -mem_image, image_subset_iff] using hd'⟩ end section constructions local notation `cont` := @continuous _ _ local notation `tspace` := topological_space open topological_space variable {f : α → β} lemma continuous_iff_induced_le {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f ↔ (induced f t₂ ≤ t₁) := ⟨assume hc s ⟨t, ht, s_eq⟩, s_eq.symm ▸ hc t ht, assume hle s h, hle _ ⟨_, h, rfl⟩⟩ lemma continuous_eq_le_coinduced {t₁ : tspace α} {t₂ : tspace β} : cont t₁ t₂ f = (t₂ ≤ coinduced f t₁) := rfl theorem continuous_generated_from {t : tspace α} {b : set (set β)} (h : ∀s∈b, is_open (preimage f s)) : cont t (generate_from b) f := assume s hs, generate_open.rec_on hs h is_open_univ (assume s t _ _, is_open_inter) (assume t _ h, by rw [preimage_sUnion]; exact (is_open_Union $ assume s, is_open_Union $ assume hs, h s hs)) lemma continuous_induced_dom {t : tspace β} : cont (induced f t) t f := assume s h, ⟨_, h, rfl⟩ lemma continuous_induced_rng {g : γ → α} {t₂ : tspace β} {t₁ : tspace γ} (h : cont t₁ t₂ (f ∘ g)) : cont t₁ (induced f t₂) g := assume s ⟨t, ht, s_eq⟩, s_eq.symm ▸ h t ht lemma continuous_coinduced_rng {t : tspace α} : cont t (coinduced f t) f := assume s h, h lemma continuous_coinduced_dom {g : β → γ} {t₁ : tspace α} {t₂ : tspace γ} (h : cont t₁ t₂ (g ∘ f)) : cont (coinduced f t₁) t₂ g := assume s hs, h s hs lemma continuous_inf_dom {t₁ t₂ : tspace α} {t₃ : tspace β} (h₁ : cont t₁ t₃ f) (h₂ : cont t₂ t₃ f) : cont (t₁ ⊓ t₂) t₃ f := assume s h, ⟨h₁ s h, h₂ s h⟩ lemma continuous_inf_rng_left {t₁ : tspace α} {t₃ t₂ : tspace β} (h : cont t₁ t₂ f) : cont t₁ (t₂ ⊓ t₃) f := assume s hs, h s hs.left lemma continuous_inf_rng_right {t₁ : tspace α} {t₃ t₂ : tspace β} (h : cont t₁ t₃ f) : cont t₁ (t₂ ⊓ t₃) f := assume s hs, h s hs.right lemma continuous_Inf_dom {t₁ : set (tspace α)} {t₂ : tspace β} (h : ∀t∈t₁, cont t t₂ f) : cont (Inf t₁) t₂ f := assume s hs t ht, h t ht s hs lemma continuous_Inf_rng {t₁ : tspace α} {t₂ : set (tspace β)} {t : tspace β} (h₁ : t ∈ t₂) (hf : cont t₁ t f) : cont t₁ (Inf t₂) f := assume s hs, hf s $ hs t h₁ lemma continuous_infi_dom {t₁ : ι → tspace α} {t₂ : tspace β} (h : ∀i, cont (t₁ i) t₂ f) : cont (infi t₁) t₂ f := continuous_Inf_dom $ assume t ⟨i, (t_eq : t = t₁ i)⟩, t_eq.symm ▸ h i lemma continuous_infi_rng {t₁ : tspace α} {t₂ : ι → tspace β} {i : ι} (h : cont t₁ (t₂ i) f) : cont t₁ (infi t₂) f := continuous_Inf_rng ⟨i, rfl⟩ h lemma continuous_top {t : tspace β} : cont ⊤ t f := assume s h, trivial lemma continuous_bot {t : tspace α} : cont t ⊥ f := continuous_Inf_rng (mem_univ $ coinduced f t) continuous_coinduced_rng lemma continuous_sup_rng {t₁ : tspace α} {t₂ t₃ : tspace β} (h₁ : cont t₁ t₂ f) (h₂ : cont t₁ t₃ f) : cont t₁ (t₂ ⊔ t₃) f := continuous_Inf_rng (show t₂ ≤ coinduced f t₁ ∧ t₃ ≤ coinduced f t₁, from ⟨h₁, h₂⟩) continuous_coinduced_rng lemma continuous_sup_dom_left {t₁ t₂ : tspace α} {t₃ : tspace β} (h : cont t₁ t₃ f) : cont (t₁ ⊔ t₂) t₃ f := continuous_Inf_dom $ assume t ⟨h₁, h₂⟩ s hs, h₁ _ $ h s hs lemma continuous_sup_dom_right {t₁ t₂ : tspace α} {t₃ : tspace β} (h : cont t₂ t₃ f) : cont (t₁ ⊔ t₂) t₃ f := continuous_Inf_dom $ assume t ⟨h₁, h₂⟩ s hs, h₂ _ $ h s hs end constructions section embedding lemma induced_mono {t₁ t₂ : topological_space α} {f : β → α} (h : t₁ ≤ t₂) : t₁.induced f ≤ t₂.induced f := continuous_iff_induced_le.mp $ show @continuous β α (@topological_space.induced β α f t₂) t₁ (id ∘ f), begin apply continuous_compose, exact continuous_induced_dom, exact assume s hs, h _ hs end lemma induced_id [t : topological_space α] : t.induced id = t := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', hs, h⟩, h.symm ▸ hs, assume hs, ⟨s, hs, rfl⟩⟩ lemma induced_compose [tβ : topological_space β] [tγ : topological_space γ] {f : α → β} {g : β → γ} : (tγ.induced g).induced f = tγ.induced (g ∘ f) := topological_space_eq $ funext $ assume s, propext $ ⟨assume ⟨s', ⟨s, hs, h₂⟩, h₁⟩, h₁.symm ▸ h₂.symm ▸ ⟨s, hs, rfl⟩, assume ⟨s, hs, h⟩, ⟨preimage g s, ⟨s, hs, rfl⟩, h ▸ rfl⟩⟩ lemma induced_sup (t₁ : topological_space β) (t₂ : topological_space β) {f : α → β} : (t₁ ⊔ t₂).induced f = t₁.induced f ⊔ t₂.induced f := le_antisymm (continuous_iff_induced_le.mp $ continuous_sup_rng (continuous_sup_dom_left continuous_induced_dom) (continuous_sup_dom_right continuous_induced_dom)) (sup_le (induced_mono le_sup_left) (induced_mono le_sup_right)) def embedding [tα : topological_space α] [tβ : topological_space β] (f : α → β) : Prop := (∀a₁ a₂, f a₁ = f a₂ → a₁ = a₂) ∧ tα = tβ.induced f variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] lemma embedding_id : embedding (@id α) := ⟨assume a₁ a₂ h, h, induced_id.symm⟩ lemma embedding_compose {f : α → β} {g : β → γ} (hf : embedding f) (hg : embedding g) : embedding (g ∘ f) := ⟨assume a₁ a₂ h, hf.left _ _ $ hg.left _ _ $ h, by rw [hf.right, hg.right, induced_compose]⟩ lemma embedding_prod_mk {f : α → β} {g : γ → δ} (hf : embedding f) (hg : embedding g) : embedding (λx:α×γ, (f x.1, g x.2)) := ⟨assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨hf.left _ _ h₁, hg.left _ _ h₂⟩, by rw [prod.topological_space, prod.topological_space, hf.right, hg.right, induced_compose, induced_compose, induced_sup, induced_compose, induced_compose]⟩ lemma embedding_of_embedding_compose {f : α → β} {g : β → γ} (hf : continuous f) (hg : continuous g) (hgf : embedding (g ∘ f)) : embedding f := ⟨assume a₁ a₂ h, hgf.left a₁ a₂ $ by simp [h, (∘)], le_antisymm (by rw [hgf.right, ←continuous_iff_induced_le]; apply continuous_compose continuous_induced_dom hg) (by rwa [←continuous_iff_induced_le])⟩ lemma embedding_open {f : α → β} {s : set α} (hf : embedding f) (h : is_open (f '' univ)) (hs : is_open s) : is_open (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right] at hs; exact hs in have is_open (t ∩ f '' univ), from is_open_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_rng] lemma embedding_is_closed {f : α → β} {s : set α} (hf : embedding f) (h : is_closed (f '' univ)) (hs : is_closed s) : is_closed (f '' s) := let ⟨t, ht, h_eq⟩ := by rw [hf.right, is_closed_induced_iff] at hs; exact hs in have is_closed (t ∩ f '' univ), from is_closed_inter ht h, h_eq.symm ▸ by rwa [image_preimage_eq_inter_rng] end embedding section sierpinski variables [topological_space α] @[simp] lemma is_open_singleton_true : is_open ({true} : set Prop) := topological_space.generate_open.basic _ (by simp) lemma continuous_Prop {p : α → Prop} : continuous p ↔ is_open {x | p x} := ⟨assume h : continuous p, have is_open (preimage p {true}), from h _ is_open_singleton_true, by simp [preimage, eq_true] at this; assumption, assume h : is_open {x | p x}, continuous_generated_from $ assume s (hs : s ∈ {{true}}), by simp at hs; simp [hs, preimage, eq_true, h]⟩ end sierpinski section induced open topological_space variables [t : topological_space β] {f : α → β} theorem is_open_induced {s : set β} (h : is_open s) : (induced f t).is_open (preimage f s) := ⟨s, h, rfl⟩ lemma nhds_induced_eq_vmap {a : α} : @nhds α (induced f t) a = vmap f (nhds (f a)) := le_antisymm (assume s ⟨s', hs', (h_s : preimage f s' ⊆ s)⟩, have ∃t':set β, is_open t' ∧ t' ⊆ s' ∧ f a ∈ t', by simp [mem_nhds_sets_iff] at hs'; assumption, let ⟨t', ht', hsub, hin⟩ := this in (@nhds α (induced f t) a).upwards_sets begin simp [mem_nhds_sets_iff], exact ⟨preimage f t', is_open_induced ht', hin, preimage_mono hsub⟩ end h_s) (le_infi $ assume s, le_infi $ assume ⟨as, ⟨s', is_open_s', s_eq⟩⟩, begin simp [vmap, mem_nhds_sets_iff, s_eq], exact ⟨s', subset.refl _, s', is_open_s', subset.refl _, by rw [s_eq] at as; assumption⟩ end) lemma map_nhds_induced_eq {a : α} (h : image f univ ∈ (nhds (f a)).sets) : map f (@nhds α (induced f t) a) = nhds (f a) := le_antisymm ((@continuous_iff_tendsto α β (induced f t) _ _).mp continuous_induced_dom a) (assume s, assume hs : preimage f s ∈ (@nhds α (induced f t) a).sets, let ⟨t', t_subset, is_open_t, a_in_t⟩ := mem_nhds_sets_iff.mp h in let ⟨s', s'_subset, ⟨s'', is_open_s'', s'_eq⟩, a_in_s'⟩ := (@mem_nhds_sets_iff _ (induced f t) _ _).mp hs in by subst s'_eq; exact (mem_nhds_sets_iff.mpr $ ⟨t' ∩ s'', assume x ⟨h₁, h₂⟩, match x, h₂, t_subset h₁ with | x, h₂, ⟨y, _, y_eq⟩ := begin subst y_eq, exact s'_subset h₂ end end, is_open_inter is_open_t is_open_s'', ⟨a_in_t, a_in_s'⟩⟩)) lemma closure_induced [t : topological_space β] {f : α → β} {a : α} {s : set α} (hf : ∀x y, f x = f y → x = y) : a ∈ @closure α (topological_space.induced f t) s ↔ f a ∈ closure (f '' s) := have vmap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ ↔ nhds (f a) ⊓ principal (f '' s) ≠ ⊥, from ⟨assume h₁ h₂, h₁ $ h₂.symm ▸ vmap_bot, assume h, forall_sets_neq_empty_iff_neq_bot.mp $ assume s₁ ⟨s₂, hs₂, (hs : preimage f s₂ ⊆ s₁)⟩, have f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from mem_inf_sets_of_right $ by simp [subset.refl], have s₂ ∩ f '' s ∈ (nhds (f a) ⊓ principal (f '' s)).sets, from inter_mem_sets hs₂ this, let ⟨b, hb₁, ⟨a, ha, ha₂⟩⟩ := inhabited_of_mem_sets h this in ne_empty_of_mem $ hs $ by rwa [←ha₂] at hb₁⟩, calc a ∈ @closure α (topological_space.induced f t) s ↔ (@nhds α (topological_space.induced f t) a) ⊓ principal s ≠ ⊥ : by rw [closure_eq_nhds]; refl ... ↔ vmap f (nhds (f a)) ⊓ principal (preimage f (f '' s)) ≠ ⊥ : by rw [nhds_induced_eq_vmap, preimage_image_eq hf] ... ↔ vmap f (nhds (f a) ⊓ principal (f '' s)) ≠ ⊥ : by rw [vmap_inf, ←vmap_principal] ... ↔ _ : by rwa [closure_eq_nhds] end induced section prod open topological_space variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_fst : continuous (@prod.fst α β) := continuous_sup_dom_left continuous_induced_dom lemma continuous_snd : continuous (@prod.snd α β) := continuous_sup_dom_right continuous_induced_dom lemma continuous_prod_mk {f : γ → α} {g : γ → β} (hf : continuous f) (hg : continuous g) : continuous (λx, prod.mk (f x) (g x)) := continuous_sup_rng (continuous_induced_rng hf) (continuous_induced_rng hg) lemma is_open_prod {s : set α} {t : set β} (hs : is_open s) (ht: is_open t) : is_open (set.prod s t) := is_open_inter (continuous_fst s hs) (continuous_snd t ht) lemma prod_generate_from_generate_from_eq {s : set (set α)} {t : set (set β)} (hs : ⋃₀ s = univ) (ht : ⋃₀ t = univ) : @prod.topological_space α β (generate_from s) (generate_from t) = generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} := let G := generate_from {g | ∃u∈s, ∃v∈t, g = set.prod u v} in le_antisymm (sup_le (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume u hu, have (⋃v∈t, set.prod u v) = prod.fst ⁻¹' u, from calc (⋃v∈t, set.prod u v) = set.prod u univ: set.ext $ assume ⟨a, b⟩, by rw [←ht]; simp {contextual:=tt} ... = prod.fst ⁻¹' u : by simp [set.prod, preimage], show G.is_open (prod.fst ⁻¹' u), from this ▸ @is_open_Union _ _ G _ $ assume v, @is_open_Union _ _ G _ $ assume hv, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩) (induced_le_iff_le_coinduced.mpr $ generate_from_le $ assume v hv, have (⋃u∈s, set.prod u v) = prod.snd ⁻¹' v, from calc (⋃u∈s, set.prod u v) = set.prod univ v: set.ext $ assume ⟨a, b⟩, by rw [←hs]; by_cases b ∈ v; simp [h] {contextual:=tt} ... = prod.snd ⁻¹' v : by simp [set.prod, preimage], show G.is_open (prod.snd ⁻¹' v), from this ▸ @is_open_Union _ _ G _ $ assume u, @is_open_Union _ _ G _ $ assume hu, generate_open.basic _ ⟨_, hu, _, hv, rfl⟩)) (generate_from_le $ assume g ⟨u, hu, v, hv, g_eq⟩, g_eq.symm ▸ @is_open_prod _ _ (generate_from s) (generate_from t) _ _ (generate_open.basic _ hu) (generate_open.basic _ hv)) lemma prod_eq_generate_from [tα : topological_space α] [tβ : topological_space β] : prod.topological_space = generate_from {g | ∃(s:set α) (t:set β), is_open s ∧ is_open t ∧ g = set.prod s t} := le_antisymm (sup_le (assume s ⟨t, ht, s_eq⟩, have set.prod t univ = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨t, univ, ht, is_open_univ, rfl⟩)) (assume s ⟨t, ht, s_eq⟩, have set.prod univ t = s, by simp [s_eq, preimage, set.prod], this ▸ (generate_open.basic _ ⟨univ, t, is_open_univ, ht, rfl⟩))) (generate_from_le $ assume g ⟨s, t, hs, ht, g_eq⟩, g_eq.symm ▸ is_open_prod hs ht) lemma nhds_prod_eq {a : α} {b : β} : nhds (a, b) = filter.prod (nhds a) (nhds b) := by rw [prod_eq_generate_from, nhds_generate_from]; exact le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, begin simp [mem_nhds_sets_iff] at hs, simp [mem_nhds_sets_iff] at ht, revert hs ht, exact (assume ⟨s', hs', hs_sub, as'⟩ ⟨t', ht', ht_sub, at'⟩, infi_le_of_le (set.prod s' t') $ infi_le_of_le ⟨⟨as', at'⟩, s', t', hs', ht', rfl⟩ $ principal_mono.mpr $ set.prod_mono hs_sub ht_sub) end) (le_infi $ assume s, le_infi $ assume ⟨hab, s', t', hs', ht', s_eq⟩, begin revert hab, simp [s_eq], exact assume ha hb, @prod_mem_prod α β s' t' (nhds a) (nhds b) (mem_nhds_sets_iff.mpr ⟨s', subset.refl s', hs', ha⟩) (mem_nhds_sets_iff.mpr ⟨t', subset.refl t', ht', hb⟩) end) lemma is_open_prod_iff {s : set (α×β)} : is_open s ↔ (∀a b, (a, b) ∈ s → ∃u v, is_open u ∧ is_open v ∧ a ∈ u ∧ b ∈ v ∧ set.prod u v ⊆ s) := begin rw [is_open_iff_nhds], simp [nhds_prod_eq, mem_prod_iff], simp [mem_nhds_sets_iff], exact (forall_congr $ assume a, forall_congr $ assume b, forall_congr $ assume h, ⟨assume ⟨u', ⟨u, hu₁, hu₂, hu₃⟩, v', h, ⟨v, hv₁, hv₂, hv₃⟩⟩, ⟨u, hu₁, v, hv₁, hu₃, hv₃, subset.trans (set.prod_mono hu₂ hv₂) h⟩, assume ⟨u, hu₁, v, hv₁, hu₃, hv₃, h⟩, ⟨u, ⟨u, hu₁, subset.refl u, hu₃⟩, v, h, ⟨v, hv₁, subset.refl v, hv₃⟩⟩⟩) end lemma closure_prod_eq {s : set α} {t : set β} : closure (set.prod s t) = set.prod (closure s) (closure t) := set.ext $ assume ⟨a, b⟩, have filter.prod (nhds a) (nhds b) ⊓ principal (set.prod s t) = filter.prod (nhds a ⊓ principal s) (nhds b ⊓ principal t), by rw [←prod_inf_prod, prod_principal_principal], by simp [closure_eq_nhds, nhds_prod_eq, this]; exact prod_neq_bot lemma is_closed_prod [topological_space α] [topological_space β] {s₁ : set α} {s₂ : set β} (h₁ : is_closed s₁) (h₂ : is_closed s₂) : is_closed (set.prod s₁ s₂) := closure_eq_iff_is_closed.mp $ by simp [h₁, h₂, closure_prod_eq, closure_eq_of_is_closed] lemma is_closed_diagonal [topological_space α] [t2_space α] : is_closed {p:α×α | p.1 = p.2} := is_closed_iff_nhds.mpr $ assume ⟨a₁, a₂⟩ h, eq_of_nhds_neq_bot $ assume : nhds a₁ ⊓ nhds a₂ = ⊥, h $ let ⟨t₁, ht₁, t₂, ht₂, (h' : t₁ ∩ t₂ ⊆ ∅)⟩ := by rw [←empty_in_sets_eq_bot, mem_inf_sets] at this; exact this in begin rw [nhds_prod_eq, ←empty_in_sets_eq_bot], apply filter.upwards_sets, apply inter_mem_inf_sets (prod_mem_prod ht₁ ht₂) (mem_principal_sets.mpr (subset.refl _)), exact assume ⟨x₁, x₂⟩ ⟨⟨hx₁, hx₂⟩, (heq : x₁ = x₂)⟩, show false, from @h' x₁ ⟨hx₁, heq.symm ▸ hx₂⟩ end lemma is_closed_eq [topological_space α] [t2_space α] [topological_space β] {f g : β → α} (hf : continuous f) (hg : continuous g) : is_closed {x:β | f x = g x} := continuous_iff_is_closed.mp (continuous_prod_mk hf hg) _ is_closed_diagonal /- TODO: more fine grained instances for first_countable_topology, separable_space, t2_space, ... -/ instance [second_countable_topology α] [second_countable_topology β] : second_countable_topology (α × β) := ⟨let ⟨a, ha₁, ha₂, ha₃, ha₄, ha₅⟩ := is_open_generated_countable_inter α in let ⟨b, hb₁, hb₂, hb₃, hb₄, hb₅⟩ := is_open_generated_countable_inter β in ⟨{g | ∃u∈a, ∃v∈b, g = set.prod u v}, have {g | ∃u∈a, ∃v∈b, g = set.prod u v} = (⋃u∈a, ⋃v∈b, {set.prod u v}), by apply set.ext; simp, by rw [this]; exact (countable_bUnion ha₁ $ assume u hu, countable_bUnion hb₁ $ by simp), by rw [ha₅, hb₅, prod_generate_from_generate_from_eq ha₄ hb₄]⟩⟩ end prod section sum variables [topological_space α] [topological_space β] [topological_space γ] lemma continuous_inl : continuous (@sum.inl α β) := continuous_inf_rng_left continuous_coinduced_rng lemma continuous_inr : continuous (@sum.inr α β) := continuous_inf_rng_right continuous_coinduced_rng lemma continuous_sum_rec {f : α → γ} {g : β → γ} (hf : continuous f) (hg : continuous g) : @continuous (α ⊕ β) γ _ _ (@sum.rec α β (λ_, γ) f g) := continuous_inf_dom hf hg end sum section subtype variables [topological_space α] [topological_space β] [topological_space γ] {p : α → Prop} lemma tendsto_nhds_iff_of_embedding {f : α → β} {g : β → γ} {a : filter α} {b : β} (hg : embedding g) : tendsto f a (nhds b) ↔ tendsto (g ∘ f) a (nhds (g b)) := by rw [tendsto, tendsto, hg.right, nhds_induced_eq_vmap, le_vmap_iff_map_le, map_map] lemma continuous_iff_of_embedding {f : α → β} {g : β → γ} (hg : embedding g) : continuous f ↔ continuous (g ∘ f) := by simp [continuous_iff_tendsto, @tendsto_nhds_iff_of_embedding α β γ _ _ _ f g _ _ hg] lemma embedding_graph {f : α → β} (hf : continuous f) : embedding (λx, (x, f x)) := embedding_of_embedding_compose (continuous_prod_mk continuous_id hf) continuous_fst embedding_id lemma embedding_subtype_val : embedding (@subtype.val α p) := ⟨assume a₁ a₂, subtype.eq, rfl⟩ lemma continuous_subtype_val : continuous (@subtype.val α p) := continuous_induced_dom lemma continuous_subtype_mk {f : β → α} (hp : ∀x, p (f x)) (h : continuous f) : continuous (λx, (⟨f x, hp x⟩ : subtype p)) := continuous_induced_rng h lemma map_nhds_subtype_val_eq {a : α} (ha : p a) (h : {a | p a} ∈ (nhds a).sets) : map (@subtype.val α p) (nhds ⟨a, ha⟩) = nhds a := map_nhds_induced_eq (by simp [subtype_val_image, h]) lemma nhds_subtype_eq_vmap {a : α} {h : p a} : nhds (⟨a, h⟩ : subtype p) = vmap subtype.val (nhds a) := nhds_induced_eq_vmap lemma continuous_subtype_nhds_cover {f : α → β} {c : ι → α → Prop} (c_cover : ∀x:α, ∃i, {x | c i x} ∈ (nhds x).sets) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_tendsto.mpr $ assume x, let ⟨i, (c_sets : {x | c i x} ∈ (nhds x).sets)⟩ := c_cover x in let x' : subtype (c i) := ⟨x, mem_of_nhds c_sets⟩ in calc map f (nhds x) = map f (map subtype.val (nhds x')) : congr_arg (map f) (map_nhds_subtype_val_eq _ $ c_sets).symm ... = map (λx:subtype (c i), f x.val) (nhds x') : rfl ... ≤ nhds (f x) : continuous_iff_tendsto.mp (f_cont i) x' lemma continuous_subtype_is_closed_cover {f : α → β} (c : γ → α → Prop) (h_lf : locally_finite (λi, {x | c i x})) (h_is_closed : ∀i, is_closed {x | c i x}) (h_cover : ∀x, ∃i, c i x) (f_cont : ∀i, continuous (λ(x : subtype (c i)), f x.val)) : continuous f := continuous_iff_is_closed.mpr $ assume s hs, have ∀i, is_closed (@subtype.val α {x | c i x} '' (preimage (f ∘ subtype.val) s)), from assume i, embedding_is_closed embedding_subtype_val (by simp [subtype_val_image]; exact h_is_closed i) (continuous_iff_is_closed.mp (f_cont i) _ hs), have is_closed (⋃i, @subtype.val α {x | c i x} '' (preimage (f ∘ subtype.val) s)), from is_closed_Union_of_locally_finite (locally_finite_subset h_lf $ assume i x ⟨⟨x', hx'⟩, _, heq⟩, heq ▸ hx') this, have preimage f s = (⋃i, @subtype.val α {x | c i x} '' (preimage (f ∘ subtype.val) s)), begin apply set.ext, have : ∀ (x : α), f x ∈ s ↔ ∃ (i : γ), c i x ∧ f x ∈ s := λ x, ⟨λ hx, let ⟨i, hi⟩ := h_cover x in ⟨i, hi, hx⟩, λ ⟨i, hi, hx⟩, hx⟩, simp, simpa [(∘)] end, by rwa [this] lemma closure_subtype {p : α → Prop} {x : {a // p a}} {s : set {a // p a}}: x ∈ closure s ↔ x.val ∈ closure (subtype.val '' s) := closure_induced $ assume x y, subtype.eq end subtype section pi lemma nhds_pi {ι : Type u} {α : ι → Type v} [t : ∀i, topological_space (α i)] {a : Πi, α i} : nhds a = (⨅i, vmap (λx, x i) (nhds (a i))) := calc nhds a = (⨅i, @nhds _ (@topological_space.induced _ _ (λx:Πi, α i, x i) (t i)) a) : nhds_supr ... = (⨅i, vmap (λx, x i) (nhds (a i))) : by simp [nhds_induced_eq_vmap] /-- Tychonoff's theorem -/ lemma compact_pi_infinite {ι : Type v} {α : ι → Type u} [∀i:ι, topological_space (α i)] {s : Πi:ι, set (α i)} : (∀i, compact (s i)) → compact {x : Πi:ι, α i | ∀i, x i ∈ s i} := begin simp [compact_iff_ultrafilter_le_nhds, nhds_pi], exact assume h f hf hfs, let p : Πi:ι, filter (α i) := λi, map (λx:Πi:ι, α i, x i) f in have ∀i:ι, ∃a, a∈s i ∧ p i ≤ nhds a, from assume i, h i (p i) (ultrafilter_map hf) $ show preimage (λx:Πi:ι, α i, x i) (s i) ∈ f.sets, from f.upwards_sets hfs $ assume x (hx : ∀i, x i ∈ s i), hx i, let ⟨a, ha⟩ := classical.axiom_of_choice this in ⟨a, assume i, (ha i).left, assume i, le_vmap_iff_map_le.mpr $ (ha i).right⟩ end end pi -- TODO: use embeddings from above! structure dense_embedding [topological_space α] [topological_space β] (e : α → β) := (dense : ∀x, x ∈ closure (e '' univ)) (inj : ∀x y, e x = e y → x = y) (induced : ∀a, vmap e (nhds (e a)) = nhds a) namespace dense_embedding variables [topological_space α] [topological_space β] variables {e : α → β} (de : dense_embedding e) protected lemma embedding (de : dense_embedding e) : embedding e := ⟨de.inj, eq_of_nhds_eq_nhds begin intro a, rw [← de.induced a, nhds_induced_eq_vmap] end⟩ protected lemma tendsto (de : dense_embedding e) {a : α} : tendsto e (nhds a) (nhds (e a)) := by rw [←de.induced a]; exact tendsto_vmap protected lemma continuous (de : dense_embedding e) {a : α} : continuous e := by rw [continuous_iff_tendsto]; exact assume a, de.tendsto lemma inj_iff (de : dense_embedding e) {x y} : e x = e y ↔ x = y := ⟨de.inj _ _, assume h, h ▸ rfl⟩ lemma closure_image_univ : closure (e '' univ) = univ := let h := de.dense in set.ext $ assume x, ⟨assume _, trivial, assume _, @h x⟩ protected lemma nhds_inf_neq_bot (de : dense_embedding e) {b : β} : nhds b ⊓ principal (e '' univ) ≠ ⊥ := begin have h := de.dense, simp [closure_eq_nhds] at h, exact h _ end lemma vmap_nhds_neq_bot (de : dense_embedding e) {b : β} : vmap e (nhds b) ≠ ⊥ := forall_sets_neq_empty_iff_neq_bot.mp $ assume s ⟨t, ht, (hs : preimage e t ⊆ s)⟩, have t ∩ e '' univ ∈ (nhds b ⊓ principal (e '' univ)).sets, from inter_mem_inf_sets ht (subset.refl _), let ⟨x, ⟨hx₁, y, hy, y_eq⟩⟩ := inhabited_of_mem_sets de.nhds_inf_neq_bot this in ne_empty_of_mem $ hs $ show e y ∈ t, from y_eq.symm ▸ hx₁ variables [topological_space γ] [inhabited γ] [regular_space γ] def ext (de : dense_embedding e) (f : α → γ) : β → γ := lim ∘ map f ∘ vmap e ∘ nhds lemma ext_eq {b : β} {c : γ} {f : α → γ} (hf : map f (vmap e (nhds b)) ≤ nhds c) : de.ext f b = c := lim_eq begin simp; exact vmap_nhds_neq_bot de end hf lemma ext_e_eq {a : α} {f : α → γ} (de : dense_embedding e) (hf : map f (nhds a) ≤ nhds (f a)) : de.ext f (e a) = f a := de.ext_eq begin rw de.induced; exact hf end lemma tendsto_ext {b : β} {f : α → γ} (de : dense_embedding e) (hf : {b | ∃c, tendsto f (vmap e $ nhds b) (nhds c)} ∈ (nhds b).sets) : tendsto (de.ext f) (nhds b) (nhds (de.ext f b)) := let φ := {b | tendsto f (vmap e $ nhds b) (nhds $ de.ext f b)} in have hφ : φ ∈ (nhds b).sets, from (nhds b).upwards_sets hf $ assume b ⟨c, hc⟩, show tendsto f (vmap e (nhds b)) (nhds (de.ext f b)), from (de.ext_eq hc).symm ▸ hc, assume s hs, let ⟨s'', hs''₁, hs''₂, hs''₃⟩ := nhds_is_closed hs in let ⟨s', hs'₁, (hs'₂ : preimage e s' ⊆ preimage f s'')⟩ := mem_of_nhds hφ hs''₁ in let ⟨t, (ht₁ : t ⊆ φ ∩ s'), ht₂, ht₃⟩ := mem_nhds_sets_iff.mp $ inter_mem_sets hφ hs'₁ in have h₁ : closure (f '' preimage e s') ⊆ s'', by rw [closure_subset_iff_subset_of_is_closed hs''₃, image_subset_iff]; exact hs'₂, have h₂ : t ⊆ preimage (de.ext f) (closure (f '' preimage e t)), from assume b' hb', have nhds b' ≤ principal t, by simp; exact mem_nhds_sets ht₂ hb', have map f (vmap e (nhds b')) ≤ nhds (de.ext f b') ⊓ principal (f '' preimage e t), from calc _ ≤ map f (vmap e (nhds b' ⊓ principal t)) : map_mono $ vmap_mono $ le_inf (le_refl _) this ... ≤ map f (vmap e (nhds b')) ⊓ map f (vmap e (principal t)) : le_inf (map_mono $ vmap_mono $ inf_le_left) (map_mono $ vmap_mono $ inf_le_right) ... ≤ map f (vmap e (nhds b')) ⊓ principal (f '' preimage e t) : by simp [le_refl] ... ≤ _ : inf_le_inf ((ht₁ hb').left) (le_refl _), show de.ext f b' ∈ closure (f '' preimage e t), begin rw [closure_eq_nhds], apply neq_bot_of_le_neq_bot _ this, simp, exact de.vmap_nhds_neq_bot end, (nhds b).upwards_sets (show t ∈ (nhds b).sets, from mem_nhds_sets ht₂ ht₃) (calc t ⊆ preimage (de.ext f) (closure (f '' preimage e t)) : h₂ ... ⊆ preimage (de.ext f) (closure (f '' preimage e s')) : preimage_mono $ closure_mono $ image_subset f $ preimage_mono $ subset.trans ht₁ $ inter_subset_right _ _ ... ⊆ preimage (de.ext f) s'' : preimage_mono h₁ ... ⊆ preimage (de.ext f) s : preimage_mono hs''₂) lemma continuous_ext {f : α → γ} (de : dense_embedding e) (hf : ∀b, ∃c, tendsto f (vmap e (nhds b)) (nhds c)) : continuous (de.ext f) := continuous_iff_tendsto.mpr $ assume b, de.tendsto_ext $ univ_mem_sets' hf end dense_embedding namespace dense_embedding variables [topological_space α] [topological_space β] [topological_space γ] [topological_space δ] protected def prod {e₁ : α → β} {e₂ : γ → δ} (de₁ : dense_embedding e₁) (de₂ : dense_embedding e₂) : dense_embedding (λ(p : α × γ), (e₁ p.1, e₂ p.2)) := { dense_embedding . dense := have closure ((λ(p : α × γ), (e₁ p.1, e₂ p.2)) '' univ) = set.prod (closure (e₁ '' univ)) (closure (e₂ '' univ)), by rw [←closure_prod_eq, prod_image_image_eq, univ_prod_univ], assume ⟨b, d⟩, begin rw [this], simp, constructor, apply de₁.dense, apply de₂.dense end, inj := assume ⟨x₁, x₂⟩ ⟨y₁, y₂⟩, by simp; exact assume h₁ h₂, ⟨de₁.inj _ _ h₁, de₂.inj _ _ h₂⟩, induced := assume ⟨a, b⟩, by rw [nhds_prod_eq, nhds_prod_eq, ←prod_vmap_vmap_eq, de₁.induced, de₂.induced] } def subtype_emb (p : α → Prop) {e : α → β} (de : dense_embedding e) (x : {x // p x}) : {x // x ∈ closure (e '' {x | p x})} := ⟨e x.1, subset_closure $ mem_image_of_mem e x.2⟩ protected def subtype (p : α → Prop) {e : α → β} (de : dense_embedding e) : dense_embedding (de.subtype_emb p) := { dense_embedding . dense := assume ⟨x, hx⟩, closure_subtype.mpr $ have (λ (x : {x // p x}), e (x.val)) = e ∘ subtype.val, from rfl, begin simp [(image_comp _ _ _).symm, (∘), subtype_emb], rw [this, image_comp, subtype_val_image], simp, assumption end, inj := assume ⟨x, hx⟩ ⟨y, hy⟩ h, subtype.eq $ de.inj x y $ @@congr_arg subtype.val h, induced := assume ⟨x, hx⟩, by simp [subtype_emb, nhds_subtype_eq_vmap, vmap_vmap_comp, (∘), (de.induced x).symm] } end dense_embedding lemma is_closed_property [topological_space α] [topological_space β] {e : α → β} {p : β → Prop} (he : closure (e '' univ) = univ) (hp : is_closed {x | p x}) (h : ∀a, p (e a)) : ∀b, p b := have univ ⊆ {b | p b}, from calc univ = closure (e '' univ) : he.symm ... ⊆ closure {b | p b} : closure_mono $ image_subset_iff.mpr $ assume a _, h a ... = _ : closure_eq_of_is_closed hp, assume b, this trivial lemma is_closed_property2 [topological_space α] [topological_space β] {e : α → β} {p : β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β | p q.1 q.2}) (h : ∀a₁ a₂, p (e a₁) (e a₂)) : ∀b₁ b₂, p b₁ b₂ := have ∀q:β×β, p q.1 q.2, from is_closed_property ((he.prod he).closure_image_univ) hp $ assume ⟨a₁, a₂⟩, h _ _, assume b₁ b₂, this ⟨b₁, b₂⟩ lemma is_closed_property3 [topological_space α] [topological_space β] {e : α → β} {p : β → β → β → Prop} (he : dense_embedding e) (hp : is_closed {q:β×β×β | p q.1 q.2.1 q.2.2}) (h : ∀a₁ a₂ a₃, p (e a₁) (e a₂) (e a₃)) : ∀b₁ b₂ b₃, p b₁ b₂ b₃ := have ∀q:β×β×β, p q.1 q.2.1 q.2.2, from is_closed_property ((he.prod $ he.prod he).closure_image_univ) hp $ assume ⟨a₁, a₂, a₃⟩, h _ _ _, assume b₁ b₂ b₃, this ⟨b₁, b₂, b₃⟩ lemma mem_closure_of_continuous [topological_space α] [topological_space β] {f : α → β} {a : α} {s : set α} {t : set β} (hf : continuous f) (ha : a ∈ closure s) (h : ∀a∈s, f a ∈ closure t) : f a ∈ closure t := calc f a ∈ f '' closure s : mem_image_of_mem _ ha ... ⊆ closure (f '' s) : image_closure_subset_closure_image hf ... ⊆ closure (closure t) : closure_mono $ image_subset_iff.mpr $ h ... ⊆ closure t : begin rw [closure_eq_of_is_closed], exact subset.refl _, exact is_closed_closure end lemma mem_closure_of_continuous2 [topological_space α] [topological_space β] [topological_space γ] {f : α → β → γ} {a : α} {b : β} {s : set α} {t : set β} {u : set γ} (hf : continuous (λp:α×β, f p.1 p.2)) (ha : a ∈ closure s) (hb : b ∈ closure t) (h : ∀a∈s, ∀b∈t, f a b ∈ closure u) : f a b ∈ closure u := have (a,b) ∈ closure (set.prod s t), by simp [closure_prod_eq, ha, hb], show f (a, b).1 (a, b).2 ∈ closure u, from @mem_closure_of_continuous (α×β) _ _ _ (λp:α×β, f p.1 p.2) (a,b) _ u hf this $ assume ⟨p₁, p₂⟩ ⟨h₁, h₂⟩, h p₁ h₁ p₂ h₂
35d2a4a1e2bc266d3ec801e28617c9da342140d5
853df553b1d6ca524e3f0a79aedd32dde5d27ec3
/src/order/filter/lift.lean
5506720ffa09b64138574498b2e177c1eeeeb617
[ "Apache-2.0" ]
permissive
DanielFabian/mathlib
efc3a50b5dde303c59eeb6353ef4c35a345d7112
f520d07eba0c852e96fe26da71d85bf6d40fcc2a
refs/heads/master
1,668,739,922,971
1,595,201,756,000
1,595,201,756,000
279,469,476
0
0
null
1,594,696,604,000
1,594,696,604,000
null
UTF-8
Lean
false
false
16,461
lean
/- Copyright (c) 2019 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl Lift filters along filter and set functions. -/ import order.filter.basic open set open_locale classical filter namespace filter variables {α : Type*} {β : Type*} {γ : Type*} {ι : Sort*} section lift /-- A variant on `bind` using a function `g` taking a set instead of a member of `α`. This is essentially a push-forward along a function mapping each set to a filter. -/ protected def lift (f : filter α) (g : set α → filter β) := ⨅s ∈ f, g s variables {f f₁ f₂ : filter α} {g g₁ g₂ : set α → filter β} lemma mem_lift_sets (hg : monotone g) {s : set β} : s ∈ f.lift g ↔ ∃t∈f, s ∈ g t := mem_binfi (assume s hs t ht, ⟨s ∩ t, inter_mem_sets hs ht, hg $ inter_subset_left s t, hg $ inter_subset_right s t⟩) ⟨univ, univ_mem_sets⟩ lemma mem_lift {s : set β} {t : set α} (ht : t ∈ f) (hs : s ∈ g t) : s ∈ f.lift g := le_principal_iff.mp $ show f.lift g ≤ 𝓟 s, from infi_le_of_le t $ infi_le_of_le ht $ le_principal_iff.mpr hs lemma lift_le {f : filter α} {g : set α → filter β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : g s ≤ h) : f.lift g ≤ h := infi_le_of_le s $ infi_le_of_le hs $ hg lemma le_lift {f : filter α} {g : set α → filter β} {h : filter β} (hh : ∀s∈f, h ≤ g s) : h ≤ f.lift g := le_infi $ assume s, le_infi $ assume hs, hh s hs lemma lift_mono (hf : f₁ ≤ f₂) (hg : g₁ ≤ g₂) : f₁.lift g₁ ≤ f₂.lift g₂ := infi_le_infi $ assume s, infi_le_infi2 $ assume hs, ⟨hf hs, hg s⟩ lemma lift_mono' (hg : ∀s∈f, g₁ s ≤ g₂ s) : f.lift g₁ ≤ f.lift g₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, hg s hs lemma map_lift_eq {m : β → γ} (hg : monotone g) : map m (f.lift g) = f.lift (map m ∘ g) := have monotone (map m ∘ g), from map_mono.comp hg, filter_eq $ set.ext $ by simp only [mem_lift_sets hg, mem_lift_sets this, exists_prop, forall_const, mem_map, iff_self, function.comp_app] lemma comap_lift_eq {m : γ → β} (hg : monotone g) : comap m (f.lift g) = f.lift (comap m ∘ g) := have monotone (comap m ∘ g), from comap_mono.comp hg, filter_eq $ set.ext begin simp only [mem_lift_sets hg, mem_lift_sets this, comap, mem_lift_sets, mem_set_of_eq, exists_prop, function.comp_apply], exact λ s, ⟨λ ⟨b, ⟨a, ha, hb⟩, hs⟩, ⟨a, ha, b, hb, hs⟩, λ ⟨a, ha, b, hb, hs⟩, ⟨b, ⟨a, ha, hb⟩, hs⟩⟩ end theorem comap_lift_eq2 {m : β → α} {g : set β → filter γ} (hg : monotone g) : (comap m f).lift g = f.lift (g ∘ preimage m) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, infi_le_of_le (preimage m s) $ infi_le _ ⟨s, hs, subset.refl _⟩) (le_infi $ assume s, le_infi $ assume ⟨s', hs', (h_sub : preimage m s' ⊆ s)⟩, infi_le_of_le s' $ infi_le_of_le hs' $ hg h_sub) lemma map_lift_eq2 {g : set β → filter γ} {m : α → β} (hg : monotone g) : (map m f).lift g = f.lift (g ∘ image m) := le_antisymm (infi_le_infi2 $ assume s, ⟨image m s, infi_le_infi2 $ assume hs, ⟨ f.sets_of_superset hs $ assume a h, mem_image_of_mem _ h, le_refl _⟩⟩) (infi_le_infi2 $ assume t, ⟨preimage m t, infi_le_infi2 $ assume ht, ⟨ht, hg $ assume x, assume h : x ∈ m '' preimage m t, let ⟨y, hy, h_eq⟩ := h in show x ∈ t, from h_eq ▸ hy⟩⟩) lemma lift_comm {g : filter β} {h : set α → set β → filter γ} : f.lift (λs, g.lift (h s)) = g.lift (λt, f.lift (λs, h s t)) := le_antisymm (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) (le_infi $ assume i, le_infi $ assume hi, le_infi $ assume j, le_infi $ assume hj, infi_le_of_le j $ infi_le_of_le hj $ infi_le_of_le i $ infi_le _ hi) lemma lift_assoc {h : set β → filter γ} (hg : monotone g) : (f.lift g).lift h = f.lift (λs, (g s).lift h) := le_antisymm (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le _ $ (mem_lift_sets hg).mpr ⟨_, hs, ht⟩) (le_infi $ assume t, le_infi $ assume ht, let ⟨s, hs, h'⟩ := (mem_lift_sets hg).mp ht in infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le t $ infi_le _ h') lemma lift_lift_same_le_lift {g : set α → set α → filter β} : f.lift (λs, f.lift (g s)) ≤ f.lift (λs, g s s) := le_infi $ assume s, le_infi $ assume hs, infi_le_of_le s $ infi_le_of_le hs $ infi_le_of_le s $ infi_le _ hs lemma lift_lift_same_eq_lift {g : set α → set α → filter β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) : f.lift (λs, f.lift (g s)) = f.lift (λs, g s s) := le_antisymm lift_lift_same_le_lift (le_infi $ assume s, le_infi $ assume hs, le_infi $ assume t, le_infi $ assume ht, infi_le_of_le (s ∩ t) $ infi_le_of_le (inter_mem_sets hs ht) $ calc g (s ∩ t) (s ∩ t) ≤ g s (s ∩ t) : hg₂ (s ∩ t) (inter_subset_left _ _) ... ≤ g s t : hg₁ s (inter_subset_right _ _)) lemma lift_principal {s : set α} (hg : monotone g) : (𝓟 s).lift g = g s := le_antisymm (infi_le_of_le s $ infi_le _ $ subset.refl _) (le_infi $ assume t, le_infi $ assume hi, hg hi) theorem monotone_lift [preorder γ] {f : γ → filter α} {g : γ → set α → filter β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift (g c)) := assume a b h, lift_mono (hf h) (hg h) lemma lift_ne_bot_iff (hm : monotone g) : (f.lift g ≠ ⊥) ↔ (∀s∈f, g s ≠ ⊥) := begin rw [filter.lift, infi_subtype', infi_ne_bot_iff_of_directed', subtype.forall'], { exact ⟨⟨univ, univ_mem_sets⟩⟩ }, { rintros ⟨s, hs⟩ ⟨t, ht⟩, exact ⟨⟨s ∩ t, inter_mem_sets hs ht⟩, hm (inter_subset_left s t), hm (inter_subset_right s t)⟩ } end @[simp] lemma lift_const {f : filter α} {g : filter β} : f.lift (λx, g) = g := le_antisymm (lift_le univ_mem_sets $ le_refl g) (le_lift $ assume s hs, le_refl g) @[simp] lemma lift_inf {f : filter α} {g h : set α → filter β} : f.lift (λx, g x ⊓ h x) = f.lift g ⊓ f.lift h := by simp only [filter.lift, infi_inf_eq, eq_self_iff_true] @[simp] lemma lift_principal2 {f : filter α} : f.lift 𝓟 = f := le_antisymm (assume s hs, mem_lift hs (mem_principal_self s)) (le_infi $ assume s, le_infi $ assume hs, by simp only [hs, le_principal_iff]) lemma lift_infi {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hg : ∀{s t}, g s ⊓ g t = g (s ∩ t)) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, have g_mono : monotone g, from assume s t h, le_of_inf_eq $ eq.trans hg $ congr_arg g $ inter_eq_self_of_subset_left h, have ∀t∈(infi f), (⨅ (i : ι), filter.lift (f i) g) ≤ g t, from assume t ht, infi_sets_induct ht (let ⟨i⟩ := hι in infi_le_of_le i $ infi_le_of_le univ $ infi_le _ univ_mem_sets) (assume i s₁ s₂ hs₁ hs₂, @hg s₁ s₂ ▸ le_inf (infi_le_of_le i $ infi_le_of_le s₁ $ infi_le _ hs₁) hs₂) (assume s₁ s₂ hs₁ hs₂, le_trans hs₂ $ g_mono hs₁), begin simp only [mem_lift_sets g_mono, exists_imp_distrib], exact assume t ht hs, this t ht hs end) end lift section lift' /-- Specialize `lift` to functions `set α → set β`. This can be viewed as a generalization of `map`. This is essentially a push-forward along a function mapping each set to a set. -/ protected def lift' (f : filter α) (h : set α → set β) := f.lift (𝓟 ∘ h) variables {f f₁ f₂ : filter α} {h h₁ h₂ : set α → set β} lemma mem_lift' {t : set α} (ht : t ∈ f) : h t ∈ (f.lift' h) := le_principal_iff.mp $ show f.lift' h ≤ 𝓟 (h t), from infi_le_of_le t $ infi_le_of_le ht $ le_refl _ lemma mem_lift'_sets (hh : monotone h) {s : set β} : s ∈ (f.lift' h) ↔ (∃t∈f, h t ⊆ s) := mem_lift_sets $ monotone_principal.comp hh lemma lift'_le {f : filter α} {g : set α → set β} {h : filter β} {s : set α} (hs : s ∈ f) (hg : 𝓟 (g s) ≤ h) : f.lift' g ≤ h := lift_le hs hg lemma lift'_mono (hf : f₁ ≤ f₂) (hh : h₁ ≤ h₂) : f₁.lift' h₁ ≤ f₂.lift' h₂ := lift_mono hf $ assume s, principal_mono.mpr $ hh s lemma lift'_mono' (hh : ∀s∈f, h₁ s ⊆ h₂ s) : f.lift' h₁ ≤ f.lift' h₂ := infi_le_infi $ assume s, infi_le_infi $ assume hs, principal_mono.mpr $ hh s hs lemma lift'_cong (hh : ∀s∈f, h₁ s = h₂ s) : f.lift' h₁ = f.lift' h₂ := le_antisymm (lift'_mono' $ assume s hs, le_of_eq $ hh s hs) (lift'_mono' $ assume s hs, le_of_eq $ (hh s hs).symm) lemma map_lift'_eq {m : β → γ} (hh : monotone h) : map m (f.lift' h) = f.lift' (image m ∘ h) := calc map m (f.lift' h) = f.lift (map m ∘ 𝓟 ∘ h) : map_lift_eq $ monotone_principal.comp hh ... = f.lift' (image m ∘ h) : by simp only [(∘), filter.lift', map_principal, eq_self_iff_true] lemma map_lift'_eq2 {g : set β → set γ} {m : α → β} (hg : monotone g) : (map m f).lift' g = f.lift' (g ∘ image m) := map_lift_eq2 $ monotone_principal.comp hg theorem comap_lift'_eq {m : γ → β} (hh : monotone h) : comap m (f.lift' h) = f.lift' (preimage m ∘ h) := calc comap m (f.lift' h) = f.lift (comap m ∘ 𝓟 ∘ h) : comap_lift_eq $ monotone_principal.comp hh ... = f.lift' (preimage m ∘ h) : by simp only [(∘), filter.lift', comap_principal, eq_self_iff_true] theorem comap_lift'_eq2 {m : β → α} {g : set β → set γ} (hg : monotone g) : (comap m f).lift' g = f.lift' (g ∘ preimage m) := comap_lift_eq2 $ monotone_principal.comp hg lemma lift'_principal {s : set α} (hh : monotone h) : (𝓟 s).lift' h = 𝓟 (h s) := lift_principal $ monotone_principal.comp hh lemma principal_le_lift' {t : set β} (hh : ∀s∈f, t ⊆ h s) : 𝓟 t ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, principal_mono.mpr (hh s hs) theorem monotone_lift' [preorder γ] {f : γ → filter α} {g : γ → set α → set β} (hf : monotone f) (hg : monotone g) : monotone (λc, (f c).lift' (g c)) := assume a b h, lift'_mono (hf h) (hg h) lemma lift_lift'_assoc {g : set α → set β} {h : set β → filter γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift h = f.lift (λs, h (g s)) := calc (f.lift' g).lift h = f.lift (λs, (𝓟 (g s)).lift h) : lift_assoc (monotone_principal.comp hg) ... = f.lift (λs, h (g s)) : by simp only [lift_principal, hh, eq_self_iff_true] lemma lift'_lift'_assoc {g : set α → set β} {h : set β → set γ} (hg : monotone g) (hh : monotone h) : (f.lift' g).lift' h = f.lift' (λs, h (g s)) := lift_lift'_assoc hg (monotone_principal.comp hh) lemma lift'_lift_assoc {g : set α → filter β} {h : set β → set γ} (hg : monotone g) : (f.lift g).lift' h = f.lift (λs, (g s).lift' h) := lift_assoc hg lemma lift_lift'_same_le_lift' {g : set α → set α → set β} : f.lift (λs, f.lift' (g s)) ≤ f.lift' (λs, g s s) := lift_lift_same_le_lift lemma lift_lift'_same_eq_lift' {g : set α → set α → set β} (hg₁ : ∀s, monotone (λt, g s t)) (hg₂ : ∀t, monotone (λs, g s t)) : f.lift (λs, f.lift' (g s)) = f.lift' (λs, g s s) := lift_lift_same_eq_lift (assume s, monotone_principal.comp (hg₁ s)) (assume t, monotone_principal.comp (hg₂ t)) lemma lift'_inf_principal_eq {h : set α → set β} {s : set β} : f.lift' h ⊓ 𝓟 s = f.lift' (λt, h t ∩ s) := le_antisymm (le_infi $ assume t, le_infi $ assume ht, calc filter.lift' f h ⊓ 𝓟 s ≤ 𝓟 (h t) ⊓ 𝓟 s : inf_le_inf_right _ (infi_le_of_le t $ infi_le _ ht) ... = _ : by simp only [principal_eq_iff_eq, inf_principal, eq_self_iff_true, function.comp_app]) (le_inf (le_infi $ assume t, le_infi $ assume ht, infi_le_of_le t $ infi_le_of_le ht $ by simp only [le_principal_iff, inter_subset_left, mem_principal_sets, function.comp_app]; exact inter_subset_right _ _) (infi_le_of_le univ $ infi_le_of_le univ_mem_sets $ by simp only [le_principal_iff, inter_subset_right, mem_principal_sets, function.comp_app]; exact inter_subset_left _ _)) lemma lift'_ne_bot_iff (hh : monotone h) : (f.lift' h ≠ ⊥) ↔ (∀s∈f, (h s).nonempty) := calc (f.lift' h ≠ ⊥) ↔ (∀s∈f, 𝓟 (h s) ≠ ⊥) : lift_ne_bot_iff (monotone_principal.comp hh) ... ↔ (∀s∈f, (h s).nonempty) : by simp only [principal_ne_bot_iff] @[simp] lemma lift'_id {f : filter α} : f.lift' id = f := lift_principal2 lemma le_lift' {f : filter α} {h : set α → set β} {g : filter β} (h_le : ∀s∈f, h s ∈ g) : g ≤ f.lift' h := le_infi $ assume s, le_infi $ assume hs, by simp only [h_le, le_principal_iff, function.comp_app]; exact h_le s hs lemma lift_infi' {f : ι → filter α} {g : set α → filter β} (hι : nonempty ι) (hf : directed (≥) f) (hg : monotone g) : (infi f).lift g = (⨅i, (f i).lift g) := le_antisymm (le_infi $ assume i, lift_mono (infi_le _ _) (le_refl _)) (assume s, begin rw mem_lift_sets hg, simp only [exists_imp_distrib, mem_infi hf hι], exact assume t i ht hs, mem_infi_sets i $ mem_lift ht hs end) lemma lift'_infi {f : ι → filter α} {g : set α → set β} (hι : nonempty ι) (hg : ∀{s t}, g s ∩ g t = g (s ∩ t)) : (infi f).lift' g = (⨅i, (f i).lift' g) := lift_infi hι $ by simp only [principal_eq_iff_eq, inf_principal, function.comp_app]; apply assume s t, hg theorem comap_eq_lift' {f : filter β} {m : α → β} : comap m f = f.lift' (preimage m) := filter_eq $ set.ext $ by simp only [mem_lift'_sets, monotone_preimage, comap, exists_prop, forall_const, iff_self, mem_set_of_eq] end lift' section prod variables {f : filter α} lemma prod_def {f : filter α} {g : filter β} : f.prod g = (f.lift $ λs, g.lift' $ set.prod s) := have ∀(s:set α) (t : set β), 𝓟 (set.prod s t) = (𝓟 s).comap prod.fst ⊓ (𝓟 t).comap prod.snd, by simp only [principal_eq_iff_eq, comap_principal, inf_principal]; intros; refl, begin simp only [filter.lift', function.comp, this, -comap_principal, lift_inf, lift_const, lift_inf], rw [← comap_lift_eq monotone_principal, ← comap_lift_eq monotone_principal], simp only [filter.prod, lift_principal2, eq_self_iff_true] end lemma prod_same_eq : filter.prod f f = f.lift' (λt, set.prod t t) := by rw [prod_def]; from lift_lift'_same_eq_lift' (assume s, set.monotone_prod monotone_const monotone_id) (assume t, set.monotone_prod monotone_id monotone_const) lemma mem_prod_same_iff {s : set (α×α)} : s ∈ filter.prod f f ↔ (∃t∈f, set.prod t t ⊆ s) := by rw [prod_same_eq, mem_lift'_sets]; exact set.monotone_prod monotone_id monotone_id lemma tendsto_prod_self_iff {f : α × α → β} {x : filter α} {y : filter β} : filter.tendsto f (filter.prod x x) y ↔ ∀ W ∈ y, ∃ U ∈ x, ∀ (x x' : α), x ∈ U → x' ∈ U → f (x, x') ∈ W := by simp only [tendsto_def, mem_prod_same_iff, prod_sub_preimage_iff, exists_prop, iff_self] variables {α₁ : Type*} {α₂ : Type*} {β₁ : Type*} {β₂ : Type*} lemma prod_lift_lift {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → filter β₁} {g₂ : set α₂ → filter β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift g₁) (f₂.lift g₂) = f₁.lift (λs, f₂.lift (λt, filter.prod (g₁ s) (g₂ t))) := begin simp only [prod_def], rw [lift_assoc], apply congr_arg, funext x, rw [lift_comm], apply congr_arg, funext y, rw [lift'_lift_assoc], exact hg₂, exact hg₁ end lemma prod_lift'_lift' {f₁ : filter α₁} {f₂ : filter α₂} {g₁ : set α₁ → set β₁} {g₂ : set α₂ → set β₂} (hg₁ : monotone g₁) (hg₂ : monotone g₂) : filter.prod (f₁.lift' g₁) (f₂.lift' g₂) = f₁.lift (λs, f₂.lift' (λt, set.prod (g₁ s) (g₂ t))) := begin rw [prod_def, lift_lift'_assoc], apply congr_arg, funext x, rw [lift'_lift'_assoc], exact hg₂, exact set.monotone_prod monotone_const monotone_id, exact hg₁, exact (monotone_lift' monotone_const $ monotone_lam $ assume x, set.monotone_prod monotone_id monotone_const) end end prod end filter
fa85631f2f7415b30e5e39c02f7fca5e0d883cca
4950bf76e5ae40ba9f8491647d0b6f228ddce173
/src/algebra/quandle.lean
fd1da89dc06899c17fe91a401f63800bf8d5d525
[ "Apache-2.0" ]
permissive
ntzwq/mathlib
ca50b21079b0a7c6781c34b62199a396dd00cee2
36eec1a98f22df82eaccd354a758ef8576af2a7f
refs/heads/master
1,675,193,391,478
1,607,822,996,000
1,607,822,996,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
24,396
lean
/- Copyright (c) 2020 Kyle Miller. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Kyle Miller -/ import data.zmod.basic import data.equiv.mul_add import tactic.group /-! # Racks and Quandles This file defines racks and quandles, algebraic structures for sets that bijectively act on themselves with a self-distributivity property. If `R` is a rack and `act : R → (R ≃ R)` is the self-action, then the self-distributivity is, equivalently, that ``` act (act x y) = act x * act y * (act x)⁻¹ ``` where multiplication is composition in `R ≃ R` as a group. Quandles are racks such that `act x x = x` for all `x`. One example of a quandle (not yet in mathlib) is the action of a Lie algebra on itself, defined by `act x y = Ad (exp x) y`. Quandles and racks were independently developed by multiple mathematicians. David Joyce introduced quandles in his thesis [Joyce1982] to define an algebraic invariant of knot and link complements that is analogous to the fundamental group of the exterior, and he showed that the quandle associated to an oriented knot is invariant up to orientation-reversed mirror image. Racks were used by Fenn and Rourke for framed codimension-2 knots and links.[FennRourke1992] The name "rack" came from wordplay by Conway and Wraith for the "wrack and ruin" of forgetting everything but the conjugation operation for a group. ## Main definitions * `shelf` is a type with a self-distributive action * `rack` is a shelf whose action for each element is invertible * `quandle` is a rack whose action for an element fixes that element * `quandle.conj` defines a quandle of a group acting on itself by conjugation. * `shelf_hom` is homomorphisms of shelves, racks, and quandles. * `rack.envel_group` gives the universal group the rack maps to as a conjugation quandle. * `rack.opp` gives the rack with the action replaced by its inverse. ## Main statements * `rack.envel_group` is left adjoint to `quandle.conj` (`to_envel_group.map`). The universality statements are `to_envel_group.univ` and `to_envel_group.univ_uniq`. ## Notation The following notation is localized in `quandles`: * `x ◃ y` is `shelf.act x y` * `x ◃⁻¹ y` is `rack.inv_act x y` * `S →◃ S'` is `shelf_hom S S'` Use `open_locale quandles` to use these. ## Todo * If g is the Lie algebra of a Lie group G, then (x ◃ y) = Ad (exp x) x forms a quandle * If X is a symmetric space, then each point has a corresponding involution that acts on X, forming a quandle. * Alexander quandle with `a ◃ b = t * b + (1 - t) * b`, with `a` and `b` elements of a module over Z[t,t⁻¹]. * If G is a group, H a subgroup, and z in H, then there is a quandle `(G/H;z)` defined by `yH ◃ xH = yzy⁻¹xH`. Every homogeneous quandle (i.e., a quandle Q whose automorphism group acts transitively on Q as a set) is isomorphic to such a quandle. There is a generalization to this arbitrary quandles in [Joyce's paper (Theorem 7.2)][Joyce1982]. ## Tags rack, quandle -/ open opposite universes u v /-- A *shelf* is a structure with a self-distributive binary operation. The binary operation is regarded as a left action of the type on itself. -/ class shelf (α : Type u) := (act : α → α → α) (self_distrib : ∀ {x y z : α}, act x (act y z) = act (act x y) (act x z)) /-- The type of homomorphisms between shelves. This is also the notion of rack and quandle homomorphisms. -/ @[ext] structure shelf_hom (S₁ : Type*) (S₂ : Type*) [shelf S₁] [shelf S₂] := (to_fun : S₁ → S₂) (map_act' : ∀ {x y : S₁}, to_fun (shelf.act x y) = shelf.act (to_fun x) (to_fun y)) /-- A *rack* is an automorphic set (a set with an action on itself by bijections) that is self-distributive. It is a shelf such that each element's action is invertible. The notations `x ◃ y` and `x ◃⁻¹ y` denote the action and the inverse action, respectively, and they are right associative. -/ class rack (α : Type u) extends shelf α := (inv_act : α → α → α) (left_inv : ∀ x, function.left_inverse (inv_act x) (act x)) (right_inv : ∀ x, function.right_inverse (inv_act x) (act x)) localized "infixr ` ◃ `:65 := shelf.act" in quandles localized "infixr ` ◃⁻¹ `:65 := rack.inv_act" in quandles localized "infixr ` →◃ `:25 := shelf_hom" in quandles open_locale quandles namespace rack variables {R : Type*} [rack R] lemma self_distrib {x y z : R} : x ◃ (y ◃ z) = (x ◃ y) ◃ (x ◃ z) := shelf.self_distrib /-- A rack acts on itself by equivalences. -/ def act (x : R) : R ≃ R := { to_fun := shelf.act x, inv_fun := inv_act x, left_inv := left_inv x, right_inv := right_inv x } @[simp] lemma act_apply (x y : R) : act x y = x ◃ y := rfl @[simp] lemma act_symm_apply (x y : R) : (act x).symm y = x ◃⁻¹ y := rfl @[simp] lemma inv_act_apply (x y : R) : (act x)⁻¹ y = x ◃⁻¹ y := rfl @[simp] lemma inv_act_act_eq (x y : R) : x ◃⁻¹ x ◃ y = y := left_inv x y @[simp] lemma act_inv_act_eq (x y : R) : x ◃ x ◃⁻¹ y = y := right_inv x y lemma left_cancel (x : R) {y y' : R} : x ◃ y = x ◃ y' ↔ y = y' := by { split, apply (act x).injective, rintro rfl, refl } lemma left_cancel_inv (x : R) {y y' : R} : x ◃⁻¹ y = x ◃⁻¹ y' ↔ y = y' := by { split, apply (act x).symm.injective, rintro rfl, refl } lemma self_distrib_inv {x y z : R} : x ◃⁻¹ y ◃⁻¹ z = (x ◃⁻¹ y) ◃⁻¹ (x ◃⁻¹ z) := begin rw [←left_cancel (x ◃⁻¹ y), right_inv, ←left_cancel x, right_inv, self_distrib], repeat {rw right_inv }, end /-- The *adjoint action* of a rack on itself is `op'`, and the adjoint action of `x ◃ y` is the conjugate of the action of `y` by the action of `x`. It is another way to understand the self-distributivity axiom. This is used in the natural rack homomorphism `to_conj` from `R` to `conj (R ≃ R)` defined by `op'`. -/ lemma ad_conj {R : Type*} [rack R] (x y : R) : act (x ◃ y) = act x * act y * (act x)⁻¹ := begin apply @mul_right_cancel _ _ _ (act x), ext z, simp only [inv_mul_cancel_right], apply self_distrib.symm, end /-- The opposite rack, swapping the roles of `◃` and `◃⁻¹`. -/ instance opposite_rack : rack Rᵒᵖ := { act := λ x y, op (inv_act (unop x) (unop y)), self_distrib := λ (x y z : Rᵒᵖ), begin op_induction x, op_induction y, op_induction z, simp only [unop_op, op_inj_iff], exact self_distrib_inv, end, inv_act := λ x y, op (shelf.act (unop x) (unop y)), left_inv := λ x y, begin op_induction x, op_induction y, simp, end, right_inv := λ x y, begin op_induction x, op_induction y, simp, end } @[simp] lemma op_act_op_eq {x y : R} : (op x) ◃ (op y) = op (x ◃⁻¹ y) := rfl @[simp] lemma op_inv_act_op_eq {x y : R} : (op x) ◃⁻¹ (op y) = op (x ◃ y) := rfl @[simp] lemma self_act_act_eq {x y : R} : (x ◃ x) ◃ y = x ◃ y := by { rw [←right_inv x y, ←self_distrib] } @[simp] lemma self_inv_act_inv_act_eq {x y : R} : (x ◃⁻¹ x) ◃⁻¹ y = x ◃⁻¹ y := by { have h := @self_act_act_eq _ _ (op x) (op y), simpa using h } @[simp] lemma self_act_inv_act_eq {x y : R} : (x ◃ x) ◃⁻¹ y = x ◃⁻¹ y := by { rw ←left_cancel (x ◃ x), rw right_inv, rw self_act_act_eq, rw right_inv } @[simp] lemma self_inv_act_act_eq {x y : R} : (x ◃⁻¹ x) ◃ y = x ◃ y := by { have h := @self_act_inv_act_eq _ _ (op x) (op y), simpa using h } lemma self_act_eq_iff_eq {x y : R} : x ◃ x = y ◃ y ↔ x = y := begin split, swap, rintro rfl, refl, intro h, transitivity (x ◃ x) ◃⁻¹ (x ◃ x), rw [←left_cancel (x ◃ x), right_inv, self_act_act_eq], rw [h, ←left_cancel (y ◃ y), right_inv, self_act_act_eq], end lemma self_inv_act_eq_iff_eq {x y : R} : x ◃⁻¹ x = y ◃⁻¹ y ↔ x = y := by { have h := @self_act_eq_iff_eq _ _ (op x) (op y), simpa using h } /-- The map `x ↦ x ◃ x` is a bijection. (This has applications for the regular isotopy version of the Reidemeister I move for knot diagrams.) -/ def self_apply_equiv (R : Type*) [rack R] : R ≃ R := { to_fun := λ x, x ◃ x, inv_fun := λ x, x ◃⁻¹ x, left_inv := λ x, by simp, right_inv := λ x, by simp } /-- An involutory rack is one for which `rack.op R x` is an involution for every x. -/ def is_involutory (R : Type*) [rack R] : Prop := ∀ x : R, function.involutive (shelf.act x) lemma involutory_inv_act_eq_act {R : Type*} [rack R] (h : is_involutory R) (x y : R) : x ◃⁻¹ y = x ◃ y := begin rw [←left_cancel x, right_inv], exact ((h x).left_inverse y).symm, end /-- An abelian rack is one for which the mediality axiom holds. -/ def is_abelian (R : Type*) [rack R] : Prop := ∀ (x y z w : R), (x ◃ y) ◃ (z ◃ w) = (x ◃ z) ◃ (y ◃ w) /-- Associative racks are uninteresting. -/ lemma assoc_iff_id {R : Type*} [rack R] {x y z : R} : x ◃ y ◃ z = (x ◃ y) ◃ z ↔ x ◃ z = z := by { rw self_distrib, rw left_cancel } end rack namespace shelf_hom variables {S₁ : Type*} {S₂ : Type*} {S₃ : Type*} [shelf S₁] [shelf S₂] [shelf S₃] instance : has_coe_to_fun (S₁ →◃ S₂) := ⟨_, shelf_hom.to_fun⟩ @[simp] lemma to_fun_eq_coe (f : S₁ →◃ S₂) : f.to_fun = f := rfl @[simp] lemma map_act (f : S₁ →◃ S₂) {x y : S₁} : f (x ◃ y) = f x ◃ f y := map_act' f /-- The identity homomorphism -/ def id (S : Type*) [shelf S] : S →◃ S := { to_fun := id, map_act' := by simp } instance inhabited (S : Type*) [shelf S] : inhabited (S →◃ S) := ⟨id S⟩ /-- The composition of shelf homomorphisms -/ def comp (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) : S₁ →◃ S₃ := { to_fun := g.to_fun ∘ f.to_fun, map_act' := by simp } @[simp] lemma comp_apply (g : S₂ →◃ S₃) (f : S₁ →◃ S₂) (x : S₁) : (g.comp f) x = g (f x) := rfl end shelf_hom /-- A quandle is a rack such that each automorphism fixes its corresponding element. -/ class quandle (α : Type*) extends rack α := (fix : ∀ {x : α}, act x x = x) namespace quandle open rack variables {Q : Type*} [quandle Q] attribute [simp] fix @[simp] lemma fix_inv {x : Q} : x ◃⁻¹ x = x := by { rw ←left_cancel x, simp } instance opposite_quandle : quandle Qᵒᵖ := { fix := λ x, by { op_induction x, simp } } /-- The conjugation quandle of a group. Each element of the group acts by the corresponding inner automorphism. -/ @[nolint has_inhabited_instance] def conj (G : Type*) := G instance conj.quandle (G : Type*) [group G] : quandle (conj G) := { act := (λ x, @mul_aut.conj G _ x), self_distrib := λ x y z, begin dsimp only [mul_equiv.to_equiv_apply, mul_aut.conj_apply, conj], group, end, inv_act := (λ x, (@mul_aut.conj G _ x).symm), left_inv := λ x y, by { dsimp [act, conj], group }, right_inv := λ x y, by { dsimp [act, conj], group }, fix := λ x, by simp } @[simp] lemma conj_act_eq_conj {G : Type*} [group G] (x y : conj G) : x ◃ y = ((x : G) * (y : G) * (x : G)⁻¹ : G) := rfl lemma conj_swap {G : Type*} [group G] (x y : conj G) : x ◃ y = y ↔ y ◃ x = x := begin dsimp, split, repeat { intro h, conv_rhs { rw eq_mul_inv_of_mul_eq (eq_mul_inv_of_mul_eq h) }, simp, }, end /-- `conj` is functorial -/ def conj.map {G : Type*} {H : Type*} [group G] [group H] (f : G →* H) : conj G →◃ conj H := { to_fun := f, map_act' := by simp } instance {G : Type*} {H : Type*} [group G] [group H] : has_lift (G →* H) (conj G →◃ conj H) := { lift := conj.map } /-- The dihedral quandle. This is the conjugation quandle of the dihedral group restrict to flips. Used for Fox n-colorings of knots. -/ @[nolint has_inhabited_instance] def dihedral (n : ℕ) := zmod n /-- The operation for the dihedral quandle. It does not need to be an equivalence because it is an involution (see `dihedral_act.inv`). -/ def dihedral_act (n : ℕ) (a : zmod n) : zmod n → zmod n := λ b, 2 * a - b lemma dihedral_act.inv (n : ℕ) (a : zmod n) : function.involutive (dihedral_act n a) := by { intro b, dsimp [dihedral_act], ring } instance (n : ℕ) : quandle (dihedral n) := { act := dihedral_act n, self_distrib := λ x y z, begin dsimp [function.involutive.to_equiv, dihedral_act], ring, end, inv_act := dihedral_act n, left_inv := λ x, (dihedral_act.inv n x).left_inverse, right_inv := λ x, (dihedral_act.inv n x).right_inverse, fix := λ x, begin dsimp [function.involutive.to_equiv, dihedral_act], ring, end } end quandle namespace rack /-- This is the natural rack homomorphism to the conjugation quandle of the group `R ≃ R` that acts on the rack. -/ def to_conj (R : Type*) [rack R] : R →◃ quandle.conj (R ≃ R) := { to_fun := act, map_act' := ad_conj } section envel_group /-! ### Universal enveloping group of a rack The universal enveloping group `envel_group R` of a rack `R` is the universal group such that every rack homomorphism `R →◃ conj G` is induced by a unique group homomorphism `envel_group R →* G`. For quandles, Joyce called this group `AdConj R`. The `envel_group` functor is left adjoint to the `conj` forgetful functor, and the way we construct the enveloping group is via a technique that should work for left adjoints of forgetful functors in general. It involves thinking a little about 2-categories, but the payoff is that the map `envel_group R →* G` has a nice description. Let's think of a group as being a one-object category. The first step is to define `pre_envel_group`, which gives formal expressions for all the 1-morphisms and includes the unit element, elements of `R`, multiplication, and inverses. To introduce relations, the second step is to define `pre_envel_group_rel'`, which gives formal expressions for all 2-morphisms between the 1-morphisms. The 2-morphisms include associativity, multiplication by the unit, multiplication by inverses, compatibility with multiplication and inverses (`congr_mul` and `congr_inv`), the axioms for an equivalence relation, and, importantly, the relationship between conjugation and the rack action (see `rack.ad_conj`). None of this forms a 2-category yet, for example due to lack of associativity of `trans`. The `pre_envel_group_rel` relation is a `Prop`-valued version of `pre_envel_group_rel'`, and making it `Prop`-valued essentially introduces enough 3-isomorphisms so that every pair of compatible 2-morphisms is isomorphic. Now, while composition in `pre_envel_group` does not strictly satisfy the category axioms, `pre_envel_group` and `pre_envel_group_rel'` do form a weak 2-category. Since we just want a 1-category, the last step is to quotient `pre_envel_group` by `pre_envel_group_rel'`, and the result is the group `envel_group`. For a homomorphism `f : R →◃ conj G`, how does `envel_group.map f : envel_group R →* G` work? Let's think of `G` as being a 2-category with one object, a 1-morphism per element of `G`, and a single 2-morphism called `eq.refl` for each 1-morphism. We define the map using a "higher `quotient.lift`" -- not only do we evaluate elements of `pre_envel_group` as expressions in `G` (this is `to_envel_group.map_aux`), but we evaluate elements of `pre_envel_group'` as expressions of 2-morphisms of `G` (this is `to_envel_group.map_aux.well_def`). That is to say, `to_envel_group.map_aux.well_def` recursively evaluates formal expressions of 2-morphisms as equality proofs in `G`. Now that all morphisms are accounted for, the map descends to a homomorphism `envel_group R →* G`. Note: `Type`-valued relations are not common. The fact it is `Type`-valued is what makes `to_envel_group.map_aux.well_def` have well-founded recursion. -/ /-- Free generators of the enveloping group. -/ inductive pre_envel_group (R : Type u) : Type u | unit : pre_envel_group | incl (x : R) : pre_envel_group | mul (a b : pre_envel_group) : pre_envel_group | inv (a : pre_envel_group) : pre_envel_group instance pre_envel_group.inhabited (R : Type u) : inhabited (pre_envel_group R) := ⟨pre_envel_group.unit⟩ open pre_envel_group /-- Relations for the enveloping group. This is a type-valued relation because `to_envel_group.map_aux.well_def` inducts on it to show `to_envel_group.map` is well-defined. The relation `pre_envel_group_rel` is the `Prop`-valued version, which is used to define `envel_group` itself. -/ inductive pre_envel_group_rel' (R : Type u) [rack R] : pre_envel_group R → pre_envel_group R → Type u | refl {a : pre_envel_group R} : pre_envel_group_rel' a a | symm {a b : pre_envel_group R} (hab : pre_envel_group_rel' a b) : pre_envel_group_rel' b a | trans {a b c : pre_envel_group R} (hab : pre_envel_group_rel' a b) (hbc : pre_envel_group_rel' b c) : pre_envel_group_rel' a c | congr_mul {a b a' b' : pre_envel_group R} (ha : pre_envel_group_rel' a a') (hb : pre_envel_group_rel' b b') : pre_envel_group_rel' (mul a b) (mul a' b') | congr_inv {a a' : pre_envel_group R} (ha : pre_envel_group_rel' a a') : pre_envel_group_rel' (inv a) (inv a') | assoc (a b c : pre_envel_group R) : pre_envel_group_rel' (mul (mul a b) c) (mul a (mul b c)) | one_mul (a : pre_envel_group R) : pre_envel_group_rel' (mul unit a) a | mul_one (a : pre_envel_group R) : pre_envel_group_rel' (mul a unit) a | mul_left_inv (a : pre_envel_group R) : pre_envel_group_rel' (mul (inv a) a) unit | act_incl (x y : R) : pre_envel_group_rel' (mul (mul (incl x) (incl y)) (inv (incl x))) (incl (x ◃ y)) instance pre_envel_group_rel'.inhabited (R : Type u) [rack R] : inhabited (pre_envel_group_rel' R unit unit) := ⟨pre_envel_group_rel'.refl⟩ /-- The `pre_envel_group_rel` relation as a `Prop`. Used as the relation for `pre_envel_group.setoid`. -/ inductive pre_envel_group_rel (R : Type u) [rack R] : pre_envel_group R → pre_envel_group R → Prop | rel {a b : pre_envel_group R} (r : pre_envel_group_rel' R a b) : pre_envel_group_rel a b /-- A quick way to convert a `pre_envel_group_rel'` to a `pre_envel_group_rel`. -/ lemma pre_envel_group_rel'.rel {R : Type u} [rack R] {a b : pre_envel_group R} : pre_envel_group_rel' R a b → pre_envel_group_rel R a b := pre_envel_group_rel.rel @[refl] lemma pre_envel_group_rel.refl {R : Type u} [rack R] {a : pre_envel_group R} : pre_envel_group_rel R a a := pre_envel_group_rel.rel pre_envel_group_rel'.refl @[symm] lemma pre_envel_group_rel.symm {R : Type u} [rack R] {a b : pre_envel_group R} : pre_envel_group_rel R a b → pre_envel_group_rel R b a | ⟨r⟩ := r.symm.rel @[trans] lemma pre_envel_group_rel.trans {R : Type u} [rack R] {a b c : pre_envel_group R} : pre_envel_group_rel R a b → pre_envel_group_rel R b c → pre_envel_group_rel R a c | ⟨rab⟩ ⟨rbc⟩ := (rab.trans rbc).rel instance pre_envel_group.setoid (R : Type*) [rack R] : setoid (pre_envel_group R) := { r := pre_envel_group_rel R, iseqv := begin split, apply pre_envel_group_rel.refl, split, apply pre_envel_group_rel.symm, apply pre_envel_group_rel.trans end } /-- The universal enveloping group for the rack R. -/ def envel_group (R : Type*) [rack R] := quotient (pre_envel_group.setoid R) instance (R : Type*) [rack R] : group (envel_group R) := { mul := λ a b, quotient.lift_on₂ a b (λ a b, ⟦pre_envel_group.mul a b⟧) (λ a b a' b' ⟨ha⟩ ⟨hb⟩, quotient.sound (pre_envel_group_rel'.congr_mul ha hb).rel), one := ⟦unit⟧, inv := λ a, quotient.lift_on a (λ a, ⟦pre_envel_group.inv a⟧) (λ a a' ⟨ha⟩, quotient.sound (pre_envel_group_rel'.congr_inv ha).rel), mul_assoc := λ a b c, quotient.induction_on₃ a b c (λ a b c, quotient.sound (pre_envel_group_rel'.assoc a b c).rel), one_mul := λ a, quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.one_mul a).rel), mul_one := λ a, quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.mul_one a).rel), mul_left_inv := λ a, quotient.induction_on a (λ a, quotient.sound (pre_envel_group_rel'.mul_left_inv a).rel) } instance envel_group.inhabited (R : Type*) [rack R] : inhabited (envel_group R) := ⟨1⟩ /-- The canonical homomorphism from a rack to its enveloping group. Satisfies universal properties given by `to_envel_group.map` and `to_envel_group.univ`. -/ def to_envel_group (R : Type*) [rack R] : R →◃ quandle.conj (envel_group R) := { to_fun := λ x, ⟦incl x⟧, map_act' := λ x y, quotient.sound (pre_envel_group_rel'.act_incl x y).symm.rel } /-- The preliminary definition of the induced map from the enveloping group. See `to_envel_group.map`. -/ def to_envel_group.map_aux {R : Type*} [rack R] {G : Type*} [group G] (f : R →◃ quandle.conj G) : pre_envel_group R → G | unit := 1 | (incl x) := f x | (mul a b) := to_envel_group.map_aux a * to_envel_group.map_aux b | (inv a) := (to_envel_group.map_aux a)⁻¹ namespace to_envel_group.map_aux open pre_envel_group_rel' /-- Show that `to_envel_group.map_aux` sends equivalent expressions to equal terms. -/ lemma well_def {R : Type*} [rack R] {G : Type*} [group G] (f : R →◃ quandle.conj G) : Π {a b : pre_envel_group R}, pre_envel_group_rel' R a b → to_envel_group.map_aux f a = to_envel_group.map_aux f b | a b refl := rfl | a b (symm h) := (well_def h).symm | a b (trans hac hcb) := eq.trans (well_def hac) (well_def hcb) | _ _ (congr_mul ha hb) := by { simp [to_envel_group.map_aux, well_def ha, well_def hb] } | _ _ (congr_inv ha) := by { simp [to_envel_group.map_aux, well_def ha] } | _ _ (assoc a b c) := by { apply mul_assoc } | _ _ (one_mul a) := by { simp [to_envel_group.map_aux] } | _ _ (mul_one a) := by { simp [to_envel_group.map_aux] } | _ _ (mul_left_inv a) := by { simp [to_envel_group.map_aux] } | _ _ (act_incl x y) := by { simp [to_envel_group.map_aux] } end to_envel_group.map_aux /-- Given a map from a rack to a group, lift it to being a map from the enveloping group. More precisely, the `envel_group` functor is left adjoint to `quandle.conj`. -/ def to_envel_group.map {R : Type*} [rack R] {G : Type*} [group G] : (R →◃ quandle.conj G) ≃ (envel_group R →* G) := { to_fun := λ f, { to_fun := λ x, quotient.lift_on x (to_envel_group.map_aux f) (λ a b ⟨hab⟩, to_envel_group.map_aux.well_def f hab), map_one' := begin change quotient.lift_on ⟦unit⟧ (to_envel_group.map_aux f) _ = 1, simp [to_envel_group.map_aux], end, map_mul' := λ x y, quotient.induction_on₂ x y (λ x y, begin change quotient.lift_on ⟦mul x y⟧ (to_envel_group.map_aux f) _ = _, simp [to_envel_group.map_aux], end) }, inv_fun := λ F, (quandle.conj.map F).comp (to_envel_group R), left_inv := λ f, by { ext, refl }, right_inv := λ F, monoid_hom.ext $ λ x, quotient.induction_on x $ λ x, begin induction x, { exact F.map_one.symm, }, { refl, }, { have hm : ⟦x_a.mul x_b⟧ = @has_mul.mul (envel_group R) _ ⟦x_a⟧ ⟦x_b⟧ := rfl, rw [hm, F.map_mul, monoid_hom.map_mul, ←x_ih_a, ←x_ih_b] }, { have hm : ⟦x_a.inv⟧ = @has_inv.inv (envel_group R) _ ⟦x_a⟧ := rfl, rw [hm, F.map_inv, monoid_hom.map_inv, x_ih], } end, } /-- Given a homomorphism from a rack to a group, it factors through the enveloping group. -/ lemma to_envel_group.univ (R : Type*) [rack R] (G : Type*) [group G] (f : R →◃ quandle.conj G) : (quandle.conj.map (to_envel_group.map f)).comp (to_envel_group R) = f := to_envel_group.map.symm_apply_apply f /-- The homomorphism `to_envel_group.map f` is the unique map that fits into the commutative triangle in `to_envel_group.univ`. -/ lemma to_envel_group.univ_uniq (R : Type*) [rack R] (G : Type*) [group G] (f : R →◃ quandle.conj G) (g : envel_group R →* G) (h : f = (quandle.conj.map g).comp (to_envel_group R)) : g = to_envel_group.map f := h.symm ▸ (to_envel_group.map.apply_symm_apply g).symm /-- The induced group homomorphism from the enveloping group into bijections of the rack, using `rack.to_conj`. Satisfies the property `envel_action_prop`. This gives the rack `R` the structure of an augmented rack over `envel_group R`. -/ def envel_action {R : Type*} [rack R] : envel_group R →* (R ≃ R) := to_envel_group.map (to_conj R) @[simp] lemma envel_action_prop {R : Type*} [rack R] (x y : R) : envel_action (to_envel_group R x) y = x ◃ y := rfl end envel_group end rack
cdc9c9e219a3621fc77296840c6e92d0ab11faac
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/adjunction/opposites.lean
8a9e9573d4f95d3f40bd4c7e69c525e317414d10
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
10,927
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta, Thomas Read, Andrew Yang -/ import category_theory.adjunction.basic import category_theory.yoneda import category_theory.opposites /-! # Opposite adjunctions > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This file contains constructions to relate adjunctions of functors to adjunctions of their opposites. These constructions are used to show uniqueness of adjoints (up to natural isomorphism). ## Tags adjunction, opposite, uniqueness -/ open category_theory universes v₁ v₂ u₁ u₂ -- morphism levels before object levels. See note [category_theory universes]. variables {C : Type u₁} [category.{v₁} C] {D : Type u₂} [category.{v₂} D] namespace category_theory.adjunction /-- If `G.op` is adjoint to `F.op` then `F` is adjoint to `G`. -/ @[simps unit_app counit_app] def adjoint_of_op_adjoint_op (F : C ⥤ D) (G : D ⥤ C) (h : G.op ⊣ F.op) : F ⊣ G := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, ((h.hom_equiv (opposite.op Y) (opposite.op X)).trans (op_equiv _ _)).symm.trans (op_equiv _ _) } /-- If `G` is adjoint to `F.op` then `F` is adjoint to `G.unop`. -/ def adjoint_unop_of_adjoint_op (F : C ⥤ D) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G ⊣ F.op) : F ⊣ G.unop := adjoint_of_op_adjoint_op F G.unop (h.of_nat_iso_left G.op_unop_iso.symm) /-- If `G.op` is adjoint to `F` then `F.unop` is adjoint to `G`. -/ def unop_adjoint_of_op_adjoint (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : D ⥤ C) (h : G.op ⊣ F) : F.unop ⊣ G := adjoint_of_op_adjoint_op _ _ (h.of_nat_iso_right F.op_unop_iso.symm) /-- If `G` is adjoint to `F` then `F.unop` is adjoint to `G.unop`. -/ def unop_adjoint_unop_of_adjoint (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G ⊣ F) : F.unop ⊣ G.unop := adjoint_unop_of_adjoint_op F.unop G (h.of_nat_iso_right F.op_unop_iso.symm) /-- If `G` is adjoint to `F` then `F.op` is adjoint to `G.op`. -/ @[simps unit_app counit_app] def op_adjoint_op_of_adjoint (F : C ⥤ D) (G : D ⥤ C) (h : G ⊣ F) : F.op ⊣ G.op := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, (op_equiv _ Y).trans ((h.hom_equiv _ _).symm.trans (op_equiv X (opposite.op _)).symm) } /-- If `G` is adjoint to `F.unop` then `F` is adjoint to `G.op`. -/ def adjoint_op_of_adjoint_unop (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : D ⥤ C) (h : G ⊣ F.unop) : F ⊣ G.op := (op_adjoint_op_of_adjoint F.unop _ h).of_nat_iso_left F.op_unop_iso /-- If `G.unop` is adjoint to `F` then `F.op` is adjoint to `G`. -/ def op_adjoint_of_unop_adjoint (F : C ⥤ D) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G.unop ⊣ F) : F.op ⊣ G := (op_adjoint_op_of_adjoint _ G.unop h).of_nat_iso_right G.op_unop_iso /-- If `G.unop` is adjoint to `F.unop` then `F` is adjoint to `G`. -/ def adjoint_of_unop_adjoint_unop (F : Cᵒᵖ ⥤ Dᵒᵖ) (G : Dᵒᵖ ⥤ Cᵒᵖ) (h : G.unop ⊣ F.unop) : F ⊣ G := (adjoint_op_of_adjoint_unop _ _ h).of_nat_iso_right G.op_unop_iso /-- If `F` and `F'` are both adjoint to `G`, there is a natural isomorphism `F.op ⋙ coyoneda ≅ F'.op ⋙ coyoneda`. We use this in combination with `fully_faithful_cancel_right` to show left adjoints are unique. -/ def left_adjoints_coyoneda_equiv {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G): F.op ⋙ coyoneda ≅ F'.op ⋙ coyoneda := nat_iso.of_components (λ X, nat_iso.of_components (λ Y, ((adj1.hom_equiv X.unop Y).trans (adj2.hom_equiv X.unop Y).symm).to_iso) (by tidy)) (by tidy) /-- If `F` and `F'` are both left adjoint to `G`, then they are naturally isomorphic. -/ def left_adjoint_uniq {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) : F ≅ F' := nat_iso.remove_op (fully_faithful_cancel_right _ (left_adjoints_coyoneda_equiv adj2 adj1)) @[simp] lemma hom_equiv_left_adjoint_uniq_hom_app {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (x : C) : adj1.hom_equiv _ _ ((left_adjoint_uniq adj1 adj2).hom.app x) = adj2.unit.app x := begin apply (adj1.hom_equiv _ _).symm.injective, apply quiver.hom.op_inj, apply coyoneda.map_injective, swap, apply_instance, ext f y, simpa [left_adjoint_uniq, left_adjoints_coyoneda_equiv] end @[simp, reassoc] lemma unit_left_adjoint_uniq_hom {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) : adj1.unit ≫ whisker_right (left_adjoint_uniq adj1 adj2).hom G = adj2.unit := begin ext x, rw [nat_trans.comp_app, ← hom_equiv_left_adjoint_uniq_hom_app adj1 adj2], simp [-hom_equiv_left_adjoint_uniq_hom_app, ←G.map_comp] end @[simp, reassoc] lemma unit_left_adjoint_uniq_hom_app {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (x : C) : adj1.unit.app x ≫ G.map ((left_adjoint_uniq adj1 adj2).hom.app x) = adj2.unit.app x := by { rw ← unit_left_adjoint_uniq_hom adj1 adj2, refl } @[simp, reassoc] lemma left_adjoint_uniq_hom_counit {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) : whisker_left G (left_adjoint_uniq adj1 adj2).hom ≫ adj2.counit = adj1.counit := begin ext x, apply quiver.hom.op_inj, apply coyoneda.map_injective, swap, apply_instance, ext y f, have : F.map (adj2.unit.app (G.obj x)) ≫ adj1.counit.app (F'.obj (G.obj x)) ≫ adj2.counit.app x ≫ f = adj1.counit.app x ≫ f, { erw [← adj1.counit.naturality, ← F.map_comp_assoc], simpa }, simpa [left_adjoint_uniq, left_adjoints_coyoneda_equiv] using this end @[simp, reassoc] lemma left_adjoint_uniq_hom_app_counit {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (x : D) : (left_adjoint_uniq adj1 adj2).hom.app (G.obj x) ≫ adj2.counit.app x = adj1.counit.app x := by { rw ← left_adjoint_uniq_hom_counit adj1 adj2, refl } @[simp] lemma left_adjoint_uniq_inv_app {F F' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (x : C) : (left_adjoint_uniq adj1 adj2).inv.app x = (left_adjoint_uniq adj2 adj1).hom.app x := rfl @[simp, reassoc] lemma left_adjoint_uniq_trans {F F' F'' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (adj3 : F'' ⊣ G) : (left_adjoint_uniq adj1 adj2).hom ≫ (left_adjoint_uniq adj2 adj3).hom = (left_adjoint_uniq adj1 adj3).hom := begin ext, apply quiver.hom.op_inj, apply coyoneda.map_injective, swap, apply_instance, ext, simp [left_adjoints_coyoneda_equiv, left_adjoint_uniq] end @[simp, reassoc] lemma left_adjoint_uniq_trans_app {F F' F'' : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G) (adj3 : F'' ⊣ G) (x : C) : (left_adjoint_uniq adj1 adj2).hom.app x ≫ (left_adjoint_uniq adj2 adj3).hom.app x = (left_adjoint_uniq adj1 adj3).hom.app x := by { rw ← left_adjoint_uniq_trans adj1 adj2 adj3, refl } @[simp] lemma left_adjoint_uniq_refl {F : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) : (left_adjoint_uniq adj1 adj1).hom = 𝟙 _ := begin ext, apply quiver.hom.op_inj, apply coyoneda.map_injective, swap, apply_instance, ext, simp [left_adjoints_coyoneda_equiv, left_adjoint_uniq] end /-- If `G` and `G'` are both right adjoint to `F`, then they are naturally isomorphic. -/ def right_adjoint_uniq {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') : G ≅ G' := nat_iso.remove_op (left_adjoint_uniq (op_adjoint_op_of_adjoint _ F adj2) (op_adjoint_op_of_adjoint _ _ adj1)) @[simp] lemma hom_equiv_symm_right_adjoint_uniq_hom_app {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (x : D) : (adj2.hom_equiv _ _).symm ((right_adjoint_uniq adj1 adj2).hom.app x) = adj1.counit.app x := begin apply quiver.hom.op_inj, convert hom_equiv_left_adjoint_uniq_hom_app (op_adjoint_op_of_adjoint _ F adj2) (op_adjoint_op_of_adjoint _ _ adj1) (opposite.op x), simpa end @[simp, reassoc] lemma unit_right_adjoint_uniq_hom_app {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (x : C) : adj1.unit.app x ≫ (right_adjoint_uniq adj1 adj2).hom.app (F.obj x) = adj2.unit.app x := begin apply quiver.hom.op_inj, convert left_adjoint_uniq_hom_app_counit (op_adjoint_op_of_adjoint _ _ adj2) (op_adjoint_op_of_adjoint _ _ adj1) (opposite.op x), all_goals { simpa } end @[simp, reassoc] lemma unit_right_adjoint_uniq_hom {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') : adj1.unit ≫ whisker_left F (right_adjoint_uniq adj1 adj2).hom = adj2.unit := by { ext x, simp } @[simp, reassoc] lemma right_adjoint_uniq_hom_app_counit {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (x : D) : F.map ((right_adjoint_uniq adj1 adj2).hom.app x) ≫ adj2.counit.app x = adj1.counit.app x := begin apply quiver.hom.op_inj, convert unit_left_adjoint_uniq_hom_app (op_adjoint_op_of_adjoint _ _ adj2) (op_adjoint_op_of_adjoint _ _ adj1) (opposite.op x), all_goals { simpa } end @[simp, reassoc] lemma right_adjoint_uniq_hom_counit {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') : whisker_right (right_adjoint_uniq adj1 adj2).hom F ≫ adj2.counit = adj1.counit := by { ext, simp } @[simp] lemma right_adjoint_uniq_inv_app {F : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (x : D) : (right_adjoint_uniq adj1 adj2).inv.app x = (right_adjoint_uniq adj2 adj1).hom.app x := rfl @[simp, reassoc] lemma right_adjoint_uniq_trans_app {F : C ⥤ D} {G G' G'' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (adj3 : F ⊣ G'') (x : D) : (right_adjoint_uniq adj1 adj2).hom.app x ≫ (right_adjoint_uniq adj2 adj3).hom.app x = (right_adjoint_uniq adj1 adj3).hom.app x := begin apply quiver.hom.op_inj, exact left_adjoint_uniq_trans_app (op_adjoint_op_of_adjoint _ _ adj3) (op_adjoint_op_of_adjoint _ _ adj2) (op_adjoint_op_of_adjoint _ _ adj1) (opposite.op x) end @[simp, reassoc] lemma right_adjoint_uniq_trans {F : C ⥤ D} {G G' G'' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F ⊣ G') (adj3 : F ⊣ G'') : (right_adjoint_uniq adj1 adj2).hom ≫ (right_adjoint_uniq adj2 adj3).hom = (right_adjoint_uniq adj1 adj3).hom := by { ext, simp } @[simp] lemma right_adjoint_uniq_refl {F : C ⥤ D} {G : D ⥤ C} (adj1 : F ⊣ G) : (right_adjoint_uniq adj1 adj1).hom = 𝟙 _ := by { delta right_adjoint_uniq, simp } /-- Given two adjunctions, if the left adjoints are naturally isomorphic, then so are the right adjoints. -/ def nat_iso_of_left_adjoint_nat_iso {F F' : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G') (l : F ≅ F') : G ≅ G' := right_adjoint_uniq adj1 (adj2.of_nat_iso_left l.symm) /-- Given two adjunctions, if the right adjoints are naturally isomorphic, then so are the left adjoints. -/ def nat_iso_of_right_adjoint_nat_iso {F F' : C ⥤ D} {G G' : D ⥤ C} (adj1 : F ⊣ G) (adj2 : F' ⊣ G') (r : G ≅ G') : F ≅ F' := left_adjoint_uniq adj1 (adj2.of_nat_iso_right r.symm) end category_theory.adjunction
9b587267c9f632ed49ad73f7a3628b71f5d2528e
75c54c8946bb4203e0aaf196f918424a17b0de99
/old/language_vector.lean
59d0a9a8da9aefd265bb6427fde7cc7d3bd128cf
[ "Apache-2.0" ]
permissive
urkud/flypitch
261e2a45f1038130178575406df8aea78255ba77
2250f5eda14b6ef9fc3e4e1f4a9ac4005634de5c
refs/heads/master
1,653,266,469,246
1,577,819,679,000
1,577,819,679,000
259,862,235
1
0
Apache-2.0
1,588,147,244,000
1,588,147,244,000
null
UTF-8
Lean
false
false
4,244
lean
/- This definition of the "term" datatype is mutually inductive, along with a "term_vec" datatype. It would be more elegant and probably easier to use one inductive type Lean library's "vector" datatype, but it doesn't work out of the box due to (vector α n) taking the type α as an argument: inductive term' : Type | var : ℕ → Type | apply : ∀ f : L.functions, vector term' (L.arityF f) → term' If we use this definition, there is an error message saying the nested occurrence of the type "contains variables that are not parameters." The implementation here also has problems: Lean cannot figure out how to prove that induction over the mutually inductive types term/term_vec is well-founded. One of Lean's basic strategies to accomplish this is the has_sizeof typeclass; has_sizeof.sizeof is, ideally, a function from a Type to ℕ which decreases w.r.t. complexity; For example sizeof (term.apply f t_vec) should be greater than sizeof t_vec; and in turn sizeof t_vec should be greater than that of its members. The two functions sizeof_vec and sizeof_term_vec are meant to accomplish this, but it hasn't been done quite right; I think the problem might have to do with the use of "sizeof" within the two functions: because the function sizeof_term is passed an instance of has_sizeof term, this means that the and means that the outermost "sizeof term" is slightly, and invisibly, different than the innermost "sizeof term". Typeclasses are a mystery to me. Whatever the problem, it leads to this cryptic error , which you can see below on the first use of free_vars_vec: nested exception message: failed state: L : Language, f : L.functions, l : term_vec L (L.arityF f), this : 1 + (L.arityF f + sizeof_term_vec L (L.arityF f) l) < sizeof_term L (term.apply f l) ⊢ 1 + (L.arityF f + sizeof_term_vec L (L.arityF f) l) < sizeof_term L (term.apply f l) There must be a slight difference between the type of "this" and the conclusion Lean wishes to reach, or it could be discharged immediately. -/ import data.vector structure Language := language :: (relations : Type) (functions : Type ) (arityF : functions → nat) (arityR : relations → nat) variable L : Language universe u mutual inductive term, term_vec with term : Type | var : nat → term | apply: ∀ (f : L.functions ), (term_vec (L.arityF f)) → term with term_vec : Π (n : nat), Type | nil : term_vec 0 | cons : Π {n: nat}, term → term_vec n → term_vec (n+1) def term_vec.length {n : nat} : term_vec L n → ℕ := λ t, n def in_term_vec : Π n, term L → term_vec L n → Prop | 0 t (term_vec.nil L) := false | (m + 1) t (term_vec.cons t' ts) := t = t' ∨ in_term_vec m t ts def rel_term : term L → term L → Prop | (term.var _ n) (term.var _ m) := false | t (term.apply f ts) := (in_term_vec L (term_vec.length L ts) t ts ) ∨ (∃ t', (in_term_vec L (term_vec.length L ts) t' ts ∧ rel_term t t')) def sizeof_term_vec [β : has_sizeof (term L)] : Π n, term_vec L n → nat | 0 (term_vec.nil L) := 0 | _ (term_vec.cons (term.var _ _ ) ts) := 1 + sizeof ts | _ (term_vec.cons (term.apply f ts') ts) := sizeof ts' + sizeof ts instance term_vec_has_sizeof {n : nat} : has_sizeof (term_vec L n) := ⟨ sizeof_term_vec L n ⟩ def sizeof_term [β : has_sizeof (Π n, (term_vec L n))] : term L → ℕ | (term.var _ _ ) := 0 | (term.apply f ts) := 2 + (L.arityF f) + sizeof ts instance term_has_sizeof : has_sizeof (term L) := ⟨ sizeof_term L ⟩ mutual def free_vars, free_vars_vec with free_vars : (term L) → list nat | (term.var L n) := [n] | (term.apply f l) := have 1 + (L.arityF f + sizeof_term_vec L (L.arityF f) l) < sizeof_term L (term.apply f l), by {simp [sizeof_term, sizeof, has_sizeof.sizeof, add_lt_add_left ], admit}, free_vars_vec (L.arityF f) l with free_vars_vec : Π n, (term_vec L n) → list nat | 0 (term_vec.nil L) := [] | _ (term_vec.cons t ts) := nat_list_union (free_vars t) (free_vars_vec ts) definition substitute_formula : list nat → list (term L) → formula L := sorry
53d3884e433acd8a531377755642d0988c81b781
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/field_theory/chevalley_warning.lean
48ff66c4c355682d5002159db2dc95c3042a8a43
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
3,259
lean
/- Copyright (c) 2019 Johan Commelin. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johan Commelin -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.mv_polynomial.default import Mathlib.field_theory.finite.basic import Mathlib.PostPort universes u_1 u_2 u_3 namespace Mathlib /-! # The Chevalley–Warning theorem This file contains a proof of the Chevalley–Warning theorem. Throughout most of this file, `K` denotes a finite field and `q` is notation for the cardinality of `K`. ## Main results 1. Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : σ`) such that the total degree of `f` is less than `(q-1)` times the cardinality of `σ`. Then the evaluation of `f` on all points of `σ → K` (aka `K^σ`) sums to `0`. (`sum_mv_polynomial_eq_zero`) 2. The Chevalley–Warning theorem (`char_dvd_card_solutions`). Let `f i` be a finite family of multivariate polynomials in finitely many variables (`X s`, `s : σ`) such that the sum of the total degrees of the `f i` is less than the cardinality of `σ`. Then the number of common solutions of the `f i` is divisible by the characteristic of `K`. ## Notation - `K` is a finite field - `q` is notation for the cardinality of `K` - `σ` is the indexing type for the variables of a multivariate polynomial ring over `K` -/ theorem mv_polynomial.sum_mv_polynomial_eq_zero {K : Type u_1} {σ : Type u_2} [fintype K] [field K] [fintype σ] [DecidableEq σ] (f : mv_polynomial σ K) (h : mv_polynomial.total_degree f < (fintype.card K - 1) * fintype.card σ) : (finset.sum finset.univ fun (x : σ → K) => coe_fn (mv_polynomial.eval x) f) = 0 := sorry /-- The Chevalley–Warning theorem. Let `(f i)` be a finite family of multivariate polynomials in finitely many variables (`X s`, `s : σ`) over a finite field of characteristic `p`. Assume that the sum of the total degrees of the `f i` is less than the cardinality of `σ`. Then the number of common solutions of the `f i` is divisible by `p`. -/ theorem char_dvd_card_solutions_family {K : Type u_1} {σ : Type u_2} [fintype K] [field K] [fintype σ] [DecidableEq K] [DecidableEq σ] (p : ℕ) [char_p K p] {ι : Type u_3} {s : finset ι} {f : ι → mv_polynomial σ K} (h : (finset.sum s fun (i : ι) => mv_polynomial.total_degree (f i)) < fintype.card σ) : p ∣ fintype.card (Subtype fun (x : σ → K) => ∀ (i : ι), i ∈ s → coe_fn (mv_polynomial.eval x) (f i) = 0) := sorry /-- The Chevalley–Warning theorem. Let `f` be a multivariate polynomial in finitely many variables (`X s`, `s : σ`) over a finite field of characteristic `p`. Assume that the total degree of `f` is less than the cardinality of `σ`. Then the number of solutions of `f` is divisible by `p`. See `char_dvd_card_solutions_family` for a version that takes a family of polynomials `f i`. -/ theorem char_dvd_card_solutions {K : Type u_1} {σ : Type u_2} [fintype K] [field K] [fintype σ] [DecidableEq K] [DecidableEq σ] (p : ℕ) [char_p K p] {f : mv_polynomial σ K} (h : mv_polynomial.total_degree f < fintype.card σ) : p ∣ fintype.card (Subtype fun (x : σ → K) => coe_fn (mv_polynomial.eval x) f = 0) := sorry
631092b82200a24876a2bd5054cd683ec1c67cd7
8ac070e5078ade6d9ca9577e76883158ced4a937
/Numbers/IsFinite/basic.lean
c4cbefbc23b28c5a8bf5c02e8f21a8454e0eca94
[]
no_license
kbuzzard/Numbers
2e5cff4c0102e242e30edf47c7d2547c053500f6
c0166915ee06e901bd46c9168a158c4d139211af
refs/heads/master
1,679,975,844,345
1,617,144,136,000
1,617,144,136,000
353,096,483
2
0
null
null
null
null
UTF-8
Lean
false
false
1,074
lean
import Numbers.Equiv.basic inductive IsFinite : Type → Prop | empty : IsFinite Empty | option : IsFinite α → IsFinite (Option α) | equiv : IsFinite α → (α ≃ β) → IsFinite β attribute [class] IsFinite open IsFinite instance : IsFinite Empty := empty instance : IsFinite Unit := equiv (option inferInstance) OptionEmptyEquivUnit #check Subsingleton instance (α : Type) [Subsingleton α] : IsFinite α := by cases (SubsingletonEquivEmptyOrUnit α) with | inl h => exact equiv inferInstance h.1.symm; | inr h => exact equiv inferInstance h.1.symm; -- how to do this in term mode? instance (α : Type) [Subsingleton α] : IsFinite α := by cases (SubsingletonEquivEmptyOrUnit α) with | _ h => exact equiv inferInstance h.1.symm; instance (α β : Type) [hα : IsFinite α] [hβ : IsFinite β] : IsFinite (Sum α β) := match hα with | empty => equiv hβ (SumEmptyEquiv β).symm | option h => sorry -- need induction :-/ | equiv h e => sorry --class IsFinite (α : Type) := --(is_finite : ∃ L : List α, ∀ a : α, a ∈ L )
feb969d8947a92774a926d341d2b1754fb7e6d24
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/data/set/intervals/order_iso.lean
b477ced88c324ffd46d260b69707584844447a34
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
3,301
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Patrick Massot, Yury Kudryashov, Rémy Degenne -/ import data.set.intervals.basic import order.hom.set /-! # Lemmas about images of intervals under order isomorphisms. > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. -/ variables {α β : Type*} open set namespace order_iso section preorder variables [preorder α] [preorder β] @[simp] lemma preimage_Iic (e : α ≃o β) (b : β) : e ⁻¹' (Iic b) = Iic (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Ici (e : α ≃o β) (b : β) : e ⁻¹' (Ici b) = Ici (e.symm b) := by { ext x, simp [← e.le_iff_le] } @[simp] lemma preimage_Iio (e : α ≃o β) (b : β) : e ⁻¹' (Iio b) = Iio (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Ioi (e : α ≃o β) (b : β) : e ⁻¹' (Ioi b) = Ioi (e.symm b) := by { ext x, simp [← e.lt_iff_lt] } @[simp] lemma preimage_Icc (e : α ≃o β) (a b : β) : e ⁻¹' (Icc a b) = Icc (e.symm a) (e.symm b) := by simp [← Ici_inter_Iic] @[simp] lemma preimage_Ico (e : α ≃o β) (a b : β) : e ⁻¹' (Ico a b) = Ico (e.symm a) (e.symm b) := by simp [← Ici_inter_Iio] @[simp] lemma preimage_Ioc (e : α ≃o β) (a b : β) : e ⁻¹' (Ioc a b) = Ioc (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iic] @[simp] lemma preimage_Ioo (e : α ≃o β) (a b : β) : e ⁻¹' (Ioo a b) = Ioo (e.symm a) (e.symm b) := by simp [← Ioi_inter_Iio] @[simp] lemma image_Iic (e : α ≃o β) (a : α) : e '' (Iic a) = Iic (e a) := by rw [e.image_eq_preimage, e.symm.preimage_Iic, e.symm_symm] @[simp] lemma image_Ici (e : α ≃o β) (a : α) : e '' (Ici a) = Ici (e a) := e.dual.image_Iic a @[simp] lemma image_Iio (e : α ≃o β) (a : α) : e '' (Iio a) = Iio (e a) := by rw [e.image_eq_preimage, e.symm.preimage_Iio, e.symm_symm] @[simp] lemma image_Ioi (e : α ≃o β) (a : α) : e '' (Ioi a) = Ioi (e a) := e.dual.image_Iio a @[simp] lemma image_Ioo (e : α ≃o β) (a b : α) : e '' (Ioo a b) = Ioo (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ioo, e.symm_symm] @[simp] lemma image_Ioc (e : α ≃o β) (a b : α) : e '' (Ioc a b) = Ioc (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ioc, e.symm_symm] @[simp] lemma image_Ico (e : α ≃o β) (a b : α) : e '' (Ico a b) = Ico (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Ico, e.symm_symm] @[simp] lemma image_Icc (e : α ≃o β) (a b : α) : e '' (Icc a b) = Icc (e a) (e b) := by rw [e.image_eq_preimage, e.symm.preimage_Icc, e.symm_symm] end preorder /-- Order isomorphism between `Iic (⊤ : α)` and `α` when `α` has a top element -/ def Iic_top [preorder α] [order_top α] : set.Iic (⊤ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Iic (⊤ : α)) (λ x, le_top)), } /-- Order isomorphism between `Ici (⊥ : α)` and `α` when `α` has a bottom element -/ def Ici_bot [preorder α] [order_bot α] : set.Ici (⊥ : α) ≃o α := { map_rel_iff' := λ x y, by refl, .. (@equiv.subtype_univ_equiv α (set.Ici (⊥ : α)) (λ x, bot_le)) } end order_iso
8194e09dba8534168ea19dae26879e8f70234e10
1dd482be3f611941db7801003235dc84147ec60a
/src/linear_algebra/basic.lean
00d6f8122e43ee8f1297173a1838228200a24aec
[ "Apache-2.0" ]
permissive
sanderdahmen/mathlib
479039302bd66434bb5672c2a4cecf8d69981458
8f0eae75cd2d8b7a083cf935666fcce4565df076
refs/heads/master
1,587,491,322,775
1,549,672,060,000
1,549,672,060,000
169,748,224
0
0
Apache-2.0
1,549,636,694,000
1,549,636,694,000
null
UTF-8
Lean
false
false
47,803
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro, Kevin Buzzard Basics of linear algebra. This sets up the "categorical/lattice structure" of modules, submodules, and linear maps. -/ import algebra.pi_instances data.finsupp order.order_iso open function lattice reserve infix `≃ₗ` : 50 universes u v w x y z variables {α : Type u} {β : Type v} {γ : Type w} {δ : Type y} {ε : Type z} {ι : Type x} namespace finset lemma smul_sum [ring γ] [add_comm_group β] [module γ β] {s : finset α} {a : γ} {f : α → β} : a • (s.sum f) = s.sum (λc, a • f c) := (finset.sum_hom ((•) a)).symm end finset namespace finsupp lemma smul_sum [has_zero β] [ring γ] [add_comm_group δ] [module γ δ] {v : α →₀ β} {c : γ} {h : α → β → δ} : c • (v.sum h) = v.sum (λa b, c • h a b) := finset.smul_sum end finsupp namespace linear_map section variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] [add_comm_group ε] variables [module α β] [module α γ] [module α δ] [module α ε] variables (f g : β →ₗ[α] γ) include α @[simp] theorem comp_id : f.comp id = f := linear_map.ext $ λ x, rfl @[simp] theorem id_comp : id.comp f = f := linear_map.ext $ λ x, rfl theorem comp_assoc (g : γ →ₗ[α] δ) (h : δ →ₗ[α] ε) : (h.comp g).comp f = h.comp (g.comp f) := rfl def cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h : ∀c, f c ∈ p) : γ →ₗ[α] p := by refine {to_fun := λc, ⟨f c, h c⟩, ..}; intros; apply set_coe.ext; simp @[simp] theorem cod_restrict_apply (p : submodule α β) (f : γ →ₗ[α] β) {h} (x : γ) : (cod_restrict p f h x : β) = f x := rfl def inverse (g : γ → β) (h₁ : left_inverse g f) (h₂ : right_inverse g f) : γ →ₗ[α] β := by dsimp [left_inverse, function.right_inverse] at h₁ h₂; exact ⟨g, λ x y, by rw [← h₁ (g (x + y)), ← h₁ (g x + g y)]; simp [h₂], λ a b, by rw [← h₁ (g (a • b)), ← h₁ (a • g b)]; simp [h₂]⟩ instance : has_zero (β →ₗ[α] γ) := ⟨⟨λ _, 0, by simp, by simp⟩⟩ @[simp] lemma zero_apply (x : β) : (0 : β →ₗ[α] γ) x = 0 := rfl instance : has_neg (β →ₗ[α] γ) := ⟨λ f, ⟨λ b, - f b, by simp, by simp⟩⟩ @[simp] lemma neg_apply (x : β) : (- f) x = - f x := rfl instance : has_add (β →ₗ[α] γ) := ⟨λ f g, ⟨λ b, f b + g b, by simp, by simp [smul_add]⟩⟩ @[simp] lemma add_apply (x : β) : (f + g) x = f x + g x := rfl instance : add_comm_group (β →ₗ[α] γ) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; intros; ext; simp instance linear_map.is_add_group_hom : is_add_group_hom f := by refine_struct {..}; simp instance linear_map_apply_is_add_group_hom (a : β) : is_add_group_hom (λ f : β →ₗ[α] γ, f a) := by refine_struct {..}; simp lemma sum_apply [decidable_eq δ] (t : finset δ) (f : δ → β →ₗ[α] γ) (b : β) : t.sum f b = t.sum (λd, f d b) := (@finset.sum_hom _ _ _ t f _ _ (λ g : β →ₗ[α] γ, g b) _).symm @[simp] lemma sub_apply (x : β) : (f - g) x = f x - g x := rfl def smul_right (f : γ →ₗ[α] α) (x : β) : γ →ₗ[α] β := ⟨λb, f b • x, by simp [add_smul], by simp [smul_smul]⟩. @[simp] theorem smul_right_apply (f : γ →ₗ[α] α) (x : β) (c : γ) : (smul_right f x : γ → β) c = f c • x := rfl instance : has_one (β →ₗ[α] β) := ⟨linear_map.id⟩ instance : has_mul (β →ₗ[α] β) := ⟨linear_map.comp⟩ @[simp] lemma one_app (x : β) : (1 : β →ₗ[α] β) x = x := rfl @[simp] lemma mul_app (A B : β →ₗ[α] β) (x : β) : (A * B) x = A (B x) := rfl @[simp] theorem comp_zero : f.comp (0 : δ →ₗ[α] β) = 0 := ext $ assume c, by rw [comp_apply, zero_apply, zero_apply, f.map_zero] @[simp] theorem zero_comp : (0 : γ →ₗ[α] δ).comp f = 0 := rfl section variables (α β) include β -- declaring this an instance breaks `real.lean` with reaching max. instance resolution depth def endomorphism_ring : ring (β →ₗ[α] β) := by refine {mul := (*), one := 1, ..linear_map.add_comm_group, ..}; { intros, apply linear_map.ext, simp } /-- The group of invertible linear maps from `β` to itself -/ def general_linear_group := by haveI := endomorphism_ring α β; exact units (β →ₗ[α] β) end section variables (α β γ) def fst : β × γ →ₗ[α] β := ⟨prod.fst, λ x y, rfl, λ x y, rfl⟩ def snd : β × γ →ₗ[α] γ := ⟨prod.snd, λ x y, rfl, λ x y, rfl⟩ end @[simp] theorem fst_apply (x : β × γ) : fst α β γ x = x.1 := rfl @[simp] theorem snd_apply (x : β × γ) : snd α β γ x = x.2 := rfl def pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : β →ₗ[α] γ × δ := ⟨λ x, (f x, g x), λ x y, by simp, λ x y, by simp⟩ @[simp] theorem pair_apply (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (x : β) : pair f g x = (f x, g x) := rfl @[simp] theorem fst_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (fst α γ δ).comp (pair f g) = f := by ext; refl @[simp] theorem snd_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : (snd α γ δ).comp (pair f g) = g := by ext; refl @[simp] theorem pair_fst_snd : pair (fst α β γ) (snd α β γ) = linear_map.id := by ext; refl section variables (α β γ) def inl : β →ₗ[α] β × γ := by refine ⟨prod.inl, _, _⟩; intros; simp [prod.inl] def inr : γ →ₗ[α] β × γ := by refine ⟨prod.inr, _, _⟩; intros; simp [prod.inr] end @[simp] theorem inl_apply (x : β) : inl α β γ x = (x, 0) := rfl @[simp] theorem inr_apply (x : γ) : inr α β γ x = (0, x) := rfl def copair (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : β × γ →ₗ[α] δ := ⟨λ x, f x.1 + g x.2, λ x y, by simp, λ x y, by simp [smul_add]⟩ @[simp] theorem copair_apply (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (x : β) (y : γ) : copair f g (x, y) = f x + g y := rfl @[simp] theorem copair_inl (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inl α β γ) = f := by ext; simp @[simp] theorem copair_inr (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) : (copair f g).comp (inr α β γ) = g := by ext; simp @[simp] theorem copair_inl_inr : copair (inl α β γ) (inr α β γ) = linear_map.id := by ext ⟨x, y⟩; simp theorem fst_eq_copair : fst α β γ = copair linear_map.id 0 := by ext ⟨x, y⟩; simp theorem snd_eq_copair : snd α β γ = copair 0 linear_map.id := by ext ⟨x, y⟩; simp theorem inl_eq_pair : inl α β γ = pair linear_map.id 0 := rfl theorem inr_eq_pair : inr α β γ = pair 0 linear_map.id := rfl end section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f g : β →ₗ[α] γ) include α instance : has_scalar α (β →ₗ[α] γ) := ⟨λ a f, ⟨λ b, a • f b, by simp [smul_add], by simp [smul_smul, mul_comm]⟩⟩ @[simp] lemma smul_apply (a : α) (x : β) : (a • f) x = a • f x := rfl instance : module α (β →ₗ[α] γ) := module.of_core $ by refine { smul := (•), ..}; intros; ext; simp [smul_add, add_smul, smul_smul] def congr_right (f : γ →ₗ[α] δ) : (β →ₗ[α] γ) →ₗ[α] (β →ₗ[α] δ) := ⟨linear_map.comp f, λ _ _, linear_map.ext $ λ _, f.2 _ _, λ _ _, linear_map.ext $ λ _, f.3 _ _⟩ theorem smul_comp (g : γ →ₗ[α] δ) (a : α) : (a • g).comp f = a • (g.comp f) := rfl theorem comp_smul (g : γ →ₗ[α] δ) (a : α) : g.comp (a • f) = a • (g.comp f) := ext $ assume b, by rw [comp_apply, smul_apply, g.map_smul]; refl end comm_ring end linear_map namespace submodule variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (p p' : submodule α β) (q q' : submodule α γ) variables {r : α} {x y : β} open set lattice instance : partial_order (submodule α β) := partial_order.lift (coe : submodule α β → set β) $ λ a b, ext' lemma le_def {p p' : submodule α β} : p ≤ p' ↔ (p : set β) ⊆ p' := iff.rfl lemma le_def' {p p' : submodule α β} : p ≤ p' ↔ ∀ x ∈ p, x ∈ p' := iff.rfl def of_le {p p' : submodule α β} (h : p ≤ p') : p →ₗ[α] p' := linear_map.cod_restrict _ p.subtype $ λ ⟨x, hx⟩, h hx @[simp] theorem of_le_apply {p p' : submodule α β} (h : p ≤ p') (x : p) : (of_le h x : β) = x := rfl instance : has_bot (submodule α β) := ⟨by split; try {exact {0}}; simp {contextual := tt}⟩ @[simp] lemma bot_coe : ((⊥ : submodule α β) : set β) = {0} := rfl section variables (α) @[simp] lemma mem_bot : x ∈ (⊥ : submodule α β) ↔ x = 0 := mem_singleton_iff end instance : order_bot (submodule α β) := { bot := ⊥, bot_le := λ p x, by simp {contextual := tt}, ..submodule.partial_order } instance : has_top (submodule α β) := ⟨by split; try {exact set.univ}; simp⟩ @[simp] lemma top_coe : ((⊤ : submodule α β) : set β) = univ := rfl @[simp] lemma mem_top : x ∈ (⊤ : submodule α β) := trivial instance : order_top (submodule α β) := { top := ⊤, le_top := λ p x _, trivial, ..submodule.partial_order } instance : has_Inf (submodule α β) := ⟨λ S, { carrier := ⋂ s ∈ S, ↑s, zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ private lemma Inf_le' {S : set (submodule α β)} {p} : p ∈ S → Inf S ≤ p := bInter_subset_of_mem private lemma le_Inf' {S : set (submodule α β)} {p} : (∀p' ∈ S, p ≤ p') → p ≤ Inf S := subset_bInter instance : has_inf (submodule α β) := ⟨λ p p', { carrier := p ∩ p', zero := by simp, add := by simp [add_mem] {contextual := tt}, smul := by simp [smul_mem] {contextual := tt} }⟩ instance : complete_lattice (submodule α β) := { sup := λ a b, Inf {x | a ≤ x ∧ b ≤ x}, le_sup_left := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, ha, le_sup_right := λ a b, le_Inf' $ λ x ⟨ha, hb⟩, hb, sup_le := λ a b c h₁ h₂, Inf_le' ⟨h₁, h₂⟩, inf := (⊓), le_inf := λ a b c, subset_inter, inf_le_left := λ a b, inter_subset_left _ _, inf_le_right := λ a b, inter_subset_right _ _, Sup := λtt, Inf {t | ∀t'∈tt, t' ≤ t}, le_Sup := λ s p hs, le_Inf' $ λ p' hp', hp' _ hs, Sup_le := λ s p hs, Inf_le' hs, Inf := Inf, le_Inf := λ s a, le_Inf', Inf_le := λ s a, Inf_le', ..submodule.lattice.order_top, ..submodule.lattice.order_bot } instance : add_comm_monoid (submodule α β) := { add := (⊔), add_assoc := λ _ _ _, sup_assoc, zero := ⊥, zero_add := λ _, bot_sup_eq, add_zero := λ _, sup_bot_eq, add_comm := λ _ _, sup_comm } @[simp] lemma add_eq_sup (M N : submodule α β) : M + N = M ⊔ N := rfl @[simp] lemma zero_eq_bot : (0 : submodule α β) = ⊥ := rfl lemma eq_top_iff' {p : submodule α β} : p = ⊤ ↔ ∀ x, x ∈ p := eq_top_iff.trans ⟨λ h x, @h x trivial, λ h x _, h x⟩ @[simp] theorem inf_coe : (p ⊓ p' : set β) = p ∩ p' := rfl @[simp] theorem mem_inf {p p' : submodule α β} : x ∈ p ⊓ p' ↔ x ∈ p ∧ x ∈ p' := iff.rfl @[simp] theorem Inf_coe (P : set (submodule α β)) : (↑(Inf P) : set β) = ⋂ p ∈ P, ↑p := rfl @[simp] theorem infi_coe {ι} (p : ι → submodule α β) : (↑⨅ i, p i : set β) = ⋂ i, ↑(p i) := by rw [infi, Inf_coe]; ext a; simp; exact ⟨λ h i, h _ i rfl, λ h i x e, e ▸ h _⟩ @[simp] theorem mem_infi {ι} (p : ι → submodule α β) : x ∈ (⨅ i, p i) ↔ ∀ i, x ∈ p i := by rw [← mem_coe, infi_coe, mem_Inter]; refl theorem disjoint_def {p p' : submodule α β} : disjoint p p' ↔ ∀ x ∈ p, x ∈ p' → x = (0:β) := show (∀ x, x ∈ p ∧ x ∈ p' → x ∈ ({0} : set β)) ↔ _, by simp /-- The pushforward -/ def map (f : β →ₗ[α] γ) (p : submodule α β) : submodule α γ := { carrier := f '' p, zero := ⟨0, p.zero_mem, f.map_zero⟩, add := by rintro _ _ ⟨b₁, hb₁, rfl⟩ ⟨b₂, hb₂, rfl⟩; exact ⟨_, p.add_mem hb₁ hb₂, f.map_add _ _⟩, smul := by rintro a _ ⟨b, hb, rfl⟩; exact ⟨_, p.smul_mem _ hb, f.map_smul _ _⟩ } lemma map_coe (f : β →ₗ[α] γ) (p : submodule α β) : (map f p : set γ) = f '' p := rfl @[simp] lemma mem_map {f : β →ₗ[α] γ} {p : submodule α β} {x : γ} : x ∈ map f p ↔ ∃ y, y ∈ p ∧ f y = x := iff.rfl theorem mem_map_of_mem {f : β →ₗ[α] γ} {p : submodule α β} {r} (h : r ∈ p) : f r ∈ map f p := set.mem_image_of_mem _ h lemma map_id : map linear_map.id p = p := submodule.ext $ λ a, by simp lemma map_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α β) : map (g.comp f) p = map g (map f p) := submodule.ext' $ by simp [map_coe]; rw ← image_comp lemma map_mono {f : β →ₗ[α] γ} {p p' : submodule α β} : p ≤ p' → map f p ≤ map f p' := image_subset _ @[simp] lemma map_zero : map (0 : β →ₗ[α] γ) p = ⊥ := have ∃ (x : β), x ∈ p := ⟨0, p.zero_mem⟩, ext $ by simp [this, eq_comm] /-- The pullback -/ def comap (f : β →ₗ[α] γ) (p : submodule α γ) : submodule α β := { carrier := f ⁻¹' p, zero := by simp, add := λ x y h₁ h₂, by simp [p.add_mem h₁ h₂], smul := λ a x h, by simp [p.smul_mem _ h] } @[simp] lemma comap_coe (f : β →ₗ[α] γ) (p : submodule α γ) : (comap f p : set β) = f ⁻¹' p := rfl @[simp] lemma mem_comap {f : β →ₗ[α] γ} {p : submodule α γ} : x ∈ comap f p ↔ f x ∈ p := iff.rfl lemma comap_id : comap linear_map.id p = p := submodule.ext' rfl lemma comap_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) (p : submodule α δ) : comap (g.comp f) p = comap f (comap g p) := rfl lemma comap_mono {f : β →ₗ[α] γ} {q q' : submodule α γ} : q ≤ q' → comap f q ≤ comap f q' := preimage_mono lemma map_le_iff_le_comap {f : β →ₗ[α] γ} {p : submodule α β} {q : submodule α γ} : map f p ≤ q ↔ p ≤ comap f q := image_subset_iff lemma gc_map_comap (f : β →ₗ[α] γ) : galois_connection (map f) (comap f) | p q := map_le_iff_le_comap @[simp] lemma map_bot (f : β →ₗ[α] γ) : map f ⊥ = ⊥ := (gc_map_comap f).l_bot @[simp] lemma map_sup (f : β →ₗ[α] γ) : map f (p ⊔ p') = map f p ⊔ map f p' := (gc_map_comap f).l_sup @[simp] lemma map_supr {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α β) : map f (⨆i, p i) = (⨆i, map f (p i)) := (gc_map_comap f).l_supr @[simp] lemma comap_top (f : β →ₗ[α] γ) : comap f ⊤ = ⊤ := rfl @[simp] lemma comap_inf (f : β →ₗ[α] γ) : comap f (q ⊓ q') = comap f q ⊓ comap f q' := rfl @[simp] lemma comap_infi {ι : Sort*} (f : β →ₗ[α] γ) (p : ι → submodule α γ) : comap f (⨅i, p i) = (⨅i, comap f (p i)) := (gc_map_comap f).u_infi @[simp] lemma comap_zero : comap (0 : β →ₗ[α] γ) q = ⊤ := ext $ by simp lemma map_comap_le (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) ≤ q := (gc_map_comap f).l_u_le _ lemma le_comap_map (f : β →ₗ[α] γ) (p : submodule α β) : p ≤ comap f (map f p) := (gc_map_comap f).le_u_l _ --TODO(Mario): is there a way to prove this from order properties? lemma map_inf_eq_map_inf_comap {f : β →ₗ[α] γ} {p : submodule α β} {p' : submodule α γ} : map f p ⊓ p' = map f (p ⊓ comap f p') := le_antisymm (by rintro _ ⟨⟨x, h₁, rfl⟩, h₂⟩; exact ⟨_, ⟨h₁, h₂⟩, rfl⟩) (le_inf (map_mono inf_le_left) (map_le_iff_le_comap.2 inf_le_right)) lemma map_comap_subtype : map p.subtype (comap p.subtype p') = p ⊓ p' := ext $ λ x, ⟨by rintro ⟨⟨_, h₁⟩, h₂, rfl⟩; exact ⟨h₁, h₂⟩, λ ⟨h₁, h₂⟩, ⟨⟨_, h₁⟩, h₂, rfl⟩⟩ section variables (α) def span (s : set β) : submodule α β := Inf {p | s ⊆ p} end variables {s t : set β} lemma mem_span : x ∈ span α s ↔ ∀ p : submodule α β, s ⊆ p → x ∈ p := mem_bInter_iff lemma subset_span : s ⊆ span α s := λ x h, mem_span.2 $ λ p hp, hp h lemma span_le {p} : span α s ≤ p ↔ s ⊆ p := ⟨subset.trans subset_span, λ ss x h, mem_span.1 h _ ss⟩ lemma span_mono (h : s ⊆ t) : span α s ≤ span α t := span_le.2 $ subset.trans h subset_span lemma span_eq_of_le (h₁ : s ⊆ p) (h₂ : p ≤ span α s) : span α s = p := le_antisymm (span_le.2 h₁) h₂ @[simp] lemma span_eq : span α (p : set β) = p := span_eq_of_le _ (subset.refl _) subset_span @[elab_as_eliminator] lemma span_induction {p : β → Prop} (h : x ∈ span α s) (Hs : ∀ x ∈ s, p x) (H0 : p 0) (H1 : ∀ x y, p x → p y → p (x + y)) (H2 : ∀ (a:α) x, p x → p (a • x)) : p x := (@span_le _ _ _ _ _ _ ⟨p, H0, H1, H2⟩).2 Hs h section variables (α β) protected def gi : galois_insertion (@span α β _ _ _) coe := { choice := λ s _, span α s, gc := λ s t, span_le, le_l_u := λ s, subset_span, choice_eq := λ s h, rfl } end @[simp] lemma span_empty : span α (∅ : set β) = ⊥ := (submodule.gi α β).gc.l_bot lemma span_union (s t : set β) : span α (s ∪ t) = span α s ⊔ span α t := (submodule.gi α β).gc.l_sup lemma span_Union {ι} (s : ι → set β) : span α (⋃ i, s i) = ⨆ i, span α (s i) := (submodule.gi α β).gc.l_supr @[simp] theorem Union_coe_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) : ((supr S : submodule α β) : set β) = ⋃ i, S i := begin refine subset.antisymm _ (Union_subset $ le_supr S), rw [show supr S = ⨆ i, span α (S i), by simp, ← span_Union], unfreezeI, refine λ x hx, span_induction hx (λ _, id) _ _ _, { cases hι with i, exact mem_Union.2 ⟨i, by simp⟩ }, { simp, intros x y i hi j hj, rcases H i j with ⟨k, ik, jk⟩, exact ⟨k, add_mem _ (ik hi) (jk hj)⟩ }, { simp [-mem_coe]; exact λ a x i hi, ⟨i, smul_mem _ a hi⟩ }, end @[simp] theorem mem_supr_of_directed {ι} (hι : nonempty ι) (S : ι → submodule α β) (H : ∀ i j, ∃ k, S i ≤ S k ∧ S j ≤ S k) {x} : x ∈ supr S ↔ ∃ i, x ∈ S i := by rw [← mem_coe, Union_coe_of_directed hι S H, mem_Union]; refl theorem mem_Sup_of_directed {s : set (submodule α β)} {z} (hzs : z ∈ Sup s) (x ∈ s) (hdir : ∀ i ∈ s, ∀ j ∈ s, ∃ k ∈ s, i ≤ k ∧ j ≤ k) : ∃ y ∈ s, z ∈ y := begin haveI := classical.dec, rw Sup_eq_supr at hzs, have : ∃ (i : submodule α β), z ∈ ⨆ (H : i ∈ s), i, { refine (mem_supr_of_directed ⟨⊥⟩ _ (λ i j, _)).1 hzs, by_cases his : i ∈ s; by_cases hjs : j ∈ s, { rcases hdir i his j hjs with ⟨k, hks, hik, hjk⟩, exact ⟨k, le_supr_of_le hks (supr_le $ λ _, hik), le_supr_of_le hks (supr_le $ λ _, hjk)⟩ }, { exact ⟨i, le_refl _, supr_le $ hjs.elim⟩ }, { exact ⟨j, supr_le $ his.elim, le_refl _⟩ }, { exact ⟨⊥, supr_le $ his.elim, supr_le $ hjs.elim⟩ } }, cases this with N hzn, by_cases hns : N ∈ s, { have : (⨆ (H : N ∈ s), N) ≤ N := supr_le (λ _, le_refl _), exact ⟨N, hns, this hzn⟩ }, { have : (⨆ (H : N ∈ s), N) ≤ ⊥ := supr_le hns.elim, cases (mem_bot α).1 (this hzn), exact ⟨x, H, x.zero_mem⟩ } end section variables {p p'} lemma mem_sup : x ∈ p ⊔ p' ↔ ∃ (y ∈ p) (z ∈ p'), y + z = x := ⟨λ h, begin rw [← span_eq p, ← span_eq p', ← span_union] at h, apply span_induction h, { rintro y (h | h), { exact ⟨y, h, 0, by simp, by simp⟩ }, { exact ⟨0, by simp, y, h, by simp⟩ } }, { exact ⟨0, by simp, 0, by simp⟩ }, { rintro _ _ ⟨y₁, hy₁, z₁, hz₁, rfl⟩ ⟨y₂, hy₂, z₂, hz₂, rfl⟩, exact ⟨_, add_mem _ hy₁ hy₂, _, add_mem _ hz₁ hz₂, by simp⟩ }, { rintro a _ ⟨y, hy, z, hz, rfl⟩, exact ⟨_, smul_mem _ a hy, _, smul_mem _ a hz, by simp [smul_add]⟩ } end, by rintro ⟨y, hy, z, hz, rfl⟩; exact add_mem _ ((le_sup_left : p ≤ p ⊔ p') hy) ((le_sup_right : p' ≤ p ⊔ p') hz)⟩ end lemma mem_span_singleton {y : β} : x ∈ span α ({y} : set β) ↔ ∃ a:α, a • y = x := ⟨λ h, begin apply span_induction h, { rintro y (rfl|⟨⟨⟩⟩), exact ⟨1, by simp⟩ }, { exact ⟨0, by simp⟩ }, { rintro _ _ ⟨a, rfl⟩ ⟨b, rfl⟩, exact ⟨a + b, by simp [add_smul]⟩ }, { rintro a _ ⟨b, rfl⟩, exact ⟨a * b, by simp [smul_smul]⟩ } end, by rintro ⟨a, y, rfl⟩; exact smul_mem _ _ (subset_span $ by simp)⟩ lemma span_singleton_eq_range (y : β) : (span α ({y} : set β) : set β) = range ((• y) : α → β) := set.ext $ λ x, mem_span_singleton lemma mem_span_insert {y} : x ∈ span α (insert y s) ↔ ∃ (a:α) (z ∈ span α s), x = a • y + z := begin rw [← union_singleton, span_union, mem_sup], simp [mem_span_singleton], split, { rintro ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩, exact ⟨a, z, hz, rfl⟩ }, { rintro ⟨a, z, hz, rfl⟩, exact ⟨z, hz, _, ⟨a, rfl⟩, rfl⟩ } end lemma mem_span_insert' {y} : x ∈ span α (insert y s) ↔ ∃(a:α), x + a • y ∈ span α s := begin rw mem_span_insert, split, { rintro ⟨a, z, hz, rfl⟩, exact ⟨-a, by simp [hz]⟩ }, { rintro ⟨a, h⟩, exact ⟨-a, _, h, by simp⟩ } end lemma span_insert_eq_span (h : x ∈ span α s) : span α (insert x s) = span α s := span_eq_of_le _ (set.insert_subset.mpr ⟨h, subset_span⟩) (span_mono $ subset_insert _ _) lemma span_span : span α (span α s : set β) = span α s := span_eq _ lemma span_eq_bot : span α (s : set β) = ⊥ ↔ ∀ x ∈ s, (x:β) = 0 := eq_bot_iff.trans ⟨ λ H x h, (mem_bot α).1 $ H $ subset_span h, λ H, span_le.2 (λ x h, (mem_bot α).2 $ H x h)⟩ lemma span_singleton_eq_bot : span α ({x} : set β) = ⊥ ↔ x = 0 := span_eq_bot.trans $ by simp @[simp] lemma span_image (f : β →ₗ[α] γ) : span α (f '' s) = map f (span α s) := span_eq_of_le _ (image_subset _ subset_span) $ map_le_iff_le_comap.2 $ span_le.2 $ image_subset_iff.1 subset_span def prod : submodule α (β × γ) := { carrier := set.prod p q, zero := ⟨zero_mem _, zero_mem _⟩, add := by rintro ⟨x₁, y₁⟩ ⟨x₂, y₂⟩ ⟨hx₁, hy₁⟩ ⟨hx₂, hy₂⟩; exact ⟨add_mem _ hx₁ hx₂, add_mem _ hy₁ hy₂⟩, smul := by rintro a ⟨x, y⟩ ⟨hx, hy⟩; exact ⟨smul_mem _ a hx, smul_mem _ a hy⟩ } @[simp] lemma prod_coe : (prod p q : set (β × γ)) = set.prod p q := rfl @[simp] lemma mem_prod {p : submodule α β} {q : submodule α γ} {x : β × γ} : x ∈ prod p q ↔ x.1 ∈ p ∧ x.2 ∈ q := set.mem_prod lemma span_prod_le (s : set β) (t : set γ) : span α (set.prod s t) ≤ prod (span α s) (span α t) := span_le.2 $ set.prod_mono subset_span subset_span @[simp] lemma prod_top : (prod ⊤ ⊤ : submodule α (β × γ)) = ⊤ := by ext; simp @[simp] lemma prod_bot : (prod ⊥ ⊥ : submodule α (β × γ)) = ⊥ := by ext ⟨x, y⟩; simp [prod.zero_eq_mk] lemma prod_mono {p p' : submodule α β} {q q' : submodule α γ} : p ≤ p' → q ≤ q' → prod p q ≤ prod p' q' := prod_mono @[simp] lemma prod_inf_prod : prod p q ⊓ prod p' q' = prod (p ⊓ p') (q ⊓ q') := ext' set.prod_inter_prod @[simp] lemma prod_sup_prod : prod p q ⊔ prod p' q' = prod (p ⊔ p') (q ⊔ q') := begin refine le_antisymm (sup_le (prod_mono le_sup_left le_sup_left) (prod_mono le_sup_right le_sup_right)) _, simp [le_def'], intros xx yy hxx hyy, rcases mem_sup.1 hxx with ⟨x, hx, x', hx', rfl⟩, rcases mem_sup.1 hyy with ⟨y, hy, y', hy', rfl⟩, refine mem_sup.2 ⟨(x, y), ⟨hx, hy⟩, (x', y'), ⟨hx', hy'⟩, rfl⟩ end -- TODO(Mario): Factor through add_subgroup def quotient_rel : setoid β := ⟨λ x y, x - y ∈ p, λ x, by simp, λ x y h, by simpa using neg_mem _ h, λ x y z h₁ h₂, by simpa using add_mem _ h₁ h₂⟩ def quotient : Type* := quotient (quotient_rel p) namespace quotient def mk {p : submodule α β} : β → quotient p := quotient.mk' @[simp] theorem mk_eq_mk {p : submodule α β} (x : β) : (quotient.mk x : quotient p) = mk x := rfl @[simp] theorem mk'_eq_mk {p : submodule α β} (x : β) : (quotient.mk' x : quotient p) = mk x := rfl @[simp] theorem quot_mk_eq_mk {p : submodule α β} (x : β) : (quot.mk _ x : quotient p) = mk x := rfl protected theorem eq {x y : β} : (mk x : quotient p) = mk y ↔ x - y ∈ p := quotient.eq' instance : has_zero (quotient p) := ⟨mk 0⟩ @[simp] theorem mk_zero : mk 0 = (0 : quotient p) := rfl @[simp] theorem mk_eq_zero : (mk x : quotient p) = 0 ↔ x ∈ p := by simpa using (quotient.eq p : mk x = 0 ↔ _) instance : has_add (quotient p) := ⟨λ a b, quotient.lift_on₂' a b (λ a b, mk (a + b)) $ λ a₁ a₂ b₁ b₂ h₁ h₂, (quotient.eq p).2 $ by simpa using add_mem p h₁ h₂⟩ @[simp] theorem mk_add : (mk (x + y) : quotient p) = mk x + mk y := rfl instance : has_neg (quotient p) := ⟨λ a, quotient.lift_on' a (λ a, mk (-a)) $ λ a b h, (quotient.eq p).2 $ by simpa using neg_mem p h⟩ @[simp] theorem mk_neg : (mk (-x) : quotient p) = -mk x := rfl instance : add_comm_group (quotient p) := by refine {zero := 0, add := (+), neg := has_neg.neg, ..}; repeat {rintro ⟨⟩}; simp [-mk_zero, (mk_zero p).symm, -mk_add, (mk_add p).symm, -mk_neg, (mk_neg p).symm] instance : has_scalar α (quotient p) := ⟨λ a x, quotient.lift_on' x (λ x, mk (a • x)) $ λ x y h, (quotient.eq p).2 $ by simpa [smul_add] using smul_mem p a h⟩ @[simp] theorem mk_smul : (mk (r • x) : quotient p) = r • mk x := rfl instance : module α (quotient p) := module.of_core $ by refine {smul := (•), ..}; repeat {rintro ⟨⟩ <|> intro}; simp [smul_add, add_smul, smul_smul, -mk_add, (mk_add p).symm, -mk_smul, (mk_smul p).symm] instance {α β} {R:discrete_field α} [add_comm_group β] [vector_space α β] (p : submodule α β) : vector_space α (quotient p) := {} end quotient end submodule namespace submodule variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma comap_smul (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) (h : a ≠ 0) : p.comap (a • f) = p.comap f := by ext b; simp only [submodule.mem_comap, p.smul_mem_iff h, linear_map.smul_apply] lemma map_smul (f : β →ₗ[α] γ) (p : submodule α β) (a : α) (h : a ≠ 0) : p.map (a • f) = p.map f := le_antisymm begin rw [map_le_iff_le_comap, comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end begin rw [map_le_iff_le_comap, ← comap_smul f _ a h, ← map_le_iff_le_comap], exact le_refl _ end set_option class.instance_max_depth 40 lemma comap_smul' (f : β →ₗ[α] γ) (p : submodule α γ) (a : α) : p.comap (a • f) = (⨅ h : a ≠ 0, p.comap f) := by by_cases a = 0; simp [h, comap_smul] lemma map_smul' (f : β →ₗ[α] γ) (p : submodule α β) (a : α) : p.map (a • f) = (⨆ h : a ≠ 0, p.map f) := by by_cases a = 0; simp [h, map_smul] end submodule namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α open submodule @[simp] lemma finsupp_sum {α β γ δ} [ring α] [add_comm_group β] [module α β] [add_comm_group γ] [module α γ] [has_zero δ] (f : β →ₗ[α] γ) {t : ι →₀ δ} {g : ι → δ → β} : f (t.sum g) = t.sum (λi d, f (g i d)) := f.map_sum theorem map_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (h p') : submodule.map (cod_restrict p f h) p' = comap p.subtype (p'.map f) := submodule.ext $ λ ⟨x, hx⟩, by simp [subtype.coe_ext] theorem comap_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf p') : submodule.comap (cod_restrict p f hf) p' = submodule.comap f (map p.subtype p') := submodule.ext $ λ x, ⟨λ h, ⟨⟨_, hf x⟩, h, rfl⟩, by rintro ⟨⟨_, _⟩, h, ⟨⟩⟩; exact h⟩ def range (f : β →ₗ[α] γ) : submodule α γ := map f ⊤ theorem range_coe (f : β →ₗ[α] γ) : (range f : set γ) = set.range f := set.image_univ @[simp] theorem mem_range {f : β →ₗ[α] γ} : ∀ {x}, x ∈ range f ↔ ∃ y, f y = x := (set.ext_iff _ _).1 (range_coe f). @[simp] theorem range_id : range (linear_map.id : β →ₗ[α] β) = ⊤ := map_id _ theorem range_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) = map g (range f) := map_comp _ _ _ theorem range_comp_le_range (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : range (g.comp f) ≤ range g := by rw range_comp; exact map_mono le_top theorem range_eq_top {f : β →ₗ[α] γ} : range f = ⊤ ↔ surjective f := by rw [← submodule.ext'_iff, range_coe, top_coe, set.range_iff_surjective] lemma range_le_iff_comap {f : β →ₗ[α] γ} {p : submodule α γ} : range f ≤ p ↔ comap f p = ⊤ := by rw [range, map_le_iff_le_comap, eq_top_iff] def ker (f : β →ₗ[α] γ) : submodule α β := comap f ⊥ @[simp] theorem mem_ker {f : β →ₗ[α] γ} {y} : y ∈ ker f ↔ f y = 0 := mem_bot α @[simp] theorem ker_id : ker (linear_map.id : β →ₗ[α] β) = ⊥ := rfl theorem ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker (g.comp f) = comap f (ker g) := rfl theorem ker_le_ker_comp (f : β →ₗ[α] γ) (g : γ →ₗ[α] δ) : ker f ≤ ker (g.comp f) := by rw ker_comp; exact comap_mono bot_le theorem sub_mem_ker_iff {f : β →ₗ[α] γ} {x y} : x - y ∈ f.ker ↔ f x = f y := by rw [mem_ker, map_sub, sub_eq_zero] theorem disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x ∈ p, f x = 0 → x = 0 := by simp [disjoint_def] theorem disjoint_ker' {f : β →ₗ[α] γ} {p : submodule α β} : disjoint p (ker f) ↔ ∀ x y ∈ p, f x = f y → x = y := disjoint_ker.trans ⟨λ H x y hx hy h, eq_of_sub_eq_zero $ H _ (sub_mem _ hx hy) (by simp [h]), λ H x h₁ h₂, H x 0 h₁ (zero_mem _) (by simpa using h₂)⟩ theorem inj_of_disjoint_ker {f : β →ₗ[α] γ} {p : submodule α β} {s : set β} (h : s ⊆ p) (hd : disjoint p (ker f)) : ∀ x y ∈ s, f x = f y → x = y := λ x y hx hy, disjoint_ker'.1 hd _ _ (h hx) (h hy) theorem ker_eq_bot {f : β →ₗ[α] γ} : ker f = ⊥ ↔ injective f := by simpa [disjoint] using @disjoint_ker' _ _ _ _ _ _ _ _ f ⊤ lemma le_ker_iff_map {f : β →ₗ[α] γ} {p : submodule α β} : p ≤ ker f ↔ map f p = ⊥ := by rw [ker, eq_bot_iff, map_le_iff_le_comap] lemma ker_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : ker (cod_restrict p f hf) = ker f := by rw [ker, comap_cod_restrict, map_bot]; refl lemma range_cod_restrict (p : submodule α β) (f : γ →ₗ[α] β) (hf) : range (cod_restrict p f hf) = comap p.subtype f.range := map_cod_restrict _ _ _ _ lemma map_comap_eq (f : β →ₗ[α] γ) (q : submodule α γ) : map f (comap f q) = range f ⊓ q := le_antisymm (le_inf (map_mono le_top) (map_comap_le _ _)) $ by rintro _ ⟨⟨x, _, rfl⟩, hx⟩; exact ⟨x, hx, rfl⟩ lemma map_comap_eq_self {f : β →ₗ[α] γ} {q : submodule α γ} (h : q ≤ range f) : map f (comap f q) = q := by rw [map_comap_eq, inf_of_le_right h] lemma comap_map_eq (f : β →ₗ[α] γ) (p : submodule α β) : comap f (map f p) = p ⊔ ker f := begin refine le_antisymm _ (sup_le (le_comap_map _ _) (comap_mono bot_le)), rintro x ⟨y, hy, e⟩, exact mem_sup.2 ⟨y, hy, x - y, by simpa using sub_eq_zero.2 e.symm, by simp⟩ end lemma comap_map_eq_self {f : β →ₗ[α] γ} {p : submodule α β} (h : ker f ≤ p) : comap f (map f p) = p := by rw [comap_map_eq, sup_of_le_left h] @[simp] theorem ker_zero : ker (0 : β →ₗ[α] γ) = ⊤ := eq_top_iff'.2 $ λ x, by simp @[simp] theorem range_zero : range (0 : β →ₗ[α] γ) = ⊥ := submodule.map_zero _ theorem ker_eq_top {f : β →ₗ[α] γ} : ker f = ⊤ ↔ f = 0 := ⟨λ h, ext $ λ x, mem_ker.1 $ h.symm ▸ trivial, λ h, h.symm ▸ ker_zero⟩ lemma range_le_bot_iff (f : β →ₗ[α] γ) : range f ≤ ⊥ ↔ f = 0 := by rw [range_le_iff_comap]; exact ker_eq_top theorem map_le_map_iff {f : β →ₗ[α] γ} (hf : ker f = ⊥) {p p'} : map f p ≤ map f p' ↔ p ≤ p' := ⟨λ H x hx, let ⟨y, hy, e⟩ := H ⟨x, hx, rfl⟩ in ker_eq_bot.1 hf e ▸ hy, map_mono⟩ theorem map_injective {f : β →ₗ[α] γ} (hf : ker f = ⊥) : injective (map f) := λ p p' h, le_antisymm ((map_le_map_iff hf).1 (le_of_eq h)) ((map_le_map_iff hf).1 (ge_of_eq h)) theorem comap_le_comap_iff {f : β →ₗ[α] γ} (hf : range f = ⊤) {p p'} : comap f p ≤ comap f p' ↔ p ≤ p' := ⟨λ H x hx, by rcases range_eq_top.1 hf x with ⟨y, hy, rfl⟩; exact H hx, comap_mono⟩ theorem comap_injective {f : β →ₗ[α] γ} (hf : range f = ⊤) : injective (comap f) := λ p p' h, le_antisymm ((comap_le_comap_iff hf).1 (le_of_eq h)) ((comap_le_comap_iff hf).1 (ge_of_eq h)) theorem map_copair_prod (f : β →ₗ[α] δ) (g : γ →ₗ[α] δ) (p : submodule α β) (q : submodule α γ) : map (copair f g) (p.prod q) = map f p ⊔ map g q := begin refine le_antisymm _ (sup_le (map_le_iff_le_comap.2 _) (map_le_iff_le_comap.2 _)), { rw le_def', rintro _ ⟨x, ⟨h₁, h₂⟩, rfl⟩, exact mem_sup.2 ⟨_, ⟨_, h₁, rfl⟩, _, ⟨_, h₂, rfl⟩, rfl⟩ }, { exact λ x hx, ⟨(x, 0), by simp [hx]⟩ }, { exact λ x hx, ⟨(0, x), by simp [hx]⟩ } end theorem comap_pair_prod (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) (p : submodule α γ) (q : submodule α δ) : comap (pair f g) (p.prod q) = comap f p ⊓ comap g q := submodule.ext $ λ x, iff.rfl theorem prod_eq_inf_comap (p : submodule α β) (q : submodule α γ) : p.prod q = p.comap (linear_map.fst α β γ) ⊓ q.comap (linear_map.snd α β γ) := submodule.ext $ λ x, iff.rfl theorem prod_eq_sup_map (p : submodule α β) (q : submodule α γ) : p.prod q = p.map (linear_map.inl α β γ) ⊔ q.map (linear_map.inr α β γ) := by rw [← map_copair_prod, copair_inl_inr, map_id] lemma span_inl_union_inr {s : set β} {t : set γ} : span α (prod.inl '' s ∪ prod.inr '' t) = (span α s).prod (span α t) := by rw [span_union, prod_eq_sup_map, ← span_image, ← span_image]; refl lemma ker_pair (f : β →ₗ[α] γ) (g : β →ₗ[α] δ) : ker (pair f g) = ker f ⊓ ker g := by rw [ker, ← prod_bot, comap_pair_prod]; refl end linear_map namespace linear_map variables [discrete_field α] variables [add_comm_group β] [vector_space α β] variables [add_comm_group γ] [vector_space α γ] lemma ker_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : ker (a • f) = ker f := submodule.comap_smul f _ a h lemma ker_smul' (f : β →ₗ[α] γ) (a : α) : ker (a • f) = ⨅(h : a ≠ 0), ker f := submodule.comap_smul' f _ a lemma range_smul (f : β →ₗ[α] γ) (a : α) (h : a ≠ 0) : range (a • f) = range f := submodule.map_smul f _ a h lemma range_smul' (f : β →ₗ[α] γ) (a : α) : range (a • f) = ⨆(h : a ≠ 0), range f := submodule.map_smul' f _ a end linear_map namespace submodule variables {R:ring α} [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] variables (p p' : submodule α β) (q : submodule α γ) include R open linear_map @[simp] theorem map_top (f : β →ₗ[α] γ) : map f ⊤ = range f := rfl @[simp] theorem comap_bot (f : β →ₗ[α] γ) : comap f ⊥ = ker f := rfl @[simp] theorem ker_subtype : p.subtype.ker = ⊥ := ker_eq_bot.2 $ λ x y, subtype.eq' @[simp] theorem range_subtype : p.subtype.range = p := by simpa using map_comap_subtype p ⊤ lemma map_subtype_le (p' : submodule α p) : map p.subtype p' ≤ p := by simpa using (map_mono le_top : map p.subtype p' ≤ p.subtype.range) @[simp] theorem ker_of_le (p p' : submodule α β) (h : p ≤ p') : (of_le h).ker = ⊥ := by rw [of_le, ker_cod_restrict, ker_subtype] /-- If N ⊆ M then submodules of N are the same as submodules of M contained in N -/ def map_subtype.order_iso : ((≤) : submodule α p → submodule α p → Prop) ≃o ((≤) : {p' : submodule α β // p' ≤ p} → {p' : submodule α β // p' ≤ p} → Prop) := { to_fun := λ p', ⟨map p.subtype p', map_subtype_le p _⟩, inv_fun := λ q, comap p.subtype q, left_inv := λ p', comap_map_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [map_comap_subtype p, inf_of_le_right hq], ord := λ p₁ p₂, (map_le_map_iff $ ker_subtype _).symm } def map_subtype.le_order_embedding : ((≤) : submodule α p → submodule α p → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ map_subtype.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma map_subtype_embedding_eq (p' : submodule α p) : map_subtype.le_order_embedding p p' = map p.subtype p' := rfl def map_subtype.lt_order_embedding : ((<) : submodule α p → submodule α p → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (map_subtype.le_order_embedding p).lt_embedding_of_le_embedding @[simp] theorem map_inl : p.map (inl α β γ) = prod p ⊥ := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem map_inr : q.map (inr α β γ) = prod ⊥ q := by ext ⟨x, y⟩; simp [and.left_comm, eq_comm] @[simp] theorem comap_fst : p.comap (fst α β γ) = prod p ⊤ := by ext ⟨x, y⟩; simp @[simp] theorem comap_snd : q.comap (snd α β γ) = prod ⊤ q := by ext ⟨x, y⟩; simp @[simp] theorem prod_comap_inl : (prod p q).comap (inl α β γ) = p := by ext; simp @[simp] theorem prod_comap_inr : (prod p q).comap (inr α β γ) = q := by ext; simp @[simp] theorem prod_map_fst : (prod p q).map (fst α β γ) = p := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ q)] @[simp] theorem prod_map_snd : (prod p q).map (snd α β γ) = q := by ext x; simp [(⟨0, zero_mem _⟩ : ∃ x, x ∈ p)] @[simp] theorem ker_inl : (inl α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inl] @[simp] theorem ker_inr : (inr α β γ).ker = ⊥ := by rw [ker, ← prod_bot, prod_comap_inr] @[simp] theorem range_fst : (fst α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_fst] @[simp] theorem range_snd : (snd α β γ).range = ⊤ := by rw [range, ← prod_top, prod_map_snd] def mkq : β →ₗ[α] p.quotient := ⟨quotient.mk, by simp, by simp⟩ @[simp] theorem mkq_apply (x : β) : p.mkq x = quotient.mk x := rfl def liftq (f : β →ₗ[α] γ) (h : p ≤ f.ker) : p.quotient →ₗ[α] γ := ⟨λ x, _root_.quotient.lift_on' x f $ λ a b (ab : a - b ∈ p), eq_of_sub_eq_zero $ by simpa using h ab, by rintro ⟨x⟩ ⟨y⟩; exact f.map_add x y, by rintro a ⟨x⟩; exact f.map_smul a x⟩ @[simp] theorem liftq_apply (f : β →ₗ[α] γ) {h} (x : β) : p.liftq f h (quotient.mk x) = f x := rfl @[simp] theorem liftq_mkq (f : β →ₗ[α] γ) (h) : (p.liftq f h).comp p.mkq = f := by ext; refl @[simp] theorem range_mkq : p.mkq.range = ⊤ := eq_top_iff'.2 $ by rintro ⟨x⟩; exact ⟨x, trivial, rfl⟩ @[simp] theorem ker_mkq : p.mkq.ker = p := by ext; simp lemma le_comap_mkq (p' : submodule α p.quotient) : p ≤ comap p.mkq p' := by simpa using (comap_mono bot_le : p.mkq.ker ≤ comap p.mkq p') @[simp] theorem mkq_map_self : map p.mkq p = ⊥ := by rw [eq_bot_iff, map_le_iff_le_comap, comap_bot, ker_mkq]; exact le_refl _ @[simp] theorem comap_map_mkq : comap p.mkq (map p.mkq p') = p ⊔ p' := by simp [comap_map_eq, sup_comm] def mapq (f : β →ₗ[α] γ) (h : p ≤ comap f q) : p.quotient →ₗ[α] q.quotient := p.liftq (q.mkq.comp f) $ by simpa [ker_comp] using h @[simp] theorem mapq_apply (f : β →ₗ[α] γ) {h} (x : β) : mapq p q f h (quotient.mk x) = quotient.mk (f x) := rfl theorem mapq_mkq (f : β →ₗ[α] γ) {h} : (mapq p q f h).comp p.mkq = q.mkq.comp f := by ext x; refl theorem comap_liftq (f : β →ₗ[α] γ) (h) : q.comap (p.liftq f h) = (q.comap f).map (mkq p) := le_antisymm (by rintro ⟨x⟩ hx; exact ⟨_, hx, rfl⟩) (by rw [map_le_iff_le_comap, ← comap_comp, liftq_mkq]; exact le_refl _) theorem map_liftq (f : β →ₗ[α] γ) (h) (q : submodule α (quotient p)) : q.map (p.liftq f h) = (q.comap p.mkq).map f := le_antisymm (by rintro _ ⟨⟨x⟩, hxq, rfl⟩; exact ⟨x, hxq, rfl⟩) (by rintro _ ⟨x, hxq, rfl⟩; exact ⟨quotient.mk x, hxq, rfl⟩) theorem ker_liftq (f : β →ₗ[α] γ) (h) : ker (p.liftq f h) = (ker f).map (mkq p) := comap_liftq _ _ _ _ theorem range_liftq (f : β →ₗ[α] γ) (h) : range (p.liftq f h) = range f := map_liftq _ _ _ _ theorem ker_liftq_eq_bot (f : β →ₗ[α] γ) (h) (h' : ker f ≤ p) : ker (p.liftq f h) = ⊥ := by rw [ker_liftq, le_antisymm h h', mkq_map_self] /-- Correspondence Theorem -/ def comap_mkq.order_iso : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≃o ((≤) : {p' : submodule α β // p ≤ p'} → {p' : submodule α β // p ≤ p'} → Prop) := { to_fun := λ p', ⟨comap p.mkq p', le_comap_mkq p _⟩, inv_fun := λ q, map p.mkq q, left_inv := λ p', map_comap_eq_self $ by simp, right_inv := λ ⟨q, hq⟩, subtype.eq' $ by simp [comap_map_mkq p, sup_of_le_right hq], ord := λ p₁ p₂, (comap_le_comap_iff $ range_mkq _).symm } def comap_mkq.le_order_embedding : ((≤) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((≤) : submodule α β → submodule α β → Prop) := (order_iso.to_order_embedding $ comap_mkq.order_iso p).trans (subtype.order_embedding _ _) @[simp] lemma comap_mkq_embedding_eq (p' : submodule α p.quotient) : comap_mkq.le_order_embedding p p' = comap p.mkq p' := rfl def comap_mkq.lt_order_embedding : ((<) : submodule α p.quotient → submodule α p.quotient → Prop) ≼o ((<) : submodule α β → submodule α β → Prop) := (comap_mkq.le_order_embedding p).lt_embedding_of_le_embedding end submodule section set_option old_structure_cmd true structure linear_equiv (α : Type u) (β : Type v) (γ : Type w) [ring α] [add_comm_group β] [add_comm_group γ] [module α β] [module α γ] extends β →ₗ[α] γ, β ≃ γ end infix ` ≃ₗ ` := linear_equiv _ notation β ` ≃ₗ[`:50 α `] ` γ := linear_equiv α β γ namespace linear_equiv section ring variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α section variable (β) def refl : β ≃ₗ[α] β := { .. linear_map.id, .. equiv.refl β } end def symm (e : β ≃ₗ[α] γ) : γ ≃ₗ[α] β := { .. e.to_linear_map.inverse e.inv_fun e.left_inv e.right_inv, .. e.to_equiv.symm } def trans (e₁ : β ≃ₗ[α] γ) (e₂ : γ ≃ₗ[α] δ) : β ≃ₗ[α] δ := { .. e₂.to_linear_map.comp e₁.to_linear_map, .. e₁.to_equiv.trans e₂.to_equiv } instance : has_coe (β ≃ₗ[α] γ) (β →ₗ[α] γ) := ⟨to_linear_map⟩ @[simp] theorem apply_symm_apply (e : β ≃ₗ[α] γ) (c : γ) : e (e.symm c) = c := e.6 c @[simp] theorem symm_apply_apply (e : β ≃ₗ[α] γ) (b : β) : e.symm (e b) = b := e.5 b @[simp] theorem coe_apply (e : β ≃ₗ[α] γ) (b : β) : (e : β →ₗ[α] γ) b = e b := rfl noncomputable def of_bijective (f : β →ₗ[α] γ) (hf₁ : f.ker = ⊥) (hf₂ : f.range = ⊤) : β ≃ₗ[α] γ := { ..f, ..@equiv.of_bijective _ _ f ⟨linear_map.ker_eq_bot.1 hf₁, linear_map.range_eq_top.1 hf₂⟩ } @[simp] theorem of_bijective_apply (f : β →ₗ[α] γ) {hf₁ hf₂} (x : β) : of_bijective f hf₁ hf₂ x = f x := rfl def of_linear (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) (h₁ : f.comp g = linear_map.id) (h₂ : g.comp f = linear_map.id) : β ≃ₗ[α] γ := { inv_fun := g, left_inv := linear_map.ext_iff.1 h₂, right_inv := linear_map.ext_iff.1 h₁, ..f } @[simp] theorem of_linear_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : β) : of_linear f g h₁ h₂ x = f x := rfl @[simp] theorem of_linear_symm_apply (f : β →ₗ[α] γ) (g : γ →ₗ[α] β) {h₁ h₂} (x : γ) : (of_linear f g h₁ h₂).symm x = g x := rfl @[simp] protected theorem ker (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).ker = ⊥ := linear_map.ker_eq_bot.2 f.to_equiv.bijective.1 @[simp] protected theorem range (f : β ≃ₗ[α] γ) : (f : β →ₗ[α] γ).range = ⊤ := linear_map.range_eq_top.2 f.to_equiv.bijective.2 def of_top (p : submodule α β) (h : p = ⊤) : p ≃ₗ[α] β := { inv_fun := λ x, ⟨x, h.symm ▸ trivial⟩, left_inv := λ ⟨x, h⟩, rfl, right_inv := λ x, rfl, .. p.subtype } @[simp] theorem of_top_apply (p : submodule α β) {h} (x : p) : of_top p h x = x := rfl @[simp] theorem of_top_symm_apply (p : submodule α β) {h} (x : β) : ↑((of_top p h).symm x) = x := rfl end ring section comm_ring variables [comm_ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] include α def congr_right (f : γ ≃ₗ[α] δ) : (β →ₗ[α] γ) ≃ₗ (β →ₗ δ) := of_linear f.to_linear_map.congr_right f.symm.to_linear_map.congr_right (linear_map.ext $ λ _, linear_map.ext $ λ _, f.6 _) (linear_map.ext $ λ _, linear_map.ext $ λ _, f.5 _) end comm_ring end linear_equiv namespace linear_map variables [ring α] [add_comm_group β] [add_comm_group γ] [add_comm_group δ] variables [module α β] [module α γ] [module α δ] variables (f : β →ₗ[α] γ) /-- First Isomorphism Law -/ noncomputable def quot_ker_equiv_range : f.ker.quotient ≃ₗ[α] f.range := have hr : ∀ x : f.range, ∃ y, f y = ↑x := λ x, x.2.imp $ λ _, and.right, let F : f.ker.quotient →ₗ[α] f.range := f.ker.liftq (cod_restrict f.range f $ λ x, ⟨x, trivial, rfl⟩) (λ x hx, by simp; apply subtype.coe_ext.2; simpa using hx) in { inv_fun := λx, submodule.quotient.mk (classical.some (hr x)), left_inv := by rintro ⟨x⟩; exact (submodule.quotient.eq _).2 (sub_mem_ker_iff.2 $ classical.some_spec $ hr $ F $ submodule.quotient.mk x), right_inv := λ x : range f, subtype.eq $ classical.some_spec (hr x), .. F } open submodule def sup_quotient_to_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient →ₗ[α] (comap (p ⊔ p').subtype p').quotient := (comap p.subtype (p ⊓ p')).liftq ((comap (p ⊔ p').subtype p').mkq.comp (of_le le_sup_left)) begin rw [ker_comp, of_le, comap_cod_restrict, ker_mkq, map_comap_subtype], exact comap_mono (inf_le_inf le_sup_left (le_refl _)) end set_option class.instance_max_depth 41 /-- Second Isomorphism Law -/ noncomputable def sup_quotient_equiv_quotient_inf (p p' : submodule α β) : (comap p.subtype (p ⊓ p')).quotient ≃ₗ[α] (comap (p ⊔ p').subtype p').quotient := { .. sup_quotient_to_quotient_inf p p', .. show (comap p.subtype (p ⊓ p')).quotient ≃ (comap (p ⊔ p').subtype p').quotient, from @equiv.of_bijective _ _ (sup_quotient_to_quotient_inf p p') begin constructor, { rw [← ker_eq_bot, sup_quotient_to_quotient_inf, ker_liftq_eq_bot], rw [ker_comp, ker_mkq], rintros ⟨x, hx1⟩ hx2, exact ⟨hx1, hx2⟩ }, rw [← range_eq_top, sup_quotient_to_quotient_inf, range_liftq, eq_top_iff'], rintros ⟨x, hx⟩, rcases mem_sup.1 hx with ⟨y, hy, z, hz, rfl⟩, use [⟨y, hy⟩, trivial], apply (submodule.quotient.eq _).2, change y - (y + z) ∈ p', rwa [sub_add_eq_sub_sub, sub_self, zero_sub, neg_mem_iff] end } end linear_map
af82fde2a0bd06b59b8a0f4f428b449b2093ba56
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/run/nat_bug.lean
843d38984bcdb221a39f3b626719048039c761c1
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
814
lean
open decidable open eq namespace experiment inductive nat : Type | zero : nat | succ : nat → nat definition refl := @eq.refl namespace nat definition pred (n : nat) : nat := nat.rec zero (fun m x, m) n theorem pred_zero : pred zero = zero := refl _ theorem pred_succ (n : nat) : pred (succ n) = n := refl _ theorem zero_or_succ (n : nat) : n = zero ∨ n = succ (pred n) := nat.induction_on n (or.intro_left _ (refl zero)) (take m IH, or.intro_right _ (show succ m = succ (pred (succ m)), from congr_arg succ (symm (pred_succ m)))) theorem zero_or_succ2 (n : nat) : n = zero ∨ n = succ (pred n) := nat.induction_on n (or.intro_left _ (refl zero)) (take m IH, or.intro_right _ (show succ m = succ (pred (succ m)), from congr_arg succ (symm (pred_succ m)))) end nat end experiment
11c1bd1821c2049327cab7eee9b026427629dfb3
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/algebra/big_operators/nat_antidiagonal_auto.lean
abc6d625bf4c917acb8d3f1dc660ade305b7c561
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
2,590
lean
/- Copyright (c) 2020 Aaron Anderson. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Aaron Anderson -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.finset.nat_antidiagonal import Mathlib.algebra.big_operators.basic import Mathlib.PostPort universes u_1 u_2 namespace Mathlib /-! # Big operators for `nat_antidiagonal` This file contains theorems relevant to big operators over `finset.nat.antidiagonal`. -/ namespace finset namespace nat theorem prod_antidiagonal_succ {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.prod (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (0, n + 1) * finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p + 1, prod.snd p) := sorry theorem sum_antidiagonal_succ {N : Type u_2} [add_comm_monoid N] {n : ℕ} {f : ℕ × ℕ → N} : (finset.sum (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (0, n + 1) + finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p + 1, prod.snd p) := prod_antidiagonal_succ theorem sum_antidiagonal_swap {M : Type u_1} [add_comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.swap p)) = finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f p := sorry theorem prod_antidiagonal_succ' {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → M} : (finset.prod (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (n + 1, 0) * finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p, prod.snd p + 1) := sorry theorem sum_antidiagonal_succ' {N : Type u_2} [add_comm_monoid N] {n : ℕ} {f : ℕ × ℕ → N} : (finset.sum (antidiagonal (n + 1)) fun (p : ℕ × ℕ) => f p) = f (n + 1, 0) + finset.sum (antidiagonal n) fun (p : ℕ × ℕ) => f (prod.fst p, prod.snd p + 1) := prod_antidiagonal_succ' theorem prod_antidiagonal_subst {M : Type u_1} [comm_monoid M] {n : ℕ} {f : ℕ × ℕ → ℕ → M} : (finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f p n) = finset.prod (antidiagonal n) fun (p : ℕ × ℕ) => f p (prod.fst p + prod.snd p) := sorry theorem prod_antidiagonal_eq_prod_range_succ {M : Type u_1} [comm_monoid M] (f : ℕ → ℕ → M) (n : ℕ) : (finset.prod (antidiagonal n) fun (ij : ℕ × ℕ) => f (prod.fst ij) (prod.snd ij)) = finset.prod (range (Nat.succ n)) fun (k : ℕ) => f k (n - k) := sorry end Mathlib
a75d6899910542a117cd0b5084520c241d102bdc
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/src/algebra/add_torsor.lean
e4e8b53ba617f7a459dc26d8a4e8214f52012b83
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
13,750
lean
/- Copyright (c) 2020 Joseph Myers. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Joseph Myers, Yury Kudryashov. -/ import algebra.group.prod import algebra.group.type_tags import algebra.group.pi import data.equiv.basic /-! # Torsors of additive group actions This file defines torsors of additive group actions. ## Notations The group elements are referred to as acting on points. This file defines the notation `+ᵥ` for adding a group element to a point and `-ᵥ` for subtracting two points to produce a group element. ## Implementation notes Affine spaces are the motivating example of torsors of additive group actions. It may be appropriate to refactor in terms of the general definition of group actions, via `to_additive`, when there is a use for multiplicative torsors (currently mathlib only develops the theory of group actions for multiplicative group actions). The variable `G` is an explicit rather than implicit argument to lemmas because otherwise the elaborator sometimes has problems inferring appropriate types and type class instances. ## References * https://en.wikipedia.org/wiki/Principal_homogeneous_space * https://en.wikipedia.org/wiki/Affine_space -/ /-- Type class for the `+ᵥ` notation. -/ class has_vadd (G : Type*) (P : Type*) := (vadd : G → P → P) /-- Type class for the `-ᵥ` notation. -/ class has_vsub (G : Type*) (P : Type*) := (vsub : P → P → G) infix ` +ᵥ `:65 := has_vadd.vadd infix ` -ᵥ `:65 := has_vsub.vsub section prio set_option default_priority 100 -- see Note [default priority] set_option old_structure_cmd true /-- Type class for additive monoid actions. -/ class add_action (G : Type*) (P : Type*) [add_monoid G] extends has_vadd G P := (zero_vadd' : ∀ p : P, (0 : G) +ᵥ p = p) (vadd_assoc' : ∀ (g1 g2 : G) (p : P), g1 +ᵥ (g2 +ᵥ p) = (g1 + g2) +ᵥ p) /-- An `add_torsor G P` gives a structure to the nonempty type `P`, acted on by an `add_group G` with a transitive and free action given by the `+ᵥ` operation and a corresponding subtraction given by the `-ᵥ` operation. In the case of a vector space, it is an affine space. -/ class add_torsor (G : Type*) (P : Type*) [add_group G] extends add_action G P, has_vsub G P := [nonempty : nonempty P] (vsub_vadd' : ∀ (p1 p2 : P), (p1 -ᵥ p2 : G) +ᵥ p2 = p1) (vadd_vsub' : ∀ (g : G) (p : P), g +ᵥ p -ᵥ p = g) end prio /-- An `add_group G` is a torsor for itself. -/ instance add_group_is_add_torsor (G : Type*) [add_group G] : add_torsor G G := { vadd := has_add.add, vsub := has_sub.sub, zero_vadd' := zero_add, vadd_assoc' := λ a b c, (add_assoc a b c).symm, vsub_vadd' := sub_add_cancel, vadd_vsub' := add_sub_cancel } /-- Simplify addition for a torsor for an `add_group G` over itself. -/ @[simp] lemma vadd_eq_add (G : Type*) [add_group G] (g1 g2 : G) : g1 +ᵥ g2 = g1 + g2 := rfl /-- Simplify subtraction for a torsor for an `add_group G` over itself. -/ @[simp] lemma vsub_eq_sub (G : Type*) [add_group G] (g1 g2 : G) : g1 -ᵥ g2 = g1 - g2 := rfl namespace add_action section general variables (G : Type*) {P : Type*} [add_monoid G] [A : add_action G P] include A /-- Adding the zero group element to a point gives the same point. -/ @[simp] lemma zero_vadd (p : P) : (0 : G) +ᵥ p = p := zero_vadd' p /-- Adding two group elements to a point produces the same result as adding their sum. -/ lemma vadd_assoc (g1 g2 : G) (p : P) : g1 +ᵥ (g2 +ᵥ p) = (g1 + g2) +ᵥ p := vadd_assoc' g1 g2 p end general section comm variables (G : Type*) {P : Type*} [add_comm_monoid G] [A : add_action G P] include A /-- Adding two group elements to a point produces the same result in either order. -/ lemma vadd_comm (p : P) (g1 g2 : G) : g1 +ᵥ (g2 +ᵥ p) = g2 +ᵥ (g1 +ᵥ p) := by rw [vadd_assoc, vadd_assoc, add_comm] end comm section group variables {G : Type*} {P : Type*} [add_group G] [A : add_action G P] include A /-- If the same group element added to two points produces equal results, those points are equal. -/ lemma vadd_left_cancel {p1 p2 : P} (g : G) (h : g +ᵥ p1 = g +ᵥ p2) : p1 = p2 := begin have h2 : -g +ᵥ (g +ᵥ p1) = -g +ᵥ (g +ᵥ p2), { rw h }, rwa [vadd_assoc, vadd_assoc, add_left_neg, zero_vadd, zero_vadd] at h2 end @[simp] lemma vadd_left_cancel_iff {p₁ p₂ : P} (g : G) : g +ᵥ p₁ = g +ᵥ p₂ ↔ p₁ = p₂ := ⟨vadd_left_cancel g, λ h, h ▸ rfl⟩ end group end add_action namespace add_torsor open add_action section general variables (G : Type*) {P : Type*} [add_group G] [T : add_torsor G P] include T /-- Adding the result of subtracting from another point produces that point. -/ @[simp] lemma vsub_vadd (p1 p2 : P) : (p1 -ᵥ p2 : G) +ᵥ p2 = p1 := vsub_vadd' p1 p2 /-- Adding a group element then subtracting the original point produces that group element. -/ @[simp] lemma vadd_vsub (g : G) (p : P) : g +ᵥ p -ᵥ p = g := vadd_vsub' g p variable {G} /-- If the same point added to two group elements produces equal results, those group elements are equal. -/ lemma vadd_right_cancel {g1 g2 : G} (p : P) (h : g1 +ᵥ p = g2 +ᵥ p) : g1 = g2 := by rw [←vadd_vsub G g1, h, vadd_vsub] @[simp] lemma vadd_right_cancel_iff {g1 g2 : G} (p : P) : g1 +ᵥ p = g2 +ᵥ p ↔ g1 = g2 := ⟨vadd_right_cancel p, λ h, h ▸ rfl⟩ /-- Adding a group element to a point, then subtracting another point, produces the same result as subtracting the points then adding the group element. -/ lemma vadd_vsub_assoc (g : G) (p1 p2 : P) : g +ᵥ p1 -ᵥ p2 = g + (p1 -ᵥ p2) := begin apply vadd_right_cancel p2, rw [vsub_vadd, ←vadd_assoc, vsub_vadd] end variable (G) /-- Subtracting a point from itself produces 0. -/ @[simp] lemma vsub_self (p : P) : p -ᵥ p = (0 : G) := by rw [←zero_add (p -ᵥ p : G), ←vadd_vsub_assoc, vadd_vsub] /-- If subtracting two points produces 0, they are equal. -/ lemma eq_of_vsub_eq_zero {p1 p2 : P} (h : p1 -ᵥ p2 = (0 : G)) : p1 = p2 := by rw [←vsub_vadd G p1 p2, h, zero_vadd] /-- Subtracting two points produces 0 if and only if they are equal. -/ @[simp] lemma vsub_eq_zero_iff_eq {p1 p2 : P} : p1 -ᵥ p2 = (0 : G) ↔ p1 = p2 := iff.intro (eq_of_vsub_eq_zero G) (λ h, h ▸ vsub_self G _) /-- Cancellation adding the results of two subtractions. -/ @[simp] lemma vsub_add_vsub_cancel (p1 p2 p3 : P) : (p1 -ᵥ p2 : G) + (p2 -ᵥ p3) = (p1 -ᵥ p3) := begin apply vadd_right_cancel p3, rw [←vadd_assoc, vsub_vadd, vsub_vadd, vsub_vadd] end /-- Subtracting two points in the reverse order produces the negation of subtracting them. -/ @[simp] lemma neg_vsub_eq_vsub_rev (p1 p2 : P) : -(p1 -ᵥ p2) = (p2 -ᵥ p1 : G) := begin refine neg_eq_of_add_eq_zero (vadd_right_cancel p1 _), rw [vsub_add_vsub_cancel, vsub_self], end /-- Subtracting the result of adding a group element produces the same result as subtracting the points and subtracting that group element. -/ lemma vsub_vadd_eq_vsub_sub (p1 p2 : P) (g : G) : p1 -ᵥ (g +ᵥ p2) = (p1 -ᵥ p2) - g := by rw [←add_right_inj (p2 -ᵥ p1 : G), vsub_add_vsub_cancel, ←neg_vsub_eq_vsub_rev, vadd_vsub, ←add_sub_assoc, ←neg_vsub_eq_vsub_rev, neg_add_self, zero_sub] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] lemma vsub_sub_vsub_cancel_right (p1 p2 p3 : P) : (p1 -ᵥ p3 : G) - (p2 -ᵥ p3) = (p1 -ᵥ p2) := by rw [←vsub_vadd_eq_vsub_sub, vsub_vadd] /-- The pairwise differences of a set of points. -/ def vsub_set (s : set P) : set G := {g | ∃ x ∈ s, ∃ y ∈ s, g = x -ᵥ y} /-- `vsub_set` of an empty set. -/ @[simp] lemma vsub_set_empty : vsub_set G (∅ : set P) = ∅ := begin rw set.eq_empty_iff_forall_not_mem, rintros g ⟨p, hp, hg⟩, exact hp end /-- Each pairwise difference is in the `vsub_set`. -/ lemma vsub_mem_vsub_set {p1 p2 : P} {s : set P} (hp1 : p1 ∈ s) (hp2 : p2 ∈ s) : (p1 -ᵥ p2) ∈ vsub_set G s := ⟨p1, hp1, p2, hp2, rfl⟩ /-- `vsub_set` is contained in `vsub_set` of a larger set. -/ lemma vsub_set_mono {s1 s2 : set P} (h : s1 ⊆ s2) : vsub_set G s1 ⊆ vsub_set G s2 := begin rintros v ⟨p1, hp1, p2, hp2, hv⟩, exact ⟨p1, set.mem_of_mem_of_subset hp1 h, p2, set.mem_of_mem_of_subset hp2 h, hv⟩ end @[simp] lemma vadd_vsub_vadd_cancel_right (v₁ v₂ : G) (p : P) : ((v₁ +ᵥ p) -ᵥ (v₂ +ᵥ p) : G) = v₁ - v₂ := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, vsub_self, add_zero] /-- If the same point subtracted from two points produces equal results, those points are equal. -/ lemma vsub_left_cancel {p1 p2 p : P} (h : (p1 -ᵥ p : G) = p2 -ᵥ p) : p1 = p2 := by rwa [←sub_eq_zero, vsub_sub_vsub_cancel_right, vsub_eq_zero_iff_eq] at h /-- The same point subtracted from two points produces equal results if and only if those points are equal. -/ @[simp] lemma vsub_left_cancel_iff {p1 p2 p : P} : (p1 -ᵥ p : G) = p2 -ᵥ p ↔ p1 = p2 := ⟨vsub_left_cancel _, λ h, h ▸ rfl⟩ /-- If subtracting two points from the same point produces equal results, those points are equal. -/ lemma vsub_right_cancel {p1 p2 p : P} (h : (p -ᵥ p1 : G) = p -ᵥ p2) : p1 = p2 := begin have h2 : (p -ᵥ p2 : G) +ᵥ p1 = (p -ᵥ p1 : G) +ᵥ p1, { rw h }, conv_rhs at h2 { rw [vsub_vadd, ←vsub_vadd G p p2], }, rwa vadd_left_cancel_iff at h2 end /-- Subtracting two points from the same point produces equal results if and only if those points are equal. -/ @[simp] lemma vsub_right_cancel_iff {p1 p2 p : P} : (p -ᵥ p1 : G) = p -ᵥ p2 ↔ p1 = p2 := ⟨vsub_right_cancel _, λ h, h ▸ rfl⟩ end general section comm variables (G : Type*) {P : Type*} [add_comm_group G] [add_torsor G P] /-- Cancellation subtracting the results of two subtractions. -/ @[simp] lemma vsub_sub_vsub_cancel_left (p1 p2 p3 : P) : (p3 -ᵥ p2 : G) - (p3 -ᵥ p1) = (p1 -ᵥ p2) := by rw [sub_eq_add_neg, neg_vsub_eq_vsub_rev, add_comm, vsub_add_vsub_cancel] @[simp] lemma vadd_vsub_vadd_cancel_left (v : G) (p1 p2 : P) : ((v +ᵥ p1) -ᵥ (v +ᵥ p2) : G) = p1 -ᵥ p2 := by rw [vsub_vadd_eq_vsub_sub, vadd_vsub_assoc, add_sub_cancel'] end comm end add_torsor namespace prod variables {G : Type*} {P : Type*} {G' : Type*} {P' : Type*} [add_group G] [add_group G'] [add_torsor G P] [add_torsor G' P'] instance : add_torsor (G × G') (P × P') := { vadd := λ v p, (v.1 +ᵥ p.1, v.2 +ᵥ p.2), zero_vadd' := λ p, by simp, vadd_assoc' := by simp [add_action.vadd_assoc], vsub := λ p₁ p₂, (p₁.1 -ᵥ p₂.1, p₁.2 -ᵥ p₂.2), nonempty := @prod.nonempty _ _ (add_torsor.nonempty G) (add_torsor.nonempty G'), vsub_vadd' := λ p₁ p₂, show (p₁.1 -ᵥ p₂.1 +ᵥ p₂.1, _) = p₁, by simp, vadd_vsub' := λ v p, show (v.1 +ᵥ p.1 -ᵥ p.1, v.2 +ᵥ p.2 -ᵥ p.2) =v, by simp } @[simp] lemma fst_vadd (v : G × G') (p : P × P') : (v +ᵥ p).1 = v.1 +ᵥ p.1 := rfl @[simp] lemma snd_vadd (v : G × G') (p : P × P') : (v +ᵥ p).2 = v.2 +ᵥ p.2 := rfl @[simp] lemma mk_vadd_mk (v : G) (v' : G') (p : P) (p' : P') : (v, v') +ᵥ (p, p') = (v +ᵥ p, v' +ᵥ p') := rfl @[simp] lemma fst_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').1 = p₁.1 -ᵥ p₂.1 := rfl @[simp] lemma snd_vsub (p₁ p₂ : P × P') : (p₁ -ᵥ p₂ : G × G').2 = p₁.2 -ᵥ p₂.2 := rfl @[simp] lemma mk_vsub_mk (p₁ p₂ : P) (p₁' p₂' : P') : ((p₁, p₁') -ᵥ (p₂, p₂') : G × G') = (p₁ -ᵥ p₂, p₁' -ᵥ p₂') := rfl end prod namespace pi universes u v w variables {I : Type u} {fg : I → Type v} [∀ i, add_group (fg i)] {fp : I → Type w} open add_action add_torsor /-- A product of `add_torsor`s is an `add_torsor`. -/ instance [T : ∀ i, add_torsor (fg i) (fp i)] : add_torsor (Π i, fg i) (Π i, fp i) := { vadd := λ g p, λ i, g i +ᵥ p i, zero_vadd' := λ p, funext $ λ i, zero_vadd (fg i) (p i), vadd_assoc' := λ g₁ g₂ p, funext $ λ i, vadd_assoc (fg i) (g₁ i) (g₂ i) (p i), vsub := λ p₁ p₂, λ i, p₁ i -ᵥ p₂ i, nonempty := ⟨λ i, classical.choice (T i).nonempty⟩, vsub_vadd' := λ p₁ p₂, funext $ λ i, vsub_vadd (fg i) (p₁ i) (p₂ i), vadd_vsub' := λ g p, funext $ λ i, vadd_vsub (fg i) (g i) (p i), } /-- Addition in a product of `add_torsor`s. -/ @[simp] lemma vadd_apply [T : ∀ i, add_torsor (fg i) (fp i)] (x : Π i, fg i) (y : Π i, fp i) {i : I} : (x +ᵥ y) i = x i +ᵥ y i := rfl end pi namespace equiv variables (G : Type*) {P : Type*} [add_group G] [add_torsor G P] open add_action add_torsor /-- `v ↦ v +ᵥ p` as an equivalence. -/ def vadd_const (p : P) : G ≃ P := { to_fun := λ v, v +ᵥ p, inv_fun := λ p', p' -ᵥ p, left_inv := λ v, vadd_vsub _ _ _, right_inv := λ p', vsub_vadd _ _ _ } @[simp] lemma coe_vadd_const (p : P) : ⇑(vadd_const G p) = λ v, v+ᵥ p := rfl @[simp] lemma coe_vadd_const_symm (p : P) : ⇑(vadd_const G p).symm = λ p', p' -ᵥ p := rfl variables {G} (P) /-- The permutation given by `p ↦ v +ᵥ p`. -/ def const_vadd (v : G) : equiv.perm P := { to_fun := (+ᵥ) v, inv_fun := (+ᵥ) (-v), left_inv := λ p, by simp [vadd_assoc], right_inv := λ p, by simp [vadd_assoc] } @[simp] lemma coe_const_vadd (v : G) : ⇑(const_vadd P v) = (+ᵥ) v := rfl variable (G) @[simp] lemma const_vadd_zero : const_vadd P (0:G) = 1 := ext $ zero_vadd G variable {G} @[simp] lemma const_vadd_add (v₁ v₂ : G) : const_vadd P (v₁ + v₂) = const_vadd P v₁ * const_vadd P v₂ := ext $ λ p, (vadd_assoc G v₁ v₂ p).symm /-- `equiv.const_vadd` as a homomorphism from `multiplicative G` to `equiv.perm P` -/ def const_vadd_hom : multiplicative G →* equiv.perm P := { to_fun := λ v, const_vadd P v.to_add, map_one' := const_vadd_zero G P, map_mul' := const_vadd_add P } end equiv
a8aa2bf647839caa4bc4fd86441d5a5f3a109269
9be442d9ec2fcf442516ed6e9e1660aa9071b7bd
/src/Lean/Meta/KExprMap.lean
7c37abd29b4ebfb2209aa881e954335639c204c2
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
EdAyers/lean4
57ac632d6b0789cb91fab2170e8c9e40441221bd
37ba0df5841bde51dbc2329da81ac23d4f6a4de4
refs/heads/master
1,676,463,245,298
1,660,619,433,000
1,660,619,433,000
183,433,437
1
0
Apache-2.0
1,657,612,672,000
1,556,196,574,000
Lean
UTF-8
Lean
false
false
1,605
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Leonardo de Moura -/ import Lean.HeadIndex import Lean.Meta.Basic namespace Lean.Meta /-- A mapping that indentifies definitionally equal expressions. We implement it as a mapping from `HeadIndex` to `Std.AssocList Expr α`. Remark: this map may be quite inefficient if there are many `HeadIndex` collisions. -/ structure KExprMap (α : Type) where map : Std.PHashMap HeadIndex (Std.AssocList Expr α) := {} deriving Inhabited /-- Return `some v` if there is an entry `e ↦ v` in `m`. -/ def KExprMap.find? (m : KExprMap α) (e : Expr) : MetaM (Option α) := do match m.map.find? e.toHeadIndex with | none => return none | some ps => for ⟨e', a⟩ in ps do if (← isDefEq e e') then return some a return none private def updateList (ps : Std.AssocList Expr α) (e : Expr) (v : α) : MetaM (Std.AssocList Expr α) := do match ps with | Std.AssocList.nil => return Std.AssocList.cons e v ps | Std.AssocList.cons e' v' ps => if (← isDefEq e e') then return Std.AssocList.cons e v ps else return Std.AssocList.cons e' v' (← updateList ps e v) /-- Insert `e ↦ v` into `m` -/ def KExprMap.insert (m : KExprMap α) (e : Expr) (v : α) : MetaM (KExprMap α) := let k := e.toHeadIndex match m.map.find? k with | none => return { map := m.map.insert k (Std.AssocList.cons e v Std.AssocList.nil) } | some ps => return { map := m.map.insert k (← updateList ps e v) } end Lean.Meta
2dc9db1971b59a5f2131f9d0075ba0f6b46cc348
94e33a31faa76775069b071adea97e86e218a8ee
/src/analysis/normed_space/operator_norm.lean
1744a1198d001aeb9b30dbf4b18e49f18319be53
[ "Apache-2.0" ]
permissive
urkud/mathlib
eab80095e1b9f1513bfb7f25b4fa82fa4fd02989
6379d39e6b5b279df9715f8011369a301b634e41
refs/heads/master
1,658,425,342,662
1,658,078,703,000
1,658,078,703,000
186,910,338
0
0
Apache-2.0
1,568,512,083,000
1,557,958,709,000
Lean
UTF-8
Lean
false
false
83,459
lean
/- Copyright (c) 2019 Jan-David Salchow. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Jan-David Salchow, Sébastien Gouëzel, Jean Lo -/ import algebra.algebra.tower import analysis.asymptotics.asymptotics import analysis.normed_space.linear_isometry import analysis.normed_space.riesz_lemma /-! # Operator norm on the space of continuous linear maps Define the operator norm on the space of continuous (semi)linear maps between normed spaces, and prove its basic properties. In particular, show that this space is itself a normed space. Since a lot of elementary properties don't require `∥x∥ = 0 → x = 0` we start setting up the theory for `semi_normed_group` and we specialize to `normed_group` at the end. Note that most of statements that apply to semilinear maps only hold when the ring homomorphism is isometric, as expressed by the typeclass `[ring_hom_isometric σ]`. -/ noncomputable theory open_locale classical nnreal topological_space -- the `ₗ` subscript variables are for special cases about linear (as opposed to semilinear) maps variables {𝕜 𝕜₂ 𝕜₃ E Eₗ F Fₗ G Gₗ 𝓕 : Type*} section semi_normed variables [semi_normed_group E] [semi_normed_group Eₗ] [semi_normed_group F] [semi_normed_group Fₗ] variables [semi_normed_group G] [semi_normed_group Gₗ] open metric continuous_linear_map section normed_field /-! Most statements in this file require the field to be non-discrete, as this is necessary to deduce an inequality `∥f x∥ ≤ C ∥x∥` from the continuity of f. However, the other direction always holds. In this section, we just assume that `𝕜` is a normed field. In the remainder of the file, it will be non-discrete. -/ variables [normed_field 𝕜] [normed_field 𝕜₂] [normed_space 𝕜 E] [normed_space 𝕜₂ F] variables [normed_space 𝕜 G] {σ : 𝕜 →+* 𝕜₂} (f : E →ₛₗ[σ] F) /-- Construct a continuous linear map from a linear map and a bound on this linear map. The fact that the norm of the continuous linear map is then controlled is given in `linear_map.mk_continuous_norm_le`. -/ def linear_map.mk_continuous (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : E →SL[σ] F := ⟨f, add_monoid_hom_class.continuous_of_bound f C h⟩ /-- Reinterpret a linear map `𝕜 →ₗ[𝕜] E` as a continuous linear map. This construction is generalized to the case of any finite dimensional domain in `linear_map.to_continuous_linear_map`. -/ def linear_map.to_continuous_linear_map₁ (f : 𝕜 →ₗ[𝕜] E) : 𝕜 →L[𝕜] E := f.mk_continuous (∥f 1∥) $ λ x, le_of_eq $ by { conv_lhs { rw ← mul_one x }, rw [← smul_eq_mul, f.map_smul, norm_smul, mul_comm] } /-- Construct a continuous linear map from a linear map and the existence of a bound on this linear map. If you have an explicit bound, use `linear_map.mk_continuous` instead, as a norm estimate will follow automatically in `linear_map.mk_continuous_norm_le`. -/ def linear_map.mk_continuous_of_exists_bound (h : ∃C, ∀x, ∥f x∥ ≤ C * ∥x∥) : E →SL[σ] F := ⟨f, let ⟨C, hC⟩ := h in add_monoid_hom_class.continuous_of_bound f C hC⟩ lemma continuous_of_linear_of_boundₛₗ {f : E → F} (h_add : ∀ x y, f (x + y) = f x + f y) (h_smul : ∀ (c : 𝕜) x, f (c • x) = (σ c) • f x) {C : ℝ} (h_bound : ∀ x, ∥f x∥ ≤ C*∥x∥) : continuous f := let φ : E →ₛₗ[σ] F := { to_fun := f, map_add' := h_add, map_smul' := h_smul } in add_monoid_hom_class.continuous_of_bound φ C h_bound lemma continuous_of_linear_of_bound {f : E → G} (h_add : ∀ x y, f (x + y) = f x + f y) (h_smul : ∀ (c : 𝕜) x, f (c • x) = c • f x) {C : ℝ} (h_bound : ∀ x, ∥f x∥ ≤ C*∥x∥) : continuous f := let φ : E →ₗ[𝕜] G := { to_fun := f, map_add' := h_add, map_smul' := h_smul } in add_monoid_hom_class.continuous_of_bound φ C h_bound @[simp, norm_cast] lemma linear_map.mk_continuous_coe (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : ((f.mk_continuous C h) : E →ₛₗ[σ] F) = f := rfl @[simp] lemma linear_map.mk_continuous_apply (C : ℝ) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) (x : E) : f.mk_continuous C h x = f x := rfl @[simp, norm_cast] lemma linear_map.mk_continuous_of_exists_bound_coe (h : ∃C, ∀x, ∥f x∥ ≤ C * ∥x∥) : ((f.mk_continuous_of_exists_bound h) : E →ₛₗ[σ] F) = f := rfl @[simp] lemma linear_map.mk_continuous_of_exists_bound_apply (h : ∃C, ∀x, ∥f x∥ ≤ C * ∥x∥) (x : E) : f.mk_continuous_of_exists_bound h x = f x := rfl @[simp] lemma linear_map.to_continuous_linear_map₁_coe (f : 𝕜 →ₗ[𝕜] E) : (f.to_continuous_linear_map₁ : 𝕜 →ₗ[𝕜] E) = f := rfl @[simp] lemma linear_map.to_continuous_linear_map₁_apply (f : 𝕜 →ₗ[𝕜] E) (x) : f.to_continuous_linear_map₁ x = f x := rfl end normed_field variables [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [nondiscrete_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜 Eₗ] [normed_space 𝕜₂ F] [normed_space 𝕜 Fₗ] [normed_space 𝕜₃ G] [normed_space 𝕜 Gₗ] {σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃} {σ₁₃ : 𝕜 →+* 𝕜₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] /-- If `∥x∥ = 0` and `f` is continuous then `∥f x∥ = 0`. -/ lemma norm_image_of_norm_zero [semilinear_map_class 𝓕 σ₁₂ E F] (f : 𝓕) (hf : continuous f) {x : E} (hx : ∥x∥ = 0) : ∥f x∥ = 0 := begin refine le_antisymm (le_of_forall_pos_le_add (λ ε hε, _)) (norm_nonneg (f x)), rcases normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) ε hε with ⟨δ, δ_pos, hδ⟩, replace hδ := hδ x, rw [sub_zero, hx] at hδ, replace hδ := le_of_lt (hδ δ_pos), rw [map_zero, sub_zero] at hδ, rwa [zero_add] end section variables [ring_hom_isometric σ₁₂] [ring_hom_isometric σ₂₃] lemma semilinear_map_class.bound_of_shell_semi_normed [semilinear_map_class 𝓕 σ₁₂ E F] (f : 𝓕) {ε C : ℝ} (ε_pos : 0 < ε) {c : 𝕜} (hc : 1 < ∥c∥) (hf : ∀ x, ε / ∥c∥ ≤ ∥x∥ → ∥x∥ < ε → ∥f x∥ ≤ C * ∥x∥) {x : E} (hx : ∥x∥ ≠ 0) : ∥f x∥ ≤ C * ∥x∥ := begin rcases rescale_to_shell_semi_normed hc ε_pos hx with ⟨δ, hδ, δxle, leδx, δinv⟩, have := hf (δ • x) leδx δxle, simpa only [map_smulₛₗ, norm_smul, mul_left_comm C, mul_le_mul_left (norm_pos_iff.2 hδ), ring_hom_isometric.is_iso] using hf (δ • x) leδx δxle end /-- A continuous linear map between seminormed spaces is bounded when the field is nondiscrete. The continuity ensures boundedness on a ball of some radius `ε`. The nondiscreteness is then used to rescale any element into an element of norm in `[ε/C, ε]`, whose image has a controlled norm. The norm control for the original element follows by rescaling. -/ lemma semilinear_map_class.bound_of_continuous [semilinear_map_class 𝓕 σ₁₂ E F] (f : 𝓕) (hf : continuous f) : ∃ C, 0 < C ∧ (∀ x : E, ∥f x∥ ≤ C * ∥x∥) := begin rcases normed_group.tendsto_nhds_nhds.1 (hf.tendsto 0) 1 zero_lt_one with ⟨ε, ε_pos, hε⟩, simp only [sub_zero, map_zero] at hε, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, have : 0 < ∥c∥ / ε, from div_pos (zero_lt_one.trans hc) ε_pos, refine ⟨∥c∥ / ε, this, λ x, _⟩, by_cases hx : ∥x∥ = 0, { rw [hx, mul_zero], exact le_of_eq (norm_image_of_norm_zero f hf hx) }, refine semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc (λ x hle hlt, _) hx, refine (hε _ hlt).le.trans _, rwa [← div_le_iff' this, one_div_div] end end namespace continuous_linear_map theorem bound [ring_hom_isometric σ₁₂] (f : E →SL[σ₁₂] F) : ∃ C, 0 < C ∧ (∀ x : E, ∥f x∥ ≤ C * ∥x∥) := semilinear_map_class.bound_of_continuous f f.2 section open filter /-- A linear map which is a homothety is a continuous linear map. Since the field `𝕜` need not have `ℝ` as a subfield, this theorem is not directly deducible from the corresponding theorem about isometries plus a theorem about scalar multiplication. Likewise for the other theorems about homotheties in this file. -/ def of_homothety (f : E →ₛₗ[σ₁₂] F) (a : ℝ) (hf : ∀x, ∥f x∥ = a * ∥x∥) : E →SL[σ₁₂] F := f.mk_continuous a (λ x, le_of_eq (hf x)) variable (𝕜) lemma to_span_singleton_homothety (x : E) (c : 𝕜) : ∥linear_map.to_span_singleton 𝕜 E x c∥ = ∥x∥ * ∥c∥ := by {rw mul_comm, exact norm_smul _ _} /-- Given an element `x` of a normed space `E` over a field `𝕜`, the natural continuous linear map from `𝕜` to `E` by taking multiples of `x`.-/ def to_span_singleton (x : E) : 𝕜 →L[𝕜] E := of_homothety (linear_map.to_span_singleton 𝕜 E x) ∥x∥ (to_span_singleton_homothety 𝕜 x) lemma to_span_singleton_apply (x : E) (r : 𝕜) : to_span_singleton 𝕜 x r = r • x := by simp [to_span_singleton, of_homothety, linear_map.to_span_singleton] lemma to_span_singleton_add (x y : E) : to_span_singleton 𝕜 (x + y) = to_span_singleton 𝕜 x + to_span_singleton 𝕜 y := by { ext1, simp [to_span_singleton_apply], } lemma to_span_singleton_smul' (𝕜') [normed_field 𝕜'] [normed_space 𝕜' E] [smul_comm_class 𝕜 𝕜' E] (c : 𝕜') (x : E) : to_span_singleton 𝕜 (c • x) = c • to_span_singleton 𝕜 x := by { ext1, rw [to_span_singleton_apply, smul_apply, to_span_singleton_apply, smul_comm], } lemma to_span_singleton_smul (c : 𝕜) (x : E) : to_span_singleton 𝕜 (c • x) = c • to_span_singleton 𝕜 x := to_span_singleton_smul' 𝕜 𝕜 c x variables (𝕜 E) /-- Given a unit-length element `x` of a normed space `E` over a field `𝕜`, the natural linear isometry map from `𝕜` to `E` by taking multiples of `x`.-/ def _root_.linear_isometry.to_span_singleton {v : E} (hv : ∥v∥ = 1) : 𝕜 →ₗᵢ[𝕜] E := { norm_map' := λ x, by simp [norm_smul, hv], .. linear_map.to_span_singleton 𝕜 E v } variables {𝕜 E} @[simp] lemma _root_.linear_isometry.to_span_singleton_apply {v : E} (hv : ∥v∥ = 1) (a : 𝕜) : linear_isometry.to_span_singleton 𝕜 E hv a = a • v := rfl @[simp] lemma _root_.linear_isometry.coe_to_span_singleton {v : E} (hv : ∥v∥ = 1) : (linear_isometry.to_span_singleton 𝕜 E hv).to_linear_map = linear_map.to_span_singleton 𝕜 E v := rfl end section op_norm open set real /-- The operator norm of a continuous linear map is the inf of all its bounds. -/ def op_norm (f : E →SL[σ₁₂] F) := Inf {c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥} instance has_op_norm : has_norm (E →SL[σ₁₂] F) := ⟨op_norm⟩ lemma norm_def (f : E →SL[σ₁₂] F) : ∥f∥ = Inf {c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥} := rfl -- So that invocations of `le_cInf` make sense: we show that the set of -- bounds is nonempty and bounded below. lemma bounds_nonempty [ring_hom_isometric σ₁₂] {f : E →SL[σ₁₂] F} : ∃ c, c ∈ { c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := let ⟨M, hMp, hMb⟩ := f.bound in ⟨M, le_of_lt hMp, hMb⟩ lemma bounds_bdd_below {f : E →SL[σ₁₂] F} : bdd_below { c | 0 ≤ c ∧ ∀ x, ∥f x∥ ≤ c * ∥x∥ } := ⟨0, λ _ ⟨hn, _⟩, hn⟩ /-- If one controls the norm of every `A x`, then one controls the norm of `A`. -/ lemma op_norm_le_bound (f : E →SL[σ₁₂] F) {M : ℝ} (hMp: 0 ≤ M) (hM : ∀ x, ∥f x∥ ≤ M * ∥x∥) : ∥f∥ ≤ M := cInf_le bounds_bdd_below ⟨hMp, hM⟩ theorem op_norm_le_of_lipschitz {f : E →SL[σ₁₂] F} {K : ℝ≥0} (hf : lipschitz_with K f) : ∥f∥ ≤ K := f.op_norm_le_bound K.2 $ λ x, by simpa only [dist_zero_right, f.map_zero] using hf.dist_le_mul x 0 lemma op_norm_eq_of_bounds {φ : E →SL[σ₁₂] F} {M : ℝ} (M_nonneg : 0 ≤ M) (h_above : ∀ x, ∥φ x∥ ≤ M*∥x∥) (h_below : ∀ N ≥ 0, (∀ x, ∥φ x∥ ≤ N*∥x∥) → M ≤ N) : ∥φ∥ = M := le_antisymm (φ.op_norm_le_bound M_nonneg h_above) ((le_cInf_iff continuous_linear_map.bounds_bdd_below ⟨M, M_nonneg, h_above⟩).mpr $ λ N ⟨N_nonneg, hN⟩, h_below N N_nonneg hN) lemma op_norm_neg (f : E →SL[σ₁₂] F) : ∥-f∥ = ∥f∥ := by simp only [norm_def, neg_apply, norm_neg] theorem antilipschitz_of_bound (f : E →SL[σ₁₂] F) {K : ℝ≥0} (h : ∀ x, ∥x∥ ≤ K * ∥f x∥) : antilipschitz_with K f := add_monoid_hom_class.antilipschitz_of_bound _ h lemma bound_of_antilipschitz (f : E →SL[σ₁₂] F) {K : ℝ≥0} (h : antilipschitz_with K f) (x) : ∥x∥ ≤ K * ∥f x∥ := add_monoid_hom_class.bound_of_antilipschitz _ h x section variables [ring_hom_isometric σ₁₂] [ring_hom_isometric σ₂₃] (f g : E →SL[σ₁₂] F) (h : F →SL[σ₂₃] G) (x : E) lemma op_norm_nonneg : 0 ≤ ∥f∥ := le_cInf bounds_nonempty (λ _ ⟨hx, _⟩, hx) /-- The fundamental property of the operator norm: `∥f x∥ ≤ ∥f∥ * ∥x∥`. -/ theorem le_op_norm : ∥f x∥ ≤ ∥f∥ * ∥x∥ := begin obtain ⟨C, Cpos, hC⟩ := f.bound, replace hC := hC x, by_cases h : ∥x∥ = 0, { rwa [h, mul_zero] at ⊢ hC }, have hlt : 0 < ∥x∥ := lt_of_le_of_ne (norm_nonneg x) (ne.symm h), exact (div_le_iff hlt).mp (le_cInf bounds_nonempty (λ c ⟨_, hc⟩, (div_le_iff hlt).mpr $ by { apply hc })), end theorem dist_le_op_norm (x y : E) : dist (f x) (f y) ≤ ∥f∥ * dist x y := by simp_rw [dist_eq_norm, ← map_sub, f.le_op_norm] theorem le_op_norm_of_le {c : ℝ} {x} (h : ∥x∥ ≤ c) : ∥f x∥ ≤ ∥f∥ * c := le_trans (f.le_op_norm x) (mul_le_mul_of_nonneg_left h f.op_norm_nonneg) theorem le_of_op_norm_le {c : ℝ} (h : ∥f∥ ≤ c) (x : E) : ∥f x∥ ≤ c * ∥x∥ := (f.le_op_norm x).trans (mul_le_mul_of_nonneg_right h (norm_nonneg x)) lemma ratio_le_op_norm : ∥f x∥ / ∥x∥ ≤ ∥f∥ := div_le_of_nonneg_of_le_mul (norm_nonneg _) f.op_norm_nonneg (le_op_norm _ _) /-- The image of the unit ball under a continuous linear map is bounded. -/ lemma unit_le_op_norm : ∥x∥ ≤ 1 → ∥f x∥ ≤ ∥f∥ := mul_one ∥f∥ ▸ f.le_op_norm_of_le lemma op_norm_le_of_shell {f : E →SL[σ₁₂] F} {ε C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C) {c : 𝕜} (hc : 1 < ∥c∥) (hf : ∀ x, ε / ∥c∥ ≤ ∥x∥ → ∥x∥ < ε → ∥f x∥ ≤ C * ∥x∥) : ∥f∥ ≤ C := begin refine f.op_norm_le_bound hC (λ x, _), by_cases hx : ∥x∥ = 0, { rw [hx, mul_zero], exact le_of_eq (norm_image_of_norm_zero f f.2 hx) }, exact semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc hf hx end lemma op_norm_le_of_ball {f : E →SL[σ₁₂] F} {ε : ℝ} {C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C) (hf : ∀ x ∈ ball (0 : E) ε, ∥f x∥ ≤ C * ∥x∥) : ∥f∥ ≤ C := begin rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine op_norm_le_of_shell ε_pos hC hc (λ x _ hx, hf x _), rwa ball_zero_eq end lemma op_norm_le_of_nhds_zero {f : E →SL[σ₁₂] F} {C : ℝ} (hC : 0 ≤ C) (hf : ∀ᶠ x in 𝓝 (0 : E), ∥f x∥ ≤ C * ∥x∥) : ∥f∥ ≤ C := let ⟨ε, ε0, hε⟩ := metric.eventually_nhds_iff_ball.1 hf in op_norm_le_of_ball ε0 hC hε lemma op_norm_le_of_shell' {f : E →SL[σ₁₂] F} {ε C : ℝ} (ε_pos : 0 < ε) (hC : 0 ≤ C) {c : 𝕜} (hc : ∥c∥ < 1) (hf : ∀ x, ε * ∥c∥ ≤ ∥x∥ → ∥x∥ < ε → ∥f x∥ ≤ C * ∥x∥) : ∥f∥ ≤ C := begin by_cases h0 : c = 0, { refine op_norm_le_of_ball ε_pos hC (λ x hx, hf x _ _), { simp [h0] }, { rwa ball_zero_eq at hx } }, { rw [← inv_inv c, norm_inv, inv_lt_one_iff_of_pos (norm_pos_iff.2 $ inv_ne_zero h0)] at hc, refine op_norm_le_of_shell ε_pos hC hc _, rwa [norm_inv, div_eq_mul_inv, inv_inv] } end /-- The operator norm satisfies the triangle inequality. -/ theorem op_norm_add_le : ∥f + g∥ ≤ ∥f∥ + ∥g∥ := (f + g).op_norm_le_bound (add_nonneg f.op_norm_nonneg g.op_norm_nonneg) $ λ x, (norm_add_le_of_le (f.le_op_norm x) (g.le_op_norm x)).trans_eq (add_mul _ _ _).symm /-- The norm of the `0` operator is `0`. -/ theorem op_norm_zero : ∥(0 : E →SL[σ₁₂] F)∥ = 0 := le_antisymm (cInf_le bounds_bdd_below ⟨le_rfl, λ _, le_of_eq (by { rw [zero_mul], exact norm_zero })⟩) (op_norm_nonneg _) /-- The norm of the identity is at most `1`. It is in fact `1`, except when the space is trivial where it is `0`. It means that one can not do better than an inequality in general. -/ lemma norm_id_le : ∥id 𝕜 E∥ ≤ 1 := op_norm_le_bound _ zero_le_one (λx, by simp) /-- If there is an element with norm different from `0`, then the norm of the identity equals `1`. (Since we are working with seminorms supposing that the space is non-trivial is not enough.) -/ lemma norm_id_of_nontrivial_seminorm (h : ∃ (x : E), ∥x∥ ≠ 0) : ∥id 𝕜 E∥ = 1 := le_antisymm norm_id_le $ let ⟨x, hx⟩ := h in have _ := (id 𝕜 E).ratio_le_op_norm x, by rwa [id_apply, div_self hx] at this lemma op_norm_smul_le {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜₂ 𝕜' F] (c : 𝕜') (f : E →SL[σ₁₂] F) : ∥c • f∥ ≤ ∥c∥ * ∥f∥ := ((c • f).op_norm_le_bound (mul_nonneg (norm_nonneg _) (op_norm_nonneg _)) (λ _, begin erw [norm_smul, mul_assoc], exact mul_le_mul_of_nonneg_left (le_op_norm _ _) (norm_nonneg _) end)) /-- Continuous linear maps themselves form a seminormed space with respect to the operator norm. -/ instance to_semi_normed_group : semi_normed_group (E →SL[σ₁₂] F) := semi_normed_group.of_core _ ⟨op_norm_zero, λ x y, op_norm_add_le x y, op_norm_neg⟩ lemma nnnorm_def (f : E →SL[σ₁₂] F) : ∥f∥₊ = Inf {c | ∀ x, ∥f x∥₊ ≤ c * ∥x∥₊} := begin ext, rw [nnreal.coe_Inf, coe_nnnorm, norm_def, nnreal.coe_image], simp_rw [← nnreal.coe_le_coe, nnreal.coe_mul, coe_nnnorm, mem_set_of_eq, subtype.coe_mk, exists_prop], end /-- If one controls the norm of every `A x`, then one controls the norm of `A`. -/ lemma op_nnnorm_le_bound (f : E →SL[σ₁₂] F) (M : ℝ≥0) (hM : ∀ x, ∥f x∥₊ ≤ M * ∥x∥₊) : ∥f∥₊ ≤ M := op_norm_le_bound f (zero_le M) hM theorem op_nnnorm_le_of_lipschitz {f : E →SL[σ₁₂] F} {K : ℝ≥0} (hf : lipschitz_with K f) : ∥f∥₊ ≤ K := op_norm_le_of_lipschitz hf lemma op_nnnorm_eq_of_bounds {φ : E →SL[σ₁₂] F} (M : ℝ≥0) (h_above : ∀ x, ∥φ x∥ ≤ M*∥x∥) (h_below : ∀ N, (∀ x, ∥φ x∥₊ ≤ N*∥x∥₊) → M ≤ N) : ∥φ∥₊ = M := subtype.ext $ op_norm_eq_of_bounds (zero_le M) h_above $ subtype.forall'.mpr h_below instance to_normed_space {𝕜' : Type*} [normed_field 𝕜'] [normed_space 𝕜' F] [smul_comm_class 𝕜₂ 𝕜' F] : normed_space 𝕜' (E →SL[σ₁₂] F) := ⟨op_norm_smul_le⟩ include σ₁₃ /-- The operator norm is submultiplicative. -/ lemma op_norm_comp_le (f : E →SL[σ₁₂] F) : ∥h.comp f∥ ≤ ∥h∥ * ∥f∥ := (cInf_le bounds_bdd_below ⟨mul_nonneg (op_norm_nonneg _) (op_norm_nonneg _), λ x, by { rw mul_assoc, exact h.le_op_norm_of_le (f.le_op_norm x) } ⟩) lemma op_nnnorm_comp_le [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₂] F) : ∥h.comp f∥₊ ≤ ∥h∥₊ * ∥f∥₊ := op_norm_comp_le h f omit σ₁₃ /-- Continuous linear maps form a seminormed ring with respect to the operator norm. -/ instance to_semi_normed_ring : semi_normed_ring (E →L[𝕜] E) := { norm_mul := λ f g, op_norm_comp_le f g, .. continuous_linear_map.to_semi_normed_group, .. continuous_linear_map.ring } /-- For a normed space `E`, continuous linear endomorphisms form a normed algebra with respect to the operator norm. -/ instance to_normed_algebra : normed_algebra 𝕜 (E →L[𝕜] E) := { .. continuous_linear_map.to_normed_space, .. continuous_linear_map.algebra } theorem le_op_nnnorm : ∥f x∥₊ ≤ ∥f∥₊ * ∥x∥₊ := f.le_op_norm x theorem nndist_le_op_nnnorm (x y : E) : nndist (f x) (f y) ≤ ∥f∥₊ * nndist x y := dist_le_op_norm f x y /-- continuous linear maps are Lipschitz continuous. -/ theorem lipschitz : lipschitz_with ∥f∥₊ f := add_monoid_hom_class.lipschitz_of_bound_nnnorm f _ f.le_op_nnnorm /-- Evaluation of a continuous linear map `f` at a point is Lipschitz continuous in `f`. -/ theorem lipschitz_apply (x : E) : lipschitz_with ∥x∥₊ (λ f : E →SL[σ₁₂] F, f x) := lipschitz_with_iff_norm_sub_le.2 $ λ f g, ((f - g).le_op_norm x).trans_eq (mul_comm _ _) end section lemma op_norm_ext [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₂] F) (g : E →SL[σ₁₃] G) (h : ∀ x, ∥f x∥ = ∥g x∥) : ∥f∥ = ∥g∥ := op_norm_eq_of_bounds (norm_nonneg _) (λ x, by { rw h x, exact le_op_norm _ _ }) (λ c hc h₂, op_norm_le_bound _ hc (λ z, by { rw ←h z, exact h₂ z })) variables [ring_hom_isometric σ₂₃] theorem op_norm_le_bound₂ (f : E →SL[σ₁₃] F →SL[σ₂₃] G) {C : ℝ} (h0 : 0 ≤ C) (hC : ∀ x y, ∥f x y∥ ≤ C * ∥x∥ * ∥y∥) : ∥f∥ ≤ C := f.op_norm_le_bound h0 $ λ x, (f x).op_norm_le_bound (mul_nonneg h0 (norm_nonneg _)) $ hC x theorem le_op_norm₂ [ring_hom_isometric σ₁₃] (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (x : E) (y : F) : ∥f x y∥ ≤ ∥f∥ * ∥x∥ * ∥y∥ := (f x).le_of_op_norm_le (f.le_op_norm x) y end @[simp] lemma op_norm_prod (f : E →L[𝕜] Fₗ) (g : E →L[𝕜] Gₗ) : ∥f.prod g∥ = ∥(f, g)∥ := le_antisymm (op_norm_le_bound _ (norm_nonneg _) $ λ x, by simpa only [prod_apply, prod.norm_def, max_mul_of_nonneg, norm_nonneg] using max_le_max (le_op_norm f x) (le_op_norm g x)) $ max_le (op_norm_le_bound _ (norm_nonneg _) $ λ x, (le_max_left _ _).trans ((f.prod g).le_op_norm x)) (op_norm_le_bound _ (norm_nonneg _) $ λ x, (le_max_right _ _).trans ((f.prod g).le_op_norm x)) @[simp] lemma op_nnnorm_prod (f : E →L[𝕜] Fₗ) (g : E →L[𝕜] Gₗ) : ∥f.prod g∥₊ = ∥(f, g)∥₊ := subtype.ext $ op_norm_prod f g /-- `continuous_linear_map.prod` as a `linear_isometry_equiv`. -/ def prodₗᵢ (R : Type*) [semiring R] [module R Fₗ] [module R Gₗ] [has_continuous_const_smul R Fₗ] [has_continuous_const_smul R Gₗ] [smul_comm_class 𝕜 R Fₗ] [smul_comm_class 𝕜 R Gₗ] : (E →L[𝕜] Fₗ) × (E →L[𝕜] Gₗ) ≃ₗᵢ[R] (E →L[𝕜] Fₗ × Gₗ) := ⟨prodₗ R, λ ⟨f, g⟩, op_norm_prod f g⟩ variables [ring_hom_isometric σ₁₂] (f : E →SL[σ₁₂] F) @[simp, nontriviality] lemma op_norm_subsingleton [subsingleton E] : ∥f∥ = 0 := begin refine le_antisymm _ (norm_nonneg _), apply op_norm_le_bound _ rfl.ge, intros x, simp [subsingleton.elim x 0] end end op_norm section is_O variables [ring_hom_isometric σ₁₂] (c : 𝕜) (f g : E →SL[σ₁₂] F) (h : F →SL[σ₂₃] G) (x y z : E) open asymptotics theorem is_O_with_id (l : filter E) : is_O_with ∥f∥ l f (λ x, x) := is_O_with_of_le' _ f.le_op_norm theorem is_O_id (l : filter E) : f =O[l] (λ x, x) := (f.is_O_with_id l).is_O theorem is_O_with_comp [ring_hom_isometric σ₂₃] {α : Type*} (g : F →SL[σ₂₃] G) (f : α → F) (l : filter α) : is_O_with ∥g∥ l (λ x', g (f x')) f := (g.is_O_with_id ⊤).comp_tendsto le_top theorem is_O_comp [ring_hom_isometric σ₂₃] {α : Type*} (g : F →SL[σ₂₃] G) (f : α → F) (l : filter α) : (λ x', g (f x')) =O[l] f := (g.is_O_with_comp f l).is_O theorem is_O_with_sub (f : E →SL[σ₁₂] F) (l : filter E) (x : E) : is_O_with ∥f∥ l (λ x', f (x' - x)) (λ x', x' - x) := f.is_O_with_comp _ l theorem is_O_sub (f : E →SL[σ₁₂] F) (l : filter E) (x : E) : (λ x', f (x' - x)) =O[l] (λ x', x' - x) := f.is_O_comp _ l end is_O end continuous_linear_map namespace linear_isometry lemma norm_to_continuous_linear_map_le (f : E →ₛₗᵢ[σ₁₂] F) : ∥f.to_continuous_linear_map∥ ≤ 1 := f.to_continuous_linear_map.op_norm_le_bound zero_le_one $ λ x, by simp end linear_isometry namespace linear_map /-- If a continuous linear map is constructed from a linear map via the constructor `mk_continuous`, then its norm is bounded by the bound given to the constructor if it is nonnegative. -/ lemma mk_continuous_norm_le (f : E →ₛₗ[σ₁₂] F) {C : ℝ} (hC : 0 ≤ C) (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : ∥f.mk_continuous C h∥ ≤ C := continuous_linear_map.op_norm_le_bound _ hC h /-- If a continuous linear map is constructed from a linear map via the constructor `mk_continuous`, then its norm is bounded by the bound or zero if bound is negative. -/ lemma mk_continuous_norm_le' (f : E →ₛₗ[σ₁₂] F) {C : ℝ} (h : ∀x, ∥f x∥ ≤ C * ∥x∥) : ∥f.mk_continuous C h∥ ≤ max C 0 := continuous_linear_map.op_norm_le_bound _ (le_max_right _ _) $ λ x, (h x).trans $ mul_le_mul_of_nonneg_right (le_max_left _ _) (norm_nonneg x) variables [ring_hom_isometric σ₂₃] /-- Create a bilinear map (represented as a map `E →L[𝕜] F →L[𝕜] G`) from the corresponding linear map and a bound on the norm of the image. The linear map can be constructed using `linear_map.mk₂`. -/ def mk_continuous₂ (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) (C : ℝ) (hC : ∀ x y, ∥f x y∥ ≤ C * ∥x∥ * ∥y∥) : E →SL[σ₁₃] F →SL[σ₂₃] G := linear_map.mk_continuous { to_fun := λ x, (f x).mk_continuous (C * ∥x∥) (hC x), map_add' := λ x y, by { ext z, simp }, map_smul' := λ c x, by { ext z, simp } } (max C 0) $ λ x, (mk_continuous_norm_le' _ _).trans_eq $ by rw [max_mul_of_nonneg _ _ (norm_nonneg x), zero_mul] @[simp] lemma mk_continuous₂_apply (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ} (hC : ∀ x y, ∥f x y∥ ≤ C * ∥x∥ * ∥y∥) (x : E) (y : F) : f.mk_continuous₂ C hC x y = f x y := rfl lemma mk_continuous₂_norm_le' (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ} (hC : ∀ x y, ∥f x y∥ ≤ C * ∥x∥ * ∥y∥) : ∥f.mk_continuous₂ C hC∥ ≤ max C 0 := mk_continuous_norm_le _ (le_max_iff.2 $ or.inr le_rfl) _ lemma mk_continuous₂_norm_le (f : E →ₛₗ[σ₁₃] F →ₛₗ[σ₂₃] G) {C : ℝ} (h0 : 0 ≤ C) (hC : ∀ x y, ∥f x y∥ ≤ C * ∥x∥ * ∥y∥) : ∥f.mk_continuous₂ C hC∥ ≤ C := (f.mk_continuous₂_norm_le' hC).trans_eq $ max_eq_left h0 end linear_map namespace continuous_linear_map variables [ring_hom_isometric σ₂₃] [ring_hom_isometric σ₁₃] /-- Flip the order of arguments of a continuous bilinear map. For a version bundled as `linear_isometry_equiv`, see `continuous_linear_map.flipL`. -/ def flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : F →SL[σ₂₃] E →SL[σ₁₃] G := linear_map.mk_continuous₂ (linear_map.mk₂'ₛₗ σ₂₃ σ₁₃ (λ y x, f x y) (λ x y z, (f z).map_add x y) (λ c y x, (f x).map_smulₛₗ c y) (λ z x y, by rw [f.map_add, add_apply]) (λ c y x, by rw [f.map_smulₛₗ, smul_apply])) ∥f∥ (λ y x, (f.le_op_norm₂ x y).trans_eq $ by rw mul_right_comm) private lemma le_norm_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : ∥f∥ ≤ ∥flip f∥ := f.op_norm_le_bound₂ (norm_nonneg _) $ λ x y, by { rw mul_right_comm, exact (flip f).le_op_norm₂ y x } @[simp] lemma flip_apply (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (x : E) (y : F) : f.flip y x = f x y := rfl @[simp] lemma flip_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : f.flip.flip = f := by { ext, refl } @[simp] lemma op_norm_flip (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : ∥f.flip∥ = ∥f∥ := le_antisymm (by simpa only [flip_flip] using le_norm_flip f.flip) (le_norm_flip f) @[simp] lemma flip_add (f g : E →SL[σ₁₃] F →SL[σ₂₃] G) : (f + g).flip = f.flip + g.flip := rfl @[simp] lemma flip_smul (c : 𝕜₃) (f : E →SL[σ₁₃] F →SL[σ₂₃] G) : (c • f).flip = c • f.flip := rfl variables (E F G σ₁₃ σ₂₃) /-- Flip the order of arguments of a continuous bilinear map. This is a version bundled as a `linear_isometry_equiv`. For an unbundled version see `continuous_linear_map.flip`. -/ def flipₗᵢ' : (E →SL[σ₁₃] F →SL[σ₂₃] G) ≃ₗᵢ[𝕜₃] (F →SL[σ₂₃] E →SL[σ₁₃] G) := { to_fun := flip, inv_fun := flip, map_add' := flip_add, map_smul' := flip_smul, left_inv := flip_flip, right_inv := flip_flip, norm_map' := op_norm_flip } variables {E F G σ₁₃ σ₂₃} @[simp] lemma flipₗᵢ'_symm : (flipₗᵢ' E F G σ₂₃ σ₁₃).symm = flipₗᵢ' F E G σ₁₃ σ₂₃ := rfl @[simp] lemma coe_flipₗᵢ' : ⇑(flipₗᵢ' E F G σ₂₃ σ₁₃) = flip := rfl variables (𝕜 E Fₗ Gₗ) /-- Flip the order of arguments of a continuous bilinear map. This is a version bundled as a `linear_isometry_equiv`. For an unbundled version see `continuous_linear_map.flip`. -/ def flipₗᵢ : (E →L[𝕜] Fₗ →L[𝕜] Gₗ) ≃ₗᵢ[𝕜] (Fₗ →L[𝕜] E →L[𝕜] Gₗ) := { to_fun := flip, inv_fun := flip, map_add' := flip_add, map_smul' := flip_smul, left_inv := flip_flip, right_inv := flip_flip, norm_map' := op_norm_flip } variables {𝕜 E Fₗ Gₗ} @[simp] lemma flipₗᵢ_symm : (flipₗᵢ 𝕜 E Fₗ Gₗ).symm = flipₗᵢ 𝕜 Fₗ E Gₗ := rfl @[simp] lemma coe_flipₗᵢ : ⇑(flipₗᵢ 𝕜 E Fₗ Gₗ) = flip := rfl variables (F σ₁₂) [ring_hom_isometric σ₁₂] /-- The continuous semilinear map obtained by applying a continuous semilinear map at a given vector. This is the continuous version of `linear_map.applyₗ`. -/ def apply' : E →SL[σ₁₂] (E →SL[σ₁₂] F) →L[𝕜₂] F := flip (id 𝕜₂ (E →SL[σ₁₂] F)) variables {F σ₁₂} @[simp] lemma apply_apply' (v : E) (f : E →SL[σ₁₂] F) : apply' F σ₁₂ v f = f v := rfl variables (𝕜 Fₗ) /-- The continuous semilinear map obtained by applying a continuous semilinear map at a given vector. This is the continuous version of `linear_map.applyₗ`. -/ def apply : E →L[𝕜] (E →L[𝕜] Fₗ) →L[𝕜] Fₗ := flip (id 𝕜 (E →L[𝕜] Fₗ)) variables {𝕜 Fₗ} @[simp] lemma apply_apply (v : E) (f : E →L[𝕜] Fₗ) : apply 𝕜 Fₗ v f = f v := rfl variables (σ₁₂ σ₂₃ E F G) /-- Composition of continuous semilinear maps as a continuous semibilinear map. -/ def compSL : (F →SL[σ₂₃] G) →L[𝕜₃] (E →SL[σ₁₂] F) →SL[σ₂₃] (E →SL[σ₁₃] G) := linear_map.mk_continuous₂ (linear_map.mk₂'ₛₗ (ring_hom.id 𝕜₃) σ₂₃ comp add_comp smul_comp comp_add (λ c f g, by { ext, simp only [continuous_linear_map.map_smulₛₗ, coe_smul', coe_comp', function.comp_app, pi.smul_apply] })) 1 $ λ f g, by simpa only [one_mul] using op_norm_comp_le f g variables {𝕜 σ₁₂ σ₂₃ E F G} include σ₁₃ @[simp] lemma compSL_apply (f : F →SL[σ₂₃] G) (g : E →SL[σ₁₂] F) : compSL E F G σ₁₂ σ₂₃ f g = f.comp g := rfl lemma _root_.continuous.const_clm_comp {X} [topological_space X] {f : X → E →SL[σ₁₂] F} (hf : continuous f) (g : F →SL[σ₂₃] G) : continuous (λ x, g.comp (f x) : X → E →SL[σ₁₃] G) := (compSL E F G σ₁₂ σ₂₃ g).continuous.comp hf -- Giving the implicit argument speeds up elaboration significantly lemma _root_.continuous.clm_comp_const {X} [topological_space X] {g : X → F →SL[σ₂₃] G} (hg : continuous g) (f : E →SL[σ₁₂] F) : continuous (λ x, (g x).comp f : X → E →SL[σ₁₃] G) := (@continuous_linear_map.flip _ _ _ _ _ (E →SL[σ₁₃] G) _ _ _ _ _ _ _ _ _ _ _ _ _ (compSL E F G σ₁₂ σ₂₃) f).continuous.comp hg omit σ₁₃ variables (𝕜 σ₁₂ σ₂₃ E Fₗ Gₗ) /-- Composition of continuous linear maps as a continuous bilinear map. -/ def compL : (Fₗ →L[𝕜] Gₗ) →L[𝕜] (E →L[𝕜] Fₗ) →L[𝕜] (E →L[𝕜] Gₗ) := compSL E Fₗ Gₗ (ring_hom.id 𝕜) (ring_hom.id 𝕜) @[simp] lemma compL_apply (f : Fₗ →L[𝕜] Gₗ) (g : E →L[𝕜] Fₗ) : compL 𝕜 E Fₗ Gₗ f g = f.comp g := rfl variables (Eₗ) {𝕜 E Fₗ Gₗ} /-- Apply `L(x,-)` pointwise to bilinear maps, as a continuous bilinear map -/ @[simps apply] def precompR (L : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : E →L[𝕜] (Eₗ →L[𝕜] Fₗ) →L[𝕜] (Eₗ →L[𝕜] Gₗ) := (compL 𝕜 Eₗ Fₗ Gₗ).comp L /-- Apply `L(-,y)` pointwise to bilinear maps, as a continuous bilinear map -/ def precompL (L : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : (Eₗ →L[𝕜] E) →L[𝕜] Fₗ →L[𝕜] (Eₗ →L[𝕜] Gₗ) := (precompR Eₗ (flip L)).flip section prod universes u₁ u₂ u₃ u₄ variables (M₁ : Type u₁) [semi_normed_group M₁] [normed_space 𝕜 M₁] (M₂ : Type u₂) [semi_normed_group M₂] [normed_space 𝕜 M₂] (M₃ : Type u₃) [semi_normed_group M₃] [normed_space 𝕜 M₃] (M₄ : Type u₄) [semi_normed_group M₄] [normed_space 𝕜 M₄] variables {Eₗ} (𝕜) /-- `continuous_linear_map.prod_map` as a continuous linear map. -/ def prod_mapL : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] ((M₁ × M₃) →L[𝕜] (M₂ × M₄)) := continuous_linear_map.copy (have Φ₁ : (M₁ →L[𝕜] M₂) →L[𝕜] (M₁ →L[𝕜] M₂ × M₄), from continuous_linear_map.compL 𝕜 M₁ M₂ (M₂ × M₄) (continuous_linear_map.inl 𝕜 M₂ M₄), have Φ₂ : (M₃ →L[𝕜] M₄) →L[𝕜] (M₃ →L[𝕜] M₂ × M₄), from continuous_linear_map.compL 𝕜 M₃ M₄ (M₂ × M₄) (continuous_linear_map.inr 𝕜 M₂ M₄), have Φ₁' : _, from (continuous_linear_map.compL 𝕜 (M₁ × M₃) M₁ (M₂ × M₄)).flip (continuous_linear_map.fst 𝕜 M₁ M₃), have Φ₂' : _ , from (continuous_linear_map.compL 𝕜 (M₁ × M₃) M₃ (M₂ × M₄)).flip (continuous_linear_map.snd 𝕜 M₁ M₃), have Ψ₁ : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] (M₁ →L[𝕜] M₂), from continuous_linear_map.fst 𝕜 (M₁ →L[𝕜] M₂) (M₃ →L[𝕜] M₄), have Ψ₂ : ((M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) →L[𝕜] (M₃ →L[𝕜] M₄), from continuous_linear_map.snd 𝕜 (M₁ →L[𝕜] M₂) (M₃ →L[𝕜] M₄), Φ₁' ∘L Φ₁ ∘L Ψ₁ + Φ₂' ∘L Φ₂ ∘L Ψ₂) (λ p : (M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄), p.1.prod_map p.2) (begin apply funext, rintros ⟨φ, ψ⟩, apply continuous_linear_map.ext (λ x, _), simp only [add_apply, coe_comp', coe_fst', function.comp_app, compL_apply, flip_apply, coe_snd', inl_apply, inr_apply, prod.mk_add_mk, add_zero, zero_add, coe_prod_map', prod_map, prod.mk.inj_iff, eq_self_iff_true, and_self], refl end) variables {M₁ M₂ M₃ M₄} @[simp] lemma prod_mapL_apply (p : (M₁ →L[𝕜] M₂) × (M₃ →L[𝕜] M₄)) : continuous_linear_map.prod_mapL 𝕜 M₁ M₂ M₃ M₄ p = p.1.prod_map p.2 := rfl variables {X : Type*} [topological_space X] lemma _root_.continuous.prod_mapL {f : X → M₁ →L[𝕜] M₂} {g : X → M₃ →L[𝕜] M₄} (hf : continuous f) (hg : continuous g) : continuous (λ x, (f x).prod_map (g x)) := (prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp (hf.prod_mk hg) lemma _root_.continuous.prod_map_equivL {f : X → M₁ ≃L[𝕜] M₂} {g : X → M₃ ≃L[𝕜] M₄} (hf : continuous (λ x, (f x : M₁ →L[𝕜] M₂))) (hg : continuous (λ x, (g x : M₃ →L[𝕜] M₄))) : continuous (λ x, ((f x).prod (g x) : M₁ × M₃ →L[𝕜] M₂ × M₄)) := (prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp (hf.prod_mk hg) lemma _root_.continuous_on.prod_mapL {f : X → M₁ →L[𝕜] M₂} {g : X → M₃ →L[𝕜] M₄} {s : set X} (hf : continuous_on f s) (hg : continuous_on g s) : continuous_on (λ x, (f x).prod_map (g x)) s := ((prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp_continuous_on (hf.prod hg) : _) lemma _root_.continuous_on.prod_map_equivL {f : X → M₁ ≃L[𝕜] M₂} {g : X → M₃ ≃L[𝕜] M₄} {s : set X} (hf : continuous_on (λ x, (f x : M₁ →L[𝕜] M₂)) s) (hg : continuous_on (λ x, (g x : M₃ →L[𝕜] M₄)) s) : continuous_on (λ x, ((f x).prod (g x) : M₁ × M₃ →L[𝕜] M₂ × M₄)) s := (prod_mapL 𝕜 M₁ M₂ M₃ M₄).continuous.comp_continuous_on (hf.prod hg) end prod variables {𝕜 E Fₗ Gₗ} section multiplication_linear variables (𝕜) (𝕜' : Type*) [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] /-- Left multiplication in a normed algebra as a continuous bilinear map. -/ def lmul : 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' := (algebra.lmul 𝕜 𝕜').to_linear_map.mk_continuous₂ 1 $ λ x y, by simpa using norm_mul_le x y @[simp] lemma lmul_apply (x y : 𝕜') : lmul 𝕜 𝕜' x y = x * y := rfl @[simp] lemma op_norm_lmul_apply_le (x : 𝕜') : ∥lmul 𝕜 𝕜' x∥ ≤ ∥x∥ := (op_norm_le_bound _ (norm_nonneg x) (norm_mul_le x)) /-- Left multiplication in a normed algebra as a linear isometry to the space of continuous linear maps. -/ def lmulₗᵢ [norm_one_class 𝕜'] : 𝕜' →ₗᵢ[𝕜] 𝕜' →L[𝕜] 𝕜' := { to_linear_map := lmul 𝕜 𝕜', norm_map' := λ x, le_antisymm (op_norm_lmul_apply_le _ _ _) (by { convert ratio_le_op_norm _ (1 : 𝕜'), simp [norm_one], apply_instance }) } @[simp] lemma coe_lmulₗᵢ [norm_one_class 𝕜'] : ⇑(lmulₗᵢ 𝕜 𝕜') = lmul 𝕜 𝕜' := rfl @[simp] lemma op_norm_lmul_apply [norm_one_class 𝕜'] (x : 𝕜') : ∥lmul 𝕜 𝕜' x∥ = ∥x∥ := (lmulₗᵢ 𝕜 𝕜').norm_map x /-- Right-multiplication in a normed algebra, considered as a continuous linear map. -/ def lmul_right : 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' := (lmul 𝕜 𝕜').flip @[simp] lemma lmul_right_apply (x y : 𝕜') : lmul_right 𝕜 𝕜' x y = y * x := rfl @[simp] lemma op_norm_lmul_right_apply_le (x : 𝕜') : ∥lmul_right 𝕜 𝕜' x∥ ≤ ∥x∥ := op_norm_le_bound _ (norm_nonneg x) (λ y, (norm_mul_le y x).trans_eq (mul_comm _ _)) @[simp] lemma op_norm_lmul_right_apply [norm_one_class 𝕜'] (x : 𝕜') : ∥lmul_right 𝕜 𝕜' x∥ = ∥x∥ := le_antisymm (op_norm_lmul_right_apply_le _ _ _) (by { convert ratio_le_op_norm _ (1 : 𝕜'), simp [norm_one], apply_instance }) /-- Right-multiplication in a normed algebra, considered as a linear isometry to the space of continuous linear maps. -/ def lmul_rightₗᵢ [norm_one_class 𝕜'] : 𝕜' →ₗᵢ[𝕜] 𝕜' →L[𝕜] 𝕜' := { to_linear_map := lmul_right 𝕜 𝕜', norm_map' := op_norm_lmul_right_apply 𝕜 𝕜' } @[simp] lemma coe_lmul_rightₗᵢ [norm_one_class 𝕜'] : ⇑(lmul_rightₗᵢ 𝕜 𝕜') = lmul_right 𝕜 𝕜' := rfl /-- Simultaneous left- and right-multiplication in a normed algebra, considered as a continuous trilinear map. -/ def lmul_left_right : 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' →L[𝕜] 𝕜' := ((compL 𝕜 𝕜' 𝕜' 𝕜').comp (lmul_right 𝕜 𝕜')).flip.comp (lmul 𝕜 𝕜') @[simp] lemma lmul_left_right_apply (x y z : 𝕜') : lmul_left_right 𝕜 𝕜' x y z = x * z * y := rfl lemma op_norm_lmul_left_right_apply_apply_le (x y : 𝕜') : ∥lmul_left_right 𝕜 𝕜' x y∥ ≤ ∥x∥ * ∥y∥ := (op_norm_comp_le _ _).trans $ (mul_comm _ _).trans_le $ mul_le_mul (op_norm_lmul_apply_le _ _ _) (op_norm_lmul_right_apply_le _ _ _) (norm_nonneg _) (norm_nonneg _) lemma op_norm_lmul_left_right_apply_le (x : 𝕜') : ∥lmul_left_right 𝕜 𝕜' x∥ ≤ ∥x∥ := op_norm_le_bound _ (norm_nonneg x) (op_norm_lmul_left_right_apply_apply_le 𝕜 𝕜' x) lemma op_norm_lmul_left_right_le : ∥lmul_left_right 𝕜 𝕜'∥ ≤ 1 := op_norm_le_bound _ zero_le_one (λ x, (one_mul ∥x∥).symm ▸ op_norm_lmul_left_right_apply_le 𝕜 𝕜' x) end multiplication_linear section smul_linear variables (𝕜) (𝕜' : Type*) [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] /-- Scalar multiplication as a continuous bilinear map. -/ def lsmul : 𝕜' →L[𝕜] E →L[𝕜] E := ((algebra.lsmul 𝕜 E).to_linear_map : 𝕜' →ₗ[𝕜] E →ₗ[𝕜] E).mk_continuous₂ 1 $ λ c x, by simpa only [one_mul] using (norm_smul c x).le @[simp] lemma lsmul_apply (c : 𝕜') (x : E) : lsmul 𝕜 𝕜' c x = c • x := rfl variables {𝕜'} lemma norm_to_span_singleton (x : E) : ∥to_span_singleton 𝕜 x∥ = ∥x∥ := begin refine op_norm_eq_of_bounds (norm_nonneg _) (λ x, _) (λ N hN_nonneg h, _), { rw [to_span_singleton_apply, norm_smul, mul_comm], }, { specialize h 1, rw [to_span_singleton_apply, norm_smul, mul_comm] at h, exact (mul_le_mul_right (by simp)).mp h, }, end variables {𝕜} lemma op_norm_lsmul_apply_le (x : 𝕜') : ∥(lsmul 𝕜 𝕜' x : E →L[𝕜] E)∥ ≤ ∥x∥ := continuous_linear_map.op_norm_le_bound _ (norm_nonneg x) $ λ y, (norm_smul x y).le /-- The norm of `lsmul` is at most 1 in any semi-normed group. -/ lemma op_norm_lsmul_le : ∥(lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E)∥ ≤ 1 := begin refine continuous_linear_map.op_norm_le_bound _ zero_le_one (λ x, _), simp_rw [one_mul], exact op_norm_lsmul_apply_le _, end end smul_linear section restrict_scalars variables {𝕜' : Type*} [nondiscrete_normed_field 𝕜'] [normed_algebra 𝕜' 𝕜] variables [normed_space 𝕜' E] [is_scalar_tower 𝕜' 𝕜 E] variables [normed_space 𝕜' Fₗ] [is_scalar_tower 𝕜' 𝕜 Fₗ] @[simp] lemma norm_restrict_scalars (f : E →L[𝕜] Fₗ) : ∥f.restrict_scalars 𝕜'∥ = ∥f∥ := le_antisymm (op_norm_le_bound _ (norm_nonneg _) $ λ x, f.le_op_norm x) (op_norm_le_bound _ (norm_nonneg _) $ λ x, f.le_op_norm x) variables (𝕜 E Fₗ 𝕜') (𝕜'' : Type*) [ring 𝕜''] [module 𝕜'' Fₗ] [has_continuous_const_smul 𝕜'' Fₗ] [smul_comm_class 𝕜 𝕜'' Fₗ] [smul_comm_class 𝕜' 𝕜'' Fₗ] /-- `continuous_linear_map.restrict_scalars` as a `linear_isometry`. -/ def restrict_scalars_isometry : (E →L[𝕜] Fₗ) →ₗᵢ[𝕜''] (E →L[𝕜'] Fₗ) := ⟨restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'', norm_restrict_scalars⟩ variables {𝕜 E Fₗ 𝕜' 𝕜''} @[simp] lemma coe_restrict_scalars_isometry : ⇑(restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'') = restrict_scalars 𝕜' := rfl @[simp] lemma restrict_scalars_isometry_to_linear_map : (restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'').to_linear_map = restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'' := rfl variables (𝕜 E Fₗ 𝕜' 𝕜'') /-- `continuous_linear_map.restrict_scalars` as a `continuous_linear_map`. -/ def restrict_scalarsL : (E →L[𝕜] Fₗ) →L[𝕜''] (E →L[𝕜'] Fₗ) := (restrict_scalars_isometry 𝕜 E Fₗ 𝕜' 𝕜'').to_continuous_linear_map variables {𝕜 E Fₗ 𝕜' 𝕜''} @[simp] lemma coe_restrict_scalarsL : (restrict_scalarsL 𝕜 E Fₗ 𝕜' 𝕜'' : (E →L[𝕜] Fₗ) →ₗ[𝕜''] (E →L[𝕜'] Fₗ)) = restrict_scalarsₗ 𝕜 E Fₗ 𝕜' 𝕜'' := rfl @[simp] lemma coe_restrict_scalarsL' : ⇑(restrict_scalarsL 𝕜 E Fₗ 𝕜' 𝕜'') = restrict_scalars 𝕜' := rfl end restrict_scalars end continuous_linear_map namespace submodule lemma norm_subtypeL_le (K : submodule 𝕜 E) : ∥K.subtypeL∥ ≤ 1 := K.subtypeₗᵢ.norm_to_continuous_linear_map_le end submodule section has_sum -- Results in this section hold for continuous additive monoid homomorphisms or equivalences but we -- don't have bundled continuous additive homomorphisms. variables {ι R R₂ M M₂ : Type*} [semiring R] [semiring R₂] [add_comm_monoid M] [module R M] [add_comm_monoid M₂] [module R₂ M₂] [topological_space M] [topological_space M₂] {σ : R →+* R₂} {σ' : R₂ →+* R} [ring_hom_inv_pair σ σ'] [ring_hom_inv_pair σ' σ] /-- Applying a continuous linear map commutes with taking an (infinite) sum. -/ protected lemma continuous_linear_map.has_sum {f : ι → M} (φ : M →SL[σ] M₂) {x : M} (hf : has_sum f x) : has_sum (λ (b:ι), φ (f b)) (φ x) := by simpa only using hf.map φ.to_linear_map.to_add_monoid_hom φ.continuous alias continuous_linear_map.has_sum ← has_sum.mapL protected lemma continuous_linear_map.summable {f : ι → M} (φ : M →SL[σ] M₂) (hf : summable f) : summable (λ b:ι, φ (f b)) := (hf.has_sum.mapL φ).summable alias continuous_linear_map.summable ← summable.mapL protected lemma continuous_linear_map.map_tsum [t2_space M₂] {f : ι → M} (φ : M →SL[σ] M₂) (hf : summable f) : φ (∑' z, f z) = ∑' z, φ (f z) := (hf.has_sum.mapL φ).tsum_eq.symm include σ' /-- Applying a continuous linear map commutes with taking an (infinite) sum. -/ protected lemma continuous_linear_equiv.has_sum {f : ι → M} (e : M ≃SL[σ] M₂) {y : M₂} : has_sum (λ (b:ι), e (f b)) y ↔ has_sum f (e.symm y) := ⟨λ h, by simpa only [e.symm.coe_coe, e.symm_apply_apply] using h.mapL (e.symm : M₂ →SL[σ'] M), λ h, by simpa only [e.coe_coe, e.apply_symm_apply] using (e : M →SL[σ] M₂).has_sum h⟩ protected lemma continuous_linear_equiv.summable {f : ι → M} (e : M ≃SL[σ] M₂) : summable (λ b:ι, e (f b)) ↔ summable f := ⟨λ hf, (e.has_sum.1 hf.has_sum).summable, (e : M →SL[σ] M₂).summable⟩ lemma continuous_linear_equiv.tsum_eq_iff [t2_space M] [t2_space M₂] {f : ι → M} (e : M ≃SL[σ] M₂) {y : M₂} : ∑' z, e (f z) = y ↔ ∑' z, f z = e.symm y := begin by_cases hf : summable f, { exact ⟨λ h, (e.has_sum.mp ((e.summable.mpr hf).has_sum_iff.mpr h)).tsum_eq, λ h, (e.has_sum.mpr (hf.has_sum_iff.mpr h)).tsum_eq⟩ }, { have hf' : ¬summable (λ z, e (f z)) := λ h, hf (e.summable.mp h), rw [tsum_eq_zero_of_not_summable hf, tsum_eq_zero_of_not_summable hf'], exact ⟨by { rintro rfl, simp }, λ H, by simpa using (congr_arg (λ z, e z) H)⟩ } end protected lemma continuous_linear_equiv.map_tsum [t2_space M] [t2_space M₂] {f : ι → M} (e : M ≃SL[σ] M₂) : e (∑' z, f z) = ∑' z, e (f z) := by { refine symm (e.tsum_eq_iff.mpr _), rw e.symm_apply_apply _ } end has_sum namespace continuous_linear_equiv section variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] [ring_hom_isometric σ₁₂] variables (e : E ≃SL[σ₁₂] F) include σ₂₁ protected lemma lipschitz : lipschitz_with (∥(e : E →SL[σ₁₂] F)∥₊) e := (e : E →SL[σ₁₂] F).lipschitz theorem is_O_comp {α : Type*} (f : α → E) (l : filter α) : (λ x', e (f x')) =O[l] f := (e : E →SL[σ₁₂] F).is_O_comp f l theorem is_O_sub (l : filter E) (x : E) : (λ x', e (x' - x)) =O[l] (λ x', x' - x) := (e : E →SL[σ₁₂] F).is_O_sub l x end variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ lemma homothety_inverse (a : ℝ) (ha : 0 < a) (f : E ≃ₛₗ[σ₁₂] F) : (∀ (x : E), ∥f x∥ = a * ∥x∥) → (∀ (y : F), ∥f.symm y∥ = a⁻¹ * ∥y∥) := begin intros hf y, calc ∥(f.symm) y∥ = a⁻¹ * (a * ∥ (f.symm) y∥) : _ ... = a⁻¹ * ∥f ((f.symm) y)∥ : by rw hf ... = a⁻¹ * ∥y∥ : by simp, rw [← mul_assoc, inv_mul_cancel (ne_of_lt ha).symm, one_mul], end /-- A linear equivalence which is a homothety is a continuous linear equivalence. -/ def of_homothety (f : E ≃ₛₗ[σ₁₂] F) (a : ℝ) (ha : 0 < a) (hf : ∀x, ∥f x∥ = a * ∥x∥) : E ≃SL[σ₁₂] F := { to_linear_equiv := f, continuous_to_fun := add_monoid_hom_class.continuous_of_bound f a (λ x, le_of_eq (hf x)), continuous_inv_fun := add_monoid_hom_class.continuous_of_bound f.symm a⁻¹ (λ x, le_of_eq (homothety_inverse a ha f hf x)) } variables [ring_hom_isometric σ₂₁] (e : E ≃SL[σ₁₂] F) theorem is_O_comp_rev {α : Type*} (f : α → E) (l : filter α) : f =O[l] (λ x', e (f x')) := (e.symm.is_O_comp _ l).congr_left $ λ _, e.symm_apply_apply _ theorem is_O_sub_rev (l : filter E) (x : E) : (λ x', x' - x) =O[l] (λ x', e (x' - x)) := e.is_O_comp_rev _ _ omit σ₂₁ variable (𝕜) lemma to_span_nonzero_singleton_homothety (x : E) (h : x ≠ 0) (c : 𝕜) : ∥linear_equiv.to_span_nonzero_singleton 𝕜 E x h c∥ = ∥x∥ * ∥c∥ := continuous_linear_map.to_span_singleton_homothety _ _ _ end continuous_linear_equiv variables {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] include σ₂₁ /-- Construct a continuous linear equivalence from a linear equivalence together with bounds in both directions. -/ def linear_equiv.to_continuous_linear_equiv_of_bounds (e : E ≃ₛₗ[σ₁₂] F) (C_to C_inv : ℝ) (h_to : ∀ x, ∥e x∥ ≤ C_to * ∥x∥) (h_inv : ∀ x : F, ∥e.symm x∥ ≤ C_inv * ∥x∥) : E ≃SL[σ₁₂] F := { to_linear_equiv := e, continuous_to_fun := add_monoid_hom_class.continuous_of_bound e C_to h_to, continuous_inv_fun := add_monoid_hom_class.continuous_of_bound e.symm C_inv h_inv } omit σ₂₁ namespace continuous_linear_map variables {E' F' : Type*} [semi_normed_group E'] [semi_normed_group F'] variables {𝕜₁' : Type*} {𝕜₂' : Type*} [nondiscrete_normed_field 𝕜₁'] [nondiscrete_normed_field 𝕜₂'] [normed_space 𝕜₁' E'] [normed_space 𝕜₂' F'] {σ₁' : 𝕜₁' →+* 𝕜} {σ₁₃' : 𝕜₁' →+* 𝕜₃} {σ₂' : 𝕜₂' →+* 𝕜₂} {σ₂₃' : 𝕜₂' →+* 𝕜₃} [ring_hom_comp_triple σ₁' σ₁₃ σ₁₃'] [ring_hom_comp_triple σ₂' σ₂₃ σ₂₃'] [ring_hom_isometric σ₂₃] [ring_hom_isometric σ₁₃'] [ring_hom_isometric σ₂₃'] /-- Compose a bilinear map `E →SL[σ₁₃] F →SL[σ₂₃] G` with two linear maps `E' →SL[σ₁'] E` and `F' →SL[σ₂'] F`. -/ def bilinear_comp (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (gE : E' →SL[σ₁'] E) (gF : F' →SL[σ₂'] F) : E' →SL[σ₁₃'] F' →SL[σ₂₃'] G := ((f.comp gE).flip.comp gF).flip include σ₁₃' σ₂₃' @[simp] lemma bilinear_comp_apply (f : E →SL[σ₁₃] F →SL[σ₂₃] G) (gE : E' →SL[σ₁'] E) (gF : F' →SL[σ₂'] F) (x : E') (y : F') : f.bilinear_comp gE gF x y = f (gE x) (gF y) := rfl omit σ₁₃' σ₂₃' variables [ring_hom_isometric σ₁₃] [ring_hom_isometric σ₁'] [ring_hom_isometric σ₂'] /-- Derivative of a continuous bilinear map `f : E →L[𝕜] F →L[𝕜] G` interpreted as a map `E × F → G` at point `p : E × F` evaluated at `q : E × F`, as a continuous bilinear map. -/ def deriv₂ (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) : (E × Fₗ) →L[𝕜] (E × Fₗ) →L[𝕜] Gₗ := f.bilinear_comp (fst _ _ _) (snd _ _ _) + f.flip.bilinear_comp (snd _ _ _) (fst _ _ _) @[simp] lemma coe_deriv₂ (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) (p : E × Fₗ) : ⇑(f.deriv₂ p) = λ q : E × Fₗ, f p.1 q.2 + f q.1 p.2 := rfl lemma map_add_add (f : E →L[𝕜] Fₗ →L[𝕜] Gₗ) (x x' : E) (y y' : Fₗ) : f (x + x') (y + y') = f x y + f.deriv₂ (x, y) (x', y') + f x' y' := by simp only [map_add, add_apply, coe_deriv₂, add_assoc] end continuous_linear_map end semi_normed section normed variables [normed_group E] [normed_group F] [normed_group G] [normed_group Fₗ] open metric continuous_linear_map section variables [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [nondiscrete_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] [normed_space 𝕜₃ G] [normed_space 𝕜 Fₗ] (c : 𝕜) {σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃} (f g : E →SL[σ₁₂] F) (x y z : E) lemma linear_map.bound_of_shell [ring_hom_isometric σ₁₂] (f : E →ₛₗ[σ₁₂] F) {ε C : ℝ} (ε_pos : 0 < ε) {c : 𝕜} (hc : 1 < ∥c∥) (hf : ∀ x, ε / ∥c∥ ≤ ∥x∥ → ∥x∥ < ε → ∥f x∥ ≤ C * ∥x∥) (x : E) : ∥f x∥ ≤ C * ∥x∥ := begin by_cases hx : x = 0, { simp [hx] }, exact semilinear_map_class.bound_of_shell_semi_normed f ε_pos hc hf (ne_of_lt (norm_pos_iff.2 hx)).symm end /-- `linear_map.bound_of_ball_bound'` is a version of this lemma over a field satisfying `is_R_or_C` that produces a concrete bound. -/ lemma linear_map.bound_of_ball_bound {r : ℝ} (r_pos : 0 < r) (c : ℝ) (f : E →ₗ[𝕜] Fₗ) (h : ∀ z ∈ metric.ball (0 : E) r, ∥f z∥ ≤ c) : ∃ C, ∀ (z : E), ∥f z∥ ≤ C * ∥z∥ := begin cases @nondiscrete_normed_field.non_trivial 𝕜 _ with k hk, use c * (∥k∥ / r), intro z, refine linear_map.bound_of_shell _ r_pos hk (λ x hko hxo, _) _, calc ∥f x∥ ≤ c : h _ (mem_ball_zero_iff.mpr hxo) ... ≤ c * ((∥x∥ * ∥k∥) / r) : le_mul_of_one_le_right _ _ ... = _ : by ring, { exact le_trans (norm_nonneg _) (h 0 (by simp [r_pos])) }, { rw [div_le_iff (zero_lt_one.trans hk)] at hko, exact (one_le_div r_pos).mpr hko } end namespace continuous_linear_map section op_norm open set real /-- An operator is zero iff its norm vanishes. -/ theorem op_norm_zero_iff [ring_hom_isometric σ₁₂] : ∥f∥ = 0 ↔ f = 0 := iff.intro (λ hn, continuous_linear_map.ext (λ x, norm_le_zero_iff.1 (calc _ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... = _ : by rw [hn, zero_mul]))) (λ hf, le_antisymm (cInf_le bounds_bdd_below ⟨le_rfl, λ _, le_of_eq (by { rw [zero_mul, hf], exact norm_zero })⟩) (op_norm_nonneg _)) /-- If a normed space is non-trivial, then the norm of the identity equals `1`. -/ @[simp] lemma norm_id [nontrivial E] : ∥id 𝕜 E∥ = 1 := begin refine norm_id_of_nontrivial_seminorm _, obtain ⟨x, hx⟩ := exists_ne (0 : E), exact ⟨x, ne_of_gt (norm_pos_iff.2 hx)⟩, end instance norm_one_class [nontrivial E] : norm_one_class (E →L[𝕜] E) := ⟨norm_id⟩ /-- Continuous linear maps themselves form a normed space with respect to the operator norm. -/ instance to_normed_group [ring_hom_isometric σ₁₂] : normed_group (E →SL[σ₁₂] F) := normed_group.of_core _ ⟨λ f, op_norm_zero_iff f, op_norm_add_le, op_norm_neg⟩ /-- Continuous linear maps form a normed ring with respect to the operator norm. -/ instance to_normed_ring : normed_ring (E →L[𝕜] E) := { .. continuous_linear_map.to_normed_group, .. continuous_linear_map.to_semi_normed_ring } variable {f} lemma homothety_norm [ring_hom_isometric σ₁₂] [nontrivial E] (f : E →SL[σ₁₂] F) {a : ℝ} (hf : ∀x, ∥f x∥ = a * ∥x∥) : ∥f∥ = a := begin obtain ⟨x, hx⟩ : ∃ (x : E), x ≠ 0 := exists_ne 0, rw ← norm_pos_iff at hx, have ha : 0 ≤ a, by simpa only [hf, hx, zero_le_mul_right] using norm_nonneg (f x), apply le_antisymm (f.op_norm_le_bound ha (λ y, le_of_eq (hf y))), simpa only [hf, hx, mul_le_mul_right] using f.le_op_norm x, end lemma to_span_singleton_norm (x : E) : ∥to_span_singleton 𝕜 x∥ = ∥x∥ := homothety_norm _ (to_span_singleton_homothety 𝕜 x) variable (f) theorem uniform_embedding_of_bound {K : ℝ≥0} (hf : ∀ x, ∥x∥ ≤ K * ∥f x∥) : uniform_embedding f := (add_monoid_hom_class.antilipschitz_of_bound f hf).uniform_embedding f.uniform_continuous /-- If a continuous linear map is a uniform embedding, then it is expands the distances by a positive factor.-/ theorem antilipschitz_of_uniform_embedding (f : E →L[𝕜] Fₗ) (hf : uniform_embedding f) : ∃ K, antilipschitz_with K f := begin obtain ⟨ε, εpos, hε⟩ : ∃ (ε : ℝ) (H : ε > 0), ∀ {x y : E}, dist (f x) (f y) < ε → dist x y < 1, from (uniform_embedding_iff.1 hf).2.2 1 zero_lt_one, let δ := ε/2, have δ_pos : δ > 0 := half_pos εpos, have H : ∀{x}, ∥f x∥ ≤ δ → ∥x∥ ≤ 1, { assume x hx, have : dist x 0 ≤ 1, { refine (hε _).le, rw [f.map_zero, dist_zero_right], exact hx.trans_lt (half_lt_self εpos) }, simpa using this }, rcases normed_field.exists_one_lt_norm 𝕜 with ⟨c, hc⟩, refine ⟨⟨δ⁻¹, _⟩ * ∥c∥₊, add_monoid_hom_class.antilipschitz_of_bound f $ λx, _⟩, exact inv_nonneg.2 (le_of_lt δ_pos), by_cases hx : f x = 0, { have : f x = f 0, by { simp [hx] }, have : x = 0 := (uniform_embedding_iff.1 hf).1 this, simp [this] }, { rcases rescale_to_shell hc δ_pos hx with ⟨d, hd, dxlt, ledx, dinv⟩, rw [← f.map_smul d] at dxlt, have : ∥d • x∥ ≤ 1 := H dxlt.le, calc ∥x∥ = ∥d∥⁻¹ * ∥d • x∥ : by rwa [← norm_inv, ← norm_smul, ← mul_smul, inv_mul_cancel, one_smul] ... ≤ ∥d∥⁻¹ * 1 : mul_le_mul_of_nonneg_left this (inv_nonneg.2 (norm_nonneg _)) ... ≤ δ⁻¹ * ∥c∥ * ∥f x∥ : by rwa [mul_one] } end section completeness open_locale topological_space open filter variables {E' : Type*} [semi_normed_group E'] [normed_space 𝕜 E'] [ring_hom_isometric σ₁₂] /-- Construct a bundled continuous (semi)linear map from a map `f : E → F` and a proof of the fact that it belongs to the closure of the image of a bounded set `s : set (E →SL[σ₁₂] F)` under coercion to function. Coercion to function of the result is definitionally equal to `f`. -/ @[simps apply { fully_applied := ff }] def of_mem_closure_image_coe_bounded (f : E' → F) {s : set (E' →SL[σ₁₂] F)} (hs : bounded s) (hf : f ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) : E' →SL[σ₁₂] F := begin -- `f` is a linear map due to `linear_map_of_mem_closure_range_coe` refine (linear_map_of_mem_closure_range_coe f _).mk_continuous_of_exists_bound _, { refine closure_mono (image_subset_iff.2 $ λ g hg, _) hf, exact ⟨g, rfl⟩ }, { -- We need to show that `f` has bounded norm. Choose `C` such that `∥g∥ ≤ C` for all `g ∈ s`. rcases bounded_iff_forall_norm_le.1 hs with ⟨C, hC⟩, -- Then `∥g x∥ ≤ C * ∥x∥` for all `g ∈ s`, `x : E`, hence `∥f x∥ ≤ C * ∥x∥` for all `x`. have : ∀ x, is_closed {g : E' → F | ∥g x∥ ≤ C * ∥x∥}, from λ x, is_closed_Iic.preimage (@continuous_apply E' (λ _, F) _ x).norm, refine ⟨C, λ x, (this x).closure_subset_iff.2 (image_subset_iff.2 $ λ g hg, _) hf⟩, exact g.le_of_op_norm_le (hC _ hg) _ } end /-- Let `f : E → F` be a map, let `g : α → E →SL[σ₁₂] F` be a family of continuous (semi)linear maps that takes values in a bounded set and converges to `f` pointwise along a nontrivial filter. Then `f` is a continuous (semi)linear map. -/ @[simps apply { fully_applied := ff }] def of_tendsto_of_bounded_range {α : Type*} {l : filter α} [l.ne_bot] (f : E' → F) (g : α → E' →SL[σ₁₂] F) (hf : tendsto (λ a x, g a x) l (𝓝 f)) (hg : bounded (set.range g)) : E' →SL[σ₁₂] F := of_mem_closure_image_coe_bounded f hg $ mem_closure_of_tendsto hf $ eventually_of_forall $ λ a, mem_image_of_mem _ $ set.mem_range_self _ /-- If a Cauchy sequence of continuous linear map converges to a continuous linear map pointwise, then it converges to the same map in norm. This lemma is used to prove that the space of continuous linear maps is complete provided that the codomain is a complete space. -/ lemma tendsto_of_tendsto_pointwise_of_cauchy_seq {f : ℕ → E' →SL[σ₁₂] F} {g : E' →SL[σ₁₂] F} (hg : tendsto (λ n x, f n x) at_top (𝓝 g)) (hf : cauchy_seq f) : tendsto f at_top (𝓝 g) := begin /- Since `f` is a Cauchy sequence, there exists `b → 0` such that `∥f n - f m∥ ≤ b N` for any `m, n ≥ N`. -/ rcases cauchy_seq_iff_le_tendsto_0.1 hf with ⟨b, hb₀, hfb, hb_lim⟩, -- Since `b → 0`, it suffices to show that `∥f n x - g x∥ ≤ b n * ∥x∥` for all `n` and `x`. suffices : ∀ n x, ∥f n x - g x∥ ≤ b n * ∥x∥, from tendsto_iff_norm_tendsto_zero.2 (squeeze_zero (λ n, norm_nonneg _) (λ n, op_norm_le_bound _ (hb₀ n) (this n)) hb_lim), intros n x, -- Note that `f m x → g x`, hence `∥f n x - f m x∥ → ∥f n x - g x∥` as `m → ∞` have : tendsto (λ m, ∥f n x - f m x∥) at_top (𝓝 (∥f n x - g x∥)), from (tendsto_const_nhds.sub $ tendsto_pi_nhds.1 hg _).norm, -- Thus it suffices to verify `∥f n x - f m x∥ ≤ b n * ∥x∥` for `m ≥ n`. refine le_of_tendsto this (eventually_at_top.2 ⟨n, λ m hm, _⟩), -- This inequality follows from `∥f n - f m∥ ≤ b n`. exact (f n - f m).le_of_op_norm_le (hfb _ _ _ le_rfl hm) _ end /-- If the target space is complete, the space of continuous linear maps with its norm is also complete. This works also if the source space is seminormed. -/ instance [complete_space F] : complete_space (E' →SL[σ₁₂] F) := begin -- We show that every Cauchy sequence converges. refine metric.complete_of_cauchy_seq_tendsto (λ f hf, _), -- The evaluation at any point `v : E` is Cauchy. have cau : ∀ v, cauchy_seq (λ n, f n v), from λ v, hf.map (lipschitz_apply v).uniform_continuous, -- We assemble the limits points of those Cauchy sequences -- (which exist as `F` is complete) -- into a function which we call `G`. choose G hG using λv, cauchy_seq_tendsto_of_complete (cau v), -- Next, we show that this `G` is a continuous linear map. -- This is done in `continuous_linear_map.of_tendsto_of_bounded_range`. set Glin : E' →SL[σ₁₂] F := of_tendsto_of_bounded_range _ _ (tendsto_pi_nhds.mpr hG) hf.bounded_range, -- Finally, `f n` converges to `Glin` in norm because of -- `continuous_linear_map.tendsto_of_tendsto_pointwise_of_cauchy_seq` exact ⟨Glin, tendsto_of_tendsto_pointwise_of_cauchy_seq (tendsto_pi_nhds.2 hG) hf⟩ end /-- Let `s` be a bounded set in the space of continuous (semi)linear maps `E →SL[σ] F` taking values in a proper space. Then `s` interpreted as a set in the space of maps `E → F` with topology of pointwise convergence is precompact: its closure is a compact set. -/ lemma is_compact_closure_image_coe_of_bounded [proper_space F] {s : set (E' →SL[σ₁₂] F)} (hb : bounded s) : is_compact (closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) := have ∀ x, is_compact (closure (apply' F σ₁₂ x '' s)), from λ x, ((apply' F σ₁₂ x).lipschitz.bounded_image hb).is_compact_closure, compact_closure_of_subset_compact (is_compact_pi_infinite this) (image_subset_iff.2 $ λ g hg x, subset_closure $ mem_image_of_mem _ hg) /-- Let `s` be a bounded set in the space of continuous (semi)linear maps `E →SL[σ] F` taking values in a proper space. If `s` interpreted as a set in the space of maps `E → F` with topology of pointwise convergence is closed, then it is compact. TODO: reformulate this in terms of a type synonym with the right topology. -/ lemma is_compact_image_coe_of_bounded_of_closed_image [proper_space F] {s : set (E' →SL[σ₁₂] F)} (hb : bounded s) (hc : is_closed ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s)) : is_compact ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) := hc.closure_eq ▸ is_compact_closure_image_coe_of_bounded hb /-- If a set `s` of semilinear functions is bounded and is closed in the weak-* topology, then its image under coercion to functions `E → F` is a closed set. We don't have a name for `E →SL[σ] F` with weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`). TODO: reformulate this in terms of a type synonym with the right topology. -/ lemma is_closed_image_coe_of_bounded_of_weak_closed {s : set (E' →SL[σ₁₂] F)} (hb : bounded s) (hc : ∀ f, (⇑f : E' → F) ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) → f ∈ s) : is_closed ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) := is_closed_of_closure_subset $ λ f hf, ⟨of_mem_closure_image_coe_bounded f hb hf, hc (of_mem_closure_image_coe_bounded f hb hf) hf, rfl⟩ /-- If a set `s` of semilinear functions is bounded and is closed in the weak-* topology, then its image under coercion to functions `E → F` is a compact set. We don't have a name for `E →SL[σ] F` with weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`). -/ lemma is_compact_image_coe_of_bounded_of_weak_closed [proper_space F] {s : set (E' →SL[σ₁₂] F)} (hb : bounded s) (hc : ∀ f, (⇑f : E' → F) ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) → f ∈ s) : is_compact ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' s) := is_compact_image_coe_of_bounded_of_closed_image hb $ is_closed_image_coe_of_bounded_of_weak_closed hb hc /-- A closed ball is closed in the weak-* topology. We don't have a name for `E →SL[σ] F` with weak-* topology in `mathlib`, so we use an equivalent condition (see `is_closed_induced_iff'`). -/ lemma is_weak_closed_closed_ball (f₀ : E' →SL[σ₁₂] F) (r : ℝ) ⦃f : E' →SL[σ₁₂] F⦄ (hf : ⇑f ∈ closure ((coe_fn : (E' →SL[σ₁₂] F) → E' → F) '' (closed_ball f₀ r))) : f ∈ closed_ball f₀ r := begin have hr : 0 ≤ r, from nonempty_closed_ball.1 (nonempty_image_iff.1 (closure_nonempty_iff.1 ⟨_, hf⟩)), refine mem_closed_ball_iff_norm.2 (op_norm_le_bound _ hr $ λ x, _), have : is_closed {g : E' → F | ∥g x - f₀ x∥ ≤ r * ∥x∥}, from is_closed_Iic.preimage ((@continuous_apply E' (λ _, F) _ x).sub continuous_const).norm, refine this.closure_subset_iff.2 (image_subset_iff.2 $ λ g hg, _) hf, exact (g - f₀).le_of_op_norm_le (mem_closed_ball_iff_norm.1 hg) _ end /-- The set of functions `f : E → F` that represent continuous linear maps `f : E →SL[σ₁₂] F` at distance `≤ r` from `f₀ : E →SL[σ₁₂] F` is closed in the topology of pointwise convergence. This is one of the key steps in the proof of the **Banach-Alaoglu** theorem. -/ lemma is_closed_image_coe_closed_ball (f₀ : E →SL[σ₁₂] F) (r : ℝ) : is_closed ((coe_fn : (E →SL[σ₁₂] F) → E → F) '' closed_ball f₀ r) := is_closed_image_coe_of_bounded_of_weak_closed bounded_closed_ball (is_weak_closed_closed_ball f₀ r) /-- **Banach-Alaoglu** theorem. The set of functions `f : E → F` that represent continuous linear maps `f : E →SL[σ₁₂] F` at distance `≤ r` from `f₀ : E →SL[σ₁₂] F` is compact in the topology of pointwise convergence. Other versions of this theorem can be found in `analysis.normed_space.weak_dual`. -/ lemma is_compact_image_coe_closed_ball [proper_space F] (f₀ : E →SL[σ₁₂] F) (r : ℝ) : is_compact ((coe_fn : (E →SL[σ₁₂] F) → E → F) '' closed_ball f₀ r) := is_compact_image_coe_of_bounded_of_weak_closed bounded_closed_ball $ is_weak_closed_closed_ball f₀ r end completeness section uniformly_extend variables [complete_space F] (e : E →L[𝕜] Fₗ) (h_dense : dense_range e) section variables (h_e : uniform_inducing e) /-- Extension of a continuous linear map `f : E →SL[σ₁₂] F`, with `E` a normed space and `F` a complete normed space, along a uniform and dense embedding `e : E →L[𝕜] Fₗ`. -/ def extend : Fₗ →SL[σ₁₂] F := /- extension of `f` is continuous -/ have cont : _ := (uniform_continuous_uniformly_extend h_e h_dense f.uniform_continuous).continuous, /- extension of `f` agrees with `f` on the domain of the embedding `e` -/ have eq : _ := uniformly_extend_of_ind h_e h_dense f.uniform_continuous, { to_fun := (h_e.dense_inducing h_dense).extend f, map_add' := begin refine h_dense.induction_on₂ _ _, { exact is_closed_eq (cont.comp continuous_add) ((cont.comp continuous_fst).add (cont.comp continuous_snd)) }, { assume x y, simp only [eq, ← e.map_add], exact f.map_add _ _ }, end, map_smul' := λk, begin refine (λ b, h_dense.induction_on b _ _), { exact is_closed_eq (cont.comp (continuous_const_smul _)) ((continuous_const_smul _).comp cont) }, { assume x, rw ← map_smul, simp only [eq], exact continuous_linear_map.map_smulₛₗ _ _ _ }, end, cont := cont } lemma extend_unique (g : Fₗ →SL[σ₁₂] F) (H : g.comp e = f) : extend f e h_dense h_e = g := continuous_linear_map.coe_fn_injective $ uniformly_extend_unique h_e h_dense (continuous_linear_map.ext_iff.1 H) g.continuous @[simp] lemma extend_zero : extend (0 : E →SL[σ₁₂] F) e h_dense h_e = 0 := extend_unique _ _ _ _ _ (zero_comp _) end section variables {N : ℝ≥0} (h_e : ∀x, ∥x∥ ≤ N * ∥e x∥) [ring_hom_isometric σ₁₂] local notation `ψ` := f.extend e h_dense (uniform_embedding_of_bound _ h_e).to_uniform_inducing /-- If a dense embedding `e : E →L[𝕜] G` expands the norm by a constant factor `N⁻¹`, then the norm of the extension of `f` along `e` is bounded by `N * ∥f∥`. -/ lemma op_norm_extend_le : ∥ψ∥ ≤ N * ∥f∥ := begin have uni : uniform_inducing e := (uniform_embedding_of_bound _ h_e).to_uniform_inducing, have eq : ∀x, ψ (e x) = f x := uniformly_extend_of_ind uni h_dense f.uniform_continuous, by_cases N0 : 0 ≤ N, { refine op_norm_le_bound ψ _ (is_closed_property h_dense (is_closed_le _ _) _), { exact mul_nonneg N0 (norm_nonneg _) }, { exact continuous_norm.comp (cont ψ) }, { exact continuous_const.mul continuous_norm }, { assume x, rw eq, calc ∥f x∥ ≤ ∥f∥ * ∥x∥ : le_op_norm _ _ ... ≤ ∥f∥ * (N * ∥e x∥) : mul_le_mul_of_nonneg_left (h_e x) (norm_nonneg _) ... ≤ N * ∥f∥ * ∥e x∥ : by rw [mul_comm ↑N ∥f∥, mul_assoc] } }, { have he : ∀ x : E, x = 0, { assume x, have N0 : N ≤ 0 := le_of_lt (lt_of_not_ge N0), rw ← norm_le_zero_iff, exact le_trans (h_e x) (mul_nonpos_of_nonpos_of_nonneg N0 (norm_nonneg _)) }, have hf : f = 0, { ext, simp only [he x, zero_apply, map_zero] }, have hψ : ψ = 0, { rw hf, apply extend_zero }, rw [hψ, hf, norm_zero, norm_zero, mul_zero] } end end end uniformly_extend end op_norm end continuous_linear_map namespace linear_isometry @[simp] lemma norm_to_continuous_linear_map [nontrivial E] [ring_hom_isometric σ₁₂] (f : E →ₛₗᵢ[σ₁₂] F) : ∥f.to_continuous_linear_map∥ = 1 := f.to_continuous_linear_map.homothety_norm $ by simp variables {σ₁₃ : 𝕜 →+* 𝕜₃} [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] include σ₁₃ /-- Postcomposition of a continuous linear map with a linear isometry preserves the operator norm. -/ lemma norm_to_continuous_linear_map_comp [ring_hom_isometric σ₁₂] (f : F →ₛₗᵢ[σ₂₃] G) {g : E →SL[σ₁₂] F} : ∥f.to_continuous_linear_map.comp g∥ = ∥g∥ := op_norm_ext (f.to_continuous_linear_map.comp g) g (λ x, by simp only [norm_map, coe_to_continuous_linear_map, coe_comp']) omit σ₁₃ end linear_isometry end namespace continuous_linear_map variables [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [nondiscrete_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] [normed_space 𝕜₃ G] [normed_space 𝕜 Fₗ] (c : 𝕜) {σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₃ : 𝕜₂ →+* 𝕜₃} variables {𝕜₂' : Type*} [nondiscrete_normed_field 𝕜₂'] {F' : Type*} [normed_group F'] [normed_space 𝕜₂' F'] {σ₂' : 𝕜₂' →+* 𝕜₂} {σ₂'' : 𝕜₂ →+* 𝕜₂'} {σ₂₃' : 𝕜₂' →+* 𝕜₃} [ring_hom_inv_pair σ₂' σ₂''] [ring_hom_inv_pair σ₂'' σ₂'] [ring_hom_comp_triple σ₂' σ₂₃ σ₂₃'] [ring_hom_comp_triple σ₂'' σ₂₃' σ₂₃] [ring_hom_isometric σ₂₃] [ring_hom_isometric σ₂'] [ring_hom_isometric σ₂''] [ring_hom_isometric σ₂₃'] include σ₂'' σ₂₃' /-- Precomposition with a linear isometry preserves the operator norm. -/ lemma op_norm_comp_linear_isometry_equiv (f : F →SL[σ₂₃] G) (g : F' ≃ₛₗᵢ[σ₂'] F) : ∥f.comp g.to_linear_isometry.to_continuous_linear_map∥ = ∥f∥ := begin casesI subsingleton_or_nontrivial F', { haveI := g.symm.to_linear_equiv.to_equiv.subsingleton, simp }, refine le_antisymm _ _, { convert f.op_norm_comp_le g.to_linear_isometry.to_continuous_linear_map, simp [g.to_linear_isometry.norm_to_continuous_linear_map] }, { convert (f.comp g.to_linear_isometry.to_continuous_linear_map).op_norm_comp_le g.symm.to_linear_isometry.to_continuous_linear_map, { ext, simp }, haveI := g.symm.surjective.nontrivial, simp [g.symm.to_linear_isometry.norm_to_continuous_linear_map] }, end omit σ₂'' σ₂₃' /-- The norm of the tensor product of a scalar linear map and of an element of a normed space is the product of the norms. -/ @[simp] lemma norm_smul_right_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) : ∥smul_right c f∥ = ∥c∥ * ∥f∥ := begin refine le_antisymm _ _, { apply op_norm_le_bound _ (mul_nonneg (norm_nonneg _) (norm_nonneg _)) (λx, _), calc ∥(c x) • f∥ = ∥c x∥ * ∥f∥ : norm_smul _ _ ... ≤ (∥c∥ * ∥x∥) * ∥f∥ : mul_le_mul_of_nonneg_right (le_op_norm _ _) (norm_nonneg _) ... = ∥c∥ * ∥f∥ * ∥x∥ : by ring }, { by_cases h : f = 0, { simp [h] }, { have : 0 < ∥f∥ := norm_pos_iff.2 h, rw ← le_div_iff this, apply op_norm_le_bound _ (div_nonneg (norm_nonneg _) (norm_nonneg f)) (λx, _), rw [div_mul_eq_mul_div, le_div_iff this], calc ∥c x∥ * ∥f∥ = ∥c x • f∥ : (norm_smul _ _).symm ... = ∥smul_right c f x∥ : rfl ... ≤ ∥smul_right c f∥ * ∥x∥ : le_op_norm _ _ } }, end /-- The non-negative norm of the tensor product of a scalar linear map and of an element of a normed space is the product of the non-negative norms. -/ @[simp] lemma nnnorm_smul_right_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) : ∥smul_right c f∥₊ = ∥c∥₊ * ∥f∥₊ := nnreal.eq $ c.norm_smul_right_apply f variables (𝕜 E Fₗ) /-- `continuous_linear_map.smul_right` as a continuous trilinear map: `smul_rightL (c : E →L[𝕜] 𝕜) (f : F) (x : E) = c x • f`. -/ def smul_rightL : (E →L[𝕜] 𝕜) →L[𝕜] Fₗ →L[𝕜] E →L[𝕜] Fₗ := linear_map.mk_continuous₂ { to_fun := smul_rightₗ, map_add' := λ c₁ c₂, by { ext x, simp only [add_smul, coe_smul_rightₗ, add_apply, smul_right_apply, linear_map.add_apply] }, map_smul' := λ m c, by { ext x, simp only [smul_smul, coe_smul_rightₗ, algebra.id.smul_eq_mul, coe_smul', smul_right_apply, linear_map.smul_apply, ring_hom.id_apply, pi.smul_apply] } } 1 $ λ c x, by simp only [coe_smul_rightₗ, one_mul, norm_smul_right_apply, linear_map.coe_mk] variables {𝕜 E Fₗ} @[simp] lemma norm_smul_rightL_apply (c : E →L[𝕜] 𝕜) (f : Fₗ) : ∥smul_rightL 𝕜 E Fₗ c f∥ = ∥c∥ * ∥f∥ := norm_smul_right_apply c f @[simp] lemma norm_smul_rightL (c : E →L[𝕜] 𝕜) [nontrivial Fₗ] : ∥smul_rightL 𝕜 E Fₗ c∥ = ∥c∥ := continuous_linear_map.homothety_norm _ c.norm_smul_right_apply variables (𝕜) (𝕜' : Type*) section variables [normed_ring 𝕜'] [normed_algebra 𝕜 𝕜'] @[simp] lemma op_norm_lmul [norm_one_class 𝕜'] : ∥lmul 𝕜 𝕜'∥ = 1 := by haveI := norm_one_class.nontrivial 𝕜'; exact (lmulₗᵢ 𝕜 𝕜').norm_to_continuous_linear_map @[simp] lemma op_norm_lmul_right [norm_one_class 𝕜'] : ∥lmul_right 𝕜 𝕜'∥ = 1 := (op_norm_flip (@lmul 𝕜 _ 𝕜' _ _)).trans (op_norm_lmul _ _) end /-- The norm of `lsmul` equals 1 in any nontrivial normed group. This is `continuous_linear_map.op_norm_lsmul_le` as an equality. -/ @[simp] lemma op_norm_lsmul [normed_field 𝕜'] [normed_algebra 𝕜 𝕜'] [normed_space 𝕜' E] [is_scalar_tower 𝕜 𝕜' E] [nontrivial E] : ∥(lsmul 𝕜 𝕜' : 𝕜' →L[𝕜] E →L[𝕜] E)∥ = 1 := begin refine continuous_linear_map.op_norm_eq_of_bounds zero_le_one (λ x, _) (λ N hN h, _), { rw one_mul, exact op_norm_lsmul_apply_le _, }, obtain ⟨y, hy⟩ := exists_ne (0 : E), have := le_of_op_norm_le _ (h 1) y, simp_rw [lsmul_apply, one_smul, norm_one, mul_one] at this, refine le_of_mul_le_mul_right _ (norm_pos_iff.mpr hy), simp_rw [one_mul, this] end end continuous_linear_map namespace submodule variables [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [nondiscrete_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] {σ₁₂ : 𝕜 →+* 𝕜₂} lemma norm_subtypeL (K : submodule 𝕜 E) [nontrivial K] : ∥K.subtypeL∥ = 1 := K.subtypeₗᵢ.norm_to_continuous_linear_map end submodule namespace continuous_linear_equiv variables [nondiscrete_normed_field 𝕜] [nondiscrete_normed_field 𝕜₂] [nondiscrete_normed_field 𝕜₃] [normed_space 𝕜 E] [normed_space 𝕜₂ F] {σ₁₂ : 𝕜 →+* 𝕜₂} {σ₂₁ : 𝕜₂ →+* 𝕜} [ring_hom_inv_pair σ₁₂ σ₂₁] [ring_hom_inv_pair σ₂₁ σ₁₂] section variables [ring_hom_isometric σ₂₁] protected lemma antilipschitz (e : E ≃SL[σ₁₂] F) : antilipschitz_with ∥(e.symm : F →SL[σ₂₁] E)∥₊ e := e.symm.lipschitz.to_right_inverse e.left_inv lemma one_le_norm_mul_norm_symm [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) : 1 ≤ ∥(e : E →SL[σ₁₂] F)∥ * ∥(e.symm : F →SL[σ₂₁] E)∥ := begin rw [mul_comm], convert (e.symm : F →SL[σ₂₁] E).op_norm_comp_le (e : E →SL[σ₁₂] F), rw [e.coe_symm_comp_coe, continuous_linear_map.norm_id] end include σ₂₁ lemma norm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) : 0 < ∥(e : E →SL[σ₁₂] F)∥ := pos_of_mul_pos_left (lt_of_lt_of_le zero_lt_one e.one_le_norm_mul_norm_symm) (norm_nonneg _) omit σ₂₁ lemma norm_symm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) : 0 < ∥(e.symm : F →SL[σ₂₁] E)∥ := pos_of_mul_pos_right (zero_lt_one.trans_le e.one_le_norm_mul_norm_symm) (norm_nonneg _) lemma nnnorm_symm_pos [ring_hom_isometric σ₁₂] [nontrivial E] (e : E ≃SL[σ₁₂] F) : 0 < ∥(e.symm : F →SL[σ₂₁] E)∥₊ := e.norm_symm_pos lemma subsingleton_or_norm_symm_pos [ring_hom_isometric σ₁₂] (e : E ≃SL[σ₁₂] F) : subsingleton E ∨ 0 < ∥(e.symm : F →SL[σ₂₁] E)∥ := begin rcases subsingleton_or_nontrivial E with _i|_i; resetI, { left, apply_instance }, { right, exact e.norm_symm_pos } end lemma subsingleton_or_nnnorm_symm_pos [ring_hom_isometric σ₁₂] (e : E ≃SL[σ₁₂] F) : subsingleton E ∨ 0 < ∥(e.symm : F →SL[σ₂₁] E)∥₊ := subsingleton_or_norm_symm_pos e variable (𝕜) /-- Given a nonzero element `x` of a normed space `E₁` over a field `𝕜`, the natural continuous linear equivalence from `E₁` to the span of `x`.-/ def to_span_nonzero_singleton (x : E) (h : x ≠ 0) : 𝕜 ≃L[𝕜] (𝕜 ∙ x) := of_homothety (linear_equiv.to_span_nonzero_singleton 𝕜 E x h) ∥x∥ (norm_pos_iff.mpr h) (to_span_nonzero_singleton_homothety 𝕜 x h) /-- Given a nonzero element `x` of a normed space `E₁` over a field `𝕜`, the natural continuous linear map from the span of `x` to `𝕜`.-/ def coord (x : E) (h : x ≠ 0) : (𝕜 ∙ x) →L[𝕜] 𝕜 := (to_span_nonzero_singleton 𝕜 x h).symm @[simp] lemma coe_to_span_nonzero_singleton_symm {x : E} (h : x ≠ 0) : ⇑(to_span_nonzero_singleton 𝕜 x h).symm = coord 𝕜 x h := rfl @[simp] lemma coord_to_span_nonzero_singleton {x : E} (h : x ≠ 0) (c : 𝕜) : coord 𝕜 x h (to_span_nonzero_singleton 𝕜 x h c) = c := (to_span_nonzero_singleton 𝕜 x h).symm_apply_apply c @[simp] lemma to_span_nonzero_singleton_coord {x : E} (h : x ≠ 0) (y : 𝕜 ∙ x) : to_span_nonzero_singleton 𝕜 x h (coord 𝕜 x h y) = y := (to_span_nonzero_singleton 𝕜 x h).apply_symm_apply y @[simp] lemma coord_norm (x : E) (h : x ≠ 0) : ∥coord 𝕜 x h∥ = ∥x∥⁻¹ := begin have hx : 0 < ∥x∥ := (norm_pos_iff.mpr h), haveI : nontrivial (𝕜 ∙ x) := submodule.nontrivial_span_singleton h, exact continuous_linear_map.homothety_norm _ (λ y, homothety_inverse _ hx _ (to_span_nonzero_singleton_homothety 𝕜 x h) _) end @[simp] lemma coord_self (x : E) (h : x ≠ 0) : (coord 𝕜 x h) (⟨x, submodule.mem_span_singleton_self x⟩ : 𝕜 ∙ x) = 1 := linear_equiv.coord_self 𝕜 E x h variables {𝕜} {𝕜₄ : Type*} [nondiscrete_normed_field 𝕜₄] variables {H : Type*} [normed_group H] [normed_space 𝕜₄ H] [normed_space 𝕜₃ G] variables {σ₂₃ : 𝕜₂ →+* 𝕜₃} {σ₁₃ : 𝕜 →+* 𝕜₃} variables {σ₃₄ : 𝕜₃ →+* 𝕜₄} {σ₄₃ : 𝕜₄ →+* 𝕜₃} variables {σ₂₄ : 𝕜₂ →+* 𝕜₄} {σ₁₄ : 𝕜 →+* 𝕜₄} variables [ring_hom_inv_pair σ₃₄ σ₄₃] [ring_hom_inv_pair σ₄₃ σ₃₄] variables [ring_hom_comp_triple σ₂₁ σ₁₄ σ₂₄] [ring_hom_comp_triple σ₂₄ σ₄₃ σ₂₃] variables [ring_hom_comp_triple σ₁₂ σ₂₃ σ₁₃] [ring_hom_comp_triple σ₁₃ σ₃₄ σ₁₄] variables [ring_hom_isometric σ₁₄] [ring_hom_isometric σ₂₃] variables [ring_hom_isometric σ₄₃] [ring_hom_isometric σ₂₄] variables [ring_hom_isometric σ₁₃] [ring_hom_isometric σ₁₂] variables [ring_hom_isometric σ₃₄] include σ₂₁ σ₃₄ σ₁₃ σ₂₄ /-- A pair of continuous (semi)linear equivalences generates an continuous (semi)linear equivalence between the spaces of continuous (semi)linear maps. -/ @[simps apply symm_apply] def arrow_congrSL (e₁₂ : E ≃SL[σ₁₂] F) (e₄₃ : H ≃SL[σ₄₃] G) : (E →SL[σ₁₄] H) ≃SL[σ₄₃] (F →SL[σ₂₃] G) := { -- given explicitly to help `simps` to_fun := λ L, (e₄₃ : H →SL[σ₄₃] G).comp (L.comp (e₁₂.symm : F →SL[σ₂₁] E)), -- given explicitly to help `simps` inv_fun := λ L, (e₄₃.symm : G →SL[σ₃₄] H).comp (L.comp (e₁₂ : E →SL[σ₁₂] F)), map_add' := λ f g, by rw [add_comp, comp_add], map_smul' := λ t f, by rw [smul_comp, comp_smulₛₗ], continuous_to_fun := (continuous_id.clm_comp_const _).const_clm_comp _, continuous_inv_fun := (continuous_id.clm_comp_const _).const_clm_comp _, .. e₁₂.arrow_congr_equiv e₄₃, } omit σ₂₁ σ₃₄ σ₁₃ σ₂₄ /-- A pair of continuous linear equivalences generates an continuous linear equivalence between the spaces of continuous linear maps. -/ def arrow_congr {F H : Type*} [normed_group F] [normed_group H] [normed_space 𝕜 F] [normed_space 𝕜 G] [normed_space 𝕜 H] (e₁ : E ≃L[𝕜] F) (e₂ : H ≃L[𝕜] G) : (E →L[𝕜] H) ≃L[𝕜] (F →L[𝕜] G) := arrow_congrSL e₁ e₂ end end continuous_linear_equiv end normed /-- A bounded bilinear form `B` in a real normed space is *coercive* if there is some positive constant C such that `C * ∥u∥ * ∥u∥ ≤ B u u`. -/ def is_coercive [normed_group E] [normed_space ℝ E] (B : E →L[ℝ] E →L[ℝ] ℝ) : Prop := ∃ C, (0 < C) ∧ ∀ u, C * ∥u∥ * ∥u∥ ≤ B u u
8d3135386ae014862905b81ca85bbe4ba5dc21bd
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/tactic/omega/coeffs_auto.lean
96d8d78afe5839c8990a4b2776b30a97675e9452
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
5,886
lean
/- Copyright (c) 2019 Seul Baek. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Seul Baek -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.data.list.func import Mathlib.tactic.ring import Mathlib.tactic.omega.misc import Mathlib.PostPort namespace Mathlib /- Non-constant terms of linear constraints are represented by storing their coefficients in integer lists. -/ namespace omega namespace coeffs /-- `val_between v as l o` is the value (under valuation `v`) of the term obtained taking the term represented by `(0, as)` and dropping all subterms that include variables outside the range `[l,l+o)` -/ @[simp] def val_between (v : ℕ → ℤ) (as : List ℤ) (l : ℕ) : ℕ → ℤ := sorry @[simp] theorem val_between_nil {v : ℕ → ℤ} {l : ℕ} (m : ℕ) : val_between v [] l m = 0 := sorry /-- Evaluation of the nonconstant component of a normalized linear arithmetic term. -/ def val (v : ℕ → ℤ) (as : List ℤ) : ℤ := val_between v as 0 (list.length as) @[simp] theorem val_nil {v : ℕ → ℤ} : val v [] = 0 := rfl theorem val_between_eq_of_le {v : ℕ → ℤ} {as : List ℤ} {l : ℕ} (m : ℕ) : list.length as ≤ l + m → val_between v as l m = val_between v as l (list.length as - l) := sorry theorem val_eq_of_le {v : ℕ → ℤ} {as : List ℤ} {k : ℕ} : list.length as ≤ k → val v as = val_between v as 0 k := sorry theorem val_between_eq_val_between {v : ℕ → ℤ} {w : ℕ → ℤ} {as : List ℤ} {bs : List ℤ} {l : ℕ} {m : ℕ} : (∀ (x : ℕ), l ≤ x → x < l + m → v x = w x) → (∀ (x : ℕ), l ≤ x → x < l + m → list.func.get x as = list.func.get x bs) → val_between v as l m = val_between w bs l m := sorry theorem val_between_set {v : ℕ → ℤ} {a : ℤ} {l : ℕ} {n : ℕ} {m : ℕ} : l ≤ n → n < l + m → val_between v (list.func.set a [] n) l m = a * v n := sorry @[simp] theorem val_set {v : ℕ → ℤ} {m : ℕ} {a : ℤ} : val v (list.func.set a [] m) = a * v m := sorry theorem val_between_neg {v : ℕ → ℤ} {as : List ℤ} {l : ℕ} {o : ℕ} : val_between v (list.func.neg as) l o = -val_between v as l o := sorry @[simp] theorem val_neg {v : ℕ → ℤ} {as : List ℤ} : val v (list.func.neg as) = -val v as := sorry theorem val_between_add {v : ℕ → ℤ} {is : List ℤ} {js : List ℤ} {l : ℕ} (m : ℕ) : val_between v (list.func.add is js) l m = val_between v is l m + val_between v js l m := sorry @[simp] theorem val_add {v : ℕ → ℤ} {is : List ℤ} {js : List ℤ} : val v (list.func.add is js) = val v is + val v js := sorry theorem val_between_sub {v : ℕ → ℤ} {is : List ℤ} {js : List ℤ} {l : ℕ} (m : ℕ) : val_between v (list.func.sub is js) l m = val_between v is l m - val_between v js l m := sorry @[simp] theorem val_sub {v : ℕ → ℤ} {is : List ℤ} {js : List ℤ} : val v (list.func.sub is js) = val v is - val v js := sorry /-- `val_except k v as` is the value (under valuation `v`) of the term obtained taking the term represented by `(0, as)` and dropping the subterm that includes the `k`th variable. -/ def val_except (k : ℕ) (v : ℕ → ℤ) (as : List ℤ) : ℤ := val_between v as 0 k + val_between v as (k + 1) (list.length as - (k + 1)) theorem val_except_eq_val_except {k : ℕ} {is : List ℤ} {js : List ℤ} {v : ℕ → ℤ} {w : ℕ → ℤ} : (∀ (x : ℕ), x ≠ k → v x = w x) → (∀ (x : ℕ), x ≠ k → list.func.get x is = list.func.get x js) → val_except k v is = val_except k w js := sorry theorem val_except_update_set {v : ℕ → ℤ} {n : ℕ} {as : List ℤ} {i : ℤ} {j : ℤ} : val_except n (update n i v) (list.func.set j as n) = val_except n v as := val_except_eq_val_except update_eq_of_ne (list.func.get_set_eq_of_ne n) theorem val_between_add_val_between {v : ℕ → ℤ} {as : List ℤ} {l : ℕ} {m : ℕ} {n : ℕ} : val_between v as l m + val_between v as (l + m) n = val_between v as l (m + n) := sorry theorem val_except_add_eq {v : ℕ → ℤ} (n : ℕ) {as : List ℤ} : val_except n v as + list.func.get n as * v n = val v as := sorry @[simp] theorem val_between_map_mul {v : ℕ → ℤ} {i : ℤ} {as : List ℤ} {l : ℕ} {m : ℕ} : val_between v (list.map (Mul.mul i) as) l m = i * val_between v as l m := sorry theorem forall_val_dvd_of_forall_mem_dvd {i : ℤ} {as : List ℤ} : (∀ (x : ℤ), x ∈ as → i ∣ x) → ∀ (n : ℕ), i ∣ list.func.get n as := fun (ᾰ : ∀ (x : ℤ), x ∈ as → i ∣ x) (n : ℕ) => idRhs ((fun (x : ℤ) => i ∣ x) (list.func.get n as)) (list.func.forall_val_of_forall_mem (dvd_zero i) ᾰ n) theorem dvd_val_between {v : ℕ → ℤ} {i : ℤ} {as : List ℤ} {l : ℕ} {m : ℕ} : (∀ (x : ℤ), x ∈ as → i ∣ x) → i ∣ val_between v as l m := sorry theorem dvd_val {v : ℕ → ℤ} {as : List ℤ} {i : ℤ} : (∀ (x : ℤ), x ∈ as → i ∣ x) → i ∣ val v as := dvd_val_between @[simp] theorem val_between_map_div {v : ℕ → ℤ} {as : List ℤ} {i : ℤ} {l : ℕ} (h1 : ∀ (x : ℤ), x ∈ as → i ∣ x) {m : ℕ} : val_between v (list.map (fun (x : ℤ) => x / i) as) l m = val_between v as l m / i := sorry @[simp] theorem val_map_div {v : ℕ → ℤ} {as : List ℤ} {i : ℤ} : (∀ (x : ℤ), x ∈ as → i ∣ x) → val v (list.map (fun (x : ℤ) => x / i) as) = val v as / i := sorry theorem val_between_eq_zero {v : ℕ → ℤ} {is : List ℤ} {l : ℕ} {m : ℕ} : (∀ (x : ℤ), x ∈ is → x = 0) → val_between v is l m = 0 := sorry theorem val_eq_zero {v : ℕ → ℤ} {is : List ℤ} : (∀ (x : ℤ), x ∈ is → x = 0) → val v is = 0 := val_between_eq_zero end Mathlib
d6a232ffd4fca0be8a064a1385a0a75d569d7f36
5ae26df177f810c5006841e9c73dc56e01b978d7
/src/category_theory/full_subcategory.lean
db88774d2500965a13b04b87f500138a15150436
[ "Apache-2.0" ]
permissive
ChrisHughes24/mathlib
98322577c460bc6b1fe5c21f42ce33ad1c3e5558
a2a867e827c2a6702beb9efc2b9282bd801d5f9a
refs/heads/master
1,583,848,251,477
1,565,164,247,000
1,565,164,247,000
129,409,993
0
1
Apache-2.0
1,565,164,817,000
1,523,628,059,000
Lean
UTF-8
Lean
false
false
2,529
lean
/- Copyright (c) 2017 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Scott Morrison, Reid Barton -/ import category_theory.fully_faithful namespace category_theory universes v u₁ u₂ -- declare the `v`'s first; see `category_theory.category` for an explanation section induced /- Induced categories. Given a category D and a function F : C → D from a type C to the objects of D, there is an essentially unique way to give C a category structure such that F becomes a fully faithful functor, namely by taking Hom_C(X, Y) = Hom_D(FX, FY). We call this the category induced from D along F. As a special case, if C is a subtype of D, this produces the full subcategory of D on the objects belonging to C. In general the induced category is equivalent to the full subcategory of D on the image of F. -/ variables {C : Type u₁} {D : Type u₂} [𝒟 : category.{v} D] include 𝒟 variables (F : C → D) include F def induced_category : Type u₁ := C instance induced_category.category : category.{v} (induced_category F) := { hom := λ X Y, F X ⟶ F Y, id := λ X, 𝟙 (F X), comp := λ _ _ _ f g, f ≫ g } def induced_functor : induced_category F ⥤ D := { obj := F, map := λ x y f, f } @[simp] lemma induced_functor.obj {X} : (induced_functor F).obj X = F X := rfl @[simp] lemma induced_functor.hom {X Y} {f : X ⟶ Y} : (induced_functor F).map f = f := rfl instance induced_category.full : full (induced_functor F) := { preimage := λ x y f, f } instance induced_category.faithful : faithful (induced_functor F) := {} end induced section full_subcategory /- A full subcategory is the special case of an induced category with F = subtype.val. -/ variables {C : Type u₂} [𝒞 : category.{v} C] include 𝒞 variables (Z : C → Prop) instance full_subcategory : category.{v} {X : C // Z X} := induced_category.category subtype.val def full_subcategory_inclusion : {X : C // Z X} ⥤ C := induced_functor subtype.val @[simp] lemma full_subcategory_inclusion.obj {X} : (full_subcategory_inclusion Z).obj X = X.val := rfl @[simp] lemma full_subcategory_inclusion.map {X Y} {f : X ⟶ Y} : (full_subcategory_inclusion Z).map f = f := rfl instance full_subcategory.ful : full (full_subcategory_inclusion Z) := induced_category.full subtype.val instance full_subcategory.faithful : faithful (full_subcategory_inclusion Z) := induced_category.faithful subtype.val end full_subcategory end category_theory
ae1685a6d6aff26b51d5eab4b682cda4b0d7689d
df7bb3acd9623e489e95e85d0bc55590ab0bc393
/lean/love05_inductive_predicates_demo.lean
526e56d04e038658d49ed5580c1965b0e68b639f
[]
no_license
MaschavanderMarel/logical_verification_2020
a41c210b9237c56cb35f6cd399e3ac2fe42e775d
7d562ef174cc6578ca6013f74db336480470b708
refs/heads/master
1,692,144,223,196
1,634,661,675,000
1,634,661,675,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
13,827
lean
import .love03_forward_proofs_demo import .love04_functional_programming_demo /- # LoVe Demo 5: Inductive Predicates __Inductive predicates__, or inductively defined propositions, are a convenient way to specify functions of type `⋯ → Prop`. They are reminiscent of formal systems and of the Horn clauses of Prolog, the logic programming language par excellence. A possible view of Lean: Lean = functional programming + logic programming + more logic -/ set_option pp.beta true set_option pp.generalized_field_notation false namespace LoVe /- ## Introductory Examples ### Even Numbers Mathematicians often define sets as the smallest that meets some criteria. For example: The set `E` of even natural numbers is defined as the smallest set closed under the following rules: (1) `0 ∈ E` and (2) for every `k ∈ ℕ`, if `k ∈ E`, then `k + 2 ∈ E`. In Lean, we can define the corresponding "is even" predicate as follows: -/ inductive even : ℕ → Prop | zero : even 0 | add_two : ∀k : ℕ, even k → even (k + 2) /- This should look familiar. We have used the same syntax, except with `Type` instead of `Prop`, for inductive types. The above command introduces a new unary predicate `even` as well as two constructors, `even.zero` and `even.add_two`, which can be used to build proof terms. Thanks to the "no junk" guarantee of inductive definitions, `even.zero` and `even.add_two` are the only two ways to construct proofs of `even`. By the Curry–Howard correspondence, `even` can be seen as an inductive type, the values being the proof terms. -/ lemma even_4 : even 4 := have even_0 : even 0 := even.zero, have even_2 : even 2 := even.add_two _ even_0, show even 4, from even.add_two _ even_2 /- Why cannot we simply define `even` recursively? Indeed, why not? -/ def even₂ : ℕ → bool | 0 := tt | 1 := ff | (k + 2) := even₂ k /- There are advantages and disadvantages to both styles. The recursive version requires us to specify a false case (1), and it requires us to worry about termination. On the other hand, because it is computational, it works well with `refl`, `simp`, `#reduce`, and `#eval`. The inductive version is often considered more abstract and elegant. Each rule is stated independently of the others. Yet another way to define `even` is as a nonrecursive definition: -/ def even₃ (k : ℕ) : bool := k % 2 = 0 /- Mathematicians would probably find this the most satisfactory definition. But the inductive version is a convenient, intuitive example that is typical of many realistic inductive definitions. ### Tennis Games Transition systems consists of transition rules, which together specify a binary predicate connecting a "before" and an "after" state. As a simple specimen of a transition system, we consider the possible transitions, in a game of tennis, starting from 0–0. -/ inductive score : Type | vs : ℕ → ℕ → score | adv_srv : score | adv_rcv : score | game_srv : score | game_rcv : score infixr ` – ` : 10 := score.vs inductive step : score → score → Prop | srv_0_15 : ∀n, step (0–n) (15–n) | srv_15_30 : ∀n, step (15–n) (30–n) | srv_30_40 : ∀n, step (30–n) (40–n) | srv_40_game : ∀n, n < 40 → step (40–n) score.game_srv | srv_40_adv : step (40–40) score.adv_srv | srv_adv_40 : step score.adv_srv (40–40) | rcv_0_15 : ∀n, step (n–0) (n–15) | rcv_15_30 : ∀n, step (n–15) (n–30) | rcv_30_40 : ∀n, step (n–30) (n–40) | rcv_40_game : ∀n, n < 40 → step (n–40) score.game_rcv | rcv_40_adv : step (40–40) score.adv_rcv | rcv_adv_40 : step score.adv_rcv (40–40) infixr ` ⇒ ` := step /- We can ask, and formally answer, questions such as: Is this transition system confluent? Does it always terminate? Can the score 65–15 be reached from 0–0? ### Reflexive Transitive Closure Our last introductory example is the reflexive transitive closure of a relation `r`, modeled as a binary predicate `star r`. -/ inductive star {α : Type} (r : α → α → Prop) : α → α → Prop | base (a b : α) : r a b → star a b | refl (a : α) : star a a | trans (a b c : α) : star a b → star b c → star a c /- The first rule embeds `r` into `star r`. The second rule achieves the reflexive closure. The third rule achieves the transitive closure. The definition is truly elegant. If you doubt this, try implementing `star` as a recursive function: -/ def star₂ {α : Type} (r : α → α → Prop) : α → α → Prop := sorry /- ### A Nonexample Not all inductive definitions admit a least solution. -/ -- fails inductive illegal₂ : Prop | intro : ¬ illegal₂ → illegal₂ /- ## Logical Symbols The truth values `false` and `true`, the connectives `∧` and `∨`, the `∃`-quantifier, and the equality predicate `=` are all defined as inductive propositions or predicates. In contrast, `∀` (= `Π`) and `→` are built into the logic. Syntactic sugar: `∃x : α, p` := `Exists (λx : α, p)` `x = y` := `eq x y` -/ namespace logical_symbols inductive and (a b : Prop) : Prop | intro : a → b → and inductive or (a b : Prop) : Prop | intro_left : a → or | intro_right : b → or inductive iff (a b : Prop) : Prop | intro : (a → b) → (b → a) → iff inductive Exists {α : Type} (p : α → Prop) : Prop | intro : ∀a : α, p a → Exists inductive true : Prop | intro : true inductive false : Prop inductive eq {α : Type} : α → α → Prop | refl : ∀a : α, eq a a end logical_symbols #print and #print or #print iff #print Exists #print true #print false #print eq /- ## Rule Induction Just as we can perform induction on a term, we can perform induction on a proof term. This is called __rule induction__, because the induction is on the introduction rules (i.e., the constructors of the proof term). Thanks to the Curry–Howard correspondence, this works as expected. -/ lemma mod_two_eq_zero_of_even (n : ℕ) (h : even n) : n % 2 = 0 := begin induction' h, case zero { refl }, case add_two : k hk ih { simp [ih] } end lemma not_even_two_mul_add_one (n : ℕ) : ¬ even (2 * n + 1) := begin intro h, induction' h, apply ih (n - 1), cases' n, case zero { linarith }, case succ { simp [nat.succ_eq_add_one] at *, linarith } end /- `linarith` proves goals involving linear arithmetic equalities or inequalities. "Linear" means it works only with `+` and `-`, not `*` and `/` (but multiplication by a constant is supported). -/ lemma linarith_example (i : ℤ) (hi : i > 5) : 2 * i + 3 > 11 := by linarith lemma star_star_iff_star {α : Type} (r : α → α → Prop) (a b : α) : star (star r) a b ↔ star r a b := begin apply iff.intro, { intro h, induction' h, case base : a b hab { exact hab }, case refl : a { apply star.refl }, case trans : a b c hab hbc ihab ihbc { apply star.trans a b, { exact ihab }, { exact ihbc } } }, { intro h, apply star.base, exact h } end @[simp] lemma star_star_eq_star {α : Type} (r : α → α → Prop) : star (star r) = star r := begin apply funext, intro a, apply funext, intro b, apply propext, apply star_star_iff_star end #check funext #check propext /- ## Elimination Given an inductive predicate `p`, its introduction rules typically have the form `∀…, ⋯ → p …` and can be used to prove goals of the form `⊢ p …`. Elimination works the other way around: It extracts information from a lemma or hypothesis of the form `p …`. Elimination takes various forms: pattern matching, the `cases'` and `induction'` tactics, and custom elimination rules (e.g., `and.elim_left`). * `cases'` works like `induction'` but without induction hypothesis. * `match` is available as well, but it corresponds to dependently typed pattern matching (cf. `vector` in lecture 4). Now we can finally analyze how `cases' h`, where `h : l = r`, and how `cases' classical.em h` work. -/ #print eq lemma cases_eq_example {α : Type} (l r : α) (h : l = r) (p : α → α → Prop) : p l r := begin cases' h, sorry end #check classical.em #print or lemma cases_classical_em_example {α : Type} (a : α) (p q : α → Prop) : q a := begin have h : p a ∨ ¬ p a := classical.em (p a), cases' h, case inl { sorry }, case inr { sorry } end /- Often it is convenient to rewrite concrete terms of the form `p (c …)`, where `c` is typically a constructor. We can state and prove an __inversion rule__ to support such eliminative reasoning. Typical inversion rule: `∀x y, p (c x y) → (∃…, ⋯ ∧ ⋯) ∨ ⋯ ∨ (∃…, ⋯ ∧ ⋯)` It can be useful to combine introduction and elimination into a single lemma, which can be used for rewriting both the hypotheses and conclusions of goals: `∀x y, p (c x y) ↔ (∃…, ⋯ ∧ ⋯) ∨ ⋯ ∨ (∃…, ⋯ ∧ ⋯)` -/ lemma even_iff (n : ℕ) : even n ↔ n = 0 ∨ (∃m : ℕ, n = m + 2 ∧ even m) := begin apply iff.intro, { intro hn, cases' hn, case zero { simp }, case add_two : k hk { apply or.intro_right, apply exists.intro k, simp [hk] } }, { intro hor, cases' hor, case inl : heq { simp [heq, even.zero] }, case inr : hex { cases' hex with k hand, cases' hand with heq hk, simp [heq, even.add_two _ hk] } } end lemma even_iff₂ (n : ℕ) : even n ↔ n = 0 ∨ (∃m : ℕ, n = m + 2 ∧ even m) := iff.intro (assume hn : even n, match n, hn with | _, even.zero := show 0 = 0 ∨ _, from by simp | _, even.add_two k hk := show _ ∨ (∃m, k + 2 = m + 2 ∧ even m), from or.intro_right _ (exists.intro k (by simp [*])) end) (assume hor : n = 0 ∨ (∃m, n = m + 2 ∧ even m), match hor with | or.intro_left _ heq := show even n, from by simp [heq, even.zero] | or.intro_right _ hex := match hex with | Exists.intro m hand := match hand with | and.intro heq hm := show even n, from by simp [heq, even.add_two _ hm] end end end) /- ## Further Examples ### Sorted Lists -/ inductive sorted : list ℕ → Prop | nil : sorted [] | single {x : ℕ} : sorted [x] | two_or_more {x y : ℕ} {zs : list ℕ} (hle : x ≤ y) (hsorted : sorted (y :: zs)) : sorted (x :: y :: zs) lemma sorted_nil : sorted [] := sorted.nil lemma sorted_2 : sorted [2] := sorted.single lemma sorted_3_5 : sorted [3, 5] := begin apply sorted.two_or_more, { linarith }, { exact sorted.single } end lemma sorted_3_5₂ : sorted [3, 5] := sorted.two_or_more (by linarith) sorted.single lemma sorted_7_9_9_11 : sorted [7, 9, 9, 11] := sorted.two_or_more (by linarith) (sorted.two_or_more (by linarith) (sorted.two_or_more (by linarith) sorted.single)) lemma not_sorted_17_13 : ¬ sorted [17, 13] := begin intro h, cases' h, linarith end /- ### Palindromes -/ inductive palindrome {α : Type} : list α → Prop | nil : palindrome [] | single (x : α) : palindrome [x] | sandwich (x : α) (xs : list α) (hxs : palindrome xs) : palindrome ([x] ++ xs ++ [x]) -- fails def palindrome₂ {α : Type} : list α → Prop | [] := true | [_] := true | ([x] ++ xs ++ [x]) := palindrome₂ xs | _ := false lemma palindrome_aa {α : Type} (a : α) : palindrome [a, a] := palindrome.sandwich a _ palindrome.nil lemma palindrome_aba {α : Type} (a b : α) : palindrome [a, b, a] := palindrome.sandwich a _ (palindrome.single b) lemma reverse_palindrome {α : Type} (xs : list α) (hxs : palindrome xs) : palindrome (reverse xs) := begin induction' hxs, case nil { exact palindrome.nil }, case single { exact palindrome.single x }, case sandwich { simp [reverse, reverse_append], exact palindrome.sandwich _ _ ih } end /- ### Full Binary Trees -/ #check btree inductive is_full {α : Type} : btree α → Prop | empty : is_full btree.empty | node (a : α) (l r : btree α) (hl : is_full l) (hr : is_full r) (hiff : l = btree.empty ↔ r = btree.empty) : is_full (btree.node a l r) lemma is_full_singleton {α : Type} (a : α) : is_full (btree.node a btree.empty btree.empty) := begin apply is_full.node, { exact is_full.empty }, { exact is_full.empty }, { refl } end lemma is_full_mirror {α : Type} (t : btree α) (ht : is_full t) : is_full (mirror t) := begin induction' ht, case empty { exact is_full.empty }, case node : a l r hl hr hiff ih_l ih_r { rw mirror, apply is_full.node, { exact ih_r }, { exact ih_l }, { simp [mirror_eq_empty_iff, *] } } end lemma is_full_mirror₂ {α : Type} : ∀t : btree α, is_full t → is_full (mirror t) | btree.empty := begin intro ht, exact ht end | (btree.node a l r) := begin intro ht, cases ht with _ _ _ hl hr hiff, rw mirror, apply is_full.node, { exact is_full_mirror₂ _ hr }, { apply is_full_mirror₂ _ hl }, { simp [mirror_eq_empty_iff, *] } end /- ### First-Order Terms -/ inductive term (α β : Type) : Type | var {} : β → term | fn : α → list term → term inductive well_formed {α β : Type} (arity : α → ℕ) : term α β → Prop | var (x : β) : well_formed (term.var x) | fn (f : α) (ts : list (term α β)) (hargs : ∀t ∈ ts, well_formed t) (hlen : list.length ts = arity f) : well_formed (term.fn f ts) inductive variable_free {α β : Type} : term α β → Prop | fn (f : α) (ts : list (term α β)) (hargs : ∀t ∈ ts, variable_free t) : variable_free (term.fn f ts) end LoVe
06f03643908d7f1e310d8a91a4cb2f406fd11c13
274748215b6d042f0d9c9a505f9551fa8e0c5f38
/src/abstract_V_and_I.lean
ae113498188308072d599b087712ecf9f4b3eb4c
[ "Apache-2.0" ]
permissive
ImperialCollegeLondon/M4P33
878ecb515c77d20cc799ff1ebd78f1bf4fd65c12
1a179372db71ad6802d11eacbc1f02f327d55f8f
refs/heads/master
1,607,519,867,193
1,583,344,297,000
1,583,344,297,000
233,316,107
59
4
Apache-2.0
1,579,285,778,000
1,578,788,367,000
Lean
UTF-8
Lean
false
false
9,845
lean
/- Algebraic geometry M4P33, Jan-Mar 2020, formalised in Lean. Copyright (c) 2020 Kevin Buzzard Released under Apache 2.0 license as described in the file LICENSE. Authors: Kevin Buzzard, Alexander Uhlmann, and whoever else in the class wants to join in. Thoughts about how much of the 𝕍 and 𝕀 theory goes through abstractly. Note: I think that this file is now pretty much done. -/ -- This is set theory. But we want to do some type theory as well. -- Types are a convenient way of gathering mathematical objects -- into well-defined collections, such as rings. import data.type.basic -- We want to prove that the images of 𝕍 and 𝕀 are in natural -- bijection with each other so we need the general theory -- of bijections import data.equiv.basic -- the lattice structure on subsets (infinite unions etc) import data.set.lattice universes u v -- set theorists can set these both to be 0. -- (R : Type 0) means "let R be a set". -- Let R be a set. -- For example R could be the ring `k[X₁,…,Xₙ]` variables (R : Type u) -- When we're talking about 𝕍 and 𝕀, we will not mention R as part -- of the notation even though it is playing a role {R} -- Let 𝔸ⁿ be another set. variables (A : Type v) -- Similarly we will not explicitly mention A most of the time {A} -- Let `P` be a way of getting a true/false statement from a pair of -- elements f ∈ R and x ∈ 𝔸ⁿ. For example P(f,x) can be -- the statement that `f(x) = 0`. You can think of `P` as being a subset -- of R × 𝔸ⁿ if you like. variable (P : R → A → Prop) include P -- Let 𝕍, a function from subsets of R to subsets of -- 𝔸ⁿ, and 𝕀, a function from subsets of 𝔸ⁿ -- to subsets of R, be defined in the usual way. -- One can think of 𝕍(S) as being the largest U such that S × U ⊆ P, -- and 𝕀(U) as being the largest S such that S × U ⊆ P. -- The main theorem we will prove today is -- *Theorem* For all S ⊆ R, 𝕍(𝕀(𝕍(S)))=𝕍(S), -- assuming some irrelevant extra hypotheses, such as the assumption -- that k is algebraically closed, or S is an ideal. def 𝕍_ (S : set R) : set A := {x : A | ∀ f ∈ S, P f x} notation `𝕍`:max := 𝕍_ (by exact P) -- Type of 𝕍_ is Π {R : Type*} {A : Type*}, (R → A → Prop) → set R → set A -- i.e. (R → A → Prop) → (R → Prop) → (A → Prop) def 𝕀_ (X : set A) : set R := {f : R | ∀ x ∈ X, P f x} notation `𝕀`:max := 𝕀_ (by exact P) -- restate definitions lemma mem_𝕍_iff (S : set R) (x : A) : x ∈ 𝕍 S ↔ ∀ f ∈ S, P f x := iff.rfl lemma mem_𝕀_iff (V : set A) (f : R) : f ∈ 𝕀 V ↔ ∀ x ∈ V, P f x := iff.rfl -- we're going to be doing set theory open set /-- 𝕍(∅) is everything. -/ lemma 𝕍_empty : 𝕍 ∅ = univ := begin -- It suffices to show that every x of type A is in 𝕍 ∅ rw eq_univ_iff_forall, -- so let x be in A intro x, -- we need to show that every f in the empty set satisfies (f,x) ∈ P rw mem_𝕍_iff, -- so say f is in the empty set intros f hf, -- but there are no elements in the empty set so we must be done cases hf, end /-- 𝕀(∅) is everything -/ lemma 𝕀_empty : 𝕀 ∅ = univ := -- computer science proof eq_univ_iff_forall.2 $ λ x f, by rintro ⟨⟩ -- 𝕍 is inclusion-reversing lemma 𝕍_antimono (S T : set R) (h : S ⊆ T) : 𝕍 T ⊆ 𝕍 S := begin -- say x ∈ 𝕍(T) intros x hx, -- and s in S, intros s hs, -- We want to show P(s,x) is true, or (s,x) ∈ P or s(x) = 0 or however -- you think about it. -- Because x ∈ 𝕍(T), we know P(t,x) is true for all t ∈ T, -- so it suffices to prove s ∈ T apply hx, -- But S ⊆ T apply h, -- and s ∈ S so we're done exact hs end -- Here is how a computer scientist would write this proof: lemma 𝕍_antimono' (S T : set R) (h : S ⊆ T) : 𝕍 T ⊆ 𝕍 S := λ x hx s hs, hx _ (h hs) -- The advantage of writing it this way is that it also proves the converse! lemma 𝕀_antimono (U V : set A) (h : U ⊆ V) : 𝕀 V ⊆ 𝕀 U := λ x hx s hs, hx _ (h hs) -- Exercise: prove 𝕀_antimono the way a mathematician would, using only -- intros, apply and exact. Need help? Try the natural number game. -- Thanks to Alexander Uhlmann for spotting definition 3.1 here: -- https://ncatlab.org/nlab/show/Galois+connection#GaloisTheory lemma is_galois_connection {Y : set A} {J : set R} : Y ⊆ 𝕍 J ↔ 𝕀 Y ⊇ J := begin split, { intros hY f hf, rw mem_𝕀_iff, intros x hx, exact hY hx f hf, }, { intros hJ x hx f hf, exact hJ hf x hx, } end lemma 𝕍_union (S T : set R) : 𝕍 (S ∪ T) = 𝕍 S ∩ 𝕍 T := begin -- we prove both inclusions apply set.subset.antisymm, { -- say x ∈ 𝕍(S ∪ T) intros x hx, -- we need to prove x ∈ 𝕍 S and x ∈ 𝕍 T split, -- both of these follow easily from 𝕍_antimono -- exact 𝕍_antimono _ _ _ _ hx, -- TODO(kmb) -- why is the wrong underscore marked in red?? exact 𝕍_antimono _ _ _ (subset_union_left _ _) hx, exact 𝕍_antimono _ _ _ (subset_union_right _ _) hx, }, { -- say x ∈ 𝕍(S) ∩ 𝕍(T) rintros x ⟨hxS, hxT⟩, -- we need to prove that for all f ∈ S ∪ T, f(x) = 0 intros f hf, -- well f is either in S or T (or both) cases hf, { -- and if f ∈ S then we're done because x ∈ 𝕍(S) exact hxS _ hf }, { -- whereas if f ∈ T then we're done because x ∈ 𝕍(T) exact hxT _ hf } } end -- We prove this one in a slightly different way. lemma 𝕀_union (W X : set A) : 𝕀 (W ∪ X) = 𝕀 W ∩ 𝕀 X := begin -- By extensionality, two sets are equal iff they have the same elements ext x, -- To be in the intersection of two sets just means being in both of them show _ ↔ x ∈ 𝕀 W ∧ x ∈ 𝕀 X, -- By the definition of 𝕀,... rw [mem_𝕀_iff, mem_𝕀_iff, mem_𝕀_iff], --- we have to prove that -- W ∪ X ⊆ {f : f(x) = 0} iff W ⊆ {f : f(x) = 0} and X ⊆ {f : f(x) = 0} show W ∪ X ⊆ _ ↔ W ⊆ _ ∧ X ⊆ _, -- the underscore just means "guess the set" -- We prove the iff by proving both directions separately split, { -- Here we prove W ∪ X ⊆ Z → W ⊆ Z ∧ X ⊆ Z intro hWX, split, refine set.subset.trans _ hWX, apply subset_union_left, refine set.subset.trans _ hWX, apply subset_union_right, }, { -- and here we prove W ⊆ Z ∧ X ⊆ Z → W ∪ X ⊆ Z rintros ⟨hW, hX⟩, apply union_subset; assumption } end lemma 𝕍_Union (ι : Type*) (S : ι → set R) : 𝕍 (⋃ i, S i) = ⋂ i, 𝕍 (S i) := begin -- two sets are equal iff they have the same elements ext x, -- To be in the intersection of a bunch of sets just means -- being in all of them rw mem_Inter, -- By the definition of 𝕍, rw [mem_𝕍_iff], show (∀ (f : R), (f ∈ ⋃ (i : ι), S i) → P f x) ↔ ∀ (i : ι), ∀ f ∈ S i, P f x, -- Now prove both inclusions separately split, intros, apply a, rw mem_Union, use i, assumption, intros h f hf, rw mem_Union at hf, cases hf with i hi, apply h i, assumption, end -- an AI can find a proof of this too: lemma 𝕀_Union (ι : Type*) (J : ι → set A) : 𝕀 (⋃ i, J i) = ⋂ i, 𝕀 (J i) := begin ext, simp [𝕀_], tauto, end lemma 𝕍𝕀_mono (U V : set A) (h : U ⊆ V) : 𝕍 (𝕀 U) ⊆ 𝕍 (𝕀 V) := begin -- 𝕍 is anti-monotonic apply 𝕍_antimono, -- and 𝕀 is too apply 𝕀_antimono, -- so we just have to prove U ⊆ V, which is an assumption exact h end -- computer science proof of the other direction lemma 𝕀𝕍_mono (S T : set R) (h : S ⊆ T) : 𝕀 (𝕍 S) ⊆ 𝕀 (𝕍 T) := 𝕀_antimono P _ _ (𝕍_antimono P _ _ h) -- During the lecture today (17/01/20), it was pointed out that 𝕍(S) was the -- largest U such that S × U was a subset of P, and 𝕀(U) was the largest S -- such that S × U was a subset of P. This geometric way of thinking -- about things makes the next lemma trivial. Can you understand the Lean proof? /-- U ⊆ 𝕍(𝕀(U)) -/ lemma sub_𝕍𝕀 (U : set A) : U ⊆ 𝕍 (𝕀 U) := begin intros x hx, rw mem_𝕍_iff, intros f hf, rw mem_𝕀_iff at hf, apply hf, exact hx, end -- Because the proofs of sub_𝕍𝕀 and sub_𝕀𝕍 are basically -- the same, it might come as no surprise to see that you -- can prove one of them using the other one! The trick is -- to make sure you allow quantification over all R and A -- so you can switch them around. /-- S ⊆ 𝕀(𝕍(S)) -/ lemma sub_𝕀𝕍 (S : set R) : S ⊆ 𝕀 (𝕍 S) := sub_𝕍𝕀 _ _ -- the big theorem lemma 𝕍𝕀𝕍_eq_𝕍 (S : set R) : 𝕍 (𝕀 (𝕍 S)) = 𝕍 S := begin apply set.subset.antisymm, { apply 𝕍_antimono, apply sub_𝕀𝕍 }, { apply sub_𝕍𝕀, } end -- the same theorem again (permute R and A) lemma 𝕀𝕍𝕀_eq_𝕀 (V : set A) : (𝕀 (𝕍 (𝕀 V))) = 𝕀 V := 𝕍𝕀𝕍_eq_𝕍 _ V -- same proof but with a different P open set -- this final proof is written in a very computer-science way /-- The images of 𝕍 and of 𝕀 are naturally in bijection -/ lemma not_the_nullstellensatz : {V // ∃ J, 𝕍 J = V} ≃ {I // ∃ V, 𝕀 V = I} := { to_fun := λ V, ⟨𝕀 (V.1), V, rfl⟩, inv_fun := λ I, ⟨𝕍 I.1, I, rfl⟩, left_inv := begin rintro ⟨V, J, hJ⟩, rw subtype.ext, change 𝕍 (𝕀 V) = V, rw ←hJ, refine 𝕍𝕀𝕍_eq_𝕍 _ _, end, right_inv := begin rintro ⟨J, V, hV⟩, rw subtype.ext, change 𝕀 (𝕍 J) = J, rw ←hV, refine 𝕀𝕍𝕀_eq_𝕀 _ _, end } -- The Nullstellensatz says that the image of 𝕀 is precisely the -- radical ideals. One inclusion is clear (which?)
6cb2bf8e7e00959d755d23e1be9958ad0d5694bf
d406927ab5617694ec9ea7001f101b7c9e3d9702
/src/measure_theory/measure/hausdorff.lean
da68167418b46295f4f400778c48bbc8dcde00b6
[ "Apache-2.0" ]
permissive
alreadydone/mathlib
dc0be621c6c8208c581f5170a8216c5ba6721927
c982179ec21091d3e102d8a5d9f5fe06c8fafb73
refs/heads/master
1,685,523,275,196
1,670,184,141,000
1,670,184,141,000
287,574,545
0
0
Apache-2.0
1,670,290,714,000
1,597,421,623,000
Lean
UTF-8
Lean
false
false
44,812
lean
/- Copyright (c) 2021 Yury Kudryashov. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yury Kudryashov -/ import analysis.special_functions.pow import measure_theory.constructions.borel_space import measure_theory.measure.lebesgue import topology.metric_space.holder import topology.metric_space.metric_separated /-! # Hausdorff measure and metric (outer) measures In this file we define the `d`-dimensional Hausdorff measure on an (extended) metric space `X` and the Hausdorff dimension of a set in an (extended) metric space. Let `μ d δ` be the maximal outer measure such that `μ d δ s ≤ (emetric.diam s) ^ d` for every set of diameter less than `δ`. Then the Hausdorff measure `μH[d] s` of `s` is defined as `⨆ δ > 0, μ d δ s`. By Caratheodory theorem `measure_theory.outer_measure.is_metric.borel_le_caratheodory`, this is a Borel measure on `X`. The value of `μH[d]`, `d > 0`, on a set `s` (measurable or not) is given by ``` μH[d] s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, emetric.diam (t n) ≤ r), ∑' n, emetric.diam (t n) ^ d ``` For every set `s` for any `d < d'` we have either `μH[d] s = ∞` or `μH[d'] s = 0`, see `measure_theory.measure.hausdorff_measure_zero_or_top`. In `topology.metric_space.hausdorff_dimension` we use this fact to define the Hausdorff dimension `dimH` of a set in an (extended) metric space. We also define two generalizations of the Hausdorff measure. In one generalization (see `measure_theory.measure.mk_metric`) we take any function `m (diam s)` instead of `(diam s) ^ d`. In an even more general definition (see `measure_theory.measure.mk_metric'`) we use any function of `m : set X → ℝ≥0∞`. Some authors start with a partial function `m` defined only on some sets `s : set X` (e.g., only on balls or only on measurable sets). This is equivalent to our definition applied to `measure_theory.extend m`. We also define a predicate `measure_theory.outer_measure.is_metric` which says that an outer measure is additive on metric separated pairs of sets: `μ (s ∪ t) = μ s + μ t` provided that `⨅ (x ∈ s) (y ∈ t), edist x y ≠ 0`. This is the property required for the Caratheodory theorem `measure_theory.outer_measure.is_metric.borel_le_caratheodory`, so we prove this theorem for any metric outer measure, then prove that outer measures constructed using `mk_metric'` are metric outer measures. ## Main definitions * `measure_theory.outer_measure.is_metric`: an outer measure `μ` is called *metric* if `μ (s ∪ t) = μ s + μ t` for any two metric separated sets `s` and `t`. A metric outer measure in a Borel extended metric space is guaranteed to satisfy the Caratheodory condition, see `measure_theory.outer_measure.is_metric.borel_le_caratheodory`. * `measure_theory.outer_measure.mk_metric'` and its particular case `measure_theory.outer_measure.mk_metric`: a construction of an outer measure that is guaranteed to be metric. Both constructions are generalizations of the Hausdorff measure. The same measures interpreted as Borel measures are called `measure_theory.measure.mk_metric'` and `measure_theory.measure.mk_metric`. * `measure_theory.measure.hausdorff_measure` a.k.a. `μH[d]`: the `d`-dimensional Hausdorff measure. There are many definitions of the Hausdorff measure that differ from each other by a multiplicative constant. We put `μH[d] s = ⨆ r > 0, ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, emetric.diam (t n) ≤ r), ∑' n, ⨆ (ht : ¬set.subsingleton (t n)), (emetric.diam (t n)) ^ d`, see `measure_theory.measure.hausdorff_measure_apply'`. In the most interesting case `0 < d` one can omit the `⨆ (ht : ¬set.subsingleton (t n))` part. ## Main statements ### Basic properties * `measure_theory.outer_measure.is_metric.borel_le_caratheodory`: if `μ` is a metric outer measure on an extended metric space `X` (that is, it is additive on pairs of metric separated sets), then every Borel set is Caratheodory measurable (hence, `μ` defines an actual `measure_theory.measure`). See also `measure_theory.measure.mk_metric`. * `measure_theory.measure.hausdorff_measure_mono`: `μH[d] s` is an antitone function of `d`. * `measure_theory.measure.hausdorff_measure_zero_or_top`: if `d₁ < d₂`, then for any `s`, either `μH[d₂] s = 0` or `μH[d₁] s = ∞`. Together with the previous lemma, this means that `μH[d] s` is equal to infinity on some ray `(-∞, D)` and is equal to zero on `(D, +∞)`, where `D` is a possibly infinite number called the *Hausdorff dimension* of `s`; `μH[D] s` can be zero, infinity, or anything in between. * `measure_theory.measure.no_atoms_hausdorff`: Hausdorff measure has no atoms. ### Hausdorff measure in `ℝⁿ` * `measure_theory.hausdorff_measure_pi_real`: for a nonempty `ι`, `μH[card ι]` on `ι → ℝ` equals Lebesgue measure. ## Notations We use the following notation localized in `measure_theory`. - `μH[d]` : `measure_theory.measure.hausdorff_measure d` ## Implementation notes There are a few similar constructions called the `d`-dimensional Hausdorff measure. E.g., some sources only allow coverings by balls and use `r ^ d` instead of `(diam s) ^ d`. While these construction lead to different Hausdorff measures, they lead to the same notion of the Hausdorff dimension. ## TODO * prove that `1`-dimensional Hausdorff measure on `ℝ` equals `volume`; * prove a similar statement for `ℝ × ℝ`. ## References * [Herbert Federer, Geometric Measure Theory, Chapter 2.10][Federer1996] ## Tags Hausdorff measure, measure, metric measure -/ open_locale nnreal ennreal topological_space big_operators open emetric set function filter encodable finite_dimensional topological_space noncomputable theory variables {ι X Y : Type*} [emetric_space X] [emetric_space Y] namespace measure_theory namespace outer_measure /-! ### Metric outer measures In this section we define metric outer measures and prove Caratheodory theorem: a metric outer measure has the Caratheodory property. -/ /-- We say that an outer measure `μ` in an (e)metric space is *metric* if `μ (s ∪ t) = μ s + μ t` for any two metric separated sets `s`, `t`. -/ def is_metric (μ : outer_measure X) : Prop := ∀ (s t : set X), is_metric_separated s t → μ (s ∪ t) = μ s + μ t namespace is_metric variables {μ : outer_measure X} /-- A metric outer measure is additive on a finite set of pairwise metric separated sets. -/ lemma finset_Union_of_pairwise_separated (hm : is_metric μ) {I : finset ι} {s : ι → set X} (hI : ∀ (i ∈ I) (j ∈ I), i ≠ j → is_metric_separated (s i) (s j)) : μ (⋃ i ∈ I, s i) = ∑ i in I, μ (s i) := begin classical, induction I using finset.induction_on with i I hiI ihI hI, { simp }, simp only [finset.mem_insert] at hI, rw [finset.set_bUnion_insert, hm, ihI, finset.sum_insert hiI], exacts [λ i hi j hj hij, (hI i (or.inr hi) j (or.inr hj) hij), is_metric_separated.finset_Union_right (λ j hj, hI i (or.inl rfl) j (or.inr hj) (ne_of_mem_of_not_mem hj hiI).symm)] end /-- Caratheodory theorem. If `m` is a metric outer measure, then every Borel measurable set `t` is Caratheodory measurable: for any (not necessarily measurable) set `s` we have `μ (s ∩ t) + μ (s \ t) = μ s`. -/ lemma borel_le_caratheodory (hm : is_metric μ) : borel X ≤ μ.caratheodory := begin rw [borel_eq_generate_from_is_closed], refine measurable_space.generate_from_le (λ t ht, μ.is_caratheodory_iff_le.2 $ λ s, _), set S : ℕ → set X := λ n, {x ∈ s | (↑n)⁻¹ ≤ inf_edist x t}, have n0 : ∀ {n : ℕ}, (n⁻¹ : ℝ≥0∞) ≠ 0, from λ n, ennreal.inv_ne_zero.2 (ennreal.nat_ne_top _), have Ssep : ∀ n, is_metric_separated (S n) t, from λ n, ⟨n⁻¹, n0, λ x hx y hy, hx.2.trans $ inf_edist_le_edist_of_mem hy⟩, have Ssep' : ∀ n, is_metric_separated (S n) (s ∩ t), from λ n, (Ssep n).mono subset.rfl (inter_subset_right _ _), have S_sub : ∀ n, S n ⊆ s \ t, from λ n, subset_inter (inter_subset_left _ _) (Ssep n).subset_compl_right, have hSs : ∀ n, μ (s ∩ t) + μ (S n) ≤ μ s, from λ n, calc μ (s ∩ t) + μ (S n) = μ (s ∩ t ∪ S n) : eq.symm $ hm _ _ $ (Ssep' n).symm ... ≤ μ (s ∩ t ∪ s \ t) : by { mono*, exact le_rfl } ... = μ s : by rw [inter_union_diff], have Union_S : (⋃ n, S n) = s \ t, { refine subset.antisymm (Union_subset S_sub) _, rintro x ⟨hxs, hxt⟩, rw mem_iff_inf_edist_zero_of_closed ht at hxt, rcases ennreal.exists_inv_nat_lt hxt with ⟨n, hn⟩, exact mem_Union.2 ⟨n, hxs, hn.le⟩ }, /- Now we have `∀ n, μ (s ∩ t) + μ (S n) ≤ μ s` and we need to prove `μ (s ∩ t) + μ (⋃ n, S n) ≤ μ s`. We can't pass to the limit because `μ` is only an outer measure. -/ by_cases htop : μ (s \ t) = ∞, { rw [htop, ennreal.add_top, ← htop], exact μ.mono (diff_subset _ _) }, suffices : μ (⋃ n, S n) ≤ ⨆ n, μ (S n), calc μ (s ∩ t) + μ (s \ t) = μ (s ∩ t) + μ (⋃ n, S n) : by rw Union_S ... ≤ μ (s ∩ t) + ⨆ n, μ (S n) : add_le_add le_rfl this ... = ⨆ n, μ (s ∩ t) + μ (S n) : ennreal.add_supr ... ≤ μ s : supr_le hSs, /- It suffices to show that `∑' k, μ (S (k + 1) \ S k) ≠ ∞`. Indeed, if we have this, then for all `N` we have `μ (⋃ n, S n) ≤ μ (S N) + ∑' k, m (S (N + k + 1) \ S (N + k))` and the second term tends to zero, see `outer_measure.Union_nat_of_monotone_of_tsum_ne_top` for details. -/ have : ∀ n, S n ⊆ S (n + 1), from λ n x hx, ⟨hx.1, le_trans (ennreal.inv_le_inv.2 $ ennreal.coe_nat_le_coe_nat.2 n.le_succ) hx.2⟩, refine (μ.Union_nat_of_monotone_of_tsum_ne_top this _).le, clear this, /- While the sets `S (k + 1) \ S k` are not pairwise metric separated, the sets in each subsequence `S (2 * k + 1) \ S (2 * k)` and `S (2 * k + 2) \ S (2 * k)` are metric separated, so `m` is additive on each of those sequences. -/ rw [← tsum_even_add_odd ennreal.summable ennreal.summable, ennreal.add_ne_top], suffices : ∀ a, (∑' (k : ℕ), μ (S (2 * k + 1 + a) \ S (2 * k + a))) ≠ ∞, from ⟨by simpa using this 0, by simpa using this 1⟩, refine λ r, ne_top_of_le_ne_top htop _, rw [← Union_S, ennreal.tsum_eq_supr_nat, supr_le_iff], intro n, rw [← hm.finset_Union_of_pairwise_separated], { exact μ.mono (Union_subset $ λ i, Union_subset $ λ hi x hx, mem_Union.2 ⟨_, hx.1⟩) }, suffices : ∀ i j, i < j → is_metric_separated (S (2 * i + 1 + r)) (s \ S (2 * j + r)), from λ i _ j _ hij, hij.lt_or_lt.elim (λ h, (this i j h).mono (inter_subset_left _ _) (λ x hx, ⟨hx.1.1, hx.2⟩)) (λ h, (this j i h).symm.mono (λ x hx, ⟨hx.1.1, hx.2⟩) (inter_subset_left _ _)), intros i j hj, have A : ((↑(2 * j + r))⁻¹ : ℝ≥0∞) < (↑(2 * i + 1 + r))⁻¹, by { rw [ennreal.inv_lt_inv, ennreal.coe_nat_lt_coe_nat], linarith }, refine ⟨(↑(2 * i + 1 + r))⁻¹ - (↑(2 * j + r))⁻¹, by simpa using A, λ x hx y hy, _⟩, have : inf_edist y t < (↑(2 * j + r))⁻¹, from not_le.1 (λ hle, hy.2 ⟨hy.1, hle⟩), rcases inf_edist_lt_iff.mp this with ⟨z, hzt, hyz⟩, have hxz : (↑(2 * i + 1 + r))⁻¹ ≤ edist x z, from le_inf_edist.1 hx.2 _ hzt, apply ennreal.le_of_add_le_add_right hyz.ne_top, refine le_trans _ (edist_triangle _ _ _), refine (add_le_add le_rfl hyz.le).trans (eq.trans_le _ hxz), rw [tsub_add_cancel_of_le A.le] end lemma le_caratheodory [measurable_space X] [borel_space X] (hm : is_metric μ) : ‹measurable_space X› ≤ μ.caratheodory := by { rw @borel_space.measurable_eq X _ _, exact hm.borel_le_caratheodory } end is_metric /-! ### Constructors of metric outer measures In this section we provide constructors `measure_theory.outer_measure.mk_metric'` and `measure_theory.outer_measure.mk_metric` and prove that these outer measures are metric outer measures. We also prove basic lemmas about `map`/`comap` of these measures. -/ /-- Auxiliary definition for `outer_measure.mk_metric'`: given a function on sets `m : set X → ℝ≥0∞`, returns the maximal outer measure `μ` such that `μ s ≤ m s` for any set `s` of diameter at most `r`.-/ def mk_metric'.pre (m : set X → ℝ≥0∞) (r : ℝ≥0∞) : outer_measure X := bounded_by $ extend (λ s (hs : diam s ≤ r), m s) /-- Given a function `m : set X → ℝ≥0∞`, `mk_metric' m` is the supremum of `mk_metric'.pre m r` over `r > 0`. Equivalently, it is the limit of `mk_metric'.pre m r` as `r` tends to zero from the right. -/ def mk_metric' (m : set X → ℝ≥0∞) : outer_measure X := ⨆ r > 0, mk_metric'.pre m r /-- Given a function `m : ℝ≥0∞ → ℝ≥0∞` and `r > 0`, let `μ r` be the maximal outer measure such that `μ s ≤ m (emetric.diam s)` whenever `emetric.diam s < r`. Then `mk_metric m = ⨆ r > 0, μ r`. -/ def mk_metric (m : ℝ≥0∞ → ℝ≥0∞) : outer_measure X := mk_metric' (λ s, m (diam s)) namespace mk_metric' variables {m : set X → ℝ≥0∞} {r : ℝ≥0∞} {μ : outer_measure X} {s : set X} lemma le_pre : μ ≤ pre m r ↔ ∀ s : set X, diam s ≤ r → μ s ≤ m s := by simp only [pre, le_bounded_by, extend, le_infi_iff] lemma pre_le (hs : diam s ≤ r) : pre m r s ≤ m s := (bounded_by_le _).trans $ infi_le _ hs lemma mono_pre (m : set X → ℝ≥0∞) {r r' : ℝ≥0∞} (h : r ≤ r') : pre m r' ≤ pre m r := le_pre.2 $ λ s hs, pre_le (hs.trans h) lemma mono_pre_nat (m : set X → ℝ≥0∞) : monotone (λ k : ℕ, pre m k⁻¹) := λ k l h, le_pre.2 $ λ s hs, pre_le (hs.trans $ by simpa) lemma tendsto_pre (m : set X → ℝ≥0∞) (s : set X) : tendsto (λ r, pre m r s) (𝓝[>] 0) (𝓝 $ mk_metric' m s) := begin rw [← map_coe_Ioi_at_bot, tendsto_map'_iff], simp only [mk_metric', outer_measure.supr_apply, supr_subtype'], exact tendsto_at_bot_supr (λ r r' hr, mono_pre _ hr _) end lemma tendsto_pre_nat (m : set X → ℝ≥0∞) (s : set X) : tendsto (λ n : ℕ, pre m n⁻¹ s) at_top (𝓝 $ mk_metric' m s) := begin refine (tendsto_pre m s).comp (tendsto_inf.2 ⟨ennreal.tendsto_inv_nat_nhds_zero, _⟩), refine tendsto_principal.2 (eventually_of_forall $ λ n, _), simp end lemma eq_supr_nat (m : set X → ℝ≥0∞) : mk_metric' m = ⨆ n : ℕ, mk_metric'.pre m n⁻¹ := begin ext1 s, rw supr_apply, refine tendsto_nhds_unique (mk_metric'.tendsto_pre_nat m s) (tendsto_at_top_supr $ λ k l hkl, mk_metric'.mono_pre_nat m hkl s) end /-- `measure_theory.outer_measure.mk_metric'.pre m r` is a trimmed measure provided that `m (closure s) = m s` for any set `s`. -/ lemma trim_pre [measurable_space X] [opens_measurable_space X] (m : set X → ℝ≥0∞) (hcl : ∀ s, m (closure s) = m s) (r : ℝ≥0∞) : (pre m r).trim = pre m r := begin refine le_antisymm (le_pre.2 $ λ s hs, _) (le_trim _), rw trim_eq_infi, refine (infi_le_of_le (closure s) $ infi_le_of_le subset_closure $ infi_le_of_le measurable_set_closure ((pre_le _).trans_eq (hcl _))), rwa diam_closure end end mk_metric' /-- An outer measure constructed using `outer_measure.mk_metric'` is a metric outer measure. -/ lemma mk_metric'_is_metric (m : set X → ℝ≥0∞) : (mk_metric' m).is_metric := begin rintros s t ⟨r, r0, hr⟩, refine tendsto_nhds_unique_of_eventually_eq (mk_metric'.tendsto_pre _ _) ((mk_metric'.tendsto_pre _ _).add (mk_metric'.tendsto_pre _ _)) _, rw [← pos_iff_ne_zero] at r0, filter_upwards [Ioo_mem_nhds_within_Ioi ⟨le_rfl, r0⟩], rintro ε ⟨ε0, εr⟩, refine bounded_by_union_of_top_of_nonempty_inter _, rintro u ⟨x, hxs, hxu⟩ ⟨y, hyt, hyu⟩, have : ε < diam u, from εr.trans_le ((hr x hxs y hyt).trans $ edist_le_diam_of_mem hxu hyu), exact infi_eq_top.2 (λ h, (this.not_le h).elim) end /-- If `c ∉ {0, ∞}` and `m₁ d ≤ c * m₂ d` for `d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[≥] 0]` to state this), then `mk_metric m₁ hm₁ ≤ c • mk_metric m₂ hm₂`. -/ lemma mk_metric_mono_smul {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} {c : ℝ≥0∞} (hc : c ≠ ∞) (h0 : c ≠ 0) (hle : m₁ ≤ᶠ[𝓝[≥] 0] c • m₂) : (mk_metric m₁ : outer_measure X) ≤ c • mk_metric m₂ := begin classical, rcases (mem_nhds_within_Ici_iff_exists_Ico_subset' ennreal.zero_lt_one).1 hle with ⟨r, hr0, hr⟩, refine λ s, le_of_tendsto_of_tendsto (mk_metric'.tendsto_pre _ s) (ennreal.tendsto.const_mul (mk_metric'.tendsto_pre _ s) (or.inr hc)) (mem_of_superset (Ioo_mem_nhds_within_Ioi ⟨le_rfl, hr0⟩) (λ r' hr', _)), simp only [mem_set_of_eq, mk_metric'.pre, ring_hom.id_apply], rw [←smul_eq_mul, ← smul_apply, smul_bounded_by hc], refine le_bounded_by.2 (λ t, (bounded_by_le _).trans _) _, simp only [smul_eq_mul, pi.smul_apply, extend, infi_eq_if], split_ifs with ht ht, { apply hr, exact ⟨zero_le _, ht.trans_lt hr'.2⟩ }, { simp [h0] } end /-- If `m₁ d ≤ m₂ d` for `d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[≥] 0]` to state this), then `mk_metric m₁ hm₁ ≤ mk_metric m₂ hm₂`-/ lemma mk_metric_mono {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} (hle : m₁ ≤ᶠ[𝓝[≥] 0] m₂) : (mk_metric m₁ : outer_measure X) ≤ mk_metric m₂ := by { convert mk_metric_mono_smul ennreal.one_ne_top ennreal.zero_lt_one.ne' _; simp * } lemma isometry_comap_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) {f : X → Y} (hf : isometry f) (H : monotone m ∨ surjective f) : comap f (mk_metric m) = mk_metric m := begin simp only [mk_metric, mk_metric', mk_metric'.pre, induced_outer_measure, comap_supr], refine surjective_id.supr_congr id (λ ε, surjective_id.supr_congr id $ λ hε, _), rw comap_bounded_by _ (H.imp (λ h_mono, _) id), { congr' with s : 1, apply extend_congr, { simp [hf.ediam_image] }, { intros, simp [hf.injective.subsingleton_image_iff, hf.ediam_image] } }, { assume s t hst, simp only [extend, le_infi_iff], assume ht, apply le_trans _ (h_mono (diam_mono hst)), simp only [(diam_mono hst).trans ht, le_refl, cinfi_pos] } end lemma isometry_map_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) {f : X → Y} (hf : isometry f) (H : monotone m ∨ surjective f) : map f (mk_metric m) = restrict (range f) (mk_metric m) := by rw [← isometry_comap_mk_metric _ hf H, map_comap] lemma isometric_comap_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (f : X ≃ᵢ Y) : comap f (mk_metric m) = mk_metric m := isometry_comap_mk_metric _ f.isometry (or.inr f.surjective) lemma isometric_map_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (f : X ≃ᵢ Y) : map f (mk_metric m) = mk_metric m := by rw [← isometric_comap_mk_metric _ f, map_comap_of_surjective f.surjective] lemma trim_mk_metric [measurable_space X] [borel_space X] (m : ℝ≥0∞ → ℝ≥0∞) : (mk_metric m : outer_measure X).trim = mk_metric m := begin simp only [mk_metric, mk_metric'.eq_supr_nat, trim_supr], congr' 1 with n : 1, refine mk_metric'.trim_pre _ (λ s, _) _, simp end lemma le_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (μ : outer_measure X) (r : ℝ≥0∞) (h0 : 0 < r) (hr : ∀ s, diam s ≤ r → μ s ≤ m (diam s)) : μ ≤ mk_metric m := le_supr₂_of_le r h0 $ mk_metric'.le_pre.2 $ λ s hs, hr _ hs end outer_measure /-! ### Metric measures In this section we use `measure_theory.outer_measure.to_measure` and theorems about `measure_theory.outer_measure.mk_metric'`/`measure_theory.outer_measure.mk_metric` to define `measure_theory.measure.mk_metric'`/`measure_theory.measure.mk_metric`. We also restate some lemmas about metric outer measures for metric measures. -/ namespace measure variables [measurable_space X] [borel_space X] /-- Given a function `m : set X → ℝ≥0∞`, `mk_metric' m` is the supremum of `μ r` over `r > 0`, where `μ r` is the maximal outer measure `μ` such that `μ s ≤ m s` for all `s`. While each `μ r` is an *outer* measure, the supremum is a measure. -/ def mk_metric' (m : set X → ℝ≥0∞) : measure X := (outer_measure.mk_metric' m).to_measure (outer_measure.mk_metric'_is_metric _).le_caratheodory /-- Given a function `m : ℝ≥0∞ → ℝ≥0∞`, `mk_metric m` is the supremum of `μ r` over `r > 0`, where `μ r` is the maximal outer measure `μ` such that `μ s ≤ m s` for all sets `s` that contain at least two points. While each `mk_metric'.pre` is an *outer* measure, the supremum is a measure. -/ def mk_metric (m : ℝ≥0∞ → ℝ≥0∞) : measure X := (outer_measure.mk_metric m).to_measure (outer_measure.mk_metric'_is_metric _).le_caratheodory @[simp] lemma mk_metric'_to_outer_measure (m : set X → ℝ≥0∞) : (mk_metric' m).to_outer_measure = (outer_measure.mk_metric' m).trim := rfl @[simp] lemma mk_metric_to_outer_measure (m : ℝ≥0∞ → ℝ≥0∞) : (mk_metric m : measure X).to_outer_measure = outer_measure.mk_metric m := outer_measure.trim_mk_metric m end measure lemma outer_measure.coe_mk_metric [measurable_space X] [borel_space X] (m : ℝ≥0∞ → ℝ≥0∞) : ⇑(outer_measure.mk_metric m : outer_measure X) = measure.mk_metric m := by rw [← measure.mk_metric_to_outer_measure, coe_to_outer_measure] namespace measure variables [measurable_space X] [borel_space X] /-- If `c ∉ {0, ∞}` and `m₁ d ≤ c * m₂ d` for `d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[≥] 0]` to state this), then `mk_metric m₁ hm₁ ≤ c • mk_metric m₂ hm₂`. -/ lemma mk_metric_mono_smul {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} {c : ℝ≥0∞} (hc : c ≠ ∞) (h0 : c ≠ 0) (hle : m₁ ≤ᶠ[𝓝[≥] 0] c • m₂) : (mk_metric m₁ : measure X) ≤ c • mk_metric m₂ := begin intros s hs, rw [← outer_measure.coe_mk_metric, coe_smul, ← outer_measure.coe_mk_metric], exact outer_measure.mk_metric_mono_smul hc h0 hle s end /-- If `m₁ d ≤ m₂ d` for `d < ε` for some `ε > 0` (we use `≤ᶠ[𝓝[≥] 0]` to state this), then `mk_metric m₁ hm₁ ≤ mk_metric m₂ hm₂`-/ lemma mk_metric_mono {m₁ m₂ : ℝ≥0∞ → ℝ≥0∞} (hle : m₁ ≤ᶠ[𝓝[≥] 0] m₂) : (mk_metric m₁ : measure X) ≤ mk_metric m₂ := by { convert mk_metric_mono_smul ennreal.one_ne_top ennreal.zero_lt_one.ne' _; simp * } /-- A formula for `measure_theory.measure.mk_metric`. -/ lemma mk_metric_apply (m : ℝ≥0∞ → ℝ≥0∞) (s : set X) : mk_metric m s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (h : s ⊆ Union t) (h' : ∀ n, diam (t n) ≤ r), ∑' n, ⨆ (h : (t n).nonempty), m (diam (t n)) := begin -- We mostly unfold the definitions but we need to switch the order of `∑'` and `⨅` classical, simp only [← outer_measure.coe_mk_metric, outer_measure.mk_metric, outer_measure.mk_metric', outer_measure.supr_apply, outer_measure.mk_metric'.pre, outer_measure.bounded_by_apply, extend], refine surjective_id.supr_congr (λ r, r) (λ r, supr_congr_Prop iff.rfl $ λ hr, surjective_id.infi_congr _ $ λ t, infi_congr_Prop iff.rfl $ λ ht, _), dsimp, by_cases htr : ∀ n, diam (t n) ≤ r, { rw [infi_eq_if, if_pos htr], congr' 1 with n : 1, simp only [infi_eq_if, htr n, id, if_true, supr_and'] }, { rw [infi_eq_if, if_neg htr], push_neg at htr, rcases htr with ⟨n, hn⟩, refine ennreal.tsum_eq_top_of_eq_top ⟨n, _⟩, rw [supr_eq_if, if_pos, infi_eq_if, if_neg], exact hn.not_le, rcases diam_pos_iff.1 ((zero_le r).trans_lt hn) with ⟨x, hx, -⟩, exact ⟨x, hx⟩ } end lemma le_mk_metric (m : ℝ≥0∞ → ℝ≥0∞) (μ : measure X) (ε : ℝ≥0∞) (h₀ : 0 < ε) (h : ∀ s : set X, diam s ≤ ε → μ s ≤ m (diam s)) : μ ≤ mk_metric m := begin rw [← to_outer_measure_le, mk_metric_to_outer_measure], exact outer_measure.le_mk_metric m μ.to_outer_measure ε h₀ h end /-- To bound the Hausdorff measure (or, more generally, for a measure defined using `measure_theory.measure.mk_metric`) of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of countable types. -/ lemma mk_metric_le_liminf_tsum {β : Type*} {ι : β → Type*} [∀ n, countable (ι n)] (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) (m : ℝ≥0∞ → ℝ≥0∞) : mk_metric m s ≤ liminf (λ n, ∑' i, m (diam (t n i))) l := begin haveI : Π n, encodable (ι n) := λ n, encodable.of_countable _, simp only [mk_metric_apply], refine supr₂_le (λ ε hε, _), refine le_of_forall_le_of_dense (λ c hc, _), rcases ((frequently_lt_of_liminf_lt (by apply_auto_param) hc).and_eventually ((hr.eventually (gt_mem_nhds hε)).and (ht.and hst))).exists with ⟨n, hn, hrn, htn, hstn⟩, set u : ℕ → set X := λ j, ⋃ b ∈ decode₂ (ι n) j, t n b, refine infi₂_le_of_le u (by rwa Union_decode₂) _, refine infi_le_of_le (λ j, _) _, { rw emetric.diam_Union_mem_option, exact supr₂_le (λ _ _, (htn _).trans hrn.le) }, { calc (∑' (j : ℕ), ⨆ (h : (u j).nonempty), m (diam (u j))) = _ : tsum_Union_decode₂ (λ t : set X, ⨆ (h : t.nonempty), m (diam t)) (by simp) _ ... ≤ ∑' (i : ι n), m (diam (t n i)) : ennreal.tsum_le_tsum (λ b, supr_le $ λ htb, le_rfl) ... ≤ c : hn.le } end /-- To bound the Hausdorff measure (or, more generally, for a measure defined using `measure_theory.measure.mk_metric`) of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of finite types. -/ lemma mk_metric_le_liminf_sum {β : Type*} {ι : β → Type*} [hι : ∀ n, fintype (ι n)] (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) (m : ℝ≥0∞ → ℝ≥0∞) : mk_metric m s ≤ liminf (λ n, ∑ i, m (diam (t n i))) l := by simpa only [tsum_fintype] using mk_metric_le_liminf_tsum s r hr t ht hst m /-! ### Hausdorff measure and Hausdorff dimension -/ /-- Hausdorff measure on an (e)metric space. -/ def hausdorff_measure (d : ℝ) : measure X := mk_metric (λ r, r ^ d) localized "notation (name := hausdorff_measure) `μH[` d `]` := measure_theory.measure.hausdorff_measure d" in measure_theory lemma le_hausdorff_measure (d : ℝ) (μ : measure X) (ε : ℝ≥0∞) (h₀ : 0 < ε) (h : ∀ s : set X, diam s ≤ ε → μ s ≤ diam s ^ d) : μ ≤ μH[d] := le_mk_metric _ μ ε h₀ h /-- A formula for `μH[d] s`. -/ lemma hausdorff_measure_apply (d : ℝ) (s : set X) : μH[d] s = ⨆ (r : ℝ≥0∞) (hr : 0 < r), ⨅ (t : ℕ → set X) (hts : s ⊆ ⋃ n, t n) (ht : ∀ n, diam (t n) ≤ r), ∑' n, ⨆ (h : (t n).nonempty), (diam (t n)) ^ d := mk_metric_apply _ _ /-- To bound the Hausdorff measure of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of countable types. -/ lemma hausdorff_measure_le_liminf_tsum {β : Type*} {ι : β → Type*} [hι : ∀ n, countable (ι n)] (d : ℝ) (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) : μH[d] s ≤ liminf (λ n, ∑' i, diam (t n i) ^ d) l := mk_metric_le_liminf_tsum s r hr t ht hst _ /-- To bound the Hausdorff measure of a set, one may use coverings with maximum diameter tending to `0`, indexed by any sequence of finite types. -/ lemma hausdorff_measure_le_liminf_sum {β : Type*} {ι : β → Type*} [hι : ∀ n, fintype (ι n)] (d : ℝ) (s : set X) {l : filter β} (r : β → ℝ≥0∞) (hr : tendsto r l (𝓝 0)) (t : Π (n : β), ι n → set X) (ht : ∀ᶠ n in l, ∀ i, diam (t n i) ≤ r n) (hst : ∀ᶠ n in l, s ⊆ ⋃ i, t n i) : μH[d] s ≤ liminf (λ n, ∑ i, diam (t n i) ^ d) l := mk_metric_le_liminf_sum s r hr t ht hst _ /-- If `d₁ < d₂`, then for any set `s` we have either `μH[d₂] s = 0`, or `μH[d₁] s = ∞`. -/ lemma hausdorff_measure_zero_or_top {d₁ d₂ : ℝ} (h : d₁ < d₂) (s : set X) : μH[d₂] s = 0 ∨ μH[d₁] s = ∞ := begin by_contra' H, suffices : ∀ (c : ℝ≥0), c ≠ 0 → μH[d₂] s ≤ c * μH[d₁] s, { rcases ennreal.exists_nnreal_pos_mul_lt H.2 H.1 with ⟨c, hc0, hc⟩, exact hc.not_le (this c (pos_iff_ne_zero.1 hc0)) }, intros c hc, refine le_iff'.1 (mk_metric_mono_smul ennreal.coe_ne_top (by exact_mod_cast hc) _) s, have : 0 < (c ^ (d₂ - d₁)⁻¹ : ℝ≥0∞), { rw [ennreal.coe_rpow_of_ne_zero hc, pos_iff_ne_zero, ne.def, ennreal.coe_eq_zero, nnreal.rpow_eq_zero_iff], exact mt and.left hc }, filter_upwards [Ico_mem_nhds_within_Ici ⟨le_rfl, this⟩], rintro r ⟨hr₀, hrc⟩, lift r to ℝ≥0 using ne_top_of_lt hrc, rw [pi.smul_apply, smul_eq_mul, ← ennreal.div_le_iff_le_mul (or.inr ennreal.coe_ne_top) (or.inr $ mt ennreal.coe_eq_zero.1 hc)], rcases eq_or_ne r 0 with rfl|hr₀, { rcases lt_or_le 0 d₂ with h₂|h₂, { simp only [h₂, ennreal.zero_rpow_of_pos, zero_le', ennreal.coe_nonneg, ennreal.zero_div, ennreal.coe_zero] }, { simp only [h.trans_le h₂, ennreal.div_top, zero_le', ennreal.coe_nonneg, ennreal.zero_rpow_of_neg, ennreal.coe_zero] } }, { have : (r : ℝ≥0∞) ≠ 0, by simpa only [ennreal.coe_eq_zero, ne.def] using hr₀, rw [← ennreal.rpow_sub _ _ this ennreal.coe_ne_top], refine (ennreal.rpow_lt_rpow hrc (sub_pos.2 h)).le.trans _, rw [← ennreal.rpow_mul, inv_mul_cancel (sub_pos.2 h).ne', ennreal.rpow_one], exact le_rfl } end /-- Hausdorff measure `μH[d] s` is monotone in `d`. -/ lemma hausdorff_measure_mono {d₁ d₂ : ℝ} (h : d₁ ≤ d₂) (s : set X) : μH[d₂] s ≤ μH[d₁] s := begin rcases h.eq_or_lt with rfl|h, { exact le_rfl }, cases hausdorff_measure_zero_or_top h s with hs hs, { rw hs, exact zero_le _ }, { rw hs, exact le_top } end variables (X) lemma no_atoms_hausdorff {d : ℝ} (hd : 0 < d) : has_no_atoms (hausdorff_measure d : measure X) := begin refine ⟨λ x, _⟩, rw [← nonpos_iff_eq_zero, hausdorff_measure_apply], refine supr₂_le (λ ε ε0, infi₂_le_of_le (λ n, {x}) _ $ infi_le_of_le (λ n, _) _), { exact subset_Union (λ n, {x} : ℕ → set X) 0 }, { simp only [emetric.diam_singleton, zero_le] }, { simp [hd] } end variables {X} @[simp] lemma hausdorff_measure_zero_singleton (x : X) : μH[0] ({x} : set X) = 1 := begin apply le_antisymm, { let r : ℕ → ℝ≥0∞ := λ _, 0, let t : ℕ → unit → set X := λ n _, {x}, have ht : ∀ᶠ n in at_top, ∀ i, diam (t n i) ≤ r n, by simp only [implies_true_iff, eq_self_iff_true, diam_singleton, eventually_at_top, nonpos_iff_eq_zero, exists_const], simpa [liminf_const] using hausdorff_measure_le_liminf_sum 0 {x} r tendsto_const_nhds t ht }, { rw hausdorff_measure_apply, suffices : (1 : ℝ≥0∞) ≤ ⨅ (t : ℕ → set X) (hts : {x} ⊆ ⋃ n, t n) (ht : ∀ n, diam (t n) ≤ 1), ∑' n, ⨆ (h : (t n).nonempty), (diam (t n)) ^ (0 : ℝ), { apply le_trans this _, convert le_supr₂ (1 : ℝ≥0∞) (ennreal.zero_lt_one), refl }, simp only [ennreal.rpow_zero, le_infi_iff], assume t hst h't, rcases mem_Union.1 (hst (mem_singleton x)) with ⟨m, hm⟩, have A : (t m).nonempty := ⟨x, hm⟩, calc (1 : ℝ≥0∞) = ⨆ (h : (t m).nonempty), 1 : by simp only [A, csupr_pos] ... ≤ ∑' n, ⨆ (h : (t n).nonempty), 1 : ennreal.le_tsum _ } end lemma one_le_hausdorff_measure_zero_of_nonempty {s : set X} (h : s.nonempty) : 1 ≤ μH[0] s := begin rcases h with ⟨x, hx⟩, calc (1 : ℝ≥0∞) = μH[0] ({x} : set X) : (hausdorff_measure_zero_singleton x).symm ... ≤ μH[0] s : measure_mono (singleton_subset_iff.2 hx) end lemma hausdorff_measure_le_one_of_subsingleton {s : set X} (hs : s.subsingleton) {d : ℝ} (hd : 0 ≤ d) : μH[d] s ≤ 1 := begin rcases eq_empty_or_nonempty s with rfl|⟨x, hx⟩, { simp only [measure_empty, zero_le] }, { rw (subsingleton_iff_singleton hx).1 hs, rcases eq_or_lt_of_le hd with rfl|dpos, { simp only [le_refl, hausdorff_measure_zero_singleton] }, { haveI := no_atoms_hausdorff X dpos, simp only [zero_le, measure_singleton] } } end end measure open_locale measure_theory open measure /-! ### Hausdorff measure and Lebesgue measure -/ /-- In the space `ι → ℝ`, Hausdorff measure coincides exactly with Lebesgue measure. -/ @[simp] theorem hausdorff_measure_pi_real {ι : Type*} [fintype ι] : (μH[fintype.card ι] : measure (ι → ℝ)) = volume := begin classical, -- it suffices to check that the two measures coincide on products of rational intervals refine (pi_eq_generate_from (λ i, real.borel_eq_generate_from_Ioo_rat.symm) (λ i, real.is_pi_system_Ioo_rat) (λ i, real.finite_spanning_sets_in_Ioo_rat _) _).symm, simp only [mem_Union, mem_singleton_iff], -- fix such a product `s` of rational intervals, of the form `Π (a i, b i)`. intros s hs, choose a b H using hs, obtain rfl : s = λ i, Ioo (a i) (b i), from funext (λ i, (H i).2), replace H := λ i, (H i).1, apply le_antisymm _, -- first check that `volume s ≤ μH s` { have Hle : volume ≤ (μH[fintype.card ι] : measure (ι → ℝ)), { refine le_hausdorff_measure _ _ ∞ ennreal.coe_lt_top (λ s _, _), rw [ennreal.rpow_nat_cast], exact real.volume_pi_le_diam_pow s }, rw [← volume_pi_pi (λ i, Ioo (a i : ℝ) (b i))], exact measure.le_iff'.1 Hle _ }, /- For the other inequality `μH s ≤ volume s`, we use a covering of `s` by sets of small diameter `1/n`, namely cubes with left-most point of the form `a i + f i / n` with `f i` ranging between `0` and `⌈(b i - a i) * n⌉`. Their number is asymptotic to `n^d * Π (b i - a i)`. -/ have I : ∀ i, 0 ≤ (b i : ℝ) - a i := λ i, by simpa only [sub_nonneg, rat.cast_le] using (H i).le, let γ := λ (n : ℕ), (Π (i : ι), fin ⌈((b i : ℝ) - a i) * n⌉₊), let t : Π (n : ℕ), γ n → set (ι → ℝ) := λ n f, set.pi univ (λ i, Icc (a i + f i / n) (a i + (f i + 1) / n)), have A : tendsto (λ (n : ℕ), 1/(n : ℝ≥0∞)) at_top (𝓝 0), by simp only [one_div, ennreal.tendsto_inv_nat_nhds_zero], have B : ∀ᶠ n in at_top, ∀ (i : γ n), diam (t n i) ≤ 1 / n, { apply eventually_at_top.2 ⟨1, λ n hn, _⟩, assume f, apply diam_pi_le_of_le (λ b, _), simp only [real.ediam_Icc, add_div, ennreal.of_real_div_of_pos (nat.cast_pos.mpr hn), le_refl, add_sub_add_left_eq_sub, add_sub_cancel', ennreal.of_real_one, ennreal.of_real_coe_nat] }, have C : ∀ᶠ n in at_top, set.pi univ (λ (i : ι), Ioo (a i : ℝ) (b i)) ⊆ ⋃ (i : γ n), t n i, { apply eventually_at_top.2 ⟨1, λ n hn, _⟩, have npos : (0 : ℝ) < n := nat.cast_pos.2 hn, assume x hx, simp only [mem_Ioo, mem_univ_pi] at hx, simp only [mem_Union, mem_Ioo, mem_univ_pi, coe_coe], let f : γ n := λ i, ⟨⌊(x i - a i) * n⌋₊, begin apply nat.floor_lt_ceil_of_lt_of_pos, { refine (mul_lt_mul_right npos).2 _, simp only [(hx i).right, sub_lt_sub_iff_right] }, { refine mul_pos _ npos, simpa only [rat.cast_lt, sub_pos] using H i } end⟩, refine ⟨f, λ i, ⟨_, _⟩⟩, { calc (a i : ℝ) + ⌊(x i - a i) * n⌋₊ / n ≤ (a i : ℝ) + ((x i - a i) * n) / n : begin refine add_le_add le_rfl ((div_le_div_right npos).2 _), exact nat.floor_le (mul_nonneg (sub_nonneg.2 (hx i).1.le) npos.le), end ... = x i : by field_simp [npos.ne'] }, { calc x i = (a i : ℝ) + ((x i - a i) * n) / n : by field_simp [npos.ne'] ... ≤ (a i : ℝ) + (⌊(x i - a i) * n⌋₊ + 1) / n : add_le_add le_rfl ((div_le_div_right npos).2 (nat.lt_floor_add_one _).le) } }, calc μH[fintype.card ι] (set.pi univ (λ (i : ι), Ioo (a i : ℝ) (b i))) ≤ liminf (λ (n : ℕ), ∑ (i : γ n), diam (t n i) ^ ↑(fintype.card ι)) at_top : hausdorff_measure_le_liminf_sum _ (set.pi univ (λ i, Ioo (a i : ℝ) (b i))) (λ (n : ℕ), 1/(n : ℝ≥0∞)) A t B C ... ≤ liminf (λ (n : ℕ), ∑ (i : γ n), (1/n) ^ (fintype.card ι)) at_top : begin refine liminf_le_liminf _ (by is_bounded_default), filter_upwards [B] with _ hn, apply finset.sum_le_sum (λ i _, _), rw ennreal.rpow_nat_cast, exact pow_le_pow_of_le_left' (hn i) _, end ... = liminf (λ (n : ℕ), ∏ (i : ι), (⌈((b i : ℝ) - a i) * n⌉₊ : ℝ≥0∞) / n) at_top : begin simp only [finset.card_univ, nat.cast_prod, one_mul, fintype.card_fin, finset.sum_const, nsmul_eq_mul, fintype.card_pi, div_eq_mul_inv, finset.prod_mul_distrib, finset.prod_const] end ... = ∏ (i : ι), volume (Ioo (a i : ℝ) (b i)) : begin simp only [real.volume_Ioo], apply tendsto.liminf_eq, refine ennreal.tendsto_finset_prod_of_ne_top _ (λ i hi, _) (λ i hi, _), { apply tendsto.congr' _ ((ennreal.continuous_of_real.tendsto _).comp ((tendsto_nat_ceil_mul_div_at_top (I i)).comp tendsto_coe_nat_at_top_at_top)), apply eventually_at_top.2 ⟨1, λ n hn, _⟩, simp only [ennreal.of_real_div_of_pos (nat.cast_pos.mpr hn), comp_app, ennreal.of_real_coe_nat] }, { simp only [ennreal.of_real_ne_top, ne.def, not_false_iff] } end end end measure_theory /-! ### Hausdorff measure, Hausdorff dimension, and Hölder or Lipschitz continuous maps -/ open_locale measure_theory open measure_theory measure_theory.measure variables [measurable_space X] [borel_space X] [measurable_space Y] [borel_space Y] namespace holder_on_with variables {C r : ℝ≥0} {f : X → Y} {s t : set X} /-- If `f : X → Y` is Hölder continuous on `s` with a positive exponent `r`, then `μH[d] (f '' s) ≤ C ^ d * μH[r * d] s`. -/ lemma hausdorff_measure_image_le (h : holder_on_with C r f s) (hr : 0 < r) {d : ℝ} (hd : 0 ≤ d) : μH[d] (f '' s) ≤ C ^ d * μH[r * d] s := begin -- We start with the trivial case `C = 0` rcases (zero_le C).eq_or_lt with rfl|hC0, { rcases eq_empty_or_nonempty s with rfl|⟨x, hx⟩, { simp only [measure_empty, nonpos_iff_eq_zero, mul_zero, image_empty] }, have : f '' s = {f x}, { have : (f '' s).subsingleton, by simpa [diam_eq_zero_iff] using h.ediam_image_le, exact (subsingleton_iff_singleton (mem_image_of_mem f hx)).1 this }, rw this, rcases eq_or_lt_of_le hd with rfl|h'd, { simp only [ennreal.rpow_zero, one_mul, mul_zero], rw hausdorff_measure_zero_singleton, exact one_le_hausdorff_measure_zero_of_nonempty ⟨x, hx⟩ }, { haveI := no_atoms_hausdorff Y h'd, simp only [zero_le, measure_singleton] } }, -- Now assume `C ≠ 0` { have hCd0 : (C : ℝ≥0∞) ^ d ≠ 0, by simp [hC0.ne'], have hCd : (C : ℝ≥0∞) ^ d ≠ ∞, by simp [hd], simp only [hausdorff_measure_apply, ennreal.mul_supr, ennreal.mul_infi_of_ne hCd0 hCd, ← ennreal.tsum_mul_left], refine supr_le (λ R, supr_le $ λ hR, _), have : tendsto (λ d : ℝ≥0∞, (C : ℝ≥0∞) * d ^ (r : ℝ)) (𝓝 0) (𝓝 0), from ennreal.tendsto_const_mul_rpow_nhds_zero_of_pos ennreal.coe_ne_top hr, rcases ennreal.nhds_zero_basis_Iic.eventually_iff.1 (this.eventually (gt_mem_nhds hR)) with ⟨δ, δ0, H⟩, refine le_supr₂_of_le δ δ0 (infi₂_mono' $ λ t hst, ⟨λ n, f '' (t n ∩ s), _, infi_mono' $ λ htδ, ⟨λ n, (h.ediam_image_inter_le (t n)).trans (H (htδ n)).le, _⟩⟩), { rw [← image_Union, ← Union_inter], exact image_subset _ (subset_inter hst subset.rfl) }, { apply ennreal.tsum_le_tsum (λ n, _), simp only [supr_le_iff, nonempty_image_iff], assume hft, simp only [nonempty.mono ((t n).inter_subset_left s) hft, csupr_pos], rw [ennreal.rpow_mul, ← ennreal.mul_rpow_of_nonneg _ _ hd], exact ennreal.rpow_le_rpow (h.ediam_image_inter_le _) hd } } end end holder_on_with namespace lipschitz_on_with variables {K : ℝ≥0} {f : X → Y} {s t : set X} /-- If `f : X → Y` is `K`-Lipschitz on `s`, then `μH[d] (f '' s) ≤ K ^ d * μH[d] s`. -/ lemma hausdorff_measure_image_le (h : lipschitz_on_with K f s) {d : ℝ} (hd : 0 ≤ d) : μH[d] (f '' s) ≤ K ^ d * μH[d] s := by simpa only [nnreal.coe_one, one_mul] using h.holder_on_with.hausdorff_measure_image_le zero_lt_one hd end lipschitz_on_with namespace lipschitz_with variables {K : ℝ≥0} {f : X → Y} /-- If `f` is a `K`-Lipschitz map, then it increases the Hausdorff `d`-measures of sets at most by the factor of `K ^ d`.-/ lemma hausdorff_measure_image_le (h : lipschitz_with K f) {d : ℝ} (hd : 0 ≤ d) (s : set X) : μH[d] (f '' s) ≤ K ^ d * μH[d] s := (h.lipschitz_on_with s).hausdorff_measure_image_le hd end lipschitz_with /-! ### Antilipschitz maps do not decrease Hausdorff measures and dimension -/ namespace antilipschitz_with variables {f : X → Y} {K : ℝ≥0} {d : ℝ} lemma hausdorff_measure_preimage_le (hf : antilipschitz_with K f) (hd : 0 ≤ d) (s : set Y) : μH[d] (f ⁻¹' s) ≤ K ^ d * μH[d] s := begin rcases eq_or_ne K 0 with rfl|h0, { rcases eq_empty_or_nonempty (f ⁻¹' s) with hs|⟨x, hx⟩, { simp only [hs, measure_empty, zero_le], }, have : f ⁻¹' s = {x}, { haveI : subsingleton X := hf.subsingleton, have : (f ⁻¹' s).subsingleton, from subsingleton_univ.anti (subset_univ _), exact (subsingleton_iff_singleton hx).1 this }, rw this, rcases eq_or_lt_of_le hd with rfl|h'd, { simp only [ennreal.rpow_zero, one_mul, mul_zero], rw hausdorff_measure_zero_singleton, exact one_le_hausdorff_measure_zero_of_nonempty ⟨f x, hx⟩ }, { haveI := no_atoms_hausdorff X h'd, simp only [zero_le, measure_singleton] } }, have hKd0 : (K : ℝ≥0∞) ^ d ≠ 0, by simp [h0], have hKd : (K : ℝ≥0∞) ^ d ≠ ∞, by simp [hd], simp only [hausdorff_measure_apply, ennreal.mul_supr, ennreal.mul_infi_of_ne hKd0 hKd, ← ennreal.tsum_mul_left], refine supr₂_le (λ ε ε0, _), refine le_supr₂_of_le (ε / K) (by simp [ε0.ne']) _, refine le_infi₂ (λ t hst, le_infi $ λ htε, _), replace hst : f ⁻¹' s ⊆ _ := preimage_mono hst, rw preimage_Union at hst, refine infi₂_le_of_le _ hst (infi_le_of_le (λ n, _) _), { exact (hf.ediam_preimage_le _).trans (ennreal.mul_le_of_le_div' $ htε n) }, { refine ennreal.tsum_le_tsum (λ n, supr_le_iff.2 (λ hft, _)), simp only [nonempty_of_nonempty_preimage hft, csupr_pos], rw [← ennreal.mul_rpow_of_nonneg _ _ hd], exact ennreal.rpow_le_rpow (hf.ediam_preimage_le _) hd } end lemma le_hausdorff_measure_image (hf : antilipschitz_with K f) (hd : 0 ≤ d) (s : set X) : μH[d] s ≤ K ^ d * μH[d] (f '' s) := calc μH[d] s ≤ μH[d] (f ⁻¹' (f '' s)) : measure_mono (subset_preimage_image _ _) ... ≤ K ^ d * μH[d] (f '' s) : hf.hausdorff_measure_preimage_le hd (f '' s) end antilipschitz_with /-! ### Isometries preserve the Hausdorff measure and Hausdorff dimension -/ namespace isometry variables {f : X → Y} {d : ℝ} lemma hausdorff_measure_image (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) (s : set X) : μH[d] (f '' s) = μH[d] s := begin simp only [hausdorff_measure, ← outer_measure.coe_mk_metric, ← outer_measure.comap_apply], rw [outer_measure.isometry_comap_mk_metric _ hf (hd.imp_left _)], exact λ hd x y hxy, ennreal.rpow_le_rpow hxy hd end lemma hausdorff_measure_preimage (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) (s : set Y) : μH[d] (f ⁻¹' s) = μH[d] (s ∩ range f) := by rw [← hf.hausdorff_measure_image hd, image_preimage_eq_inter_range] lemma map_hausdorff_measure (hf : isometry f) (hd : 0 ≤ d ∨ surjective f) : measure.map f μH[d] = (μH[d]).restrict (range f) := begin ext1 s hs, rw [map_apply hf.continuous.measurable hs, restrict_apply hs, hf.hausdorff_measure_preimage hd] end end isometry namespace isometric @[simp] lemma hausdorff_measure_image (e : X ≃ᵢ Y) (d : ℝ) (s : set X) : μH[d] (e '' s) = μH[d] s := e.isometry.hausdorff_measure_image (or.inr e.surjective) s @[simp] lemma hausdorff_measure_preimage (e : X ≃ᵢ Y) (d : ℝ) (s : set Y) : μH[d] (e ⁻¹' s) = μH[d] s := by rw [← e.image_symm, e.symm.hausdorff_measure_image] end isometric
fb776daace1e9773d30d678d59c76d29ed7ebd81
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/measure_theory/group/measure.lean
5207c9ebdc00f618517054f2ac41efe93c535825
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
31,560
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import dynamics.ergodic.measure_preserving import measure_theory.measure.regular import measure_theory.group.measurable_equiv import measure_theory.measure.open_pos import measure_theory.group.action import measure_theory.constructions.prod.basic import topology.continuous_function.cocompact_map /-! # Measures on Groups > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. We develop some properties of measures on (topological) groups * We define properties on measures: measures that are left or right invariant w.r.t. multiplication. * We define the measure `μ.inv : A ↦ μ(A⁻¹)` and show that it is right invariant iff `μ` is left invariant. * We define a class `is_haar_measure μ`, requiring that the measure `μ` is left-invariant, finite on compact sets, and positive on open sets. We also give analogues of all these notions in the additive world. -/ noncomputable theory open_locale nnreal ennreal pointwise big_operators topology open has_inv set function measure_theory.measure filter variables {𝕜 G H : Type*} [measurable_space G] [measurable_space H] namespace measure_theory namespace measure /-- A measure `μ` on a measurable additive group is left invariant if the measure of left translations of a set are equal to the measure of the set itself. -/ class is_add_left_invariant [has_add G] (μ : measure G) : Prop := (map_add_left_eq_self : ∀ g : G, map ((+) g) μ = μ) /-- A measure `μ` on a measurable group is left invariant if the measure of left translations of a set are equal to the measure of the set itself. -/ @[to_additive] class is_mul_left_invariant [has_mul G] (μ : measure G) : Prop := (map_mul_left_eq_self : ∀ g : G, map ((*) g) μ = μ) /-- A measure `μ` on a measurable additive group is right invariant if the measure of right translations of a set are equal to the measure of the set itself. -/ class is_add_right_invariant [has_add G] (μ : measure G) : Prop := (map_add_right_eq_self : ∀ g : G, map (+ g) μ = μ) /-- A measure `μ` on a measurable group is right invariant if the measure of right translations of a set are equal to the measure of the set itself. -/ @[to_additive] class is_mul_right_invariant [has_mul G] (μ : measure G) : Prop := (map_mul_right_eq_self : ∀ g : G, map (* g) μ = μ) end measure open measure section mul variables [has_mul G] {μ : measure G} @[to_additive] lemma map_mul_left_eq_self (μ : measure G) [is_mul_left_invariant μ] (g : G) : map ((*) g) μ = μ := is_mul_left_invariant.map_mul_left_eq_self g @[to_additive] lemma map_mul_right_eq_self (μ : measure G) [is_mul_right_invariant μ] (g : G) : map (* g) μ = μ := is_mul_right_invariant.map_mul_right_eq_self g @[to_additive measure_theory.is_add_left_invariant_smul] instance is_mul_left_invariant_smul [is_mul_left_invariant μ] (c : ℝ≥0∞) : is_mul_left_invariant (c • μ) := ⟨λ g, by rw [measure.map_smul, map_mul_left_eq_self]⟩ @[to_additive measure_theory.is_add_right_invariant_smul] instance is_mul_right_invariant_smul [is_mul_right_invariant μ] (c : ℝ≥0∞) : is_mul_right_invariant (c • μ) := ⟨λ g, by rw [measure.map_smul, map_mul_right_eq_self]⟩ @[to_additive measure_theory.is_add_left_invariant_smul_nnreal] instance is_mul_left_invariant_smul_nnreal [is_mul_left_invariant μ] (c : ℝ≥0) : is_mul_left_invariant (c • μ) := measure_theory.is_mul_left_invariant_smul (c : ℝ≥0∞) @[to_additive measure_theory.is_add_right_invariant_smul_nnreal] instance is_mul_right_invariant_smul_nnreal [is_mul_right_invariant μ] (c : ℝ≥0) : is_mul_right_invariant (c • μ) := measure_theory.is_mul_right_invariant_smul (c : ℝ≥0∞) section has_measurable_mul variables [has_measurable_mul G] @[to_additive] lemma measure_preserving_mul_left (μ : measure G) [is_mul_left_invariant μ] (g : G) : measure_preserving ((*) g) μ μ := ⟨measurable_const_mul g, map_mul_left_eq_self μ g⟩ @[to_additive] lemma measure_preserving.mul_left (μ : measure G) [is_mul_left_invariant μ] (g : G) {X : Type*} [measurable_space X] {μ' : measure X} {f : X → G} (hf : measure_preserving f μ' μ) : measure_preserving (λ x, g * f x) μ' μ := (measure_preserving_mul_left μ g).comp hf @[to_additive] lemma measure_preserving_mul_right (μ : measure G) [is_mul_right_invariant μ] (g : G) : measure_preserving (* g) μ μ := ⟨measurable_mul_const g, map_mul_right_eq_self μ g⟩ @[to_additive] lemma measure_preserving.mul_right (μ : measure G) [is_mul_right_invariant μ] (g : G) {X : Type*} [measurable_space X] {μ' : measure X} {f : X → G} (hf : measure_preserving f μ' μ) : measure_preserving (λ x, f x * g) μ' μ := (measure_preserving_mul_right μ g).comp hf @[to_additive] instance is_mul_left_invariant.smul_invariant_measure [is_mul_left_invariant μ] : smul_invariant_measure G G μ := ⟨λ x s hs, (measure_preserving_mul_left μ x).measure_preimage hs⟩ @[to_additive] instance is_mul_right_invariant.to_smul_invariant_measure_op [μ.is_mul_right_invariant] : smul_invariant_measure Gᵐᵒᵖ G μ := ⟨λ x s hs, (measure_preserving_mul_right μ (mul_opposite.unop x)).measure_preimage hs⟩ @[to_additive] instance subgroup.smul_invariant_measure {G α : Type*} [group G] [mul_action G α] [measurable_space α] {μ : measure α} [smul_invariant_measure G α μ] (H : subgroup G) : smul_invariant_measure H α μ := ⟨λ y s hs, by convert smul_invariant_measure.measure_preimage_smul μ (y : G) hs⟩ /-- An alternative way to prove that `μ` is left invariant under multiplication. -/ @[to_additive /-" An alternative way to prove that `μ` is left invariant under addition. "-/] lemma forall_measure_preimage_mul_iff (μ : measure G) : (∀ (g : G) (A : set G), measurable_set A → μ ((λ h, g * h) ⁻¹' A) = μ A) ↔ is_mul_left_invariant μ := begin transitivity ∀ g, map ((*) g) μ = μ, { simp_rw [measure.ext_iff], refine forall_congr (λ g, forall_congr $ λ A, forall_congr $ λ hA, _), rw [map_apply (measurable_const_mul g) hA] }, exact ⟨λ h, ⟨h⟩, λ h, h.1⟩ end /-- An alternative way to prove that `μ` is right invariant under multiplication. -/ @[to_additive /-" An alternative way to prove that `μ` is right invariant under addition. "-/] lemma forall_measure_preimage_mul_right_iff (μ : measure G) : (∀ (g : G) (A : set G), measurable_set A → μ ((λ h, h * g) ⁻¹' A) = μ A) ↔ is_mul_right_invariant μ := begin transitivity ∀ g, map (* g) μ = μ, { simp_rw [measure.ext_iff], refine forall_congr (λ g, forall_congr $ λ A, forall_congr $ λ hA, _), rw [map_apply (measurable_mul_const g) hA] }, exact ⟨λ h, ⟨h⟩, λ h, h.1⟩ end @[to_additive] instance [is_mul_left_invariant μ] [sigma_finite μ] {H : Type*} [has_mul H] {mH : measurable_space H} {ν : measure H} [has_measurable_mul H] [is_mul_left_invariant ν] [sigma_finite ν] : is_mul_left_invariant (μ.prod ν) := begin constructor, rintros ⟨g, h⟩, change map (prod.map ((*) g) ((*) h)) (μ.prod ν) = μ.prod ν, rw [← map_prod_map _ _ (measurable_const_mul g) (measurable_const_mul h), map_mul_left_eq_self μ g, map_mul_left_eq_self ν h], { rw map_mul_left_eq_self μ g, apply_instance }, { rw map_mul_left_eq_self ν h, apply_instance }, end @[to_additive] instance [is_mul_right_invariant μ] [sigma_finite μ] {H : Type*} [has_mul H] {mH : measurable_space H} {ν : measure H} [has_measurable_mul H] [is_mul_right_invariant ν] [sigma_finite ν] : is_mul_right_invariant (μ.prod ν) := begin constructor, rintros ⟨g, h⟩, change map (prod.map (* g) (* h)) (μ.prod ν) = μ.prod ν, rw [← map_prod_map _ _ (measurable_mul_const g) (measurable_mul_const h), map_mul_right_eq_self μ g, map_mul_right_eq_self ν h], { rw map_mul_right_eq_self μ g, apply_instance }, { rw map_mul_right_eq_self ν h, apply_instance }, end @[to_additive] lemma is_mul_left_invariant_map {H : Type*} [measurable_space H] [has_mul H] [has_measurable_mul H] [is_mul_left_invariant μ] (f : G →ₙ* H) (hf : measurable f) (h_surj : surjective f) : is_mul_left_invariant (measure.map f μ) := begin refine ⟨λ h, _⟩, rw map_map (measurable_const_mul _) hf, obtain ⟨g, rfl⟩ := h_surj h, conv_rhs { rw ← map_mul_left_eq_self μ g }, rw map_map hf (measurable_const_mul _), congr' 2, ext y, simp only [comp_app, map_mul], end end has_measurable_mul end mul section div_inv_monoid variables [div_inv_monoid G] @[to_additive] lemma map_div_right_eq_self (μ : measure G) [is_mul_right_invariant μ] (g : G) : map (/ g) μ = μ := by simp_rw [div_eq_mul_inv, map_mul_right_eq_self μ g⁻¹] end div_inv_monoid section group variables [group G] [has_measurable_mul G] @[to_additive] lemma measure_preserving_div_right (μ : measure G) [is_mul_right_invariant μ] (g : G) : measure_preserving (/ g) μ μ := by simp_rw [div_eq_mul_inv, measure_preserving_mul_right μ g⁻¹] /-- We shorten this from `measure_preimage_mul_left`, since left invariant is the preferred option for measures in this formalization. -/ @[simp, to_additive "We shorten this from `measure_preimage_add_left`, since left invariant is the preferred option for measures in this formalization."] lemma measure_preimage_mul (μ : measure G) [is_mul_left_invariant μ] (g : G) (A : set G) : μ ((λ h, g * h) ⁻¹' A) = μ A := calc μ ((λ h, g * h) ⁻¹' A) = map (λ h, g * h) μ A : ((measurable_equiv.mul_left g).map_apply A).symm ... = μ A : by rw map_mul_left_eq_self μ g @[simp, to_additive] lemma measure_preimage_mul_right (μ : measure G) [is_mul_right_invariant μ] (g : G) (A : set G) : μ ((λ h, h * g) ⁻¹' A) = μ A := calc μ ((λ h, h * g) ⁻¹' A) = map (λ h, h * g) μ A : ((measurable_equiv.mul_right g).map_apply A).symm ... = μ A : by rw map_mul_right_eq_self μ g @[to_additive] lemma map_mul_left_ae (μ : measure G) [is_mul_left_invariant μ] (x : G) : filter.map (λ h, x * h) μ.ae = μ.ae := ((measurable_equiv.mul_left x).map_ae μ).trans $ congr_arg ae $ map_mul_left_eq_self μ x @[to_additive] lemma map_mul_right_ae (μ : measure G) [is_mul_right_invariant μ] (x : G) : filter.map (λ h, h * x) μ.ae = μ.ae := ((measurable_equiv.mul_right x).map_ae μ).trans $ congr_arg ae $ map_mul_right_eq_self μ x @[to_additive] lemma map_div_right_ae (μ : measure G) [is_mul_right_invariant μ] (x : G) : filter.map (λ t, t / x) μ.ae = μ.ae := ((measurable_equiv.div_right x).map_ae μ).trans $ congr_arg ae $ map_div_right_eq_self μ x @[to_additive] lemma eventually_mul_left_iff (μ : measure G) [is_mul_left_invariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (t * x)) ↔ ∀ᵐ x ∂μ, p x := by { conv_rhs { rw [filter.eventually, ← map_mul_left_ae μ t] }, refl } @[to_additive] lemma eventually_mul_right_iff (μ : measure G) [is_mul_right_invariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (x * t)) ↔ ∀ᵐ x ∂μ, p x := by { conv_rhs { rw [filter.eventually, ← map_mul_right_ae μ t] }, refl } @[to_additive] lemma eventually_div_right_iff (μ : measure G) [is_mul_right_invariant μ] (t : G) {p : G → Prop} : (∀ᵐ x ∂μ, p (x / t)) ↔ ∀ᵐ x ∂μ, p x := by { conv_rhs { rw [filter.eventually, ← map_div_right_ae μ t] }, refl } end group namespace measure /-- The measure `A ↦ μ (A⁻¹)`, where `A⁻¹` is the pointwise inverse of `A`. -/ @[to_additive "The measure `A ↦ μ (- A)`, where `- A` is the pointwise negation of `A`."] protected def inv [has_inv G] (μ : measure G) : measure G := measure.map inv μ /-- A measure is invariant under negation if `- μ = μ`. Equivalently, this means that for all measurable `A` we have `μ (- A) = μ A`, where `- A` is the pointwise negation of `A`. -/ class is_neg_invariant [has_neg G] (μ : measure G) : Prop := (neg_eq_self : μ.neg = μ) /-- A measure is invariant under inversion if `μ⁻¹ = μ`. Equivalently, this means that for all measurable `A` we have `μ (A⁻¹) = μ A`, where `A⁻¹` is the pointwise inverse of `A`. -/ @[to_additive] class is_inv_invariant [has_inv G] (μ : measure G) : Prop := (inv_eq_self : μ.inv = μ) section inv variables [has_inv G] @[simp, to_additive] lemma inv_eq_self (μ : measure G) [is_inv_invariant μ] : μ.inv = μ := is_inv_invariant.inv_eq_self @[simp, to_additive] lemma map_inv_eq_self (μ : measure G) [is_inv_invariant μ] : map has_inv.inv μ = μ := is_inv_invariant.inv_eq_self variables [has_measurable_inv G] @[to_additive] lemma measure_preserving_inv (μ : measure G) [is_inv_invariant μ] : measure_preserving has_inv.inv μ μ := ⟨measurable_inv, map_inv_eq_self μ⟩ end inv section has_involutive_inv variables [has_involutive_inv G] [has_measurable_inv G] @[simp, to_additive] lemma inv_apply (μ : measure G) (s : set G) : μ.inv s = μ s⁻¹ := (measurable_equiv.inv G).map_apply s @[simp, to_additive] protected lemma inv_inv (μ : measure G) : μ.inv.inv = μ := (measurable_equiv.inv G).map_symm_map @[simp, to_additive] lemma measure_inv (μ : measure G) [is_inv_invariant μ] (A : set G) : μ A⁻¹ = μ A := by rw [← inv_apply, inv_eq_self] @[to_additive] lemma measure_preimage_inv (μ : measure G) [is_inv_invariant μ] (A : set G) : μ (has_inv.inv ⁻¹' A) = μ A := μ.measure_inv A @[to_additive] instance (μ : measure G) [sigma_finite μ] : sigma_finite μ.inv := (measurable_equiv.inv G).sigma_finite_map ‹_› end has_involutive_inv section division_monoid variables [division_monoid G] [has_measurable_mul G] [has_measurable_inv G] {μ : measure G} @[to_additive] instance [is_mul_left_invariant μ] : is_mul_right_invariant μ.inv := begin constructor, intro g, conv_rhs { rw [← map_mul_left_eq_self μ g⁻¹] }, simp_rw [measure.inv, map_map (measurable_mul_const g) measurable_inv, map_map measurable_inv (measurable_const_mul g⁻¹), function.comp, mul_inv_rev, inv_inv] end @[to_additive] instance [is_mul_right_invariant μ] : is_mul_left_invariant μ.inv := begin constructor, intro g, conv_rhs { rw [← map_mul_right_eq_self μ g⁻¹] }, simp_rw [measure.inv, map_map (measurable_const_mul g) measurable_inv, map_map measurable_inv (measurable_mul_const g⁻¹), function.comp, mul_inv_rev, inv_inv] end @[to_additive] lemma measure_preserving_div_left (μ : measure G) [is_inv_invariant μ] [is_mul_left_invariant μ] (g : G) : measure_preserving (λ t, g / t) μ μ := begin simp_rw [div_eq_mul_inv], exact (measure_preserving_mul_left μ g).comp (measure_preserving_inv μ) end @[to_additive] lemma map_div_left_eq_self (μ : measure G) [is_inv_invariant μ] [is_mul_left_invariant μ] (g : G) : map (λ t, g / t) μ = μ := (measure_preserving_div_left μ g).map_eq @[to_additive] lemma measure_preserving_mul_right_inv (μ : measure G) [is_inv_invariant μ] [is_mul_left_invariant μ] (g : G) : measure_preserving (λ t, (g * t)⁻¹) μ μ := (measure_preserving_inv μ).comp $ measure_preserving_mul_left μ g @[to_additive] lemma map_mul_right_inv_eq_self (μ : measure G) [is_inv_invariant μ] [is_mul_left_invariant μ] (g : G) : map (λ t, (g * t)⁻¹) μ = μ := (measure_preserving_mul_right_inv μ g).map_eq end division_monoid section group variables [group G] [has_measurable_mul G] [has_measurable_inv G] {μ : measure G} @[to_additive] lemma map_div_left_ae (μ : measure G) [is_mul_left_invariant μ] [is_inv_invariant μ] (x : G) : filter.map (λ t, x / t) μ.ae = μ.ae := ((measurable_equiv.div_left x).map_ae μ).trans $ congr_arg ae $ map_div_left_eq_self μ x end group end measure section topological_group variables [topological_space G] [borel_space G] {μ : measure G} [group G] @[to_additive] instance measure.regular.inv [has_continuous_inv G] [t2_space G] [regular μ] : regular μ.inv := regular.map (homeomorph.inv G) variables [topological_group G] @[to_additive] lemma regular_inv_iff [t2_space G] : μ.inv.regular ↔ μ.regular := begin split, { introI h, rw ← μ.inv_inv, exact measure.regular.inv }, { introI h, exact measure.regular.inv } end variables [is_mul_left_invariant μ] /-- If a left-invariant measure gives positive mass to a compact set, then it gives positive mass to any open set. -/ @[to_additive "If a left-invariant measure gives positive mass to a compact set, then it gives positive mass to any open set."] lemma is_open_pos_measure_of_mul_left_invariant_of_compact (K : set G) (hK : is_compact K) (h : μ K ≠ 0) : is_open_pos_measure μ := begin refine ⟨λ U hU hne, _⟩, contrapose! h, rw ← nonpos_iff_eq_zero, rw ← hU.interior_eq at hne, obtain ⟨t, hKt⟩ : ∃ (t : finset G), K ⊆ ⋃ (g : G) (H : g ∈ t), (λ (h : G), g * h) ⁻¹' U := compact_covered_by_mul_left_translates hK hne, calc μ K ≤ μ (⋃ (g : G) (H : g ∈ t), (λ (h : G), g * h) ⁻¹' U) : measure_mono hKt ... ≤ ∑ g in t, μ ((λ (h : G), g * h) ⁻¹' U) : measure_bUnion_finset_le _ _ ... = 0 : by simp [measure_preimage_mul, h] end /-- A nonzero left-invariant regular measure gives positive mass to any open set. -/ @[to_additive "A nonzero left-invariant regular measure gives positive mass to any open set."] lemma is_open_pos_measure_of_mul_left_invariant_of_regular [regular μ] (h₀ : μ ≠ 0) : is_open_pos_measure μ := let ⟨K, hK, h2K⟩ := regular.exists_compact_not_null.mpr h₀ in is_open_pos_measure_of_mul_left_invariant_of_compact K hK h2K @[to_additive] lemma null_iff_of_is_mul_left_invariant [regular μ] {s : set G} (hs : is_open s) : μ s = 0 ↔ s = ∅ ∨ μ = 0 := begin by_cases h3μ : μ = 0, { simp [h3μ] }, { haveI := is_open_pos_measure_of_mul_left_invariant_of_regular h3μ, simp only [h3μ, or_false, hs.measure_eq_zero_iff μ] }, end @[to_additive] lemma measure_ne_zero_iff_nonempty_of_is_mul_left_invariant [regular μ] (hμ : μ ≠ 0) {s : set G} (hs : is_open s) : μ s ≠ 0 ↔ s.nonempty := by simpa [null_iff_of_is_mul_left_invariant hs, hμ] using nonempty_iff_ne_empty.symm @[to_additive] lemma measure_pos_iff_nonempty_of_is_mul_left_invariant [regular μ] (h3μ : μ ≠ 0) {s : set G} (hs : is_open s) : 0 < μ s ↔ s.nonempty := pos_iff_ne_zero.trans $ measure_ne_zero_iff_nonempty_of_is_mul_left_invariant h3μ hs /-- If a left-invariant measure gives finite mass to a nonempty open set, then it gives finite mass to any compact set. -/ @[to_additive "If a left-invariant measure gives finite mass to a nonempty open set, then it gives finite mass to any compact set."] lemma measure_lt_top_of_is_compact_of_is_mul_left_invariant (U : set G) (hU : is_open U) (h'U : U.nonempty) (h : μ U ≠ ∞) {K : set G} (hK : is_compact K) : μ K < ∞ := begin rw ← hU.interior_eq at h'U, obtain ⟨t, hKt⟩ : ∃ (t : finset G), K ⊆ ⋃ (g : G) (H : g ∈ t), (λ (h : G), g * h) ⁻¹' U := compact_covered_by_mul_left_translates hK h'U, calc μ K ≤ μ (⋃ (g : G) (H : g ∈ t), (λ (h : G), g * h) ⁻¹' U) : measure_mono hKt ... ≤ ∑ g in t, μ ((λ (h : G), g * h) ⁻¹' U) : measure_bUnion_finset_le _ _ ... = finset.card t * μ U : by simp only [measure_preimage_mul, finset.sum_const, nsmul_eq_mul] ... < ∞ : ennreal.mul_lt_top (ennreal.nat_ne_top _) h end /-- If a left-invariant measure gives finite mass to a set with nonempty interior, then it gives finite mass to any compact set. -/ @[to_additive "If a left-invariant measure gives finite mass to a set with nonempty interior, then it gives finite mass to any compact set."] lemma measure_lt_top_of_is_compact_of_is_mul_left_invariant' {U : set G} (hU : (interior U).nonempty) (h : μ U ≠ ∞) {K : set G} (hK : is_compact K) : μ K < ∞ := measure_lt_top_of_is_compact_of_is_mul_left_invariant (interior U) is_open_interior hU ((measure_mono (interior_subset)).trans_lt (lt_top_iff_ne_top.2 h)).ne hK /-- In a noncompact locally compact group, a left-invariant measure which is positive on open sets has infinite mass. -/ @[simp, to_additive "In a noncompact locally compact additive group, a left-invariant measure which is positive on open sets has infinite mass."] lemma measure_univ_of_is_mul_left_invariant [locally_compact_space G] [noncompact_space G] (μ : measure G) [is_open_pos_measure μ] [μ.is_mul_left_invariant] : μ univ = ∞ := begin /- Consider a closed compact set `K` with nonempty interior. For any compact set `L`, one may find `g = g (L)` such that `L` is disjoint from `g • K`. Iterating this, one finds infinitely many translates of `K` which are disjoint from each other. As they all have the same positive mass, it follows that the space has infinite measure. -/ obtain ⟨K, hK, Kclosed, Kint⟩ : ∃ (K : set G), is_compact K ∧ is_closed K ∧ (1 : G) ∈ interior K, { rcases local_is_compact_is_closed_nhds_of_group (is_open_univ.mem_nhds (mem_univ (1 : G))) with ⟨K, hK⟩, exact ⟨K, hK.1, hK.2.1, hK.2.2.2⟩, }, have K_pos : 0 < μ K, from measure_pos_of_nonempty_interior _ ⟨_, Kint⟩, have A : ∀ (L : set G), is_compact L → ∃ (g : G), disjoint L (g • K), from λ L hL, exists_disjoint_smul_of_is_compact hL hK, choose! g hg using A, set L : ℕ → set G := λ n, (λ T, T ∪ (g T • K))^[n] K with hL, have Lcompact : ∀ n, is_compact (L n), { assume n, induction n with n IH, { exact hK }, { simp_rw [hL, iterate_succ'], apply is_compact.union IH (hK.smul (g (L n))) } }, have Lclosed : ∀ n, is_closed (L n), { assume n, induction n with n IH, { exact Kclosed }, { simp_rw [hL, iterate_succ'], apply is_closed.union IH (Kclosed.smul (g (L n))) } }, have M : ∀ n, μ (L n) = (n + 1 : ℕ) * μ K, { assume n, induction n with n IH, { simp only [L, one_mul, algebra_map.coe_one, iterate_zero, id.def] }, { calc μ (L (n + 1)) = μ (L n) + μ (g (L n) • K) : begin simp_rw [hL, iterate_succ'], exact measure_union' (hg _ (Lcompact _)) (Lclosed _).measurable_set end ... = ((n + 1) + 1 : ℕ) * μ K : by simp only [IH, measure_smul, add_mul, nat.cast_add, algebra_map.coe_one, one_mul] } }, have N : tendsto (λ n, μ (L n)) at_top (𝓝 (∞ * μ K)), { simp_rw [M], apply ennreal.tendsto.mul_const _ (or.inl ennreal.top_ne_zero), exact ennreal.tendsto_nat_nhds_top.comp (tendsto_add_at_top_nat _) }, simp only [ennreal.top_mul, K_pos.ne', if_false] at N, apply top_le_iff.1, exact le_of_tendsto' N (λ n, measure_mono (subset_univ _)), end end topological_group section comm_semigroup variables [comm_semigroup G] /-- In an abelian group every left invariant measure is also right-invariant. We don't declare the converse as an instance, since that would loop type-class inference, and we use `is_mul_left_invariant` as the default hypothesis in abelian groups. -/ @[priority 100, to_additive is_add_left_invariant.is_add_right_invariant "In an abelian additive group every left invariant measure is also right-invariant. We don't declare the converse as an instance, since that would loop type-class inference, and we use `is_add_left_invariant` as the default hypothesis in abelian groups."] instance is_mul_left_invariant.is_mul_right_invariant {μ : measure G} [is_mul_left_invariant μ] : is_mul_right_invariant μ := ⟨λ g, by simp_rw [mul_comm, map_mul_left_eq_self]⟩ end comm_semigroup section haar namespace measure /-- A measure on an additive group is an additive Haar measure if it is left-invariant, and gives finite mass to compact sets and positive mass to open sets. -/ class is_add_haar_measure {G : Type*} [add_group G] [topological_space G] [measurable_space G] (μ : measure G) extends is_finite_measure_on_compacts μ, is_add_left_invariant μ, is_open_pos_measure μ : Prop /-- A measure on a group is a Haar measure if it is left-invariant, and gives finite mass to compact sets and positive mass to open sets. -/ @[to_additive] class is_haar_measure {G : Type*} [group G] [topological_space G] [measurable_space G] (μ : measure G) extends is_finite_measure_on_compacts μ, is_mul_left_invariant μ, is_open_pos_measure μ : Prop /-- Record that a Haar measure on a locally compact space is locally finite. This is needed as the fact that a measure which is finite on compacts is locally finite is not registered as an instance, to avoid an instance loop. See Note [lower instance priority]. -/ @[priority 100, to_additive "Record that an additive Haar measure on a locally compact space is locally finite. This is needed as the fact that a measure which is finite on compacts is locally finite is not registered as an instance, to avoid an instance loop. See Note [lower instance priority]"] instance is_locally_finite_measure_of_is_haar_measure {G : Type*} [group G] [measurable_space G] [topological_space G] [locally_compact_space G] (μ : measure G) [is_haar_measure μ] : is_locally_finite_measure μ := is_locally_finite_measure_of_is_finite_measure_on_compacts section variables [group G] [topological_space G] (μ : measure G) [is_haar_measure μ] @[simp, to_additive] lemma haar_singleton [topological_group G] [borel_space G] (g : G) : μ {g} = μ {(1 : G)} := begin convert measure_preimage_mul μ (g⁻¹) _, simp only [mul_one, preimage_mul_left_singleton, inv_inv], end @[to_additive measure_theory.measure.is_add_haar_measure.smul] lemma is_haar_measure.smul {c : ℝ≥0∞} (cpos : c ≠ 0) (ctop : c ≠ ∞) : is_haar_measure (c • μ) := { lt_top_of_is_compact := λ K hK, ennreal.mul_lt_top ctop hK.measure_lt_top.ne, to_is_open_pos_measure := is_open_pos_measure_smul μ cpos } /-- If a left-invariant measure gives positive mass to some compact set with nonempty interior, then it is a Haar measure. -/ @[to_additive "If a left-invariant measure gives positive mass to some compact set with nonempty interior, then it is an additive Haar measure."] lemma is_haar_measure_of_is_compact_nonempty_interior [topological_group G] [borel_space G] (μ : measure G) [is_mul_left_invariant μ] (K : set G) (hK : is_compact K) (h'K : (interior K).nonempty) (h : μ K ≠ 0) (h' : μ K ≠ ∞) : is_haar_measure μ := { lt_top_of_is_compact := λ L hL, measure_lt_top_of_is_compact_of_is_mul_left_invariant' h'K h' hL, to_is_open_pos_measure := is_open_pos_measure_of_mul_left_invariant_of_compact K hK h } /-- The image of a Haar measure under a continuous surjective proper group homomorphism is again a Haar measure. See also `mul_equiv.is_haar_measure_map`. -/ @[to_additive "The image of an additive Haar measure under a continuous surjective proper additive group homomorphism is again an additive Haar measure. See also `add_equiv.is_add_haar_measure_map`."] lemma is_haar_measure_map [borel_space G] [topological_group G] {H : Type*} [group H] [topological_space H] [measurable_space H] [borel_space H] [t2_space H] [topological_group H] (f : G →* H) (hf : continuous f) (h_surj : surjective f) (h_prop : tendsto f (cocompact G) (cocompact H)) : is_haar_measure (measure.map f μ) := { to_is_mul_left_invariant := is_mul_left_invariant_map f.to_mul_hom hf.measurable h_surj, lt_top_of_is_compact := begin assume K hK, rw map_apply hf.measurable hK.measurable_set, exact is_compact.measure_lt_top ((⟨⟨f, hf⟩, h_prop⟩ : cocompact_map G H).is_compact_preimage hK), end, to_is_open_pos_measure := hf.is_open_pos_measure_map h_surj } /-- A convenience wrapper for `measure_theory.measure.is_haar_measure_map`. -/ @[to_additive "A convenience wrapper for `measure_theory.measure.is_add_haar_measure_map`."] lemma _root_.mul_equiv.is_haar_measure_map [borel_space G] [topological_group G] {H : Type*} [group H] [topological_space H] [measurable_space H] [borel_space H] [t2_space H] [topological_group H] (e : G ≃* H) (he : continuous e) (hesymm : continuous e.symm) : is_haar_measure (measure.map e μ) := is_haar_measure_map μ (e : G →* H) he e.surjective ({ .. e } : G ≃ₜ H).to_cocompact_map.cocompact_tendsto' /-- A Haar measure on a σ-compact space is σ-finite. See Note [lower instance priority] -/ @[priority 100, to_additive "A Haar measure on a σ-compact space is σ-finite. See Note [lower instance priority]"] instance is_haar_measure.sigma_finite [sigma_compact_space G] : sigma_finite μ := ⟨⟨{ set := compact_covering G, set_mem := λ n, mem_univ _, finite := λ n, is_compact.measure_lt_top $ is_compact_compact_covering G n, spanning := Union_compact_covering G }⟩⟩ @[to_additive] instance {G : Type*} [group G] [topological_space G] {mG : measurable_space G} {H : Type*} [group H] [topological_space H] {mH : measurable_space H} (μ : measure G) (ν : measure H) [is_haar_measure μ] [is_haar_measure ν] [sigma_finite μ] [sigma_finite ν] [has_measurable_mul G] [has_measurable_mul H] : is_haar_measure (μ.prod ν) := {} /-- If the neutral element of a group is not isolated, then a Haar measure on this group has no atoms. The additive version of this instance applies in particular to show that an additive Haar measure on a nontrivial finite-dimensional real vector space has no atom. -/ @[priority 100, to_additive "If the zero element of an additive group is not isolated, then an additive Haar measure on this group has no atoms. This applies in particular to show that an additive Haar measure on a nontrivial finite-dimensional real vector space has no atom."] instance is_haar_measure.has_no_atoms [topological_group G] [borel_space G] [t1_space G] [locally_compact_space G] [(𝓝[≠] (1 : G)).ne_bot] (μ : measure G) [μ.is_haar_measure] : has_no_atoms μ := begin suffices H : μ {(1 : G)} ≤ 0, by { constructor, simp [le_bot_iff.1 H] }, obtain ⟨K, K_compact, K_int⟩ : ∃ (K : set G), is_compact K ∧ (1 : G) ∈ interior K, { rcases exists_compact_subset is_open_univ (mem_univ (1 : G)) with ⟨K, hK⟩, exact ⟨K, hK.1, hK.2.1⟩ }, have K_inf : set.infinite K := infinite_of_mem_nhds (1 : G) (mem_interior_iff_mem_nhds.1 K_int), have μKlt : μ K ≠ ∞ := K_compact.measure_lt_top.ne, have I : ∀ (n : ℕ), μ {(1 : G)} ≤ μ K / n, { assume n, obtain ⟨t, tK, tn⟩ : ∃ (t : finset G), ↑t ⊆ K ∧ t.card = n := K_inf.exists_subset_card_eq n, have A : μ t ≤ μ K := measure_mono tK, have B : μ t = n * μ {(1 : G)}, { rw ← bUnion_of_singleton ↑t, change μ (⋃ (x ∈ t), {x}) = n * μ {1}, rw @measure_bUnion_finset G G _ μ t (λ i, {i}), { simp only [tn, finset.sum_const, nsmul_eq_mul, haar_singleton] }, { assume x hx y hy xy, simp only [on_fun, xy.symm, mem_singleton_iff, not_false_iff, disjoint_singleton_right] }, { assume b hb, exact measurable_set_singleton b } }, rw B at A, rwa [ennreal.le_div_iff_mul_le _ (or.inr μKlt), mul_comm], right, apply (measure_pos_of_nonempty_interior μ ⟨_, K_int⟩).ne' }, have J : tendsto (λ (n : ℕ), μ K / n) at_top (𝓝 (μ K / ∞)) := ennreal.tendsto.const_div ennreal.tendsto_nat_nhds_top (or.inr μKlt), simp only [ennreal.div_top] at J, exact ge_of_tendsto' J I, end end end measure end haar end measure_theory
56683b95f61d72618ba79ebcdb6602b69780634a
cf39355caa609c0f33405126beee2739aa3cb77e
/tests/lean/run/sub.lean
8f9fb20950b98a6295b221529c8964bf0d1b4e9f
[ "Apache-2.0" ]
permissive
leanprover-community/lean
12b87f69d92e614daea8bcc9d4de9a9ace089d0e
cce7990ea86a78bdb383e38ed7f9b5ba93c60ce0
refs/heads/master
1,687,508,156,644
1,684,951,104,000
1,684,951,104,000
169,960,991
457
107
Apache-2.0
1,686,744,372,000
1,549,790,268,000
C++
UTF-8
Lean
false
false
65
lean
#check { x : nat // x > 0 } #check { x : nat → nat // true }
b1dd4aaf32b605b986cd323fb5e4b0507a6c7115
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/run/ac_refl1.lean
835cb0256f50143dc536d6abc46923bc8d72ba7c
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
608
lean
example (a b c : nat) (f : nat → nat) : f (a + b + c) = f (b + c + a) := by ac_refl example (a b c : nat) (f : nat → nat) : f (a + b + (c * b * a)) = f (b + (a * c * b) + a) := by ac_refl example (a b c : nat) (f : nat → nat → nat) : f (b * c) (c * b * a) = f (c * b) (a * c * b) := by ac_refl example (a b c : nat) (f : nat → nat) : f (a + (b * c) + (c * b * a)) = f ((c * b) + (a * c * b) + a) := by ac_refl example (a b c : nat) (f : nat → nat) (g : nat → nat → nat) : g (f (a + (b * c) + (c * b * a))) (a + c + b + a) = g (f ((c * b) + (a * c * b) + a)) (c + b + a + a) := by ac_refl
ab053f0d9fc2970961258e69f85a9513ad7a0d58
7cef822f3b952965621309e88eadf618da0c8ae9
/src/data/zsqrtd/gaussian_int.lean
21aed14d13a3204a0ab2183f82faddbe454b67dc
[ "Apache-2.0" ]
permissive
rmitta/mathlib
8d90aee30b4db2b013e01f62c33f297d7e64a43d
883d974b608845bad30ae19e27e33c285200bf84
refs/heads/master
1,585,776,832,544
1,576,874,096,000
1,576,874,096,000
153,663,165
0
2
Apache-2.0
1,544,806,490,000
1,539,884,365,000
Lean
UTF-8
Lean
false
false
12,409
lean
/- Copyright (c) 2019 Chris Hughes. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Chris Hughes -/ import data.zsqrtd.basic data.complex.basic ring_theory.principal_ideal_domain import data.zmod.quadratic_reciprocity /-! # Gaussian integers The Gaussian integers are complex integer, complex numbers whose real and imaginary parts are both integers. ## Main definitions The Euclidean domain structure on `ℤ[i]` is defined in this file. The homomorphism `to_complex` into the complex numbers is also defined in this file. ## Main statements `prime_iff_mod_four_eq_three_of_nat_prime` A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` ## Notations This file uses the local notation `ℤ[i]` for `gaussian_int` ## Implementation notes Gaussian integers are implemented using the more general definition `zsqrtd`, the type of integers adjoined a square root of `d`, in this case `-1`. The definition is reducible, so that properties and definitions about `zsqrtd` can easily be used. -/ open zsqrtd complex @[reducible] def gaussian_int : Type := zsqrtd (-1) local notation `ℤ[i]` := gaussian_int namespace gaussian_int instance : has_repr ℤ[i] := ⟨λ x, "⟨" ++ repr x.re ++ ", " ++ repr x.im ++ "⟩"⟩ instance : comm_ring ℤ[i] := zsqrtd.comm_ring def to_complex (x : ℤ[i]) : ℂ := x.re + x.im * I instance : has_coe (ℤ[i]) ℂ := ⟨to_complex⟩ lemma to_complex_def (x : ℤ[i]) : (x : ℂ) = x.re + x.im * I := rfl lemma to_complex_def' (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ) = x + y * I := by simp [to_complex_def] lemma to_complex_def₂ (x : ℤ[i]) : (x : ℂ) = ⟨x.re, x.im⟩ := by apply complex.ext; simp [to_complex_def] instance to_complex.is_ring_hom : is_ring_hom to_complex:= by refine_struct {..}; intros; apply complex.ext; simp [to_complex] instance : is_ring_hom (coe : ℤ[i] → ℂ) := to_complex.is_ring_hom @[simp] lemma to_real_re (x : ℤ[i]) : ((x.re : ℤ) : ℝ) = (x : ℂ).re := by simp [to_complex_def] @[simp] lemma to_real_im (x : ℤ[i]) : ((x.im : ℤ) : ℝ) = (x : ℂ).im := by simp [to_complex_def] @[simp] lemma to_complex_re (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).re = x := by simp [to_complex_def] @[simp] lemma to_complex_im (x y : ℤ) : ((⟨x, y⟩ : ℤ[i]) : ℂ).im = y := by simp [to_complex_def] @[simp] lemma to_complex_add (x y : ℤ[i]) : ((x + y : ℤ[i]) : ℂ) = x + y := is_ring_hom.map_add coe @[simp] lemma to_complex_mul (x y : ℤ[i]) : ((x * y : ℤ[i]) : ℂ) = x * y := is_ring_hom.map_mul coe @[simp] lemma to_complex_one : ((1 : ℤ[i]) : ℂ) = 1 := is_ring_hom.map_one coe @[simp] lemma to_complex_zero : ((0 : ℤ[i]) : ℂ) = 0 := is_ring_hom.map_zero coe @[simp] lemma to_complex_neg (x : ℤ[i]) : ((-x : ℤ[i]) : ℂ) = -x := is_ring_hom.map_neg coe @[simp] lemma to_complex_sub (x y : ℤ[i]) : ((x - y : ℤ[i]) : ℂ) = x - y := is_ring_hom.map_sub coe @[simp] lemma to_complex_inj {x y : ℤ[i]} : (x : ℂ) = y ↔ x = y := by cases x; cases y; simp [to_complex_def₂] @[simp] lemma to_complex_eq_zero {x : ℤ[i]} : (x : ℂ) = 0 ↔ x = 0 := by rw [← to_complex_zero, to_complex_inj] @[simp] lemma nat_cast_real_norm (x : ℤ[i]) : (x.norm : ℝ) = (x : ℂ).norm_sq := by rw [norm, norm_sq]; simp @[simp] lemma nat_cast_complex_norm (x : ℤ[i]) : (x.norm : ℂ) = (x : ℂ).norm_sq := by cases x; rw [norm, norm_sq]; simp lemma norm_nonneg (x : ℤ[i]) : 0 ≤ norm x := norm_nonneg trivial _ @[simp] lemma norm_eq_zero {x : ℤ[i]} : norm x = 0 ↔ x = 0 := by rw [← @int.cast_inj ℝ _ _ _]; simp lemma norm_pos {x : ℤ[i]} : 0 < norm x ↔ x ≠ 0 := by rw [lt_iff_le_and_ne, ne.def, eq_comm, norm_eq_zero]; simp [norm_nonneg] @[simp] lemma coe_nat_abs_norm (x : ℤ[i]) : (x.norm.nat_abs : ℤ) = x.norm := int.nat_abs_of_nonneg (norm_nonneg _) @[simp] lemma nat_cast_nat_abs_norm {α : Type*} [ring α] (x : ℤ[i]) : (x.norm.nat_abs : α) = x.norm := by rw [← int.cast_coe_nat, coe_nat_abs_norm] lemma nat_abs_norm_eq (x : ℤ[i]) : x.norm.nat_abs = x.re.nat_abs * x.re.nat_abs + x.im.nat_abs * x.im.nat_abs := int.coe_nat_inj $ begin simp, simp [norm] end protected def div (x y : ℤ[i]) : ℤ[i] := let n := (rat.of_int (norm y))⁻¹ in let c := y.conj in ⟨round (rat.of_int (x * c).re * n : ℚ), round (rat.of_int (x * c).im * n : ℚ)⟩ instance : has_div ℤ[i] := ⟨gaussian_int.div⟩ lemma div_def (x y : ℤ[i]) : x / y = ⟨round ((x * conj y).re / norm y : ℚ), round ((x * conj y).im / norm y : ℚ)⟩ := show zsqrtd.mk _ _ = _, by simp [rat.of_int_eq_mk, rat.mk_eq_div, div_eq_mul_inv] lemma to_complex_div_re (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).re = round ((x / y : ℂ).re) := by rw [div_def, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] lemma to_complex_div_im (x y : ℤ[i]) : ((x / y : ℤ[i]) : ℂ).im = round ((x / y : ℂ).im) := by rw [div_def, ← @rat.cast_round ℝ _ _, ← @rat.cast_round ℝ _ _]; simp [-rat.cast_round, mul_assoc, div_eq_mul_inv, mul_add, add_mul] local notation `abs'` := _root_.abs lemma norm_sq_le_norm_sq_of_re_le_of_im_le {x y : ℂ} (hre : abs' x.re ≤ abs' y.re) (him : abs' x.im ≤ abs' y.im) : x.norm_sq ≤ y.norm_sq := by rw [norm_sq, norm_sq, ← _root_.abs_mul_self, _root_.abs_mul, ← _root_.abs_mul_self y.re, _root_.abs_mul y.re, ← _root_.abs_mul_self x.im, _root_.abs_mul x.im, ← _root_.abs_mul_self y.im, _root_.abs_mul y.im]; exact (add_le_add (mul_self_le_mul_self (abs_nonneg _) hre) (mul_self_le_mul_self (abs_nonneg _) him)) lemma norm_sq_div_sub_div_lt_one (x y : ℤ[i]) : ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq < 1 := calc ((x / y : ℂ) - ((x / y : ℤ[i]) : ℂ)).norm_sq = ((x / y : ℂ).re - ((x / y : ℤ[i]) : ℂ).re + ((x / y : ℂ).im - ((x / y : ℤ[i]) : ℂ).im) * I : ℂ).norm_sq : congr_arg _ $ by apply complex.ext; simp ... ≤ (1 / 2 + 1 / 2 * I).norm_sq : have abs' (2 / (2 * 2) : ℝ) = 1 / 2, by rw _root_.abs_of_nonneg; norm_num, norm_sq_le_norm_sq_of_re_le_of_im_le (by rw [to_complex_div_re]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).re) (by rw [to_complex_div_im]; simp [norm_sq, this]; simpa using abs_sub_round (x / y : ℂ).im) ... < 1 : by simp [norm_sq]; norm_num protected def mod (x y : ℤ[i]) : ℤ[i] := x - y * (x / y) instance : has_mod ℤ[i] := ⟨gaussian_int.mod⟩ lemma mod_def (x y : ℤ[i]) : x % y = x - y * (x / y) := rfl lemma norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm < y.norm := have (y : ℂ) ≠ 0, by rwa [ne.def, ← to_complex_zero, to_complex_inj], (@int.cast_lt ℝ _ _ _).1 $ calc ↑(norm (x % y)) = (x - y * (x / y : ℤ[i]) : ℂ).norm_sq : by simp [mod_def] ... = (y : ℂ).norm_sq * (((x / y) - (x / y : ℤ[i])) : ℂ).norm_sq : by rw [← norm_sq_mul, mul_sub, mul_div_cancel' _ this] ... < (y : ℂ).norm_sq * 1 : mul_lt_mul_of_pos_left (norm_sq_div_sub_div_lt_one _ _) (norm_sq_pos.2 this) ... = norm y : by simp lemma nat_abs_norm_mod_lt (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (x % y).norm.nat_abs < y.norm.nat_abs := int.coe_nat_lt.1 (by simp [-int.coe_nat_lt, norm_mod_lt x hy]) lemma norm_le_norm_mul_left (x : ℤ[i]) {y : ℤ[i]} (hy : y ≠ 0) : (norm x).nat_abs ≤ (norm (x * y)).nat_abs := by rw [norm_mul, int.nat_abs_mul]; exact le_mul_of_one_le_right' (nat.zero_le _) (int.coe_nat_le.1 (by rw [coe_nat_abs_norm]; exact norm_pos.2 hy)) instance : nonzero_comm_ring ℤ[i] := { zero_ne_one := dec_trivial, ..gaussian_int.comm_ring } instance : euclidean_domain ℤ[i] := { quotient := (/), remainder := (%), quotient_zero := λ _, by simp [div_def]; refl, quotient_mul_add_remainder_eq := λ _ _, by simp [mod_def], r := _, r_well_founded := measure_wf (int.nat_abs ∘ norm), remainder_lt := nat_abs_norm_mod_lt, mul_left_not_lt := λ a b hb0, not_lt_of_ge $ norm_le_norm_mul_left a hb0 } open principal_ideal_domain lemma mod_four_eq_three_of_nat_prime_of_prime {p : ℕ} (hp : p.prime) (hpi : prime (p : ℤ[i])) : p % 4 = 3 := hp.eq_two_or_odd.elim (λ hp2, absurd hpi (mt irreducible_iff_prime.2 $ λ ⟨hu, h⟩, begin have := h ⟨1, 1⟩ ⟨1, -1⟩ (hp2.symm ▸ rfl), rw [← norm_eq_one_iff, ← norm_eq_one_iff] at this, exact absurd this dec_trivial end)) (λ hp1, by_contradiction $ λ hp3 : p % 4 ≠ 3, have hp41 : p % 4 = 1, begin rw [← nat.mod_mul_left_mod p 2 2, show 2 * 2 = 4, from rfl] at hp1, have := nat.mod_lt p (show 0 < 4, from dec_trivial), revert this hp3 hp1, generalize hm : p % 4 = m, clear hm, revert m, exact dec_trivial, end, let ⟨k, hk⟩ := (zmodp.exists_pow_two_eq_neg_one_iff_mod_four_ne_three hp).2 $ by rw hp41; exact dec_trivial in have hpk : p ∣ k.val ^ 2 + 1, by rw [← zmodp.eq_zero_iff_dvd_nat hp]; simp *, have hkmul : (k.val ^ 2 + 1 : ℤ[i]) = ⟨k.val, 1⟩ * ⟨k.val, -1⟩ := by simp [_root_.pow_two, zsqrtd.ext], have hpne1 : p ≠ 1, from (ne_of_lt (hp.one_lt)).symm, have hkltp : 1 + k.val * k.val < p * p, from calc 1 + k.val * k.val ≤ k.val + k.val * k.val : add_le_add_right (nat.pos_of_ne_zero (λ hk0, by clear_aux_decl; simp [*, nat.pow_succ] at *)) _ ... = k.val * (k.val + 1) : by simp [mul_add] ... < p * p : mul_lt_mul k.2 k.2 (nat.succ_pos _) (nat.zero_le _), have hpk₁ : ¬ (p : ℤ[i]) ∣ ⟨k.val, -1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k.val, -1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (-1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpk₂ : ¬ (p : ℤ[i]) ∣ ⟨k.val, 1⟩ := λ ⟨x, hx⟩, lt_irrefl (p * x : ℤ[i]).norm.nat_abs $ calc (norm (p * x : ℤ[i])).nat_abs = (norm ⟨k.val, 1⟩).nat_abs : by rw hx ... < (norm (p : ℤ[i])).nat_abs : by simpa [norm] using hkltp ... ≤ (norm (p * x : ℤ[i])).nat_abs : norm_le_norm_mul_left _ (λ hx0, (show (1 : ℤ) ≠ 0, from dec_trivial) $ by simpa [hx0] using congr_arg zsqrtd.im hx), have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $ by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.one_lt).symm h.1, let ⟨y, hy⟩ := hpk in by have := hpi.2.2 ⟨k.val, 1⟩ ⟨k.val, -1⟩ ⟨y, by rw [← hkmul, ← nat.cast_mul p, ← hy]; simp⟩; clear_aux_decl; tauto) lemma sum_two_squares_of_nat_prime_of_not_irreducible {p : ℕ} (hp : p.prime) (hpi : ¬irreducible (p : ℤ[i])) : ∃ a b, a^2 + b^2 = p := have hpu : ¬ is_unit (p : ℤ[i]), from mt norm_eq_one_iff.2 $ by rw [norm_nat_cast, int.nat_abs_mul, nat.mul_eq_one_iff]; exact λ h, (ne_of_lt hp.one_lt).symm h.1, have hab : ∃ a b, (p : ℤ[i]) = a * b ∧ ¬ is_unit a ∧ ¬ is_unit b, by simpa [irreducible, hpu, classical.not_forall, not_or_distrib] using hpi, let ⟨a, b, hpab, hau, hbu⟩ := hab in have hnap : (norm a).nat_abs = p, from ((hp.mul_eq_prime_pow_two_iff (mt norm_eq_one_iff.1 hau) (mt norm_eq_one_iff.1 hbu)).1 $ by rw [← int.coe_nat_inj', int.coe_nat_pow, _root_.pow_two, ← @norm_nat_cast (-1), hpab]; simp).1, ⟨a.re.nat_abs, a.im.nat_abs, by simpa [nat_abs_norm_eq, nat.pow_two] using hnap⟩ lemma prime_of_nat_prime_of_mod_four_eq_three {p : ℕ} (hp : p.prime) (hp3 : p % 4 = 3) : prime (p : ℤ[i]) := irreducible_iff_prime.1 $ classical.by_contradiction $ λ hpi, let ⟨a, b, hab⟩ := sum_two_squares_of_nat_prime_of_not_irreducible hp hpi in have ∀ a b : zmod 4, a^2 + b^2 ≠ p, by erw [← zmod.cast_mod_nat 4 p, hp3]; exact dec_trivial, this a b (hab ▸ by simp) /-- A prime natural number is prime in `ℤ[i]` if and only if it is `3` mod `4` -/ lemma prime_iff_mod_four_eq_three_of_nat_prime {p : ℕ} (hp : p.prime) : prime (p : ℤ[i]) ↔ p % 4 = 3 := ⟨mod_four_eq_three_of_nat_prime_of_prime hp, prime_of_nat_prime_of_mod_four_eq_three hp⟩ end gaussian_int
4f19a967981411e01f456d90e922a383f1f97ca4
302b541ac2e998a523ae04da7673fd0932ded126
/tests/bench/expression.lean
5b03d836a8226d3c2be50f411df9e84d135dcd41
[]
no_license
mattweingarten/lambdapure
4aeff69e8e3b8e78ea3c0a2b9b61770ef5a689b1
f920a4ad78e6b1e3651f30bf8445c9105dfa03a8
refs/heads/master
1,680,665,168,790
1,618,420,180,000
1,618,420,180,000
310,816,264
2
1
null
null
null
null
UTF-8
Lean
false
false
293
lean
set_option trace.compiler.ir.init true inductive Expr | Val : Nat → Expr | Add : Expr → Expr → Expr | Mul : Expr → Expr → Expr open Expr def const_fold : Expr -> Expr | Val n => Val n | Add (Val x) (Val y) => Val (x + y) | Mul (Val x) (Val y) => Val (x * y) | e => e
eced8596637003d38a8cd7c6a16869e4e0dbc6c7
4b846d8dabdc64e7ea03552bad8f7fa74763fc67
/library/init/meta/smt/smt_tactic.lean
0b9aacdaa68a8f08a6aa84b53cca3ea9c07d927f
[ "Apache-2.0" ]
permissive
pacchiano/lean
9324b33f3ac3b5c5647285160f9f6ea8d0d767dc
fdadada3a970377a6df8afcd629a6f2eab6e84e8
refs/heads/master
1,611,357,380,399
1,489,870,101,000
1,489,870,101,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
15,147
lean
/- Copyright (c) 2016 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ prelude import init.meta.simp_tactic import init.meta.smt.congruence_closure import init.meta.smt.ematch universe u run_cmd mk_simp_attr `pre_smt run_cmd mk_hinst_lemma_attr_set `ematch [] [`ematch_lhs] /-- Configuration for the smt tactic preprocessor. The preprocessor is applied whenever a new hypothesis is introduced. - simp_attr: is the attribute name for the simplification lemmas that are used during the preprocessing step. - max_steps: it is the maximum number of steps performed by the simplifier. - zeta: if tt, then zeta reduction (i.e., unfolding let-expressions) is used during preprocessing. -/ structure smt_pre_config := (simp_attr : name := `pre_smt) (max_steps : nat := 1000000) (zeta : bool := ff) /-- Configuration for the smt_state object. - em_attr: is the attribute name for the hinst_lemmas that are used for ematching -/ structure smt_config := (cc_cfg : cc_config := {}) (em_cfg : ematch_config := {}) (pre_cfg : smt_pre_config := {}) (em_attr : name := `ematch) meta def smt_config.set_classical (c : smt_config) (b : bool) : smt_config := {c with cc_cfg := { (c^.cc_cfg) with em := b}} meta constant smt_goal : Type meta def smt_state := list smt_goal meta constant smt_state.mk : smt_config → tactic smt_state meta constant smt_state.to_format : smt_state → tactic_state → format /-- Return tt iff classical excluded middle was enabled at smt_state.mk -/ meta constant smt_state.classical : smt_state → bool meta def smt_tactic := state_t smt_state tactic meta instance : has_append smt_state := list.has_append meta instance : monad smt_tactic := state_t.monad _ _ /- We don't use the default state_t lift operation because only tactics that do not change hypotheses can be automatically lifted to smt_tactic. -/ meta constant tactic_to_smt_tactic (α : Type) : tactic α → smt_tactic α meta instance : monad.has_monad_lift tactic smt_tactic := ⟨tactic_to_smt_tactic⟩ meta instance (α : Type) : has_coe (tactic α) (smt_tactic α) := ⟨monad.monad_lift⟩ meta def smt_tactic_orelse {α : Type} (t₁ t₂ : smt_tactic α) : smt_tactic α := λ ss ts, result.cases_on (t₁ ss ts) result.success (λ e₁ ref₁ s', result.cases_on (t₂ ss ts) result.success result.exception) meta instance : monad_fail smt_tactic := { smt_tactic.monad with fail := λ α s, (tactic.fail (to_fmt s) : smt_tactic α) } meta instance : alternative smt_tactic := { smt_tactic.monad with failure := λ α, @tactic.failed α, orelse := @smt_tactic_orelse } namespace smt_tactic open tactic (transparency) meta constant intros : smt_tactic unit meta constant intron : nat → smt_tactic unit meta constant intro_lst : list name → smt_tactic unit /-- Try to close main goal by using equalities implied by the congruence closure module. -/ meta constant close : smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from lemmas in the main goal with terms in the main goal. The set of lemmas is populated with theorems tagged with the attribute specified at smt_config.em_attr, and lemmas added using tactics such as `smt_tactic.add_lemmas`. The current set of lemmas can be retrieved using the tactic `smt_tactic.get_lemmas`. Remark: the given predicate is applied to every new instance. The instance is only added to the state if the predicate returns tt. -/ meta constant ematch_core : (expr → bool) → smt_tactic unit /-- Produce new facts using heuristic lemma instantiation based on E-matching. This tactic tries to match patterns from the given lemmas with terms in the main goal. -/ meta constant ematch_using : hinst_lemmas → smt_tactic unit meta constant mk_ematch_eqn_lemmas_for_core : transparency → name → smt_tactic hinst_lemmas meta constant to_cc_state : smt_tactic cc_state meta constant to_em_state : smt_tactic ematch_state meta constant get_config : smt_tactic smt_config /-- Preprocess the given term using the same simplifications rules used when we introduce a new hypothesis. The result is pair containing the resulting term and a proof that it is equal to the given one. -/ meta constant preprocess : expr → smt_tactic (expr × expr) meta constant get_lemmas : smt_tactic hinst_lemmas meta constant set_lemmas : hinst_lemmas → smt_tactic unit meta constant add_lemmas : hinst_lemmas → smt_tactic unit meta def add_ematch_lemma_core (md : transparency) (as_simp : bool) (e : expr) : smt_tactic unit := do h ← hinst_lemma.mk_core md e as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_lemma_from_decl_core (md : transparency) (as_simp : bool) (n : name) : smt_tactic unit := do h ← hinst_lemma.mk_from_decl_core md n as_simp, add_lemmas (mk_hinst_singleton h) meta def add_ematch_eqn_lemmas_for_core (md : transparency) (n : name) : smt_tactic unit := do hs ← mk_ematch_eqn_lemmas_for_core md n, add_lemmas hs meta def ematch : smt_tactic unit := ematch_core (λ _, tt) meta def failed {α} : smt_tactic α := tactic.failed meta def fail {α : Type} {β : Type u} [has_to_format β] (msg : β) : tactic α := tactic.fail msg meta def try {α : Type} (t : smt_tactic α) : smt_tactic unit := λ ss ts, result.cases_on (t ss ts) (λ ⟨a, new_ss⟩, result.success ((), new_ss)) (λ e ref s', result.success ((), ss) ts) /- (repeat_at_most n t): repeat the given tactic at most n times or until t fails -/ meta def repeat_at_most : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := (do t, repeat_at_most n t) <|> return () /-- (repeat_exactly n t) : execute t n times -/ meta def repeat_exactly : nat → smt_tactic unit → smt_tactic unit | 0 t := return () | (n+1) t := do t, repeat_exactly n t meta def repeat : smt_tactic unit → smt_tactic unit := repeat_at_most 100000 meta def eblast : smt_tactic unit := repeat (ematch >> try close) open tactic protected meta def read : smt_tactic (smt_state × tactic_state) := do s₁ ← state_t.read, s₂ ← tactic.read, return (s₁, s₂) protected meta def write : smt_state × tactic_state → smt_tactic unit := λ ⟨ss, ts⟩ _ _, result.success ((), ss) ts private meta def mk_smt_goals_for (cfg : smt_config) : list expr → list smt_goal → list expr → tactic (list smt_goal × list expr) | [] sr tr := return (sr^.reverse, tr^.reverse) | (tg::tgs) sr tr := do tactic.set_goals [tg], [new_sg] ← smt_state.mk cfg | tactic.failed, [new_tg] ← get_goals | tactic.failed, mk_smt_goals_for tgs (new_sg::sr) (new_tg::tr) /- See slift -/ meta def slift_aux {α : Type} (t : tactic α) (cfg : smt_config) : smt_tactic α := λ ss, do _::sgs ← return ss | fail "slift tactic failed, there no smt goals to be solved", tg::tgs ← tactic.get_goals | tactic.failed, tactic.set_goals [tg], a ← t, new_tgs ← tactic.get_goals, (new_sgs, new_tgs) ← mk_smt_goals_for cfg new_tgs [] [], tactic.set_goals (new_tgs ++ tgs), return (a, new_sgs ++ sgs) /-- This lift operation will restart the SMT state. It is useful for using tactics that change the set of hypotheses. -/ meta def slift {α : Type} (t : tactic α) : smt_tactic α := get_config >>= slift_aux t meta def trace_state : smt_tactic unit := do (s₁, s₂) ← smt_tactic.read, trace (smt_state.to_format s₁ s₂) meta def trace {α : Type} [has_to_tactic_format α] (a : α) : smt_tactic unit := tactic.trace a meta def to_expr (q : pexpr) (allow_mvars := tt) (report_errors := ff) : smt_tactic expr := tactic.to_expr q allow_mvars report_errors meta def classical : smt_tactic bool := do s ← state_t.read, return s^.classical meta def num_goals : smt_tactic nat := λ ss, return (ss^.length, ss) /- Low level primitives for managing set of goals -/ meta def get_goals : smt_tactic (list smt_goal × list expr) := do (g₁, _) ← smt_tactic.read, g₂ ← tactic.get_goals, return (g₁, g₂) meta def set_goals : list smt_goal → list expr → smt_tactic unit := λ g₁ g₂ ss, tactic.set_goals g₂ >> return ((), g₁) private meta def all_goals_core (tac : smt_tactic unit) : list smt_goal → list expr → list smt_goal → list expr → smt_tactic unit | [] ts acs act := set_goals acs (ts ++ act) | (s :: ss) [] acs act := fail "ill-formed smt_state" | (s :: ss) (t :: ts) acs act := do set_goals [s] [t], tac, (new_ss, new_ts) ← get_goals, all_goals_core ss ts (acs ++ new_ss) (act ++ new_ts) /- Apply the given tactic to all goals. -/ meta def all_goals (tac : smt_tactic unit) : smt_tactic unit := do (ss, ts) ← get_goals, all_goals_core tac ss ts [] [] /- LCF-style AND_THEN tactic. It applies tac1, and if succeed applies tac2 to each subgoal produced by tac1 -/ meta def seq (tac1 : smt_tactic unit) (tac2 : smt_tactic unit) : smt_tactic unit := do (s::ss, t::ts) ← get_goals, set_goals [s] [t], tac1, all_goals tac2, (new_ss, new_ts) ← get_goals, set_goals (new_ss ++ ss) (new_ts ++ ts) meta instance : has_andthen (smt_tactic unit) := ⟨seq⟩ meta def focus1 {α} (tac : smt_tactic α) : smt_tactic α := do (s::ss, t::ts) ← get_goals, match ss with | [] := tac | _ := do set_goals [s] [t], a ← tac, (ss', ts') ← get_goals, set_goals (ss' ++ ss) (ts' ++ ts), return a end meta def solve1 (tac : smt_tactic unit) : smt_tactic unit := do (ss, gs) ← get_goals, match ss, gs with | [], _ := fail "solve1 tactic failed, there isn't any goal left to focus" | _, [] := fail "solve1 tactic failed, there isn't any smt goal left to focus" | s::ss, g::gs := do set_goals [s] [g], tac, (ss', gs') ← get_goals, match ss', gs' with | [], [] := set_goals ss gs | _, _ := fail "solve1 tactic failed, focused goal has not been solved" end end meta def swap : smt_tactic unit := do (ss, ts) ← get_goals, match ss, ts with | (s₁ :: s₂ :: ss), (t₁ :: t₂ :: ts) := set_goals (s₂ :: s₁ :: ss) (t₂ :: t₁ :: ts) | _, _ := failed end /-- Add a new goal for t, and the hypothesis (h : t) in the current goal. -/ meta def assert (h : name) (t : expr) : smt_tactic unit := tactic.assert_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t) in the current goal if v has type t. -/ meta def assertv (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.assertv_core h t v >> intros >> return () /-- Add a new goal for t, and the hypothesis (h : t := ?M) in the current goal. -/ meta def define (h : name) (t : expr) : smt_tactic unit := tactic.define_core h t >> swap >> intros >> swap >> try close /-- Add the hypothesis (h : t := v) in the current goal if v has type t. -/ meta def definev (h : name) (t : expr) (v : expr) : smt_tactic unit := tactic.definev_core h t v >> intros >> return () /-- Add (h : t := pr) to the current goal -/ meta def pose (h : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, definev h t pr /- Add (h : t) to the current goal, given a proof (pr : t) -/ meta def note (n : name) (pr : expr) : smt_tactic unit := do t ← tactic.infer_type pr, assertv n t pr meta def destruct (e : expr) : smt_tactic unit := smt_tactic.seq (tactic.destruct e) smt_tactic.intros meta def by_cases (e : expr) : smt_tactic unit := do c ← classical, if c then destruct (expr.app (expr.const `classical.em []) e) else do dec_e ← (mk_app `decidable [e] <|> fail "by_cases smt_tactic failed, type is not a proposition"), inst ← (mk_instance dec_e <|> fail "by_cases smt_tactic failed, type of given expression is not decidable"), em ← mk_app `decidable.em [e, inst], destruct em meta def by_contradiction : smt_tactic unit := do t ← target, c ← classical, if t^.is_false then skip else if c then do apply (expr.app (expr.const `classical.by_contradiction []) t), intros else do dec_t ← (mk_app `decidable [t] <|> fail "by_contradiction smt_tactic failed, target is not a proposition"), inst ← (mk_instance dec_t <|> fail "by_contradiction smt_tactic failed, target is not decidable"), a ← mk_mapp `decidable.by_contradiction [some t, some inst], apply a, intros /- Return a proof for e, if 'e' is a known fact in the main goal. -/ meta def proof_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.proof_for e /- Return a refutation for e (i.e., a proof for (not e)), if 'e' has been refuted in the main goal. -/ meta def refutation_for (e : expr) : smt_tactic expr := do cc ← to_cc_state, cc^.refutation_for e meta def get_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_true meta def get_refuted_facts : smt_tactic (list expr) := do cc ← to_cc_state, return $ cc^.eqc_of expr.mk_false meta def add_ematch_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible ff meta def add_ematch_lhs_lemma : expr → smt_tactic unit := add_ematch_lemma_core reducible tt meta def add_ematch_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_lhs_lemma_from_decl : name → smt_tactic unit := add_ematch_lemma_from_decl_core reducible ff meta def add_ematch_eqn_lemmas_for : name → smt_tactic unit := add_ematch_eqn_lemmas_for_core reducible meta def add_lemmas_from_facts_core : list expr → smt_tactic unit | [] := return () | (f::fs) := do try (is_prop f >> guard (f^.is_pi && bnot (f^.is_arrow)) >> proof_for f >>= add_ematch_lemma_core reducible ff), add_lemmas_from_facts_core fs meta def add_lemmas_from_facts : smt_tactic unit := get_facts >>= add_lemmas_from_facts_core meta def induction (e : expr) (ids : list name := []) (rec : option name := none) : smt_tactic unit := slift (tactic.induction e ids rec >> return ()) -- pass on the information? meta def when (c : Prop) [decidable c] (tac : smt_tactic unit) : smt_tactic unit := if c then tac else skip meta def when_tracing (n : name) (tac : smt_tactic unit) : smt_tactic unit := when (is_trace_enabled_for n = tt) tac end smt_tactic open smt_tactic meta def using_smt {α} (t : smt_tactic α) (cfg : smt_config := {}) : tactic α := do ss ← smt_state.mk cfg, (a, _) ← (do a ← t, repeat close, return a) ss, return a meta def using_smt_with {α} (cfg : smt_config) (t : smt_tactic α) : tactic α := using_smt t cfg
73eb10af80af3ed8671b9d51c7795fc89496e84b
a0e23cfdd129a671bf3154ee1a8a3a72bf4c7940
/stage0/src/Lean/Data/Json/FromToJson.lean
63b7c38faf989f2b319bd03931145b22cc8fed12
[ "Apache-2.0" ]
permissive
WojciechKarpiel/lean4
7f89706b8e3c1f942b83a2c91a3a00b05da0e65b
f6e1314fa08293dea66a329e05b6c196a0189163
refs/heads/master
1,686,633,402,214
1,625,821,189,000
1,625,821,258,000
384,640,886
0
0
Apache-2.0
1,625,903,617,000
1,625,903,026,000
null
UTF-8
Lean
false
false
2,126
lean
/- Copyright (c) 2019 Gabriel Ebner. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Gabriel Ebner, Marc Huisinga -/ import Lean.Data.Json.Basic namespace Lean universe u class FromJson (α : Type u) where fromJson? : Json → Except String α export FromJson (fromJson?) class ToJson (α : Type u) where toJson : α → Json export ToJson (toJson) instance : FromJson Json := ⟨Except.ok⟩ instance : ToJson Json := ⟨id⟩ instance : FromJson JsonNumber := ⟨Json.getNum?⟩ instance : ToJson JsonNumber := ⟨Json.num⟩ -- looks like id, but there are coercions happening instance : FromJson Bool := ⟨Json.getBool?⟩ instance : ToJson Bool := ⟨fun b => b⟩ instance : FromJson Nat := ⟨Json.getNat?⟩ instance : ToJson Nat := ⟨fun n => n⟩ instance : FromJson Int := ⟨Json.getInt?⟩ instance : ToJson Int := ⟨fun n => Json.num n⟩ instance : FromJson String := ⟨Json.getStr?⟩ instance : ToJson String := ⟨fun s => s⟩ instance [FromJson α] : FromJson (Array α) where fromJson? | Json.arr a => a.mapM fromJson? | _ => throw "JSON array expected" instance [ToJson α] : ToJson (Array α) := ⟨fun a => Json.arr (a.map toJson)⟩ instance [FromJson α] : FromJson (Option α) where fromJson? | Json.null => Except.ok none | j => some <$> fromJson? j instance [ToJson α] : ToJson (Option α) := ⟨fun | none => Json.null | some a => toJson a⟩ namespace Json instance : FromJson Structured := ⟨fun | arr a => Structured.arr a | obj o => Structured.obj o | _ => throw "structured object expected"⟩ instance : ToJson Structured := ⟨fun | Structured.arr a => arr a | Structured.obj o => obj o⟩ def toStructured? [ToJson α] (v : α) : Except String Structured := fromJson? (toJson v) def getObjValAs? (j : Json) (α : Type u) [FromJson α] (k : String) : Except String α := fromJson? <| j.getObjValD k def opt [ToJson α] (k : String) : Option α → List (String × Json) | none => [] | some o => [⟨k, toJson o⟩] end Json end Lean
cbc48c4e06678d33fd35db19eaf4dd1fd6d62a29
57c233acf9386e610d99ed20ef139c5f97504ba3
/src/analysis/normed_space/extend.lean
f26c5aeb1c0b3882b9c2fbd4987771e346056d03
[ "Apache-2.0" ]
permissive
robertylewis/mathlib
3d16e3e6daf5ddde182473e03a1b601d2810952c
1d13f5b932f5e40a8308e3840f96fc882fae01f0
refs/heads/master
1,651,379,945,369
1,644,276,960,000
1,644,276,960,000
98,875,504
0
0
Apache-2.0
1,644,253,514,000
1,501,495,700,000
Lean
UTF-8
Lean
false
false
6,751
lean
/- Copyright (c) 2020 Ruben Van de Velde. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Ruben Van de Velde -/ import algebra.algebra.restrict_scalars import data.complex.is_R_or_C /-! # Extending a continuous `ℝ`-linear map to a continuous `𝕜`-linear map In this file we provide a way to extend a continuous `ℝ`-linear map to a continuous `𝕜`-linear map in a way that bounds the norm by the norm of the original map, when `𝕜` is either `ℝ` (the extension is trivial) or `ℂ`. We formulate the extension uniformly, by assuming `is_R_or_C 𝕜`. We motivate the form of the extension as follows. Note that `fc : F →ₗ[𝕜] 𝕜` is determined fully by `Re fc`: for all `x : F`, `fc (I • x) = I * fc x`, so `Im (fc x) = -Re (fc (I • x))`. Therefore, given an `fr : F →ₗ[ℝ] ℝ`, we define `fc x = fr x - fr (I • x) * I`. ## Main definitions * `linear_map.extend_to_𝕜` * `continuous_linear_map.extend_to_𝕜` ## Implementation details For convenience, the main definitions above operate in terms of `restrict_scalars ℝ 𝕜 F`. Alternate forms which operate on `[is_scalar_tower ℝ 𝕜 F]` instead are provided with a primed name. -/ open is_R_or_C variables {𝕜 : Type*} [is_R_or_C 𝕜] {F : Type*} [semi_normed_group F] [normed_space 𝕜 F] local notation `abs𝕜` := @is_R_or_C.abs 𝕜 _ /-- Extend `fr : F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜` in a way that will also be continuous and have its norm bounded by `∥fr∥` if `fr` is continuous. -/ noncomputable def linear_map.extend_to_𝕜' [module ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := begin let fc : F → 𝕜 := λ x, (fr x : 𝕜) - (I : 𝕜) * (fr ((I : 𝕜) • x)), have add : ∀ x y : F, fc (x + y) = fc x + fc y, { assume x y, simp only [fc], simp only [smul_add, linear_map.map_add, of_real_add], rw mul_add, abel, }, have A : ∀ (c : ℝ) (x : F), (fr ((c : 𝕜) • x) : 𝕜) = (c : 𝕜) * (fr x : 𝕜), { assume c x, rw [← of_real_mul], congr' 1, rw [is_R_or_C.of_real_alg, smul_assoc, fr.map_smul, algebra.id.smul_eq_mul, one_smul] }, have smul_ℝ : ∀ (c : ℝ) (x : F), fc ((c : 𝕜) • x) = (c : 𝕜) * fc x, { assume c x, simp only [fc, A], rw A c x, rw [smul_smul, mul_comm I (c : 𝕜), ← smul_smul, A, mul_sub], ring }, have smul_I : ∀ x : F, fc ((I : 𝕜) • x) = (I : 𝕜) * fc x, { assume x, simp only [fc], cases @I_mul_I_ax 𝕜 _ with h h, { simp [h] }, rw [mul_sub, ← mul_assoc, smul_smul, h], simp only [neg_mul_eq_neg_mul_symm, linear_map.map_neg, one_mul, one_smul, mul_neg_eq_neg_mul_symm, of_real_neg, neg_smul, sub_neg_eq_add, add_comm] }, have smul_𝕜 : ∀ (c : 𝕜) (x : F), fc (c • x) = c • fc x, { assume c x, rw [← re_add_im c, add_smul, add_smul, add, smul_ℝ, ← smul_smul, smul_ℝ, smul_I, ← mul_assoc], refl }, exact { to_fun := fc, map_add' := add, map_smul' := smul_𝕜 } end lemma linear_map.extend_to_𝕜'_apply [module ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl /-- The norm of the extension is bounded by `∥fr∥`. -/ lemma norm_bound [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) (x : F) : ∥(fr.to_linear_map.extend_to_𝕜' x : 𝕜)∥ ≤ ∥fr∥ * ∥x∥ := begin let lm : F →ₗ[𝕜] 𝕜 := fr.to_linear_map.extend_to_𝕜', -- We aim to find a `t : 𝕜` such that -- * `lm (t • x) = fr (t • x)` (so `lm (t • x) = t * lm x ∈ ℝ`) -- * `∥lm x∥ = ∥lm (t • x)∥` (so `t.abs` must be 1) -- If `lm x ≠ 0`, `(lm x)⁻¹` satisfies the first requirement, and after normalizing, it -- satisfies the second. -- (If `lm x = 0`, the goal is trivial.) classical, by_cases h : lm x = 0, { rw [h, norm_zero], apply mul_nonneg; exact norm_nonneg _ }, let fx := (lm x)⁻¹, let t := fx / (abs𝕜 fx : 𝕜), have ht : abs𝕜 t = 1, by field_simp [abs_of_real, of_real_inv, is_R_or_C.abs_inv, is_R_or_C.abs_div, is_R_or_C.abs_abs, h], have h1 : (fr (t • x) : 𝕜) = lm (t • x), { apply ext, { simp only [lm, of_real_re, linear_map.extend_to_𝕜'_apply, mul_re, I_re, of_real_im, zero_mul, add_monoid_hom.map_sub, sub_zero, mul_zero], refl }, { symmetry, calc im (lm (t • x)) = im (t * lm x) : by rw [lm.map_smul, smul_eq_mul] ... = im ((lm x)⁻¹ / (abs𝕜 (lm x)⁻¹) * lm x) : rfl ... = im (1 / (abs𝕜 (lm x)⁻¹ : 𝕜)) : by rw [div_mul_eq_mul_div, inv_mul_cancel h] ... = 0 : by rw [← of_real_one, ← of_real_div, of_real_im] ... = im (fr (t • x) : 𝕜) : by rw [of_real_im] } }, calc ∥lm x∥ = abs𝕜 t * ∥lm x∥ : by rw [ht, one_mul] ... = ∥t * lm x∥ : by rw [← norm_eq_abs, normed_field.norm_mul] ... = ∥lm (t • x)∥ : by rw [←smul_eq_mul, lm.map_smul] ... = ∥(fr (t • x) : 𝕜)∥ : by rw h1 ... = ∥fr (t • x)∥ : by rw [norm_eq_abs, abs_of_real, norm_eq_abs, abs_to_real] ... ≤ ∥fr∥ * ∥t • x∥ : continuous_linear_map.le_op_norm _ _ ... = ∥fr∥ * (∥t∥ * ∥x∥) : by rw norm_smul ... ≤ ∥fr∥ * ∥x∥ : by rw [norm_eq_abs, ht, one_mul] end /-- Extend `fr : F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def continuous_linear_map.extend_to_𝕜' [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := linear_map.mk_continuous _ (∥fr∥) (norm_bound _) lemma continuous_linear_map.extend_to_𝕜'_apply [normed_space ℝ F] [is_scalar_tower ℝ 𝕜 F] (fr : F →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜' x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl /-- Extend `fr : restrict_scalars ℝ 𝕜 F →ₗ[ℝ] ℝ` to `F →ₗ[𝕜] 𝕜`. -/ noncomputable def linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) : F →ₗ[𝕜] 𝕜 := fr.extend_to_𝕜' lemma linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →ₗ[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl /-- Extend `fr : restrict_scalars ℝ 𝕜 F →L[ℝ] ℝ` to `F →L[𝕜] 𝕜`. -/ noncomputable def continuous_linear_map.extend_to_𝕜 (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) : F →L[𝕜] 𝕜 := fr.extend_to_𝕜' lemma continuous_linear_map.extend_to_𝕜_apply (fr : (restrict_scalars ℝ 𝕜 F) →L[ℝ] ℝ) (x : F) : fr.extend_to_𝕜 x = (fr x : 𝕜) - (I : 𝕜) * fr ((I : 𝕜) • x) := rfl
68476495eca6ab3e1fb8ebe7e7986ae00f728df8
4727251e0cd73359b15b664c3170e5d754078599
/src/analysis/special_functions/log/monotone.lean
665632df01b50d17c2d0edbc1076382c60896945
[ "Apache-2.0" ]
permissive
Vierkantor/mathlib
0ea59ac32a3a43c93c44d70f441c4ee810ccceca
83bc3b9ce9b13910b57bda6b56222495ebd31c2f
refs/heads/master
1,658,323,012,449
1,652,256,003,000
1,652,256,003,000
209,296,341
0
1
Apache-2.0
1,568,807,655,000
1,568,807,655,000
null
UTF-8
Lean
false
false
3,281
lean
/- Copyright (c) 2021 Bolton Bailey. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bolton Bailey -/ import analysis.special_functions.log.basic import analysis.special_functions.pow /-! # Logarithm Tonality In this file we describe the tonality of the logarithm function when multiplied by functions of the form `x ^ a`. ## Tags logarithm, tonality -/ open set filter function open_locale topological_space noncomputable theory namespace real variables {x y : ℝ} lemma log_mul_self_monotone_on : monotone_on (λ x : ℝ, log x * x) {x | 1 ≤ x} := begin -- TODO: can be strengthened to exp (-1) ≤ x simp only [monotone_on, mem_set_of_eq], intros x hex y hey hxy, have x_pos : 0 < x := lt_of_lt_of_le zero_lt_one hex, have y_pos : 0 < y := lt_of_lt_of_le zero_lt_one hey, refine mul_le_mul ((log_le_log x_pos y_pos).mpr hxy) hxy (le_of_lt x_pos) _, rwa [le_log_iff_exp_le y_pos, real.exp_zero], end lemma log_div_self_antitone_on : antitone_on (λ x : ℝ, log x / x) {x | exp 1 ≤ x} := begin simp only [antitone_on, mem_set_of_eq], intros x hex y hey hxy, have x_pos : 0 < x := (exp_pos 1).trans_le hex, have y_pos : 0 < y := (exp_pos 1).trans_le hey, have hlogx : 1 ≤ log x := by rwa le_log_iff_exp_le x_pos, have hyx : 0 ≤ y / x - 1 := by rwa [le_sub_iff_add_le, le_div_iff x_pos, zero_add, one_mul], rw [div_le_iff y_pos, ←sub_le_sub_iff_right (log x)], calc log y - log x = log (y / x) : by rw [log_div (y_pos.ne') (x_pos.ne')] ... ≤ (y / x) - 1 : log_le_sub_one_of_pos (div_pos y_pos x_pos) ... ≤ log x * (y / x - 1) : le_mul_of_one_le_left hyx hlogx ... = log x / x * y - log x : by ring, end lemma log_div_self_rpow_antitone_on {a : ℝ} (ha : 0 < a) : antitone_on (λ x : ℝ, log x / x ^ a) {x | exp (1 / a) ≤ x} := begin simp only [antitone_on, mem_set_of_eq], intros x hex y hey hxy, have x_pos : 0 < x := lt_of_lt_of_le (exp_pos (1 / a)) hex, have y_pos : 0 < y := by linarith, have x_nonneg : 0 ≤ x := le_trans (le_of_lt (exp_pos (1 / a))) hex, have y_nonneg : 0 ≤ y := by linarith, nth_rewrite 0 ←rpow_one y, nth_rewrite 0 ←rpow_one x, rw [←div_self (ne_of_lt ha).symm, div_eq_mul_one_div a a, rpow_mul y_nonneg, rpow_mul x_nonneg, log_rpow (rpow_pos_of_pos y_pos a), log_rpow (rpow_pos_of_pos x_pos a), mul_div_assoc, mul_div_assoc, mul_le_mul_left (one_div_pos.mpr ha)], { refine log_div_self_antitone_on _ _ _, { simp only [set.mem_set_of_eq], convert rpow_le_rpow _ hex (le_of_lt ha), rw ←exp_mul, simp only [real.exp_eq_exp], field_simp [(ne_of_lt ha).symm], exact le_of_lt (exp_pos (1 / a)), }, { simp only [set.mem_set_of_eq], convert rpow_le_rpow _ (trans hex hxy) (le_of_lt ha), rw ←exp_mul, simp only [real.exp_eq_exp], field_simp [(ne_of_lt ha).symm], exact le_of_lt (exp_pos (1 / a)), }, exact rpow_le_rpow x_nonneg hxy (le_of_lt ha), }, end lemma log_div_sqrt_antitone_on : antitone_on (λ x : ℝ, log x / sqrt x) {x | exp 2 ≤ x} := begin simp_rw sqrt_eq_rpow, convert @log_div_self_rpow_antitone_on (1 / 2) (by norm_num), norm_num, end end real
2861114c16530c829f9f7153635d914a08395baf
b7f22e51856f4989b970961f794f1c435f9b8f78
/tests/lean/run/unfold_test.lean
73fb33d92aa174a1f97d6b2df0fcea1d6a454867
[ "Apache-2.0" ]
permissive
soonhokong/lean
cb8aa01055ffe2af0fb99a16b4cda8463b882cd1
38607e3eb57f57f77c0ac114ad169e9e4262e24f
refs/heads/master
1,611,187,284,081
1,450,766,737,000
1,476,122,547,000
11,513,992
2
0
null
1,401,763,102,000
1,374,182,235,000
C++
UTF-8
Lean
false
false
1,483
lean
import data.list data.examples.vector variables {A B : Type} section open list theorem last_concat {x : A} : ∀ {l : list A} (h : concat x l ≠ []), last (concat x l) h = x | [] h := rfl | [a] h := rfl | (a₁::a₂::l) h := by xrewrite [↑concat, ↑concat, last_cons_cons, ↓concat x (a₂::l), last_concat] theorem reverse_append : ∀ (s t : list A), reverse (s ++ t) = (reverse t) ++ (reverse s) | [] t2 := by esimp [append, reverse]; rewrite append_nil_right | (a2 :: s2) t2 := by rewrite [↑append, ↑reverse, reverse_append, concat_eq_append, append.assoc, -concat_eq_append] end section open vector nat prod theorem unzip_zip : ∀ {n : nat} (v₁ : vector A n) (v₂ : vector B n), unzip (zip v₁ v₂) = (v₁, v₂) | 0 [] [] := rfl | (n+1) (a::va) (b::vb) := by rewrite [↑zip, ↑unzip, unzip_zip] theorem zip_unzip : ∀ {n : nat} (v : vector (A × B) n), zip (pr₁ (unzip v)) (pr₂ (unzip v)) = v | 0 [] := rfl | (n+1) ((a, b) :: v) := by rewrite [↑unzip,↑zip,zip_unzip] theorem reverse_concat : Π {n : nat} (xs : vector A n) (a : A), reverse (concat xs a) = a :: reverse xs | 0 [] a := rfl | (n+1) (x :: xs) a := by xrewrite [↑concat,↑reverse,reverse_concat] theorem reverse_reverse : Π {n : nat} (xs : vector A n), reverse (reverse xs) = xs | 0 [] := rfl | (succ n) (x :: xs) := by rewrite [↑reverse at {1}, reverse_concat, reverse_reverse] end
1448db02161976e4311c35f1d33db1a23a73a0d0
7cdf3413c097e5d36492d12cdd07030eb991d394
/src/game/world3/level8.lean
a047ed6e2eacc64e488048c1c146eea7ce2bc4ed
[]
no_license
alreadydone/natural_number_game
3135b9385a9f43e74cfbf79513fc37e69b99e0b3
1a39e693df4f4e871eb449890d3c7715a25c2ec9
refs/heads/master
1,599,387,390,105
1,573,200,587,000
1,573,200,691,000
220,397,084
0
0
null
1,573,192,734,000
1,573,192,733,000
null
UTF-8
Lean
false
false
1,104
lean
import game.world3.level7 -- hide namespace mynat -- hide /- # Multiplication World ## Level 8: `mul_comm` Finally, the boss level of multiplication world. But (assuming you didn't cheat) you are well-prepared for it -- you have `zero_mul` and `mul_zero`, as well as `succ_mul` and `mul_succ`. After this level you can of course throw away one of each pair if you like, but I would recommend you hold on to them, sometimes it's convenient to have exactly the right tools to do a job. -/ /- Lemma Multiplication is commutative. -/ lemma mul_comm (a b : mynat) : a * b = b * a := begin [less_leaky] induction b with d hd, { rw zero_mul, rw mul_zero, refl, }, { rw succ_mul, rw ←hd, rw mul_succ, refl, } end /- You've now proved that the natural numbers are a commutative semiring! That's the last collectible in Multiplication World. -/ instance mynat.comm_semiring : comm_semiring mynat := by structure_helper /- But don't leave multiplication just yet -- prove `mul_left_comm`, the last level of the world, and I'll show you a trick. -/ end mynat -- hide
af4cf34aefdaca181add81699231ad165eb7f693
55c7fc2bf55d496ace18cd6f3376e12bb14c8cc5
/archive/100-theorems-list/73_ascending_descending_sequences.lean
3a80df060fe0154956af87f13a6fd2e5685e67c9
[ "Apache-2.0" ]
permissive
dupuisf/mathlib
62de4ec6544bf3b79086afd27b6529acfaf2c1bb
8582b06b0a5d06c33ee07d0bdf7c646cae22cf36
refs/heads/master
1,669,494,854,016
1,595,692,409,000
1,595,692,409,000
272,046,630
0
0
Apache-2.0
1,592,066,143,000
1,592,066,142,000
null
UTF-8
Lean
false
false
8,116
lean
/- Copyright (c) 2020 Bhavik Mehta. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Bhavik Mehta -/ import tactic.basic import data.fintype.basic /-! # Erdős–Szekeres theorem This file proves Theorem 73 from the [100 Theorems List](https://www.cs.ru.nl/~freek/100/), also known as the Erdős–Szekeres theorem: given a sequence of more than `r * s` distinct values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`. We use the proof outlined at https://en.wikipedia.org/wiki/Erdos-Szekeres_theorem#Pigeonhole_principle. ## Tags sequences, increasing, decreasing, Ramsey, Erdos-Szekeres, Erdős–Szekeres, Erdős-Szekeres -/ variables {α : Type*} [linear_order α] {β : Type*} open function finset open_locale classical /-- Given a sequence of more than `r * s` distinct values, there is an increasing sequence of length longer than `r` or a decreasing sequence of length longer than `s`. Proof idea: We label each value in the sequence with two numbers specifying the longest increasing subsequence ending there, and the longest decreasing subsequence ending there. We then show the pair of labels must be unique. Now if there is no increasing sequence longer than `r` and no decreasing sequence longer than `s`, then there are at most `r * s` possible labels, which is a contradiction if there are more than `r * s` elements. -/ theorem erdos_szekeres {r s n : ℕ} {f : fin n → α} (hn : r * s < n) (hf : injective f) : (∃ (t : finset (fin n)), r < t.card ∧ strict_mono_incr_on f ↑t) ∨ (∃ (t : finset (fin n)), s < t.card ∧ strict_mono_decr_on f ↑t) := begin -- Given an index `i`, produce the set of increasing (resp., decreasing) subsequences which ends -- at `i`. let inc_sequences_ending_in : fin n → finset (finset (fin n)) := λ i, univ.powerset.filter (λ t, finset.max t = some i ∧ strict_mono_incr_on f ↑t), let dec_sequences_ending_in : fin n → finset (finset (fin n)) := λ i, univ.powerset.filter (λ t, finset.max t = some i ∧ strict_mono_decr_on f ↑t), -- The singleton sequence is in both of the above collections. -- (This is useful to show that the maximum length subsequence is at least 1, and that the set -- of subsequences is nonempty.) have inc_i : ∀ i, {i} ∈ inc_sequences_ending_in i := λ i, by simp [strict_mono_incr_on], have dec_i : ∀ i, {i} ∈ dec_sequences_ending_in i := λ i, by simp [strict_mono_decr_on], -- Define the pair of labels: at index `i`, the pair is the maximum length of an increasing -- subsequence ending at `i`, paired with the maximum length of a decreasing subsequence ending -- at `i`. -- We call these labels `(a_i, b_i)`. let ab : fin n → ℕ × ℕ, { intro i, apply (max' ((inc_sequences_ending_in i).image card) (nonempty.image ⟨{i}, inc_i i⟩ _), max' ((dec_sequences_ending_in i).image card) (nonempty.image ⟨{i}, dec_i i⟩ _)) }, -- It now suffices to show that one of the labels is 'big' somewhere. In particular, if the -- first in the pair is more than `r` somewhere, then we have an increasing subsequence in our -- set, and if the second is more than `s` somewhere, then we have a decreasing subsequence. suffices : ∃ i, r < (ab i).1 ∨ s < (ab i).2, { obtain ⟨i, hi⟩ := this, apply or.imp _ _ hi, work_on_goal 0 { have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 1 { have : (ab i).2 ∈ _ := max'_mem _ _ }, all_goals { intro hi, rw mem_image at this, obtain ⟨t, ht₁, ht₂⟩ := this, refine ⟨t, by rwa ht₂, _⟩, rw mem_filter at ht₁, apply ht₁.2.2 } }, -- Show first that the pair of labels is unique. have : injective ab, { apply injective_of_lt_imp_ne, intros i j k q, injection q with q₁ q₂, -- We have two cases: `f i < f j` or `f j < f i`. -- In the former we'll show `a_i < a_j`, and in the latter we'll show `b_i < b_j`. cases lt_or_gt_of_ne (λ _, ne_of_lt ‹i < j› (hf ‹f i = f j›)), work_on_goal 0 { apply ne_of_lt _ q₁, have : (ab i).1 ∈ _ := max'_mem _ _ }, work_on_goal 1 { apply ne_of_lt _ q₂, have : (ab i).2 ∈ _ := max'_mem _ _ }, all_goals { -- Reduce to showing there is a subsequence of length `a_i + 1` which ends at `j`. rw nat.lt_iff_add_one_le, apply le_max', rw mem_image at this ⊢, -- In particular we take the subsequence `t` of length `a_i` which ends at `i`, by definition of `a_i` rcases this with ⟨t, ht₁, ht₂⟩, rw mem_filter at ht₁, -- Ensure `t` ends at `i`. have : i ∈ t.max, simp [ht₁.2.1], -- Now our new subsequence is given by adding `j` at the end of `t`. refine ⟨insert j t, _, _⟩, -- First make sure it's valid, i.e., that this subsequence ends at `j` and is increasing { rw mem_filter, refine ⟨_, _, _⟩, { rw mem_powerset, apply subset_univ }, -- It ends at `j` since `i < j`. { rw [max_insert, ht₁.2.1, option.lift_or_get_some_some, max_eq_left, with_top.some_eq_coe], apply le_of_lt ‹i < j› }, -- To show it's increasing (i.e., `f` is monotone increasing on `t.insert j`), we do cases on -- what the possibilities could be - either in `t` or equals `j`. simp only [strict_mono_incr_on, strict_mono_decr_on, coe_insert, set.mem_insert_iff, mem_coe], -- Most of the cases are just bashes. rintros x ⟨rfl | _⟩ y ⟨rfl | _⟩ _, { apply (irrefl _ ‹j < j›).elim }, { exfalso, apply not_le_of_lt (trans ‹i < j› ‹j < y›) (le_max_of_mem ‹y ∈ t› ‹i ∈ t.max›) }, { apply lt_of_le_of_lt _ ‹f i < f j› <|> apply lt_of_lt_of_le ‹f j < f i› _, rcases lt_or_eq_of_le (le_max_of_mem ‹x ∈ t› ‹i ∈ t.max›) with _ | rfl, { apply le_of_lt (ht₁.2.2 _ ‹x ∈ t› i (mem_of_max ‹i ∈ t.max›) ‹x < i›) }, { refl } }, { apply ht₁.2.2 _ ‹x ∈ t› _ ‹y ∈ t› ‹x < y› } }, -- Finally show that this new subsequence is one longer than the old one. { rw [card_insert_of_not_mem, ht₂], intro _, apply not_le_of_lt ‹i < j› (le_max_of_mem ‹j ∈ t› ‹i ∈ t.max›) } } }, -- Finished both goals! -- Now that we have uniqueness of each label, it remains to do some counting to finish off. -- Suppose all the labels are small. by_contra q, push_neg at q, -- Then the labels `(a_i, b_i)` all fit in the following set: `{ (x,y) | 1 ≤ x ≤ r, 1 ≤ y ≤ s }` let ran : finset (ℕ × ℕ) := ((range r).image nat.succ).product ((range s).image nat.succ), -- which we prove here. have : image ab univ ⊆ ran, -- First some logical shuffling { rintro ⟨x₁, x₂⟩, simp only [mem_image, exists_prop, mem_range, mem_univ, mem_product, true_and, prod.mk.inj_iff], rintros ⟨i, rfl, rfl⟩, specialize q i, -- Show `1 ≤ a_i` and `1 ≤ b_i`, which is easy from the fact that `{i}` is a increasing and decreasing -- subsequence which we did right near the top. have z : 1 ≤ (ab i).1 ∧ 1 ≤ (ab i).2, { split; { apply le_max', rw mem_image, refine ⟨{i}, by solve_by_elim, card_singleton i⟩ } }, refine ⟨_, _⟩, -- Need to get `a_i ≤ r`, here phrased as: there is some `a < r` with `a+1 = a_i`. { refine ⟨(ab i).1 - 1, _, nat.succ_pred_eq_of_pos z.1⟩, rw nat.sub_lt_right_iff_lt_add z.1, apply nat.lt_succ_of_le q.1 }, { refine ⟨(ab i).2 - 1, _, nat.succ_pred_eq_of_pos z.2⟩, rw nat.sub_lt_right_iff_lt_add z.2, apply nat.lt_succ_of_le q.2 } }, -- To get our contradiction, it suffices to prove `n ≤ r * s` apply not_le_of_lt hn, -- Which follows from considering the cardinalities of the subset above, since `ab` is injective. simpa [nat.succ_injective, card_image_of_injective, ‹injective ab›] using card_le_of_subset this, end
c7a941cc738bc7f55d0231715bfaf9d2d9cdfe7d
63abd62053d479eae5abf4951554e1064a4c45b4
/src/topology/metric_space/cau_seq_filter.lean
843a4df6664db90cdd111f86233a74b2c19dfa11
[ "Apache-2.0" ]
permissive
Lix0120/mathlib
0020745240315ed0e517cbf32e738d8f9811dd80
e14c37827456fc6707f31b4d1d16f1f3a3205e91
refs/heads/master
1,673,102,855,024
1,604,151,044,000
1,604,151,044,000
308,930,245
0
0
Apache-2.0
1,604,164,710,000
1,604,163,547,000
null
UTF-8
Lean
false
false
3,702
lean
/- Copyright (c) 2018 Robert Y. Lewis. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Robert Y. Lewis, Sébastien Gouëzel -/ import analysis.normed_space.basic /-! # Completeness in terms of `cauchy` filters vs `is_cau_seq` sequences In this file we apply `metric.complete_of_cauchy_seq_tendsto` to prove that a `normed_ring` is complete in terms of `cauchy` filter if and only if it is complete in terms of `cau_seq` Cauchy sequences. -/ universes u v open set filter open_locale topological_space classical variable {β : Type v} lemma cau_seq.tendsto_limit [normed_ring β] [hn : is_absolute_value (norm : β → ℝ)] (f : cau_seq β norm) [cau_seq.is_complete β norm] : tendsto f at_top (𝓝 f.lim) := _root_.tendsto_nhds.mpr begin intros s os lfs, suffices : ∃ (a : ℕ), ∀ (b : ℕ), b ≥ a → f b ∈ s, by simpa using this, rcases metric.is_open_iff.1 os _ lfs with ⟨ε, ⟨hε, hεs⟩⟩, cases setoid.symm (cau_seq.equiv_lim f) _ hε with N hN, existsi N, intros b hb, apply hεs, dsimp [metric.ball], rw [dist_comm, dist_eq_norm], solve_by_elim end variables [normed_field β] /- This section shows that if we have a uniform space generated by an absolute value, topological completeness and Cauchy sequence completeness coincide. The problem is that there isn't a good notion of "uniform space generated by an absolute value", so right now this is specific to norm. Furthermore, norm only instantiates is_absolute_value on normed_field. This needs to be fixed, since it prevents showing that ℤ_[hp] is complete -/ instance normed_field.is_absolute_value : is_absolute_value (norm : β → ℝ) := { abv_nonneg := norm_nonneg, abv_eq_zero := λ _, norm_eq_zero, abv_add := norm_add_le, abv_mul := normed_field.norm_mul } open metric lemma cauchy_seq.is_cau_seq {f : ℕ → β} (hf : cauchy_seq f) : is_cau_seq norm f := begin cases cauchy_iff.1 hf with hf1 hf2, intros ε hε, rcases hf2 {x | dist x.1 x.2 < ε} (dist_mem_uniformity hε) with ⟨t, ⟨ht, htsub⟩⟩, simp at ht, cases ht with N hN, existsi N, intros j hj, rw ←dist_eq_norm, apply @htsub (f j, f N), apply set.mk_mem_prod; solve_by_elim [le_refl] end lemma cau_seq.cauchy_seq (f : cau_seq β norm) : cauchy_seq f := begin refine cauchy_iff.2 ⟨by apply_instance, λ s hs, _⟩, rcases mem_uniformity_dist.1 hs with ⟨ε, ⟨hε, hεs⟩⟩, cases cau_seq.cauchy₂ f hε with N hN, existsi {n | n ≥ N}.image f, simp, split, { existsi N, intros b hb, existsi b, simp [hb] }, { rintros ⟨a, b⟩ ⟨⟨a', ⟨ha'1, ha'2⟩⟩, ⟨b', ⟨hb'1, hb'2⟩⟩⟩, dsimp at ha'1 ha'2 hb'1 hb'2, rw [←ha'2, ←hb'2], apply hεs, rw dist_eq_norm, apply hN; assumption } end /-- In a normed field, `cau_seq` coincides with the usual notion of Cauchy sequences. -/ lemma cau_seq_iff_cauchy_seq {α : Type u} [normed_field α] {u : ℕ → α} : is_cau_seq norm u ↔ cauchy_seq u := ⟨λh, cau_seq.cauchy_seq ⟨u, h⟩, λh, h.is_cau_seq⟩ /-- A complete normed field is complete as a metric space, as Cauchy sequences converge by assumption and this suffices to characterize completeness. -/ @[priority 100] -- see Note [lower instance priority] instance complete_space_of_cau_seq_complete [cau_seq.is_complete β norm] : complete_space β := begin apply complete_of_cauchy_seq_tendsto, assume u hu, have C : is_cau_seq norm u := cau_seq_iff_cauchy_seq.2 hu, existsi cau_seq.lim ⟨u, C⟩, rw metric.tendsto_at_top, assume ε εpos, cases (cau_seq.equiv_lim ⟨u, C⟩) _ εpos with N hN, existsi N, simpa [dist_eq_norm] using hN end
bdf3e4cceaf85b338a4f5dde2da789f5827068cb
f57749ca63d6416f807b770f67559503fdb21001
/hott/hit/refl_quotient.hlean
374beaf897f309014f91f8e508d4dec736425bed
[ "Apache-2.0" ]
permissive
aliassaf/lean
bd54e85bed07b1ff6f01396551867b2677cbc6ac
f9b069b6a50756588b309b3d716c447004203152
refs/heads/master
1,610,982,152,948
1,438,916,029,000
1,438,916,029,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
3,409
hlean
/- Copyright (c) 2015 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn Quotient of a reflexive relation -/ import hit.circle types.cubical.squareover .two_quotient open eq simple_two_quotient e_closure namespace refl_quotient section parameters {A : Type} (R : A → A → Type) (ρ : Πa, R a a) inductive refl_quotient_Q : Π⦃a : A⦄, e_closure R a a → Type := | Qmk {} : Π(a : A), refl_quotient_Q [ρ a] open refl_quotient_Q local abbreviation Q := refl_quotient_Q definition refl_quotient : Type := simple_two_quotient R Q -- TODO: define this in root namespace definition rclass_of (a : A) : refl_quotient := incl0 R Q a definition req_of_rel ⦃a a' : A⦄ (r : R a a') : rclass_of a = rclass_of a' := incl1 R Q r definition pρ (a : A) : req_of_rel (ρ a) = idp := incl2 R Q (Qmk a) -- protected definition rec {P : refl_quotient → Type} -- (Pc : Π(a : A), P (rclass_of a)) -- (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[req_of_rel H] Pc a') -- (Pr : Π(a : A), Pp (ρ a) =[pρ a] idpo) -- (x : refl_quotient) : P x := -- sorry -- protected definition rec_on [reducible] {P : refl_quotient → Type} -- (Pc : Π(a : A), P (rclass_of a)) -- (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[req_of_rel H] Pc a') -- (Pr : Π(a : A), Pp (ρ a) =[pρ a] idpo) : P y := -- rec Pinl Pinr Pglue y -- definition rec_req_of_rel {P : Type} {P : refl_quotient → Type} -- (Pc : Π(a : A), P (rclass_of a)) -- (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[req_of_rel H] Pc a') -- (Pr : Π(a : A), Pp (ρ a) =[pρ a] idpo) -- ⦃a a' : A⦄ (r : R a a') : apdo (rec Pc Pp Pr) (req_of_rel r) = Pp r := -- !rec_incl1 -- theorem rec_pρ {P : Type} {P : refl_quotient → Type} -- (Pc : Π(a : A), P (rclass_of a)) -- (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a =[req_of_rel H] Pc a') -- (Pr : Π(a : A), Pp (ρ a) =[pρ a] idpo) (a : A) -- : square (ap02 (rec Pc Pp Pr) (pρ a)) (Pr a) (elim_req_of_rel Pr (ρ a)) idp := -- !rec_incl2 protected definition elim {P : Type} (Pc : Π(a : A), P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') (Pr : Π(a : A), Pp (ρ a) = idp) (x : refl_quotient) : P := begin induction x, exact Pc a, exact Pp s, induction q, apply Pr end protected definition elim_on [reducible] {P : Type} (x : refl_quotient) (Pc : Π(a : A), P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') (Pr : Π(a : A), Pp (ρ a) = idp) : P := elim Pc Pp Pr x definition elim_req_of_rel {P : Type} {Pc : Π(a : A), P} {Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a'} (Pr : Π(a : A), Pp (ρ a) = idp) ⦃a a' : A⦄ (r : R a a') : ap (elim Pc Pp Pr) (req_of_rel r) = Pp r := !elim_incl1 theorem elim_pρ {P : Type} (Pc : Π(a : A), P) (Pp : Π⦃a a' : A⦄ (H : R a a'), Pc a = Pc a') (Pr : Π(a : A), Pp (ρ a) = idp) (a : A) : square (ap02 (elim Pc Pp Pr) (pρ a)) (Pr a) (elim_req_of_rel Pr (ρ a)) idp := !elim_incl2 end end refl_quotient attribute refl_quotient.rclass_of [constructor] attribute /-refl_quotient.rec-/ refl_quotient.elim [unfold 8] [recursor 8] --attribute refl_quotient.elim_type [unfold 9] attribute /-refl_quotient.rec_on-/ refl_quotient.elim_on [unfold 5] --attribute refl_quotient.elim_type_on [unfold 6]
6c27559005fd0dba54dde7baa6328222f69c6e5b
367134ba5a65885e863bdc4507601606690974c1
/src/algebra/module/basic.lean
44dd797f946d87a47aab42913a8a5ff90cf748fd
[ "Apache-2.0" ]
permissive
kodyvajjha/mathlib
9bead00e90f68269a313f45f5561766cfd8d5cad
b98af5dd79e13a38d84438b850a2e8858ec21284
refs/heads/master
1,624,350,366,310
1,615,563,062,000
1,615,563,062,000
162,666,963
0
0
Apache-2.0
1,545,367,651,000
1,545,367,651,000
null
UTF-8
Lean
false
false
21,820
lean
/- Copyright (c) 2015 Nathaniel Thomas. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nathaniel Thomas, Jeremy Avigad, Johannes Hölzl, Mario Carneiro -/ import algebra.big_operators.basic import algebra.group.hom import algebra.ring.basic import data.rat.cast import group_theory.group_action.group import tactic.nth_rewrite /-! # Modules over a ring In this file we define * `semimodule R M` : an additive commutative monoid `M` is a `semimodule` over a `semiring` `R` if for `r : R` and `x : M` their "scalar multiplication `r • x : M` is defined, and the operation `•` satisfies some natural associativity and distributivity axioms similar to those on a ring. * `module R M` : same as `semimodule R M` but assumes that `R` is a `ring` and `M` is an additive commutative group. * `vector_space k M` : same as `semimodule k M` and `module k M` but assumes that `k` is a `field` and `M` is an additive commutative group. * `linear_map R M M₂`, `M →ₗ[R] M₂` : a linear map between two R-`semimodule`s. ## Implementation notes * `vector_space` and `module` are abbreviations for `semimodule R M`. ## Tags semimodule, module, vector space -/ open function open_locale big_operators universes u u' v w x y z variables {R : Type u} {k : Type u'} {S : Type v} {M : Type w} {M₂ : Type x} {M₃ : Type y} {ι : Type z} /-- A semimodule is a generalization of vector spaces to a scalar semiring. It consists of a scalar semiring `R` and an additive monoid of "vectors" `M`, connected by a "scalar multiplication" operation `r • x : M` (where `r : R` and `x : M`) with some natural associativity and distributivity axioms similar to those on a ring. -/ @[protect_proj] class semimodule (R : Type u) (M : Type v) [semiring R] [add_comm_monoid M] extends distrib_mul_action R M := (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (zero_smul : ∀x : M, (0 : R) • x = 0) section add_comm_monoid variables [semiring R] [add_comm_monoid M] [semimodule R M] (r s : R) (x y : M) theorem add_smul : (r + s) • x = r • x + s • x := semimodule.add_smul r s x variables (R) @[simp] theorem zero_smul : (0 : R) • x = 0 := semimodule.zero_smul x theorem two_smul : (2 : R) • x = x + x := by rw [bit0, add_smul, one_smul] theorem two_smul' : (2 : R) • x = bit0 x := two_smul R x /-- Pullback a `semimodule` structure along an injective additive monoid homomorphism. -/ protected def function.injective.semimodule [add_comm_monoid M₂] [has_scalar R M₂] (f : M₂ →+ M) (hf : injective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : semimodule R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, hf $ by simp only [smul, f.map_add, add_smul], zero_smul := λ x, hf $ by simp only [smul, zero_smul, f.map_zero], .. hf.distrib_mul_action f smul } /-- Pushforward a `semimodule` structure along a surjective additive monoid homomorphism. -/ protected def function.surjective.semimodule [add_comm_monoid M₂] [has_scalar R M₂] (f : M →+ M₂) (hf : surjective f) (smul : ∀ (c : R) x, f (c • x) = c • f x) : semimodule R M₂ := { smul := (•), add_smul := λ c₁ c₂ x, by { rcases hf x with ⟨x, rfl⟩, simp only [add_smul, ← smul, ← f.map_add] }, zero_smul := λ x, by { rcases hf x with ⟨x, rfl⟩, simp only [← f.map_zero, ← smul, zero_smul] }, .. hf.distrib_mul_action f smul } variable (M) /-- `(•)` as an `add_monoid_hom`. -/ def smul_add_hom : R →+ M →+ M := { to_fun := const_smul_hom M, map_zero' := add_monoid_hom.ext $ λ r, by simp, map_add' := λ x y, add_monoid_hom.ext $ λ r, by simp [add_smul] } variables {R M} @[simp] lemma smul_add_hom_apply (r : R) (x : M) : smul_add_hom R M r x = r • x := rfl lemma semimodule.eq_zero_of_zero_eq_one (zero_eq_one : (0 : R) = 1) : x = 0 := by rw [←one_smul R x, ←zero_eq_one, zero_smul] lemma list.sum_smul {l : list R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_list_sum l lemma multiset.sum_smul {l : multiset R} {x : M} : l.sum • x = (l.map (λ r, r • x)).sum := ((smul_add_hom R M).flip x).map_multiset_sum l lemma finset.sum_smul {f : ι → R} {s : finset ι} {x : M} : (∑ i in s, f i) • x = (∑ i in s, (f i) • x) := ((smul_add_hom R M).flip x).map_sum f s end add_comm_monoid variables (R) /-- An `add_comm_monoid` that is a `semimodule` over a `ring` carries a natural `add_comm_group` structure. -/ def semimodule.add_comm_monoid_to_add_comm_group [ring R] [add_comm_monoid M] [semimodule R M] : add_comm_group M := { neg := λ a, (-1 : R) • a, add_left_neg := λ a, show (-1 : R) • a + a = 0, by { nth_rewrite 1 ← one_smul _ a, rw [← add_smul, add_left_neg, zero_smul] }, ..(infer_instance : add_comm_monoid M), } variables {R} section add_comm_group variables (R M) [semiring R] [add_comm_group M] /-- A structure containing most informations as in a semimodule, except the fields `zero_smul` and `smul_zero`. As these fields can be deduced from the other ones when `M` is an `add_comm_group`, this provides a way to construct a semimodule structure by checking less properties, in `semimodule.of_core`. -/ @[nolint has_inhabited_instance] structure semimodule.core extends has_scalar R M := (smul_add : ∀(r : R) (x y : M), r • (x + y) = r • x + r • y) (add_smul : ∀(r s : R) (x : M), (r + s) • x = r • x + s • x) (mul_smul : ∀(r s : R) (x : M), (r * s) • x = r • s • x) (one_smul : ∀x : M, (1 : R) • x = x) variables {R M} /-- Define `semimodule` without proving `zero_smul` and `smul_zero` by using an auxiliary structure `semimodule.core`, when the underlying space is an `add_comm_group`. -/ def semimodule.of_core (H : semimodule.core R M) : semimodule R M := by letI := H.to_has_scalar; exact { zero_smul := λ x, (add_monoid_hom.mk' (λ r : R, r • x) (λ r s, H.add_smul r s x)).map_zero, smul_zero := λ r, (add_monoid_hom.mk' ((•) r) (H.smul_add r)).map_zero, ..H } end add_comm_group /-- Modules are defined as an `abbreviation` for semimodules, if the base semiring is a ring. (A previous definition made `module` a structure defined to be `semimodule`.) This has as advantage that modules are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for modules as well. A cosmetic disadvantage is that one can not extend modules as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "module definition" /-- A module is the same as a semimodule, except the scalar semiring is actually a ring. This is the traditional generalization of spaces like `ℤ^n`, which have a natural addition operation and a way to multiply them by elements of a ring, but no multiplication operation between vectors. -/ abbreviation module (R : Type u) (M : Type v) [ring R] [add_comm_group M] := semimodule R M /-- To prove two semimodule structures on a fixed `add_comm_monoid` agree, it suffices to check the scalar multiplications agree. -/ -- We'll later use this to show `semimodule ℕ M` and `module ℤ M` are subsingletons. @[ext] lemma semimodule_ext {R : Type*} [semiring R] {M : Type*} [add_comm_monoid M] (P Q : semimodule R M) (w : ∀ (r : R) (m : M), by { haveI := P, exact r • m } = by { haveI := Q, exact r • m }) : P = Q := begin unfreezingI { rcases P with ⟨⟨⟨⟨P⟩⟩⟩⟩, rcases Q with ⟨⟨⟨⟨Q⟩⟩⟩⟩ }, congr, funext r m, exact w r m, all_goals { apply proof_irrel_heq }, end section module variables [ring R] [add_comm_group M] [module R M] (r s : R) (x y : M) @[simp] theorem neg_smul : -r • x = - (r • x) := eq_neg_of_add_eq_zero (by rw [← add_smul, add_left_neg, zero_smul]) variables (R) theorem neg_one_smul (x : M) : (-1 : R) • x = -x := by simp variables {R} theorem sub_smul (r s : R) (y : M) : (r - s) • y = r • y - s • y := by simp [add_smul, sub_eq_add_neg] end module /-- A semimodule over a `subsingleton` semiring is a `subsingleton`. We cannot register this as an instance because Lean has no way to guess `R`. -/ theorem semimodule.subsingleton (R M : Type*) [semiring R] [subsingleton R] [add_comm_monoid M] [semimodule R M] : subsingleton M := ⟨λ x y, by rw [← one_smul R x, ← one_smul R y, subsingleton.elim (1:R) 0, zero_smul, zero_smul]⟩ @[priority 910] -- see Note [lower instance priority] instance semiring.to_semimodule [semiring R] : semimodule R R := { smul_add := mul_add, add_smul := add_mul, zero_smul := zero_mul, smul_zero := mul_zero } /-- A ring homomorphism `f : R →+* M` defines a module structure by `r • x = f r * x`. -/ def ring_hom.to_semimodule [semiring R] [semiring S] (f : R →+* S) : semimodule R S := { smul := λ r x, f r * x, smul_add := λ r x y, by unfold has_scalar.smul; rw [mul_add], add_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_add, add_mul], mul_smul := λ r s x, by unfold has_scalar.smul; rw [f.map_mul, mul_assoc], one_smul := λ x, show f 1 * x = _, by rw [f.map_one, one_mul], zero_smul := λ x, show f 0 * x = 0, by rw [f.map_zero, zero_mul], smul_zero := λ r, mul_zero (f r) } /-- Vector spaces are defined as an `abbreviation` for semimodules, if the base ring is a field. (A previous definition made `vector_space` a structure defined to be `module`.) This has as advantage that vector spaces are completely transparent for type class inference, which means that all instances for semimodules are immediately picked up for vector spaces as well. A cosmetic disadvantage is that one can not extend vector spaces as such, in definitions such as `normed_space`. The solution is to extend `semimodule` instead. -/ library_note "vector space definition" /-- A vector space is the same as a module, except the scalar ring is actually a field. (This adds commutativity of the multiplication and existence of inverses.) This is the traditional generalization of spaces like `ℝ^n`, which have a natural addition operation and a way to multiply them by real numbers, but no multiplication operation between vectors. -/ abbreviation vector_space (R : Type u) (M : Type v) [field R] [add_comm_group M] := semimodule R M section add_comm_monoid variables [semiring R] [add_comm_monoid M] [semimodule R M] /-- The natural ℕ-semimodule structure on any `add_comm_monoid`. -/ -- We don't make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℕ`. def add_comm_monoid.nat_semimodule : semimodule ℕ M := { smul := nsmul, smul_add := λ _ _ _, nsmul_add _ _ _, add_smul := λ _ _ _, add_nsmul _ _ _, mul_smul := λ _ _ _, mul_nsmul _ _ _, one_smul := one_nsmul, zero_smul := zero_nsmul, smul_zero := nsmul_zero } section local attribute [instance] add_comm_monoid.nat_semimodule /-- `nsmul` is defined as the `smul` action of `add_comm_monoid.nat_semimodule`. -/ lemma nsmul_def (n : ℕ) (x : M) : n •ℕ x = n • x := rfl end section variables (R) /-- `nsmul` is equal to any other semimodule structure via a cast. -/ lemma nsmul_eq_smul_cast (n : ℕ) (b : M) : n •ℕ b = (n : R) • b := begin rw nsmul_def, induction n with n ih, { rw [nat.cast_zero, zero_smul, zero_smul] }, { rw [nat.succ_eq_add_one, nat.cast_succ, add_smul, add_smul, one_smul, ih, one_smul] } end end /-- `nsmul` is equal to any `ℕ`-semimodule structure. -/ lemma nsmul_eq_smul [semimodule ℕ M] (n : ℕ) (b : M) : n •ℕ b = n • b := by rw [nsmul_eq_smul_cast ℕ, n.cast_id] /-- All `ℕ`-semimodule structures are equal. -/ instance add_comm_monoid.nat_semimodule.subsingleton : subsingleton (semimodule ℕ M) := ⟨λ P Q, by { ext n, rw [←nsmul_eq_smul, ←nsmul_eq_smul], }⟩ /-- Note this does not depend on the `nat_semimodule` definition above, to avoid issues when diamonds occur in finding `semimodule ℕ M` instances. -/ instance add_comm_monoid.nat_is_scalar_tower [semimodule ℕ R] [semimodule ℕ M] : is_scalar_tower ℕ R M := { smul_assoc := λ n x y, nat.rec_on n (by simp only [zero_smul]) (λ n ih, by simp only [nat.succ_eq_add_one, add_smul, one_smul, ih]) } instance add_comm_monoid.nat_smul_comm_class [semimodule ℕ M] : smul_comm_class ℕ R M := { smul_comm := λ n r m, nat.rec_on n (by simp only [zero_smul, smul_zero]) (λ n ih, by simp only [nat.succ_eq_add_one, add_smul, one_smul, ←ih, smul_add]) } -- `smul_comm_class.symm` is not registered as an instance, as it would cause a loop instance add_comm_monoid.nat_smul_comm_class' [semimodule ℕ M] : smul_comm_class R ℕ M := smul_comm_class.symm _ _ _ end add_comm_monoid section add_comm_group variables [semiring S] [ring R] [add_comm_group M] [semimodule S M] [semimodule R M] /-- The natural ℤ-module structure on any `add_comm_group`. -/ -- We don't immediately make this a global instance, as it results in too many instances, -- and confusing ambiguity in the notation `n • x` when `n : ℤ`. -- We do turn it into a global instance, but only at the end of this file, -- and I remain dubious whether this is a good idea. def add_comm_group.int_module : module ℤ M := { smul := gsmul, smul_add := λ _ _ _, gsmul_add _ _ _, add_smul := λ _ _ _, add_gsmul _ _ _, mul_smul := λ _ _ _, gsmul_mul _ _ _, one_smul := one_gsmul, zero_smul := zero_gsmul, smul_zero := gsmul_zero } section local attribute [instance] add_comm_group.int_module /-- `gsmul` is defined as the `smul` action of `add_comm_group.int_module`. -/ lemma gsmul_def (n : ℤ) (x : M) : gsmul n x = n • x := rfl end section variables (R) /-- `gsmul` is equal to any other module structure via a cast. -/ lemma gsmul_eq_smul_cast (n : ℤ) (b : M) : gsmul n b = (n : R) • b := begin rw gsmul_def, induction n using int.induction_on with p hp n hn, { rw [int.cast_zero, zero_smul, zero_smul] }, { rw [int.cast_add, int.cast_one, add_smul, add_smul, one_smul, one_smul, hp] }, { rw [int.cast_sub, int.cast_one, sub_smul, sub_smul, one_smul, one_smul, hn] }, end end /-- `gsmul` is equal to any `ℤ`-module structure. -/ lemma gsmul_eq_smul [semimodule ℤ M] (n : ℤ) (b : M) : n •ℤ b = n • b := by rw [gsmul_eq_smul_cast ℤ, n.cast_id] /-- All `ℤ`-module structures are equal. -/ instance add_comm_group.int_module.subsingleton : subsingleton (semimodule ℤ M) := ⟨λ P Q, by { ext n, rw [←gsmul_eq_smul, ←gsmul_eq_smul], }⟩ instance add_comm_group.int_is_scalar_tower [semimodule ℤ R] [semimodule ℤ M] : is_scalar_tower ℤ R M := { smul_assoc := λ n x y, int.induction_on n (by simp only [zero_smul]) (λ n ih, by simp only [one_smul, add_smul, ih]) (λ n ih, by simp only [one_smul, sub_smul, ih]) } instance add_comm_group.int_smul_comm_class [semimodule ℤ M] : smul_comm_class ℤ S M := { smul_comm := λ n x y, int.induction_on n (by simp only [zero_smul, smul_zero]) (λ n ih, by simp only [one_smul, add_smul, smul_add, ih]) (λ n ih, by simp only [one_smul, sub_smul, smul_sub, ih]) } -- `smul_comm_class.symm` is not registered as an instance, as it would cause a loop instance add_comm_group.int_smul_comm_class' [semimodule ℤ M] : smul_comm_class S ℤ M := smul_comm_class.symm _ _ _ end add_comm_group namespace add_monoid_hom -- We prove this without using the `add_comm_group.int_module` instance, so the `•`s here -- come from whatever the local `module ℤ` structure actually is. lemma map_int_module_smul [add_comm_group M] [add_comm_group M₂] [module ℤ M] [module ℤ M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f (x • a) = x • f a := by simp only [←gsmul_eq_smul, f.map_gsmul] lemma map_int_cast_smul [ring R] [add_comm_group M] [add_comm_group M₂] [module R M] [module R M₂] (f : M →+ M₂) (x : ℤ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [←gsmul_eq_smul_cast, f.map_gsmul] lemma map_nat_cast_smul [semiring R] [add_comm_monoid M] [add_comm_monoid M₂] [semimodule R M] [semimodule R M₂] (f : M →+ M₂) (x : ℕ) (a : M) : f ((x : R) • a) = (x : R) • f a := by simp only [←nsmul_eq_smul_cast, f.map_nsmul] lemma map_rat_cast_smul {R : Type*} [division_ring R] [char_zero R] {E : Type*} [add_comm_group E] [module R E] {F : Type*} [add_comm_group F] [module R F] (f : E →+ F) (c : ℚ) (x : E) : f ((c : R) • x) = (c : R) • f x := begin have : ∀ (x : E) (n : ℕ), 0 < n → f (((n⁻¹ : ℚ) : R) • x) = ((n⁻¹ : ℚ) : R) • f x, { intros x n hn, replace hn : (n : R) ≠ 0 := nat.cast_ne_zero.2 (ne_of_gt hn), conv_rhs { congr, skip, rw [← one_smul R x, ← mul_inv_cancel hn, mul_smul] }, rw [f.map_nat_cast_smul, smul_smul, rat.cast_inv, rat.cast_coe_nat, inv_mul_cancel hn, one_smul] }, refine c.num_denom_cases_on (λ m n hn hmn, _), rw [rat.mk_eq_div, div_eq_mul_inv, rat.cast_mul, int.cast_coe_nat, mul_smul, mul_smul, rat.cast_coe_int, f.map_int_cast_smul, this _ n hn] end lemma map_rat_module_smul {E : Type*} [add_comm_group E] [vector_space ℚ E] {F : Type*} [add_comm_group F] [module ℚ F] (f : E →+ F) (c : ℚ) (x : E) : f (c • x) = c • f x := rat.cast_id c ▸ f.map_rat_cast_smul c x @[simp] lemma nat_smul_apply [add_monoid M] [add_comm_monoid M₂] [semimodule ℕ (M →+ M₂)] [semimodule ℕ M₂] (n : ℕ) (f : M →+ M₂) (a : M) : (n • f) a = n • (f a) := begin induction n with n IH, { simp only [zero_smul, zero_apply] }, { simp only [nat.succ_eq_add_one, add_smul, IH, one_smul, add_apply] } end @[simp] lemma int_smul_apply [add_monoid M] [add_comm_group M₂] [module ℤ (M →+ M₂)] [module ℤ M₂] (n : ℤ) (f : M →+ M₂) (a : M) : (n • f) a = n • (f a) := begin apply int.induction_on' n 0, { simp only [zero_smul, zero_apply] }, all_goals { intros k hk IH, simp only [add_smul, sub_smul, IH, one_smul, add_apply, sub_apply] } end end add_monoid_hom section no_zero_smul_divisors /-! ### `no_zero_smul_divisors` This section defines the `no_zero_smul_divisors` class, and includes some tests for the vanishing of elements (especially in modules over division rings). -/ /-- `no_zero_smul_divisors R M` states that a scalar multiple is `0` only if either argument is `0`. The main application of `no_zero_smul_divisors R M`, when `M` is a semimodule, is the result `smul_eq_zero`: a scalar multiple is `0` iff either argument is `0`. It is a generalization of the `no_zero_divisors` class to heterogeneous multiplication. -/ class no_zero_smul_divisors (R M : Type*) [has_zero R] [has_zero M] [has_scalar R M] : Prop := (eq_zero_or_eq_zero_of_smul_eq_zero : ∀ {c : R} {x : M}, c • x = 0 → c = 0 ∨ x = 0) export no_zero_smul_divisors (eq_zero_or_eq_zero_of_smul_eq_zero) section semimodule variables [semiring R] [add_comm_monoid M] [semimodule R M] instance no_zero_smul_divisors.of_no_zero_divisors [no_zero_divisors R] : no_zero_smul_divisors R R := ⟨λ c x, no_zero_divisors.eq_zero_or_eq_zero_of_mul_eq_zero⟩ @[simp] theorem smul_eq_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x = 0 ↔ c = 0 ∨ x = 0 := ⟨eq_zero_or_eq_zero_of_smul_eq_zero, λ h, h.elim (λ h, h.symm ▸ zero_smul R x) (λ h, h.symm ▸ smul_zero c)⟩ theorem smul_ne_zero [no_zero_smul_divisors R M] {c : R} {x : M} : c • x ≠ 0 ↔ c ≠ 0 ∧ x ≠ 0 := by simp only [ne.def, smul_eq_zero, not_or_distrib] section nat variables (R) (M) [no_zero_smul_divisors R M] [semimodule ℕ M] [char_zero R] include R lemma nat.no_zero_smul_divisors : no_zero_smul_divisors ℕ M := ⟨by { intros c x, rw [← nsmul_eq_smul, nsmul_eq_smul_cast R, smul_eq_zero], simp }⟩ variables {M} lemma eq_zero_of_smul_two_eq_zero {v : M} (hv : 2 • v = 0) : v = 0 := by haveI := nat.no_zero_smul_divisors R M; exact (smul_eq_zero.mp hv).resolve_left (by norm_num) end nat end semimodule section add_comm_group -- `R` can still be a semiring here variables [semiring R] [add_comm_group M] [semimodule R M] lemma smul_injective [no_zero_smul_divisors R M] {c : R} (hc : c ≠ 0) : function.injective (λ (x : M), c • x) := λ x y h, sub_eq_zero.mp ((smul_eq_zero.mp (calc c • (x - y) = c • x - c • y : smul_sub c x y ... = 0 : sub_eq_zero.mpr h)).resolve_left hc) section nat variables (R) [no_zero_smul_divisors R M] [char_zero R] include R lemma eq_zero_of_eq_neg {v : M} (hv : v = - v) : v = 0 := begin -- any semimodule will do haveI : semimodule ℕ M := add_comm_monoid.nat_semimodule, haveI := nat.no_zero_smul_divisors R M, refine eq_zero_of_smul_two_eq_zero R _, rw ←nsmul_eq_smul, convert add_eq_zero_iff_eq_neg.mpr hv, abel end end nat end add_comm_group section module section nat variables {R} [ring R] [add_comm_group M] [module R M] [no_zero_smul_divisors R M] [char_zero R] lemma ne_neg_of_ne_zero [no_zero_divisors R] {v : R} (hv : v ≠ 0) : v ≠ -v := λ h, have semimodule ℕ R := add_comm_monoid.nat_semimodule, by exactI hv (eq_zero_of_eq_neg R h) end nat end module section division_ring variables [division_ring R] [add_comm_group M] [module R M] @[priority 100] -- see note [lower instance priority] instance no_zero_smul_divisors.of_division_ring : no_zero_smul_divisors R M := ⟨λ c x h, or_iff_not_imp_left.2 $ λ hc, (units.mk0 c hc).smul_eq_zero.1 h⟩ end division_ring end no_zero_smul_divisors -- We finally turn on these instances globally. By doing this here, we ensure that none of the -- lemmas about nat semimodules above are specific to these instances. attribute [instance] add_comm_monoid.nat_semimodule add_comm_group.int_module
17714dde12b263676f38d3ac82b7fc460c43d859
a7eef317ddec01b9fc6cfbb876fe7ac00f205ac7
/src/data/equiv/ring.lean
395855db068b7319228e4a0ea10b9fe46b63cc5b
[ "Apache-2.0" ]
permissive
kmill/mathlib
ea5a007b67ae4e9e18dd50d31d8aa60f650425ee
1a419a9fea7b959317eddd556e1bb9639f4dcc05
refs/heads/master
1,668,578,197,719
1,593,629,163,000
1,593,629,163,000
276,482,939
0
0
null
1,593,637,960,000
1,593,637,959,000
null
UTF-8
Lean
false
false
10,179
lean
/- Copyright (c) 2018 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Callum Sutton, Yury Kudryashov -/ import data.equiv.mul_add import algebra.field import algebra.opposites import deprecated.ring /-! # (Semi)ring equivs In this file we define extension of `equiv` called `ring_equiv`, which is a datatype representing an isomorphism of `semiring`s, `ring`s, `division_ring`s, or `field`s. We also introduce the corresponding group of automorphisms `ring_aut`. ## Notations The extended equiv have coercions to functions, and the coercion is the canonical notation when treating the isomorphism as maps. ## Implementation notes The fields for `ring_equiv` now avoid the unbundled `is_mul_hom` and `is_add_hom`, as these are deprecated. Definition of multiplication in the groups of automorphisms agrees with function composition, multiplication in `equiv.perm`, and multiplication in `category_theory.End`, not with `category_theory.comp`. ## Tags equiv, mul_equiv, add_equiv, ring_equiv, mul_aut, add_aut, ring_aut -/ variables {R : Type*} {S : Type*} {S' : Type*} set_option old_structure_cmd true /- (semi)ring equivalence. -/ structure ring_equiv (R S : Type*) [has_mul R] [has_add R] [has_mul S] [has_add S] extends R ≃ S, R ≃* S, R ≃+ S infix ` ≃+* `:25 := ring_equiv namespace ring_equiv section basic variables [has_mul R] [has_add R] [has_mul S] [has_add S] [has_mul S'] [has_add S'] instance : has_coe_to_fun (R ≃+* S) := ⟨_, ring_equiv.to_fun⟩ @[simp] lemma to_fun_eq_coe_fun (f : R ≃+* S) : f.to_fun = f := rfl instance has_coe_to_mul_equiv : has_coe (R ≃+* S) (R ≃* S) := ⟨ring_equiv.to_mul_equiv⟩ instance has_coe_to_add_equiv : has_coe (R ≃+* S) (R ≃+ S) := ⟨ring_equiv.to_add_equiv⟩ @[norm_cast] lemma coe_mul_equiv (f : R ≃+* S) (a : R) : (f : R ≃* S) a = f a := rfl @[norm_cast] lemma coe_add_equiv (f : R ≃+* S) (a : R) : (f : R ≃+ S) a = f a := rfl variable (R) /-- The identity map is a ring isomorphism. -/ @[refl] protected def refl : R ≃+* R := { .. mul_equiv.refl R, .. add_equiv.refl R } @[simp] lemma refl_apply (x : R) : ring_equiv.refl R x = x := rfl @[simp] lemma coe_add_equiv_refl : (ring_equiv.refl R : R ≃+ R) = add_equiv.refl R := rfl @[simp] lemma coe_mul_equiv_refl : (ring_equiv.refl R : R ≃* R) = mul_equiv.refl R := rfl variables {R} /-- The inverse of a ring isomorphism is a ring isomorphism. -/ @[symm] protected def symm (e : R ≃+* S) : S ≃+* R := { .. e.to_mul_equiv.symm, .. e.to_add_equiv.symm } /-- Transitivity of `ring_equiv`. -/ @[trans] protected def trans (e₁ : R ≃+* S) (e₂ : S ≃+* S') : R ≃+* S' := { .. (e₁.to_mul_equiv.trans e₂.to_mul_equiv), .. (e₁.to_add_equiv.trans e₂.to_add_equiv) } protected lemma bijective (e : R ≃+* S) : function.bijective e := e.to_equiv.bijective protected lemma injective (e : R ≃+* S) : function.injective e := e.to_equiv.injective protected lemma surjective (e : R ≃+* S) : function.surjective e := e.to_equiv.surjective @[simp] lemma apply_symm_apply (e : R ≃+* S) : ∀ x, e (e.symm x) = x := e.to_equiv.apply_symm_apply @[simp] lemma symm_apply_apply (e : R ≃+* S) : ∀ x, e.symm (e x) = x := e.to_equiv.symm_apply_apply lemma image_eq_preimage (e : R ≃+* S) (s : set R) : e '' s = e.symm ⁻¹' s := e.to_equiv.image_eq_preimage s end basic section comm_semiring open opposite variables (R) [comm_semiring R] /-- A commutative ring is isomorphic to its opposite. -/ def to_opposite : R ≃+* Rᵒᵖ := { map_add' := λ x y, rfl, map_mul' := λ x y, mul_comm (op y) (op x), ..equiv_to_opposite } @[simp] lemma to_opposite_apply (r : R) : to_opposite R r = op r := rfl @[simp] lemma to_opposite_symm_apply (r : Rᵒᵖ) : (to_opposite R).symm r = unop r := rfl end comm_semiring section variables [semiring R] [semiring S] (f : R ≃+* S) (x y : R) /-- A ring isomorphism preserves multiplication. -/ @[simp] lemma map_mul : f (x * y) = f x * f y := f.map_mul' x y /-- A ring isomorphism sends one to one. -/ @[simp] lemma map_one : f 1 = 1 := (f : R ≃* S).map_one /-- A ring isomorphism preserves addition. -/ @[simp] lemma map_add : f (x + y) = f x + f y := f.map_add' x y /-- A ring isomorphism sends zero to zero. -/ @[simp] lemma map_zero : f 0 = 0 := (f : R ≃+ S).map_zero variable {x} @[simp] lemma map_eq_one_iff : f x = 1 ↔ x = 1 := (f : R ≃* S).map_eq_one_iff @[simp] lemma map_eq_zero_iff : f x = 0 ↔ x = 0 := (f : R ≃+ S).map_eq_zero_iff lemma map_ne_one_iff : f x ≠ 1 ↔ x ≠ 1 := (f : R ≃* S).map_ne_one_iff lemma map_ne_zero_iff : f x ≠ 0 ↔ x ≠ 0 := (f : R ≃+ S).map_ne_zero_iff end section variables [ring R] [ring S] (f : R ≃+* S) (x y : R) @[simp] lemma map_neg : f (-x) = -f x := (f : R ≃+ S).map_neg x @[simp] lemma map_sub : f (x - y) = f x - f y := (f : R ≃+ S).map_sub x y @[simp] lemma map_neg_one : f (-1) = -1 := f.map_one ▸ f.map_neg 1 end section semiring_hom variables [semiring R] [semiring S] /-- Reinterpret a ring equivalence as a ring homomorphism. -/ def to_ring_hom (e : R ≃+* S) : R →+* S := { .. e.to_mul_equiv.to_monoid_hom, .. e.to_add_equiv.to_add_monoid_hom } /-- Reinterpret a ring equivalence as a monoid homomorphism. -/ abbreviation to_monoid_hom (e : R ≃+* S) : R →* S := e.to_ring_hom.to_monoid_hom /-- Reinterpret a ring equivalence as an `add_monoid` homomorphism. -/ abbreviation to_add_monoid_hom (e : R ≃+* S) : R →+ S := e.to_ring_hom.to_add_monoid_hom /-- Interpret an equivalence `f : R ≃ S` as a ring equivalence `R ≃+* S`. -/ def of (e : R ≃ S) [is_semiring_hom e] : R ≃+* S := { .. e, .. monoid_hom.of e, .. add_monoid_hom.of e } instance (e : R ≃+* S) : is_semiring_hom e := e.to_ring_hom.is_semiring_hom @[simp] lemma to_ring_hom_refl : (ring_equiv.refl R).to_ring_hom = ring_hom.id R := rfl @[simp] lemma to_monoid_hom_refl : (ring_equiv.refl R).to_monoid_hom = monoid_hom.id R := rfl @[simp] lemma to_add_monoid_hom_refl : (ring_equiv.refl R).to_add_monoid_hom = add_monoid_hom.id R := rfl @[simp] lemma to_ring_hom_apply_symm_to_ring_hom_apply {R S} [semiring R] [semiring S] (e : R ≃+* S) : ∀ (y : S), e.to_ring_hom (e.symm.to_ring_hom y) = y := e.to_equiv.apply_symm_apply @[simp] lemma symm_to_ring_hom_apply_to_ring_hom_apply {R S} [semiring R] [semiring S] (e : R ≃+* S) : ∀ (x : R), e.symm.to_ring_hom (e.to_ring_hom x) = x := equiv.symm_apply_apply (e.to_equiv) end semiring_hom end ring_equiv namespace mul_equiv /-- Gives a `ring_equiv` from a `mul_equiv` preserving addition.-/ def to_ring_equiv {R : Type*} {S : Type*} [has_add R] [has_add S] [has_mul R] [has_mul S] (h : R ≃* S) (H : ∀ x y : R, h (x + y) = h x + h y) : R ≃+* S := {..h.to_equiv, ..h, ..add_equiv.mk' h.to_equiv H } end mul_equiv namespace ring_equiv section ring_hom variables [ring R] [ring S] /-- Interpret an equivalence `f : R ≃ S` as a ring equivalence `R ≃+* S`. -/ def of' (e : R ≃ S) [is_ring_hom e] : R ≃+* S := { .. e, .. monoid_hom.of e, .. add_monoid_hom.of e } instance (e : R ≃+* S) : is_ring_hom e := e.to_ring_hom.is_ring_hom end ring_hom /-- Two ring isomorphisms agree if they are defined by the same underlying function. -/ @[ext] lemma ext {R S : Type*} [has_mul R] [has_add R] [has_mul S] [has_add S] {f g : R ≃+* S} (h : ∀ x, f x = g x) : f = g := begin have h₁ : f.to_equiv = g.to_equiv := equiv.ext h, cases f, cases g, congr, { exact (funext h) }, { exact congr_arg equiv.inv_fun h₁ } end /-- If two rings are isomorphic, and the second is an integral domain, then so is the first. -/ protected lemma is_integral_domain {A : Type*} (B : Type*) [ring A] [ring B] (hB : is_integral_domain B) (e : A ≃+* B) : is_integral_domain A := { mul_comm := λ x y, have e.symm (e x * e y) = e.symm (e y * e x), by rw hB.mul_comm, by simpa, eq_zero_or_eq_zero_of_mul_eq_zero := λ x y hxy, have e x * e y = 0, by rw [← e.map_mul, hxy, e.map_zero], (hB.eq_zero_or_eq_zero_of_mul_eq_zero _ _ this).imp (λ hx, by simpa using congr_arg e.symm hx) (λ hy, by simpa using congr_arg e.symm hy), zero_ne_one := λ H, hB.zero_ne_one $ by rw [← e.map_zero, ← e.map_one, H] } /-- If two rings are isomorphic, and the second is an integral domain, then so is the first. -/ protected def integral_domain {A : Type*} (B : Type*) [ring A] [integral_domain B] (e : A ≃+* B) : integral_domain A := { .. (‹_› : ring A), .. e.is_integral_domain B (integral_domain.to_is_integral_domain B) } end ring_equiv /-- The group of ring automorphisms. -/ @[reducible] def ring_aut (R : Type*) [has_mul R] [has_add R] := ring_equiv R R namespace ring_aut variables (R) [has_mul R] [has_add R] /-- The group operation on automorphisms of a ring is defined by λ g h, ring_equiv.trans h g. This means that multiplication agrees with composition, (g*h)(x) = g (h x) . -/ instance : group (ring_aut R) := by refine_struct { mul := λ g h, ring_equiv.trans h g, one := ring_equiv.refl R, inv := ring_equiv.symm }; intros; ext; try { refl }; apply equiv.left_inv instance : inhabited (ring_aut R) := ⟨1⟩ /-- Monoid homomorphism from ring automorphisms to additive automorphisms. -/ def to_add_aut : ring_aut R →* add_aut R := by refine_struct { to_fun := ring_equiv.to_add_equiv }; intros; refl /-- Monoid homomorphism from ring automorphisms to multiplicative automorphisms. -/ def to_mul_aut : ring_aut R →* mul_aut R := by refine_struct { to_fun := ring_equiv.to_mul_equiv }; intros; refl /-- Monoid homomorphism from ring automorphisms to permutations. -/ def to_perm : ring_aut R →* equiv.perm R := by refine_struct { to_fun := ring_equiv.to_equiv }; intros; refl end ring_aut namespace equiv variables (K : Type*) [division_ring K] def units_equiv_ne_zero : units K ≃ {a : K | a ≠ 0} := ⟨λ a, ⟨a.1, a.coe_ne_zero⟩, λ a, units.mk0 _ a.2, λ ⟨_, _, _, _⟩, units.ext rfl, λ ⟨_, _⟩, rfl⟩ variable {K} @[simp] lemma coe_units_equiv_ne_zero (a : units K) : ((units_equiv_ne_zero K a) : K) = a := rfl end equiv
53c85c945cbe3a32a891540966ff403252ccdb53
437dc96105f48409c3981d46fb48e57c9ac3a3e4
/src/data/pnat/intervals.lean
ec18274c34fdc28c51a823fb2b73e5cbc3dbd9df
[ "Apache-2.0" ]
permissive
dan-c-k/mathlib
08efec79bd7481ee6da9cc44c24a653bff4fbe0d
96efc220f6225bc7a5ed8349900391a33a38cc56
refs/heads/master
1,658,082,847,093
1,589,013,201,000
1,589,013,201,000
null
0
0
null
null
null
null
UTF-8
Lean
false
false
803
lean
/- Copyright (c) 2020 Scott Morrison. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Author: Scott Morrison -/ import data.pnat.basic import data.finset namespace pnat /-- `Ico l u` is the set of positive natural numbers `l ≤ k < u`. -/ def Ico (l u : ℕ+) : finset ℕ+ := (finset.Ico l u).attach.map { to_fun := λ n, ⟨(n : ℕ), lt_of_lt_of_le l.2 (finset.Ico.mem.1 n.2).1⟩, -- why can't we do this directly? inj := λ n m h, subtype.eq (by { replace h := congr_arg subtype.val h, exact h }) } @[simp] lemma Ico.mem : ∀ {n m l : ℕ+}, l ∈ Ico n m ↔ n ≤ l ∧ l < m := by { rintro ⟨n, hn⟩ ⟨m, hm⟩ ⟨l, hl⟩, simp [pnat.Ico] } @[simp] lemma Ico.card (l u : ℕ+) : (Ico l u).card = u - l := by simp [pnat.Ico] end pnat
000385f272788e61f47c77a6b34ce0c97e046dee
f6fc81bc8b5b766295e383d57865e4a9d2ea12c9
/sphinx/hanoi_project/Exercise-NgoNga.lean
b578de8a13f369c253c226d21ce84710d6b60ab5
[ "CC-BY-4.0" ]
permissive
locnh2012/formalabstracts
ef43b9bbf31bc8c448bc6628a9b936253f351964
67bec3c68a962718528f5b968b739bd889be69b7
refs/heads/master
1,584,616,125,965
1,529,294,544,000
1,529,294,700,000
136,434,328
0
0
null
1,528,354,107,000
1,528,354,106,000
null
UTF-8
Lean
false
false
22
lean
#check nat #eval 1+1
e86986a1a4ba7ad2222703207787c7817cac8099
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/src/category_theory/category/Bipointed.lean
12a348f069d99b18b50c17b524a86f814cf210cf
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
7,009
lean
/- Copyright (c) 2022 Yaël Dillies. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Yaël Dillies -/ import category_theory.category.Pointed /-! # The category of bipointed types > THIS FILE IS SYNCHRONIZED WITH MATHLIB4. > Any changes to this file require a corresponding PR to mathlib4. This defines `Bipointed`, the category of bipointed types. ## TODO Monoidal structure -/ open category_theory universes u variables {α β : Type*} /-- The category of bipointed types. -/ structure Bipointed : Type.{u + 1} := (X : Type.{u}) (to_prod : X × X) namespace Bipointed instance : has_coe_to_sort Bipointed Type* := ⟨X⟩ attribute [protected] Bipointed.X /-- Turns a bipointing into a bipointed type. -/ def of {X : Type*} (to_prod : X × X) : Bipointed := ⟨X, to_prod⟩ @[simp] lemma coe_of {X : Type*} (to_prod : X × X) : ↥(of to_prod) = X := rfl alias of ← _root_.prod.Bipointed instance : inhabited Bipointed := ⟨of ((), ())⟩ /-- Morphisms in `Bipointed`. -/ @[ext] protected structure hom (X Y : Bipointed.{u}) : Type u := (to_fun : X → Y) (map_fst : to_fun X.to_prod.1 = Y.to_prod.1) (map_snd : to_fun X.to_prod.2 = Y.to_prod.2) namespace hom /-- The identity morphism of `X : Bipointed`. -/ @[simps] def id (X : Bipointed) : hom X X := ⟨id, rfl, rfl⟩ instance (X : Bipointed) : inhabited (hom X X) := ⟨id X⟩ /-- Composition of morphisms of `Bipointed`. -/ @[simps] def comp {X Y Z : Bipointed.{u}} (f : hom X Y) (g : hom Y Z) : hom X Z := ⟨g.to_fun ∘ f.to_fun, by rw [function.comp_apply, f.map_fst, g.map_fst], by rw [function.comp_apply, f.map_snd, g.map_snd]⟩ end hom instance large_category : large_category Bipointed := { hom := hom, id := hom.id, comp := @hom.comp, id_comp' := λ _ _ _, hom.ext _ _ rfl, comp_id' := λ _ _ _, hom.ext _ _ rfl, assoc' := λ _ _ _ _ _ _ _, hom.ext _ _ rfl } instance concrete_category : concrete_category Bipointed := { forget := { obj := Bipointed.X, map := @hom.to_fun }, forget_faithful := ⟨@hom.ext⟩ } /-- Swaps the pointed elements of a bipointed type. `prod.swap` as a functor. -/ @[simps] def swap : Bipointed ⥤ Bipointed := { obj := λ X, ⟨X, X.to_prod.swap⟩, map := λ X Y f, ⟨f.to_fun, f.map_snd, f.map_fst⟩ } /-- The equivalence between `Bipointed` and itself induced by `prod.swap` both ways. -/ @[simps] def swap_equiv : Bipointed ≌ Bipointed := equivalence.mk swap swap (nat_iso.of_components (λ X, { hom := ⟨id, rfl, rfl⟩, inv := ⟨id, rfl, rfl⟩ }) $ λ X Y f, rfl) (nat_iso.of_components (λ X, { hom := ⟨id, rfl, rfl⟩, inv := ⟨id, rfl, rfl⟩ }) $ λ X Y f, rfl) @[simp] lemma swap_equiv_symm : swap_equiv.symm = swap_equiv := rfl end Bipointed /-- The forgetful functor from `Bipointed` to `Pointed` which forgets about the second point. -/ def Bipointed_to_Pointed_fst : Bipointed ⥤ Pointed := { obj := λ X, ⟨X, X.to_prod.1⟩, map := λ X Y f, ⟨f.to_fun, f.map_fst⟩ } /-- The forgetful functor from `Bipointed` to `Pointed` which forgets about the first point. -/ def Bipointed_to_Pointed_snd : Bipointed ⥤ Pointed := { obj := λ X, ⟨X, X.to_prod.2⟩, map := λ X Y f, ⟨f.to_fun, f.map_snd⟩ } @[simp] lemma Bipointed_to_Pointed_fst_comp_forget : Bipointed_to_Pointed_fst ⋙ forget Pointed = forget Bipointed := rfl @[simp] lemma Bipointed_to_Pointed_snd_comp_forget : Bipointed_to_Pointed_snd ⋙ forget Pointed = forget Bipointed := rfl @[simp] lemma swap_comp_Bipointed_to_Pointed_fst : Bipointed.swap ⋙ Bipointed_to_Pointed_fst = Bipointed_to_Pointed_snd := rfl @[simp] lemma swap_comp_Bipointed_to_Pointed_snd : Bipointed.swap ⋙ Bipointed_to_Pointed_snd = Bipointed_to_Pointed_fst := rfl /-- The functor from `Pointed` to `Bipointed` which bipoints the point. -/ def Pointed_to_Bipointed : Pointed.{u} ⥤ Bipointed := { obj := λ X, ⟨X, X.point, X.point⟩, map := λ X Y f, ⟨f.to_fun, f.map_point, f.map_point⟩ } /-- The functor from `Pointed` to `Bipointed` which adds a second point. -/ def Pointed_to_Bipointed_fst : Pointed.{u} ⥤ Bipointed := { obj := λ X, ⟨option X, X.point, none⟩, map := λ X Y f, ⟨option.map f.to_fun, congr_arg _ f.map_point, rfl⟩, map_id' := λ X, Bipointed.hom.ext _ _ option.map_id, map_comp' := λ X Y Z f g, Bipointed.hom.ext _ _ (option.map_comp_map _ _).symm } /-- The functor from `Pointed` to `Bipointed` which adds a first point. -/ def Pointed_to_Bipointed_snd : Pointed.{u} ⥤ Bipointed := { obj := λ X, ⟨option X, none, X.point⟩, map := λ X Y f, ⟨option.map f.to_fun, rfl, congr_arg _ f.map_point⟩, map_id' := λ X, Bipointed.hom.ext _ _ option.map_id, map_comp' := λ X Y Z f g, Bipointed.hom.ext _ _ (option.map_comp_map _ _).symm } @[simp] lemma Pointed_to_Bipointed_fst_comp_swap : Pointed_to_Bipointed_fst ⋙ Bipointed.swap = Pointed_to_Bipointed_snd := rfl @[simp] lemma Pointed_to_Bipointed_snd_comp_swap : Pointed_to_Bipointed_snd ⋙ Bipointed.swap = Pointed_to_Bipointed_fst := rfl /-- `Bipointed_to_Pointed_fst` is inverse to `Pointed_to_Bipointed`. -/ @[simps] def Pointed_to_Bipointed_comp_Bipointed_to_Pointed_fst : Pointed_to_Bipointed ⋙ Bipointed_to_Pointed_fst ≅ 𝟭 _ := nat_iso.of_components (λ X, { hom := ⟨id, rfl⟩, inv := ⟨id, rfl⟩ }) $ λ X Y f, rfl /-- `Bipointed_to_Pointed_snd` is inverse to `Pointed_to_Bipointed`. -/ @[simps] def Pointed_to_Bipointed_comp_Bipointed_to_Pointed_snd : Pointed_to_Bipointed ⋙ Bipointed_to_Pointed_snd ≅ 𝟭 _ := nat_iso.of_components (λ X, { hom := ⟨id, rfl⟩, inv := ⟨id, rfl⟩ }) $ λ X Y f, rfl /-- The free/forgetful adjunction between `Pointed_to_Bipointed_fst` and `Bipointed_to_Pointed_fst`. -/ def Pointed_to_Bipointed_fst_Bipointed_to_Pointed_fst_adjunction : Pointed_to_Bipointed_fst ⊣ Bipointed_to_Pointed_fst := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, ⟨f.to_fun ∘ option.some, f.map_fst⟩, inv_fun := λ f, ⟨λ o, o.elim Y.to_prod.2 f.to_fun, f.map_point, rfl⟩, left_inv := λ f, by { ext, cases x, exact f.map_snd.symm, refl }, right_inv := λ f, Pointed.hom.ext _ _ rfl }, hom_equiv_naturality_left_symm' := λ X' X Y f g, by { ext, cases x; refl } } /-- The free/forgetful adjunction between `Pointed_to_Bipointed_snd` and `Bipointed_to_Pointed_snd`. -/ def Pointed_to_Bipointed_snd_Bipointed_to_Pointed_snd_adjunction : Pointed_to_Bipointed_snd ⊣ Bipointed_to_Pointed_snd := adjunction.mk_of_hom_equiv { hom_equiv := λ X Y, { to_fun := λ f, ⟨f.to_fun ∘ option.some, f.map_snd⟩, inv_fun := λ f, ⟨λ o, o.elim Y.to_prod.1 f.to_fun, rfl, f.map_point⟩, left_inv := λ f, by { ext, cases x, exact f.map_fst.symm, refl }, right_inv := λ f, Pointed.hom.ext _ _ rfl }, hom_equiv_naturality_left_symm' := λ X' X Y f g, by { ext, cases x; refl } }
614ef5f34aebc98ae36ec21de5cd1f4a0b7ac88c
74addaa0e41490cbaf2abd313a764c96df57b05d
/Mathlib/measure_theory/haar_measure.lean
fd24712dfde58478187ebf33c7f7fd79bbe6fd64
[]
no_license
AurelienSaue/Mathlib4_auto
f538cfd0980f65a6361eadea39e6fc639e9dae14
590df64109b08190abe22358fabc3eae000943f2
refs/heads/master
1,683,906,849,776
1,622,564,669,000
1,622,564,669,000
371,723,747
0
0
null
null
null
null
UTF-8
Lean
false
false
25,280
lean
/- Copyright (c) 2020 Floris van Doorn. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Floris van Doorn -/ import Mathlib.PrePort import Mathlib.Lean3Lib.init.default import Mathlib.measure_theory.content import Mathlib.measure_theory.prod_group import Mathlib.PostPort universes u_1 namespace Mathlib /-! # Haar measure In this file we prove the existence of Haar measure for a locally compact Hausdorff topological group. For the construction, we follow the write-up by Jonathan Gleason, *Existence and Uniqueness of Haar Measure*. This is essentially the same argument as in https://en.wikipedia.org/wiki/Haar_measure#A_construction_using_compact_subsets. We construct the Haar measure first on compact sets. For this we define `(K : U)` as the (smallest) number of left-translates of `U` are needed to cover `K` (`index` in the formalization). Then we define a function `h` on compact sets as `lim_U (K : U) / (K₀ : U)`, where `U` becomes a smaller and smaller open neighborhood of `1`, and `K₀` is a fixed compact set with nonempty interior. This function is `chaar` in the formalization, and we define the limit formally using Tychonoff's theorem. This function `h` forms a content, which we can extend to an outer measure `μ` (`haar_outer_measure`), and obtain the Haar measure from that (`haar_measure`). We normalize the Haar measure so that the measure of `K₀` is `1`. We show that for second countable spaces any left invariant Borel measure is a scalar multiple of the Haar measure. Note that `μ` need not coincide with `h` on compact sets, according to [halmos1950measure, ch. X, §53 p.233]. However, we know that `h(K)` lies between `μ(Kᵒ)` and `μ(K)`, where `ᵒ` denotes the interior. ## Main Declarations * `haar_measure`: the Haar measure on a locally compact Hausdorff group. This is a left invariant regular measure. It takes as argument a compact set of the group (with non-empty interior), and is normalized so that the measure of the given set is 1. * `haar_measure_self`: the Haar measure is normalized. * `is_left_invariant_haar_measure`: the Haar measure is left invariant. * `regular_haar_measure`: the Haar measure is a regular measure. ## References * Paul Halmos (1950), Measure Theory, §53 * Jonathan Gleason, Existence and Uniqueness of Haar Measure - Note: step 9, page 8 contains a mistake: the last defined `μ` does not extend the `μ` on compact sets, see Halmos (1950) p. 233, bottom of the page. This makes some other steps (like step 11) invalid. * https://en.wikipedia.org/wiki/Haar_measure -/ namespace measure_theory namespace measure /-! We put the internal functions in the construction of the Haar measure in a namespace, so that the chosen names don't clash with other declarations. We first define a couple of the functions before proving the properties (that require that `G` is a topological group). -/ namespace haar /-- The index or Haar covering number or ratio of `K` w.r.t. `V`, denoted `(K : V)`: it is the smallest number of (left) translates of `V` that is necessary to cover `K`. It is defined to be 0 if no finite number of translates cover `K`. -/ def index {G : Type u_1} [group G] (K : set G) (V : set G) : ℕ := Inf (finset.card '' set_of fun (t : finset G) => K ⊆ set.Union fun (g : G) => set.Union fun (H : g ∈ t) => (fun (h : G) => g * h) ⁻¹' V) theorem index_empty {G : Type u_1} [group G] {V : set G} : index ∅ V = 0 := sorry /-- `prehaar K₀ U K` is a weighted version of the index, defined as `(K : U)/(K₀ : U)`. In the applications `K₀` is compact with non-empty interior, `U` is open containing `1`, and `K` is any compact set. The argument `K` is a (bundled) compact set, so that we can consider `prehaar K₀ U` as an element of `haar_product` (below). -/ def prehaar {G : Type u_1} [group G] [topological_space G] (K₀ : set G) (U : set G) (K : topological_space.compacts G) : ℝ := ↑(index (subtype.val K) U) / ↑(index K₀ U) theorem prehaar_empty {G : Type u_1} [group G] [topological_space G] (K₀ : topological_space.positive_compacts G) {U : set G} : prehaar (subtype.val K₀) U ⊥ = 0 := sorry theorem prehaar_nonneg {G : Type u_1} [group G] [topological_space G] (K₀ : topological_space.positive_compacts G) {U : set G} (K : topological_space.compacts G) : 0 ≤ prehaar (subtype.val K₀) U K := sorry /-- `haar_product K₀` is the product of intervals `[0, (K : K₀)]`, for all compact sets `K`. For all `U`, we can show that `prehaar K₀ U ∈ haar_product K₀`. -/ def haar_product {G : Type u_1} [group G] [topological_space G] (K₀ : set G) : set (topological_space.compacts G → ℝ) := set.pi set.univ fun (K : topological_space.compacts G) => set.Icc 0 ↑(index (subtype.val K) K₀) @[simp] theorem mem_prehaar_empty {G : Type u_1} [group G] [topological_space G] {K₀ : set G} {f : topological_space.compacts G → ℝ} : f ∈ haar_product K₀ ↔ ∀ (K : topological_space.compacts G), f K ∈ set.Icc 0 ↑(index (subtype.val K) K₀) := sorry /-- The closure of the collection of elements of the form `prehaar K₀ U`, for `U` open neighbourhoods of `1`, contained in `V`. The closure is taken in the space `compacts G → ℝ`, with the topology of pointwise convergence. We show that the intersection of all these sets is nonempty, and the Haar measure on compact sets is defined to be an element in the closure of this intersection. -/ def cl_prehaar {G : Type u_1} [group G] [topological_space G] (K₀ : set G) (V : topological_space.open_nhds_of 1) : set (topological_space.compacts G → ℝ) := closure (prehaar K₀ '' set_of fun (U : set G) => U ⊆ subtype.val V ∧ is_open U ∧ 1 ∈ U) /-! ### Lemmas about `index` -/ /-- If `K` is compact and `V` has nonempty interior, then the index `(K : V)` is well-defined, there is a finite set `t` satisfying the desired properties. -/ theorem index_defined {G : Type u_1} [group G] [topological_space G] [topological_group G] {K : set G} {V : set G} (hK : is_compact K) (hV : set.nonempty (interior V)) : ∃ (n : ℕ), n ∈ finset.card '' set_of fun (t : finset G) => K ⊆ set.Union fun (g : G) => set.Union fun (H : g ∈ t) => (fun (h : G) => g * h) ⁻¹' V := Exists.dcases_on (compact_covered_by_mul_left_translates hK hV) fun (t : finset G) (ht : K ⊆ set.Union fun (g : G) => set.Union fun (H : g ∈ t) => (fun (h : G) => g * h) ⁻¹' V) => Exists.intro (finset.card t) (Exists.intro t { left := ht, right := rfl }) theorem index_elim {G : Type u_1} [group G] [topological_space G] [topological_group G] {K : set G} {V : set G} (hK : is_compact K) (hV : set.nonempty (interior V)) : ∃ (t : finset G), (K ⊆ set.Union fun (g : G) => set.Union fun (H : g ∈ t) => (fun (h : G) => g * h) ⁻¹' V) ∧ finset.card t = index K V := sorry theorem le_index_mul {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (K : topological_space.compacts G) {V : set G} (hV : set.nonempty (interior V)) : index (subtype.val K) V ≤ index (subtype.val K) (subtype.val K₀) * index (subtype.val K₀) V := sorry theorem index_pos {G : Type u_1} [group G] [topological_space G] [topological_group G] (K : topological_space.positive_compacts G) {V : set G} (hV : set.nonempty (interior V)) : 0 < index (subtype.val K) V := sorry theorem index_mono {G : Type u_1} [group G] [topological_space G] [topological_group G] {K : set G} {K' : set G} {V : set G} (hK' : is_compact K') (h : K ⊆ K') (hV : set.nonempty (interior V)) : index K V ≤ index K' V := sorry theorem index_union_le {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₁ : topological_space.compacts G) (K₂ : topological_space.compacts G) {V : set G} (hV : set.nonempty (interior V)) : index (subtype.val K₁ ∪ subtype.val K₂) V ≤ index (subtype.val K₁) V + index (subtype.val K₂) V := sorry theorem index_union_eq {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₁ : topological_space.compacts G) (K₂ : topological_space.compacts G) {V : set G} (hV : set.nonempty (interior V)) (h : disjoint (subtype.val K₁ * (V⁻¹)) (subtype.val K₂ * (V⁻¹))) : index (subtype.val K₁ ∪ subtype.val K₂) V = index (subtype.val K₁) V + index (subtype.val K₂) V := sorry theorem mul_left_index_le {G : Type u_1} [group G] [topological_space G] [topological_group G] {K : set G} (hK : is_compact K) {V : set G} (hV : set.nonempty (interior V)) (g : G) : index ((fun (h : G) => g * h) '' K) V ≤ index K V := sorry theorem is_left_invariant_index {G : Type u_1} [group G] [topological_space G] [topological_group G] {K : set G} (hK : is_compact K) (g : G) {V : set G} (hV : set.nonempty (interior V)) : index ((fun (h : G) => g * h) '' K) V = index K V := sorry /-! ### Lemmas about `prehaar` -/ theorem prehaar_le_index {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) {U : set G} (K : topological_space.compacts G) (hU : set.nonempty (interior U)) : prehaar (subtype.val K₀) U K ≤ ↑(index (subtype.val K) (subtype.val K₀)) := sorry theorem prehaar_pos {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) {U : set G} (hU : set.nonempty (interior U)) {K : set G} (h1K : is_compact K) (h2K : set.nonempty (interior K)) : 0 < prehaar (subtype.val K₀) U { val := K, property := h1K } := sorry theorem prehaar_mono {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : set.nonempty (interior U)) {K₁ : topological_space.compacts G} {K₂ : topological_space.compacts G} (h : subtype.val K₁ ⊆ subtype.val K₂) : prehaar (subtype.val K₀) U K₁ ≤ prehaar (subtype.val K₀) U K₂ := sorry theorem prehaar_self {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : set.nonempty (interior U)) : prehaar (subtype.val K₀) U { val := subtype.val K₀, property := and.left (subtype.property K₀) } = 1 := sorry theorem prehaar_sup_le {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (K₁ : topological_space.compacts G) (K₂ : topological_space.compacts G) (hU : set.nonempty (interior U)) : prehaar (subtype.val K₀) U (K₁ ⊔ K₂) ≤ prehaar (subtype.val K₀) U K₁ + prehaar (subtype.val K₀) U K₂ := sorry theorem prehaar_sup_eq {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} {K₁ : topological_space.compacts G} {K₂ : topological_space.compacts G} (hU : set.nonempty (interior U)) (h : disjoint (subtype.val K₁ * (U⁻¹)) (subtype.val K₂ * (U⁻¹))) : prehaar (subtype.val K₀) U (K₁ ⊔ K₂) = prehaar (subtype.val K₀) U K₁ + prehaar (subtype.val K₀) U K₂ := sorry theorem is_left_invariant_prehaar {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : set.nonempty (interior U)) (g : G) (K : topological_space.compacts G) : prehaar (subtype.val K₀) U (topological_space.compacts.map (fun (b : G) => g * b) (continuous_mul_left g) K) = prehaar (subtype.val K₀) U K := sorry /-! ### Lemmas about `haar_product` -/ theorem prehaar_mem_haar_product {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) {U : set G} (hU : set.nonempty (interior U)) : prehaar (subtype.val K₀) U ∈ haar_product (subtype.val K₀) := sorry theorem nonempty_Inter_cl_prehaar {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) : set.nonempty (haar_product (subtype.val K₀) ∩ set.Inter fun (V : topological_space.open_nhds_of 1) => cl_prehaar (subtype.val K₀) V) := sorry /-! ### The Haar measure on compact sets -/ /-- The Haar measure on compact sets, defined to be an arbitrary element in the intersection of all the sets `cl_prehaar K₀ V` in `haar_product K₀`. -/ def chaar {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (K : topological_space.compacts G) : ℝ := classical.some (nonempty_Inter_cl_prehaar K₀) K theorem chaar_mem_haar_product {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) : chaar K₀ ∈ haar_product (subtype.val K₀) := and.left (classical.some_spec (nonempty_Inter_cl_prehaar K₀)) theorem chaar_mem_cl_prehaar {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (V : topological_space.open_nhds_of 1) : chaar K₀ ∈ cl_prehaar (subtype.val K₀) V := sorry theorem chaar_nonneg {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (K : topological_space.compacts G) : 0 ≤ chaar K₀ K := sorry theorem chaar_empty {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) : chaar K₀ ⊥ = 0 := sorry theorem chaar_self {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) : chaar K₀ { val := subtype.val K₀, property := and.left (subtype.property K₀) } = 1 := sorry theorem chaar_mono {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {K₁ : topological_space.compacts G} {K₂ : topological_space.compacts G} (h : subtype.val K₁ ⊆ subtype.val K₂) : chaar K₀ K₁ ≤ chaar K₀ K₂ := sorry theorem chaar_sup_le {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (K₁ : topological_space.compacts G) (K₂ : topological_space.compacts G) : chaar K₀ (K₁ ⊔ K₂) ≤ chaar K₀ K₁ + chaar K₀ K₂ := sorry theorem chaar_sup_eq {G : Type u_1} [group G] [topological_space G] [topological_group G] [t2_space G] {K₀ : topological_space.positive_compacts G} {K₁ : topological_space.compacts G} {K₂ : topological_space.compacts G} (h : disjoint (subtype.val K₁) (subtype.val K₂)) : chaar K₀ (K₁ ⊔ K₂) = chaar K₀ K₁ + chaar K₀ K₂ := sorry theorem is_left_invariant_chaar {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (g : G) (K : topological_space.compacts G) : chaar K₀ (topological_space.compacts.map (fun (b : G) => g * b) (continuous_mul_left g) K) = chaar K₀ K := sorry /-- The function `chaar` interpreted in `ennreal` -/ def echaar {G : Type u_1} [group G] [topological_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (K : topological_space.compacts G) : ennreal := ↑((fun (this : nnreal) => this) { val := chaar K₀ K, property := chaar_nonneg K₀ K }) /-! We only prove the properties for `echaar` that we use at least twice below. -/ /-- The variant of `chaar_sup_le` for `echaar` -/ theorem echaar_sup_le {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (K₁ : topological_space.compacts G) (K₂ : topological_space.compacts G) : echaar K₀ (K₁ ⊔ K₂) ≤ echaar K₀ K₁ + echaar K₀ K₂ := sorry /-- The variant of `chaar_mono` for `echaar` -/ theorem echaar_mono {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {K₁ : topological_space.compacts G} {K₂ : topological_space.compacts G} (h : subtype.val K₁ ⊆ subtype.val K₂) : echaar K₀ K₁ ≤ echaar K₀ K₂ := sorry /-- The variant of `chaar_self` for `echaar` -/ theorem echaar_self {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} : echaar K₀ { val := subtype.val K₀, property := and.left (subtype.property K₀) } = 1 := sorry /-- The variant of `is_left_invariant_chaar` for `echaar` -/ theorem is_left_invariant_echaar {G : Type u_1} [group G] [topological_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (g : G) (K : topological_space.compacts G) : echaar K₀ (topological_space.compacts.map (fun (b : G) => g * b) (continuous_mul_left g) K) = echaar K₀ K := eq.mpr (id (Eq.trans (propext ennreal.coe_eq_coe) (propext (iff.symm nnreal.coe_eq)))) (eq.mp (Eq.refl (chaar K₀ (topological_space.compacts.map (Mul.mul g) (continuous_mul_left g) K) = chaar K₀ K)) (is_left_invariant_chaar g K)) end haar /-! ### The Haar outer measure -/ /-- The Haar outer measure on `G`. It is not normalized, and is mainly used to construct `haar_measure`, which is a normalized measure. -/ def haar_outer_measure {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) : outer_measure G := outer_measure.of_content (haar.echaar K₀) sorry theorem haar_outer_measure_eq_infi {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] (K₀ : topological_space.positive_compacts G) (A : set G) : coe_fn (haar_outer_measure K₀) A = infi fun (U : set G) => infi fun (hU : is_open U) => infi fun (h : A ⊆ U) => inner_content (haar.echaar K₀) { val := U, property := hU } := outer_measure.of_content_eq_infi haar.echaar_sup_le A theorem echaar_le_haar_outer_measure {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (K : topological_space.compacts G) : haar.echaar K₀ K ≤ coe_fn (haar_outer_measure K₀) (subtype.val K) := outer_measure.le_of_content_compacts haar.echaar_sup_le K theorem haar_outer_measure_of_is_open {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (U : set G) (hU : is_open U) : coe_fn (haar_outer_measure K₀) U = inner_content (haar.echaar K₀) { val := U, property := hU } := outer_measure.of_content_opens haar.echaar_sup_le { val := U, property := hU } theorem haar_outer_measure_le_echaar {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : is_open U) (K : topological_space.compacts G) (h : U ⊆ subtype.val K) : coe_fn (haar_outer_measure K₀) U ≤ haar.echaar K₀ K := outer_measure.of_content_le haar.echaar_sup_le haar.echaar_mono { val := U, property := hU } K h theorem haar_outer_measure_exists_open {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {A : set G} (hA : coe_fn (haar_outer_measure K₀) A < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ (U : topological_space.opens G), A ⊆ ↑U ∧ coe_fn (haar_outer_measure K₀) ↑U ≤ coe_fn (haar_outer_measure K₀) A + ↑ε := outer_measure.of_content_exists_open haar.echaar_sup_le hA hε theorem haar_outer_measure_exists_compact {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : topological_space.opens G} (hU : coe_fn (haar_outer_measure K₀) ↑U < ⊤) {ε : nnreal} (hε : 0 < ε) : ∃ (K : topological_space.compacts G), subtype.val K ⊆ ↑U ∧ coe_fn (haar_outer_measure K₀) ↑U ≤ coe_fn (haar_outer_measure K₀) (subtype.val K) + ↑ε := outer_measure.of_content_exists_compact haar.echaar_sup_le hU hε theorem haar_outer_measure_caratheodory {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} (A : set G) : measurable_space.is_measurable' (outer_measure.caratheodory (haar_outer_measure K₀)) A ↔ ∀ (U : topological_space.opens G), coe_fn (haar_outer_measure K₀) (↑U ∩ A) + coe_fn (haar_outer_measure K₀) (↑U \ A) ≤ coe_fn (haar_outer_measure K₀) ↑U := outer_measure.of_content_caratheodory haar.echaar_sup_le A theorem one_le_haar_outer_measure_self {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} : 1 ≤ coe_fn (haar_outer_measure K₀) (subtype.val K₀) := sorry theorem haar_outer_measure_pos_of_is_open {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : is_open U) (h2U : set.nonempty U) : 0 < coe_fn (haar_outer_measure K₀) U := sorry theorem haar_outer_measure_self_pos {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] {K₀ : topological_space.positive_compacts G} : 0 < coe_fn (haar_outer_measure K₀) (subtype.val K₀) := has_lt.lt.trans_le (haar_outer_measure_pos_of_is_open is_open_interior (and.right (subtype.property K₀))) (outer_measure.mono (haar_outer_measure K₀) interior_subset) theorem haar_outer_measure_lt_top_of_is_compact {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [locally_compact_space G] {K₀ : topological_space.positive_compacts G} {K : set G} (hK : is_compact K) : coe_fn (haar_outer_measure K₀) K < ⊤ := sorry theorem haar_caratheodory_measurable {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] (K₀ : topological_space.positive_compacts G) : S ≤ outer_measure.caratheodory (haar_outer_measure K₀) := sorry /-! ### The Haar measure -/ /-- the Haar measure on `G`, scaled so that `haar_measure K₀ K₀ = 1`. -/ def haar_measure {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] (K₀ : topological_space.positive_compacts G) : measure G := coe_fn (haar_outer_measure K₀) (subtype.val K₀)⁻¹ • outer_measure.to_measure (haar_outer_measure K₀) (haar_caratheodory_measurable K₀) theorem haar_measure_apply {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] {K₀ : topological_space.positive_compacts G} {s : set G} (hs : is_measurable s) : coe_fn (haar_measure K₀) s = coe_fn (haar_outer_measure K₀) s / coe_fn (haar_outer_measure K₀) (subtype.val K₀) := sorry theorem is_mul_left_invariant_haar_measure {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] (K₀ : topological_space.positive_compacts G) : is_mul_left_invariant ⇑(haar_measure K₀) := sorry theorem haar_measure_self {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] {K₀ : topological_space.positive_compacts G} : coe_fn (haar_measure K₀) (subtype.val K₀) = 1 := sorry theorem haar_measure_pos_of_is_open {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] {K₀ : topological_space.positive_compacts G} {U : set G} (hU : is_open U) (h2U : set.nonempty U) : 0 < coe_fn (haar_measure K₀) U := sorry theorem regular_haar_measure {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] {K₀ : topological_space.positive_compacts G} : regular (haar_measure K₀) := sorry protected instance haar_measure.measure_theory.sigma_finite {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] [topological_space.separable_space G] (K₀ : topological_space.positive_compacts G) : sigma_finite (haar_measure K₀) := regular.sigma_finite regular_haar_measure /-- The Haar measure is unique up to scaling. More precisely: every σ-finite left invariant measure is a scalar multiple of the Haar measure. -/ theorem haar_measure_unique {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] [topological_space.second_countable_topology G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) (K₀ : topological_space.positive_compacts G) : μ = coe_fn μ (subtype.val K₀) • haar_measure K₀ := sorry theorem regular_of_left_invariant {G : Type u_1} [group G] [topological_space G] [t2_space G] [topological_group G] [S : measurable_space G] [borel_space G] [locally_compact_space G] [topological_space.second_countable_topology G] {μ : measure G} [sigma_finite μ] (hμ : is_mul_left_invariant ⇑μ) {K : set G} (hK : is_compact K) (h2K : set.nonempty (interior K)) (hμK : coe_fn μ K < ⊤) : regular μ := sorry
83603cdae69a4071799c0b910e2bb523a8d92afb
8cae430f0a71442d02dbb1cbb14073b31048e4b0
/test/restate_axiom.lean
7b3cbe5fbacbd8cc35ac95ef1514727a6341b987
[ "Apache-2.0" ]
permissive
leanprover-community/mathlib
56a2cadd17ac88caf4ece0a775932fa26327ba0e
442a83d738cb208d3600056c489be16900ba701d
refs/heads/master
1,693,584,102,358
1,693,471,902,000
1,693,471,902,000
97,922,418
1,595
352
Apache-2.0
1,694,693,445,000
1,500,624,130,000
Lean
UTF-8
Lean
false
false
325
lean
import tactic.restate_axiom open tactic structure A := (x : ℕ) (a' : x = 1 . skip) (b : x = 2 . skip) restate_axiom A.a' example (z : A) : z.x = 1 := begin success_if_fail { rw A.a' }, rw A.a end restate_axiom A.b f example (z : A) : z.x = 2 := by rw A.f restate_axiom A.b example (z : A) : z.x = 2 := by rw A.b_lemma
df9072a80f4cfda7cff67a86c19ab8c0c443b469
82e44445c70db0f03e30d7be725775f122d72f3e
/src/measure_theory/measure_space.lean
279ea0ddaccf4caa68bb5dd3a67a8cbd55f60720
[ "Apache-2.0" ]
permissive
stjordanis/mathlib
51e286d19140e3788ef2c470bc7b953e4991f0c9
2568d41bca08f5d6bf39d915434c8447e21f42ee
refs/heads/master
1,631,748,053,501
1,627,938,886,000
1,627,938,886,000
228,728,358
0
0
Apache-2.0
1,576,630,588,000
1,576,630,587,000
null
UTF-8
Lean
false
false
114,469
lean
/- Copyright (c) 2017 Johannes Hölzl. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Johannes Hölzl, Mario Carneiro -/ import measure_theory.measure_space_def import measure_theory.measurable_space /-! # Measure spaces The definition of a measure and a measure space are in `measure_theory.measure_space_def`, with only a few basic properties. This file provides many more properties of these objects. This separation allows the measurability tactic to import only the file `measure_space_def`, and to be available in `measure_space` (through `measurable_space`). Given a measurable space `α`, a measure on `α` is a function that sends measurable sets to the extended nonnegative reals that satisfies the following conditions: 1. `μ ∅ = 0`; 2. `μ` is countably additive. This means that the measure of a countable union of pairwise disjoint sets is equal to the measure of the individual sets. Every measure can be canonically extended to an outer measure, so that it assigns values to all subsets, not just the measurable subsets. On the other hand, a measure that is countably additive on measurable sets can be restricted to measurable sets to obtain a measure. In this file a measure is defined to be an outer measure that is countably additive on measurable sets, with the additional assumption that the outer measure is the canonical extension of the restricted measure. Measures on `α` form a complete lattice, and are closed under scalar multiplication with `ℝ≥0∞`. We introduce the following typeclasses for measures: * `probability_measure μ`: `μ univ = 1`; * `finite_measure μ`: `μ univ < ∞`; * `sigma_finite μ`: there exists a countable collection of measurable sets that cover `univ` where `μ` is finite; * `locally_finite_measure μ` : `∀ x, ∃ s ∈ 𝓝 x, μ s < ∞`; * `has_no_atoms μ` : `∀ x, μ {x} = 0`; possibly should be redefined as `∀ s, 0 < μ s → ∃ t ⊆ s, 0 < μ t ∧ μ t < μ s`. Given a measure, the null sets are the sets where `μ s = 0`, where `μ` denotes the corresponding outer measure (so `s` might not be measurable). We can then define the completion of `μ` as the measure on the least `σ`-algebra that also contains all null sets, by defining the measure to be `0` on the null sets. ## Main statements * `completion` is the completion of a measure to all null measurable sets. * `measure.of_measurable` and `outer_measure.to_measure` are two important ways to define a measure. ## Implementation notes Given `μ : measure α`, `μ s` is the value of the *outer measure* applied to `s`. This conveniently allows us to apply the measure to sets without proving that they are measurable. We get countable subadditivity for all sets, but only countable additivity for measurable sets. You often don't want to define a measure via its constructor. Two ways that are sometimes more convenient: * `measure.of_measurable` is a way to define a measure by only giving its value on measurable sets and proving the properties (1) and (2) mentioned above. * `outer_measure.to_measure` is a way of obtaining a measure from an outer measure by showing that all measurable sets in the measurable space are Carathéodory measurable. To prove that two measures are equal, there are multiple options: * `ext`: two measures are equal if they are equal on all measurable sets. * `ext_of_generate_from_of_Union`: two measures are equal if they are equal on a π-system generating the measurable sets, if the π-system contains a spanning increasing sequence of sets where the measures take finite value (in particular the measures are σ-finite). This is a special case of the more general `ext_of_generate_from_of_cover` * `ext_of_generate_finite`: two finite measures are equal if they are equal on a π-system generating the measurable sets. This is a special case of `ext_of_generate_from_of_Union` using `C ∪ {univ}`, but is easier to work with. A `measure_space` is a class that is a measurable space with a canonical measure. The measure is denoted `volume`. ## References * <https://en.wikipedia.org/wiki/Measure_(mathematics)> * <https://en.wikipedia.org/wiki/Complete_measure> * <https://en.wikipedia.org/wiki/Almost_everywhere> ## Tags measure, almost everywhere, measure space, completion, null set, null measurable set -/ noncomputable theory open classical set filter (hiding map) function measurable_space open_locale classical topological_space big_operators filter ennreal nnreal variables {α β γ δ ι : Type*} namespace measure_theory section variables [measurable_space α] {μ μ₁ μ₂ : measure α} {s s₁ s₂ t : set α} instance ae_is_measurably_generated : is_measurably_generated μ.ae := ⟨λ s hs, let ⟨t, hst, htm, htμ⟩ := exists_measurable_superset_of_null hs in ⟨tᶜ, compl_mem_ae_iff.2 htμ, htm.compl, compl_subset_comm.1 hst⟩⟩ lemma measure_Union [encodable β] {f : β → set α} (hn : pairwise (disjoint on f)) (h : ∀ i, measurable_set (f i)) : μ (⋃ i, f i) = ∑' i, μ (f i) := begin rw [measure_eq_extend (measurable_set.Union h), extend_Union measurable_set.empty _ measurable_set.Union _ hn h], { simp [measure_eq_extend, h] }, { exact μ.empty }, { exact μ.m_Union } end lemma measure_union (hd : disjoint s₁ s₂) (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) : μ (s₁ ∪ s₂) = μ s₁ + μ s₂ := begin rw [union_eq_Union, measure_Union, tsum_fintype, fintype.sum_bool, cond, cond], exacts [pairwise_disjoint_on_bool.2 hd, λ b, bool.cases_on b h₂ h₁] end lemma measure_add_measure_compl (h : measurable_set s) : μ s + μ sᶜ = μ univ := by { rw [← union_compl_self s, measure_union _ h h.compl], exact disjoint_compl_right } lemma measure_bUnion {s : set β} {f : β → set α} (hs : countable s) (hd : pairwise_on s (disjoint on f)) (h : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑' p : s, μ (f p) := begin haveI := hs.to_encodable, rw bUnion_eq_Union, exact measure_Union (hd.on_injective subtype.coe_injective $ λ x, x.2) (λ x, h x x.2) end lemma measure_sUnion {S : set (set α)} (hs : countable S) (hd : pairwise_on S disjoint) (h : ∀ s ∈ S, measurable_set s) : μ (⋃₀ S) = ∑' s : S, μ s := by rw [sUnion_eq_bUnion, measure_bUnion hs hd h] lemma measure_bUnion_finset {s : finset ι} {f : ι → set α} (hd : pairwise_on ↑s (disjoint on f)) (hm : ∀ b ∈ s, measurable_set (f b)) : μ (⋃ b ∈ s, f b) = ∑ p in s, μ (f p) := begin rw [← finset.sum_attach, finset.attach_eq_univ, ← tsum_fintype], exact measure_bUnion s.countable_to_set hd hm end /-- If `s` is a countable set, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma tsum_measure_preimage_singleton {s : set β} (hs : countable s) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑' b : s, μ (f ⁻¹' {↑b}) = μ (f ⁻¹' s) := by rw [← set.bUnion_preimage_singleton, measure_bUnion hs (pairwise_on_disjoint_fiber _ _) hf] /-- If `s` is a `finset`, then the measure of its preimage can be found as the sum of measures of the fibers `f ⁻¹' {y}`. -/ lemma sum_measure_preimage_singleton (s : finset β) {f : α → β} (hf : ∀ y ∈ s, measurable_set (f ⁻¹' {y})) : ∑ b in s, μ (f ⁻¹' {b}) = μ (f ⁻¹' ↑s) := by simp only [← measure_bUnion_finset (pairwise_on_disjoint_fiber _ _) hf, finset.set_bUnion_preimage_singleton] lemma measure_diff_null' (h : μ (s₁ ∩ s₂) = 0) : μ (s₁ \ s₂) = μ s₁ := measure_congr $ diff_ae_eq_self.2 h lemma measure_diff_null (h : μ s₂ = 0) : μ (s₁ \ s₂) = μ s₁ := measure_diff_null' $ measure_mono_null (inter_subset_right _ _) h lemma measure_diff (h : s₂ ⊆ s₁) (h₁ : measurable_set s₁) (h₂ : measurable_set s₂) (h_fin : μ s₂ < ∞) : μ (s₁ \ s₂) = μ s₁ - μ s₂ := begin refine (ennreal.add_sub_self' h_fin).symm.trans _, rw [← measure_union disjoint_diff h₂ (h₁.diff h₂), union_diff_cancel h] end lemma meas_eq_meas_of_null_diff {s t : set α} (hst : s ⊆ t) (h_nulldiff : μ (t.diff s) = 0) : μ s = μ t := by { rw [←diff_diff_cancel_left hst, ←@measure_diff_null _ _ _ t _ h_nulldiff], refl, } lemma meas_eq_meas_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃ \ s₁) = 0) : (μ s₁ = μ s₂) ∧ (μ s₂ = μ s₃) := begin have le12 : μ s₁ ≤ μ s₂ := measure_mono h12, have le23 : μ s₂ ≤ μ s₃ := measure_mono h23, have key : μ s₃ ≤ μ s₁ := calc μ s₃ = μ ((s₃ \ s₁) ∪ s₁) : by rw (diff_union_of_subset (h12.trans h23)) ... ≤ μ (s₃ \ s₁) + μ s₁ : measure_union_le _ _ ... = μ s₁ : by simp only [h_nulldiff, zero_add], exact ⟨le12.antisymm (le23.trans key), le23.antisymm (key.trans le12)⟩, end lemma meas_eq_meas_smaller_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃.diff s₁) = 0) : μ s₁ = μ s₂ := (meas_eq_meas_of_between_null_diff h12 h23 h_nulldiff).1 lemma meas_eq_meas_larger_of_between_null_diff {s₁ s₂ s₃ : set α} (h12 : s₁ ⊆ s₂) (h23 : s₂ ⊆ s₃) (h_nulldiff : μ (s₃.diff s₁) = 0) : μ s₂ = μ s₃ := (meas_eq_meas_of_between_null_diff h12 h23 h_nulldiff).2 lemma measure_compl (h₁ : measurable_set s) (h_fin : μ s < ∞) : μ (sᶜ) = μ univ - μ s := by { rw compl_eq_univ_diff, exact measure_diff (subset_univ s) measurable_set.univ h₁ h_fin } lemma sum_measure_le_measure_univ {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : pairwise_on ↑s (disjoint on t)) : ∑ i in s, μ (t i) ≤ μ (univ : set α) := by { rw ← measure_bUnion_finset H h, exact measure_mono (subset_univ _) } lemma tsum_measure_le_measure_univ {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : pairwise (disjoint on s)) : ∑' i, μ (s i) ≤ μ (univ : set α) := begin rw [ennreal.tsum_eq_supr_sum], exact supr_le (λ s, sum_measure_le_measure_univ (λ i hi, hs i) (λ i hi j hj hij, H i j hij)) end /-- Pigeonhole principle for measure spaces: if `∑' i, μ (s i) > μ univ`, then one of the intersections `s i ∩ s j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_tsum_measure (μ : measure α) {s : ι → set α} (hs : ∀ i, measurable_set (s i)) (H : μ (univ : set α) < ∑' i, μ (s i)) : ∃ i j (h : i ≠ j), (s i ∩ s j).nonempty := begin contrapose! H, apply tsum_measure_le_measure_univ hs, exact λ i j hij x hx, H i j hij ⟨x, hx⟩ end /-- Pigeonhole principle for measure spaces: if `s` is a `finset` and `∑ i in s, μ (t i) > μ univ`, then one of the intersections `t i ∩ t j` is not empty. -/ lemma exists_nonempty_inter_of_measure_univ_lt_sum_measure (μ : measure α) {s : finset ι} {t : ι → set α} (h : ∀ i ∈ s, measurable_set (t i)) (H : μ (univ : set α) < ∑ i in s, μ (t i)) : ∃ (i ∈ s) (j ∈ s) (h : i ≠ j), (t i ∩ t j).nonempty := begin contrapose! H, apply sum_measure_le_measure_univ h, exact λ i hi j hj hij x hx, H i hi j hj hij ⟨x, hx⟩ end /-- Continuity from below: the measure of the union of a directed sequence of measurable sets is the supremum of the measures. -/ lemma measure_Union_eq_supr [encodable ι] {s : ι → set α} (h : ∀ i, measurable_set (s i)) (hd : directed (⊆) s) : μ (⋃ i, s i) = ⨆ i, μ (s i) := begin by_cases hι : nonempty ι, swap, { simp only [supr_of_empty hι, Union], exact measure_empty }, resetI, refine le_antisymm _ (supr_le $ λ i, measure_mono $ subset_Union _ _), have : ∀ n, measurable_set (disjointed (λ n, ⋃ b ∈ encodable.decode₂ ι n, s b) n) := measurable_set.disjointed (measurable_set.bUnion_decode₂ h), rw [← encodable.Union_decode₂, ← Union_disjointed, measure_Union disjoint_disjointed this, ennreal.tsum_eq_supr_nat], simp only [← measure_bUnion_finset (disjoint_disjointed.pairwise_on _) (λ n _, this n)], refine supr_le (λ n, _), refine le_trans (_ : _ ≤ μ (⋃ (k ∈ finset.range n) (i ∈ encodable.decode₂ ι k), s i)) _, exact measure_mono (bUnion_subset_bUnion_right (λ k hk, disjointed_subset)), simp only [← finset.set_bUnion_option_to_finset, ← finset.set_bUnion_bUnion], generalize : (finset.range n).bUnion (λ k, (encodable.decode₂ ι k).to_finset) = t, rcases hd.finset_le t with ⟨i, hi⟩, exact le_supr_of_le i (measure_mono $ bUnion_subset hi) end lemma measure_bUnion_eq_supr {s : ι → set α} {t : set ι} (ht : countable t) (h : ∀ i ∈ t, measurable_set (s i)) (hd : directed_on ((⊆) on s) t) : μ (⋃ i ∈ t, s i) = ⨆ i ∈ t, μ (s i) := begin haveI := ht.to_encodable, rw [bUnion_eq_Union, measure_Union_eq_supr (set_coe.forall'.1 h) hd.directed_coe, supr_subtype'], refl end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the infimum of the measures. -/ lemma measure_Inter_eq_infi [encodable ι] {s : ι → set α} (h : ∀ i, measurable_set (s i)) (hd : directed (⊇) s) (hfin : ∃ i, μ (s i) < ∞) : μ (⋂ i, s i) = (⨅ i, μ (s i)) := begin rcases hfin with ⟨k, hk⟩, rw [← ennreal.sub_sub_cancel (by exact hk) (infi_le _ k), ennreal.sub_infi, ← ennreal.sub_sub_cancel (by exact hk) (measure_mono (Inter_subset _ k)), ← measure_diff (Inter_subset _ k) (h k) (measurable_set.Inter h) (lt_of_le_of_lt (measure_mono (Inter_subset _ k)) hk), diff_Inter, measure_Union_eq_supr], { congr' 1, refine le_antisymm (supr_le_supr2 $ λ i, _) (supr_le_supr $ λ i, _), { rcases hd i k with ⟨j, hji, hjk⟩, use j, rw [← measure_diff hjk (h _) (h _) ((measure_mono hjk).trans_lt hk)], exact measure_mono (diff_subset_diff_right hji) }, { rw [ennreal.sub_le_iff_le_add, ← measure_union disjoint_diff.symm ((h k).diff (h i)) (h i), set.union_comm], exact measure_mono (diff_subset_iff.1 $ subset.refl _) } }, { exact λ i, (h k).diff (h i) }, { exact hd.mono_comp _ (λ _ _, diff_subset_diff_right) } end lemma measure_eq_inter_diff (hs : measurable_set s) (ht : measurable_set t) : μ s = μ (s ∩ t) + μ (s \ t) := have hd : disjoint (s ∩ t) (s \ t) := assume a ⟨⟨_, hs⟩, _, hns⟩, hns hs , by rw [← measure_union hd (hs.inter ht) (hs.diff ht), inter_union_diff s t] lemma measure_union_add_inter (hs : measurable_set s) (ht : measurable_set t) : μ (s ∪ t) + μ (s ∩ t) = μ s + μ t := by { rw [measure_eq_inter_diff (hs.union ht) ht, set.union_inter_cancel_right, union_diff_right, measure_eq_inter_diff hs ht], ac_refl } /-- Continuity from below: the measure of the union of an increasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Union {s : ℕ → set α} (hs : ∀ n, measurable_set (s n)) (hm : monotone s) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋃ n, s n))) := begin rw measure_Union_eq_supr hs (directed_of_sup hm), exact tendsto_at_top_supr (assume n m hnm, measure_mono $ hm hnm) end /-- Continuity from above: the measure of the intersection of a decreasing sequence of measurable sets is the limit of the measures. -/ lemma tendsto_measure_Inter {s : ℕ → set α} (hs : ∀ n, measurable_set (s n)) (hm : ∀ ⦃n m⦄, n ≤ m → s m ⊆ s n) (hf : ∃ i, μ (s i) < ∞) : tendsto (μ ∘ s) at_top (𝓝 (μ (⋂ n, s n))) := begin rw measure_Inter_eq_infi hs (directed_of_sup hm) hf, exact tendsto_at_top_infi (assume n m hnm, measure_mono $ hm hnm), end /-- One direction of the **Borel-Cantelli lemma**: if (sᵢ) is a sequence of measurable sets such that ∑ μ sᵢ exists, then the limit superior of the sᵢ is a null set. -/ lemma measure_limsup_eq_zero {s : ℕ → set α} (hs : ∀ i, measurable_set (s i)) (hs' : ∑' i, μ (s i) ≠ ∞) : μ (limsup at_top s) = 0 := begin simp only [limsup_eq_infi_supr_of_nat', set.infi_eq_Inter, set.supr_eq_Union], -- We will show that both `μ (⨅ n, ⨆ i, s (i + n))` and `0` are the limit of `μ (⊔ i, s (i + n))` -- as `n` tends to infinity. For the former, we use continuity from above. refine tendsto_nhds_unique (tendsto_measure_Inter (λ i, measurable_set.Union (λ b, hs (b + i))) _ ⟨0, lt_of_le_of_lt (measure_Union_le s) (ennreal.lt_top_iff_ne_top.2 hs')⟩) _, { intros n m hnm x, simp only [set.mem_Union], exact λ ⟨i, hi⟩, ⟨i + (m - n), by simpa only [add_assoc, nat.sub_add_cancel hnm] using hi⟩ }, { -- For the latter, notice that, `μ (⨆ i, s (i + n)) ≤ ∑' s (i + n)`. Since the right hand side -- converges to `0` by hypothesis, so does the former and the proof is complete. exact (tendsto_of_tendsto_of_tendsto_of_le_of_le' tendsto_const_nhds (ennreal.tendsto_sum_nat_add (μ ∘ s) hs') (eventually_of_forall (by simp only [forall_const, zero_le])) (eventually_of_forall (λ i, measure_Union_le _))) } end lemma measure_if {x : β} {t : set β} {s : set α} {μ : measure α} : μ (if x ∈ t then s else ∅) = indicator t (λ _, μ s) x := by { split_ifs; simp [h] } end section outer_measure variables [ms : measurable_space α] {s t : set α} include ms /-- Obtain a measure by giving an outer measure where all sets in the σ-algebra are Carathéodory measurable. -/ def outer_measure.to_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : measure α := measure.of_measurable (λ s _, m s) m.empty (λ f hf hd, m.Union_eq_of_caratheodory (λ i, h _ (hf i)) hd) lemma le_to_outer_measure_caratheodory (μ : measure α) : ms ≤ μ.to_outer_measure.caratheodory := begin assume s hs, rw to_outer_measure_eq_induced_outer_measure, refine outer_measure.of_function_caratheodory (λ t, le_infi $ λ ht, _), rw [← measure_eq_extend (ht.inter hs), ← measure_eq_extend (ht.diff hs), ← measure_union _ (ht.inter hs) (ht.diff hs), inter_union_diff], exact le_refl _, exact λ x ⟨⟨_, h₁⟩, _, h₂⟩, h₂ h₁ end @[simp] lemma to_measure_to_outer_measure (m : outer_measure α) (h : ms ≤ m.caratheodory) : (m.to_measure h).to_outer_measure = m.trim := rfl @[simp] lemma to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) {s : set α} (hs : measurable_set s) : m.to_measure h s = m s := m.trim_eq hs lemma le_to_measure_apply (m : outer_measure α) (h : ms ≤ m.caratheodory) (s : set α) : m s ≤ m.to_measure h s := m.le_trim s @[simp] lemma to_outer_measure_to_measure {μ : measure α} : μ.to_outer_measure.to_measure (le_to_outer_measure_caratheodory _) = μ := measure.ext $ λ s, μ.to_outer_measure.trim_eq end outer_measure variables [measurable_space α] [measurable_space β] [measurable_space γ] variables {μ μ₁ μ₂ μ₃ ν ν' ν₁ ν₂ : measure α} {s s' t : set α} namespace measure protected lemma caratheodory (μ : measure α) (hs : measurable_set s) : μ (t ∩ s) + μ (t \ s) = μ t := (le_to_outer_measure_caratheodory μ s hs t).symm /-! ### The `ℝ≥0∞`-module of measures -/ instance : has_zero (measure α) := ⟨{ to_outer_measure := 0, m_Union := λ f hf hd, tsum_zero.symm, trimmed := outer_measure.trim_zero }⟩ @[simp] theorem zero_to_outer_measure : (0 : measure α).to_outer_measure = 0 := rfl @[simp, norm_cast] theorem coe_zero : ⇑(0 : measure α) = 0 := rfl lemma eq_zero_of_not_nonempty (h : ¬nonempty α) (μ : measure α) : μ = 0 := ext $ λ s hs, by simp only [eq_empty_of_not_nonempty h s, measure_empty] instance : inhabited (measure α) := ⟨0⟩ instance : has_add (measure α) := ⟨λ μ₁ μ₂, { to_outer_measure := μ₁.to_outer_measure + μ₂.to_outer_measure, m_Union := λ s hs hd, show μ₁ (⋃ i, s i) + μ₂ (⋃ i, s i) = ∑' i, (μ₁ (s i) + μ₂ (s i)), by rw [ennreal.tsum_add, measure_Union hd hs, measure_Union hd hs], trimmed := by rw [outer_measure.trim_add, μ₁.trimmed, μ₂.trimmed] }⟩ @[simp] theorem add_to_outer_measure (μ₁ μ₂ : measure α) : (μ₁ + μ₂).to_outer_measure = μ₁.to_outer_measure + μ₂.to_outer_measure := rfl @[simp, norm_cast] theorem coe_add (μ₁ μ₂ : measure α) : ⇑(μ₁ + μ₂) = μ₁ + μ₂ := rfl theorem add_apply (μ₁ μ₂ : measure α) (s : set α) : (μ₁ + μ₂) s = μ₁ s + μ₂ s := rfl instance add_comm_monoid : add_comm_monoid (measure α) := to_outer_measure_injective.add_comm_monoid to_outer_measure zero_to_outer_measure add_to_outer_measure instance : has_scalar ℝ≥0∞ (measure α) := ⟨λ c μ, { to_outer_measure := c • μ.to_outer_measure, m_Union := λ s hs hd, by simp [measure_Union, *, ennreal.tsum_mul_left], trimmed := by rw [outer_measure.trim_smul, μ.trimmed] }⟩ @[simp] theorem smul_to_outer_measure (c : ℝ≥0∞) (μ : measure α) : (c • μ).to_outer_measure = c • μ.to_outer_measure := rfl @[simp, norm_cast] theorem coe_smul (c : ℝ≥0∞) (μ : measure α) : ⇑(c • μ) = c • μ := rfl theorem smul_apply (c : ℝ≥0∞) (μ : measure α) (s : set α) : (c • μ) s = c * μ s := rfl instance : module ℝ≥0∞ (measure α) := injective.module ℝ≥0∞ ⟨to_outer_measure, zero_to_outer_measure, add_to_outer_measure⟩ to_outer_measure_injective smul_to_outer_measure @[simp, norm_cast] theorem coe_nnreal_smul (c : ℝ≥0) (μ : measure α) : ⇑(c • μ) = c • μ := rfl /-! ### The complete lattice of measures -/ /-- Measures are partially ordered. The definition of less equal here is equivalent to the definition without the measurable set condition, and this is shown by `measure.le_iff'`. It is defined this way since, to prove `μ ≤ ν`, we may simply `intros s hs` instead of rewriting followed by `intros s hs`. -/ instance : partial_order (measure α) := { le := λ m₁ m₂, ∀ s, measurable_set s → m₁ s ≤ m₂ s, le_refl := assume m s hs, le_refl _, le_trans := assume m₁ m₂ m₃ h₁ h₂ s hs, le_trans (h₁ s hs) (h₂ s hs), le_antisymm := assume m₁ m₂ h₁ h₂, ext $ assume s hs, le_antisymm (h₁ s hs) (h₂ s hs) } theorem le_iff : μ₁ ≤ μ₂ ↔ ∀ s, measurable_set s → μ₁ s ≤ μ₂ s := iff.rfl theorem to_outer_measure_le : μ₁.to_outer_measure ≤ μ₂.to_outer_measure ↔ μ₁ ≤ μ₂ := by rw [← μ₂.trimmed, outer_measure.le_trim_iff]; refl theorem le_iff' : μ₁ ≤ μ₂ ↔ ∀ s, μ₁ s ≤ μ₂ s := to_outer_measure_le.symm theorem lt_iff : μ < ν ↔ μ ≤ ν ∧ ∃ s, measurable_set s ∧ μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff, not_forall, not_le, exists_prop] theorem lt_iff' : μ < ν ↔ μ ≤ ν ∧ ∃ s, μ s < ν s := lt_iff_le_not_le.trans $ and_congr iff.rfl $ by simp only [le_iff', not_forall, not_le] instance covariant_add_le : covariant_class (measure α) (measure α) (+) (≤) := ⟨λ ν μ₁ μ₂ hμ s hs, add_le_add_left (hμ s hs) _⟩ protected lemma le_add_left (h : μ ≤ ν) : μ ≤ ν' + ν := λ s hs, le_add_left (h s hs) protected lemma le_add_right (h : μ ≤ ν) : μ ≤ ν + ν' := λ s hs, le_add_right (h s hs) section Inf variables {m : set (measure α)} lemma Inf_caratheodory (s : set α) (hs : measurable_set s) : (Inf (to_outer_measure '' m)).caratheodory.measurable_set' s := begin rw [outer_measure.Inf_eq_bounded_by_Inf_gen], refine outer_measure.bounded_by_caratheodory (λ t, _), simp only [outer_measure.Inf_gen, le_infi_iff, ball_image_iff, coe_to_outer_measure, measure_eq_infi t], intros μ hμ u htu hu, have hm : ∀ {s t}, s ⊆ t → outer_measure.Inf_gen (to_outer_measure '' m) s ≤ μ t, { intros s t hst, rw [outer_measure.Inf_gen_def], refine infi_le_of_le (μ.to_outer_measure) (infi_le_of_le (mem_image_of_mem _ hμ) _), rw [to_outer_measure_apply], refine measure_mono hst }, rw [measure_eq_inter_diff hu hs], refine add_le_add (hm $ inter_subset_inter_left _ htu) (hm $ diff_subset_diff_left htu) end instance : has_Inf (measure α) := ⟨λ m, (Inf (to_outer_measure '' m)).to_measure $ Inf_caratheodory⟩ lemma Inf_apply (hs : measurable_set s) : Inf m s = Inf (to_outer_measure '' m) s := to_measure_apply _ _ hs private lemma measure_Inf_le (h : μ ∈ m) : Inf m ≤ μ := have Inf (to_outer_measure '' m) ≤ μ.to_outer_measure := Inf_le (mem_image_of_mem _ h), assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s private lemma measure_le_Inf (h : ∀ μ' ∈ m, μ ≤ μ') : μ ≤ Inf m := have μ.to_outer_measure ≤ Inf (to_outer_measure '' m) := le_Inf $ ball_image_of_ball $ assume μ hμ, to_outer_measure_le.2 $ h _ hμ, assume s hs, by rw [Inf_apply hs, ← to_outer_measure_apply]; exact this s instance : complete_semilattice_Inf (measure α) := { Inf_le := λ s a, measure_Inf_le, le_Inf := λ s a, measure_le_Inf, ..(by apply_instance : partial_order (measure α)), ..(by apply_instance : has_Inf (measure α)), } instance : complete_lattice (measure α) := { bot := 0, bot_le := assume a s hs, by exact bot_le, /- Adding an explicit `top` makes `leanchecker` fail, see lean#364, disable for now top := (⊤ : outer_measure α).to_measure (by rw [outer_measure.top_caratheodory]; exact le_top), le_top := assume a s hs, by cases s.eq_empty_or_nonempty with h h; simp [h, to_measure_apply ⊤ _ hs, outer_measure.top_apply], -/ .. complete_lattice_of_complete_semilattice_Inf (measure α) } end Inf protected lemma zero_le (μ : measure α) : 0 ≤ μ := bot_le lemma nonpos_iff_eq_zero' : μ ≤ 0 ↔ μ = 0 := μ.zero_le.le_iff_eq @[simp] lemma measure_univ_eq_zero : μ univ = 0 ↔ μ = 0 := ⟨λ h, bot_unique $ λ s hs, trans_rel_left (≤) (measure_mono (subset_univ s)) h, λ h, h.symm ▸ rfl⟩ /-! ### Pushforward and pullback -/ /-- Lift a linear map between `outer_measure` spaces such that for each measure `μ` every measurable set is caratheodory-measurable w.r.t. `f μ` to a linear map between `measure` spaces. -/ def lift_linear (f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β) (hf : ∀ μ : measure α, ‹_› ≤ (f μ.to_outer_measure).caratheodory) : measure α →ₗ[ℝ≥0∞] measure β := { to_fun := λ μ, (f μ.to_outer_measure).to_measure (hf μ), map_add' := λ μ₁ μ₂, ext $ λ s hs, by simp [hs], map_smul' := λ c μ, ext $ λ s hs, by simp [hs] } @[simp] lemma lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) {s : set β} (hs : measurable_set s) : lift_linear f hf μ s = f μ.to_outer_measure s := to_measure_apply _ _ hs lemma le_lift_linear_apply {f : outer_measure α →ₗ[ℝ≥0∞] outer_measure β} (hf) (s : set β) : f μ.to_outer_measure s ≤ lift_linear f hf μ s := le_to_measure_apply _ _ s /-- The pushforward of a measure. It is defined to be `0` if `f` is not a measurable function. -/ def map (f : α → β) : measure α →ₗ[ℝ≥0∞] measure β := if hf : measurable f then lift_linear (outer_measure.map f) $ λ μ s hs t, le_to_outer_measure_caratheodory μ _ (hf hs) (f ⁻¹' t) else 0 /-- We can evaluate the pushforward on measurable sets. For non-measurable sets, see `measure_theory.measure.le_map_apply` and `measurable_equiv.map_apply`. -/ @[simp] theorem map_apply {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : map f μ s = μ (f ⁻¹' s) := by simp [map, dif_pos hf, hs] theorem map_of_not_measurable {f : α → β} (hf : ¬measurable f) : map f μ = 0 := by rw [map, dif_neg hf, linear_map.zero_apply] @[simp] lemma map_id : map id μ = μ := ext $ λ s, map_apply measurable_id lemma map_map {g : β → γ} {f : α → β} (hg : measurable g) (hf : measurable f) : map g (map f μ) = map (g ∘ f) μ := ext $ λ s hs, by simp [hf, hg, hs, hg hs, hg.comp hf, ← preimage_comp] @[mono] lemma map_mono (f : α → β) (h : μ ≤ ν) : map f μ ≤ map f ν := if hf : measurable f then λ s hs, by simp only [map_apply hf hs, h _ (hf hs)] else by simp only [map_of_not_measurable hf, le_rfl] /-- Even if `s` is not measurable, we can bound `map f μ s` from below. See also `measurable_equiv.map_apply`. -/ theorem le_map_apply {f : α → β} (hf : measurable f) (s : set β) : μ (f ⁻¹' s) ≤ map f μ s := begin rw [measure_eq_infi' (map f μ)], refine le_infi _, rintro ⟨t, hst, ht⟩, convert measure_mono (preimage_mono hst), exact map_apply hf ht end /-- Even if `s` is not measurable, `map f μ s = 0` implies that `μ (f ⁻¹' s) = 0`. -/ lemma preimage_null_of_map_null {f : α → β} (hf : measurable f) {s : set β} (hs : map f μ s = 0) : μ (f ⁻¹' s) = 0 := nonpos_iff_eq_zero.mp $ (le_map_apply hf s).trans_eq hs lemma tendsto_ae_map {f : α → β} (hf : measurable f) : tendsto f μ.ae (map f μ).ae := λ s hs, preimage_null_of_map_null hf hs /-- Pullback of a `measure`. If `f` sends each `measurable` set to a `measurable` set, then for each measurable set `s` we have `comap f μ s = μ (f '' s)`. -/ def comap (f : α → β) : measure β →ₗ[ℝ≥0∞] measure α := if hf : injective f ∧ ∀ s, measurable_set s → measurable_set (f '' s) then lift_linear (outer_measure.comap f) $ λ μ s hs t, begin simp only [coe_to_outer_measure, outer_measure.comap_apply, ← image_inter hf.1, image_diff hf.1], apply le_to_outer_measure_caratheodory, exact hf.2 s hs end else 0 lemma comap_apply (f : α → β) (hfi : injective f) (hf : ∀ s, measurable_set s → measurable_set (f '' s)) (μ : measure β) (hs : measurable_set s) : comap f μ s = μ (f '' s) := begin rw [comap, dif_pos, lift_linear_apply _ hs, outer_measure.comap_apply, coe_to_outer_measure], exact ⟨hfi, hf⟩ end /-! ### Restricting a measure -/ /-- Restrict a measure `μ` to a set `s` as an `ℝ≥0∞`-linear map. -/ def restrictₗ (s : set α) : measure α →ₗ[ℝ≥0∞] measure α := lift_linear (outer_measure.restrict s) $ λ μ s' hs' t, begin suffices : μ (s ∩ t) = μ (s ∩ t ∩ s') + μ (s ∩ t \ s'), { simpa [← set.inter_assoc, set.inter_comm _ s, ← inter_diff_assoc] }, exact le_to_outer_measure_caratheodory _ _ hs' _, end /-- Restrict a measure `μ` to a set `s`. -/ def restrict (μ : measure α) (s : set α) : measure α := restrictₗ s μ @[simp] lemma restrictₗ_apply (s : set α) (μ : measure α) : restrictₗ s μ = μ.restrict s := rfl /-- If `t` is a measurable set, then the measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. An alternate version requiring that `s` be measurable instead of `t` exists as `measure.restrict_apply'`. -/ @[simp] lemma restrict_apply (ht : measurable_set t) : μ.restrict s t = μ (t ∩ s) := by simp [← restrictₗ_apply, restrictₗ, ht] lemma restrict_eq_self (h_meas_t : measurable_set t) (h : t ⊆ s) : μ.restrict s t = μ t := by rw [restrict_apply h_meas_t, inter_eq_left_iff_subset.mpr h] lemma restrict_apply_self (μ:measure α) (h_meas_s : measurable_set s) : (μ.restrict s) s = μ s := (restrict_eq_self h_meas_s (set.subset.refl _)) lemma restrict_apply_univ (s : set α) : μ.restrict s univ = μ s := by rw [restrict_apply measurable_set.univ, set.univ_inter] lemma le_restrict_apply (s t : set α) : μ (t ∩ s) ≤ μ.restrict s t := by { rw [restrict, restrictₗ], convert le_lift_linear_apply _ t, simp } @[simp] lemma restrict_add (μ ν : measure α) (s : set α) : (μ + ν).restrict s = μ.restrict s + ν.restrict s := (restrictₗ s).map_add μ ν @[simp] lemma restrict_zero (s : set α) : (0 : measure α).restrict s = 0 := (restrictₗ s).map_zero @[simp] lemma restrict_smul (c : ℝ≥0∞) (μ : measure α) (s : set α) : (c • μ).restrict s = c • μ.restrict s := (restrictₗ s).map_smul c μ @[simp] lemma restrict_restrict (hs : measurable_set s) : (μ.restrict t).restrict s = μ.restrict (s ∩ t) := ext $ λ u hu, by simp [*, set.inter_assoc] lemma restrict_comm (hs : measurable_set s) (ht : measurable_set t) : (μ.restrict t).restrict s = (μ.restrict s).restrict t := by rw [restrict_restrict hs, restrict_restrict ht, inter_comm] lemma restrict_apply_eq_zero (ht : measurable_set t) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := by rw [restrict_apply ht] lemma measure_inter_eq_zero_of_restrict (h : μ.restrict s t = 0) : μ (t ∩ s) = 0 := nonpos_iff_eq_zero.1 (h ▸ le_restrict_apply _ _) lemma restrict_apply_eq_zero' (hs : measurable_set s) : μ.restrict s t = 0 ↔ μ (t ∩ s) = 0 := begin refine ⟨measure_inter_eq_zero_of_restrict, λ h, _⟩, rcases exists_measurable_superset_of_null h with ⟨t', htt', ht', ht'0⟩, apply measure_mono_null ((inter_subset _ _ _).1 htt'), rw [restrict_apply (hs.compl.union ht'), union_inter_distrib_right, compl_inter_self, set.empty_union], exact measure_mono_null (inter_subset_left _ _) ht'0 end @[simp] lemma restrict_eq_zero : μ.restrict s = 0 ↔ μ s = 0 := by rw [← measure_univ_eq_zero, restrict_apply_univ] lemma restrict_zero_set {s : set α} (h : μ s = 0) : μ.restrict s = 0 := by simp only [measure.restrict_eq_zero, h] @[simp] lemma restrict_empty : μ.restrict ∅ = 0 := ext $ λ s hs, by simp [hs] @[simp] lemma restrict_univ : μ.restrict univ = μ := ext $ λ s hs, by simp [hs] lemma restrict_eq_self_of_measurable_subset (ht : measurable_set t) (t_subset : t ⊆ s) : μ.restrict s t = μ t := by rw [measure.restrict_apply ht, set.inter_eq_self_of_subset_left t_subset] lemma restrict_union_apply (h : disjoint (t ∩ s) (t ∩ s')) (hs : measurable_set s) (hs' : measurable_set s') (ht : measurable_set t) : μ.restrict (s ∪ s') t = μ.restrict s t + μ.restrict s' t := begin simp only [restrict_apply, ht, set.inter_union_distrib_left], exact measure_union h (ht.inter hs) (ht.inter hs'), end lemma restrict_union (h : disjoint s t) (hs : measurable_set s) (ht : measurable_set t) : μ.restrict (s ∪ t) = μ.restrict s + μ.restrict t := ext $ λ t' ht', restrict_union_apply (h.mono inf_le_right inf_le_right) hs ht ht' lemma restrict_union_add_inter (hs : measurable_set s) (ht : measurable_set t) : μ.restrict (s ∪ t) + μ.restrict (s ∩ t) = μ.restrict s + μ.restrict t := begin ext1 u hu, simp only [add_apply, restrict_apply hu, inter_union_distrib_left], convert measure_union_add_inter (hu.inter hs) (hu.inter ht) using 3, rw [set.inter_left_comm (u ∩ s), set.inter_assoc, ← set.inter_assoc u u, set.inter_self] end @[simp] lemma restrict_add_restrict_compl (hs : measurable_set s) : μ.restrict s + μ.restrict sᶜ = μ := by rw [← restrict_union (@disjoint_compl_right (set α) _ _) hs hs.compl, union_compl_self, restrict_univ] @[simp] lemma restrict_compl_add_restrict (hs : measurable_set s) : μ.restrict sᶜ + μ.restrict s = μ := by rw [add_comm, restrict_add_restrict_compl hs] lemma restrict_union_le (s s' : set α) : μ.restrict (s ∪ s') ≤ μ.restrict s + μ.restrict s' := begin intros t ht, suffices : μ (t ∩ s ∪ t ∩ s') ≤ μ (t ∩ s) + μ (t ∩ s'), by simpa [ht, inter_union_distrib_left], apply measure_union_le end lemma restrict_Union_apply [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ∑' i, μ.restrict (s i) t := begin simp only [restrict_apply, ht, inter_Union], exact measure_Union (λ i j hij, (hd i j hij).mono inf_le_right inf_le_right) (λ i, ht.inter (hm i)) end lemma restrict_Union_apply_eq_supr [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) (hd : directed (⊆) s) {t : set α} (ht : measurable_set t) : μ.restrict (⋃ i, s i) t = ⨆ i, μ.restrict (s i) t := begin simp only [restrict_apply ht, inter_Union], rw [measure_Union_eq_supr], exacts [λ i, ht.inter (hm i), hd.mono_comp _ (λ s₁ s₂, inter_subset_inter_right _)] end lemma restrict_map {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : (map f μ).restrict s = map f (μ.restrict $ f ⁻¹' s) := ext $ λ t ht, by simp [*, hf ht] lemma map_comap_subtype_coe (hs : measurable_set s) : (map (coe : s → α)).comp (comap coe) = restrictₗ s := linear_map.ext $ λ μ, ext $ λ t ht, by rw [restrictₗ_apply, restrict_apply ht, linear_map.comp_apply, map_apply measurable_subtype_coe ht, comap_apply (coe : s → α) subtype.val_injective (λ _, hs.subtype_image) _ (measurable_subtype_coe ht), subtype.image_preimage_coe] /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ lemma restrict_mono' ⦃s s' : set α⦄ ⦃μ ν : measure α⦄ (hs : s ≤ᵐ[μ] s') (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := assume t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ (t ∩ s') : measure_mono_ae $ hs.mono $ λ x hx ⟨hxt, hxs⟩, ⟨hxt, hx hxs⟩ ... ≤ ν (t ∩ s') : le_iff'.1 hμν (t ∩ s') ... = ν.restrict s' t : (restrict_apply ht).symm /-- Restriction of a measure to a subset is monotone both in set and in measure. -/ @[mono] lemma restrict_mono ⦃s s' : set α⦄ (hs : s ⊆ s') ⦃μ ν : measure α⦄ (hμν : μ ≤ ν) : μ.restrict s ≤ ν.restrict s' := restrict_mono' (ae_of_all _ hs) hμν lemma restrict_le_self : μ.restrict s ≤ μ := assume t ht, calc μ.restrict s t = μ (t ∩ s) : restrict_apply ht ... ≤ μ t : measure_mono $ inter_subset_left t s lemma restrict_congr_meas (hs : measurable_set s) : μ.restrict s = ν.restrict s ↔ ∀ t ⊆ s, measurable_set t → μ t = ν t := ⟨λ H t hts ht, by rw [← inter_eq_self_of_subset_left hts, ← restrict_apply ht, H, restrict_apply ht], λ H, ext $ λ t ht, by rw [restrict_apply ht, restrict_apply ht, H _ (inter_subset_right _ _) (ht.inter hs)]⟩ lemma restrict_congr_mono (hs : s ⊆ t) (hm : measurable_set s) (h : μ.restrict t = ν.restrict t) : μ.restrict s = ν.restrict s := by rw [← inter_eq_self_of_subset_left hs, ← restrict_restrict hm, h, restrict_restrict hm] /-- If two measures agree on all measurable subsets of `s` and `t`, then they agree on all measurable subsets of `s ∪ t`. -/ lemma restrict_union_congr (hsm : measurable_set s) (htm : measurable_set t) : μ.restrict (s ∪ t) = ν.restrict (s ∪ t) ↔ μ.restrict s = ν.restrict s ∧ μ.restrict t = ν.restrict t := begin refine ⟨λ h, ⟨restrict_congr_mono (subset_union_left _ _) hsm h, restrict_congr_mono (subset_union_right _ _) htm h⟩, _⟩, simp only [restrict_congr_meas, hsm, htm, hsm.union htm], rintros ⟨hs, ht⟩ u hu hum, rw [measure_eq_inter_diff hum hsm, measure_eq_inter_diff hum hsm, hs _ (inter_subset_right _ _) (hum.inter hsm), ht _ (diff_subset_iff.2 hu) (hum.diff hsm)] end lemma restrict_finset_bUnion_congr {s : finset ι} {t : ι → set α} (htm : ∀ i ∈ s, measurable_set (t i)) : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin induction s using finset.induction_on with i s hi hs, { simp }, simp only [finset.mem_insert, or_imp_distrib, forall_and_distrib, forall_eq] at htm ⊢, simp only [finset.set_bUnion_insert, ← hs htm.2], exact restrict_union_congr htm.1 (s.measurable_set_bUnion htm.2) end lemma restrict_Union_congr [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) : μ.restrict (⋃ i, s i) = ν.restrict (⋃ i, s i) ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := begin refine ⟨λ h i, restrict_congr_mono (subset_Union _ _) (hm i) h, λ h, _⟩, ext1 t ht, have M : ∀ t : finset ι, measurable_set (⋃ i ∈ t, s i) := λ t, t.measurable_set_bUnion (λ i _, hm i), have D : directed (⊆) (λ t : finset ι, ⋃ i ∈ t, s i) := directed_of_sup (λ t₁ t₂ ht, bUnion_subset_bUnion_left ht), rw [Union_eq_Union_finset], simp only [restrict_Union_apply_eq_supr M D ht, (restrict_finset_bUnion_congr (λ i hi, hm i)).2 (λ i hi, h i)], end lemma restrict_bUnion_congr {s : set ι} {t : ι → set α} (hc : countable s) (htm : ∀ i ∈ s, measurable_set (t i)) : μ.restrict (⋃ i ∈ s, t i) = ν.restrict (⋃ i ∈ s, t i) ↔ ∀ i ∈ s, μ.restrict (t i) = ν.restrict (t i) := begin simp only [bUnion_eq_Union, set_coe.forall'] at htm ⊢, haveI := hc.to_encodable, exact restrict_Union_congr htm end lemma restrict_sUnion_congr {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, measurable_set s) : μ.restrict (⋃₀ S) = ν.restrict (⋃₀ S) ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := by rw [sUnion_eq_bUnion, restrict_bUnion_congr hc hm] /-- This lemma shows that `restrict` and `to_outer_measure` commute. Note that the LHS has a restrict on measures and the RHS has a restrict on outer measures. -/ lemma restrict_to_outer_measure_eq_to_outer_measure_restrict (h : measurable_set s) : (μ.restrict s).to_outer_measure = outer_measure.restrict s μ.to_outer_measure := by simp_rw [restrict, restrictₗ, lift_linear, linear_map.coe_mk, to_measure_to_outer_measure, outer_measure.restrict_trim h, μ.trimmed] /-- This lemma shows that `Inf` and `restrict` commute for measures. -/ lemma restrict_Inf_eq_Inf_restrict {m : set (measure α)} (hm : m.nonempty) (ht : measurable_set t) : (Inf m).restrict t = Inf ((λ μ : measure α, μ.restrict t) '' m) := begin ext1 s hs, simp_rw [Inf_apply hs, restrict_apply hs, Inf_apply (measurable_set.inter hs ht), set.image_image, restrict_to_outer_measure_eq_to_outer_measure_restrict ht, ← set.image_image _ to_outer_measure, ← outer_measure.restrict_Inf_eq_Inf_restrict _ (hm.image _), outer_measure.restrict_apply] end /-- If `s` is a measurable set, then the outer measure of `t` with respect to the restriction of the measure to `s` equals the outer measure of `t ∩ s`. This is an alternate version of `measure.restrict_apply`, requiring that `s` is measurable instead of `t`. -/ lemma restrict_apply' (hs : measurable_set s) : μ.restrict s t = μ (t ∩ s) := by rw [← coe_to_outer_measure, measure.restrict_to_outer_measure_eq_to_outer_measure_restrict hs, outer_measure.restrict_apply s t _, coe_to_outer_measure] lemma restrict_eq_self_of_subset_of_measurable (hs : measurable_set s) (t_subset : t ⊆ s) : μ.restrict s t = μ t := by rw [restrict_apply' hs, set.inter_eq_self_of_subset_left t_subset] /-! ### Extensionality results -/ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `Union`). -/ lemma ext_iff_of_Union_eq_univ [encodable ι] {s : ι → set α} (hm : ∀ i, measurable_set (s i)) (hs : (⋃ i, s i) = univ) : μ = ν ↔ ∀ i, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_Union_congr hm, hs, restrict_univ, restrict_univ] alias ext_iff_of_Union_eq_univ ↔ _ measure_theory.measure.ext_of_Union_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `bUnion`). -/ lemma ext_iff_of_bUnion_eq_univ {S : set ι} {s : ι → set α} (hc : countable S) (hm : ∀ i ∈ S, measurable_set (s i)) (hs : (⋃ i ∈ S, s i) = univ) : μ = ν ↔ ∀ i ∈ S, μ.restrict (s i) = ν.restrict (s i) := by rw [← restrict_bUnion_congr hc hm, hs, restrict_univ, restrict_univ] alias ext_iff_of_bUnion_eq_univ ↔ _ measure_theory.measure.ext_of_bUnion_eq_univ /-- Two measures are equal if they have equal restrictions on a spanning collection of sets (formulated using `sUnion`). -/ lemma ext_iff_of_sUnion_eq_univ {S : set (set α)} (hc : countable S) (hm : ∀ s ∈ S, measurable_set s) (hs : (⋃₀ S) = univ) : μ = ν ↔ ∀ s ∈ S, μ.restrict s = ν.restrict s := ext_iff_of_bUnion_eq_univ hc hm $ by rwa ← sUnion_eq_bUnion alias ext_iff_of_sUnion_eq_univ ↔ _ measure_theory.measure.ext_of_sUnion_eq_univ lemma ext_of_generate_from_of_cover {S T : set (set α)} (h_gen : ‹_› = generate_from S) (hc : countable T) (h_inter : is_pi_system S) (hm : ∀ t ∈ T, measurable_set t) (hU : ⋃₀ T = univ) (htop : ∀ t ∈ T, μ t < ∞) (ST_eq : ∀ (t ∈ T) (s ∈ S), μ (s ∩ t) = ν (s ∩ t)) (T_eq : ∀ t ∈ T, μ t = ν t) : μ = ν := begin refine ext_of_sUnion_eq_univ hc hm hU (λ t ht, _), ext1 u hu, simp only [restrict_apply hu], refine induction_on_inter h_gen h_inter _ (ST_eq t ht) _ _ hu, { simp only [set.empty_inter, measure_empty] }, { intros v hv hvt, have := T_eq t ht, rw [set.inter_comm] at hvt ⊢, rwa [measure_eq_inter_diff (hm _ ht) hv, measure_eq_inter_diff (hm _ ht) hv, ← hvt, ennreal.add_right_inj] at this, exact (measure_mono $ set.inter_subset_left _ _).trans_lt (htop t ht) }, { intros f hfd hfm h_eq, have : pairwise (disjoint on λ n, f n ∩ t) := λ m n hmn, (hfd m n hmn).mono (inter_subset_left _ _) (inter_subset_left _ _), simp only [Union_inter, measure_Union this (λ n, (hfm n).inter (hm t ht)), h_eq] } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `sUnion`. -/ lemma ext_of_generate_from_of_cover_subset {S T : set (set α)} (h_gen : ‹_› = generate_from S) (h_inter : is_pi_system S) (h_sub : T ⊆ S) (hc : countable T) (hU : ⋃₀ T = univ) (htop : ∀ s ∈ T, μ s < ∞) (h_eq : ∀ s ∈ S, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover h_gen hc h_inter _ hU htop _ (λ t ht, h_eq t (h_sub ht)), { intros t ht, rw [h_gen], exact generate_measurable.basic _ (h_sub ht) }, { intros t ht s hs, cases (s ∩ t).eq_empty_or_nonempty with H H, { simp only [H, measure_empty] }, { exact h_eq _ (h_inter _ _ hs (h_sub ht) H) } } end /-- Two measures are equal if they are equal on the π-system generating the σ-algebra, and they are both finite on a increasing spanning sequence of sets in the π-system. This lemma is formulated using `Union`. `finite_spanning_sets_in.ext` is a reformulation of this lemma. -/ lemma ext_of_generate_from_of_Union (C : set (set α)) (B : ℕ → set α) (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h1B : (⋃ i, B i) = univ) (h2B : ∀ i, B i ∈ C) (hμB : ∀ i, μ (B i) < ∞) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := begin refine ext_of_generate_from_of_cover_subset hA hC _ (countable_range B) h1B _ h_eq, { rintro _ ⟨i, rfl⟩, apply h2B }, { rintro _ ⟨i, rfl⟩, apply hμB } end /-- The dirac measure. -/ def dirac (a : α) : measure α := (outer_measure.dirac a).to_measure (by simp) lemma le_dirac_apply {a} : s.indicator 1 a ≤ dirac a s := outer_measure.dirac_apply a s ▸ le_to_measure_apply _ _ _ @[simp] lemma dirac_apply' (a : α) (hs : measurable_set s) : dirac a s = s.indicator 1 a := to_measure_apply _ _ hs @[simp] lemma dirac_apply_of_mem {a : α} (h : a ∈ s) : dirac a s = 1 := begin have : ∀ t : set α, a ∈ t → t.indicator (1 : α → ℝ≥0∞) a = 1, from λ t ht, indicator_of_mem ht 1, refine le_antisymm (this univ trivial ▸ _) (this s h ▸ le_dirac_apply), rw [← dirac_apply' a measurable_set.univ], exact measure_mono (subset_univ s) end @[simp] lemma dirac_apply [measurable_singleton_class α] (a : α) (s : set α) : dirac a s = s.indicator 1 a := begin by_cases h : a ∈ s, by rw [dirac_apply_of_mem h, indicator_of_mem h, pi.one_apply], rw [indicator_of_not_mem h, ← nonpos_iff_eq_zero], calc dirac a s ≤ dirac a {a}ᶜ : measure_mono (subset_compl_comm.1 $ singleton_subset_iff.2 h) ... = 0 : by simp [dirac_apply' _ (measurable_set_singleton _).compl] end lemma map_dirac {f : α → β} (hf : measurable f) (a : α) : map f (dirac a) = dirac (f a) := ext $ assume s hs, by simp [hs, map_apply hf hs, hf hs, indicator_apply] /-- Sum of an indexed family of measures. -/ def sum (f : ι → measure α) : measure α := (outer_measure.sum (λ i, (f i).to_outer_measure)).to_measure $ le_trans (by exact le_infi (λ i, le_to_outer_measure_caratheodory _)) (outer_measure.le_sum_caratheodory _) lemma le_sum_apply (f : ι → measure α) (s : set α) : (∑' i, f i s) ≤ sum f s := le_to_measure_apply _ _ _ @[simp] lemma sum_apply (f : ι → measure α) {s : set α} (hs : measurable_set s) : sum f s = ∑' i, f i s := to_measure_apply _ _ hs lemma le_sum (μ : ι → measure α) (i : ι) : μ i ≤ sum μ := λ s hs, by simp only [sum_apply μ hs, ennreal.le_tsum i] lemma restrict_Union [encodable ι] {s : ι → set α} (hd : pairwise (disjoint on s)) (hm : ∀ i, measurable_set (s i)) : μ.restrict (⋃ i, s i) = sum (λ i, μ.restrict (s i)) := ext $ λ t ht, by simp only [sum_apply _ ht, restrict_Union_apply hd hm ht] lemma restrict_Union_le [encodable ι] {s : ι → set α} : μ.restrict (⋃ i, s i) ≤ sum (λ i, μ.restrict (s i)) := begin intros t ht, suffices : μ (⋃ i, t ∩ s i) ≤ ∑' i, μ (t ∩ s i), by simpa [ht, inter_Union], apply measure_Union_le end @[simp] lemma sum_bool (f : bool → measure α) : sum f = f tt + f ff := ext $ λ s hs, by simp [hs, tsum_fintype] @[simp] lemma sum_cond (μ ν : measure α) : sum (λ b, cond b μ ν) = μ + ν := sum_bool _ @[simp] lemma restrict_sum (μ : ι → measure α) {s : set α} (hs : measurable_set s) : (sum μ).restrict s = sum (λ i, (μ i).restrict s) := ext $ λ t ht, by simp only [sum_apply, restrict_apply, ht, ht.inter hs] /-- Counting measure on any measurable space. -/ def count : measure α := sum dirac lemma le_count_apply : (∑' i : s, 1 : ℝ≥0∞) ≤ count s := calc (∑' i : s, 1 : ℝ≥0∞) = ∑' i, indicator s 1 i : tsum_subtype s 1 ... ≤ ∑' i, dirac i s : ennreal.tsum_le_tsum $ λ x, le_dirac_apply ... ≤ count s : le_sum_apply _ _ lemma count_apply (hs : measurable_set s) : count s = ∑' i : s, 1 := by simp only [count, sum_apply, hs, dirac_apply', ← tsum_subtype s 1, pi.one_apply] @[simp] lemma count_apply_finset [measurable_singleton_class α] (s : finset α) : count (↑s : set α) = s.card := calc count (↑s : set α) = ∑' i : (↑s : set α), 1 : count_apply s.measurable_set ... = ∑ i in s, 1 : s.tsum_subtype 1 ... = s.card : by simp lemma count_apply_finite [measurable_singleton_class α] (s : set α) (hs : finite s) : count s = hs.to_finset.card := by rw [← count_apply_finset, finite.coe_to_finset] /-- `count` measure evaluates to infinity at infinite sets. -/ lemma count_apply_infinite (hs : s.infinite) : count s = ∞ := begin refine top_unique (le_of_tendsto' ennreal.tendsto_nat_nhds_top $ λ n, _), rcases hs.exists_subset_card_eq n with ⟨t, ht, rfl⟩, calc (t.card : ℝ≥0∞) = ∑ i in t, 1 : by simp ... = ∑' i : (t : set α), 1 : (t.tsum_subtype 1).symm ... ≤ count (t : set α) : le_count_apply ... ≤ count s : measure_mono ht end @[simp] lemma count_apply_eq_top [measurable_singleton_class α] : count s = ∞ ↔ s.infinite := begin by_cases hs : s.finite, { simp [set.infinite, hs, count_apply_finite] }, { change s.infinite at hs, simp [hs, count_apply_infinite] } end @[simp] lemma count_apply_lt_top [measurable_singleton_class α] : count s < ∞ ↔ s.finite := calc count s < ∞ ↔ count s ≠ ∞ : lt_top_iff_ne_top ... ↔ ¬s.infinite : not_congr count_apply_eq_top ... ↔ s.finite : not_not /-! ### Absolute continuity -/ /-- We say that `μ` is absolutely continuous with respect to `ν`, or that `μ` is dominated by `ν`, if `ν(A) = 0` implies that `μ(A) = 0`. -/ def absolutely_continuous (μ ν : measure α) : Prop := ∀ ⦃s : set α⦄, ν s = 0 → μ s = 0 infix ` ≪ `:50 := absolutely_continuous lemma absolutely_continuous_of_le (h : μ ≤ ν) : μ ≪ ν := λ s hs, nonpos_iff_eq_zero.1 $ hs ▸ le_iff'.1 h s alias absolutely_continuous_of_le ← has_le.le.absolutely_continuous lemma absolutely_continuous_of_eq (h : μ = ν) : μ ≪ ν := h.le.absolutely_continuous alias absolutely_continuous_of_eq ← eq.absolutely_continuous namespace absolutely_continuous lemma mk (h : ∀ ⦃s : set α⦄, measurable_set s → ν s = 0 → μ s = 0) : μ ≪ ν := begin intros s hs, rcases exists_measurable_superset_of_null hs with ⟨t, h1t, h2t, h3t⟩, exact measure_mono_null h1t (h h2t h3t), end @[refl] protected lemma refl (μ : measure α) : μ ≪ μ := rfl.absolutely_continuous protected lemma rfl : μ ≪ μ := λ s hs, hs @[trans] protected lemma trans (h1 : μ₁ ≪ μ₂) (h2 : μ₂ ≪ μ₃) : μ₁ ≪ μ₃ := λ s hs, h1 $ h2 hs @[mono] protected lemma map (h : μ ≪ ν) (f : α → β) : map f μ ≪ map f ν := if hf : measurable f then absolutely_continuous.mk $ λ s hs, by simpa [hf, hs] using @h _ else by simp only [map_of_not_measurable hf] end absolutely_continuous lemma ae_le_iff_absolutely_continuous : μ.ae ≤ ν.ae ↔ μ ≪ ν := ⟨λ h s, by { rw [measure_zero_iff_ae_nmem, measure_zero_iff_ae_nmem], exact λ hs, h hs }, λ h s hs, h hs⟩ alias ae_le_iff_absolutely_continuous ↔ has_le.le.absolutely_continuous_of_ae measure_theory.measure.absolutely_continuous.ae_le alias absolutely_continuous.ae_le ← ae_mono' lemma absolutely_continuous.ae_eq (h : μ ≪ ν) {f g : α → δ} (h' : f =ᵐ[ν] g) : f =ᵐ[μ] g := h.ae_le h' /-! ### Quasi measure preserving maps (a.k.a. non-singular maps) -/ /-- A map `f : α → β` is said to be *quasi measure preserving* (a.k.a. non-singular) w.r.t. measures `μa` and `μb` if it is measurable and `μb s = 0` implies `μa (f ⁻¹' s) = 0`. -/ @[protect_proj] structure quasi_measure_preserving (f : α → β) (μa : measure α . volume_tac) (μb : measure β . volume_tac) : Prop := (measurable : measurable f) (absolutely_continuous : map f μa ≪ μb) namespace quasi_measure_preserving protected lemma id (μ : measure α) : quasi_measure_preserving id μ μ := ⟨measurable_id, map_id.absolutely_continuous⟩ variables {μa μa' : measure α} {μb μb' : measure β} {μc : measure γ} {f : α → β} lemma mono_left (h : quasi_measure_preserving f μa μb) (ha : μa' ≪ μa) : quasi_measure_preserving f μa' μb := ⟨h.1, (ha.map f).trans h.2⟩ lemma mono_right (h : quasi_measure_preserving f μa μb) (ha : μb ≪ μb') : quasi_measure_preserving f μa μb' := ⟨h.1, h.2.trans ha⟩ @[mono] lemma mono (ha : μa' ≪ μa) (hb : μb ≪ μb') (h : quasi_measure_preserving f μa μb) : quasi_measure_preserving f μa' μb' := (h.mono_left ha).mono_right hb protected lemma comp {g : β → γ} {f : α → β} (hg : quasi_measure_preserving g μb μc) (hf : quasi_measure_preserving f μa μb) : quasi_measure_preserving (g ∘ f) μa μc := ⟨hg.measurable.comp hf.measurable, by { rw ← map_map hg.1 hf.1, exact (hf.2.map g).trans hg.2 }⟩ protected lemma iterate {f : α → α} (hf : quasi_measure_preserving f μa μa) : ∀ n, quasi_measure_preserving (f^[n]) μa μa | 0 := quasi_measure_preserving.id μa | (n + 1) := (iterate n).comp hf lemma ae_map_le (h : quasi_measure_preserving f μa μb) : (map f μa).ae ≤ μb.ae := h.2.ae_le lemma tendsto_ae (h : quasi_measure_preserving f μa μb) : tendsto f μa.ae μb.ae := (tendsto_ae_map h.1).mono_right h.ae_map_le lemma ae (h : quasi_measure_preserving f μa μb) {p : β → Prop} (hg : ∀ᵐ x ∂μb, p x) : ∀ᵐ x ∂μa, p (f x) := h.tendsto_ae hg lemma ae_eq (h : quasi_measure_preserving f μa μb) {g₁ g₂ : β → δ} (hg : g₁ =ᵐ[μb] g₂) : g₁ ∘ f =ᵐ[μa] g₂ ∘ f := h.ae hg end quasi_measure_preserving /-! ### The `cofinite` filter -/ /-- The filter of sets `s` such that `sᶜ` has finite measure. -/ def cofinite (μ : measure α) : filter α := { sets := {s | μ sᶜ < ∞}, univ_sets := by simp, inter_sets := λ s t hs ht, by { simp only [compl_inter, mem_set_of_eq], calc μ (sᶜ ∪ tᶜ) ≤ μ sᶜ + μ tᶜ : measure_union_le _ _ ... < ∞ : ennreal.add_lt_top.2 ⟨hs, ht⟩ }, sets_of_superset := λ s t hs hst, lt_of_le_of_lt (measure_mono $ compl_subset_compl.2 hst) hs } lemma mem_cofinite : s ∈ μ.cofinite ↔ μ sᶜ < ∞ := iff.rfl lemma compl_mem_cofinite : sᶜ ∈ μ.cofinite ↔ μ s < ∞ := by rw [mem_cofinite, compl_compl] lemma eventually_cofinite {p : α → Prop} : (∀ᶠ x in μ.cofinite, p x) ↔ μ {x | ¬p x} < ∞ := iff.rfl end measure open measure @[simp] lemma ae_eq_bot : μ.ae = ⊥ ↔ μ = 0 := by rw [← empty_in_sets_eq_bot, mem_ae_iff, compl_empty, measure_univ_eq_zero] @[simp] lemma ae_ne_bot : μ.ae.ne_bot ↔ μ ≠ 0 := ne_bot_iff.trans (not_congr ae_eq_bot) @[simp] lemma ae_zero : (0 : measure α).ae = ⊥ := ae_eq_bot.2 rfl @[mono] lemma ae_mono {μ ν : measure α} (h : μ ≤ ν) : μ.ae ≤ ν.ae := h.absolutely_continuous.ae_le lemma mem_ae_map_iff {f : α → β} (hf : measurable f) {s : set β} (hs : measurable_set s) : s ∈ (map f μ).ae ↔ (f ⁻¹' s) ∈ μ.ae := by simp only [mem_ae_iff, map_apply hf hs.compl, preimage_compl] lemma mem_ae_of_mem_ae_map {f : α → β} (hf : measurable f) {s : set β} (hs : s ∈ (map f μ).ae) : f ⁻¹' s ∈ μ.ae := begin apply le_antisymm _ bot_le, calc μ (f ⁻¹' sᶜ) ≤ (map f μ) sᶜ : le_map_apply hf sᶜ ... = 0 : hs end lemma ae_map_iff {f : α → β} (hf : measurable f) {p : β → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ y ∂ (map f μ), p y) ↔ ∀ᵐ x ∂ μ, p (f x) := mem_ae_map_iff hf hp lemma ae_of_ae_map {f : α → β} (hf : measurable f) {p : β → Prop} (h : ∀ᵐ y ∂ (map f μ), p y) : ∀ᵐ x ∂ μ, p (f x) := mem_ae_of_mem_ae_map hf h lemma ae_map_mem_range (f : α → β) (hf : measurable_set (range f)) (μ : measure α) : ∀ᵐ x ∂(map f μ), x ∈ range f := begin by_cases h : measurable f, { change range f ∈ (map f μ).ae, rw mem_ae_map_iff h hf, apply eventually_of_forall, exact mem_range_self }, { simp [map_of_not_measurable h] } end lemma ae_restrict_iff {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply hp.compl], congr' with x, simp [and_comm] end lemma ae_imp_of_ae_restrict {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂(μ.restrict s), p x) : ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff] at h ⊢, simpa [set_of_and, inter_comm] using measure_inter_eq_zero_of_restrict h end lemma ae_restrict_iff' {s : set α} {p : α → Prop} (hs : measurable_set s) : (∀ᵐ x ∂(μ.restrict s), p x) ↔ ∀ᵐ x ∂μ, x ∈ s → p x := begin simp only [ae_iff, ← compl_set_of, restrict_apply_eq_zero' hs], congr' with x, simp [and_comm] end lemma ae_restrict_mem {s : set α} (hs : measurable_set s) : ∀ᵐ x ∂(μ.restrict s), x ∈ s := (ae_restrict_iff' hs).2 (filter.eventually_of_forall (λ x, id)) lemma ae_restrict_of_ae {s : set α} {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) : (∀ᵐ x ∂(μ.restrict s), p x) := eventually.filter_mono (ae_mono measure.restrict_le_self) h lemma ae_restrict_of_ae_restrict_of_subset {s t : set α} {p : α → Prop} (hst : s ⊆ t) (h : ∀ᵐ x ∂(μ.restrict t), p x) : (∀ᵐ x ∂(μ.restrict s), p x) := h.filter_mono (ae_mono $ measure.restrict_mono hst (le_refl μ)) lemma ae_smul_measure {p : α → Prop} (h : ∀ᵐ x ∂μ, p x) (c : ℝ≥0∞) : ∀ᵐ x ∂(c • μ), p x := ae_iff.2 $ by rw [smul_apply, ae_iff.1 h, mul_zero] lemma ae_smul_measure_iff {p : α → Prop} {c : ℝ≥0∞} (hc : c ≠ 0) : (∀ᵐ x ∂(c • μ), p x) ↔ ∀ᵐ x ∂μ, p x := by simp [ae_iff, hc] lemma ae_add_measure_iff {p : α → Prop} {ν} : (∀ᵐ x ∂μ + ν, p x) ↔ (∀ᵐ x ∂μ, p x) ∧ ∀ᵐ x ∂ν, p x := add_eq_zero_iff lemma ae_eq_comp' {ν : measure β} {f : α → β} {g g' : β → δ} (hf : measurable f) (h : g =ᵐ[ν] g') (h2 : map f μ ≪ ν) : g ∘ f =ᵐ[μ] g' ∘ f := (quasi_measure_preserving.mk hf h2).ae_eq h lemma ae_eq_comp {f : α → β} {g g' : β → δ} (hf : measurable f) (h : g =ᵐ[measure.map f μ] g') : g ∘ f =ᵐ[μ] g' ∘ f := ae_eq_comp' hf h absolutely_continuous.rfl lemma le_ae_restrict : μ.ae ⊓ 𝓟 s ≤ (μ.restrict s).ae := λ s hs, eventually_inf_principal.2 (ae_imp_of_ae_restrict hs) @[simp] lemma ae_restrict_eq (hs : measurable_set s) : (μ.restrict s).ae = μ.ae ⊓ 𝓟 s := begin ext t, simp only [mem_inf_principal, mem_ae_iff, restrict_apply_eq_zero' hs, compl_set_of, not_imp, and_comm (_ ∈ s)], refl end @[simp] lemma ae_restrict_eq_bot {s} : (μ.restrict s).ae = ⊥ ↔ μ s = 0 := ae_eq_bot.trans restrict_eq_zero @[simp] lemma ae_restrict_ne_bot {s} : (μ.restrict s).ae.ne_bot ↔ 0 < μ s := ne_bot_iff.trans $ (not_congr ae_restrict_eq_bot).trans pos_iff_ne_zero.symm lemma self_mem_ae_restrict {s} (hs : measurable_set s) : s ∈ (μ.restrict s).ae := by simp only [ae_restrict_eq hs, exists_prop, mem_principal_sets, mem_inf_sets]; exact ⟨_, univ_mem_sets, s, by rw [univ_inter, and_self]⟩ /-- A version of the Borel-Cantelli lemma: if `sᵢ` is a sequence of measurable sets such that `∑ μ sᵢ` exists, then for almost all `x`, `x` does not belong to almost all `sᵢ`. -/ lemma ae_eventually_not_mem {s : ℕ → set α} (hs : ∀ i, measurable_set (s i)) (hs' : ∑' i, μ (s i) ≠ ∞) : ∀ᵐ x ∂ μ, ∀ᶠ n in at_top, x ∉ s n := begin refine measure_mono_null _ (measure_limsup_eq_zero hs hs'), rw ←set.le_eq_subset, refine le_Inf (λ t ht x hx, _), simp only [le_eq_subset, not_exists, eventually_map, exists_prop, ge_iff_le, mem_set_of_eq, eventually_at_top, mem_compl_eq, not_forall, not_not_mem] at hx ht, rcases ht with ⟨i, hi⟩, rcases hx i with ⟨j, ⟨hj, hj'⟩⟩, exact hi j hj hj' end lemma mem_ae_dirac_iff {a : α} (hs : measurable_set s) : s ∈ (dirac a).ae ↔ a ∈ s := by by_cases a ∈ s; simp [mem_ae_iff, dirac_apply', hs.compl, indicator_apply, *] lemma ae_dirac_iff {a : α} {p : α → Prop} (hp : measurable_set {x | p x}) : (∀ᵐ x ∂(dirac a), p x) ↔ p a := mem_ae_dirac_iff hp @[simp] lemma ae_dirac_eq [measurable_singleton_class α] (a : α) : (dirac a).ae = pure a := by { ext s, simp [mem_ae_iff, imp_false] } lemma ae_eq_dirac' [measurable_singleton_class β] {a : α} {f : α → β} (hf : measurable f) : f =ᵐ[dirac a] const α (f a) := (ae_dirac_iff $ show measurable_set (f ⁻¹' {f a}), from hf $ measurable_set_singleton _).2 rfl lemma ae_eq_dirac [measurable_singleton_class α] {a : α} (f : α → δ) : f =ᵐ[dirac a] const α (f a) := by simp [filter.eventually_eq] lemma restrict_mono_ae (h : s ≤ᵐ[μ] t) : μ.restrict s ≤ μ.restrict t := begin intros u hu, simp only [restrict_apply hu], exact measure_mono_ae (h.mono $ λ x hx, and.imp id hx) end lemma restrict_congr_set (H : s =ᵐ[μ] t) : μ.restrict s = μ.restrict t := le_antisymm (restrict_mono_ae H.le) (restrict_mono_ae H.symm.le) section finite_measure /-- A measure `μ` is called finite if `μ univ < ∞`. -/ class finite_measure (μ : measure α) : Prop := (measure_univ_lt_top : μ univ < ∞) instance restrict.finite_measure (μ : measure α) [hs : fact (μ s < ∞)] : finite_measure (μ.restrict s) := ⟨by simp [hs.elim]⟩ lemma measure_lt_top (μ : measure α) [finite_measure μ] (s : set α) : μ s < ∞ := (measure_mono (subset_univ s)).trans_lt finite_measure.measure_univ_lt_top lemma measure_ne_top (μ : measure α) [finite_measure μ] (s : set α) : μ s ≠ ∞ := ne_of_lt (measure_lt_top μ s) /-- The measure of the whole space with respect to a finite measure, considered as `ℝ≥0`. -/ def measure_univ_nnreal (μ : measure α) : ℝ≥0 := (μ univ).to_nnreal @[simp] lemma coe_measure_univ_nnreal (μ : measure α) [finite_measure μ] : ↑(measure_univ_nnreal μ) = μ univ := ennreal.coe_to_nnreal (measure_ne_top μ univ) instance finite_measure_zero : finite_measure (0 : measure α) := ⟨by simp⟩ instance finite_measure_add [finite_measure μ] [finite_measure ν] : finite_measure (μ + ν) := { measure_univ_lt_top := begin rw [measure.coe_add, pi.add_apply, ennreal.add_lt_top], exact ⟨measure_lt_top _ _, measure_lt_top _ _⟩, end } instance finite_measure_smul_nnreal [finite_measure μ] {r : ℝ≥0} : finite_measure (r • μ) := { measure_univ_lt_top := ennreal.mul_lt_top ennreal.coe_lt_top (measure_lt_top _ _) } @[simp] lemma measure_univ_nnreal_zero : measure_univ_nnreal (0 : measure α) = 0 := rfl @[simp] lemma measure_univ_nnreal_eq_zero [finite_measure μ] : measure_univ_nnreal μ = 0 ↔ μ = 0 := begin rw [← measure_theory.measure.measure_univ_eq_zero, ← coe_measure_univ_nnreal], norm_cast end lemma measure_univ_nnreal_pos [finite_measure μ] (hμ : μ ≠ 0) : 0 < measure_univ_nnreal μ := begin contrapose! hμ, simpa [measure_univ_nnreal_eq_zero, le_zero_iff] using hμ end /-- `le_of_add_le_add_left` is normally applicable to `ordered_cancel_add_comm_monoid`, but it holds for measures with the additional assumption that μ is finite. -/ lemma measure.le_of_add_le_add_left {μ ν₁ ν₂ : measure α} [finite_measure μ] (A2 : μ + ν₁ ≤ μ + ν₂) : ν₁ ≤ ν₂ := λ S B1, ennreal.le_of_add_le_add_left (measure_theory.measure_lt_top μ S) (A2 S B1) lemma summable_measure_to_real [hμ : finite_measure μ] {f : ℕ → set α} (hf₁ : ∀ (i : ℕ), measurable_set (f i)) (hf₂ : pairwise (disjoint on f)) : summable (λ x, (μ (f x)).to_real) := begin apply ennreal.summable_to_real, rw ← measure_theory.measure_Union hf₂ hf₁, exact ne_of_lt (measure_lt_top _ _) end end finite_measure section probability_measure /-- A measure `μ` is called a probability measure if `μ univ = 1`. -/ class probability_measure (μ : measure α) : Prop := (measure_univ : μ univ = 1) export probability_measure (measure_univ) instance measure.dirac.probability_measure {x : α} : probability_measure (dirac x) := ⟨dirac_apply_of_mem $ mem_univ x⟩ @[priority 100] instance probability_measure.to_finite_measure (μ : measure α) [probability_measure μ] : finite_measure μ := ⟨by simp only [measure_univ, ennreal.one_lt_top]⟩ lemma probability_measure.ne_zero (μ : measure α) [probability_measure μ] : μ ≠ 0 := mt measure_univ_eq_zero.2 $ by simp [measure_univ] lemma prob_add_prob_compl [probability_measure μ] (h : measurable_set s) : μ s + μ sᶜ = 1 := (measure_add_measure_compl h).trans measure_univ lemma prob_le_one [probability_measure μ] : μ s ≤ 1 := (measure_mono $ set.subset_univ _).trans_eq measure_univ end probability_measure section no_atoms /-- Measure `μ` *has no atoms* if the measure of each singleton is zero. NB: Wikipedia assumes that for any measurable set `s` with positive `μ`-measure, there exists a measurable `t ⊆ s` such that `0 < μ t < μ s`. While this implies `μ {x} = 0`, the converse is not true. -/ class has_no_atoms (μ : measure α) : Prop := (measure_singleton : ∀ x, μ {x} = 0) export has_no_atoms (measure_singleton) attribute [simp] measure_singleton variables [has_no_atoms μ] lemma measure_subsingleton (hs : s.subsingleton) : μ s = 0 := hs.induction_on measure_empty measure_singleton alias measure_subsingleton ← set.subsingleton.measure_eq @[simp] lemma measure.restrict_singleton' {a : α} : μ.restrict {a} = 0 := by simp only [measure_singleton, measure.restrict_eq_zero] instance (s : set α) : has_no_atoms (μ.restrict s) := begin refine ⟨λ x, _⟩, obtain ⟨t, hxt, ht1, ht2⟩ := exists_measurable_superset_of_null (measure_singleton x : μ {x} = 0), apply measure_mono_null hxt, rw measure.restrict_apply ht1, apply measure_mono_null (inter_subset_left t s) ht2 end lemma _root_.set.countable.measure_zero (h : countable s) : μ s = 0 := begin rw [← bUnion_of_singleton s, ← nonpos_iff_eq_zero], refine le_trans (measure_bUnion_le h _) _, simp end lemma _root_.set.finite.measure_zero (h : s.finite) : μ s = 0 := h.countable.measure_zero lemma _root_.finset.measure_zero (s : finset α) : μ ↑s = 0 := s.finite_to_set.measure_zero lemma insert_ae_eq_self (a : α) (s : set α) : (insert a s : set α) =ᵐ[μ] s := union_ae_eq_right.2 $ measure_mono_null (diff_subset _ _) (measure_singleton _) variables [partial_order α] {a b : α} lemma Iio_ae_eq_Iic : Iio a =ᵐ[μ] Iic a := by simp only [← Iic_diff_right, diff_ae_eq_self, measure_mono_null (set.inter_subset_right _ _) (measure_singleton a)] lemma Ioi_ae_eq_Ici : Ioi a =ᵐ[μ] Ici a := @Iio_ae_eq_Iic (order_dual α) ‹_› ‹_› _ _ _ lemma Ioo_ae_eq_Ioc : Ioo a b =ᵐ[μ] Ioc a b := (ae_eq_refl _).inter Iio_ae_eq_Iic lemma Ioc_ae_eq_Icc : Ioc a b =ᵐ[μ] Icc a b := Ioi_ae_eq_Ici.inter (ae_eq_refl _) lemma Ioo_ae_eq_Ico : Ioo a b =ᵐ[μ] Ico a b := Ioi_ae_eq_Ici.inter (ae_eq_refl _) lemma Ioo_ae_eq_Icc : Ioo a b =ᵐ[μ] Icc a b := Ioi_ae_eq_Ici.inter Iio_ae_eq_Iic lemma Ico_ae_eq_Icc : Ico a b =ᵐ[μ] Icc a b := (ae_eq_refl _).inter Iio_ae_eq_Iic lemma Ico_ae_eq_Ioc : Ico a b =ᵐ[μ] Ioc a b := Ioo_ae_eq_Ico.symm.trans Ioo_ae_eq_Ioc end no_atoms lemma ite_ae_eq_of_measure_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ s = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] g := begin have h_ss : sᶜ ⊆ {a : α | ite (a ∈ s) (f a) (g a) = g a}, from λ x hx, by simp [(set.mem_compl_iff _ _).mp hx], refine measure_mono_null _ hs_zero, nth_rewrite 0 ←compl_compl s, rwa set.compl_subset_compl, end lemma ite_ae_eq_of_measure_compl_zero {γ} (f : α → γ) (g : α → γ) (s : set α) (hs_zero : μ sᶜ = 0) : (λ x, ite (x ∈ s) (f x) (g x)) =ᵐ[μ] f := by { filter_upwards [hs_zero], intros, split_ifs, refl } namespace measure /-- A measure is called finite at filter `f` if it is finite at some set `s ∈ f`. Equivalently, it is eventually finite at `s` in `f.lift' powerset`. -/ def finite_at_filter (μ : measure α) (f : filter α) : Prop := ∃ s ∈ f, μ s < ∞ lemma finite_at_filter_of_finite (μ : measure α) [finite_measure μ] (f : filter α) : μ.finite_at_filter f := ⟨univ, univ_mem_sets, measure_lt_top μ univ⟩ lemma finite_at_filter.exists_mem_basis {μ : measure α} {f : filter α} (hμ : finite_at_filter μ f) {p : ι → Prop} {s : ι → set α} (hf : f.has_basis p s) : ∃ i (hi : p i), μ (s i) < ∞ := (hf.exists_iff (λ s t hst ht, (measure_mono hst).trans_lt ht)).1 hμ lemma finite_at_bot (μ : measure α) : μ.finite_at_filter ⊥ := ⟨∅, mem_bot_sets, by simp only [measure_empty, with_top.zero_lt_top]⟩ /-- `μ` has finite spanning sets in `C` if there is a countable sequence of sets in `C` that have finite measures. This structure is a type, which is useful if we want to record extra properties about the sets, such as that they are monotone. `sigma_finite` is defined in terms of this: `μ` is σ-finite if there exists a sequence of finite spanning sets in the collection of all measurable sets. -/ @[protect_proj, nolint has_inhabited_instance] structure finite_spanning_sets_in (μ : measure α) (C : set (set α)) := (set : ℕ → set α) (set_mem : ∀ i, set i ∈ C) (finite : ∀ i, μ (set i) < ∞) (spanning : (⋃ i, set i) = univ) end measure open measure /-- A measure `μ` is called σ-finite if there is a countable collection of sets `{ A i | i ∈ ℕ }` such that `μ (A i) < ∞` and `⋃ i, A i = s`. -/ class sigma_finite (μ : measure α) : Prop := (out' : nonempty (μ.finite_spanning_sets_in {s | measurable_set s})) theorem sigma_finite_iff {μ : measure α} : sigma_finite μ ↔ nonempty (μ.finite_spanning_sets_in {s | measurable_set s}) := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem sigma_finite.out {μ : measure α} (h : sigma_finite μ) : nonempty (μ.finite_spanning_sets_in {s | measurable_set s}) := h.1 /-- If `μ` is σ-finite it has finite spanning sets in the collection of all measurable sets. -/ def measure.to_finite_spanning_sets_in (μ : measure α) [h : sigma_finite μ] : μ.finite_spanning_sets_in {s | measurable_set s} := classical.choice h.out /-- A noncomputable way to get a monotone collection of sets that span `univ` and have finite measure using `classical.some`. This definition satisfies monotonicity in addition to all other properties in `sigma_finite`. -/ def spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : set α := accumulate μ.to_finite_spanning_sets_in.set i lemma monotone_spanning_sets (μ : measure α) [sigma_finite μ] : monotone (spanning_sets μ) := monotone_accumulate lemma measurable_spanning_sets (μ : measure α) [sigma_finite μ] (i : ℕ) : measurable_set (spanning_sets μ i) := measurable_set.Union $ λ j, measurable_set.Union_Prop $ λ hij, μ.to_finite_spanning_sets_in.set_mem j lemma measure_spanning_sets_lt_top (μ : measure α) [sigma_finite μ] (i : ℕ) : μ (spanning_sets μ i) < ∞ := measure_bUnion_lt_top (finite_le_nat i) $ λ j _, μ.to_finite_spanning_sets_in.finite j lemma Union_spanning_sets (μ : measure α) [sigma_finite μ] : (⋃ i : ℕ, spanning_sets μ i) = univ := by simp_rw [spanning_sets, Union_accumulate, μ.to_finite_spanning_sets_in.spanning] lemma is_countably_spanning_spanning_sets (μ : measure α) [sigma_finite μ] : is_countably_spanning (range (spanning_sets μ)) := ⟨spanning_sets μ, mem_range_self, Union_spanning_sets μ⟩ namespace measure lemma supr_restrict_spanning_sets [sigma_finite μ] (hs : measurable_set s) : (⨆ i, μ.restrict (spanning_sets μ i) s) = μ s := begin convert (restrict_Union_apply_eq_supr (measurable_spanning_sets μ) _ hs).symm, { simp [Union_spanning_sets] }, { exact directed_of_sup (monotone_spanning_sets μ) } end namespace finite_spanning_sets_in variables {C D : set (set α)} /-- If `μ` has finite spanning sets in `C` and `C ⊆ D` then `μ` has finite spanning sets in `D`. -/ protected def mono (h : μ.finite_spanning_sets_in C) (hC : C ⊆ D) : μ.finite_spanning_sets_in D := ⟨h.set, λ i, hC (h.set_mem i), h.finite, h.spanning⟩ /-- If `μ` has finite spanning sets in the collection of measurable sets `C`, then `μ` is σ-finite. -/ protected lemma sigma_finite (h : μ.finite_spanning_sets_in C) (hC : ∀ s ∈ C, measurable_set s) : sigma_finite μ := ⟨⟨h.mono hC⟩⟩ /-- An extensionality for measures. It is `ext_of_generate_from_of_Union` formulated in terms of `finite_spanning_sets_in`. -/ protected lemma ext {ν : measure α} {C : set (set α)} (hA : ‹_› = generate_from C) (hC : is_pi_system C) (h : μ.finite_spanning_sets_in C) (h_eq : ∀ s ∈ C, μ s = ν s) : μ = ν := ext_of_generate_from_of_Union C _ hA hC h.spanning h.set_mem h.finite h_eq protected lemma is_countably_spanning (h : μ.finite_spanning_sets_in C) : is_countably_spanning C := ⟨_, h.set_mem, h.spanning⟩ end finite_spanning_sets_in lemma sigma_finite_of_not_nonempty (μ : measure α) (hα : ¬ nonempty α) : sigma_finite μ := ⟨⟨⟨λ _, ∅, λ n, measurable_set.empty, λ n, by simp, by simp [eq_empty_of_not_nonempty hα univ]⟩⟩⟩ lemma sigma_finite_of_countable {S : set (set α)} (hc : countable S) (hμ : ∀ s ∈ S, μ s < ∞) (hU : ⋃₀ S = univ) : sigma_finite μ := begin obtain ⟨s, hμ, hs⟩ : ∃ s : ℕ → set α, (∀ n, μ (s n) < ∞) ∧ (⋃ n, s n) = univ, from (exists_seq_cover_iff_countable ⟨∅, by simp⟩).2 ⟨S, hc, hμ, hU⟩, refine ⟨⟨⟨λ n, to_measurable μ (s n), λ n, measurable_set_to_measurable _ _, by simpa, _⟩⟩⟩, exact eq_univ_of_subset (Union_subset_Union $ λ n, subset_to_measurable μ (s n)) hs end end measure /-- Every finite measure is σ-finite. -/ @[priority 100] instance finite_measure.to_sigma_finite (μ : measure α) [finite_measure μ] : sigma_finite μ := ⟨⟨⟨λ _, univ, λ _, measurable_set.univ, λ _, measure_lt_top μ _, Union_const _⟩⟩⟩ instance restrict.sigma_finite (μ : measure α) [sigma_finite μ] (s : set α) : sigma_finite (μ.restrict s) := begin refine ⟨⟨⟨spanning_sets μ, measurable_spanning_sets μ, λ i, _, Union_spanning_sets μ⟩⟩⟩, rw [restrict_apply (measurable_spanning_sets μ i)], exact (measure_mono $ inter_subset_left _ _).trans_lt (measure_spanning_sets_lt_top μ i) end instance sum.sigma_finite {ι} [fintype ι] (μ : ι → measure α) [∀ i, sigma_finite (μ i)] : sigma_finite (sum μ) := begin haveI : encodable ι := fintype.encodable ι, have : ∀ n, measurable_set (⋂ (i : ι), spanning_sets (μ i) n) := λ n, measurable_set.Inter (λ i, measurable_spanning_sets (μ i) n), refine ⟨⟨⟨λ n, ⋂ i, spanning_sets (μ i) n, this, λ n, _, _⟩⟩⟩, { rw [sum_apply _ (this n), tsum_fintype, ennreal.sum_lt_top_iff], rintro i -, exact (measure_mono $ Inter_subset _ i).trans_lt (measure_spanning_sets_lt_top (μ i) n) }, { rw [Union_Inter_of_monotone], simp_rw [Union_spanning_sets, Inter_univ], exact λ i, monotone_spanning_sets (μ i), } end instance add.sigma_finite (μ ν : measure α) [sigma_finite μ] [sigma_finite ν] : sigma_finite (μ + ν) := by { rw [← sum_cond], refine @sum.sigma_finite _ _ _ _ _ (bool.rec _ _); simpa } lemma sigma_finite.of_map (μ : measure α) {f : α → β} (hf : measurable f) (h : sigma_finite (map f μ)) : sigma_finite μ := ⟨⟨⟨λ n, f ⁻¹' (spanning_sets (map f μ) n), λ n, hf $ measurable_spanning_sets _ _, λ n, by simp only [← map_apply hf, measurable_spanning_sets, measure_spanning_sets_lt_top], by rw [← preimage_Union, Union_spanning_sets, preimage_univ]⟩⟩⟩ /-- A measure is called locally finite if it is finite in some neighborhood of each point. -/ class locally_finite_measure [topological_space α] (μ : measure α) : Prop := (finite_at_nhds : ∀ x, μ.finite_at_filter (𝓝 x)) @[priority 100] -- see Note [lower instance priority] instance finite_measure.to_locally_finite_measure [topological_space α] (μ : measure α) [finite_measure μ] : locally_finite_measure μ := ⟨λ x, finite_at_filter_of_finite _ _⟩ lemma measure.finite_at_nhds [topological_space α] (μ : measure α) [locally_finite_measure μ] (x : α) : μ.finite_at_filter (𝓝 x) := locally_finite_measure.finite_at_nhds x lemma measure.smul_finite {α : Type*} [measurable_space α] (μ : measure α) [finite_measure μ] {c : ℝ≥0∞} (hc : c < ∞) : finite_measure (c • μ) := begin refine ⟨_⟩, rw measure.smul_apply, exact ennreal.mul_lt_top hc (measure_lt_top μ set.univ), end lemma measure.exists_is_open_measure_lt_top [topological_space α] (μ : measure α) [locally_finite_measure μ] (x : α) : ∃ s : set α, x ∈ s ∧ is_open s ∧ μ s < ∞ := by simpa only [exists_prop, and.assoc] using (μ.finite_at_nhds x).exists_mem_basis (nhds_basis_opens x) @[priority 100] -- see Note [lower instance priority] instance sigma_finite_of_locally_finite [topological_space α] [topological_space.second_countable_topology α] {μ : measure α} [locally_finite_measure μ] : sigma_finite μ := begin choose s hsx hsμ using μ.finite_at_nhds, rcases topological_space.countable_cover_nhds hsx with ⟨t, htc, htU⟩, refine measure.sigma_finite_of_countable (htc.image s) (ball_image_iff.2 $ λ x hx, hsμ x) _, rwa sUnion_image end /-- If two finite measures give the same mass to the whole space and coincide on a π-system made of measurable sets, then they coincide on all sets in the σ-algebra generated by the π-system. -/ lemma ext_on_measurable_space_of_generate_finite {α} (m₀ : measurable_space α) {μ ν : measure α} [finite_measure μ] (C : set (set α)) (hμν : ∀ s ∈ C, μ s = ν s) {m : measurable_space α} (h : m ≤ m₀) (hA : m = measurable_space.generate_from C) (hC : is_pi_system C) (h_univ : μ set.univ = ν set.univ) {s : set α} (hs : m.measurable_set' s) : μ s = ν s := begin haveI : @finite_measure _ m₀ ν := begin constructor, rw ← h_univ, apply finite_measure.measure_univ_lt_top, end, refine induction_on_inter hA hC (by simp) hμν _ _ hs, { intros t h1t h2t, have h1t_ : @measurable_set α m₀ t, from h _ h1t, rw [@measure_compl α m₀ μ t h1t_ (@measure_lt_top α m₀ μ _ t), @measure_compl α m₀ ν t h1t_ (@measure_lt_top α m₀ ν _ t), h_univ, h2t], }, { intros f h1f h2f h3f, have h2f_ : ∀ (i : ℕ), @measurable_set α m₀ (f i), from (λ i, h _ (h2f i)), have h_Union : @measurable_set α m₀ (⋃ (i : ℕ), f i),from @measurable_set.Union α ℕ m₀ _ f h2f_, simp [measure_Union, h_Union, h1f, h3f, h2f_], }, end /-- Two finite measures are equal if they are equal on the π-system generating the σ-algebra (and `univ`). -/ lemma ext_of_generate_finite (C : set (set α)) (hA : _inst_1 = generate_from C) (hC : is_pi_system C) {μ ν : measure α} [finite_measure μ] (hμν : ∀ s ∈ C, μ s = ν s) (h_univ : μ univ = ν univ) : μ = ν := measure.ext (λ s hs, ext_on_measurable_space_of_generate_finite _inst_1 C hμν (le_refl _inst_1) hA hC h_univ hs) namespace measure namespace finite_at_filter variables {f g : filter α} lemma filter_mono (h : f ≤ g) : μ.finite_at_filter g → μ.finite_at_filter f := λ ⟨s, hs, hμ⟩, ⟨s, h hs, hμ⟩ lemma inf_of_left (h : μ.finite_at_filter f) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_left lemma inf_of_right (h : μ.finite_at_filter g) : μ.finite_at_filter (f ⊓ g) := h.filter_mono inf_le_right @[simp] lemma inf_ae_iff : μ.finite_at_filter (f ⊓ μ.ae) ↔ μ.finite_at_filter f := begin refine ⟨_, λ h, h.filter_mono inf_le_left⟩, rintros ⟨s, ⟨t, ht, u, hu, hs⟩, hμ⟩, suffices : μ t ≤ μ s, from ⟨t, ht, this.trans_lt hμ⟩, exact measure_mono_ae (mem_sets_of_superset hu (λ x hu ht, hs ⟨ht, hu⟩)) end alias inf_ae_iff ↔ measure_theory.measure.finite_at_filter.of_inf_ae _ lemma filter_mono_ae (h : f ⊓ μ.ae ≤ g) (hg : μ.finite_at_filter g) : μ.finite_at_filter f := inf_ae_iff.1 (hg.filter_mono h) protected lemma measure_mono (h : μ ≤ ν) : ν.finite_at_filter f → μ.finite_at_filter f := λ ⟨s, hs, hν⟩, ⟨s, hs, (measure.le_iff'.1 h s).trans_lt hν⟩ @[mono] protected lemma mono (hf : f ≤ g) (hμ : μ ≤ ν) : ν.finite_at_filter g → μ.finite_at_filter f := λ h, (h.filter_mono hf).measure_mono hμ protected lemma eventually (h : μ.finite_at_filter f) : ∀ᶠ s in f.lift' powerset, μ s < ∞ := (eventually_lift'_powerset' $ λ s t hst ht, (measure_mono hst).trans_lt ht).2 h lemma filter_sup : μ.finite_at_filter f → μ.finite_at_filter g → μ.finite_at_filter (f ⊔ g) := λ ⟨s, hsf, hsμ⟩ ⟨t, htg, htμ⟩, ⟨s ∪ t, union_mem_sup hsf htg, (measure_union_le s t).trans_lt (ennreal.add_lt_top.2 ⟨hsμ, htμ⟩)⟩ end finite_at_filter lemma finite_at_nhds_within [topological_space α] (μ : measure α) [locally_finite_measure μ] (x : α) (s : set α) : μ.finite_at_filter (𝓝[s] x) := (finite_at_nhds μ x).inf_of_left @[simp] lemma finite_at_principal : μ.finite_at_filter (𝓟 s) ↔ μ s < ∞ := ⟨λ ⟨t, ht, hμ⟩, (measure_mono ht).trans_lt hμ, λ h, ⟨s, mem_principal_self s, h⟩⟩ /-! ### Subtraction of measures -/ /-- The measure `μ - ν` is defined to be the least measure `τ` such that `μ ≤ τ + ν`. It is the equivalent of `(μ - ν) ⊔ 0` if `μ` and `ν` were signed measures. Compare with `ennreal.has_sub`. Specifically, note that if you have `α = {1,2}`, and `μ {1} = 2`, `μ {2} = 0`, and `ν {2} = 2`, `ν {1} = 0`, then `(μ - ν) {1, 2} = 2`. However, if `μ ≤ ν`, and `ν univ ≠ ∞`, then `(μ - ν) + ν = μ`. -/ noncomputable instance has_sub {α : Type*} [measurable_space α] : has_sub (measure α) := ⟨λ μ ν, Inf {τ | μ ≤ τ + ν} ⟩ section measure_sub lemma sub_def : μ - ν = Inf {d | μ ≤ d + ν} := rfl lemma sub_eq_zero_of_le (h : μ ≤ ν) : μ - ν = 0 := begin rw [← nonpos_iff_eq_zero', measure.sub_def], apply @Inf_le (measure α) _ _, simp [h], end /-- This application lemma only works in special circumstances. Given knowledge of when `μ ≤ ν` and `ν ≤ μ`, a more general application lemma can be written. -/ lemma sub_apply [finite_measure ν] (h₁ : measurable_set s) (h₂ : ν ≤ μ) : (μ - ν) s = μ s - ν s := begin -- We begin by defining `measure_sub`, which will be equal to `(μ - ν)`. let measure_sub : measure α := @measure_theory.measure.of_measurable α _ (λ (t : set α) (h_t_measurable_set : measurable_set t), (μ t - ν t)) begin simp end begin intros g h_meas h_disj, simp only, rw ennreal.tsum_sub, repeat { rw ← measure_theory.measure_Union h_disj h_meas }, apply measure_theory.measure_lt_top, intro i, apply h₂, apply h_meas end, -- Now, we demonstrate `μ - ν = measure_sub`, and apply it. begin have h_measure_sub_add : (ν + measure_sub = μ), { ext t h_t_measurable_set, simp only [pi.add_apply, coe_add], rw [measure_theory.measure.of_measurable_apply _ h_t_measurable_set, add_comm, ennreal.sub_add_cancel_of_le (h₂ t h_t_measurable_set)] }, have h_measure_sub_eq : (μ - ν) = measure_sub, { rw measure_theory.measure.sub_def, apply le_antisymm, { apply @Inf_le (measure α) measure.complete_semilattice_Inf, simp [le_refl, add_comm, h_measure_sub_add] }, apply @le_Inf (measure α) measure.complete_semilattice_Inf, intros d h_d, rw [← h_measure_sub_add, mem_set_of_eq, add_comm d] at h_d, apply measure.le_of_add_le_add_left h_d }, rw h_measure_sub_eq, apply measure.of_measurable_apply _ h₁, end end lemma sub_add_cancel_of_le [finite_measure ν] (h₁ : ν ≤ μ) : μ - ν + ν = μ := begin ext s h_s_meas, rw [add_apply, sub_apply h_s_meas h₁, ennreal.sub_add_cancel_of_le (h₁ s h_s_meas)], end lemma sub_le : μ - ν ≤ μ := Inf_le (measure.le_add_right (le_refl _)) end measure_sub lemma restrict_sub_eq_restrict_sub_restrict (h_meas_s : measurable_set s) : (μ - ν).restrict s = (μ.restrict s) - (ν.restrict s) := begin repeat {rw sub_def}, have h_nonempty : {d | μ ≤ d + ν}.nonempty, { apply @set.nonempty_of_mem _ _ μ, rw mem_set_of_eq, intros t h_meas, exact le_self_add }, rw restrict_Inf_eq_Inf_restrict h_nonempty h_meas_s, apply le_antisymm, { apply @Inf_le_Inf_of_forall_exists_le (measure α) _, intros ν' h_ν'_in, rw mem_set_of_eq at h_ν'_in, apply exists.intro (ν'.restrict s), split, { rw mem_image, apply exists.intro (ν' + (⊤ : measure_theory.measure α).restrict sᶜ), rw mem_set_of_eq, split, { rw [add_assoc, add_comm _ ν, ← add_assoc, measure_theory.measure.le_iff], intros t h_meas_t, have h_inter_inter_eq_inter : ∀ t' : set α , t ∩ t' ∩ t' = t ∩ t', { intro t', rw set.inter_eq_self_of_subset_left, apply set.inter_subset_right t t' }, have h_meas_t_inter_s : measurable_set (t ∩ s) := h_meas_t.inter h_meas_s, repeat {rw measure_eq_inter_diff h_meas_t h_meas_s, rw set.diff_eq}, refine add_le_add _ _, { rw add_apply, apply le_add_right _, rw add_apply, rw ← @restrict_eq_self _ _ μ s _ h_meas_t_inter_s (set.inter_subset_right _ _), rw ← @restrict_eq_self _ _ ν s _ h_meas_t_inter_s (set.inter_subset_right _ _), apply h_ν'_in _ h_meas_t_inter_s }, cases (@set.eq_empty_or_nonempty _ (t ∩ sᶜ)) with h_inter_empty h_inter_nonempty, { simp [h_inter_empty] }, { rw add_apply, have h_meas_inter_compl := h_meas_t.inter (measurable_set.compl h_meas_s), rw [restrict_apply h_meas_inter_compl, h_inter_inter_eq_inter sᶜ], have h_mu_le_add_top : μ ≤ ν' + ν + ⊤, { rw add_comm, have h_le_top : μ ≤ ⊤ := le_top, apply (λ t₂ h_meas, le_add_right (h_le_top t₂ h_meas)) }, apply h_mu_le_add_top _ h_meas_inter_compl } }, { ext1 t h_meas_t, simp [restrict_apply h_meas_t, restrict_apply (h_meas_t.inter h_meas_s), set.inter_assoc] } }, { apply restrict_le_self } }, { apply @Inf_le_Inf_of_forall_exists_le (measure α) _, intros s h_s_in, cases h_s_in with t h_t, cases h_t with h_t_in h_t_eq, subst s, apply exists.intro (t.restrict s), split, { rw [set.mem_set_of_eq, ← restrict_add], apply restrict_mono (set.subset.refl _) h_t_in }, { apply le_refl _ } }, end lemma sub_apply_eq_zero_of_restrict_le_restrict (h_le : μ.restrict s ≤ ν.restrict s) (h_meas_s : measurable_set s) : (μ - ν) s = 0 := begin rw [← restrict_apply_self _ h_meas_s, restrict_sub_eq_restrict_sub_restrict, sub_eq_zero_of_le], repeat {simp [*]}, end instance finite_measure_sub [finite_measure μ] : finite_measure (μ - ν) := { measure_univ_lt_top := lt_of_le_of_lt (measure.sub_le set.univ measurable_set.univ) (measure_lt_top _ _) } end measure end measure_theory open measure_theory measure_theory.measure namespace measurable_equiv /-! Interactions of measurable equivalences and measures -/ open equiv measure_theory.measure variables [measurable_space α] [measurable_space β] {μ : measure α} {ν : measure β} /-- If we map a measure along a measurable equivalence, we can compute the measure on all sets (not just the measurable ones). -/ protected theorem map_apply (f : α ≃ᵐ β) (s : set β) : map f μ s = μ (f ⁻¹' s) := begin refine le_antisymm _ (le_map_apply f.measurable s), rw [measure_eq_infi' μ], refine le_infi _, rintro ⟨t, hst, ht⟩, rw [subtype.coe_mk], have := f.symm.to_equiv.image_eq_preimage, simp only [←coe_eq, symm_symm, symm_to_equiv] at this, rw [← this, image_subset_iff] at hst, convert measure_mono hst, rw [map_apply, preimage_preimage], { refine congr_arg μ (eq.symm _), convert preimage_id, exact funext f.left_inv }, exacts [f.measurable, f.measurable_inv_fun ht] end @[simp] lemma map_symm_map (e : α ≃ᵐ β) : map e.symm (map e μ) = μ := by simp [map_map e.symm.measurable e.measurable] @[simp] lemma map_map_symm (e : α ≃ᵐ β) : map e (map e.symm ν) = ν := by simp [map_map e.measurable e.symm.measurable] lemma map_measurable_equiv_injective (e : α ≃ᵐ β) : injective (map e) := by { intros μ₁ μ₂ hμ, apply_fun map e.symm at hμ, simpa [map_symm_map e] using hμ } lemma map_apply_eq_iff_map_symm_apply_eq (e : α ≃ᵐ β) : map e μ = ν ↔ map e.symm ν = μ := by rw [← (map_measurable_equiv_injective e).eq_iff, map_map_symm, eq_comm] end measurable_equiv section is_complete /-- A measure is complete if every null set is also measurable. A null set is a subset of a measurable set with measure `0`. Since every measure is defined as a special case of an outer measure, we can more simply state that a set `s` is null if `μ s = 0`. -/ class measure_theory.measure.is_complete {_ : measurable_space α} (μ : measure α) : Prop := (out' : ∀ s, μ s = 0 → measurable_set s) theorem measure_theory.measure.is_complete_iff {_ : measurable_space α} {μ : measure α} : μ.is_complete ↔ ∀ s, μ s = 0 → measurable_set s := ⟨λ h, h.1, λ h, ⟨h⟩⟩ theorem measure_theory.measure.is_complete.out {_ : measurable_space α} {μ : measure α} (h : μ.is_complete) : ∀ s, μ s = 0 → measurable_set s := h.1 variables [measurable_space α] {μ : measure α} {s t z : set α} /-- A set is null measurable if it is the union of a null set and a measurable set. -/ def null_measurable_set (μ : measure α) (s : set α) : Prop := ∃ t z, s = t ∪ z ∧ measurable_set t ∧ μ z = 0 theorem null_measurable_set_iff : null_measurable_set μ s ↔ ∃ t, t ⊆ s ∧ measurable_set t ∧ μ (s \ t) = 0 := begin split, { rintro ⟨t, z, rfl, ht, hz⟩, refine ⟨t, set.subset_union_left _ _, ht, measure_mono_null _ hz⟩, simp [union_diff_left, diff_subset] }, { rintro ⟨t, st, ht, hz⟩, exact ⟨t, _, (union_diff_cancel st).symm, ht, hz⟩ } end theorem null_measurable_set_measure_eq (st : t ⊆ s) (hz : μ (s \ t) = 0) : μ s = μ t := begin refine le_antisymm _ (measure_mono st), have := measure_union_le t (s \ t), rw [union_diff_cancel st, hz] at this, simpa end theorem measurable_set.null_measurable_set (μ : measure α) (hs : measurable_set s) : null_measurable_set μ s := ⟨s, ∅, by simp, hs, μ.empty⟩ theorem null_measurable_set_of_complete (μ : measure α) [c : μ.is_complete] : null_measurable_set μ s ↔ measurable_set s := ⟨by rintro ⟨t, z, rfl, ht, hz⟩; exact measurable_set.union ht (c.out _ hz), λ h, h.null_measurable_set _⟩ theorem null_measurable_set.union_null (hs : null_measurable_set μ s) (hz : μ z = 0) : null_measurable_set μ (s ∪ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, exact ⟨t, z' ∪ z, set.union_assoc _ _ _, ht, nonpos_iff_eq_zero.1 (le_trans (measure_union_le _ _) $ by simp [hz, hz'])⟩ end theorem null_null_measurable_set (hz : μ z = 0) : null_measurable_set μ z := by simpa using (measurable_set.empty.null_measurable_set _).union_null hz theorem null_measurable_set.Union_nat {s : ℕ → set α} (hs : ∀ i, null_measurable_set μ (s i)) : null_measurable_set μ (Union s) := begin choose t ht using assume i, null_measurable_set_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, refine null_measurable_set_iff.2 ⟨Union t, Union_subset_Union st, measurable_set.Union ht, measure_mono_null _ (measure_Union_null hz)⟩, rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) end theorem measurable_set.diff_null (hs : measurable_set s) (hz : μ z = 0) : null_measurable_set μ (s \ z) := begin rw measure_eq_infi at hz, choose f hf using show ∀ q : {q : ℚ // q > 0}, ∃ t : set α, z ⊆ t ∧ measurable_set t ∧ μ t < (real.to_nnreal q.1 : ℝ≥0∞), { rintro ⟨ε, ε0⟩, have : 0 < (real.to_nnreal ε : ℝ≥0∞), { simpa using ε0 }, rw ← hz at this, simpa [infi_lt_iff] }, refine null_measurable_set_iff.2 ⟨s \ Inter f, diff_subset_diff_right (subset_Inter (λ i, (hf i).1)), hs.diff (measurable_set.Inter (λ i, (hf i).2.1)), measure_mono_null _ (nonpos_iff_eq_zero.1 $ le_of_not_lt $ λ h, _)⟩, { exact Inter f }, { rw [diff_subset_iff, diff_union_self], exact subset.trans (diff_subset _ _) (subset_union_left _ _) }, rcases ennreal.lt_iff_exists_rat_btwn.1 h with ⟨ε, ε0', ε0, h⟩, simp at ε0, apply not_le_of_lt (lt_trans (hf ⟨ε, ε0⟩).2.2 h), exact measure_mono (Inter_subset _ _) end theorem null_measurable_set.diff_null (hs : null_measurable_set μ s) (hz : μ z = 0) : null_measurable_set μ (s \ z) := begin rcases hs with ⟨t, z', rfl, ht, hz'⟩, rw [set.union_diff_distrib], exact (ht.diff_null hz).union_null (measure_mono_null (diff_subset _ _) hz') end theorem null_measurable_set.compl (hs : null_measurable_set μ s) : null_measurable_set μ sᶜ := begin rcases hs with ⟨t, z, rfl, ht, hz⟩, rw compl_union, exact ht.compl.diff_null hz end theorem null_measurable_set_iff_ae {s : set α} : null_measurable_set μ s ↔ ∃ t, measurable_set t ∧ s =ᵐ[μ] t := begin simp only [ae_eq_set], split, { assume h, rcases null_measurable_set_iff.1 h with ⟨t, ts, tmeas, ht⟩, refine ⟨t, tmeas, ht, _⟩, rw [diff_eq_empty.2 ts, measure_empty] }, { rintros ⟨t, tmeas, h₁, h₂⟩, have : null_measurable_set μ (t ∪ (s \ t)) := null_measurable_set.union_null (tmeas.null_measurable_set _) h₁, have A : null_measurable_set μ ((t ∪ (s \ t)) \ (t \ s)) := null_measurable_set.diff_null this h₂, have : (t ∪ (s \ t)) \ (t \ s) = s, { apply subset.antisymm, { assume x hx, simp only [mem_union_eq, not_and, mem_diff, not_not_mem] at hx, cases hx.1, { exact hx.2 h }, { exact h.1 } }, { assume x hx, simp [hx, classical.em (x ∈ t)] } }, rwa this at A } end theorem null_measurable_set_iff_sandwich {s : set α} : null_measurable_set μ s ↔ ∃ (t u : set α), measurable_set t ∧ measurable_set u ∧ t ⊆ s ∧ s ⊆ u ∧ μ (u \ t) = 0 := begin split, { assume h, rcases null_measurable_set_iff.1 h with ⟨t, ts, tmeas, ht⟩, rcases null_measurable_set_iff.1 h.compl with ⟨u', u's, u'meas, hu'⟩, have A : s ⊆ u'ᶜ := subset_compl_comm.mp u's, refine ⟨t, u'ᶜ, tmeas, u'meas.compl, ts, A, _⟩, have : sᶜ \ u' = u'ᶜ \ s, by simp [compl_eq_univ_diff, diff_diff, union_comm], rw this at hu', apply le_antisymm _ bot_le, calc μ (u'ᶜ \ t) ≤ μ ((u'ᶜ \ s) ∪ (s \ t)) : begin apply measure_mono, assume x hx, simp at hx, simp [hx, or_comm, classical.em], end ... ≤ μ (u'ᶜ \ s) + μ (s \ t) : measure_union_le _ _ ... = 0 : by rw [ht, hu', zero_add] }, { rintros ⟨t, u, tmeas, umeas, ts, su, hμ⟩, refine null_measurable_set_iff.2 ⟨t, ts, tmeas, _⟩, apply le_antisymm _ bot_le, calc μ (s \ t) ≤ μ (u \ t) : measure_mono (diff_subset_diff_left su) ... = 0 : hμ } end lemma restrict_apply_of_null_measurable_set {s t : set α} (ht : null_measurable_set (μ.restrict s) t) : μ.restrict s t = μ (t ∩ s) := begin rcases null_measurable_set_iff_sandwich.1 ht with ⟨u, v, umeas, vmeas, ut, tv, huv⟩, apply le_antisymm _ (le_restrict_apply _ _), calc μ.restrict s t ≤ μ.restrict s v : measure_mono tv ... = μ (v ∩ s) : restrict_apply vmeas ... ≤ μ ((u ∩ s) ∪ ((v \ u) ∩ s)) : measure_mono $ by { assume x hx, simp at hx, simp [hx, classical.em] } ... ≤ μ (u ∩ s) + μ ((v \ u) ∩ s) : measure_union_le _ _ ... = μ (u ∩ s) + μ.restrict s (v \ u) : by rw measure.restrict_apply (vmeas.diff umeas) ... = μ (u ∩ s) : by rw [huv, add_zero] ... ≤ μ (t ∩ s) : measure_mono $ inter_subset_inter_left s ut end /-- The measurable space of all null measurable sets. -/ def null_measurable (μ : measure α) : measurable_space α := { measurable_set' := null_measurable_set μ, measurable_set_empty := measurable_set.empty.null_measurable_set _, measurable_set_compl := λ s hs, hs.compl, measurable_set_Union := λ f, null_measurable_set.Union_nat } /-- Given a measure we can complete it to a (complete) measure on all null measurable sets. -/ def completion (μ : measure α) : @measure_theory.measure α (null_measurable μ) := { to_outer_measure := μ.to_outer_measure, m_Union := λ s hs hd, show μ (Union s) = ∑' i, μ (s i), begin choose t ht using assume i, null_measurable_set_iff.1 (hs i), simp [forall_and_distrib] at ht, rcases ht with ⟨st, ht, hz⟩, rw null_measurable_set_measure_eq (Union_subset_Union st), { rw measure_Union _ ht, { congr, funext i, exact (null_measurable_set_measure_eq (st i) (hz i)).symm }, { rintro i j ij x ⟨h₁, h₂⟩, exact hd i j ij ⟨st i h₁, st j h₂⟩ } }, { refine measure_mono_null _ (measure_Union_null hz), rw [diff_subset_iff, ← Union_union_distrib], exact Union_subset_Union (λ i, by rw ← diff_subset_iff) } end, trimmed := begin letI := null_measurable μ, refine le_antisymm (λ s, _) (outer_measure.le_trim _), rw outer_measure.trim_eq_infi, dsimp, clear _inst, resetI, rw measure_eq_infi s, exact infi_le_infi (λ t, infi_le_infi $ λ st, infi_le_infi2 $ λ ht, ⟨ht.null_measurable_set _, le_refl _⟩) end } instance completion.is_complete (μ : measure α) : (completion μ).is_complete := ⟨λ z hz, null_null_measurable_set hz⟩ lemma measurable.ae_eq {α β} [measurable_space α] [measurable_space β] {μ : measure α} [hμ : μ.is_complete] {f g : α → β} (hf : measurable f) (hfg : f =ᵐ[μ] g) : measurable g := begin intros s hs, let t := {x | f x = g x}, have ht_compl : μ tᶜ = 0, by rwa [filter.eventually_eq, ae_iff] at hfg, rw (set.inter_union_compl (g ⁻¹' s) t).symm, refine measurable_set.union _ _, { have h_g_to_f : (g ⁻¹' s) ∩ t = (f ⁻¹' s) ∩ t, { ext, simp only [set.mem_inter_iff, set.mem_preimage, and.congr_left_iff, set.mem_set_of_eq], exact λ hx, by rw hx, }, rw h_g_to_f, exact measurable_set.inter (hf hs) (measurable_set.compl_iff.mp (hμ.out tᶜ ht_compl)), }, { exact hμ.out (g ⁻¹' s ∩ tᶜ) (measure_mono_null (set.inter_subset_right _ _) ht_compl), }, end end is_complete namespace measure_theory lemma outer_measure.to_measure_zero [measurable_space α] : (0 : outer_measure α).to_measure ((le_top).trans outer_measure.zero_caratheodory.symm.le) = 0 := by rw [← measure.measure_univ_eq_zero, to_measure_apply _ _ measurable_set.univ, outer_measure.coe_zero, pi.zero_apply] section trim /-- Restriction of a measure to a sub-sigma algebra. It is common to see a measure `μ` on a measurable space structure `m0` as being also a measure on any `m ≤ m0`. Since measures in mathlib have to be trimmed to the measurable space, `μ` itself cannot be a measure on `m`, hence the definition of `μ.trim hm`. This notion is related to `outer_measure.trim`, see the lemma `to_outer_measure_trim_eq_trim_to_outer_measure`. -/ def measure.trim {m m0 : measurable_space α} (μ : @measure α m0) (hm : m ≤ m0) : @measure α m := @outer_measure.to_measure α m μ.to_outer_measure (hm.trans (le_to_outer_measure_caratheodory μ)) @[simp] lemma trim_eq_self [measurable_space α] {μ : measure α} : μ.trim le_rfl = μ := by simp [measure.trim] variables {m m0 : measurable_space α} {μ : measure α} {s : set α} lemma to_outer_measure_trim_eq_trim_to_outer_measure (μ : measure α) (hm : m ≤ m0) : @measure.to_outer_measure _ m (μ.trim hm) = @outer_measure.trim _ m μ.to_outer_measure := by rw [measure.trim, to_measure_to_outer_measure] @[simp] lemma zero_trim (hm : m ≤ m0) : (0 : measure α).trim hm = (0 : @measure α m) := by simp [measure.trim, outer_measure.to_measure_zero] lemma trim_measurable_set_eq (hm : m ≤ m0) (hs : @measurable_set α m s) : μ.trim hm s = μ s := by simp [measure.trim, hs] lemma le_trim (hm : m ≤ m0) : μ s ≤ μ.trim hm s := by { simp_rw [measure.trim], exact (@le_to_measure_apply _ m _ _ _), } lemma measure_eq_zero_of_trim_eq_zero (hm : m ≤ m0) (h : μ.trim hm s = 0) : μ s = 0 := le_antisymm ((le_trim hm).trans (le_of_eq h)) (zero_le _) lemma measure_trim_to_measurable_eq_zero {hm : m ≤ m0} (hs : μ.trim hm s = 0) : μ (@to_measurable α m (μ.trim hm) s) = 0 := measure_eq_zero_of_trim_eq_zero hm (by rwa measure_to_measurable) lemma ae_eq_of_ae_eq_trim {E} {hm : m ≤ m0} {f₁ f₂ : α → E} (h12 : f₁ =ᶠ[@measure.ae α m (μ.trim hm)] f₂) : f₁ =ᵐ[μ] f₂ := measure_eq_zero_of_trim_eq_zero hm h12 lemma restrict_trim (hm : m ≤ m0) (μ : measure α) (hs : @measurable_set α m s) : @measure.restrict α m (μ.trim hm) s = (μ.restrict s).trim hm := begin ext1 t ht, rw [@measure.restrict_apply α m _ _ _ ht, trim_measurable_set_eq hm ht, measure.restrict_apply (hm t ht), trim_measurable_set_eq hm (@measurable_set.inter α m t s ht hs)], end instance finite_measure_trim (hm : m ≤ m0) [finite_measure μ] : @finite_measure α m (μ.trim hm) := { measure_univ_lt_top := by { rw trim_measurable_set_eq hm (@measurable_set.univ _ m), exact measure_lt_top _ _, } } end trim end measure_theory /-! # Almost everywhere measurable functions A function is almost everywhere measurable if it coincides almost everywhere with a measurable function. This property, called `ae_measurable f μ`, is defined in the file `measure_space_def`. We discuss several of its properties that are analogous to properties of measurable functions. -/ section open measure_theory variables [measurable_space α] [measurable_space β] {f g : α → β} {μ ν : measure α} @[nontriviality, measurability] lemma subsingleton.ae_measurable [subsingleton α] : ae_measurable f μ := subsingleton.measurable.ae_measurable @[simp, measurability] lemma ae_measurable_zero_measure : ae_measurable f 0 := begin nontriviality α, inhabit α, exact ⟨λ x, f (default α), measurable_const, rfl⟩ end lemma ae_measurable_iff_measurable [μ.is_complete] : ae_measurable f μ ↔ measurable f := begin split; intro h, { rcases h with ⟨g, hg_meas, hfg⟩, exact hg_meas.ae_eq hfg.symm, }, { exact h.ae_measurable, }, end namespace ae_measurable lemma mono_measure (h : ae_measurable f μ) (h' : ν ≤ μ) : ae_measurable f ν := ⟨h.mk f, h.measurable_mk, eventually.filter_mono (ae_mono h') h.ae_eq_mk⟩ lemma mono_set {s t} (h : s ⊆ t) (ht : ae_measurable f (μ.restrict t)) : ae_measurable f (μ.restrict s) := ht.mono_measure (restrict_mono h le_rfl) protected lemma mono' (h : ae_measurable f μ) (h' : ν ≪ μ) : ae_measurable f ν := ⟨h.mk f, h.measurable_mk, h' h.ae_eq_mk⟩ lemma ae_mem_imp_eq_mk {s} (h : ae_measurable f (μ.restrict s)) : ∀ᵐ x ∂μ, x ∈ s → f x = h.mk f x := ae_imp_of_ae_restrict h.ae_eq_mk lemma ae_inf_principal_eq_mk {s} (h : ae_measurable f (μ.restrict s)) : f =ᶠ[μ.ae ⊓ 𝓟 s] h.mk f := le_ae_restrict h.ae_eq_mk @[measurability] lemma add_measure {f : α → β} (hμ : ae_measurable f μ) (hν : ae_measurable f ν) : ae_measurable f (μ + ν) := begin let s := {x | f x ≠ hμ.mk f x}, have : μ s = 0 := hμ.ae_eq_mk, obtain ⟨t, st, t_meas, μt⟩ : ∃ t, s ⊆ t ∧ measurable_set t ∧ μ t = 0 := exists_measurable_superset_of_null this, let g : α → β := t.piecewise (hν.mk f) (hμ.mk f), refine ⟨g, measurable.piecewise t_meas hν.measurable_mk hμ.measurable_mk, _⟩, change μ {x | f x ≠ g x} + ν {x | f x ≠ g x} = 0, suffices : μ {x | f x ≠ g x} = 0 ∧ ν {x | f x ≠ g x} = 0, by simp [this.1, this.2], have ht : {x | f x ≠ g x} ⊆ t, { assume x hx, by_contra h, simp only [g, h, mem_set_of_eq, ne.def, not_false_iff, piecewise_eq_of_not_mem] at hx, exact h (st hx) }, split, { have : μ {x | f x ≠ g x} ≤ μ t := measure_mono ht, rw μt at this, exact le_antisymm this bot_le }, { have : {x | f x ≠ g x} ⊆ {x | f x ≠ hν.mk f x}, { assume x hx, simpa [ht hx, g] using hx }, apply le_antisymm _ bot_le, calc ν {x | f x ≠ g x} ≤ ν {x | f x ≠ hν.mk f x} : measure_mono this ... = 0 : hν.ae_eq_mk } end @[measurability] lemma smul_measure (h : ae_measurable f μ) (c : ℝ≥0∞) : ae_measurable f (c • μ) := ⟨h.mk f, h.measurable_mk, ae_smul_measure h.ae_eq_mk c⟩ lemma comp_measurable [measurable_space δ] {f : α → δ} {g : δ → β} (hg : ae_measurable g (map f μ)) (hf : measurable f) : ae_measurable (g ∘ f) μ := ⟨hg.mk g ∘ f, hg.measurable_mk.comp hf, ae_eq_comp hf hg.ae_eq_mk⟩ lemma comp_measurable' {δ} [measurable_space δ] {ν : measure δ} {f : α → δ} {g : δ → β} (hg : ae_measurable g ν) (hf : measurable f) (h : map f μ ≪ ν) : ae_measurable (g ∘ f) μ := (hg.mono' h).comp_measurable hf @[measurability] lemma prod_mk {γ : Type*} [measurable_space γ] {f : α → β} {g : α → γ} (hf : ae_measurable f μ) (hg : ae_measurable g μ) : ae_measurable (λ x, (f x, g x)) μ := ⟨λ a, (hf.mk f a, hg.mk g a), hf.measurable_mk.prod_mk hg.measurable_mk, eventually_eq.prod_mk hf.ae_eq_mk hg.ae_eq_mk⟩ protected lemma null_measurable_set (h : ae_measurable f μ) {s : set β} (hs : measurable_set s) : null_measurable_set μ (f ⁻¹' s) := begin apply null_measurable_set_iff_ae.2, refine ⟨(h.mk f) ⁻¹' s, h.measurable_mk hs, _⟩, filter_upwards [h.ae_eq_mk], assume x hx, change (f x ∈ s) = ((h.mk f) x ∈ s), rwa hx end end ae_measurable @[simp] lemma ae_measurable_add_measure_iff : ae_measurable f (μ + ν) ↔ ae_measurable f μ ∧ ae_measurable f ν := ⟨λ h, ⟨h.mono_measure (measure.le_add_right (le_refl _)), h.mono_measure (measure.le_add_left (le_refl _))⟩, λ h, h.1.add_measure h.2⟩ @[simp, to_additive] lemma ae_measurable_one [has_one β] : ae_measurable (λ a : α, (1 : β)) μ := measurable_one.ae_measurable @[simp] lemma ae_measurable_smul_measure_iff {c : ℝ≥0∞} (hc : c ≠ 0) : ae_measurable f (c • μ) ↔ ae_measurable f μ := ⟨λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).1 h.ae_eq_mk⟩, λ h, ⟨h.mk f, h.measurable_mk, (ae_smul_measure_iff hc).2 h.ae_eq_mk⟩⟩ lemma ae_measurable_of_ae_measurable_trim {α} {m m0 : measurable_space α} {μ : measure α} (hm : m ≤ m0) {f : α → β} (hf : @ae_measurable _ _ m _ f (μ.trim hm)) : ae_measurable f μ := begin let f' := @ae_measurable.mk _ _ m _ _ _ hf, have hf'_meas : @measurable _ _ m _ f', from @ae_measurable.measurable_mk _ _ m _ _ _ hf, have hff'_m : f' =ᶠ[@measure.ae _ m (μ.trim hm)] f, from (@ae_measurable.ae_eq_mk _ _ m _ _ _ hf).symm, have hff' : f' =ᵐ[μ] f, from ae_eq_of_ae_eq_trim hff'_m, exact ⟨f', measurable.mono hf'_meas hm le_rfl, hff'.symm⟩, end lemma ae_measurable_restrict_of_measurable_subtype {s : set α} (hs : measurable_set s) (hf : measurable (λ x : s, f x)) : ae_measurable f (μ.restrict s) := begin by_cases h : nonempty β, { refine ⟨s.piecewise f (λ x, classical.choice h), _, (ae_restrict_iff' hs).mpr $ ae_of_all _ (λ x hx, (piecewise_eq_of_mem s _ _ hx).symm)⟩, intros t ht, rw piecewise_preimage, refine measurable_set.union _ ((measurable_const ht).diff hs), rw [← subtype.image_preimage_coe, ← preimage_comp], exact hs.subtype_image (hf ht) }, { exact (measurable_of_not_nonempty (mt (nonempty.map f) h) f).ae_measurable } end end namespace is_compact variables [topological_space α] [measurable_space α] {μ : measure α} {s : set α} lemma finite_measure_of_nhds_within (hs : is_compact s) : (∀ a ∈ s, μ.finite_at_filter (𝓝[s] a)) → μ s < ∞ := by simpa only [← measure.compl_mem_cofinite, measure.finite_at_filter] using hs.compl_mem_sets_of_nhds_within lemma finite_measure [locally_finite_measure μ] (hs : is_compact s) : μ s < ∞ := hs.finite_measure_of_nhds_within $ λ a ha, μ.finite_at_nhds_within _ _ lemma measure_zero_of_nhds_within (hs : is_compact s) : (∀ a ∈ s, ∃ t ∈ 𝓝[s] a, μ t = 0) → μ s = 0 := by simpa only [← compl_mem_ae_iff] using hs.compl_mem_sets_of_nhds_within end is_compact lemma metric.bounded.finite_measure [metric_space α] [proper_space α] [measurable_space α] {μ : measure α} [locally_finite_measure μ] {s : set α} (hs : metric.bounded s) : μ s < ∞ := (measure_mono subset_closure).trans_lt (metric.compact_iff_closed_bounded.2 ⟨is_closed_closure, metric.bounded_closure_of_bounded hs⟩).finite_measure section piecewise variables [measurable_space α] {μ : measure α} {s t : set α} {f g : α → β} lemma piecewise_ae_eq_restrict (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict s] f := begin rw [ae_restrict_eq hs], exact (piecewise_eq_on s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_restrict_compl (hs : measurable_set s) : piecewise s f g =ᵐ[μ.restrict sᶜ] g := begin rw [ae_restrict_eq hs.compl], exact (piecewise_eq_on_compl s f g).eventually_eq.filter_mono inf_le_right end lemma piecewise_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.piecewise f g =ᵐ[μ] t.piecewise f g := begin filter_upwards [hst], intros x hx, replace hx : x ∈ s ↔ x ∈ t := iff_of_eq hx, by_cases h : x ∈ s; have h' := h; rw hx at h'; simp [h, h'] end end piecewise section indicator_function variables [measurable_space α] {μ : measure α} {s t : set α} {f : α → β} lemma ae_measurable.restrict [measurable_space β] (hfm : ae_measurable f μ) {s} : ae_measurable f (μ.restrict s) := ⟨ae_measurable.mk f hfm, hfm.measurable_mk, ae_restrict_of_ae hfm.ae_eq_mk⟩ variables [has_zero β] lemma indicator_ae_eq_restrict (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict s] f := piecewise_ae_eq_restrict hs lemma indicator_ae_eq_restrict_compl (hs : measurable_set s) : indicator s f =ᵐ[μ.restrict sᶜ] 0 := piecewise_ae_eq_restrict_compl hs lemma indicator_ae_eq_of_ae_eq_set (hst : s =ᵐ[μ] t) : s.indicator f =ᵐ[μ] t.indicator f := piecewise_ae_eq_of_ae_eq_set hst variables [measurable_space β] lemma ae_measurable_indicator_iff {s} (hs : measurable_set s) : ae_measurable (indicator s f) μ ↔ ae_measurable f (μ.restrict s) := begin split, { assume h, exact (h.mono_measure measure.restrict_le_self).congr (indicator_ae_eq_restrict hs) }, { assume h, refine ⟨indicator s (h.mk f), h.measurable_mk.indicator hs, _⟩, have A : s.indicator f =ᵐ[μ.restrict s] s.indicator (ae_measurable.mk f h) := (indicator_ae_eq_restrict hs).trans (h.ae_eq_mk.trans $ (indicator_ae_eq_restrict hs).symm), have B : s.indicator f =ᵐ[μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) := (indicator_ae_eq_restrict_compl hs).trans (indicator_ae_eq_restrict_compl hs).symm, have : s.indicator f =ᵐ[μ.restrict s + μ.restrict sᶜ] s.indicator (ae_measurable.mk f h) := ae_add_measure_iff.2 ⟨A, B⟩, simpa only [hs, measure.restrict_add_restrict_compl] using this }, end lemma ae_measurable.indicator (hfm : ae_measurable f μ) {s} (hs : measurable_set s) : ae_measurable (s.indicator f) μ := (ae_measurable_indicator_iff hs).mpr hfm.restrict end indicator_function
c098690102bb570d96269dbe935c3602d2655798
5c4b17dae42fab1d4f493f3b52977bffa54fefea
/3.proofs.lean
092f1467e141a572364b76a4d0e46154d26e5138
[]
no_license
hyponymous/theorem-proving-in-lean-solutions
9214cb45cc87347862fd17dfdea79fdf24b9df92
a95320ae81c90c1b15da04574602cd378794400d
refs/heads/master
1,585,777,733,214
1,541,039,359,000
1,541,039,359,000
153,676,525
2
0
null
null
null
null
UTF-8
Lean
false
false
9,176
lean
variables p q r s : Prop -- commutativity of ∧ and ∨ example : p ∧ q ↔ q ∧ p := iff.intro (assume h : p ∧ q, show q ∧ p, from ⟨(and.right h), (and.left h)⟩) (assume h : q ∧ p, show p ∧ q, from ⟨(and.right h), (and.left h)⟩) example : p ∨ q ↔ q ∨ p := iff.intro (assume h : p ∨ q, or.elim h (assume hp : p, show q ∨ p, from or.inr hp) (assume hq : q, show q ∨ p, from or.inl hq)) (assume h : q ∨ p, or.elim h (assume hq : q, show p ∨ q, from or.inr hq) (assume hp : p, show p ∨ q, from or.inl hp)) -- associativity of ∧ and ∨ example : (p ∧ q) ∧ r ↔ p ∧ (q ∧ r) := iff.intro (assume h : (p ∧ q) ∧ r, have hpq : p ∧ q, from and.left h, show p ∧ (q ∧ r), from ⟨and.left hpq, ⟨and.right hpq, and.right h⟩⟩) (assume h : p ∧ (q ∧ r), have hqr : q ∧ r, from and.right h, show (p ∧ q) ∧ r, from ⟨⟨and.left h, and.left hqr⟩, and.right hqr⟩) example : (p ∨ q) ∨ r ↔ p ∨ (q ∨ r) := iff.intro (assume h : (p ∨ q) ∨ r, show p ∨ (q ∨ r), from or.elim h (assume hpq : p ∨ q, or.elim hpq (assume hp : p, or.inl hp) (assume hq : q, or.inr (or.inl hq))) (assume hr : r, or.inr (or.inr hr))) (assume h : p ∨ (q ∨ r), show (p ∨ q) ∨ r, from or.elim h (assume hp : p, or.inl (or.inl hp)) (assume hqr : q ∨ r, or.elim hqr (assume hq : q, or.inl (or.inr hq)) (assume hr : r, or.inr hr))) -- distributivity example : p ∧ (q ∨ r) ↔ (p ∧ q) ∨ (p ∧ r) := iff.intro (assume h : p ∧ (q ∨ r), show (p ∧ q) ∨ (p ∧ r), from have hp : p, from and.left h, or.elim (and.right h) (assume hq : q, or.inl ⟨hp, hq⟩) (assume hr : r, or.inr ⟨hp, hr⟩)) (assume h: (p ∧ q) ∨ (p ∧ r), show p ∧ (q ∨ r), from or.elim h (assume hpq : p ∧ q, ⟨and.left hpq, or.inl (and.right hpq)⟩) (assume hpr : p ∧ r, ⟨and.left hpr, or.inr (and.right hpr)⟩)) example : p ∨ (q ∧ r) ↔ (p ∨ q) ∧ (p ∨ r) := iff.intro (assume h : p ∨ (q ∧ r), show (p ∨ q) ∧ (p ∨ r), from or.elim h (assume hp : p, ⟨or.inl hp, or.inl hp⟩) (assume hqr : q ∧ r, have hq : q, from hqr.left, have hr : r, from hqr.right, ⟨or.inr hq, or.inr hr⟩)) (assume h : (p ∨ q) ∧ (p ∨ r), show p ∨ (q ∧ r), from have hpq : p ∨ q, from h.left, have hpr : p ∨ r, from h.right, or.elim hpq (assume hp : p, or.inl hp) (assume hq : q, or.elim hpr (assume hp : p, or.inl hp) (assume hr : r, or.inr ⟨hq, hr⟩))) -- other properties example : (p → (q → r)) ↔ (p ∧ q → r) := iff.intro (assume h : (p → (q → r)), show (p ∧ q → r), from assume hpq : p ∧ q, h hpq.left hpq.right) (assume h : (p ∧ q → r), show (p → (q → r)), from assume hp : p, assume hq : q, h ⟨hp, hq⟩) example : ((p ∨ q) → r) ↔ (p → r) ∧ (q → r) := iff.intro (assume h : ((p ∨ q) → r), show (p → r) ∧ (q → r), from and.intro (assume hp : p, h (or.inl hp)) (assume hq : q, h (or.inr hq))) (assume h : (p → r) ∧ (q → r), show ((p ∨ q) → r), from assume hpq : p ∨ q, or.elim hpq (assume hp : p, h.left hp) (assume hq : q, h.right hq)) theorem dem1 : ¬(p ∨ q) ↔ ¬p ∧ ¬q := iff.intro (assume h : ¬(p ∨ q), show ¬p ∧ ¬q, from and.intro (show ¬p, from (assume hp : p, h (or.inl hp))) (show ¬q, from (assume hq : q, h (or.inr hq)))) (assume h : ¬p ∧ ¬q, show ¬(p ∨ q), from (assume hpq : p ∨ q, or.elim hpq (assume hp : p, absurd hp h.left) (assume hq : q, absurd hq h.right))) theorem dem2 : ¬p ∨ ¬q → ¬(p ∧ q) := assume h : ¬p ∨ ¬q, show ¬(p ∧ q), from (assume hpq : p ∧ q, or.elim h (assume hnp : ¬p, absurd hpq.left hnp) (assume hnq : ¬q, absurd hpq.right hnq)) example : ¬(p ∧ ¬p) := show ¬(p ∧ ¬p), from assume hpnp : p ∧ ¬p, absurd hpnp.left hpnp.right example : p ∧ ¬q → ¬(p → q) := assume h : p ∧ ¬q, show ¬(p → q), from assume hnpiq : p → q, absurd (hnpiq h.left) h.right example : ¬p → (p → q) := assume hnp : ¬p, show p → q, from assume hp : p, absurd hp hnp example : (¬p ∨ q) → (p → q) := assume h : (¬p ∨ q), show (p → q), from (assume hp : p, or.elim h (assume hnp : ¬p, absurd hp hnp) (assume hq : q, hq)) example : p ∨ false ↔ p := iff.intro (assume h : p ∨ false, show p, from or.elim h (assume hp : p, hp) false.elim) (assume hp : p, show p ∨ false, from or.inl hp) example : p ∧ false ↔ false := iff.intro (assume h : p ∧ false, show false, from h.right) false.elim -- with classical (because couldn't figure out how to do it without, for a while) -- p ¬(p ↔ ¬p) -- 1 1 -- 0 1 example : ¬(p ↔ ¬p) := assume hneg : p ↔ ¬p, or.elim (classical.em p) (assume hp : p, absurd hp (hneg.mp hp)) (assume hnp : ¬p, absurd (hneg.mpr hnp) hnp) -- without classical example : ¬(p ↔ ¬p) := assume h : p ↔ ¬p, have hnp : ¬p, from (show p → false, from assume hp : p, absurd hp (h.mp hp)), absurd (h.mpr hnp) hnp example : (p → q) → (¬q → ¬p) := assume h : p → q, show ¬q → ¬p, from -- (q → false) → (p → false) assume hnq : ¬q, show ¬p, from assume hp : p, absurd (h hp) hnq -- these require classical reasoning open classical example : (p → r ∨ s) → ((p → r) ∨ (p → s)) := assume h : p → r ∨ s, show (p → r) ∨ (p → s), from or.elim (em p) (assume hp : p, have hrs : r ∨ s, from h hp, or.elim hrs (assume hr : r, show (p → r) ∨ (p → s), from suffices hpr : p → r, from or.inl hpr, show (p → r), from assume hp : p, hr) (assume hs : s, show (p → r) ∨ (p → s), from suffices hps : p → s, from or.inr hps, show (p → s), from assume hp : p, hs)) (assume hnp : ¬p, show (p → r) ∨ (p → s), from suffices hpr : p → r, from or.inl hpr, show (p → r), from assume hp : p, absurd hp hnp) example : ¬(p ∧ q) → ¬p ∨ ¬q := assume h : ¬(p ∧ q), show ¬p ∨ ¬q, from or.elim (em p) (assume hp : p, or.elim (em q) (assume hq : q, show ¬p ∨ ¬q, from false.elim (h ⟨hp, hq⟩)) or.inr) or.inl example : ¬(p → q) → p ∧ ¬q := assume h : ¬(p → q), show p ∧ ¬q, from or.elim (em q) (assume hq : q, suffices hneg : p → q, from false.elim (h hneg), assume hp : p, hq) (assume hnq : ¬q, or.elim (em p) (assume hp : p, ⟨hp, hnq⟩) (assume hnp : ¬p, suffices hneg : p → q, from false.elim (h hneg), assume hp : p, absurd hp hnp)) -- p q ¬p ∨ q -- 1 1 1 -- 1 0 0 -- 0 1 1 -- 0 0 1 example : (p → q) → (¬p ∨ q) := assume h : p → q, show ¬p ∨ q, from or.elim (em q) (assume hq : q, or.inr hq) (assume hnq : ¬q, show ¬p ∨ q, from or.elim (em p) (assume hp : p, absurd (h hp) hnq) (assume hnp : ¬p, or.inl hnp)) -- p q ¬q → ¬p p → q -- 1 1 1 1 -- 1 0 0 0 -- 0 1 1 1 -- 0 0 1 1 example : (¬q → ¬p) → (p → q) := assume h : ¬q → ¬p, show p → q, from or.elim (em p) (assume hp : p, or.elim (em q) (assume hq : q, show p → q, from assume hp : p, hq) (assume hnq : ¬q, suffices hneg : ¬(¬q → ¬p), from absurd h hneg, absurd hp (h hnq))) (assume hnp : ¬p, assume hp : p, absurd hp hnp) example : p ∨ ¬p := em p example : (((p → q) → p) → p) := assume h1 : (p → q) → p, show p, from or.elim (em p) id (assume hnp : ¬p, have hpq : p → q, from (assume hp : p, absurd hp hnp), absurd (h1 hpq) hnp)
ed6407f4d4cd1533dd86fbebc2ad14a8abaaff89
6432ea7a083ff6ba21ea17af9ee47b9c371760f7
/src/Lean/Compiler/LCNF/Specialize.lean
1149e1ddea7b020214c604a89a79e3520ab84157
[ "Apache-2.0", "LLVM-exception", "NCSA", "LGPL-3.0-only", "LicenseRef-scancode-inner-net-2.0", "BSD-3-Clause", "LGPL-2.0-or-later", "Spencer-94", "LGPL-2.1-or-later", "HPND", "LicenseRef-scancode-pcre", "ISC", "LGPL-2.1-only", "LicenseRef-scancode-other-permissive", "SunPro", "CMU-Mach"...
permissive
leanprover/lean4
4bdf9790294964627eb9be79f5e8f6157780b4cc
f1f9dc0f2f531af3312398999d8b8303fa5f096b
refs/heads/master
1,693,360,665,786
1,693,350,868,000
1,693,350,868,000
129,571,436
2,827
311
Apache-2.0
1,694,716,156,000
1,523,760,560,000
Lean
UTF-8
Lean
false
false
14,444
lean
/- Copyright (c) 2022 Microsoft Corporation. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Leonardo de Moura -/ import Lean.Compiler.Specialize import Lean.Compiler.LCNF.Simp import Lean.Compiler.LCNF.SpecInfo import Lean.Compiler.LCNF.PrettyPrinter import Lean.Compiler.LCNF.ToExpr import Lean.Compiler.LCNF.Level import Lean.Compiler.LCNF.PhaseExt import Lean.Compiler.LCNF.MonadScope import Lean.Compiler.LCNF.Closure import Lean.Compiler.LCNF.FVarUtil namespace Lean.Compiler.LCNF namespace Specialize abbrev Cache := SMap Expr Name structure CacheEntry where key : Expr declName : Name deriving Inhabited def addEntry (cache : Cache) (e : CacheEntry) : Cache := cache.insert e.key e.declName builtin_initialize specCacheExt : SimplePersistentEnvExtension CacheEntry Cache ← registerSimplePersistentEnvExtension { addEntryFn := addEntry addImportedFn := fun es => (mkStateFromImportedEntries addEntry {} es).switch } def cacheSpec (key : Expr) (declName : Name) : CoreM Unit := modifyEnv fun env => specCacheExt.addEntry env { key, declName } def findSpecCache? (key : Expr) : CoreM (Option Name) := return specCacheExt.getState (← getEnv) |>.find? key structure Context where /-- Set of free variables in scope. The "collector" uses this information when collecting dependencies for code specialization. -/ scope : FVarIdSet := {} /-- Set of let-declarations in scope that do not depend on parameters. -/ ground : FVarIdSet := {} /-- Name of the declaration being processed -/ declName : Name structure State where decls : Array Decl := #[] abbrev SpecializeM := ReaderT Context $ StateRefT State CompilerM instance : MonadScope SpecializeM where getScope := return (← read).scope withScope f := withReader (fun ctx => { ctx with scope := f ctx.scope }) /-- Return `true` if `e` is a ground term. That is, it contains only free variables tagged as ground -/ def isGround [TraverseFVar α] (e : α) : SpecializeM Bool := do let s := (← read).ground return allFVar (s.contains ·) e @[inline] def withLetDecl (decl : LetDecl) (x : SpecializeM α) : SpecializeM α := do let grd ← isGround decl.value let fvarId := decl.fvarId withReader (fun { scope, ground, declName } => { declName, scope := scope.insert fvarId, ground := if grd then ground.insert fvarId else ground }) x namespace Collector /-! # Dependency collector for the code specialization function. During code specialization, we select which arguments are going to be used during the specialization. Then, we have to collect their dependencies. For example, suppose are trying to specialize the following `IO.println` and `List.forM` applications in the following example: ``` def f xs a.1 := let _x.2 := @instMonadEIO IO.Error let _x.5 := instToStringString let _x.9 := instToStringNat let _x.6 := "hello" let _x.61 := @IO.println String _x.5 _x.6 a.1 -- (*) cases _x.61 | EStateM.Result.ok a.6 a.7 => fun _f.72 _y.69 _y.70 := let _x.71 := @IO.println Nat _x.9 _y.69 _y.70 -- (*) _x.71 let _x.65 := @List.forM (fun α => PUnit → EStateM.Result IO.Error PUnit α) _x.2 Nat xs _f.72 a.7 -- (*) ... ... ``` For `IO.println` the `SpecArgInfo` is `[N, I, O, O]`, i.e., only the first two arguments are considered for code specialization. The first one is computationally neutral, and the second one is an instance. For `List.forM`, we have `[N, I, N, O, H]`. In this case, the fifth argument (tagged as `H`) is a function. Note that the actual `List.forM` application has 6 arguments, the extra argument comes from the `IO` monad. For the first `IO.println` application, the collector collects `_x.5`. For the `List.forM`, it collects `_x.2`, `_x.9`, and `_f.72`. The collected values are used to construct a key to identify the specialization. Arguments that were not considered are replaced with `lcErased`. The key is used to make sure we don't keep generating the same specialization over and over again. This is not an optimization, it is essential to prevent the code specializer from looping while specializing recursive functions. The keys for these two applications are the terms. ``` @IO.println Nat instToStringNat lcErased lcErased ``` and ``` @List.forM (fun α => PUnit → EStateM.Result IO.Error PUnit α) (@instMonadEIO IO.Error) Nat lcErased (fun _y.69 _y.70 => let _x.71 := @IO.println Nat instToStringNat _y.69 _y.70; _x.71) ``` The keys never contain free variables or loose bound variables. -/ /-- Given the specialization mask `paramsInfo` and the arguments `args`, collect their dependencies, and return an array `mask` of size `paramsInfo.size` s.t. - `mask[i] = some args[i]` if `paramsInfo[i] != .other` - `mask[i] = none`, otherwise. That is, `mask` contains only the arguments that are contributing to the code specialization. We use this information to compute a "key" to uniquely identify the code specialization, and creating the specialized code. -/ def collect (paramsInfo : Array SpecParamInfo) (args : Array Arg) : SpecializeM (Array (Option Arg) × Array Param × Array CodeDecl) := do let ctx ← read let lctx := (← getThe CompilerM.State).lctx let abstract (fvarId : FVarId) : Bool := -- We convert let-declarations that are not ground into parameters !lctx.funDecls.contains fvarId && !ctx.ground.contains fvarId Closure.run (inScope := ctx.scope.contains) (abstract := abstract) do let mut argMask := #[] for paramInfo in paramsInfo, arg in args do match paramInfo with | .other => argMask := argMask.push none | .fixedNeutral | .user | .fixedInst | .fixedHO => argMask := argMask.push (some arg) Closure.collectArg arg return argMask end Collector /-- Return `true` if it is worth using arguments `args` for specialization given the parameter specialization information. -/ def shouldSpecialize (paramsInfo : Array SpecParamInfo) (args : Array Arg) : SpecializeM Bool := do for paramInfo in paramsInfo, arg in args do match paramInfo with | .other => pure () | .fixedNeutral => pure () -- If we want to monomorphize types such as `Array`, we need to change here | .fixedInst | .user => if (← isGround arg) then return true | .fixedHO => return true -- TODO: check whether this is too aggressive return false /-- Convert the given declarations into `Expr`, and "zeta-reduce" them into body. This function is used to compute the key that uniquely identifies an code specialization. -/ def expandCodeDecls (decls : Array CodeDecl) (body : LetValue) : CompilerM Expr := do let xs := decls.map (mkFVar ·.fvarId) let values := decls.map fun | .let decl => decl.value.toExpr | .fun decl | .jp decl => decl.toExpr let rec go (i : Nat) (subst : Array Expr) : Expr := if h : i < values.size then let value := values[i].abstractRange i xs let value := value.instantiateRev subst go (i+1) (subst.push value) else (body.toExpr.abstract xs).instantiateRev subst return go 0 #[] termination_by go => values.size - i /-- Create the "key" that uniquely identifies a code specialization. `params` and `decls` are the declarations collected by the `collect` function above. The result contains the list of universe level parameter names the key that `params`, `decls`, and `body` depends on. We use this information to create the new auxiliary declaration and resulting application. -/ def mkKey (params : Array Param) (decls : Array CodeDecl) (body : LetValue) : CompilerM (Expr × List Name) := do let body ← expandCodeDecls decls body let key := ToExpr.run do ToExpr.withParams params do ToExpr.mkLambdaM params (← ToExpr.abstractM body) return normLevelParams key open Internalize in /-- Specialize `decl` using - `us`: the universe level used to instantiate `decl.name` - `argMask`: arguments that are being used to specialize the declaration. - `params`: new parameters that arguments in `argMask` depend on. - `decls`: local declarations that arguments in `argMask` depend on. - `levelParamsNew`: the universe level parameters for the new declaration. -/ def mkSpecDecl (decl : Decl) (us : List Level) (argMask : Array (Option Arg)) (params : Array Param) (decls : Array CodeDecl) (levelParamsNew : List Name) : SpecializeM Decl := do let nameNew := decl.name ++ `_at_ ++ (← read).declName.eraseMacroScopes ++ (`spec).appendIndexAfter (← get).decls.size /- Recall that we have just retrieved `decl` using `getDecl?`, and it may have free variable identifiers that overlap with the free-variables in `params` and `decls` (i.e., the "closure"). Recall that `params` and `decls` are internalized, but `decl` is not. Thus, we internalize `decl` before glueing these "pieces" together. We erase the internalized information after we are done. -/ let decl ← decl.internalize try go decl nameNew |>.run' {} finally eraseDecl decl where go (decl : Decl) (nameNew : Name) : InternalizeM Decl := do let mut params ← params.mapM internalizeParam let decls ← decls.mapM internalizeCodeDecl for param in decl.params, arg in argMask do if let some arg := arg then let arg ← normArg arg modify fun s => s.insert param.fvarId arg.toExpr else -- Keep the parameter let param := { param with type := param.type.instantiateLevelParamsNoCache decl.levelParams us } params := params.push (← internalizeParam param) for param in decl.params[argMask.size:] do let param := { param with type := param.type.instantiateLevelParamsNoCache decl.levelParams us } params := params.push (← internalizeParam param) let value := decl.instantiateValueLevelParams us let value ← internalizeCode value let value := attachCodeDecls decls value let type ← value.inferType let type ← mkForallParams params type let safe := decl.safe let recursive := decl.recursive let decl := { name := nameNew, levelParams := levelParamsNew, params, type, value, safe, recursive, inlineAttr? := none : Decl } return decl.setLevelParams /-- Given the specialization mask `paramsInfo` and the arguments `args`, return the arguments that have not been considered for specialization. -/ def getRemainingArgs (paramsInfo : Array SpecParamInfo) (args : Array Arg) : Array Arg := Id.run do let mut result := #[] for info in paramsInfo, arg in args do if info matches .other then result := result.push arg return result ++ args[paramsInfo.size:] mutual /-- Try to specialize the function application in the given let-declaration. `k` is the continuation for the let-declaration. -/ partial def specializeApp? (e : LetValue) : SpecializeM (Option LetValue) := do let .const declName us args := e | return none if args.isEmpty then return none if (← Meta.isInstance declName) then return none let some paramsInfo ← getSpecParamInfo? declName | return none unless (← shouldSpecialize paramsInfo args) do return none let some decl ← getDecl? declName | return none trace[Compiler.specialize.candidate] "{e.toExpr}, {paramsInfo}" let (argMask, params, decls) ← Collector.collect paramsInfo args let keyBody := .const declName us (argMask.filterMap id) let (key, levelParamsNew) ← mkKey params decls keyBody trace[Compiler.specialize.candidate] "key: {key}" assert! !key.hasLooseBVars assert! !key.hasFVar let usNew := levelParamsNew.map mkLevelParam let argsNew := params.map (.fvar ·.fvarId) ++ getRemainingArgs paramsInfo args if let some declName ← findSpecCache? key then trace[Compiler.specialize.step] "cached: {declName}" return some (.const declName usNew argsNew) else let specDecl ← mkSpecDecl decl us argMask params decls levelParamsNew trace[Compiler.specialize.step] "new: {specDecl.name}" cacheSpec key specDecl.name specDecl.saveBase let specDecl ← specDecl.etaExpand specDecl.saveBase let specDecl ← specDecl.simp {} let specDecl ← specDecl.simp { etaPoly := true, inlinePartial := true, implementedBy := true } let value ← withReader (fun _ => { declName := specDecl.name }) do withParams specDecl.params <| visitCode specDecl.value let specDecl := { specDecl with value } modify fun s => { s with decls := s.decls.push specDecl } return some (.const specDecl.name usNew argsNew) partial def visitFunDecl (funDecl : FunDecl) : SpecializeM FunDecl := do let value ← withParams funDecl.params <| visitCode funDecl.value funDecl.update' funDecl.type value partial def visitCode (code : Code) : SpecializeM Code := do match code with | .let decl k => let mut decl := decl if let some value ← specializeApp? decl.value then decl ← decl.updateValue value let k ← withLetDecl decl <| visitCode k return code.updateLet! decl k | .fun decl k | .jp decl k => let decl ← visitFunDecl decl let k ← withFVar decl.fvarId <| visitCode k return code.updateFun! decl k | .cases c => let alts ← c.alts.mapMonoM fun alt => match alt with | .default k => return alt.updateCode (← visitCode k) | .alt _ ps k => withParams ps do return alt.updateCode (← visitCode k) return code.updateAlts! alts | .unreach .. | .jmp .. | .return .. => return code end def main (decl : Decl) : SpecializeM Decl := do if (← decl.isTemplateLike) then return decl else let value ← withParams decl.params <| visitCode decl.value return { decl with value } end Specialize partial def Decl.specialize (decl : Decl) : CompilerM (Array Decl) := do let (decl, s) ← Specialize.main decl |>.run { declName := decl.name } |>.run {} return s.decls.push decl def specialize : Pass where phase := .base name := `specialize run := fun decls => do saveSpecParamInfo decls decls.foldlM (init := #[]) fun decls decl => return decls ++ (← decl.specialize) builtin_initialize registerTraceClass `Compiler.specialize (inherited := true) registerTraceClass `Compiler.specialize.candidate registerTraceClass `Compiler.specialize.step end Lean.Compiler.LCNF
6ab7a86ffa4fd8981780ab56a245de990a2210fb
a9d0fb7b0e4f802bd3857b803e6c5c23d87fef91
/tests/lean/coe4.lean
0b990c0962067c9bd0f28d5a915c3afe5052cb69
[ "Apache-2.0" ]
permissive
soonhokong/lean-osx
4a954262c780e404c1369d6c06516161d07fcb40
3670278342d2f4faa49d95b46d86642d7875b47c
refs/heads/master
1,611,410,334,552
1,474,425,686,000
1,474,425,686,000
12,043,103
5
1
null
null
null
null
UTF-8
Lean
false
false
396
lean
structure Functor (A : Type*) := (fn : A → A → A) (inj : ∀ x y, fn x = fn y → x = y) attribute [instance] definition coe_functor_to_fn (A : Type*) : has_coe_to_fun (Functor A) := has_coe_to_fun.mk (A → A → A) Functor.fn constant f : Functor nat check f 0 1 set_option pp.coercions false check f 0 1 set_option pp.coercions true check f 0 1 set_option pp.all true check f 0 1
6f02411e44d5eed6f0b3f5415902d6789e37f127
c777c32c8e484e195053731103c5e52af26a25d1
/src/topology/vector_bundle/constructions.lean
f03647810d1f88fb8c3e83aeca3a7b0a936d9850
[ "Apache-2.0" ]
permissive
kbuzzard/mathlib
2ff9e85dfe2a46f4b291927f983afec17e946eb8
58537299e922f9c77df76cb613910914a479c1f7
refs/heads/master
1,685,313,702,744
1,683,974,212,000
1,683,974,212,000
128,185,277
1
0
null
1,522,920,600,000
1,522,920,600,000
null
UTF-8
Lean
false
false
8,353
lean
/- Copyright © 2022 Nicolò Cavalleri. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Nicolò Cavalleri, Sébastien Gouëzel, Heather Macbeth, Floris van Doorn -/ import topology.fiber_bundle.constructions import topology.vector_bundle.basic /-! # Standard constructions on vector bundles This file contains several standard constructions on vector bundles: * `bundle.trivial.vector_bundle 𝕜 B F`: the trivial vector bundle with scalar field `𝕜` and model fiber `F` over the base `B` * `vector_bundle.prod`: for vector bundles `E₁` and `E₂` with scalar field `𝕜` over a common base, a vector bundle structure on their direct sum `E₁ ×ᵇ E₂` (the notation stands for `λ x, E₁ x × E₂ x`). * `vector_bundle.pullback`: for a vector bundle `E` over `B`, a vector bundle structure on its pullback `f *ᵖ E` by a map `f : B' → B` (the notation is a type synonym for `E ∘ f`). ## Tags Vector bundle, direct sum, pullback -/ noncomputable theory open bundle set fiber_bundle open_locale classical bundle /-! ### The trivial vector bundle -/ namespace bundle.trivial variables (𝕜 : Type*) (B : Type*) (F : Type*) [nontrivially_normed_field 𝕜] [normed_add_comm_group F] [normed_space 𝕜 F] [topological_space B] instance trivialization.is_linear : (trivialization B F).is_linear 𝕜 := { linear := λ x hx, ⟨λ y z, rfl, λ c y, rfl⟩ } variables {𝕜} lemma trivialization.coord_changeL (b : B) : (trivialization B F).coord_changeL 𝕜 (trivialization B F) b = continuous_linear_equiv.refl 𝕜 F := begin ext v, rw [trivialization.coord_changeL_apply'], exacts [rfl, ⟨mem_univ _, mem_univ _⟩] end variables (𝕜) instance vector_bundle : vector_bundle 𝕜 F (bundle.trivial B F) := { trivialization_linear' := begin introsI e he, rw eq_trivialization B F e, apply_instance end, continuous_on_coord_change' := begin introsI e e' he he', unfreezingI { obtain rfl := eq_trivialization B F e }, unfreezingI { obtain rfl := eq_trivialization B F e' }, simp_rw trivialization.coord_changeL, exact continuous_const.continuous_on end } end bundle.trivial /-! ### Direct sum of two vector bundles -/ section variables (𝕜 : Type*) {B : Type*} [nontrivially_normed_field 𝕜] [topological_space B] (F₁ : Type*) [normed_add_comm_group F₁] [normed_space 𝕜 F₁] (E₁ : B → Type*) [topological_space (total_space E₁)] (F₂ : Type*) [normed_add_comm_group F₂] [normed_space 𝕜 F₂] (E₂ : B → Type*) [topological_space (total_space E₂)] namespace trivialization variables {F₁ E₁ F₂ E₂} [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜 (E₁ x)] [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜 (E₂ x)] (e₁ e₁' : trivialization F₁ (π E₁)) (e₂ e₂' : trivialization F₂ (π E₂)) instance prod.is_linear [e₁.is_linear 𝕜] [e₂.is_linear 𝕜] : (e₁.prod e₂).is_linear 𝕜 := { linear := λ x ⟨h₁, h₂⟩, (((e₁.linear 𝕜 h₁).mk' _).prod_map ((e₂.linear 𝕜 h₂).mk' _)).is_linear } @[simp] lemma coord_changeL_prod [e₁.is_linear 𝕜] [e₁'.is_linear 𝕜] [e₂.is_linear 𝕜] [e₂'.is_linear 𝕜] ⦃b⦄ (hb : b ∈ ((e₁.prod e₂).base_set ∩ (e₁'.prod e₂').base_set)) : ((e₁.prod e₂).coord_changeL 𝕜 (e₁'.prod e₂') b : F₁ × F₂ →L[𝕜] F₁ × F₂) = (e₁.coord_changeL 𝕜 e₁' b : F₁ →L[𝕜] F₁).prod_map (e₂.coord_changeL 𝕜 e₂' b) := begin rw [continuous_linear_map.ext_iff, continuous_linear_map.coe_prod_map'], rintro ⟨v₁, v₂⟩, show (e₁.prod e₂).coord_changeL 𝕜 (e₁'.prod e₂') b (v₁, v₂) = (e₁.coord_changeL 𝕜 e₁' b v₁, e₂.coord_changeL 𝕜 e₂' b v₂), rw [e₁.coord_changeL_apply e₁', e₂.coord_changeL_apply e₂', (e₁.prod e₂).coord_changeL_apply'], exacts [rfl, hb, ⟨hb.1.2, hb.2.2⟩, ⟨hb.1.1, hb.2.1⟩] end variables {e₁ e₂} [Π x : B, topological_space (E₁ x)] [Π x : B, topological_space (E₂ x)] [fiber_bundle F₁ E₁] [fiber_bundle F₂ E₂] lemma prod_apply [e₁.is_linear 𝕜] [e₂.is_linear 𝕜] {x : B} (hx₁ : x ∈ e₁.base_set) (hx₂ : x ∈ e₂.base_set) (v₁ : E₁ x) (v₂ : E₂ x) : prod e₁ e₂ ⟨x, (v₁, v₂)⟩ = ⟨x, e₁.continuous_linear_equiv_at 𝕜 x hx₁ v₁, e₂.continuous_linear_equiv_at 𝕜 x hx₂ v₂⟩ := rfl end trivialization open trivialization variables [Π x, add_comm_monoid (E₁ x)] [Π x, module 𝕜 (E₁ x)] [Π x, add_comm_monoid (E₂ x)] [Π x, module 𝕜 (E₂ x)] [Π x : B, topological_space (E₁ x)] [Π x : B, topological_space (E₂ x)] [fiber_bundle F₁ E₁] [fiber_bundle F₂ E₂] /-- The product of two vector bundles is a vector bundle. -/ instance vector_bundle.prod [vector_bundle 𝕜 F₁ E₁] [vector_bundle 𝕜 F₂ E₂] : vector_bundle 𝕜 (F₁ × F₂) (E₁ ×ᵇ E₂) := { trivialization_linear' := begin rintros _ ⟨e₁, e₂, he₁, he₂, rfl⟩, resetI, apply_instance end, continuous_on_coord_change' := begin rintros _ _ ⟨e₁, e₂, he₁, he₂, rfl⟩ ⟨e₁', e₂', he₁', he₂', rfl⟩, resetI, refine (((continuous_on_coord_change 𝕜 e₁ e₁').mono _).prod_mapL 𝕜 ((continuous_on_coord_change 𝕜 e₂ e₂').mono _)).congr _; dsimp only [base_set_prod] with mfld_simps, { mfld_set_tac }, { mfld_set_tac }, { rintro b hb, rw [continuous_linear_map.ext_iff], rintro ⟨v₁, v₂⟩, show (e₁.prod e₂).coord_changeL 𝕜 (e₁'.prod e₂') b (v₁, v₂) = (e₁.coord_changeL 𝕜 e₁' b v₁, e₂.coord_changeL 𝕜 e₂' b v₂), rw [e₁.coord_changeL_apply e₁', e₂.coord_changeL_apply e₂', (e₁.prod e₂).coord_changeL_apply'], exacts [rfl, hb, ⟨hb.1.2, hb.2.2⟩, ⟨hb.1.1, hb.2.1⟩] } end } variables {𝕜 F₁ E₁ F₂ E₂} @[simp] lemma trivialization.continuous_linear_equiv_at_prod {e₁ : trivialization F₁ (π E₁)} {e₂ : trivialization F₂ (π E₂)} [e₁.is_linear 𝕜] [e₂.is_linear 𝕜] {x : B} (hx₁ : x ∈ e₁.base_set) (hx₂ : x ∈ e₂.base_set) : (e₁.prod e₂).continuous_linear_equiv_at 𝕜 x ⟨hx₁, hx₂⟩ = (e₁.continuous_linear_equiv_at 𝕜 x hx₁).prod (e₂.continuous_linear_equiv_at 𝕜 x hx₂) := begin ext1, funext v, obtain ⟨v₁, v₂⟩ := v, rw [(e₁.prod e₂).continuous_linear_equiv_at_apply 𝕜, trivialization.prod], exact (congr_arg prod.snd (prod_apply 𝕜 hx₁ hx₂ v₁ v₂) : _) end end /-! ### Pullbacks of vector bundles -/ section variables (R 𝕜 : Type*) {B : Type*} (F : Type*) (E : B → Type*) {B' : Type*} (f : B' → B) instance [∀ (x : B), add_comm_monoid (E x)] : ∀ (x : B'), add_comm_monoid ((f *ᵖ E) x) := by delta_instance bundle.pullback instance [semiring R] [∀ (x : B), add_comm_monoid (E x)] [∀ x, module R (E x)] : ∀ (x : B'), module R ((f *ᵖ E) x) := by delta_instance bundle.pullback variables {E F} [topological_space B'] [topological_space (total_space E)] [nontrivially_normed_field 𝕜] [normed_add_comm_group F] [normed_space 𝕜 F] [topological_space B] [∀ x, add_comm_monoid (E x)] [∀ x, module 𝕜 (E x)] {K : Type*} [continuous_map_class K B' B] instance trivialization.pullback_linear (e : trivialization F (π E)) [e.is_linear 𝕜] (f : K) : (@trivialization.pullback _ _ _ B' _ _ _ _ _ _ _ e f).is_linear 𝕜 := { linear := λ x h, e.linear 𝕜 h } instance vector_bundle.pullback [∀ x, topological_space (E x)] [fiber_bundle F E] [vector_bundle 𝕜 F E] (f : K) : vector_bundle 𝕜 F ((f : B' → B) *ᵖ E) := { trivialization_linear' := begin rintro _ ⟨e, he, rfl⟩, resetI, apply_instance, end, continuous_on_coord_change' := begin rintro _ _ ⟨e, he, rfl⟩ ⟨e', he', rfl⟩, resetI, refine ((continuous_on_coord_change 𝕜 e e').comp (map_continuous f).continuous_on (λ b hb, hb)).congr _, rintro b (hb : f b ∈ e.base_set ∩ e'.base_set), ext v, show ((e.pullback f).coord_changeL 𝕜 (e'.pullback f) b) v = (e.coord_changeL 𝕜 e' (f b)) v, rw [e.coord_changeL_apply e' hb, (e.pullback f).coord_changeL_apply' _], exacts [rfl, hb] end } end
4cc468693e9dd1617a63930f1a40aaf21595bfed
957a80ea22c5abb4f4670b250d55534d9db99108
/tests/lean/parser_error_recovery.lean
7b09ae2700ddf8f0b3830efc3a3d3792551b353d
[ "Apache-2.0" ]
permissive
GaloisInc/lean
aa1e64d604051e602fcf4610061314b9a37ab8cd
f1ec117a24459b59c6ff9e56a1d09d9e9e60a6c0
refs/heads/master
1,592,202,909,807
1,504,624,387,000
1,504,624,387,000
75,319,626
2
1
Apache-2.0
1,539,290,164,000
1,480,616,104,000
C++
UTF-8
Lean
false
false
643
lean
/- unknown identifiers -/ def f1 : ℕ → ℕ | 42 := 9000 | arg := ag #eval f1 42 -- OK (prints 9000) /- incomplete structure instances -/ def f2 : ℕ × ℕ := { fst := 9000, sn} #reduce f2.fst -- OK (prints 9000) /- incomplete if-then-else -/ def f3 (x : ℕ) : ℕ := (if x ≥ 42 then 9000) -- ^ missing else reported here #eval f3 42 -- OK (prints 9000) /- η-expanded syntax errors -/ def f4 : list ℕ := list.map nat.suc [] #eval f4 -- OK (prints []) /- tactic scripts with syntax errors -/ lemma f5 (x : ℕ) : x+1 = 1+x := by { simp, trace_state, -- OK (no goals) simmp,***, simp a,,,
c136c8e052b1d0ca4979133ef87ad4c754a41c88
5d166a16ae129621cb54ca9dde86c275d7d2b483
/tests/lean/run/unfold_lemmas.lean
426d7f65606f5b443927b1e6843ab8fd7eea93b9
[ "Apache-2.0" ]
permissive
jcarlson23/lean
b00098763291397e0ac76b37a2dd96bc013bd247
8de88701247f54d325edd46c0eed57aeacb64baf
refs/heads/master
1,611,571,813,719
1,497,020,963,000
1,497,021,515,000
93,882,536
1
0
null
1,497,029,896,000
1,497,029,896,000
null
UTF-8
Lean
false
false
339
lean
open nat well_founded def gcd.F : Π (y : ℕ), (Π (y' : ℕ), y' < y → nat → nat) → nat → nat | 0 f x := x | (succ y) f x := f (x % succ y) (mod_lt _ $ succ_pos _) (succ y) def gcd (x y : nat) := fix lt_wf gcd.F y x set_option type_context.unfold_lemmas true @[simp] lemma gcd_zero_right (x : nat) : gcd x 0 = x := rfl
3427d52dea30ab7b3b8dcd412875cf7c3321c08d
a76f677b87d42a9470ba3a0a78cfddd3063118e6
/src/order/sidedness.lean
72a5df7a89a34c41af8e2d87abd87c0b93523ce9
[]
no_license
Ja1941/hilberts-axioms
50219c732ad5fa167408432e8c8baae259777a40
5b653a92e448b77da41c9893066b641bc4e6b316
refs/heads/master
1,693,238,884,856
1,635,702,120,000
1,635,702,120,000
385,546,384
9
1
null
null
null
null
UTF-8
Lean
false
false
25,952
lean
/- Copyright (c) 2021 Tianchen Zhao. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Tianchen Zhao -/ import order.segment /-! # sidedness This file defines sidedness (two points lie on the same side of a line, or same side of a point) using `incidence_order_geometry.between` and apply them to prove some theorems of betweenness. ## Main definitions * `same_side_line` defines a relation that two points lie on the same side of a line, `diff_side_line` meaning that they lie on different side of a line. * `same_side_pt` defines, on a line, two points lie on the same side with respect to a point, `diff_side_pt` meaning the opposite. ## References * See [Geometry: Euclid and Beyond] -/ open_locale classical open set variable [B : incidence_order_geometry] open incidence_geometry incidence_order_geometry include B /--Two points `a` `b` are on the same side of a line if seg `a` `b` doesn't intersects with the line. -/ def same_side_line (l : set pts) (a b : pts) : Prop := ¬(l ♥ (a-ₛb).inside) /--seg `a` `b` intersects with the line and not at `a` or `b`. -/ def diff_side_line (l : set pts) (a b : pts) : Prop := (l ♥ (a-ₛb).inside) ∧ a ∉ l ∧ b ∉ l lemma plane_separation {l : set pts} {a b : pts} (ha : a ∉ l) (hb : b ∉ l) : (same_side_line l a b ∨ diff_side_line l a b) ∧ ¬(same_side_line l a b ∧ diff_side_line l a b) := begin unfold same_side_line diff_side_line, unfold two_pt_seg, split, { apply not_or_of_imp, intro h, exact ⟨h, ha, hb⟩ }, { intro hf, exact hf.1 hf.2.1 } end lemma not_same_side_line {l : set pts} {a b : pts} (ha : a ∉ l) (hb : b ∉ l) : ¬(same_side_line l a b) ↔ (diff_side_line l a b) := begin split, { intro hns, cases (plane_separation ha hb).1 with hs hd, exact absurd hs hns, exact hd }, { intros hd hs, exact absurd (by exact ⟨hs, hd⟩) (plane_separation ha hb).2 } end lemma not_diff_side_line {l : set pts} {a b : pts} (ha : a ∉ l) (hb : b ∉ l) : ¬(diff_side_line l a b) ↔ (same_side_line l a b) := by rw [←not_iff_not.mpr (not_same_side_line ha hb), not_not] lemma same_side_line_refl {l : set pts} {a : pts} (ha : a ∉ l) : same_side_line l a a := begin unfold same_side_line intersect, rw seg_singleton, rw not_nonempty_iff_eq_empty, ext1, simp, intros h hxa, rw hxa at h, exact ha h end lemma same_side_line_symm {l : set pts} {a b : pts} : same_side_line l a b → same_side_line l b a := by {unfold same_side_line, rw seg_symm, simp} lemma diff_side_line_symm {l : set pts} {a b : pts} : diff_side_line l a b → diff_side_line l b a := by {unfold diff_side_line, rw seg_symm, tauto} lemma same_side_line_notin {x y : pts} {l : set pts} : same_side_line l x y → x ∉ l ∧ y ∉ l := begin intro hlxy, unfold same_side_line intersect at hlxy, rw not_nonempty_iff_eq_empty at hlxy, split, { intro hxl, have : x ∈ l ∩ (x-ₛy).inside, simp, exact ⟨hxl, by {unfold two_pt_seg, simp}⟩, rw hlxy at this, exact this }, { intro hyl, have : y ∈ l ∩ (x-ₛy).inside, { simp, exact ⟨hyl, by {unfold two_pt_seg, simp}⟩ }, rw hlxy at this, exact this } end lemma same_side_line_neq {a b x y : pts} : same_side_line (a-ₗb) x y → x ≠ a ∧ x ≠ b := begin intro hxy, split; intro hf; rw hf at hxy, exact (same_side_line_notin hxy).1 (pt_left_in_line a b), exact (same_side_line_notin hxy).1 (pt_right_in_line a b) end lemma same_side_line_neq' {a b x y : pts} : same_side_line (a-ₗb) x y → y ≠ a ∧ y ≠ b := λhxy, same_side_line_neq (same_side_line_symm hxy) lemma same_side_line_noncol {a b c d : pts} : same_side_line (a-ₗb) c d → a ≠ b → noncol a b c ∧ noncol a b d := begin intros hcd hab, split; intro h, exact (same_side_line_notin hcd).1 (col_in12 h hab), exact (same_side_line_notin hcd).2 (col_in12 h hab) end lemma diff_side_line_neq {a b x y : pts} : diff_side_line (a-ₗb) x y → x ≠ a ∧ x ≠ b := begin intro hxy, split; intro hf; rw hf at hxy, exact hxy.2.1 (pt_left_in_line a b), exact hxy.2.1 (pt_right_in_line a b) end lemma diff_side_line_neq' {a b x y : pts} : diff_side_line (a-ₗb) x y → y ≠ a ∧ y ≠ b := λhxy, diff_side_line_neq (diff_side_line_symm hxy) lemma diff_side_line_neq'' {a b : pts} {l : set pts} (hlab : diff_side_line l a b) : a ≠ b := begin have hal := hlab.2.1, intro hab, rw ←hab at hlab, rw ←not_same_side_line at hlab, exact hlab (same_side_line_refl hal), exact hal, exact hal end lemma diff_side_line_noncol {a b c d : pts} : diff_side_line (a-ₗb) c d → a ≠ b → noncol a b c ∧ noncol a b d := λhcd hab, ⟨noncol_in12' hab hcd.2.1, noncol_in12' hab hcd.2.2⟩ private lemma same_side_line_trans_noncol {l : set pts} (hl : l ∈ lines) {a b c : pts} : noncol a b c → same_side_line l a b → same_side_line l b c → same_side_line l a c := begin unfold same_side_line, intros h hlab hlbc, rw seg_symm at hlbc, intro hlac, cases (pasch (noncol23 h) hl (same_side_line_notin hlab).1 (same_side_line_notin hlbc).1 (same_side_line_notin hlab).2 hlac).1 with hf hf, exact hlab hf, exact hlbc hf end lemma same_side_line_trans {l : set pts} (hl : l ∈ lines) {a b c : pts} : same_side_line l a b → same_side_line l b c → same_side_line l a c := begin by_cases col a b c; intros hlab hlbc, by_cases hab : a = b, rw ←hab at hlbc, exact hlbc, by_cases hbc : b = c, rw hbc at hlab, exact hlab, by_cases hac : a = c, rw hac, exact same_side_line_refl (same_side_line_notin hlbc).2, rcases h with ⟨m, hm, ham, hbm, hcm⟩, have hd : ∃ d : pts, d ∈ l ∧ d ∉ m, rcases two_pt_on_one_line hl with ⟨x, y, hxy, hxl, hyl⟩, have hlm : l ≠ m, intro hlm, rw ←hlm at ham, exact (same_side_line_notin hlab).1 ham, by_contra, push_neg at h, exact hxy (two_line_one_pt hl hm hlm hxl (h x hxl) hyl (h y hyl)), rcases hd with ⟨d, hdl, hdm⟩, have habd : noncol a b d, apply noncol_in12' hab, rw two_pt_one_line (line_in_lines hab) hm hab (pt_left_in_line a b) (pt_right_in_line a b) ham hbm, exact hdm, have had := (noncol_neq habd).2.1, cases between_extend had.symm with e hdae, have hae := (between_neq hdae).2.2, have hlae : same_side_line l a e, intro hlae, cases hlae with f hf, simp at hf, have hflae : f ∈ l ∧ f ∈ (a-ₗe), from ⟨hf.1, seg_in_line a e hf.2⟩, have hdlae : d ∈ l ∧ d ∈ (a-ₗe), from ⟨hdl, col_in23 (between_col hdae) hae⟩, have hneq : l ≠ (a-ₗe), intro hf, have := (same_side_line_notin hlab).1, rw hf at this, exact this (pt_left_in_line a e), have hdf := two_line_one_pt hl (line_in_lines (between_neq hdae).2.2) hneq hdlae.1 hdlae.2 hflae.1 hflae.2, rw hdf at hdae, unfold two_pt_seg at hf, simp at hf, have := between_neq hdae, rcases hf.2 with hf | hf | hf, exact this.1 hf, exact this.2.1 hf, exact between_contra.1 ⟨hf, hdae⟩, have hbae := noncol132 (col_noncol (col12 (between_col hdae)) (noncol23 habd) hae), have hebc := noncol132 (col_noncol ⟨m, hm, hbm, ham, hcm⟩ hbae hbc), have haec := noncol23 (col_noncol ⟨m, hm, ham, hbm, hcm⟩ (noncol12 hbae) hac), have hlbe := same_side_line_trans_noncol hl hbae (same_side_line_symm hlab) hlae, have hlec := same_side_line_trans_noncol hl hebc (same_side_line_symm hlbe) hlbc, exact same_side_line_trans_noncol hl haec hlae hlec, exact same_side_line_trans_noncol hl h hlab hlbc end /--Two points `a` `b` are on the same side of the point `o` if they are col and `o` is not in seg `a` `b`. -/ def same_side_pt (o a b : pts) : Prop := o ∉ (a-ₛb).inside ∧ col o a b /--`o` is in seg `a` `b` but is not `a` and `b`. -/ def diff_side_pt (o a b : pts) : Prop := o ∈ (a-ₛb).inside ∧ a ≠ o ∧ b ≠ o lemma same_side_pt_neq {o a b : pts} (hoab : same_side_pt o a b) : a ≠ o ∧ b ≠ o := begin unfold same_side_pt at hoab, unfold two_pt_seg at hoab, split, intro hao, rw hao at hoab, simp at hoab, exact hoab, intro hbo, rw hbo at hoab, simp at hoab, exact hoab end lemma diff_side_pt_col {o a b : pts} : diff_side_pt o a b → col o a b := begin intro hoab, by_cases a = b, rw h, exact ⟨(b-ₗo), line_in_lines hoab.2.2, pt_right_in_line b o, pt_left_in_line b o, pt_left_in_line b o⟩, exact ⟨(a-ₗb), line_in_lines h, (seg_in_line a b) hoab.1, pt_left_in_line a b, pt_right_in_line a b⟩ end theorem line_separation {p a b : pts} (hpab : col p a b) (hap : a ≠ p) (hbp : b ≠ p) : (same_side_pt p a b ∨ diff_side_pt p a b) ∧ ¬(same_side_pt p a b ∧ diff_side_pt p a b) := begin unfold same_side_pt diff_side_pt, split, { by_cases hp : p ∈ (a-ₛb).inside, right, exact ⟨hp, hap, hbp⟩, left, exact ⟨hp, hpab⟩ }, { push_neg, intros h₁ h₂, exact absurd h₂ h₁.1 } end lemma not_same_side_pt {p a b : pts} (hpab : col p a b) (ha : a ≠ p) (hb : b ≠ p) : (¬same_side_pt p a b ↔ diff_side_pt p a b) := begin have := line_separation hpab ha hb, split, intro hs, cases this.1 with h h, exact absurd h hs, exact h, intro hd, cases (not_and_distrib.mp this.2) with h h, exact h, exact absurd hd h end lemma not_diff_side_pt {p a b : pts} (hpab : col p a b) (ha : a ≠ p) (hb : b ≠ p) : (¬diff_side_pt p a b ↔ same_side_pt p a b) := by rw [←not_iff_not.mpr (not_same_side_pt hpab ha hb), not_not] lemma same_side_pt_refl {a b : pts} (hab : a ≠ b) : same_side_pt a b b := begin split, rw seg_singleton, exact hab, exact ⟨a-ₗb, line_in_lines hab, pt_left_in_line a b, pt_right_in_line a b, pt_right_in_line a b⟩ end lemma same_side_pt_symm {a b c : pts} : same_side_pt a b c → same_side_pt a c b := begin unfold same_side_pt, intro habc, split, rw seg_symm, exact habc.1, rcases habc.2 with ⟨l, hl, hal, hbl, hcl⟩, exact ⟨l, hl, hal, hcl, hbl⟩ end lemma diff_side_pt_symm {a b c : pts} : diff_side_pt a b c → diff_side_pt a c b := by { unfold diff_side_pt, rw seg_symm, tauto } lemma same_side_pt_line {a b c : pts} (habc : same_side_pt a b c) {l : set pts} (hl : l ∈ lines) (hal : a ∈ l) (hbl : b ∉ l) (hcl : c ∉ l) : same_side_line l b c := begin by_cases hbc : b = c, rw ←hbc, exact same_side_line_refl hbl, rintros ⟨x, hxl, hxbc⟩, apply habc.1, have hneq : l ≠ (b-ₗc), intro hf, rw hf at hbl, exact hbl (pt_left_in_line b c), rw two_line_one_pt hl (line_in_lines hbc) hneq hal (col_in23 habc.2 hbc) hxl ((seg_in_line b c) hxbc), exact hxbc end lemma between_diff_side_pt {a b c : pts} : between a b c ↔ diff_side_pt b a c := begin split, exact λh, ⟨by {left, exact h}, (between_neq h).1, (between_neq h).2.2.symm⟩, { intro h, rcases h.1 with h | hf | hf, exact h, exact absurd hf h.2.1.symm, exact absurd hf h.2.2.symm } end lemma diff_side_pt_neq' {a b c : pts} (habc : diff_side_pt a b c) : b ≠ c := by { rw ←between_diff_side_pt at habc, exact (between_neq habc).2.1 } lemma diff_side_pt_line {a b c : pts} (habc : diff_side_pt a b c) {l : set pts} (hl : l ∈ lines) (hal : a ∈ l) (hbl : b ∉ l) (hcl : c ∉ l) : diff_side_line l b c := ⟨⟨a, hal, by {left, exact between_diff_side_pt.2 habc}⟩, hbl, hcl⟩ private lemma between_same_side_pt_prep {a b c : pts} : between a b c → same_side_pt a b c := begin intro habc, have hab := (between_neq habc).1, have hac := (between_neq habc).2.1, split, { intro hf, replace hf := seg_in_neq hab hac hf, exact between_contra.1 ⟨habc, hf⟩ }, exact between_col habc end lemma between_same_side_pt {a b c : pts} : between a b c ↔ same_side_pt a b c ∧ same_side_pt c a b := begin split, { intro habc, split, exact between_same_side_pt_prep habc, rw between_symm at habc, exact same_side_pt_symm (between_same_side_pt_prep habc) }, { rintros ⟨habc, hcab⟩, have hab := (same_side_pt_neq habc).1.symm, have hac := (same_side_pt_neq habc).2.symm, have hbc := (same_side_pt_neq hcab).2, rcases between_tri habc.2 hab hac hbc with h | h | h, exact h, exfalso, apply hcab.1, left, exact h, exfalso, apply habc.1, left, exact h } end lemma same_side_line_pt {a b c : pts} (habc : col a b c) (l : set pts) (hl : l ∈ lines) (hal : a ∈ l) (hbl : b ∉ l) (hcl : c ∉ l) (hlbc : same_side_line l b c) : same_side_pt a b c := begin have hba : b ≠ a, intro hf, rw hf at hbl, exact hbl hal, have hca : c ≠ a, intro hf, rw hf at hcl, exact hcl hal, rw ←(not_diff_side_pt habc hba hca), intro hf, have := (diff_side_pt_line hf) hl hal hbl hcl, exact (plane_separation hbl hcl).2 ⟨hlbc, this⟩ end lemma diff_side_line_pt {a b c : pts} (habc : col a b c) (l : set pts) (hl : l ∈ lines) (hal : a ∈ l) (hbl : b ∉ l) (hcl : c ∉ l) (hlbc : diff_side_line l b c) : diff_side_pt a b c := begin have hba : b ≠ a, intro hf, rw hf at hbl, exact hbl hal, have hca : c ≠ a, intro hf, rw hf at hcl, exact hcl hal, rw ←not_same_side_pt habc hba hca, intro hf, have := same_side_pt_line hf hl hal hbl hcl, exact (plane_separation hbl hcl).2 ⟨this, hlbc⟩ end lemma line_pt_exist {a b c : pts} (habc : col a b c) (hab : a ≠ b) (hac : a ≠ c) : ∃ l ∈ lines, a ∈ l ∧ b ∉ l ∧ c ∉ l := begin rcases habc with ⟨l, hl, hal, hbl, hcl⟩, have hd : ∃ d : pts, noncol a b d ∧ d ∉ l, cases noncol_exist hab with d habd, use d, split, exact habd, intro hdl, exact habd ⟨l, hl, hal, hbl, hdl⟩, rcases hd with ⟨d, habd, hdl⟩, have hb : b ∉ (a-ₗd), intro hb, exact habd ⟨(a-ₗd), line_in_lines (noncol_neq habd).2.1, pt_left_in_line a d, hb, pt_right_in_line a d⟩, have hc : c ∉ (a-ₗd), intro hc, rw two_pt_one_line hl (line_in_lines (noncol_neq habd).2.1) hac hal hcl (pt_left_in_line a d) hc at hbl, exact hb hbl, exact ⟨(a-ₗd), line_in_lines (noncol_neq habd).2.1, pt_left_in_line a d, hb, hc⟩ end lemma same_side_pt_trans {a b c d : pts} : same_side_pt a b c → same_side_pt a c d → same_side_pt a b d := begin intros habc hacd, have hac := (same_side_pt_neq habc).2.symm, rcases line_pt_exist habc.2 (same_side_pt_neq habc).1.symm hac with ⟨l, hl, hal, hbl, hcl⟩, have hdl : d ∉ l, intro hdl, rcases hacd.2 with ⟨m, hm, ham, hcm, hdm⟩, rw two_pt_one_line hm hl (same_side_pt_neq hacd).2.symm ham hdm hal hdl at hcm, exact hcl hcm, apply same_side_line_pt, exact col_trans (col23 habc.2) hacd.2 hac, exact hl, exact hal, exact hbl, exact hdl, exact same_side_line_trans hl (same_side_pt_line habc hl hal hbl hcl) (same_side_pt_line hacd hl hal hcl hdl) end lemma between_same_side_pt' {a b c d : pts} (habc : between a b c) (hbcd : same_side_pt b c d) : between a b d := begin have hbc := (between_neq habc).2.2, rw [between_diff_side_pt, ←not_same_side_pt], { intro hf, rw [between_diff_side_pt, ←not_same_side_pt] at habc, exact habc (same_side_pt_trans hf (same_side_pt_symm hbcd)), exact (diff_side_pt_col habc), exact habc.2.1, exact habc.2.2 }, exact col_trans (col123 (between_col habc)) hbcd.2 hbc, exact (between_neq habc).1, exact (same_side_pt_neq hbcd).2 end lemma between_trans {a b c d : pts} : between a b c → between b c d → between a b d ∧ between a c d := begin have : ∀ {a b c d : pts}, between a b c → between b c d → between a b d , intros a b c d habc hbcd, have hab := (between_neq habc).1, have hbc := (between_neq habc).2.2, have hbd := (between_neq hbcd).2.1, rw [between_diff_side_pt, ←not_same_side_pt], { intro hf, have habc' := between_col habc, rw [between_diff_side_pt, ←not_same_side_pt] at habc, apply habc, exact (same_side_pt_trans hf (same_side_pt_symm (between_same_side_pt.1 hbcd).1)), exact col12 habc', exact hab, exact hbc.symm }, exact col_trans (col123 (between_col habc)) (between_col hbcd) hbc, exact hab, exact hbd.symm, intros habc hbcd, split, exact this habc hbcd, rw between_symm at habc hbcd, rw between_symm, exact this hbcd habc end lemma between_trans' {a b c d : pts} : between a b d → between b c d → between a b c ∧ between a c d := begin have : ∀ {a b c d : pts}, between a b d → between b c d → between a b c, intros a b c d habd hbcd, have hab := (between_neq habd).1, have hbc := (between_neq hbcd).1, have hbd := (between_neq hbcd).2.1, rw [between_diff_side_pt, ←not_same_side_pt], { intro hf, have habd' := between_col habd, rw [between_diff_side_pt, ←not_same_side_pt] at habd, apply habd, exact (same_side_pt_trans hf (between_same_side_pt.1 hbcd).1), exact col12 habd', exact hab, exact hbd.symm }, exact col_trans (col123 (between_col habd)) (col23 (between_col hbcd)) hbd, exact hab, exact hbc.symm, intros habd hbcd, have habc := this habd hbcd, exact ⟨habc, (between_trans habc hbcd).2⟩ end lemma same_side_pt_between {a b c : pts} : same_side_pt a b c → b ≠ c → between a b c ∨ between a c b := begin intros habc hbc, rcases between_tri habc.2 (same_side_pt_neq habc).1.symm (same_side_pt_neq habc).2.symm hbc with h | h | h, left, exact h, right, exact h, { rw [between_diff_side_pt, ←not_same_side_pt habc.2] at h, exact absurd habc h, exact (same_side_pt_neq habc).1, exact (same_side_pt_neq habc).2 } end lemma between_same_side_pt_between {a b c d : pts} : between a b c → same_side_pt b c d → between a b d := begin intros habc hbcd, by_cases hcd : c = d, rw hcd at habc, exact habc, cases same_side_pt_between hbcd hcd, exact (between_trans habc h).1, exact (between_trans' habc h).1 end lemma diff_side_pt_cancel {a b c d : pts} : diff_side_pt a b c → diff_side_pt a b d → same_side_pt a c d := begin intros hbac hbad, have hab := hbac.2.1.symm, have hac := hbac.2.2.symm, have hbc := diff_side_pt_neq' hbac, have hbd := diff_side_pt_neq' hbad, by_cases hcd : c = d, rw ←hcd, exact same_side_pt_refl hac, rw ←between_diff_side_pt at hbac hbad, rw [←not_diff_side_pt, ←between_diff_side_pt], { intro hcad, have hbcd := col_trans (between_col hbac) (between_col hbad) hab.symm, rcases between_tri hbcd hbc hbd hcd with hbcd | hbdc | hcbd, exact between_contra.2.1 ⟨(between_trans' hbcd hcad).1, hbac⟩, { rw between_symm at hcad, exact between_contra.2.1 ⟨(between_trans' hbdc hcad).1, hbad⟩ }, { rw between_symm at hbac, exact between_contra.2.1 ⟨(between_trans' hcbd hbad).1, hbac⟩ } }, exact col_trans (col12 (between_col hbac)) (col12 (between_col hbad)) hab, exact (between_neq hbac).2.2.symm, exact (between_neq hbad).2.2.symm, end lemma diff_side_line_cancel {l : set pts} (hl : l ∈ lines) {a b c : pts} : diff_side_line l a b → diff_side_line l b c → same_side_line l a c := begin intros h₁ h₂, have hab : a ≠ b, have hal := h₁.2.1, intro hf, rw [←hf, ←not_same_side_line] at h₁, exact h₁ (same_side_line_refl hal), exact hal, exact hal, by_cases hac : a = c, rw ←hac, exact same_side_line_refl h₁.2.1, have hbc : b ≠ c, have hbl := h₁.2.2, intro hf, rw [←hf, ←not_same_side_line] at h₂, exact h₂ (same_side_line_refl hbl), exact hbl, exact hbl, by_cases habc : col a b c, { cases h₁.1 with x hx, cases h₂.1 with y hy, have hxa : x ≠ a, intro hf, rw hf at hx, exact h₁.2.1 hx.1, have hxb : x ≠ b, intro hf, rw hf at hx, exact h₁.2.2 hx.1, have haxb := seg_in_neq hxa hxb hx.2, have hyb : y ≠ b, intro hf, rw hf at hy, exact h₁.2.2 hy.1, have hyc : y ≠ c, intro hf, rw hf at hy, exact h₂.2.2 hy.1, have hbyc := seg_in_neq hyb hyc hy.2, have hxy : x = y, apply two_line_one_pt hl (line_in_lines hab), intro hf, rw hf at h₁, exact h₁.2.2 (pt_right_in_line a b), exact hx.1, exact (seg_in_line a b) hx.2, exact hy.1, exact col_in21 (col_trans (col123 habc) (col23 (between_col hbyc)) hbc) hab, rw ←hxy at hbyc, apply same_side_pt_line, exact diff_side_pt_cancel (diff_side_pt_symm (between_diff_side_pt.1 haxb)) (between_diff_side_pt.1 hbyc), exact hl, exact hx.1, exact h₁.2.1, exact h₂.2.2 }, by_contra h₃, rw not_same_side_line h₁.2.1 h₂.2.2 at h₃, unfold diff_side_line at h₁ h₂ h₃, exact (pasch habc hl h₁.2.1 h₁.2.2 h₂.2.2 h₁.1).2 ⟨h₃.1, h₂.1⟩ end lemma diff_same_side_line {l : set pts} (hl : l ∈ lines) {a b c : pts} : diff_side_line l a b → same_side_line l b c → diff_side_line l a c := begin intros hlab hlbc, rw ←not_same_side_line hlab.2.1 (same_side_line_notin hlbc).2, rw ←not_same_side_line at hlab, intro hlac, exact hlab (same_side_line_trans hl hlac (same_side_line_symm hlbc)), exact hlab.2.1, exact hlab.2.2 end lemma same_diff_side_line {l : set pts} (hl : l ∈ lines) {a b c : pts} : same_side_line l a b → diff_side_line l b c → diff_side_line l a c := λhlab hlbc, diff_side_line_symm (diff_same_side_line hl (diff_side_line_symm hlbc) (same_side_line_symm hlab)) lemma diff_same_side_pt {a b c d : pts} : diff_side_pt a b c → same_side_pt a b d → diff_side_pt a c d := begin intros habc habd, rw ←not_same_side_pt, rw ←not_same_side_pt at habc, intro hacd, exact habc (same_side_pt_trans habd (same_side_pt_symm hacd)), exact (diff_side_pt_col habc), exact habc.2.1, exact habc.2.2, exact col_trans (diff_side_pt_col habc) habd.2 habc.2.1.symm, exact habc.2.2, exact (same_side_pt_neq habd).2 end private lemma two_pt_seg_pt_prep {a b a' b' : pts} : (a-ₛb) = (a'-ₛb') → a = a' → b = b' := begin intros haba'b' haa', replace haba'b' : (a-ₛb).inside = (a-ₛb').inside, rw [haba'b', ←haa'], by_contra hbb', by_cases hab : a = b, rw hab at haba'b', { rw seg_singleton at haba'b', cases (two_pt_between hbb') with x hbxb', have hx : x ∈ (b-ₛb').inside, unfold two_pt_seg, simp, right, right, exact hbxb', rw ←haba'b' at hx, simp at hx, exact (between_neq hbxb').1 hx.symm }, by_cases hab' : a = b', rw hab' at haba'b', { rw seg_singleton at haba'b', cases (two_pt_between hbb') with x hbxb', have hx : x ∈ (b-ₛb').inside, unfold two_pt_seg, simp, right, right, exact hbxb', rw [seg_symm, haba'b'] at hx, simp at hx, exact (between_neq hbxb').2.2 hx }, by_cases habb' : col a b b', { rcases between_tri habb' hab hab' hbb' with h | h | h, cases (two_pt_between (between_neq h).2.2) with x hbxb', have haxb' := (between_trans' h hbxb').2, have h₁ : x ∈ (a-ₛb').inside, unfold two_pt_seg, simp, right, right, exact haxb', have h₂ : x ∉ (a-ₛb).inside, unfold two_pt_seg, simp, intro hf, rcases hf with hf | hf | hf, exact (between_neq (between_trans' h hbxb').2).1 hf.symm, exact (between_neq hbxb').1 hf.symm, have habx := (between_trans' h hbxb').1, exact between_contra.2.1 ⟨hf, habx⟩, rw haba'b' at h₂, exact absurd h₁ h₂, cases (two_pt_between (between_neq h).2.2) with x hb'xb, have haxb := (between_trans' h hb'xb).2, have h₁ : x ∈ (a-ₛb).inside, unfold two_pt_seg, simp, right, right, exact haxb, have h₂ : x ∉ (a-ₛb').inside, unfold two_pt_seg, simp, intro hf, rcases hf with hf | hf | hf, exact (between_neq (between_trans' h hb'xb).2).1 hf.symm, exact (between_neq hb'xb).1 hf.symm, have hab'x := (between_trans' h hb'xb).1, exact between_contra.2.1 ⟨hf, hab'x⟩, rw haba'b' at h₁, exact absurd h₁ h₂, cases (two_pt_between hab) with x haxb, have h₁ : x ∈ (a-ₛb).inside, unfold two_pt_seg, simp, right, right, exact haxb, have h₂ : x ∉ (a-ₛb').inside, unfold two_pt_seg, simp, intro hf, rw between_symm at h, rcases hf with hf | hf | hf, exact (between_neq haxb).1 hf.symm, exact (between_neq (between_trans' h haxb).2).1 hf.symm, have hb'ax := (between_trans' h haxb).1, rw between_symm at hf, exact between_contra.2.1 ⟨hf, hb'ax⟩, rw haba'b' at h₁, exact absurd h₁ h₂ }, cases (two_pt_between hab) with x haxb, have h : x ∈ (a-ₛb').inside, rw ←haba'b', unfold two_pt_seg, simp, right, right, exact haxb, unfold two_pt_seg at h, simp at h, rcases h with h | h | h, exact absurd h (between_neq haxb).1.symm, rw h at haxb, exact habb' (col23 (between_col haxb)), exact habb' (col_trans (between_col haxb) (between_col h) (between_neq haxb).1) end lemma two_pt_seg_pt {a b a' b' : B.pts} : (a-ₛb) = (a'-ₛb') → (a = a' ∧ b = b') ∨ (a = b' ∧ b = a') := begin intros haba'b', by_cases haa' : a = a', left, exact ⟨haa', two_pt_seg_pt_prep haba'b' haa'⟩, by_cases hab' : a = b', right, rw seg_symm a' b' at haba'b', exact ⟨hab', two_pt_seg_pt_prep haba'b' hab'⟩, have ha'ab' := (seg_in_neq haa' hab' (by {rw ←haba'b', exact pt_left_in_seg a b })), by_cases ha'b : a' = b, right, rw seg_symm at haba'b', exact ⟨two_pt_seg_pt_prep haba'b' ha'b.symm, ha'b.symm⟩, have haa'b := (seg_in_neq (ne.symm haa') ha'b (by {rw haba'b', exact pt_left_in_seg a' b' })), by_cases hbb' : b = b', { rw [seg_symm, seg_symm a' b'] at haba'b', left, exact ⟨two_pt_seg_pt_prep haba'b' hbb', hbb'⟩ }, have ha'bb' := (seg_in_neq (ne.symm ha'b) hbb' (by {rw ←haba'b', exact pt_right_in_seg a b })), rw between_symm at ha'ab', have hb'a'b := (between_trans ha'ab' haa'b).2, rw between_symm at ha'bb', exfalso, exact between_contra.2.1 ⟨hb'a'b, ha'bb'⟩ end